blob: 91aa0cabfb28b79cd89d8d760da3c78ea2dfa3f2 [file] [log] [blame]
Michael Wrightd02c5b62014-02-10 15:10:22 -08001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Dominik Laskowski2f01d772022-03-23 16:01:29 -070017#include <cinttypes>
18#include <memory>
Harry Cuttsf13161a2023-03-08 14:15:49 +000019#include <optional>
Dominik Laskowski2f01d772022-03-23 16:01:29 -070020
Prabir Pradhan2770d242019-09-02 18:07:11 -070021#include <CursorInputMapper.h>
22#include <InputDevice.h>
23#include <InputMapper.h>
24#include <InputReader.h>
Prabir Pradhan1aed8582019-12-30 11:46:51 -080025#include <InputReaderBase.h>
26#include <InputReaderFactory.h>
Arthur Hung6d5b4b22022-01-21 07:21:10 +000027#include <JoystickInputMapper.h>
Prabir Pradhan2770d242019-09-02 18:07:11 -070028#include <KeyboardInputMapper.h>
29#include <MultiTouchInputMapper.h>
Chris Ye1dd2e5c2021-04-04 23:12:41 -070030#include <PeripheralController.h>
Chris Yef59a2f42020-10-16 12:55:26 -070031#include <SensorInputMapper.h>
Prabir Pradhan2770d242019-09-02 18:07:11 -070032#include <SingleTouchInputMapper.h>
33#include <SwitchInputMapper.h>
Prabir Pradhane3b28dd2023-10-06 04:19:29 +000034#include <TestEventMatchers.h>
Prabir Pradhan2770d242019-09-02 18:07:11 -070035#include <TestInputListener.h>
36#include <TouchInputMapper.h>
Prabir Pradhan1aed8582019-12-30 11:46:51 -080037#include <UinputDevice.h>
Chris Ye87143712020-11-10 05:05:58 +000038#include <VibratorInputMapper.h>
Prabir Pradhan2574dfa2019-10-16 16:35:07 -070039#include <android-base/thread_annotations.h>
Byoungho Jungda10dd32023-10-06 17:03:45 +090040#include <com_android_input_flags.h>
Michael Wrighta9cf4192022-12-01 23:46:39 +000041#include <ftl/enum.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080042#include <gtest/gtest.h>
chaviw3277faf2021-05-19 16:45:23 -050043#include <gui/constants.h>
Michael Wrighta9cf4192022-12-01 23:46:39 +000044#include <ui/Rotation.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080045
Siarhei Vishniakou21e96e62022-10-27 10:23:37 -070046#include <thread>
Harry Cuttsa5b71292022-11-28 12:56:17 +000047#include "FakeEventHub.h"
Harry Cutts6b5fbc52022-11-28 16:37:43 +000048#include "FakeInputReaderPolicy.h"
Harry Cuttsb57f1702022-11-28 15:34:22 +000049#include "FakePointerController.h"
Harry Cuttse6512e12022-11-28 18:44:01 +000050#include "InputMapperTest.h"
Harry Cutts144ff542022-11-28 17:41:06 +000051#include "InstrumentedInputReader.h"
Harry Cuttsa5b71292022-11-28 12:56:17 +000052#include "TestConstants.h"
Michael Wrightdde67b82020-10-27 16:09:22 +000053#include "input/DisplayViewport.h"
54#include "input/Input.h"
Michael Wright17db18e2020-06-26 20:51:44 +010055
Michael Wrightd02c5b62014-02-10 15:10:22 -080056namespace android {
57
Dominik Laskowski2f01d772022-03-23 16:01:29 -070058using namespace ftl::flag_operators;
Prabir Pradhan739dca42022-09-09 20:12:01 +000059using testing::AllOf;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -070060using std::chrono_literals::operator""ms;
61
Michael Wrightd02c5b62014-02-10 15:10:22 -080062// Arbitrary display properties.
arthurhungcc7f9802020-04-30 17:55:40 +080063static constexpr int32_t DISPLAY_ID = 0;
Prabir Pradhanc13ff082022-09-08 22:03:30 +000064static const std::string DISPLAY_UNIQUE_ID = "local:1";
arthurhungcc7f9802020-04-30 17:55:40 +080065static constexpr int32_t SECONDARY_DISPLAY_ID = DISPLAY_ID + 1;
Prabir Pradhanc13ff082022-09-08 22:03:30 +000066static const std::string SECONDARY_DISPLAY_UNIQUE_ID = "local:2";
arthurhungcc7f9802020-04-30 17:55:40 +080067static constexpr int32_t DISPLAY_WIDTH = 480;
68static constexpr int32_t DISPLAY_HEIGHT = 800;
69static constexpr int32_t VIRTUAL_DISPLAY_ID = 1;
70static constexpr int32_t VIRTUAL_DISPLAY_WIDTH = 400;
71static constexpr int32_t VIRTUAL_DISPLAY_HEIGHT = 500;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -070072static const char* VIRTUAL_DISPLAY_UNIQUE_ID = "virtual:1";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -070073static constexpr std::optional<uint8_t> NO_PORT = std::nullopt; // no physical port is specified
Michael Wrightd02c5b62014-02-10 15:10:22 -080074
arthurhungcc7f9802020-04-30 17:55:40 +080075static constexpr int32_t FIRST_SLOT = 0;
76static constexpr int32_t SECOND_SLOT = 1;
77static constexpr int32_t THIRD_SLOT = 2;
78static constexpr int32_t INVALID_TRACKING_ID = -1;
79static constexpr int32_t FIRST_TRACKING_ID = 0;
80static constexpr int32_t SECOND_TRACKING_ID = 1;
81static constexpr int32_t THIRD_TRACKING_ID = 2;
Chris Ye3fdbfef2021-01-06 18:45:18 -080082static constexpr int32_t LIGHT_BRIGHTNESS = 0x55000000;
83static constexpr int32_t LIGHT_COLOR = 0x7F448866;
84static constexpr int32_t LIGHT_PLAYER_ID = 2;
arthurhungcc7f9802020-04-30 17:55:40 +080085
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080086static constexpr int32_t ACTION_POINTER_0_DOWN =
87 AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
88static constexpr int32_t ACTION_POINTER_0_UP =
89 AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
90static constexpr int32_t ACTION_POINTER_1_DOWN =
91 AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
92static constexpr int32_t ACTION_POINTER_1_UP =
93 AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
94
Prabir Pradhanb08a0e82023-09-14 22:28:32 +000095static constexpr uint32_t STYLUS_FUSION_SOURCE =
96 AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_BLUETOOTH_STYLUS;
97
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +000098// Minimum timestamp separation between subsequent input events from a Bluetooth device.
99static constexpr nsecs_t MIN_BLUETOOTH_TIMESTAMP_DELTA = ms2ns(4);
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +0000100
Byoungho Jungda10dd32023-10-06 17:03:45 +0900101namespace input_flags = com::android::input::flags;
102
Michael Wrightd02c5b62014-02-10 15:10:22 -0800103template<typename T>
104static inline T min(T a, T b) {
105 return a < b ? a : b;
106}
107
108static inline float avg(float x, float y) {
109 return (x + y) / 2;
110}
111
Chris Ye3fdbfef2021-01-06 18:45:18 -0800112// Mapping for light color name and the light color
113const std::unordered_map<std::string, LightColor> LIGHT_COLORS = {{"red", LightColor::RED},
114 {"green", LightColor::GREEN},
115 {"blue", LightColor::BLUE}};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800116
Michael Wrighta9cf4192022-12-01 23:46:39 +0000117static ui::Rotation getInverseRotation(ui::Rotation orientation) {
Prabir Pradhanc14266f2021-05-12 15:56:24 -0700118 switch (orientation) {
Michael Wrighta9cf4192022-12-01 23:46:39 +0000119 case ui::ROTATION_90:
120 return ui::ROTATION_270;
121 case ui::ROTATION_270:
122 return ui::ROTATION_90;
Prabir Pradhanc14266f2021-05-12 15:56:24 -0700123 default:
124 return orientation;
125 }
126}
127
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800128static void assertAxisResolution(MultiTouchInputMapper& mapper, int axis, float resolution) {
129 InputDeviceInfo info;
Harry Cuttsd02ea102023-03-17 18:21:30 +0000130 mapper.populateDeviceInfo(info);
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800131
132 const InputDeviceInfo::MotionRange* motionRange =
133 info.getMotionRange(axis, AINPUT_SOURCE_TOUCHSCREEN);
134 ASSERT_NEAR(motionRange->resolution, resolution, EPSILON);
135}
136
137static void assertAxisNotPresent(MultiTouchInputMapper& mapper, int axis) {
138 InputDeviceInfo info;
Harry Cuttsd02ea102023-03-17 18:21:30 +0000139 mapper.populateDeviceInfo(info);
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800140
141 const InputDeviceInfo::MotionRange* motionRange =
142 info.getMotionRange(axis, AINPUT_SOURCE_TOUCHSCREEN);
143 ASSERT_EQ(nullptr, motionRange);
144}
145
Siarhei Vishniakou21e96e62022-10-27 10:23:37 -0700146[[maybe_unused]] static void dumpReader(InputReader& reader) {
147 std::string dump;
148 reader.dump(dump);
149 std::istringstream iss(dump);
150 for (std::string line; std::getline(iss, line);) {
151 ALOGE("%s", line.c_str());
152 std::this_thread::sleep_for(std::chrono::milliseconds(1));
153 }
154}
155
Michael Wrightd02c5b62014-02-10 15:10:22 -0800156// --- FakeInputMapper ---
157
158class FakeInputMapper : public InputMapper {
159 uint32_t mSources;
160 int32_t mKeyboardType;
161 int32_t mMetaState;
162 KeyedVector<int32_t, int32_t> mKeyCodeStates;
163 KeyedVector<int32_t, int32_t> mScanCodeStates;
164 KeyedVector<int32_t, int32_t> mSwitchStates;
Philip Junker4af3b3d2021-12-14 10:36:55 +0100165 // fake mapping which would normally come from keyCharacterMap
166 std::unordered_map<int32_t, int32_t> mKeyCodeMapping;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800167 std::vector<int32_t> mSupportedKeyCodes;
Yeabkal Wubshite03e8b12023-06-27 16:23:12 -0700168 std::list<NotifyArgs> mProcessResult;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800169
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700170 std::mutex mLock;
171 std::condition_variable mStateChangedCondition;
172 bool mConfigureWasCalled GUARDED_BY(mLock);
173 bool mResetWasCalled GUARDED_BY(mLock);
174 bool mProcessWasCalled GUARDED_BY(mLock);
175 RawEvent mLastEvent GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800176
Arthur Hungc23540e2018-11-29 20:42:11 +0800177 std::optional<DisplayViewport> mViewport;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800178public:
Arpit Singh8e6fb252023-04-06 11:49:17 +0000179 FakeInputMapper(InputDeviceContext& deviceContext, const InputReaderConfiguration& readerConfig,
180 uint32_t sources)
181 : InputMapper(deviceContext, readerConfig),
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800182 mSources(sources),
183 mKeyboardType(AINPUT_KEYBOARD_TYPE_NONE),
Michael Wrightd02c5b62014-02-10 15:10:22 -0800184 mMetaState(0),
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800185 mConfigureWasCalled(false),
186 mResetWasCalled(false),
187 mProcessWasCalled(false) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800188
Chris Yea52ade12020-08-27 16:49:20 -0700189 virtual ~FakeInputMapper() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800190
191 void setKeyboardType(int32_t keyboardType) {
192 mKeyboardType = keyboardType;
193 }
194
195 void setMetaState(int32_t metaState) {
196 mMetaState = metaState;
197 }
198
Yeabkal Wubshite03e8b12023-06-27 16:23:12 -0700199 // Sets the return value for the `process` call.
200 void setProcessResult(std::list<NotifyArgs> notifyArgs) {
201 mProcessResult.clear();
202 for (auto notifyArg : notifyArgs) {
203 mProcessResult.push_back(notifyArg);
204 }
205 }
206
Michael Wrightd02c5b62014-02-10 15:10:22 -0800207 void assertConfigureWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700208 std::unique_lock<std::mutex> lock(mLock);
209 base::ScopedLockAssertion assumeLocked(mLock);
210 const bool configureCalled =
211 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
212 return mConfigureWasCalled;
213 });
214 if (!configureCalled) {
215 FAIL() << "Expected configure() to have been called.";
216 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800217 mConfigureWasCalled = false;
218 }
219
220 void assertResetWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700221 std::unique_lock<std::mutex> lock(mLock);
222 base::ScopedLockAssertion assumeLocked(mLock);
223 const bool resetCalled =
224 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
225 return mResetWasCalled;
226 });
227 if (!resetCalled) {
228 FAIL() << "Expected reset() to have been called.";
229 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800230 mResetWasCalled = false;
231 }
232
Prabir Pradhanf8d9e442023-12-06 22:06:13 +0000233 void assertResetWasNotCalled() {
234 std::scoped_lock lock(mLock);
235 ASSERT_FALSE(mResetWasCalled) << "Expected reset to not have been called.";
236 }
237
Yi Kong9b14ac62018-07-17 13:48:38 -0700238 void assertProcessWasCalled(RawEvent* outLastEvent = nullptr) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700239 std::unique_lock<std::mutex> lock(mLock);
240 base::ScopedLockAssertion assumeLocked(mLock);
241 const bool processCalled =
242 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
243 return mProcessWasCalled;
244 });
245 if (!processCalled) {
246 FAIL() << "Expected process() to have been called.";
247 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800248 if (outLastEvent) {
249 *outLastEvent = mLastEvent;
250 }
251 mProcessWasCalled = false;
252 }
253
Prabir Pradhanf8d9e442023-12-06 22:06:13 +0000254 void assertProcessWasNotCalled() {
255 std::scoped_lock lock(mLock);
256 ASSERT_FALSE(mProcessWasCalled) << "Expected process to not have been called.";
257 }
258
Michael Wrightd02c5b62014-02-10 15:10:22 -0800259 void setKeyCodeState(int32_t keyCode, int32_t state) {
260 mKeyCodeStates.replaceValueFor(keyCode, state);
261 }
262
263 void setScanCodeState(int32_t scanCode, int32_t state) {
264 mScanCodeStates.replaceValueFor(scanCode, state);
265 }
266
267 void setSwitchState(int32_t switchCode, int32_t state) {
268 mSwitchStates.replaceValueFor(switchCode, state);
269 }
270
271 void addSupportedKeyCode(int32_t keyCode) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800272 mSupportedKeyCodes.push_back(keyCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800273 }
274
Philip Junker4af3b3d2021-12-14 10:36:55 +0100275 void addKeyCodeMapping(int32_t fromKeyCode, int32_t toKeyCode) {
276 mKeyCodeMapping.insert_or_assign(fromKeyCode, toKeyCode);
277 }
278
Michael Wrightd02c5b62014-02-10 15:10:22 -0800279private:
Philip Junker4af3b3d2021-12-14 10:36:55 +0100280 uint32_t getSources() const override { return mSources; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800281
Harry Cuttsd02ea102023-03-17 18:21:30 +0000282 void populateDeviceInfo(InputDeviceInfo& deviceInfo) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800283 InputMapper::populateDeviceInfo(deviceInfo);
284
285 if (mKeyboardType != AINPUT_KEYBOARD_TYPE_NONE) {
Harry Cuttsd02ea102023-03-17 18:21:30 +0000286 deviceInfo.setKeyboardType(mKeyboardType);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800287 }
288 }
289
Arpit Singhed6c3de2023-04-05 19:24:37 +0000290 std::list<NotifyArgs> reconfigure(nsecs_t, const InputReaderConfiguration& config,
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000291 ConfigurationChanges changes) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700292 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800293 mConfigureWasCalled = true;
Arthur Hungc23540e2018-11-29 20:42:11 +0800294
295 // Find the associated viewport if exist.
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800296 const std::optional<uint8_t> displayPort = getDeviceContext().getAssociatedDisplayPort();
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000297 if (displayPort && changes.test(InputReaderConfiguration::Change::DISPLAY_INFO)) {
Arpit Singhed6c3de2023-04-05 19:24:37 +0000298 mViewport = config.getDisplayViewportByPort(*displayPort);
Arthur Hungc23540e2018-11-29 20:42:11 +0800299 }
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700300
301 mStateChangedCondition.notify_all();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700302 return {};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800303 }
304
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700305 std::list<NotifyArgs> reset(nsecs_t) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700306 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800307 mResetWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700308 mStateChangedCondition.notify_all();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700309 return {};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800310 }
311
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700312 std::list<NotifyArgs> process(const RawEvent* rawEvent) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700313 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800314 mLastEvent = *rawEvent;
315 mProcessWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700316 mStateChangedCondition.notify_all();
Yeabkal Wubshite03e8b12023-06-27 16:23:12 -0700317 return mProcessResult;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800318 }
319
Chris Yea52ade12020-08-27 16:49:20 -0700320 int32_t getKeyCodeState(uint32_t, int32_t keyCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800321 ssize_t index = mKeyCodeStates.indexOfKey(keyCode);
322 return index >= 0 ? mKeyCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
323 }
324
Philip Junker4af3b3d2021-12-14 10:36:55 +0100325 int32_t getKeyCodeForKeyLocation(int32_t locationKeyCode) const override {
326 auto it = mKeyCodeMapping.find(locationKeyCode);
327 return it != mKeyCodeMapping.end() ? it->second : locationKeyCode;
328 }
329
Chris Yea52ade12020-08-27 16:49:20 -0700330 int32_t getScanCodeState(uint32_t, int32_t scanCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800331 ssize_t index = mScanCodeStates.indexOfKey(scanCode);
332 return index >= 0 ? mScanCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
333 }
334
Chris Yea52ade12020-08-27 16:49:20 -0700335 int32_t getSwitchState(uint32_t, int32_t switchCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800336 ssize_t index = mSwitchStates.indexOfKey(switchCode);
337 return index >= 0 ? mSwitchStates.valueAt(index) : AKEY_STATE_UNKNOWN;
338 }
339
Chris Yea52ade12020-08-27 16:49:20 -0700340 // Return true if the device has non-empty key layout.
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700341 bool markSupportedKeyCodes(uint32_t, const std::vector<int32_t>& keyCodes,
Chris Yea52ade12020-08-27 16:49:20 -0700342 uint8_t* outFlags) override {
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700343 for (size_t i = 0; i < keyCodes.size(); i++) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800344 for (size_t j = 0; j < mSupportedKeyCodes.size(); j++) {
345 if (keyCodes[i] == mSupportedKeyCodes[j]) {
346 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800347 }
348 }
349 }
Chris Yea52ade12020-08-27 16:49:20 -0700350 bool result = mSupportedKeyCodes.size() > 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800351 return result;
352 }
353
354 virtual int32_t getMetaState() {
355 return mMetaState;
356 }
357
358 virtual void fadePointer() {
359 }
Arthur Hungc23540e2018-11-29 20:42:11 +0800360
361 virtual std::optional<int32_t> getAssociatedDisplay() {
362 if (mViewport) {
363 return std::make_optional(mViewport->displayId);
364 }
365 return std::nullopt;
366 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800367};
368
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700369// --- InputReaderPolicyTest ---
370class InputReaderPolicyTest : public testing::Test {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -0700371protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700372 sp<FakeInputReaderPolicy> mFakePolicy;
373
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -0700374 void SetUp() override { mFakePolicy = sp<FakeInputReaderPolicy>::make(); }
Chris Yea52ade12020-08-27 16:49:20 -0700375 void TearDown() override { mFakePolicy.clear(); }
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700376};
377
378/**
379 * Check that empty set of viewports is an acceptable configuration.
380 * Also try to get internal viewport two different ways - by type and by uniqueId.
381 *
382 * There will be confusion if two viewports with empty uniqueId and identical type are present.
383 * Such configuration is not currently allowed.
384 */
385TEST_F(InputReaderPolicyTest, Viewports_GetCleared) {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -0700386 static const std::string uniqueId = "local:0";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700387
388 // We didn't add any viewports yet, so there shouldn't be any.
389 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100390 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700391 ASSERT_FALSE(internalViewport);
392
393 // Add an internal viewport, then clear it
Michael Wrighta9cf4192022-12-01 23:46:39 +0000394 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +0000395 /*isActive=*/true, uniqueId, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700396
397 // Check matching by uniqueId
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700398 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700399 ASSERT_TRUE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100400 ASSERT_EQ(ViewportType::INTERNAL, internalViewport->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700401
402 // Check matching by viewport type
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100403 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700404 ASSERT_TRUE(internalViewport);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700405 ASSERT_EQ(uniqueId, internalViewport->uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700406
407 mFakePolicy->clearViewports();
408 // Make sure nothing is found after clear
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700409 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700410 ASSERT_FALSE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100411 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700412 ASSERT_FALSE(internalViewport);
413}
414
415TEST_F(InputReaderPolicyTest, Viewports_GetByType) {
416 const std::string internalUniqueId = "local:0";
417 const std::string externalUniqueId = "local:1";
418 const std::string virtualUniqueId1 = "virtual:2";
419 const std::string virtualUniqueId2 = "virtual:3";
420 constexpr int32_t virtualDisplayId1 = 2;
421 constexpr int32_t virtualDisplayId2 = 3;
422
423 // Add an internal viewport
Michael Wrighta9cf4192022-12-01 23:46:39 +0000424 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +0000425 /*isActive=*/true, internalUniqueId, NO_PORT,
Michael Wrighta9cf4192022-12-01 23:46:39 +0000426 ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700427 // Add an external viewport
Michael Wrighta9cf4192022-12-01 23:46:39 +0000428 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +0000429 /*isActive=*/true, externalUniqueId, NO_PORT,
Michael Wrighta9cf4192022-12-01 23:46:39 +0000430 ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700431 // Add an virtual viewport
432 mFakePolicy->addDisplayViewport(virtualDisplayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +0000433 ui::ROTATION_0, /*isActive=*/true, virtualUniqueId1, NO_PORT,
Michael Wrighta9cf4192022-12-01 23:46:39 +0000434 ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700435 // Add another virtual viewport
436 mFakePolicy->addDisplayViewport(virtualDisplayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +0000437 ui::ROTATION_0, /*isActive=*/true, virtualUniqueId2, NO_PORT,
Michael Wrighta9cf4192022-12-01 23:46:39 +0000438 ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700439
440 // Check matching by type for internal
441 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100442 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700443 ASSERT_TRUE(internalViewport);
444 ASSERT_EQ(internalUniqueId, internalViewport->uniqueId);
445
446 // Check matching by type for external
447 std::optional<DisplayViewport> externalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100448 mFakePolicy->getDisplayViewportByType(ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700449 ASSERT_TRUE(externalViewport);
450 ASSERT_EQ(externalUniqueId, externalViewport->uniqueId);
451
452 // Check matching by uniqueId for virtual viewport #1
453 std::optional<DisplayViewport> virtualViewport1 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700454 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700455 ASSERT_TRUE(virtualViewport1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100456 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport1->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700457 ASSERT_EQ(virtualUniqueId1, virtualViewport1->uniqueId);
458 ASSERT_EQ(virtualDisplayId1, virtualViewport1->displayId);
459
460 // Check matching by uniqueId for virtual viewport #2
461 std::optional<DisplayViewport> virtualViewport2 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700462 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700463 ASSERT_TRUE(virtualViewport2);
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100464 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport2->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700465 ASSERT_EQ(virtualUniqueId2, virtualViewport2->uniqueId);
466 ASSERT_EQ(virtualDisplayId2, virtualViewport2->displayId);
467}
468
469
470/**
471 * We can have 2 viewports of the same kind. We can distinguish them by uniqueId, and confirm
472 * that lookup works by checking display id.
473 * Check that 2 viewports of each kind is possible, for all existing viewport types.
474 */
475TEST_F(InputReaderPolicyTest, Viewports_TwoOfSameType) {
476 const std::string uniqueId1 = "uniqueId1";
477 const std::string uniqueId2 = "uniqueId2";
478 constexpr int32_t displayId1 = 2;
479 constexpr int32_t displayId2 = 3;
480
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100481 std::vector<ViewportType> types = {ViewportType::INTERNAL, ViewportType::EXTERNAL,
482 ViewportType::VIRTUAL};
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700483 for (const ViewportType& type : types) {
484 mFakePolicy->clearViewports();
485 // Add a viewport
Michael Wrighta9cf4192022-12-01 23:46:39 +0000486 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +0000487 /*isActive=*/true, uniqueId1, NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700488 // Add another viewport
Michael Wrighta9cf4192022-12-01 23:46:39 +0000489 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +0000490 /*isActive=*/true, uniqueId2, NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700491
492 // Check that correct display viewport was returned by comparing the display IDs.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700493 std::optional<DisplayViewport> viewport1 =
494 mFakePolicy->getDisplayViewportByUniqueId(uniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700495 ASSERT_TRUE(viewport1);
496 ASSERT_EQ(displayId1, viewport1->displayId);
497 ASSERT_EQ(type, viewport1->type);
498
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700499 std::optional<DisplayViewport> viewport2 =
500 mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700501 ASSERT_TRUE(viewport2);
502 ASSERT_EQ(displayId2, viewport2->displayId);
503 ASSERT_EQ(type, viewport2->type);
504
505 // When there are multiple viewports of the same kind, and uniqueId is not specified
506 // in the call to getDisplayViewport, then that situation is not supported.
507 // The viewports can be stored in any order, so we cannot rely on the order, since that
508 // is just implementation detail.
509 // However, we can check that it still returns *a* viewport, we just cannot assert
510 // which one specifically is returned.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700511 std::optional<DisplayViewport> someViewport = mFakePolicy->getDisplayViewportByType(type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700512 ASSERT_TRUE(someViewport);
513 }
514}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800515
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700516/**
Michael Wrightdde67b82020-10-27 16:09:22 +0000517 * When we have multiple internal displays make sure we always return the default display when
518 * querying by type.
519 */
520TEST_F(InputReaderPolicyTest, Viewports_ByTypeReturnsDefaultForInternal) {
521 const std::string uniqueId1 = "uniqueId1";
522 const std::string uniqueId2 = "uniqueId2";
523 constexpr int32_t nonDefaultDisplayId = 2;
524 static_assert(nonDefaultDisplayId != ADISPLAY_ID_DEFAULT,
525 "Test display ID should not be ADISPLAY_ID_DEFAULT");
526
527 // Add the default display first and ensure it gets returned.
528 mFakePolicy->clearViewports();
529 mFakePolicy->addDisplayViewport(ADISPLAY_ID_DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +0000530 ui::ROTATION_0, /*isActive=*/true, uniqueId1, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +0000531 ViewportType::INTERNAL);
532 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +0000533 ui::ROTATION_0, /*isActive=*/true, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +0000534 ViewportType::INTERNAL);
535
536 std::optional<DisplayViewport> viewport =
537 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
538 ASSERT_TRUE(viewport);
539 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
540 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
541
542 // Add the default display second to make sure order doesn't matter.
543 mFakePolicy->clearViewports();
544 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +0000545 ui::ROTATION_0, /*isActive=*/true, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +0000546 ViewportType::INTERNAL);
547 mFakePolicy->addDisplayViewport(ADISPLAY_ID_DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +0000548 ui::ROTATION_0, /*isActive=*/true, uniqueId1, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +0000549 ViewportType::INTERNAL);
550
551 viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
552 ASSERT_TRUE(viewport);
553 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
554 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
555}
556
557/**
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700558 * Check getDisplayViewportByPort
559 */
560TEST_F(InputReaderPolicyTest, Viewports_GetByPort) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100561 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700562 const std::string uniqueId1 = "uniqueId1";
563 const std::string uniqueId2 = "uniqueId2";
564 constexpr int32_t displayId1 = 1;
565 constexpr int32_t displayId2 = 2;
566 const uint8_t hdmi1 = 0;
567 const uint8_t hdmi2 = 1;
568 const uint8_t hdmi3 = 2;
569
570 mFakePolicy->clearViewports();
571 // Add a viewport that's associated with some display port that's not of interest.
Michael Wrighta9cf4192022-12-01 23:46:39 +0000572 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +0000573 /*isActive=*/true, uniqueId1, hdmi3, type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700574 // Add another viewport, connected to HDMI1 port
Michael Wrighta9cf4192022-12-01 23:46:39 +0000575 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +0000576 /*isActive=*/true, uniqueId2, hdmi1, type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700577
578 // Check that correct display viewport was returned by comparing the display ports.
579 std::optional<DisplayViewport> hdmi1Viewport = mFakePolicy->getDisplayViewportByPort(hdmi1);
580 ASSERT_TRUE(hdmi1Viewport);
581 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
582 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
583
584 // Check that we can still get the same viewport using the uniqueId
585 hdmi1Viewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
586 ASSERT_TRUE(hdmi1Viewport);
587 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
588 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
589 ASSERT_EQ(type, hdmi1Viewport->type);
590
591 // Check that we cannot find a port with "HDMI2", because we never added one
592 std::optional<DisplayViewport> hdmi2Viewport = mFakePolicy->getDisplayViewportByPort(hdmi2);
593 ASSERT_FALSE(hdmi2Viewport);
594}
595
Michael Wrightd02c5b62014-02-10 15:10:22 -0800596// --- InputReaderTest ---
597
598class InputReaderTest : public testing::Test {
599protected:
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700600 std::unique_ptr<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800601 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700602 std::shared_ptr<FakeEventHub> mFakeEventHub;
Prabir Pradhan28efc192019-11-05 01:10:04 +0000603 std::unique_ptr<InstrumentedInputReader> mReader;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800604
Chris Yea52ade12020-08-27 16:49:20 -0700605 void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700606 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -0700607 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700608 mFakeListener = std::make_unique<TestInputListener>();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800609
Prabir Pradhan28efc192019-11-05 01:10:04 +0000610 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700611 *mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800612 }
613
Chris Yea52ade12020-08-27 16:49:20 -0700614 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700615 mFakeListener.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800616 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800617 }
618
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700619 void addDevice(int32_t eventHubId, const std::string& name,
620 ftl::Flags<InputDeviceClass> classes, const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800621 mFakeEventHub->addDevice(eventHubId, name, classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800622
623 if (configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800624 mFakeEventHub->addConfigurationMap(eventHubId, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800625 }
626 mFakeEventHub->finishDeviceScan();
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000627 mReader->loopOnce();
628 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700629 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
Prabir Pradhane3da4bb2023-04-05 23:51:23 +0000630 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyInputDevicesChangedWasCalled());
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700631 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800632 }
633
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800634 void disableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700635 mFakePolicy->addDisabledDevice(deviceId);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000636 mReader->requestRefreshConfiguration(InputReaderConfiguration::Change::ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700637 }
638
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800639 void enableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700640 mFakePolicy->removeDisabledDevice(deviceId);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000641 mReader->requestRefreshConfiguration(InputReaderConfiguration::Change::ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700642 }
643
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800644 FakeInputMapper& addDeviceWithFakeInputMapper(int32_t deviceId, int32_t eventHubId,
Chris Ye1b0c7342020-07-28 21:57:03 -0700645 const std::string& name,
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700646 ftl::Flags<InputDeviceClass> classes,
647 uint32_t sources,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800648 const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800649 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, name);
Arpit Singh8e6fb252023-04-06 11:49:17 +0000650 FakeInputMapper& mapper =
651 device->addMapper<FakeInputMapper>(eventHubId,
652 mFakePolicy->getReaderConfiguration(), sources);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -0800653 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800654 addDevice(eventHubId, name, classes, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800655 return mapper;
656 }
657};
658
Chris Ye98d3f532020-10-01 21:48:59 -0700659TEST_F(InputReaderTest, PolicyGetInputDevices) {
660 ASSERT_NO_FATAL_FAILURE(addDevice(1, "keyboard", InputDeviceClass::KEYBOARD, nullptr));
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700661 ASSERT_NO_FATAL_FAILURE(addDevice(2, "ignored", ftl::Flags<InputDeviceClass>(0),
Chris Ye98d3f532020-10-01 21:48:59 -0700662 nullptr)); // no classes so device will be ignored
Michael Wrightd02c5b62014-02-10 15:10:22 -0800663
664 // Should also have received a notification describing the new input devices.
Chris Ye98d3f532020-10-01 21:48:59 -0700665 const std::vector<InputDeviceInfo>& inputDevices = mFakePolicy->getInputDevices();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800666 ASSERT_EQ(1U, inputDevices.size());
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800667 ASSERT_EQ(END_RESERVED_ID + 1, inputDevices[0].getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100668 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800669 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
670 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000671 ASSERT_EQ(0U, inputDevices[0].getMotionRanges().size());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800672}
673
Vaibhav Devmurari5fc7d852023-03-17 18:43:33 +0000674TEST_F(InputReaderTest, InputDeviceRecreatedOnSysfsNodeChanged) {
675 ASSERT_NO_FATAL_FAILURE(addDevice(1, "keyboard", InputDeviceClass::KEYBOARD, nullptr));
676 mFakeEventHub->setSysfsRootPath(1, "xyz");
677
678 // Should also have received a notification describing the new input device.
679 ASSERT_EQ(1U, mFakePolicy->getInputDevices().size());
680 InputDeviceInfo inputDevice = mFakePolicy->getInputDevices()[0];
681 ASSERT_EQ(0U, inputDevice.getLights().size());
682
683 RawLightInfo infoMonolight = {.id = 123,
684 .name = "mono_keyboard_backlight",
685 .maxBrightness = 255,
686 .flags = InputLightClass::BRIGHTNESS,
687 .path = ""};
688 mFakeEventHub->addRawLightInfo(/*rawId=*/123, std::move(infoMonolight));
689 mReader->sysfsNodeChanged("xyz");
690 mReader->loopOnce();
691
692 // Should also have received a notification describing the new recreated input device.
693 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
694 inputDevice = mFakePolicy->getInputDevices()[0];
695 ASSERT_EQ(1U, inputDevice.getLights().size());
696}
697
Chris Yee7310032020-09-22 15:36:28 -0700698TEST_F(InputReaderTest, GetMergedInputDevices) {
699 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
700 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
701 // Add two subdevices to device
702 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
703 // Must add at least one mapper or the device will be ignored!
Arpit Singh8e6fb252023-04-06 11:49:17 +0000704 device->addMapper<FakeInputMapper>(eventHubIds[0], mFakePolicy->getReaderConfiguration(),
705 AINPUT_SOURCE_KEYBOARD);
706 device->addMapper<FakeInputMapper>(eventHubIds[1], mFakePolicy->getReaderConfiguration(),
707 AINPUT_SOURCE_KEYBOARD);
Chris Yee7310032020-09-22 15:36:28 -0700708
709 // Push same device instance for next device to be added, so they'll have same identifier.
710 mReader->pushNextDevice(device);
711 mReader->pushNextDevice(device);
712 ASSERT_NO_FATAL_FAILURE(
713 addDevice(eventHubIds[0], "fake1", InputDeviceClass::KEYBOARD, nullptr));
714 ASSERT_NO_FATAL_FAILURE(
715 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
716
717 // Two devices will be merged to one input device as they have same identifier
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000718 ASSERT_EQ(1U, mFakePolicy->getInputDevices().size());
Chris Yee7310032020-09-22 15:36:28 -0700719}
720
Chris Yee14523a2020-12-19 13:46:00 -0800721TEST_F(InputReaderTest, GetMergedInputDevicesEnabled) {
722 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
723 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
724 // Add two subdevices to device
725 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
726 // Must add at least one mapper or the device will be ignored!
Arpit Singh8e6fb252023-04-06 11:49:17 +0000727 device->addMapper<FakeInputMapper>(eventHubIds[0], mFakePolicy->getReaderConfiguration(),
728 AINPUT_SOURCE_KEYBOARD);
729 device->addMapper<FakeInputMapper>(eventHubIds[1], mFakePolicy->getReaderConfiguration(),
730 AINPUT_SOURCE_KEYBOARD);
Chris Yee14523a2020-12-19 13:46:00 -0800731
732 // Push same device instance for next device to be added, so they'll have same identifier.
733 mReader->pushNextDevice(device);
734 mReader->pushNextDevice(device);
735 // Sensor device is initially disabled
736 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1",
737 InputDeviceClass::KEYBOARD | InputDeviceClass::SENSOR,
738 nullptr));
739 // Device is disabled because the only sub device is a sensor device and disabled initially.
740 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
741 ASSERT_FALSE(device->isEnabled());
742 ASSERT_NO_FATAL_FAILURE(
743 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
744 // The merged device is enabled if any sub device is enabled
745 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
746 ASSERT_TRUE(device->isEnabled());
747}
748
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700749TEST_F(InputReaderTest, WhenEnabledChanges_SendsDeviceResetNotification) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800750 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700751 constexpr ftl::Flags<InputDeviceClass> deviceClass(InputDeviceClass::KEYBOARD);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800752 constexpr int32_t eventHubId = 1;
753 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700754 // Must add at least one mapper or the device will be ignored!
Arpit Singh8e6fb252023-04-06 11:49:17 +0000755 device->addMapper<FakeInputMapper>(eventHubId, mFakePolicy->getReaderConfiguration(),
756 AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -0800757 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800758 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700759
Yi Kong9b14ac62018-07-17 13:48:38 -0700760 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700761
762 NotifyDeviceResetArgs resetArgs;
763 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700764 ASSERT_EQ(deviceId, resetArgs.deviceId);
765
766 ASSERT_EQ(device->isEnabled(), true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800767 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000768 mReader->loopOnce();
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700769
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700770 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700771 ASSERT_EQ(deviceId, resetArgs.deviceId);
772 ASSERT_EQ(device->isEnabled(), false);
773
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800774 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000775 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700776 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
777 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasNotCalled());
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700778 ASSERT_EQ(device->isEnabled(), false);
779
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800780 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000781 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700782 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700783 ASSERT_EQ(deviceId, resetArgs.deviceId);
784 ASSERT_EQ(device->isEnabled(), true);
785}
786
Michael Wrightd02c5b62014-02-10 15:10:22 -0800787TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800788 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700789 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800790 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800791 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800792 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800793 AINPUT_SOURCE_KEYBOARD, nullptr);
794 mapper.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800795
796 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(0,
797 AINPUT_SOURCE_ANY, AKEYCODE_A))
798 << "Should return unknown when the device id is >= 0 but unknown.";
799
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800800 ASSERT_EQ(AKEY_STATE_UNKNOWN,
801 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
802 << "Should return unknown when the device id is valid but the sources are not "
803 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800804
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800805 ASSERT_EQ(AKEY_STATE_DOWN,
806 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
807 AKEYCODE_A))
808 << "Should return value provided by mapper when device id is valid and the device "
809 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800810
811 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(-1,
812 AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
813 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
814
815 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getKeyCodeState(-1,
816 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
817 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
818}
819
Philip Junker4af3b3d2021-12-14 10:36:55 +0100820TEST_F(InputReaderTest, GetKeyCodeForKeyLocation_ForwardsRequestsToMappers) {
821 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
822 constexpr int32_t eventHubId = 1;
823 FakeInputMapper& mapper = addDeviceWithFakeInputMapper(deviceId, eventHubId, "keyboard",
824 InputDeviceClass::KEYBOARD,
825 AINPUT_SOURCE_KEYBOARD, nullptr);
826 mapper.addKeyCodeMapping(AKEYCODE_Y, AKEYCODE_Z);
827
828 ASSERT_EQ(AKEYCODE_UNKNOWN, mReader->getKeyCodeForKeyLocation(0, AKEYCODE_Y))
829 << "Should return unknown when the device with the specified id is not found.";
830
831 ASSERT_EQ(AKEYCODE_Z, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_Y))
832 << "Should return correct mapping when device id is valid and mapping exists.";
833
834 ASSERT_EQ(AKEYCODE_A, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_A))
835 << "Should return the location key code when device id is valid and there's no "
836 "mapping.";
837}
838
839TEST_F(InputReaderTest, GetKeyCodeForKeyLocation_NoKeyboardMapper) {
840 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
841 constexpr int32_t eventHubId = 1;
842 FakeInputMapper& mapper = addDeviceWithFakeInputMapper(deviceId, eventHubId, "joystick",
843 InputDeviceClass::JOYSTICK,
844 AINPUT_SOURCE_GAMEPAD, nullptr);
845 mapper.addKeyCodeMapping(AKEYCODE_Y, AKEYCODE_Z);
846
847 ASSERT_EQ(AKEYCODE_UNKNOWN, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_Y))
848 << "Should return unknown when the device id is valid but there is no keyboard mapper";
849}
850
Michael Wrightd02c5b62014-02-10 15:10:22 -0800851TEST_F(InputReaderTest, GetScanCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800852 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700853 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800854 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800855 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800856 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800857 AINPUT_SOURCE_KEYBOARD, nullptr);
858 mapper.setScanCodeState(KEY_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800859
860 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(0,
861 AINPUT_SOURCE_ANY, KEY_A))
862 << "Should return unknown when the device id is >= 0 but unknown.";
863
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800864 ASSERT_EQ(AKEY_STATE_UNKNOWN,
865 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, KEY_A))
866 << "Should return unknown when the device id is valid but the sources are not "
867 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800868
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800869 ASSERT_EQ(AKEY_STATE_DOWN,
870 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
871 KEY_A))
872 << "Should return value provided by mapper when device id is valid and the device "
873 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800874
875 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(-1,
876 AINPUT_SOURCE_TRACKBALL, KEY_A))
877 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
878
879 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getScanCodeState(-1,
880 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, KEY_A))
881 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
882}
883
884TEST_F(InputReaderTest, GetSwitchState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800885 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700886 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800887 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800888 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800889 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800890 AINPUT_SOURCE_KEYBOARD, nullptr);
891 mapper.setSwitchState(SW_LID, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800892
893 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(0,
894 AINPUT_SOURCE_ANY, SW_LID))
895 << "Should return unknown when the device id is >= 0 but unknown.";
896
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800897 ASSERT_EQ(AKEY_STATE_UNKNOWN,
898 mReader->getSwitchState(deviceId, AINPUT_SOURCE_TRACKBALL, SW_LID))
899 << "Should return unknown when the device id is valid but the sources are not "
900 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800901
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800902 ASSERT_EQ(AKEY_STATE_DOWN,
903 mReader->getSwitchState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
904 SW_LID))
905 << "Should return value provided by mapper when device id is valid and the device "
906 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800907
908 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(-1,
909 AINPUT_SOURCE_TRACKBALL, SW_LID))
910 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
911
912 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getSwitchState(-1,
913 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, SW_LID))
914 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
915}
916
917TEST_F(InputReaderTest, MarkSupportedKeyCodes_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800918 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700919 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800920 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800921 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800922 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800923 AINPUT_SOURCE_KEYBOARD, nullptr);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100924
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800925 mapper.addSupportedKeyCode(AKEYCODE_A);
926 mapper.addSupportedKeyCode(AKEYCODE_B);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800927
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700928 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800929 uint8_t flags[4] = { 0, 0, 0, 1 };
930
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700931 ASSERT_FALSE(mReader->hasKeys(0, AINPUT_SOURCE_ANY, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800932 << "Should return false when device id is >= 0 but unknown.";
933 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
934
935 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700936 ASSERT_FALSE(mReader->hasKeys(deviceId, AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800937 << "Should return false when device id is valid but the sources are not supported by "
938 "the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800939 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
940
941 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700942 ASSERT_TRUE(mReader->hasKeys(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800943 keyCodes, flags))
944 << "Should return value provided by mapper when device id is valid and the device "
945 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800946 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
947
948 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700949 ASSERT_FALSE(mReader->hasKeys(-1, AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
950 << "Should return false when the device id is < 0 but the sources are not supported by "
951 "any device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800952 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
953
954 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700955 ASSERT_TRUE(
956 mReader->hasKeys(-1, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
957 << "Should return value provided by mapper when device id is < 0 and one of the "
958 "devices supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800959 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
960}
961
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000962TEST_F(InputReaderTest, LoopOnce_WhenDeviceScanFinished_SendsConfigurationChanged) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800963 constexpr int32_t eventHubId = 1;
Chris Ye1b0c7342020-07-28 21:57:03 -0700964 addDevice(eventHubId, "ignored", InputDeviceClass::KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800965
966 NotifyConfigurationChangedArgs args;
967
968 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(&args));
969 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
970}
971
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000972TEST_F(InputReaderTest, LoopOnce_ForwardsRawEventsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800973 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700974 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000975 constexpr nsecs_t when = 0;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800976 constexpr int32_t eventHubId = 1;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000977 constexpr nsecs_t readTime = 2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800978 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800979 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800980 AINPUT_SOURCE_KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800981
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000982 mFakeEventHub->enqueueEvent(when, readTime, eventHubId, EV_KEY, KEY_A, 1);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000983 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800984 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
985
986 RawEvent event;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800987 ASSERT_NO_FATAL_FAILURE(mapper.assertProcessWasCalled(&event));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000988 ASSERT_EQ(when, event.when);
989 ASSERT_EQ(readTime, event.readTime);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800990 ASSERT_EQ(eventHubId, event.deviceId);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800991 ASSERT_EQ(EV_KEY, event.type);
992 ASSERT_EQ(KEY_A, event.code);
993 ASSERT_EQ(1, event.value);
994}
995
Garfield Tan1c7bc862020-01-28 13:24:04 -0800996TEST_F(InputReaderTest, DeviceReset_RandomId) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800997 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700998 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800999 constexpr int32_t eventHubId = 1;
1000 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Prabir Pradhan42611e02018-11-27 14:04:02 -08001001 // Must add at least one mapper or the device will be ignored!
Arpit Singh8e6fb252023-04-06 11:49:17 +00001002 device->addMapper<FakeInputMapper>(eventHubId, mFakePolicy->getReaderConfiguration(),
1003 AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001004 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001005 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan42611e02018-11-27 14:04:02 -08001006
1007 NotifyDeviceResetArgs resetArgs;
1008 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001009 int32_t prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001010
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001011 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001012 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001013 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001014 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001015 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001016
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001017 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001018 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001019 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001020 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001021 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001022
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001023 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001024 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001025 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001026 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001027 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001028}
1029
Garfield Tan1c7bc862020-01-28 13:24:04 -08001030TEST_F(InputReaderTest, DeviceReset_GenerateIdWithInputReaderSource) {
1031 constexpr int32_t deviceId = 1;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001032 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Garfield Tan1c7bc862020-01-28 13:24:04 -08001033 constexpr int32_t eventHubId = 1;
1034 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1035 // Must add at least one mapper or the device will be ignored!
Arpit Singh8e6fb252023-04-06 11:49:17 +00001036 device->addMapper<FakeInputMapper>(eventHubId, mFakePolicy->getReaderConfiguration(),
1037 AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001038 mReader->pushNextDevice(device);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001039 ASSERT_NO_FATAL_FAILURE(addDevice(deviceId, "fake", deviceClass, nullptr));
1040
1041 NotifyDeviceResetArgs resetArgs;
1042 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1043 ASSERT_EQ(IdGenerator::Source::INPUT_READER, IdGenerator::getSource(resetArgs.id));
1044}
1045
Arthur Hungc23540e2018-11-29 20:42:11 +08001046TEST_F(InputReaderTest, Device_CanDispatchToDisplay) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001047 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001048 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001049 constexpr int32_t eventHubId = 1;
Arthur Hungc23540e2018-11-29 20:42:11 +08001050 const char* DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001051 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1052 FakeInputMapper& mapper =
Arpit Singh8e6fb252023-04-06 11:49:17 +00001053 device->addMapper<FakeInputMapper>(eventHubId, mFakePolicy->getReaderConfiguration(),
1054 AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001055 mReader->pushNextDevice(device);
Arthur Hungc23540e2018-11-29 20:42:11 +08001056
1057 const uint8_t hdmi1 = 1;
1058
1059 // Associated touch screen with second display.
1060 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
1061
1062 // Add default and second display.
Prabir Pradhan28efc192019-11-05 01:10:04 +00001063 mFakePolicy->clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00001064 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +00001065 /*isActive=*/true, "local:0", NO_PORT, ViewportType::INTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08001066 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +00001067 ui::ROTATION_0, /*isActive=*/true, "local:1", hdmi1,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001068 ViewportType::EXTERNAL);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00001069 mReader->requestRefreshConfiguration(InputReaderConfiguration::Change::DISPLAY_INFO);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001070 mReader->loopOnce();
Prabir Pradhan28efc192019-11-05 01:10:04 +00001071
1072 // Add the device, and make sure all of the callbacks are triggered.
1073 // The device is added after the input port associations are processed since
1074 // we do not yet support dynamic device-to-display associations.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001075 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001076 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled());
Prabir Pradhan28efc192019-11-05 01:10:04 +00001077 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001078 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
Arthur Hungc23540e2018-11-29 20:42:11 +08001079
Arthur Hung2c9a3342019-07-23 14:18:59 +08001080 // Device should only dispatch to the specified display.
Arthur Hungc23540e2018-11-29 20:42:11 +08001081 ASSERT_EQ(deviceId, device->getId());
1082 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, DISPLAY_ID));
1083 ASSERT_TRUE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hung2c9a3342019-07-23 14:18:59 +08001084
1085 // Can't dispatch event from a disabled device.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001086 disableDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001087 mReader->loopOnce();
Arthur Hung2c9a3342019-07-23 14:18:59 +08001088 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hungc23540e2018-11-29 20:42:11 +08001089}
1090
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001091TEST_F(InputReaderTest, WhenEnabledChanges_AllSubdevicesAreUpdated) {
1092 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001093 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001094 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1095 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1096 // Must add at least one mapper or the device will be ignored!
Arpit Singh8e6fb252023-04-06 11:49:17 +00001097 device->addMapper<FakeInputMapper>(eventHubIds[0], mFakePolicy->getReaderConfiguration(),
1098 AINPUT_SOURCE_KEYBOARD);
1099 device->addMapper<FakeInputMapper>(eventHubIds[1], mFakePolicy->getReaderConfiguration(),
1100 AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001101 mReader->pushNextDevice(device);
1102 mReader->pushNextDevice(device);
1103 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
1104 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
1105
1106 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
1107
1108 NotifyDeviceResetArgs resetArgs;
1109 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1110 ASSERT_EQ(deviceId, resetArgs.deviceId);
1111 ASSERT_TRUE(device->isEnabled());
1112 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1113 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1114
1115 disableDevice(deviceId);
1116 mReader->loopOnce();
1117
1118 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1119 ASSERT_EQ(deviceId, resetArgs.deviceId);
1120 ASSERT_FALSE(device->isEnabled());
1121 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1122 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1123
1124 enableDevice(deviceId);
1125 mReader->loopOnce();
1126
1127 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1128 ASSERT_EQ(deviceId, resetArgs.deviceId);
1129 ASSERT_TRUE(device->isEnabled());
1130 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1131 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1132}
1133
1134TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToSubdeviceMappers) {
1135 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001136 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001137 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1138 // Add two subdevices to device
1139 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1140 FakeInputMapper& mapperDevice1 =
Arpit Singh8e6fb252023-04-06 11:49:17 +00001141 device->addMapper<FakeInputMapper>(eventHubIds[0],
1142 mFakePolicy->getReaderConfiguration(),
1143 AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001144 FakeInputMapper& mapperDevice2 =
Arpit Singh8e6fb252023-04-06 11:49:17 +00001145 device->addMapper<FakeInputMapper>(eventHubIds[1],
1146 mFakePolicy->getReaderConfiguration(),
1147 AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001148 mReader->pushNextDevice(device);
1149 mReader->pushNextDevice(device);
1150 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
1151 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
1152
1153 mapperDevice1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
1154 mapperDevice2.setKeyCodeState(AKEYCODE_B, AKEY_STATE_DOWN);
1155
1156 ASSERT_EQ(AKEY_STATE_DOWN,
1157 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_A));
1158 ASSERT_EQ(AKEY_STATE_DOWN,
1159 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_B));
1160 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1161 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_C));
1162}
1163
Prabir Pradhan7e186182020-11-10 13:56:45 -08001164TEST_F(InputReaderTest, ChangingPointerCaptureNotifiesInputListener) {
1165 NotifyPointerCaptureChangedArgs args;
1166
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001167 auto request = mFakePolicy->setPointerCapture(true);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00001168 mReader->requestRefreshConfiguration(InputReaderConfiguration::Change::POINTER_CAPTURE);
Prabir Pradhan7e186182020-11-10 13:56:45 -08001169 mReader->loopOnce();
1170 mFakeListener->assertNotifyCaptureWasCalled(&args);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001171 ASSERT_TRUE(args.request.enable) << "Pointer Capture should be enabled.";
1172 ASSERT_EQ(args.request, request) << "Pointer Capture sequence number should match.";
Prabir Pradhan7e186182020-11-10 13:56:45 -08001173
1174 mFakePolicy->setPointerCapture(false);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00001175 mReader->requestRefreshConfiguration(InputReaderConfiguration::Change::POINTER_CAPTURE);
Prabir Pradhan7e186182020-11-10 13:56:45 -08001176 mReader->loopOnce();
1177 mFakeListener->assertNotifyCaptureWasCalled(&args);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001178 ASSERT_FALSE(args.request.enable) << "Pointer Capture should be disabled.";
Prabir Pradhan7e186182020-11-10 13:56:45 -08001179
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001180 // Verify that the Pointer Capture state is not updated when the configuration value
Prabir Pradhan7e186182020-11-10 13:56:45 -08001181 // does not change.
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00001182 mReader->requestRefreshConfiguration(InputReaderConfiguration::Change::POINTER_CAPTURE);
Prabir Pradhan7e186182020-11-10 13:56:45 -08001183 mReader->loopOnce();
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001184 mFakeListener->assertNotifyCaptureWasNotCalled();
Prabir Pradhan7e186182020-11-10 13:56:45 -08001185}
1186
Chris Ye87143712020-11-10 05:05:58 +00001187class FakeVibratorInputMapper : public FakeInputMapper {
1188public:
Arpit Singh8e6fb252023-04-06 11:49:17 +00001189 FakeVibratorInputMapper(InputDeviceContext& deviceContext,
1190 const InputReaderConfiguration& readerConfig, uint32_t sources)
1191 : FakeInputMapper(deviceContext, readerConfig, sources) {}
Chris Ye87143712020-11-10 05:05:58 +00001192
1193 std::vector<int32_t> getVibratorIds() override { return getDeviceContext().getVibratorIds(); }
1194};
1195
1196TEST_F(InputReaderTest, VibratorGetVibratorIds) {
1197 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001198 ftl::Flags<InputDeviceClass> deviceClass =
1199 InputDeviceClass::KEYBOARD | InputDeviceClass::VIBRATOR;
Chris Ye87143712020-11-10 05:05:58 +00001200 constexpr int32_t eventHubId = 1;
1201 const char* DEVICE_LOCATION = "BLUETOOTH";
1202 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1203 FakeVibratorInputMapper& mapper =
Arpit Singh8e6fb252023-04-06 11:49:17 +00001204 device->addMapper<FakeVibratorInputMapper>(eventHubId,
1205 mFakePolicy->getReaderConfiguration(),
1206 AINPUT_SOURCE_KEYBOARD);
Chris Ye87143712020-11-10 05:05:58 +00001207 mReader->pushNextDevice(device);
1208
1209 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
1210 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
1211
1212 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
1213 ASSERT_EQ(mReader->getVibratorIds(deviceId).size(), 2U);
1214}
1215
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001216// --- FakePeripheralController ---
Kim Low03ea0352020-11-06 12:45:07 -08001217
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001218class FakePeripheralController : public PeripheralControllerInterface {
Chris Yee2b1e5c2021-03-10 22:45:12 -08001219public:
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001220 FakePeripheralController(InputDeviceContext& deviceContext) : mDeviceContext(deviceContext) {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08001221
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001222 ~FakePeripheralController() override {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08001223
Andy Chenf9f1a022022-08-29 20:07:10 -04001224 int32_t getEventHubId() const { return getDeviceContext().getEventHubId(); }
1225
Chris Yee2b1e5c2021-03-10 22:45:12 -08001226 void populateDeviceInfo(InputDeviceInfo* deviceInfo) override {}
1227
1228 void dump(std::string& dump) override {}
1229
1230 std::optional<int32_t> getBatteryCapacity(int32_t batteryId) override {
1231 return getDeviceContext().getBatteryCapacity(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08001232 }
1233
Chris Yee2b1e5c2021-03-10 22:45:12 -08001234 std::optional<int32_t> getBatteryStatus(int32_t batteryId) override {
1235 return getDeviceContext().getBatteryStatus(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08001236 }
Chris Ye3fdbfef2021-01-06 18:45:18 -08001237
1238 bool setLightColor(int32_t lightId, int32_t color) override {
1239 getDeviceContext().setLightBrightness(lightId, color >> 24);
1240 return true;
1241 }
1242
1243 std::optional<int32_t> getLightColor(int32_t lightId) override {
1244 std::optional<int32_t> result = getDeviceContext().getLightBrightness(lightId);
1245 if (!result.has_value()) {
1246 return std::nullopt;
1247 }
1248 return result.value() << 24;
1249 }
Chris Yee2b1e5c2021-03-10 22:45:12 -08001250
1251 bool setLightPlayerId(int32_t lightId, int32_t playerId) override { return true; }
1252
1253 std::optional<int32_t> getLightPlayerId(int32_t lightId) override { return std::nullopt; }
1254
1255private:
1256 InputDeviceContext& mDeviceContext;
1257 inline int32_t getDeviceId() { return mDeviceContext.getId(); }
1258 inline InputDeviceContext& getDeviceContext() { return mDeviceContext; }
Andy Chenf9f1a022022-08-29 20:07:10 -04001259 inline InputDeviceContext& getDeviceContext() const { return mDeviceContext; }
Chris Ye3fdbfef2021-01-06 18:45:18 -08001260};
1261
Chris Yee2b1e5c2021-03-10 22:45:12 -08001262TEST_F(InputReaderTest, BatteryGetCapacity) {
1263 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001264 ftl::Flags<InputDeviceClass> deviceClass =
1265 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
Chris Yee2b1e5c2021-03-10 22:45:12 -08001266 constexpr int32_t eventHubId = 1;
1267 const char* DEVICE_LOCATION = "BLUETOOTH";
1268 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001269 FakePeripheralController& controller =
1270 device->addController<FakePeripheralController>(eventHubId);
Chris Yee2b1e5c2021-03-10 22:45:12 -08001271 mReader->pushNextDevice(device);
1272
1273 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
1274
Harry Cuttsa5b71292022-11-28 12:56:17 +00001275 ASSERT_EQ(controller.getBatteryCapacity(FakeEventHub::DEFAULT_BATTERY),
1276 FakeEventHub::BATTERY_CAPACITY);
1277 ASSERT_EQ(mReader->getBatteryCapacity(deviceId), FakeEventHub::BATTERY_CAPACITY);
Chris Yee2b1e5c2021-03-10 22:45:12 -08001278}
1279
1280TEST_F(InputReaderTest, BatteryGetStatus) {
1281 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001282 ftl::Flags<InputDeviceClass> deviceClass =
1283 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
Chris Yee2b1e5c2021-03-10 22:45:12 -08001284 constexpr int32_t eventHubId = 1;
1285 const char* DEVICE_LOCATION = "BLUETOOTH";
1286 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001287 FakePeripheralController& controller =
1288 device->addController<FakePeripheralController>(eventHubId);
Chris Yee2b1e5c2021-03-10 22:45:12 -08001289 mReader->pushNextDevice(device);
1290
1291 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
1292
Harry Cuttsa5b71292022-11-28 12:56:17 +00001293 ASSERT_EQ(controller.getBatteryStatus(FakeEventHub::DEFAULT_BATTERY),
1294 FakeEventHub::BATTERY_STATUS);
1295 ASSERT_EQ(mReader->getBatteryStatus(deviceId), FakeEventHub::BATTERY_STATUS);
Chris Yee2b1e5c2021-03-10 22:45:12 -08001296}
1297
Prabir Pradhane287ecd2022-09-07 21:18:05 +00001298TEST_F(InputReaderTest, BatteryGetDevicePath) {
1299 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1300 ftl::Flags<InputDeviceClass> deviceClass =
1301 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
1302 constexpr int32_t eventHubId = 1;
1303 const char* DEVICE_LOCATION = "BLUETOOTH";
1304 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1305 device->addController<FakePeripheralController>(eventHubId);
1306 mReader->pushNextDevice(device);
1307
1308 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
1309
Harry Cuttsa5b71292022-11-28 12:56:17 +00001310 ASSERT_EQ(mReader->getBatteryDevicePath(deviceId), FakeEventHub::BATTERY_DEVPATH);
Prabir Pradhane287ecd2022-09-07 21:18:05 +00001311}
1312
Chris Ye3fdbfef2021-01-06 18:45:18 -08001313TEST_F(InputReaderTest, LightGetColor) {
1314 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001315 ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD | InputDeviceClass::LIGHT;
Chris Ye3fdbfef2021-01-06 18:45:18 -08001316 constexpr int32_t eventHubId = 1;
1317 const char* DEVICE_LOCATION = "BLUETOOTH";
1318 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001319 FakePeripheralController& controller =
1320 device->addController<FakePeripheralController>(eventHubId);
Chris Ye3fdbfef2021-01-06 18:45:18 -08001321 mReader->pushNextDevice(device);
1322 RawLightInfo info = {.id = 1,
1323 .name = "Mono",
1324 .maxBrightness = 255,
1325 .flags = InputLightClass::BRIGHTNESS,
1326 .path = ""};
Harry Cutts33476232023-01-30 19:57:29 +00001327 mFakeEventHub->addRawLightInfo(/*rawId=*/1, std::move(info));
1328 mFakeEventHub->fakeLightBrightness(/*rawId=*/1, 0x55);
Chris Ye3fdbfef2021-01-06 18:45:18 -08001329
1330 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Chris Ye3fdbfef2021-01-06 18:45:18 -08001331
Harry Cutts33476232023-01-30 19:57:29 +00001332 ASSERT_TRUE(controller.setLightColor(/*lightId=*/1, LIGHT_BRIGHTNESS));
1333 ASSERT_EQ(controller.getLightColor(/*lightId=*/1), LIGHT_BRIGHTNESS);
1334 ASSERT_TRUE(mReader->setLightColor(deviceId, /*lightId=*/1, LIGHT_BRIGHTNESS));
1335 ASSERT_EQ(mReader->getLightColor(deviceId, /*lightId=*/1), LIGHT_BRIGHTNESS);
Chris Ye3fdbfef2021-01-06 18:45:18 -08001336}
1337
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001338// --- InputReaderIntegrationTest ---
1339
1340// These tests create and interact with the InputReader only through its interface.
1341// The InputReader is started during SetUp(), which starts its processing in its own
1342// thread. The tests use linux uinput to emulate input devices.
1343// NOTE: Interacting with the physical device while these tests are running may cause
1344// the tests to fail.
1345class InputReaderIntegrationTest : public testing::Test {
1346protected:
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001347 std::unique_ptr<TestInputListener> mTestListener;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001348 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001349 std::unique_ptr<InputReaderInterface> mReader;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001350
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00001351 std::shared_ptr<FakePointerController> mFakePointerController;
1352
Chris Yea52ade12020-08-27 16:49:20 -07001353 void SetUp() override {
Siarhei Vishniakou31977182022-09-30 08:51:23 -07001354#if !defined(__ANDROID__)
1355 GTEST_SKIP();
1356#endif
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001357 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00001358 mFakePointerController = std::make_shared<FakePointerController>();
1359 mFakePolicy->setPointerController(mFakePointerController);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001360
Arpit Singh440bf652023-08-09 09:23:43 +00001361 setupInputReader();
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001362 }
1363
Chris Yea52ade12020-08-27 16:49:20 -07001364 void TearDown() override {
Siarhei Vishniakou31977182022-09-30 08:51:23 -07001365#if !defined(__ANDROID__)
1366 return;
1367#endif
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001368 ASSERT_EQ(mReader->stop(), OK);
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001369 mReader.reset();
1370 mTestListener.reset();
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001371 mFakePolicy.clear();
1372 }
Prabir Pradhanda20b172022-09-26 17:01:18 +00001373
1374 std::optional<InputDeviceInfo> findDeviceByName(const std::string& name) {
1375 const std::vector<InputDeviceInfo> inputDevices = mFakePolicy->getInputDevices();
1376 const auto& it = std::find_if(inputDevices.begin(), inputDevices.end(),
1377 [&name](const InputDeviceInfo& info) {
1378 return info.getIdentifier().name == name;
1379 });
1380 return it != inputDevices.end() ? std::make_optional(*it) : std::nullopt;
1381 }
Arpit Singh440bf652023-08-09 09:23:43 +00001382
1383 void setupInputReader() {
1384 mTestListener = std::make_unique<TestInputListener>(/*eventHappenedTimeout=*/2000ms,
1385 /*eventDidNotHappenTimeout=*/30ms);
1386
1387 mReader = std::make_unique<InputReader>(std::make_shared<EventHub>(), mFakePolicy,
1388 *mTestListener);
1389 ASSERT_EQ(mReader->start(), OK);
1390
1391 // Since this test is run on a real device, all the input devices connected
1392 // to the test device will show up in mReader. We wait for those input devices to
1393 // show up before beginning the tests.
1394 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1395 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyInputDevicesChangedWasCalled());
1396 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1397 }
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001398};
1399
1400TEST_F(InputReaderIntegrationTest, TestInvalidDevice) {
1401 // An invalid input device that is only used for this test.
1402 class InvalidUinputDevice : public UinputDevice {
1403 public:
Harry Cutts33476232023-01-30 19:57:29 +00001404 InvalidUinputDevice() : UinputDevice("Invalid Device", /*productId=*/99) {}
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001405
1406 private:
1407 void configureDevice(int fd, uinput_user_dev* device) override {}
1408 };
1409
1410 const size_t numDevices = mFakePolicy->getInputDevices().size();
1411
1412 // UinputDevice does not set any event or key bits, so InputReader should not
1413 // consider it as a valid device.
1414 std::unique_ptr<UinputDevice> invalidDevice = createUinputDevice<InvalidUinputDevice>();
1415 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
1416 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
1417 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
1418
1419 invalidDevice.reset();
1420 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
1421 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
1422 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
1423}
1424
1425TEST_F(InputReaderIntegrationTest, AddNewDevice) {
1426 const size_t initialNumDevices = mFakePolicy->getInputDevices().size();
1427
1428 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
1429 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1430 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1431 ASSERT_EQ(initialNumDevices + 1, mFakePolicy->getInputDevices().size());
1432
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001433 const auto device = findDeviceByName(keyboard->getName());
1434 ASSERT_TRUE(device.has_value());
1435 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, device->getKeyboardType());
1436 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, device->getSources());
1437 ASSERT_EQ(0U, device->getMotionRanges().size());
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001438
1439 keyboard.reset();
1440 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1441 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1442 ASSERT_EQ(initialNumDevices, mFakePolicy->getInputDevices().size());
1443}
1444
1445TEST_F(InputReaderIntegrationTest, SendsEventsToInputListener) {
1446 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
1447 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1448
1449 NotifyConfigurationChangedArgs configChangedArgs;
1450 ASSERT_NO_FATAL_FAILURE(
1451 mTestListener->assertNotifyConfigurationChangedWasCalled(&configChangedArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001452 int32_t prevId = configChangedArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001453 nsecs_t prevTimestamp = configChangedArgs.eventTime;
1454
1455 NotifyKeyArgs keyArgs;
1456 keyboard->pressAndReleaseHomeKey();
1457 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
1458 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001459 ASSERT_NE(prevId, keyArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001460 prevId = keyArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001461 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001462 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001463 prevTimestamp = keyArgs.eventTime;
1464
1465 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
1466 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001467 ASSERT_NE(prevId, keyArgs.id);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001468 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001469 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001470}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001471
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001472TEST_F(InputReaderIntegrationTest, ExternalStylusesButtons) {
1473 std::unique_ptr<UinputExternalStylus> stylus = createUinputDevice<UinputExternalStylus>();
1474 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1475
1476 const auto device = findDeviceByName(stylus->getName());
1477 ASSERT_TRUE(device.has_value());
1478
Prabir Pradhana3621852022-10-14 18:57:23 +00001479 // An external stylus with buttons should also be recognized as a keyboard.
1480 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_STYLUS, device->getSources())
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001481 << "Unexpected source " << inputEventSourceToString(device->getSources()).c_str();
1482 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, device->getKeyboardType());
1483
1484 const auto DOWN =
1485 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD));
1486 const auto UP = AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD));
1487
1488 stylus->pressAndReleaseKey(BTN_STYLUS);
1489 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1490 AllOf(DOWN, WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY))));
1491 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1492 AllOf(UP, WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY))));
1493
1494 stylus->pressAndReleaseKey(BTN_STYLUS2);
1495 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1496 AllOf(DOWN, WithKeyCode(AKEYCODE_STYLUS_BUTTON_SECONDARY))));
1497 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1498 AllOf(UP, WithKeyCode(AKEYCODE_STYLUS_BUTTON_SECONDARY))));
1499
1500 stylus->pressAndReleaseKey(BTN_STYLUS3);
1501 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1502 AllOf(DOWN, WithKeyCode(AKEYCODE_STYLUS_BUTTON_TERTIARY))));
1503 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1504 AllOf(UP, WithKeyCode(AKEYCODE_STYLUS_BUTTON_TERTIARY))));
1505}
1506
Prabir Pradhan3c28b942023-08-18 20:02:01 +00001507TEST_F(InputReaderIntegrationTest, KeyboardWithStylusButtons) {
1508 std::unique_ptr<UinputKeyboard> keyboard =
1509 createUinputDevice<UinputKeyboard>("KeyboardWithStylusButtons", /*productId=*/99,
1510 std::initializer_list<int>{KEY_Q, KEY_W, KEY_E,
1511 KEY_R, KEY_T, KEY_Y,
1512 BTN_STYLUS, BTN_STYLUS2,
1513 BTN_STYLUS3});
1514 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1515
1516 const auto device = findDeviceByName(keyboard->getName());
1517 ASSERT_TRUE(device.has_value());
1518
1519 // An alphabetical keyboard that reports stylus buttons should not be recognized as a stylus.
1520 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, device->getSources())
1521 << "Unexpected source " << inputEventSourceToString(device->getSources()).c_str();
1522 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_ALPHABETIC, device->getKeyboardType());
1523}
1524
Prabir Pradhan37a819b2023-08-22 23:20:16 +00001525TEST_F(InputReaderIntegrationTest, HidUsageKeyboardIsNotAStylus) {
1526 // Create a Uinput keyboard that simulates a keyboard that can report HID usage codes. The
1527 // hid-input driver reports HID usage codes using the value for EV_MSC MSC_SCAN event.
1528 std::unique_ptr<UinputKeyboardWithHidUsage> keyboard =
1529 createUinputDevice<UinputKeyboardWithHidUsage>(
1530 std::initializer_list<int>{KEY_VOLUMEUP, KEY_VOLUMEDOWN});
1531 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1532
1533 const auto device = findDeviceByName(keyboard->getName());
1534 ASSERT_TRUE(device.has_value());
1535
1536 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, device->getSources())
1537 << "Unexpected source " << inputEventSourceToString(device->getSources()).c_str();
1538
1539 // If a device supports reporting HID usage codes, it shouldn't automatically support
1540 // stylus keys.
1541 const std::vector<int> keycodes{AKEYCODE_STYLUS_BUTTON_PRIMARY};
1542 uint8_t outFlags[] = {0};
1543 ASSERT_TRUE(mReader->hasKeys(device->getId(), AINPUT_SOURCE_KEYBOARD, keycodes, outFlags));
1544 ASSERT_EQ(0, outFlags[0]) << "Keyboard should not have stylus button";
1545}
1546
Siarhei Vishniakoua0d2b802020-05-13 14:00:31 -07001547/**
1548 * The Steam controller sends BTN_GEAR_DOWN and BTN_GEAR_UP for the two "paddle" buttons
1549 * on the back. In this test, we make sure that BTN_GEAR_DOWN / BTN_WHEEL and BTN_GEAR_UP
1550 * are passed to the listener.
1551 */
1552static_assert(BTN_GEAR_DOWN == BTN_WHEEL);
1553TEST_F(InputReaderIntegrationTest, SendsGearDownAndUpToInputListener) {
1554 std::unique_ptr<UinputSteamController> controller = createUinputDevice<UinputSteamController>();
1555 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1556 NotifyKeyArgs keyArgs;
1557
1558 controller->pressAndReleaseKey(BTN_GEAR_DOWN);
1559 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
1560 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
1561 ASSERT_EQ(BTN_GEAR_DOWN, keyArgs.scanCode);
1562
1563 controller->pressAndReleaseKey(BTN_GEAR_UP);
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_UP, keyArgs.scanCode);
1567}
1568
Prabir Pradhan484d55a2022-10-14 23:17:16 +00001569// --- TouchIntegrationTest ---
1570
Arpit Singh440bf652023-08-09 09:23:43 +00001571class BaseTouchIntegrationTest : public InputReaderIntegrationTest {
Arthur Hungaab25622020-01-16 11:22:11 +08001572protected:
Arthur Hungaab25622020-01-16 11:22:11 +08001573 const std::string UNIQUE_ID = "local:0";
1574
Chris Yea52ade12020-08-27 16:49:20 -07001575 void SetUp() override {
Siarhei Vishniakou31977182022-09-30 08:51:23 -07001576#if !defined(__ANDROID__)
1577 GTEST_SKIP();
1578#endif
Arthur Hungaab25622020-01-16 11:22:11 +08001579 InputReaderIntegrationTest::SetUp();
1580 // At least add an internal display.
Michael Wrighta9cf4192022-12-01 23:46:39 +00001581 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
1582 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Arthur Hungaab25622020-01-16 11:22:11 +08001583
1584 mDevice = createUinputDevice<UinputTouchScreen>(Rect(0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT));
1585 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1586 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
Prabir Pradhanda20b172022-09-26 17:01:18 +00001587 const auto info = findDeviceByName(mDevice->getName());
1588 ASSERT_TRUE(info);
1589 mDeviceInfo = *info;
Arthur Hungaab25622020-01-16 11:22:11 +08001590 }
1591
1592 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
Michael Wrighta9cf4192022-12-01 23:46:39 +00001593 ui::Rotation orientation, const std::string& uniqueId,
Arthur Hungaab25622020-01-16 11:22:11 +08001594 std::optional<uint8_t> physicalPort,
1595 ViewportType viewportType) {
Harry Cutts33476232023-01-30 19:57:29 +00001596 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, /*isActive=*/true,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001597 uniqueId, physicalPort, viewportType);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00001598 mReader->requestRefreshConfiguration(InputReaderConfiguration::Change::DISPLAY_INFO);
Arthur Hungaab25622020-01-16 11:22:11 +08001599 }
1600
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001601 void assertReceivedMotion(int32_t action, const std::vector<Point>& points) {
1602 NotifyMotionArgs args;
1603 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1604 EXPECT_EQ(action, args.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07001605 ASSERT_EQ(points.size(), args.getPointerCount());
1606 for (size_t i = 0; i < args.getPointerCount(); i++) {
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001607 EXPECT_EQ(points[i].x, args.pointerCoords[i].getX());
1608 EXPECT_EQ(points[i].y, args.pointerCoords[i].getY());
1609 }
1610 }
1611
Arthur Hungaab25622020-01-16 11:22:11 +08001612 std::unique_ptr<UinputTouchScreen> mDevice;
Prabir Pradhanda20b172022-09-26 17:01:18 +00001613 InputDeviceInfo mDeviceInfo;
Arthur Hungaab25622020-01-16 11:22:11 +08001614};
1615
Arpit Singh440bf652023-08-09 09:23:43 +00001616enum class TouchIntegrationTestDisplays { DISPLAY_INTERNAL, DISPLAY_INPUT_PORT, DISPLAY_UNIQUE_ID };
1617
1618class TouchIntegrationTest : public BaseTouchIntegrationTest,
1619 public testing::WithParamInterface<TouchIntegrationTestDisplays> {
1620protected:
1621 static constexpr std::optional<uint8_t> DISPLAY_PORT = 0;
1622 const std::string INPUT_PORT = "uinput_touch/input0";
1623
1624 void SetUp() override {
1625#if !defined(__ANDROID__)
1626 GTEST_SKIP();
1627#endif
1628 if (GetParam() == TouchIntegrationTestDisplays::DISPLAY_INTERNAL) {
1629 BaseTouchIntegrationTest::SetUp();
1630 return;
1631 }
1632
1633 // setup policy with a input-port or UniqueId association to the display
1634 bool isInputPortAssociation =
1635 GetParam() == TouchIntegrationTestDisplays::DISPLAY_INPUT_PORT;
1636
1637 mFakePolicy = sp<FakeInputReaderPolicy>::make();
1638 if (isInputPortAssociation) {
1639 mFakePolicy->addInputPortAssociation(INPUT_PORT, DISPLAY_PORT.value());
1640 } else {
1641 mFakePolicy->addInputUniqueIdAssociation(INPUT_PORT, UNIQUE_ID);
1642 }
1643 mFakePointerController = std::make_shared<FakePointerController>();
1644 mFakePolicy->setPointerController(mFakePointerController);
1645
1646 InputReaderIntegrationTest::setupInputReader();
1647
1648 mDevice = createUinputDevice<UinputTouchScreen>(Rect(0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT),
1649 INPUT_PORT);
1650 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1651
1652 // Add a display linked to a physical port or UniqueId.
1653 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
1654 UNIQUE_ID, isInputPortAssociation ? DISPLAY_PORT : NO_PORT,
1655 ViewportType::INTERNAL);
1656 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1657 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1658 const auto info = findDeviceByName(mDevice->getName());
1659 ASSERT_TRUE(info);
1660 mDeviceInfo = *info;
1661 }
1662};
1663
1664TEST_P(TouchIntegrationTest, MultiTouchDeviceSource) {
Prabir Pradhanf9a41282022-10-25 17:15:50 +00001665 // The UinputTouchScreen is an MT device that supports MT_TOOL_TYPE and also supports stylus
1666 // buttons. It should show up as a touchscreen, stylus, and keyboard (for reporting button
1667 // presses).
1668 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_KEYBOARD,
1669 mDeviceInfo.getSources());
1670}
1671
Arpit Singh440bf652023-08-09 09:23:43 +00001672TEST_P(TouchIntegrationTest, InputEvent_ProcessSingleTouch) {
Arthur Hungaab25622020-01-16 11:22:11 +08001673 NotifyMotionArgs args;
1674 const Point centerPoint = mDevice->getCenterPoint();
1675
1676 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00001677 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08001678 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001679 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001680 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1681 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
1682
1683 // ACTION_MOVE
1684 mDevice->sendMove(centerPoint + Point(1, 1));
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001685 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001686 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1687 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
1688
1689 // ACTION_UP
1690 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001691 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001692 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1693 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
1694}
1695
Arpit Singh440bf652023-08-09 09:23:43 +00001696TEST_P(TouchIntegrationTest, InputEvent_ProcessMultiTouch) {
Arthur Hungaab25622020-01-16 11:22:11 +08001697 NotifyMotionArgs args;
1698 const Point centerPoint = mDevice->getCenterPoint();
1699
1700 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00001701 mDevice->sendSlot(FIRST_SLOT);
1702 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08001703 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001704 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001705 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1706 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
1707
1708 // ACTION_POINTER_DOWN (Second slot)
1709 const Point secondPoint = centerPoint + Point(100, 100);
1710 mDevice->sendSlot(SECOND_SLOT);
1711 mDevice->sendTrackingId(SECOND_TRACKING_ID);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001712 mDevice->sendDown(secondPoint);
1713 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001714 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08001715 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001716
1717 // ACTION_MOVE (Second slot)
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001718 mDevice->sendMove(secondPoint + Point(1, 1));
1719 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001720 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1721 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
1722
1723 // ACTION_POINTER_UP (Second slot)
arthurhungcc7f9802020-04-30 17:55:40 +08001724 mDevice->sendPointerUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001725 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001726 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08001727 ASSERT_EQ(ACTION_POINTER_1_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001728
1729 // ACTION_UP
1730 mDevice->sendSlot(FIRST_SLOT);
1731 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001732 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001733 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1734 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
1735}
1736
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001737/**
1738 * What happens when a pointer goes up while another pointer moves in the same frame? Are POINTER_UP
1739 * events guaranteed to contain the same data as a preceding MOVE, or can they contain different
1740 * data?
1741 * In this test, we try to send a change in coordinates in Pointer 0 in the same frame as the
1742 * liftoff of Pointer 1. We check that POINTER_UP event is generated first, and the MOVE event
1743 * for Pointer 0 only is generated after.
1744 * Suppose we are only interested in learning the movement of Pointer 0. If we only observe MOVE
1745 * events, we will not miss any information.
1746 * Even though the Pointer 1 up event contains updated Pointer 0 coordinates, there is another MOVE
1747 * event generated afterwards that contains the newest movement of pointer 0.
1748 * This is important for palm rejection. If there is a subsequent InputListener stage that detects
1749 * palms, and wants to cancel Pointer 1, then it is safe to simply drop POINTER_1_UP event without
1750 * losing information about non-palm pointers.
1751 */
Arpit Singh440bf652023-08-09 09:23:43 +00001752TEST_P(TouchIntegrationTest, MultiTouch_PointerMoveAndSecondPointerUp) {
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001753 NotifyMotionArgs args;
1754 const Point centerPoint = mDevice->getCenterPoint();
1755
1756 // ACTION_DOWN
1757 mDevice->sendSlot(FIRST_SLOT);
1758 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1759 mDevice->sendDown(centerPoint);
1760 mDevice->sendSync();
1761 assertReceivedMotion(AMOTION_EVENT_ACTION_DOWN, {centerPoint});
1762
1763 // ACTION_POINTER_DOWN (Second slot)
1764 const Point secondPoint = centerPoint + Point(100, 100);
1765 mDevice->sendSlot(SECOND_SLOT);
1766 mDevice->sendTrackingId(SECOND_TRACKING_ID);
1767 mDevice->sendDown(secondPoint);
1768 mDevice->sendSync();
1769 assertReceivedMotion(ACTION_POINTER_1_DOWN, {centerPoint, secondPoint});
1770
1771 // ACTION_MOVE (First slot)
1772 mDevice->sendSlot(FIRST_SLOT);
1773 mDevice->sendMove(centerPoint + Point(5, 5));
1774 // ACTION_POINTER_UP (Second slot)
1775 mDevice->sendSlot(SECOND_SLOT);
1776 mDevice->sendPointerUp();
1777 // Send a single sync for the above 2 pointer updates
1778 mDevice->sendSync();
1779
1780 // First, we should get POINTER_UP for the second pointer
1781 assertReceivedMotion(ACTION_POINTER_1_UP,
1782 {/*first pointer */ centerPoint + Point(5, 5),
1783 /*second pointer*/ secondPoint});
1784
1785 // Next, the MOVE event for the first pointer
1786 assertReceivedMotion(AMOTION_EVENT_ACTION_MOVE, {centerPoint + Point(5, 5)});
1787}
1788
1789/**
1790 * Similar scenario as above. The difference is that when the second pointer goes up, it will first
1791 * move, and then it will go up, all in the same frame.
1792 * In this scenario, the movement of the second pointer just prior to liftoff is ignored, and never
1793 * gets sent to the listener.
1794 */
Arpit Singh440bf652023-08-09 09:23:43 +00001795TEST_P(TouchIntegrationTest, MultiTouch_PointerMoveAndSecondPointerMoveAndUp) {
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001796 NotifyMotionArgs args;
1797 const Point centerPoint = mDevice->getCenterPoint();
1798
1799 // ACTION_DOWN
1800 mDevice->sendSlot(FIRST_SLOT);
1801 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1802 mDevice->sendDown(centerPoint);
1803 mDevice->sendSync();
1804 assertReceivedMotion(AMOTION_EVENT_ACTION_DOWN, {centerPoint});
1805
1806 // ACTION_POINTER_DOWN (Second slot)
1807 const Point secondPoint = centerPoint + Point(100, 100);
1808 mDevice->sendSlot(SECOND_SLOT);
1809 mDevice->sendTrackingId(SECOND_TRACKING_ID);
1810 mDevice->sendDown(secondPoint);
1811 mDevice->sendSync();
1812 assertReceivedMotion(ACTION_POINTER_1_DOWN, {centerPoint, secondPoint});
1813
1814 // ACTION_MOVE (First slot)
1815 mDevice->sendSlot(FIRST_SLOT);
1816 mDevice->sendMove(centerPoint + Point(5, 5));
1817 // ACTION_POINTER_UP (Second slot)
1818 mDevice->sendSlot(SECOND_SLOT);
1819 mDevice->sendMove(secondPoint + Point(6, 6));
1820 mDevice->sendPointerUp();
1821 // Send a single sync for the above 2 pointer updates
1822 mDevice->sendSync();
1823
1824 // First, we should get POINTER_UP for the second pointer
1825 // The movement of the second pointer during the liftoff frame is ignored.
1826 // The coordinates 'secondPoint + Point(6, 6)' are never sent to the listener.
1827 assertReceivedMotion(ACTION_POINTER_1_UP,
1828 {/*first pointer */ centerPoint + Point(5, 5),
1829 /*second pointer*/ secondPoint});
1830
1831 // Next, the MOVE event for the first pointer
1832 assertReceivedMotion(AMOTION_EVENT_ACTION_MOVE, {centerPoint + Point(5, 5)});
1833}
1834
Arpit Singh440bf652023-08-09 09:23:43 +00001835TEST_P(TouchIntegrationTest, InputEvent_ProcessPalm) {
Arthur Hungaab25622020-01-16 11:22:11 +08001836 NotifyMotionArgs args;
1837 const Point centerPoint = mDevice->getCenterPoint();
1838
1839 // ACTION_DOWN
arthurhungcc7f9802020-04-30 17:55:40 +08001840 mDevice->sendSlot(FIRST_SLOT);
1841 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08001842 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001843 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001844 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1845 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
1846
arthurhungcc7f9802020-04-30 17:55:40 +08001847 // ACTION_POINTER_DOWN (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08001848 const Point secondPoint = centerPoint + Point(100, 100);
1849 mDevice->sendSlot(SECOND_SLOT);
1850 mDevice->sendTrackingId(SECOND_TRACKING_ID);
1851 mDevice->sendDown(secondPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001852 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001853 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08001854 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001855
arthurhungcc7f9802020-04-30 17:55:40 +08001856 // ACTION_MOVE (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08001857 mDevice->sendMove(secondPoint + Point(1, 1));
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001858 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001859 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1860 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
1861
arthurhungcc7f9802020-04-30 17:55:40 +08001862 // Send MT_TOOL_PALM (second slot), which indicates that the touch IC has determined this to be
1863 // a palm event.
1864 // Expect to receive the ACTION_POINTER_UP with cancel flag.
Arthur Hungaab25622020-01-16 11:22:11 +08001865 mDevice->sendToolType(MT_TOOL_PALM);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001866 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001867 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08001868 ASSERT_EQ(ACTION_POINTER_1_UP, args.action);
arthurhungcc7f9802020-04-30 17:55:40 +08001869 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, args.flags);
Arthur Hungaab25622020-01-16 11:22:11 +08001870
arthurhungcc7f9802020-04-30 17:55:40 +08001871 // Send up to second slot, expect first slot send moving.
1872 mDevice->sendPointerUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001873 mDevice->sendSync();
arthurhungcc7f9802020-04-30 17:55:40 +08001874 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1875 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001876
arthurhungcc7f9802020-04-30 17:55:40 +08001877 // Send ACTION_UP (first slot)
Arthur Hungaab25622020-01-16 11:22:11 +08001878 mDevice->sendSlot(FIRST_SLOT);
1879 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001880 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001881
arthurhungcc7f9802020-04-30 17:55:40 +08001882 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1883 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001884}
1885
Prabir Pradhanc09ec6d2023-08-14 22:31:43 +00001886/**
1887 * Some drivers historically have reported axis values outside of the range specified in the
1888 * evdev axis info. Ensure we don't crash when this happens. For example, a driver may report a
1889 * pressure value greater than the reported maximum, since it unclear what specific meaning the
1890 * maximum value for pressure has (beyond the maximum value that can be produced by a sensor),
1891 * and no units for pressure (resolution) is specified by the evdev documentation.
1892 */
1893TEST_P(TouchIntegrationTest, AcceptsAxisValuesOutsideReportedRange) {
1894 const Point centerPoint = mDevice->getCenterPoint();
1895
1896 // Down with pressure outside the reported range
1897 mDevice->sendSlot(FIRST_SLOT);
1898 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1899 mDevice->sendDown(centerPoint);
1900 mDevice->sendPressure(UinputTouchScreen::RAW_PRESSURE_MAX + 2);
1901 mDevice->sendSync();
1902 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
1903 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
1904
1905 // Move to a point outside the reported range
1906 mDevice->sendMove(Point(DISPLAY_WIDTH, DISPLAY_HEIGHT) + Point(1, 1));
1907 mDevice->sendSync();
1908 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
1909 WithMotionAction(AMOTION_EVENT_ACTION_MOVE)));
1910
1911 // Up
1912 mDevice->sendUp();
1913 mDevice->sendSync();
1914 ASSERT_NO_FATAL_FAILURE(
1915 mTestListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
1916}
1917
Arpit Singh440bf652023-08-09 09:23:43 +00001918TEST_P(TouchIntegrationTest, NotifiesPolicyWhenStylusGestureStarted) {
Prabir Pradhanda20b172022-09-26 17:01:18 +00001919 const Point centerPoint = mDevice->getCenterPoint();
1920
1921 // Send down with the pen tool selected. The policy should be notified of the stylus presence.
1922 mDevice->sendSlot(FIRST_SLOT);
1923 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1924 mDevice->sendToolType(MT_TOOL_PEN);
1925 mDevice->sendDown(centerPoint);
1926 mDevice->sendSync();
1927 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
1928 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001929 WithToolType(ToolType::STYLUS))));
Prabir Pradhanda20b172022-09-26 17:01:18 +00001930
1931 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotified(mDeviceInfo.getId()));
1932
1933 // Release the stylus touch.
1934 mDevice->sendUp();
1935 mDevice->sendSync();
1936 ASSERT_NO_FATAL_FAILURE(
1937 mTestListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
1938
1939 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotNotified());
1940
1941 // Touch down with the finger, without the pen tool selected. The policy is not notified.
1942 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1943 mDevice->sendToolType(MT_TOOL_FINGER);
1944 mDevice->sendDown(centerPoint);
1945 mDevice->sendSync();
1946 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
1947 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001948 WithToolType(ToolType::FINGER))));
Prabir Pradhanda20b172022-09-26 17:01:18 +00001949
1950 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotNotified());
1951
1952 mDevice->sendUp();
1953 mDevice->sendSync();
1954 ASSERT_NO_FATAL_FAILURE(
1955 mTestListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
1956
1957 // Send a move event with the stylus tool without BTN_TOUCH to generate a hover enter.
1958 // The policy should be notified of the stylus presence.
1959 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1960 mDevice->sendToolType(MT_TOOL_PEN);
1961 mDevice->sendMove(centerPoint);
1962 mDevice->sendSync();
1963 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
1964 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001965 WithToolType(ToolType::STYLUS))));
Prabir Pradhanda20b172022-09-26 17:01:18 +00001966
1967 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotified(mDeviceInfo.getId()));
1968}
1969
Arpit Singh440bf652023-08-09 09:23:43 +00001970TEST_P(TouchIntegrationTest, ExternalStylusConnectedDuringTouchGesture) {
Prabir Pradhan85cf63e2023-08-07 21:02:13 +00001971 const Point centerPoint = mDevice->getCenterPoint();
1972
1973 // Down
1974 mDevice->sendSlot(FIRST_SLOT);
1975 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1976 mDevice->sendDown(centerPoint);
1977 mDevice->sendSync();
1978 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
1979 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
1980
1981 // Move
1982 mDevice->sendMove(centerPoint + Point(1, 1));
1983 mDevice->sendSync();
1984 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
1985 WithMotionAction(AMOTION_EVENT_ACTION_MOVE)));
1986
1987 // Connecting an external stylus mid-gesture should not interrupt the ongoing gesture stream.
1988 auto externalStylus = createUinputDevice<UinputExternalStylus>();
1989 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1990 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1991 const auto stylusInfo = findDeviceByName(externalStylus->getName());
1992 ASSERT_TRUE(stylusInfo);
Prabir Pradhan85cf63e2023-08-07 21:02:13 +00001993
1994 // Move
1995 mDevice->sendMove(centerPoint + Point(2, 2));
1996 mDevice->sendSync();
1997 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
1998 WithMotionAction(AMOTION_EVENT_ACTION_MOVE)));
1999
2000 // Disconnecting an external stylus mid-gesture should not interrupt the ongoing gesture stream.
2001 externalStylus.reset();
2002 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2003 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2004 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2005
2006 // Up
2007 mDevice->sendUp();
2008 mDevice->sendSync();
2009 ASSERT_NO_FATAL_FAILURE(
2010 mTestListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
2011
2012 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2013}
2014
Arpit Singh440bf652023-08-09 09:23:43 +00002015INSTANTIATE_TEST_SUITE_P(TouchIntegrationTestDisplayVariants, TouchIntegrationTest,
2016 testing::Values(TouchIntegrationTestDisplays::DISPLAY_INTERNAL,
2017 TouchIntegrationTestDisplays::DISPLAY_INPUT_PORT,
2018 TouchIntegrationTestDisplays::DISPLAY_UNIQUE_ID));
2019
Prabir Pradhan124ea442022-10-28 20:27:44 +00002020// --- StylusButtonIntegrationTest ---
Prabir Pradhane1a41a82022-10-14 18:06:50 +00002021
Prabir Pradhan124ea442022-10-28 20:27:44 +00002022// Verify the behavior of button presses reported by various kinds of styluses, including buttons
2023// reported by the touchscreen's device, by a fused external stylus, and by an un-fused external
2024// stylus.
2025template <typename UinputStylusDevice>
Arpit Singh440bf652023-08-09 09:23:43 +00002026class StylusButtonIntegrationTest : public BaseTouchIntegrationTest {
Prabir Pradhan124ea442022-10-28 20:27:44 +00002027protected:
2028 void SetUp() override {
2029#if !defined(__ANDROID__)
2030 GTEST_SKIP();
2031#endif
Arpit Singh440bf652023-08-09 09:23:43 +00002032 BaseTouchIntegrationTest::SetUp();
Prabir Pradhan124ea442022-10-28 20:27:44 +00002033 mTouchscreen = mDevice.get();
2034 mTouchscreenInfo = mDeviceInfo;
2035
2036 setUpStylusDevice();
2037 }
2038
2039 UinputStylusDevice* mStylus{nullptr};
2040 InputDeviceInfo mStylusInfo{};
2041
2042 UinputTouchScreen* mTouchscreen{nullptr};
2043 InputDeviceInfo mTouchscreenInfo{};
2044
2045private:
2046 // When we are attempting to test stylus button events that are sent from the touchscreen,
2047 // use the same Uinput device for the touchscreen and the stylus.
2048 template <typename T = UinputStylusDevice>
2049 std::enable_if_t<std::is_same_v<UinputTouchScreen, T>, void> setUpStylusDevice() {
2050 mStylus = mDevice.get();
2051 mStylusInfo = mDeviceInfo;
2052 }
2053
2054 // When we are attempting to stylus buttons from an external stylus being merged with touches
2055 // from a touchscreen, create a new Uinput device through which stylus buttons can be injected.
2056 template <typename T = UinputStylusDevice>
2057 std::enable_if_t<!std::is_same_v<UinputTouchScreen, T>, void> setUpStylusDevice() {
2058 mStylusDeviceLifecycleTracker = createUinputDevice<T>();
2059 mStylus = mStylusDeviceLifecycleTracker.get();
2060 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2061 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2062 const auto info = findDeviceByName(mStylus->getName());
2063 ASSERT_TRUE(info);
2064 mStylusInfo = *info;
2065 }
2066
2067 std::unique_ptr<UinputStylusDevice> mStylusDeviceLifecycleTracker{};
2068
2069 // Hide the base class's device to expose it with a different name for readability.
Arpit Singh440bf652023-08-09 09:23:43 +00002070 using BaseTouchIntegrationTest::mDevice;
2071 using BaseTouchIntegrationTest::mDeviceInfo;
Prabir Pradhan124ea442022-10-28 20:27:44 +00002072};
2073
2074using StylusButtonIntegrationTestTypes =
2075 ::testing::Types<UinputTouchScreen, UinputExternalStylus, UinputExternalStylusWithPressure>;
2076TYPED_TEST_SUITE(StylusButtonIntegrationTest, StylusButtonIntegrationTestTypes);
2077
Prabir Pradhan39d60aa2023-07-13 22:01:04 +00002078TYPED_TEST(StylusButtonIntegrationTest, StylusButtonsGenerateKeyEvents) {
Prabir Pradhan124ea442022-10-28 20:27:44 +00002079 const auto stylusId = TestFixture::mStylusInfo.getId();
2080
2081 TestFixture::mStylus->pressKey(BTN_STYLUS);
2082 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
2083 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
2084 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2085
2086 TestFixture::mStylus->releaseKey(BTN_STYLUS);
2087 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhane1a41a82022-10-14 18:06:50 +00002088 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002089 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
Prabir Pradhane1a41a82022-10-14 18:06:50 +00002090}
2091
Prabir Pradhan39d60aa2023-07-13 22:01:04 +00002092TYPED_TEST(StylusButtonIntegrationTest, StylusButtonsSurroundingTouchGesture) {
Prabir Pradhan124ea442022-10-28 20:27:44 +00002093 const Point centerPoint = TestFixture::mTouchscreen->getCenterPoint();
2094 const auto touchscreenId = TestFixture::mTouchscreenInfo.getId();
2095 const auto stylusId = TestFixture::mStylusInfo.getId();
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002096
2097 // Press the stylus button.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002098 TestFixture::mStylus->pressKey(BTN_STYLUS);
2099 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002100 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002101 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002102
2103 // Start and finish a stylus gesture.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002104 TestFixture::mTouchscreen->sendSlot(FIRST_SLOT);
2105 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
2106 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
2107 TestFixture::mTouchscreen->sendDown(centerPoint);
2108 TestFixture::mTouchscreen->sendSync();
2109 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002110 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002111 WithToolType(ToolType::STYLUS),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002112 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY),
2113 WithDeviceId(touchscreenId))));
2114 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002115 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
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))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002119
Prabir Pradhan124ea442022-10-28 20:27:44 +00002120 TestFixture::mTouchscreen->sendTrackingId(INVALID_TRACKING_ID);
2121 TestFixture::mTouchscreen->sendSync();
2122 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002123 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002124 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002125 WithDeviceId(touchscreenId))));
2126 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002127 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002128 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002129 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002130
2131 // Release the stylus button.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002132 TestFixture::mStylus->releaseKey(BTN_STYLUS);
2133 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002134 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002135 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002136}
2137
Prabir Pradhan39d60aa2023-07-13 22:01:04 +00002138TYPED_TEST(StylusButtonIntegrationTest, StylusButtonsSurroundingHoveringTouchGesture) {
Prabir Pradhan9a561c22022-11-07 16:11:23 +00002139 const Point centerPoint = TestFixture::mTouchscreen->getCenterPoint();
2140 const auto touchscreenId = TestFixture::mTouchscreenInfo.getId();
2141 const auto stylusId = TestFixture::mStylusInfo.getId();
2142 auto toolTypeDevice =
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002143 AllOf(WithToolType(ToolType::STYLUS), WithDeviceId(touchscreenId));
Prabir Pradhan9a561c22022-11-07 16:11:23 +00002144
2145 // Press the stylus button.
2146 TestFixture::mStylus->pressKey(BTN_STYLUS);
2147 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
2148 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
2149 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2150
2151 // Start hovering with the stylus.
2152 TestFixture::mTouchscreen->sendSlot(FIRST_SLOT);
2153 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
2154 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
2155 TestFixture::mTouchscreen->sendMove(centerPoint);
2156 TestFixture::mTouchscreen->sendSync();
2157 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2158 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
2159 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2160 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2161 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
2162 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2163 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2164 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
2165 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2166
2167 // Touch down with the stylus.
2168 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
2169 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
2170 TestFixture::mTouchscreen->sendDown(centerPoint);
2171 TestFixture::mTouchscreen->sendSync();
2172 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2173 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT),
2174 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2175
2176 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2177 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
2178 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2179
2180 // Stop touching with the stylus, and start hovering.
2181 TestFixture::mTouchscreen->sendUp();
2182 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
2183 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
2184 TestFixture::mTouchscreen->sendMove(centerPoint);
2185 TestFixture::mTouchscreen->sendSync();
2186 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2187 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_UP),
2188 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2189 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2190 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
2191 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2192 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2193 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
2194 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2195
2196 // Stop hovering.
2197 TestFixture::mTouchscreen->sendTrackingId(INVALID_TRACKING_ID);
2198 TestFixture::mTouchscreen->sendSync();
2199 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2200 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
2201 WithButtonState(0))));
2202 // TODO(b/257971675): Fix inconsistent button state when exiting hover.
2203 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2204 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT),
2205 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2206
2207 // Release the stylus button.
2208 TestFixture::mStylus->releaseKey(BTN_STYLUS);
2209 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
2210 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
2211 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2212}
2213
Prabir Pradhan39d60aa2023-07-13 22:01:04 +00002214TYPED_TEST(StylusButtonIntegrationTest, StylusButtonsWithinTouchGesture) {
Prabir Pradhan124ea442022-10-28 20:27:44 +00002215 const Point centerPoint = TestFixture::mTouchscreen->getCenterPoint();
2216 const auto touchscreenId = TestFixture::mTouchscreenInfo.getId();
2217 const auto stylusId = TestFixture::mStylusInfo.getId();
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002218
2219 // Start a stylus gesture.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002220 TestFixture::mTouchscreen->sendSlot(FIRST_SLOT);
2221 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
2222 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
2223 TestFixture::mTouchscreen->sendDown(centerPoint);
2224 TestFixture::mTouchscreen->sendSync();
2225 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002226 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002227 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002228 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002229
2230 // Press and release a stylus button. Each change in button state also generates a MOVE event.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002231 TestFixture::mStylus->pressKey(BTN_STYLUS);
2232 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002233 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002234 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2235 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002236 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002237 WithToolType(ToolType::STYLUS),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002238 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY),
2239 WithDeviceId(touchscreenId))));
2240 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002241 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
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))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002245
Prabir Pradhan124ea442022-10-28 20:27:44 +00002246 TestFixture::mStylus->releaseKey(BTN_STYLUS);
2247 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002248 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002249 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2250 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002251 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002252 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002253 WithDeviceId(touchscreenId))));
2254 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002255 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002256 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002257 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002258
2259 // Finish the stylus gesture.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002260 TestFixture::mTouchscreen->sendTrackingId(INVALID_TRACKING_ID);
2261 TestFixture::mTouchscreen->sendSync();
2262 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002263 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002264 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002265 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002266}
2267
Prabir Pradhan39d60aa2023-07-13 22:01:04 +00002268TYPED_TEST(StylusButtonIntegrationTest, StylusButtonMotionEventsDisabled) {
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002269 TestFixture::mFakePolicy->setStylusButtonMotionEventsEnabled(false);
2270 TestFixture::mReader->requestRefreshConfiguration(
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002271 InputReaderConfiguration::Change::STYLUS_BUTTON_REPORTING);
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002272
2273 const Point centerPoint = TestFixture::mTouchscreen->getCenterPoint();
2274 const auto touchscreenId = TestFixture::mTouchscreenInfo.getId();
2275 const auto stylusId = TestFixture::mStylusInfo.getId();
2276
2277 // Start a stylus gesture. By the time this event is processed, the configuration change that
2278 // was requested is guaranteed to be completed.
2279 TestFixture::mTouchscreen->sendSlot(FIRST_SLOT);
2280 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
2281 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
2282 TestFixture::mTouchscreen->sendDown(centerPoint);
2283 TestFixture::mTouchscreen->sendSync();
2284 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2285 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002286 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002287 WithDeviceId(touchscreenId))));
2288
2289 // Press and release a stylus button. Each change only generates a MOVE motion event.
2290 // Key events are unaffected.
2291 TestFixture::mStylus->pressKey(BTN_STYLUS);
2292 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
2293 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
2294 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2295 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2296 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002297 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002298 WithDeviceId(touchscreenId))));
2299
2300 TestFixture::mStylus->releaseKey(BTN_STYLUS);
2301 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
2302 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
2303 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2304 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2305 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002306 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002307 WithDeviceId(touchscreenId))));
2308
2309 // Finish the stylus gesture.
2310 TestFixture::mTouchscreen->sendTrackingId(INVALID_TRACKING_ID);
2311 TestFixture::mTouchscreen->sendSync();
2312 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2313 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002314 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002315 WithDeviceId(touchscreenId))));
2316}
2317
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002318// --- ExternalStylusIntegrationTest ---
2319
2320// Verify the behavior of an external stylus. An external stylus can report pressure or button
2321// data independently of the touchscreen, which is then sent as a MotionEvent as part of an
2322// ongoing stylus gesture that is being emitted by the touchscreen.
Arpit Singh440bf652023-08-09 09:23:43 +00002323using ExternalStylusIntegrationTest = BaseTouchIntegrationTest;
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002324
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00002325TEST_F(ExternalStylusIntegrationTest, ExternalStylusConnectionChangesTouchscreenSource) {
2326 // Create an external stylus capable of reporting pressure data that
2327 // should be fused with a touch pointer.
2328 std::unique_ptr<UinputExternalStylusWithPressure> stylus =
2329 createUinputDevice<UinputExternalStylusWithPressure>();
2330 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2331 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2332 const auto stylusInfo = findDeviceByName(stylus->getName());
2333 ASSERT_TRUE(stylusInfo);
2334
2335 // Connecting an external stylus changes the source of the touchscreen.
2336 const auto deviceInfo = findDeviceByName(mDevice->getName());
2337 ASSERT_TRUE(deviceInfo);
2338 ASSERT_TRUE(isFromSource(deviceInfo->getSources(), STYLUS_FUSION_SOURCE));
2339}
2340
Prabir Pradhan39d60aa2023-07-13 22:01:04 +00002341TEST_F(ExternalStylusIntegrationTest, FusedExternalStylusPressureReported) {
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002342 const Point centerPoint = mDevice->getCenterPoint();
2343
2344 // Create an external stylus capable of reporting pressure data that
2345 // should be fused with a touch pointer.
2346 std::unique_ptr<UinputExternalStylusWithPressure> stylus =
2347 createUinputDevice<UinputExternalStylusWithPressure>();
2348 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2349 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2350 const auto stylusInfo = findDeviceByName(stylus->getName());
2351 ASSERT_TRUE(stylusInfo);
2352
2353 ASSERT_EQ(AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_KEYBOARD, stylusInfo->getSources());
2354
2355 const auto touchscreenId = mDeviceInfo.getId();
2356
2357 // Set a pressure value on the stylus. It doesn't generate any events.
2358 const auto& RAW_PRESSURE_MAX = UinputExternalStylusWithPressure::RAW_PRESSURE_MAX;
2359 stylus->setPressure(100);
2360 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2361
2362 // Start a finger gesture, and ensure it shows up as stylus gesture
2363 // with the pressure set by the external stylus.
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002364 mDevice->sendSlot(FIRST_SLOT);
Chris Ye1b0c7342020-07-28 21:57:03 -07002365 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002366 mDevice->sendToolType(MT_TOOL_FINGER);
Prabir Pradhanb54ffb22022-10-27 18:03:34 +00002367 mDevice->sendDown(centerPoint);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002368 mDevice->sendSync();
2369 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00002370 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithToolType(ToolType::STYLUS),
2371 WithButtonState(0), WithSource(STYLUS_FUSION_SOURCE), WithDeviceId(touchscreenId),
2372 WithPressure(100.f / RAW_PRESSURE_MAX))));
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002373
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002374 // Change the pressure on the external stylus, and ensure the touchscreen generates a MOVE
2375 // event with the updated pressure.
2376 stylus->setPressure(200);
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +00002377 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00002378 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithToolType(ToolType::STYLUS),
2379 WithButtonState(0), WithSource(STYLUS_FUSION_SOURCE), WithDeviceId(touchscreenId),
2380 WithPressure(200.f / RAW_PRESSURE_MAX))));
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002381
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002382 // The external stylus did not generate any events.
2383 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2384 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasNotCalled());
2385}
2386
Prabir Pradhan39d60aa2023-07-13 22:01:04 +00002387TEST_F(ExternalStylusIntegrationTest, FusedExternalStylusPressureNotReported) {
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002388 const Point centerPoint = mDevice->getCenterPoint();
2389
2390 // Create an external stylus capable of reporting pressure data that
2391 // should be fused with a touch pointer.
2392 std::unique_ptr<UinputExternalStylusWithPressure> stylus =
2393 createUinputDevice<UinputExternalStylusWithPressure>();
2394 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2395 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2396 const auto stylusInfo = findDeviceByName(stylus->getName());
2397 ASSERT_TRUE(stylusInfo);
2398
2399 ASSERT_EQ(AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_KEYBOARD, stylusInfo->getSources());
2400
2401 const auto touchscreenId = mDeviceInfo.getId();
2402
2403 // Set a pressure value of 0 on the stylus. It doesn't generate any events.
2404 const auto& RAW_PRESSURE_MAX = UinputExternalStylusWithPressure::RAW_PRESSURE_MAX;
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00002405 // Send a non-zero value first to prevent the kernel from consuming the zero event.
2406 stylus->setPressure(100);
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002407 stylus->setPressure(0);
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00002408 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002409
2410 // Start a finger gesture. The touch device will withhold generating any touches for
2411 // up to 72 milliseconds while waiting for pressure data from the external stylus.
2412 mDevice->sendSlot(FIRST_SLOT);
2413 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2414 mDevice->sendToolType(MT_TOOL_FINGER);
2415 mDevice->sendDown(centerPoint);
2416 auto waitUntil = std::chrono::system_clock::now() +
2417 std::chrono::milliseconds(ns2ms(EXTERNAL_STYLUS_DATA_TIMEOUT));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07002418 mDevice->sendSync();
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002419 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled(waitUntil));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002420
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002421 // Since the external stylus did not report a pressure value within the timeout,
2422 // it shows up as a finger pointer.
2423 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2424 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00002425 WithSource(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS),
2426 WithToolType(ToolType::FINGER), WithDeviceId(touchscreenId), WithPressure(1.f))));
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002427
2428 // Change the pressure on the external stylus. Since the pressure was not present at the start
2429 // of the gesture, it is ignored for now.
2430 stylus->setPressure(200);
2431 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2432
2433 // Finish the finger gesture.
Michael Wrightd02c5b62014-02-10 15:10:22 -08002434 mDevice->sendTrackingId(INVALID_TRACKING_ID);
2435 mDevice->sendSync();
2436 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2437 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00002438 WithSource(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002439 WithToolType(ToolType::FINGER))));
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002440
2441 // Start a new gesture. Since we have a valid pressure value, it shows up as a stylus.
2442 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2443 mDevice->sendToolType(MT_TOOL_FINGER);
2444 mDevice->sendDown(centerPoint);
2445 mDevice->sendSync();
2446 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00002447 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithSource(STYLUS_FUSION_SOURCE),
2448 WithToolType(ToolType::STYLUS), WithButtonState(0), WithDeviceId(touchscreenId),
2449 WithPressure(200.f / RAW_PRESSURE_MAX))));
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002450
2451 // The external stylus did not generate any events.
2452 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2453 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasNotCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002454}
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002455
Prabir Pradhan39d60aa2023-07-13 22:01:04 +00002456TEST_F(ExternalStylusIntegrationTest, UnfusedExternalStylus) {
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00002457 const Point centerPoint = mDevice->getCenterPoint();
2458
2459 // Create an external stylus device that does not support pressure. It should not affect any
2460 // touch pointers.
2461 std::unique_ptr<UinputExternalStylus> stylus = createUinputDevice<UinputExternalStylus>();
2462 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2463 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2464 const auto stylusInfo = findDeviceByName(stylus->getName());
2465 ASSERT_TRUE(stylusInfo);
2466
2467 ASSERT_EQ(AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_KEYBOARD, stylusInfo->getSources());
2468
2469 const auto touchscreenId = mDeviceInfo.getId();
2470
2471 // Start a finger gesture and ensure a finger pointer is generated for it, without waiting for
2472 // pressure data from the external stylus.
2473 mDevice->sendSlot(FIRST_SLOT);
2474 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2475 mDevice->sendToolType(MT_TOOL_FINGER);
2476 mDevice->sendDown(centerPoint);
2477 auto waitUntil = std::chrono::system_clock::now() +
2478 std::chrono::milliseconds(ns2ms(EXTERNAL_STYLUS_DATA_TIMEOUT));
2479 mDevice->sendSync();
2480 ASSERT_NO_FATAL_FAILURE(
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00002481 mTestListener->assertNotifyMotionWasCalled(AllOf(WithMotionAction(
2482 AMOTION_EVENT_ACTION_DOWN),
2483 WithToolType(ToolType::FINGER),
2484 WithSource(AINPUT_SOURCE_TOUCHSCREEN |
2485 AINPUT_SOURCE_STYLUS),
2486 WithButtonState(0),
2487 WithDeviceId(touchscreenId),
2488 WithPressure(1.f)),
2489 waitUntil));
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00002490
2491 // The external stylus did not generate any events.
2492 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2493 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasNotCalled());
2494}
2495
Michael Wrightd02c5b62014-02-10 15:10:22 -08002496// --- InputDeviceTest ---
2497class InputDeviceTest : public testing::Test {
2498protected:
2499 static const char* DEVICE_NAME;
2500 static const char* DEVICE_LOCATION;
2501 static const int32_t DEVICE_ID;
2502 static const int32_t DEVICE_GENERATION;
2503 static const int32_t DEVICE_CONTROLLER_NUMBER;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002504 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002505 static const int32_t EVENTHUB_ID;
2506 static const std::string DEVICE_BLUETOOTH_ADDRESS;
2507
2508 std::shared_ptr<FakeEventHub> mFakeEventHub;
2509 sp<FakeInputReaderPolicy> mFakePolicy;
2510 std::unique_ptr<TestInputListener> mFakeListener;
2511 std::unique_ptr<InstrumentedInputReader> mReader;
2512 std::shared_ptr<InputDevice> mDevice;
2513
2514 void SetUp() override {
2515 mFakeEventHub = std::make_unique<FakeEventHub>();
2516 mFakePolicy = sp<FakeInputReaderPolicy>::make();
2517 mFakeListener = std::make_unique<TestInputListener>();
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002518 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Michael Wrightd02c5b62014-02-10 15:10:22 -08002519 *mFakeListener);
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002520 InputDeviceIdentifier identifier;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002521 identifier.name = DEVICE_NAME;
2522 identifier.location = DEVICE_LOCATION;
2523 identifier.bluetoothAddress = DEVICE_BLUETOOTH_ADDRESS;
2524 mDevice = std::make_shared<InputDevice>(mReader->getContext(), DEVICE_ID, DEVICE_GENERATION,
2525 identifier);
2526 mReader->pushNextDevice(mDevice);
2527 mFakeEventHub->addDevice(EVENTHUB_ID, DEVICE_NAME, ftl::Flags<InputDeviceClass>(0));
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002528 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002529 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002530
2531 void TearDown() override {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002532 mFakeListener.reset();
2533 mFakePolicy.clear();
2534 }
2535};
2536
2537const char* InputDeviceTest::DEVICE_NAME = "device";
2538const char* InputDeviceTest::DEVICE_LOCATION = "USB1";
2539const int32_t InputDeviceTest::DEVICE_ID = END_RESERVED_ID + 1000;
2540const int32_t InputDeviceTest::DEVICE_GENERATION = 2;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002541const int32_t InputDeviceTest::DEVICE_CONTROLLER_NUMBER = 0;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002542const ftl::Flags<InputDeviceClass> InputDeviceTest::DEVICE_CLASSES =
2543 InputDeviceClass::KEYBOARD | InputDeviceClass::TOUCH | InputDeviceClass::JOYSTICK;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002544const int32_t InputDeviceTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002545const std::string InputDeviceTest::DEVICE_BLUETOOTH_ADDRESS = "11:AA:22:BB:33:CC";
2546
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002547TEST_F(InputDeviceTest, ImmutableProperties) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002548 ASSERT_EQ(DEVICE_ID, mDevice->getId());
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002549 ASSERT_STREQ(DEVICE_NAME, mDevice->getName().c_str());
2550 ASSERT_EQ(ftl::Flags<InputDeviceClass>(0), mDevice->getClasses());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002551}
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002552
Michael Wrightd02c5b62014-02-10 15:10:22 -08002553TEST_F(InputDeviceTest, WhenDeviceCreated_EnabledIsFalse) {
2554 ASSERT_EQ(mDevice->isEnabled(), false);
2555}
2556
2557TEST_F(InputDeviceTest, WhenNoMappersAreRegistered_DeviceIsIgnored) {
2558 // Configuration.
2559 InputReaderConfiguration config;
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002560 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, config, /*changes=*/{});
Michael Wrightd02c5b62014-02-10 15:10:22 -08002561
2562 // Reset.
2563 unused += mDevice->reset(ARBITRARY_TIME);
2564
2565 NotifyDeviceResetArgs resetArgs;
2566 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2567 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2568 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2569
2570 // Metadata.
2571 ASSERT_TRUE(mDevice->isIgnored());
2572 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mDevice->getSources());
2573
2574 InputDeviceInfo info = mDevice->getDeviceInfo();
2575 ASSERT_EQ(DEVICE_ID, info.getId());
2576 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
2577 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NONE, info.getKeyboardType());
2578 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, info.getSources());
2579
2580 // State queries.
2581 ASSERT_EQ(0, mDevice->getMetaState());
2582
2583 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2584 << "Ignored device should return unknown key code state.";
2585 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2586 << "Ignored device should return unknown scan code state.";
2587 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 0))
2588 << "Ignored device should return unknown switch state.";
2589
2590 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B};
2591 uint8_t flags[2] = { 0, 1 };
2592 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, keyCodes, flags))
2593 << "Ignored device should never mark any key codes.";
2594 ASSERT_EQ(0, flags[0]) << "Flag for unsupported key should be unchanged.";
2595 ASSERT_EQ(1, flags[1]) << "Flag for unsupported key should be unchanged.";
2596}
2597
2598TEST_F(InputDeviceTest, WhenMappersAreRegistered_DeviceIsNotIgnoredAndForwardsRequestsToMappers) {
2599 // Configuration.
2600 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "key", "value");
2601
2602 FakeInputMapper& mapper1 =
Arpit Singh8e6fb252023-04-06 11:49:17 +00002603 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2604 AINPUT_SOURCE_KEYBOARD);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002605 mapper1.setKeyboardType(AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2606 mapper1.setMetaState(AMETA_ALT_ON);
2607 mapper1.addSupportedKeyCode(AKEYCODE_A);
2608 mapper1.addSupportedKeyCode(AKEYCODE_B);
2609 mapper1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
2610 mapper1.setKeyCodeState(AKEYCODE_B, AKEY_STATE_UP);
2611 mapper1.setScanCodeState(2, AKEY_STATE_DOWN);
2612 mapper1.setScanCodeState(3, AKEY_STATE_UP);
2613 mapper1.setSwitchState(4, AKEY_STATE_DOWN);
2614
2615 FakeInputMapper& mapper2 =
Arpit Singh8e6fb252023-04-06 11:49:17 +00002616 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2617 AINPUT_SOURCE_TOUCHSCREEN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002618 mapper2.setMetaState(AMETA_SHIFT_ON);
2619
2620 InputReaderConfiguration config;
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002621 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, config, /*changes=*/{});
Michael Wrightd02c5b62014-02-10 15:10:22 -08002622
Harry Cuttsf13161a2023-03-08 14:15:49 +00002623 std::optional<std::string> propertyValue = mDevice->getConfiguration().getString("key");
2624 ASSERT_TRUE(propertyValue.has_value())
Michael Wrightd02c5b62014-02-10 15:10:22 -08002625 << "Device should have read configuration during configuration phase.";
Harry Cuttsf13161a2023-03-08 14:15:49 +00002626 ASSERT_EQ("value", *propertyValue);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002627
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002628 ASSERT_NO_FATAL_FAILURE(mapper1.assertConfigureWasCalled());
2629 ASSERT_NO_FATAL_FAILURE(mapper2.assertConfigureWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002630
2631 // Reset
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002632 unused += mDevice->reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002633 ASSERT_NO_FATAL_FAILURE(mapper1.assertResetWasCalled());
2634 ASSERT_NO_FATAL_FAILURE(mapper2.assertResetWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002635
2636 NotifyDeviceResetArgs resetArgs;
2637 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2638 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2639 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2640
2641 // Metadata.
2642 ASSERT_FALSE(mDevice->isIgnored());
2643 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), mDevice->getSources());
2644
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002645 InputDeviceInfo info = mDevice->getDeviceInfo();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002646 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002647 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002648 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_ALPHABETIC, info.getKeyboardType());
2649 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), info.getSources());
2650
2651 // State queries.
2652 ASSERT_EQ(AMETA_ALT_ON | AMETA_SHIFT_ON, mDevice->getMetaState())
2653 << "Should query mappers and combine meta states.";
2654
2655 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2656 << "Should return unknown key code state when source not supported.";
2657 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2658 << "Should return unknown scan code state when source not supported.";
2659 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2660 << "Should return unknown switch state when source not supported.";
2661
2662 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, AKEYCODE_A))
2663 << "Should query mapper when source is supported.";
2664 ASSERT_EQ(AKEY_STATE_UP, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 3))
2665 << "Should query mapper when source is supported.";
2666 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 4))
2667 << "Should query mapper when source is supported.";
2668
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002669 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2};
Michael Wrightd02c5b62014-02-10 15:10:22 -08002670 uint8_t flags[4] = { 0, 0, 0, 1 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002671 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08002672 << "Should do nothing when source is unsupported.";
2673 ASSERT_EQ(0, flags[0]) << "Flag should be unchanged when source is unsupported.";
2674 ASSERT_EQ(0, flags[1]) << "Flag should be unchanged when source is unsupported.";
2675 ASSERT_EQ(0, flags[2]) << "Flag should be unchanged when source is unsupported.";
2676 ASSERT_EQ(1, flags[3]) << "Flag should be unchanged when source is unsupported.";
2677
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002678 ASSERT_TRUE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08002679 << "Should query mapper when source is supported.";
2680 ASSERT_EQ(1, flags[0]) << "Flag for supported key should be set.";
2681 ASSERT_EQ(1, flags[1]) << "Flag for supported key should be set.";
2682 ASSERT_EQ(0, flags[2]) << "Flag for unsupported key should be unchanged.";
2683 ASSERT_EQ(1, flags[3]) << "Flag for unsupported key should be unchanged.";
2684
2685 // Event handling.
2686 RawEvent event;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002687 event.deviceId = EVENTHUB_ID;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002688 unused += mDevice->process(&event, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002689
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002690 ASSERT_NO_FATAL_FAILURE(mapper1.assertProcessWasCalled());
2691 ASSERT_NO_FATAL_FAILURE(mapper2.assertProcessWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002692}
2693
Yeabkal Wubshite03e8b12023-06-27 16:23:12 -07002694TEST_F(InputDeviceTest, WakeDevice_AddsWakeFlagToProcessNotifyArgs) {
2695 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "device.wake", "1");
2696 FakeInputMapper& mapper =
2697 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2698 AINPUT_SOURCE_KEYBOARD);
2699 NotifyMotionArgs args1;
2700 NotifySwitchArgs args2;
2701 NotifyKeyArgs args3;
2702 mapper.setProcessResult({args1, args2, args3});
2703
2704 InputReaderConfiguration config;
2705 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, config, /*changes=*/{});
2706
2707 RawEvent event;
2708 event.deviceId = EVENTHUB_ID;
2709 std::list<NotifyArgs> notifyArgs = mDevice->process(&event, 1);
2710
2711 for (auto& arg : notifyArgs) {
2712 if (const auto notifyMotionArgs = std::get_if<NotifyMotionArgs>(&arg)) {
2713 ASSERT_EQ(POLICY_FLAG_WAKE, notifyMotionArgs->policyFlags);
2714 } else if (const auto notifySwitchArgs = std::get_if<NotifySwitchArgs>(&arg)) {
2715 ASSERT_EQ(POLICY_FLAG_WAKE, notifySwitchArgs->policyFlags);
2716 } else if (const auto notifyKeyArgs = std::get_if<NotifyKeyArgs>(&arg)) {
2717 ASSERT_EQ(POLICY_FLAG_WAKE, notifyKeyArgs->policyFlags);
2718 }
2719 }
2720}
2721
2722TEST_F(InputDeviceTest, NotWakeDevice_DoesNotAddWakeFlagToProcessNotifyArgs) {
2723 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "device.wake", "0");
2724 FakeInputMapper& mapper =
2725 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2726 AINPUT_SOURCE_KEYBOARD);
2727 NotifyMotionArgs args;
2728 mapper.setProcessResult({args});
2729
2730 InputReaderConfiguration config;
2731 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, config, /*changes=*/{});
2732
2733 RawEvent event;
2734 event.deviceId = EVENTHUB_ID;
2735 std::list<NotifyArgs> notifyArgs = mDevice->process(&event, 1);
2736
2737 // POLICY_FLAG_WAKE is not added to the NotifyArgs.
2738 ASSERT_EQ(0u, std::get<NotifyMotionArgs>(notifyArgs.front()).policyFlags);
2739}
2740
2741TEST_F(InputDeviceTest, NotWakeDevice_DoesNotRemoveExistingWakeFlagFromProcessNotifyArgs) {
2742 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "device.wake", "0");
2743 FakeInputMapper& mapper =
2744 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2745 AINPUT_SOURCE_KEYBOARD);
2746 NotifyMotionArgs args;
2747 args.policyFlags = POLICY_FLAG_WAKE;
2748 mapper.setProcessResult({args});
2749
2750 InputReaderConfiguration config;
2751 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, config, /*changes=*/{});
2752
2753 RawEvent event;
2754 event.deviceId = EVENTHUB_ID;
2755 std::list<NotifyArgs> notifyArgs = mDevice->process(&event, 1);
2756
2757 // The POLICY_FLAG_WAKE is preserved, despite the device being a non-wake device.
2758 ASSERT_EQ(POLICY_FLAG_WAKE, std::get<NotifyMotionArgs>(notifyArgs.front()).policyFlags);
2759}
2760
Arthur Hung2c9a3342019-07-23 14:18:59 +08002761// A single input device is associated with a specific display. Check that:
2762// 1. Device is disabled if the viewport corresponding to the associated display is not found
Arpit Singh48189772023-05-30 14:12:49 +00002763// 2. Device is disabled when configure API is called
Arthur Hung2c9a3342019-07-23 14:18:59 +08002764TEST_F(InputDeviceTest, Configure_AssignsDisplayPort) {
Arpit Singh8e6fb252023-04-06 11:49:17 +00002765 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2766 AINPUT_SOURCE_TOUCHSCREEN);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002767
2768 // First Configuration.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002769 std::list<NotifyArgs> unused =
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002770 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2771 /*changes=*/{});
Arthur Hung2c9a3342019-07-23 14:18:59 +08002772
2773 // Device should be enabled by default.
2774 ASSERT_TRUE(mDevice->isEnabled());
2775
2776 // Prepare associated info.
2777 constexpr uint8_t hdmi = 1;
2778 const std::string UNIQUE_ID = "local:1";
2779
2780 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002781 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002782 InputReaderConfiguration::Change::DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002783 // Device should be disabled because it is associated with a specific display via
2784 // input port <-> display port association, but the corresponding display is not found
2785 ASSERT_FALSE(mDevice->isEnabled());
2786
2787 // Prepare displays.
2788 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +00002789 ui::ROTATION_0, /*isActive=*/true, UNIQUE_ID, hdmi,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002790 ViewportType::INTERNAL);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002791 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002792 InputReaderConfiguration::Change::DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002793 ASSERT_TRUE(mDevice->isEnabled());
2794
2795 // Device should be disabled after set disable.
2796 mFakePolicy->addDisabledDevice(mDevice->getId());
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002797 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002798 InputReaderConfiguration::Change::ENABLED_STATE);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002799 ASSERT_FALSE(mDevice->isEnabled());
2800
2801 // Device should still be disabled even found the associated display.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002802 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002803 InputReaderConfiguration::Change::DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002804 ASSERT_FALSE(mDevice->isEnabled());
2805}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002806
Christine Franks1ba71cc2021-04-07 14:37:42 -07002807TEST_F(InputDeviceTest, Configure_AssignsDisplayUniqueId) {
2808 // Device should be enabled by default.
2809 mFakePolicy->clearViewports();
Arpit Singh8e6fb252023-04-06 11:49:17 +00002810 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2811 AINPUT_SOURCE_KEYBOARD);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002812 std::list<NotifyArgs> unused =
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002813 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2814 /*changes=*/{});
Christine Franks1ba71cc2021-04-07 14:37:42 -07002815 ASSERT_TRUE(mDevice->isEnabled());
2816
2817 // Device should be disabled because it is associated with a specific display, but the
2818 // corresponding display is not found.
Christine Franks2a2293c2022-01-18 11:51:16 -08002819 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002820 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002821 InputReaderConfiguration::Change::DISPLAY_INFO);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002822 ASSERT_FALSE(mDevice->isEnabled());
2823
2824 // Device should be enabled when a display is found.
2825 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +00002826 ui::ROTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
Christine Franks1ba71cc2021-04-07 14:37:42 -07002827 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002828 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002829 InputReaderConfiguration::Change::DISPLAY_INFO);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002830 ASSERT_TRUE(mDevice->isEnabled());
2831
2832 // Device should be disabled after set disable.
2833 mFakePolicy->addDisabledDevice(mDevice->getId());
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002834 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002835 InputReaderConfiguration::Change::ENABLED_STATE);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002836 ASSERT_FALSE(mDevice->isEnabled());
2837
2838 // Device should still be disabled even found the associated display.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002839 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002840 InputReaderConfiguration::Change::DISPLAY_INFO);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002841 ASSERT_FALSE(mDevice->isEnabled());
2842}
2843
Christine Franks2a2293c2022-01-18 11:51:16 -08002844TEST_F(InputDeviceTest, Configure_UniqueId_CorrectlyMatches) {
2845 mFakePolicy->clearViewports();
Arpit Singh8e6fb252023-04-06 11:49:17 +00002846 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2847 AINPUT_SOURCE_KEYBOARD);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002848 std::list<NotifyArgs> unused =
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002849 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2850 /*changes=*/{});
Christine Franks2a2293c2022-01-18 11:51:16 -08002851
Christine Franks2a2293c2022-01-18 11:51:16 -08002852 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
2853 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +00002854 ui::ROTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
Christine Franks2a2293c2022-01-18 11:51:16 -08002855 NO_PORT, ViewportType::INTERNAL);
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 Franks2a2293c2022-01-18 11:51:16 -08002858 ASSERT_EQ(DISPLAY_UNIQUE_ID, mDevice->getAssociatedDisplayUniqueId());
2859}
2860
Siarhei Vishniakou30feb8c2022-09-28 10:48:29 -07002861/**
2862 * This test reproduces a crash caused by a dangling reference that remains after device is added
2863 * and removed. The reference is accessed in InputDevice::dump(..);
2864 */
2865TEST_F(InputDeviceTest, DumpDoesNotCrash) {
2866 constexpr int32_t TEST_EVENTHUB_ID = 10;
2867 mFakeEventHub->addDevice(TEST_EVENTHUB_ID, "Test EventHub device", InputDeviceClass::BATTERY);
2868
Harry Cutts33476232023-01-30 19:57:29 +00002869 InputDevice device(mReader->getContext(), /*id=*/1, /*generation=*/2, /*identifier=*/{});
Arpit Singh82f29a12023-06-13 15:05:53 +00002870 auto _ = device.addEventHubDevice(ARBITRARY_TIME, TEST_EVENTHUB_ID,
2871 mFakePolicy->getReaderConfiguration());
Siarhei Vishniakou30feb8c2022-09-28 10:48:29 -07002872 device.removeEventHubDevice(TEST_EVENTHUB_ID);
2873 std::string dumpStr, eventHubDevStr;
2874 device.dump(dumpStr, eventHubDevStr);
2875}
2876
Prabir Pradhanb54ffb22022-10-27 18:03:34 +00002877TEST_F(InputDeviceTest, GetBluetoothAddress) {
2878 const auto& address = mReader->getBluetoothAddress(DEVICE_ID);
2879 ASSERT_TRUE(address);
2880 ASSERT_EQ(DEVICE_BLUETOOTH_ADDRESS, *address);
2881}
2882
Prabir Pradhanf8d9e442023-12-06 22:06:13 +00002883TEST_F(InputDeviceTest, KernelBufferOverflowResetsMappers) {
2884 mFakePolicy->clearViewports();
2885 FakeInputMapper& mapper =
2886 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2887 AINPUT_SOURCE_KEYBOARD);
2888 std::list<NotifyArgs> unused =
2889 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2890 /*changes=*/{});
2891
2892 mapper.assertConfigureWasCalled();
2893 mapper.assertResetWasNotCalled();
2894
Siarhei Vishniakou1ff00cc2023-12-13 16:12:13 -08002895 RawEvent event{.when = ARBITRARY_TIME,
Prabir Pradhanf8d9e442023-12-06 22:06:13 +00002896 .readTime = ARBITRARY_TIME,
Siarhei Vishniakou1ff00cc2023-12-13 16:12:13 -08002897 .deviceId = EVENTHUB_ID,
Prabir Pradhanf8d9e442023-12-06 22:06:13 +00002898 .type = EV_SYN,
2899 .code = SYN_REPORT,
2900 .value = 0};
2901
2902 // Events are processed normally.
2903 unused = mDevice->process(&event, /*count=*/1);
2904 mapper.assertProcessWasCalled();
2905
2906 // Simulate a kernel buffer overflow, which generates a SYN_DROPPED event.
2907 // This should reset the mapper.
2908 event.type = EV_SYN;
2909 event.code = SYN_DROPPED;
2910 event.value = 0;
2911 unused = mDevice->process(&event, /*count=*/1);
2912 mapper.assertProcessWasNotCalled();
2913 mapper.assertResetWasCalled();
2914
2915 // All events until the next SYN_REPORT should be dropped.
2916 event.type = EV_KEY;
2917 event.code = KEY_A;
2918 event.value = 1;
2919 unused = mDevice->process(&event, /*count=*/1);
2920 mapper.assertProcessWasNotCalled();
2921
2922 // We get the SYN_REPORT event now, which is not forwarded to mappers.
2923 event.type = EV_SYN;
2924 event.code = SYN_REPORT;
2925 event.value = 0;
2926 unused = mDevice->process(&event, /*count=*/1);
2927 mapper.assertProcessWasNotCalled();
2928
2929 // The mapper receives events normally now.
2930 event.type = EV_KEY;
2931 event.code = KEY_B;
2932 event.value = 1;
2933 unused = mDevice->process(&event, /*count=*/1);
2934 mapper.assertProcessWasCalled();
2935}
2936
Michael Wrightd02c5b62014-02-10 15:10:22 -08002937// --- SwitchInputMapperTest ---
2938
2939class SwitchInputMapperTest : public InputMapperTest {
2940protected:
2941};
2942
2943TEST_F(SwitchInputMapperTest, GetSources) {
Arpit Singhdf992eb2023-04-26 16:12:10 +00002944 SwitchInputMapper& mapper = constructAndAddMapper<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002945
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002946 ASSERT_EQ(uint32_t(AINPUT_SOURCE_SWITCH), mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002947}
2948
2949TEST_F(SwitchInputMapperTest, GetSwitchState) {
Arpit Singhdf992eb2023-04-26 16:12:10 +00002950 SwitchInputMapper& mapper = constructAndAddMapper<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002951
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002952 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002953 ASSERT_EQ(1, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002954
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002955 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002956 ASSERT_EQ(0, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002957}
2958
2959TEST_F(SwitchInputMapperTest, Process) {
Arpit Singhdf992eb2023-04-26 16:12:10 +00002960 SwitchInputMapper& mapper = constructAndAddMapper<SwitchInputMapper>();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002961 std::list<NotifyArgs> out;
2962 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_LID, 1);
2963 ASSERT_TRUE(out.empty());
2964 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_JACK_PHYSICAL_INSERT, 1);
2965 ASSERT_TRUE(out.empty());
2966 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_HEADPHONE_INSERT, 0);
2967 ASSERT_TRUE(out.empty());
2968 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002969
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002970 ASSERT_EQ(1u, out.size());
2971 const NotifySwitchArgs& args = std::get<NotifySwitchArgs>(*out.begin());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002972 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
Dan Albert1bd2fc02016-02-02 15:11:57 -08002973 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT), args.switchValues);
2974 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT) | (1 << SW_HEADPHONE_INSERT),
Michael Wrightd02c5b62014-02-10 15:10:22 -08002975 args.switchMask);
2976 ASSERT_EQ(uint32_t(0), args.policyFlags);
2977}
2978
Chris Ye87143712020-11-10 05:05:58 +00002979// --- VibratorInputMapperTest ---
2980class VibratorInputMapperTest : public InputMapperTest {
2981protected:
2982 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::VIBRATOR); }
2983};
2984
2985TEST_F(VibratorInputMapperTest, GetSources) {
Arpit Singh0f26b302023-04-26 16:23:13 +00002986 VibratorInputMapper& mapper = constructAndAddMapper<VibratorInputMapper>();
Chris Ye87143712020-11-10 05:05:58 +00002987
2988 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mapper.getSources());
2989}
2990
2991TEST_F(VibratorInputMapperTest, GetVibratorIds) {
Arpit Singh0f26b302023-04-26 16:23:13 +00002992 VibratorInputMapper& mapper = constructAndAddMapper<VibratorInputMapper>();
Chris Ye87143712020-11-10 05:05:58 +00002993
2994 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
2995}
2996
2997TEST_F(VibratorInputMapperTest, Vibrate) {
2998 constexpr uint8_t DEFAULT_AMPLITUDE = 192;
Chris Yefb552902021-02-03 17:18:37 -08002999 constexpr int32_t VIBRATION_TOKEN = 100;
Arpit Singh0f26b302023-04-26 16:23:13 +00003000 VibratorInputMapper& mapper = constructAndAddMapper<VibratorInputMapper>();
Chris Ye87143712020-11-10 05:05:58 +00003001
3002 VibrationElement pattern(2);
3003 VibrationSequence sequence(2);
3004 pattern.duration = std::chrono::milliseconds(200);
Harry Cutts33476232023-01-30 19:57:29 +00003005 pattern.channels = {{/*vibratorId=*/0, DEFAULT_AMPLITUDE / 2},
3006 {/*vibratorId=*/1, DEFAULT_AMPLITUDE}};
Chris Ye87143712020-11-10 05:05:58 +00003007 sequence.addElement(pattern);
3008 pattern.duration = std::chrono::milliseconds(500);
Harry Cutts33476232023-01-30 19:57:29 +00003009 pattern.channels = {{/*vibratorId=*/0, DEFAULT_AMPLITUDE / 4},
3010 {/*vibratorId=*/1, DEFAULT_AMPLITUDE}};
Chris Ye87143712020-11-10 05:05:58 +00003011 sequence.addElement(pattern);
3012
3013 std::vector<int64_t> timings = {0, 1};
3014 std::vector<uint8_t> amplitudes = {DEFAULT_AMPLITUDE, DEFAULT_AMPLITUDE / 2};
3015
3016 ASSERT_FALSE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08003017 // Start vibrating
Harry Cutts33476232023-01-30 19:57:29 +00003018 std::list<NotifyArgs> out = mapper.vibrate(sequence, /*repeat=*/-1, VIBRATION_TOKEN);
Chris Ye87143712020-11-10 05:05:58 +00003019 ASSERT_TRUE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08003020 // Verify vibrator state listener was notified.
3021 mReader->loopOnce();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003022 ASSERT_EQ(1u, out.size());
3023 const NotifyVibratorStateArgs& vibrateArgs = std::get<NotifyVibratorStateArgs>(*out.begin());
3024 ASSERT_EQ(DEVICE_ID, vibrateArgs.deviceId);
3025 ASSERT_TRUE(vibrateArgs.isOn);
Chris Yefb552902021-02-03 17:18:37 -08003026 // Stop vibrating
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003027 out = mapper.cancelVibrate(VIBRATION_TOKEN);
Chris Yefb552902021-02-03 17:18:37 -08003028 ASSERT_FALSE(mapper.isVibrating());
3029 // Verify vibrator state listener was notified.
3030 mReader->loopOnce();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003031 ASSERT_EQ(1u, out.size());
3032 const NotifyVibratorStateArgs& cancelArgs = std::get<NotifyVibratorStateArgs>(*out.begin());
3033 ASSERT_EQ(DEVICE_ID, cancelArgs.deviceId);
3034 ASSERT_FALSE(cancelArgs.isOn);
Chris Ye87143712020-11-10 05:05:58 +00003035}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003036
Chris Yef59a2f42020-10-16 12:55:26 -07003037// --- SensorInputMapperTest ---
3038
3039class SensorInputMapperTest : public InputMapperTest {
3040protected:
3041 static const int32_t ACCEL_RAW_MIN;
3042 static const int32_t ACCEL_RAW_MAX;
3043 static const int32_t ACCEL_RAW_FUZZ;
3044 static const int32_t ACCEL_RAW_FLAT;
3045 static const int32_t ACCEL_RAW_RESOLUTION;
3046
3047 static const int32_t GYRO_RAW_MIN;
3048 static const int32_t GYRO_RAW_MAX;
3049 static const int32_t GYRO_RAW_FUZZ;
3050 static const int32_t GYRO_RAW_FLAT;
3051 static const int32_t GYRO_RAW_RESOLUTION;
3052
3053 static const float GRAVITY_MS2_UNIT;
3054 static const float DEGREE_RADIAN_UNIT;
3055
3056 void prepareAccelAxes();
3057 void prepareGyroAxes();
3058 void setAccelProperties();
3059 void setGyroProperties();
3060 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::SENSOR); }
3061};
3062
3063const int32_t SensorInputMapperTest::ACCEL_RAW_MIN = -32768;
3064const int32_t SensorInputMapperTest::ACCEL_RAW_MAX = 32768;
3065const int32_t SensorInputMapperTest::ACCEL_RAW_FUZZ = 16;
3066const int32_t SensorInputMapperTest::ACCEL_RAW_FLAT = 0;
3067const int32_t SensorInputMapperTest::ACCEL_RAW_RESOLUTION = 8192;
3068
3069const int32_t SensorInputMapperTest::GYRO_RAW_MIN = -2097152;
3070const int32_t SensorInputMapperTest::GYRO_RAW_MAX = 2097152;
3071const int32_t SensorInputMapperTest::GYRO_RAW_FUZZ = 16;
3072const int32_t SensorInputMapperTest::GYRO_RAW_FLAT = 0;
3073const int32_t SensorInputMapperTest::GYRO_RAW_RESOLUTION = 1024;
3074
3075const float SensorInputMapperTest::GRAVITY_MS2_UNIT = 9.80665f;
3076const float SensorInputMapperTest::DEGREE_RADIAN_UNIT = 0.0174533f;
3077
3078void SensorInputMapperTest::prepareAccelAxes() {
3079 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3080 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3081 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3082 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3083 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Z, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3084 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3085}
3086
3087void SensorInputMapperTest::prepareGyroAxes() {
3088 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RX, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3089 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3090 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RY, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3091 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3092 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RZ, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3093 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3094}
3095
3096void SensorInputMapperTest::setAccelProperties() {
3097 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 0, InputDeviceSensorType::ACCELEROMETER,
3098 /* sensorDataIndex */ 0);
3099 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 1, InputDeviceSensorType::ACCELEROMETER,
3100 /* sensorDataIndex */ 1);
3101 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 2, InputDeviceSensorType::ACCELEROMETER,
3102 /* sensorDataIndex */ 2);
3103 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
3104 addConfigurationProperty("sensor.accelerometer.reportingMode", "0");
3105 addConfigurationProperty("sensor.accelerometer.maxDelay", "100000");
3106 addConfigurationProperty("sensor.accelerometer.minDelay", "5000");
3107 addConfigurationProperty("sensor.accelerometer.power", "1.5");
3108}
3109
3110void SensorInputMapperTest::setGyroProperties() {
3111 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 3, InputDeviceSensorType::GYROSCOPE,
3112 /* sensorDataIndex */ 0);
3113 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 4, InputDeviceSensorType::GYROSCOPE,
3114 /* sensorDataIndex */ 1);
3115 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 5, InputDeviceSensorType::GYROSCOPE,
3116 /* sensorDataIndex */ 2);
3117 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
3118 addConfigurationProperty("sensor.gyroscope.reportingMode", "0");
3119 addConfigurationProperty("sensor.gyroscope.maxDelay", "100000");
3120 addConfigurationProperty("sensor.gyroscope.minDelay", "5000");
3121 addConfigurationProperty("sensor.gyroscope.power", "0.8");
3122}
3123
3124TEST_F(SensorInputMapperTest, GetSources) {
Arpit Singhfb706c32023-04-26 15:07:55 +00003125 SensorInputMapper& mapper = constructAndAddMapper<SensorInputMapper>();
Chris Yef59a2f42020-10-16 12:55:26 -07003126
3127 ASSERT_EQ(static_cast<uint32_t>(AINPUT_SOURCE_SENSOR), mapper.getSources());
3128}
3129
3130TEST_F(SensorInputMapperTest, ProcessAccelerometerSensor) {
3131 setAccelProperties();
3132 prepareAccelAxes();
Arpit Singhfb706c32023-04-26 15:07:55 +00003133 SensorInputMapper& mapper = constructAndAddMapper<SensorInputMapper>();
Chris Yef59a2f42020-10-16 12:55:26 -07003134
3135 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::ACCELEROMETER,
3136 std::chrono::microseconds(10000),
3137 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08003138 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003139 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, 20000);
3140 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, -20000);
3141 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Z, 40000);
3142 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
3143 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07003144
3145 NotifySensorArgs args;
3146 std::vector<float> values = {20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
3147 -20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
3148 40000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT};
3149
3150 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
3151 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
3152 ASSERT_EQ(args.deviceId, DEVICE_ID);
3153 ASSERT_EQ(args.sensorType, InputDeviceSensorType::ACCELEROMETER);
3154 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
3155 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
3156 ASSERT_EQ(args.values, values);
3157 mapper.flushSensor(InputDeviceSensorType::ACCELEROMETER);
3158}
3159
3160TEST_F(SensorInputMapperTest, ProcessGyroscopeSensor) {
3161 setGyroProperties();
3162 prepareGyroAxes();
Arpit Singhfb706c32023-04-26 15:07:55 +00003163 SensorInputMapper& mapper = constructAndAddMapper<SensorInputMapper>();
Chris Yef59a2f42020-10-16 12:55:26 -07003164
3165 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::GYROSCOPE,
3166 std::chrono::microseconds(10000),
3167 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08003168 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003169 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RX, 20000);
3170 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RY, -20000);
3171 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RZ, 40000);
3172 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
3173 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07003174
3175 NotifySensorArgs args;
3176 std::vector<float> values = {20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
3177 -20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
3178 40000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT};
3179
3180 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
3181 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
3182 ASSERT_EQ(args.deviceId, DEVICE_ID);
3183 ASSERT_EQ(args.sensorType, InputDeviceSensorType::GYROSCOPE);
3184 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
3185 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
3186 ASSERT_EQ(args.values, values);
3187 mapper.flushSensor(InputDeviceSensorType::GYROSCOPE);
3188}
3189
Michael Wrightd02c5b62014-02-10 15:10:22 -08003190// --- KeyboardInputMapperTest ---
3191
3192class KeyboardInputMapperTest : public InputMapperTest {
3193protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003194 const std::string UNIQUE_ID = "local:0";
Zixuan Qufecb6062022-11-12 04:44:31 +00003195 const KeyboardLayoutInfo DEVICE_KEYBOARD_LAYOUT_INFO = KeyboardLayoutInfo("en-US", "qwerty");
Michael Wrighta9cf4192022-12-01 23:46:39 +00003196 void prepareDisplay(ui::Rotation orientation);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003197
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003198 void testDPadKeyRotation(KeyboardInputMapper& mapper, int32_t originalScanCode,
Arthur Hung2c9a3342019-07-23 14:18:59 +08003199 int32_t originalKeyCode, int32_t rotatedKeyCode,
3200 int32_t displayId = ADISPLAY_ID_NONE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003201};
3202
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003203/* Similar to setDisplayInfoAndReconfigure, but pre-populates all parameters except for the
3204 * orientation.
3205 */
Michael Wrighta9cf4192022-12-01 23:46:39 +00003206void KeyboardInputMapperTest::prepareDisplay(ui::Rotation orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003207 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
3208 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003209}
3210
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003211void KeyboardInputMapperTest::testDPadKeyRotation(KeyboardInputMapper& mapper,
Arthur Hung2c9a3342019-07-23 14:18:59 +08003212 int32_t originalScanCode, int32_t originalKeyCode,
3213 int32_t rotatedKeyCode, int32_t displayId) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003214 NotifyKeyArgs args;
3215
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003216 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003217 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3218 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3219 ASSERT_EQ(originalScanCode, args.scanCode);
3220 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003221 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003222
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003223 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003224 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3225 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3226 ASSERT_EQ(originalScanCode, args.scanCode);
3227 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003228 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003229}
3230
Michael Wrightd02c5b62014-02-10 15:10:22 -08003231TEST_F(KeyboardInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003232 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003233 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003234 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003235
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003236 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003237}
3238
3239TEST_F(KeyboardInputMapperTest, Process_SimpleKeyPress) {
3240 const int32_t USAGE_A = 0x070004;
3241 const int32_t USAGE_UNKNOWN = 0x07ffff;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003242 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3243 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
Chris Yea52ade12020-08-27 16:49:20 -07003244 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, POLICY_FLAG_WAKE);
3245 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, POLICY_FLAG_WAKE);
3246 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003247
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003248 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003249 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003250 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003251 // Initial metastate is AMETA_NONE.
3252 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003253
3254 // Key down by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003255 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003256 NotifyKeyArgs args;
3257 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3258 ASSERT_EQ(DEVICE_ID, args.deviceId);
3259 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3260 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3261 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3262 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3263 ASSERT_EQ(KEY_HOME, args.scanCode);
3264 ASSERT_EQ(AMETA_NONE, args.metaState);
3265 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3266 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3267 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3268
3269 // Key up by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003270 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003271 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3272 ASSERT_EQ(DEVICE_ID, args.deviceId);
3273 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3274 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3275 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3276 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3277 ASSERT_EQ(KEY_HOME, args.scanCode);
3278 ASSERT_EQ(AMETA_NONE, args.metaState);
3279 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3280 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3281 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3282
3283 // Key down by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003284 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3285 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, 0, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003286 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3287 ASSERT_EQ(DEVICE_ID, args.deviceId);
3288 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3289 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3290 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3291 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3292 ASSERT_EQ(0, args.scanCode);
3293 ASSERT_EQ(AMETA_NONE, args.metaState);
3294 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3295 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3296 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3297
3298 // Key up by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003299 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3300 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003301 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3302 ASSERT_EQ(DEVICE_ID, args.deviceId);
3303 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3304 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3305 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3306 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3307 ASSERT_EQ(0, args.scanCode);
3308 ASSERT_EQ(AMETA_NONE, args.metaState);
3309 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3310 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3311 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3312
3313 // Key down with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003314 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3315 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UNKNOWN, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003316 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3317 ASSERT_EQ(DEVICE_ID, args.deviceId);
3318 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3319 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3320 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3321 ASSERT_EQ(0, args.keyCode);
3322 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3323 ASSERT_EQ(AMETA_NONE, args.metaState);
3324 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3325 ASSERT_EQ(0U, args.policyFlags);
3326 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3327
3328 // Key up with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003329 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3330 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003331 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3332 ASSERT_EQ(DEVICE_ID, args.deviceId);
3333 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3334 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3335 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3336 ASSERT_EQ(0, args.keyCode);
3337 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3338 ASSERT_EQ(AMETA_NONE, args.metaState);
3339 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3340 ASSERT_EQ(0U, args.policyFlags);
3341 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3342}
3343
Vaibhav Devmuraricbba14c2022-10-10 16:54:49 +00003344TEST_F(KeyboardInputMapperTest, Process_KeyRemapping) {
3345 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
3346 mFakeEventHub->addKey(EVENTHUB_ID, KEY_B, 0, AKEYCODE_B, 0);
3347 mFakeEventHub->addKeyRemapping(EVENTHUB_ID, AKEYCODE_A, AKEYCODE_B);
3348
3349 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003350 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Vaibhav Devmuraricbba14c2022-10-10 16:54:49 +00003351 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3352
3353 // Key down by scan code.
3354 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_A, 1);
3355 NotifyKeyArgs args;
3356 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3357 ASSERT_EQ(AKEYCODE_B, args.keyCode);
3358
3359 // Key up by scan code.
3360 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_A, 0);
3361 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3362 ASSERT_EQ(AKEYCODE_B, args.keyCode);
3363}
3364
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003365/**
3366 * Ensure that the readTime is set to the time when the EV_KEY is received.
3367 */
3368TEST_F(KeyboardInputMapperTest, Process_SendsReadTime) {
3369 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3370
3371 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003372 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003373 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3374 NotifyKeyArgs args;
3375
3376 // Key down
Harry Cutts33476232023-01-30 19:57:29 +00003377 process(mapper, ARBITRARY_TIME, /*readTime=*/12, EV_KEY, KEY_HOME, 1);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003378 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3379 ASSERT_EQ(12, args.readTime);
3380
3381 // Key up
Harry Cutts33476232023-01-30 19:57:29 +00003382 process(mapper, ARBITRARY_TIME, /*readTime=*/15, EV_KEY, KEY_HOME, 1);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003383 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3384 ASSERT_EQ(15, args.readTime);
3385}
3386
Michael Wrightd02c5b62014-02-10 15:10:22 -08003387TEST_F(KeyboardInputMapperTest, Process_ShouldUpdateMetaState) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003388 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFTSHIFT, 0, AKEYCODE_SHIFT_LEFT, 0);
3389 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003390 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, 0);
3391 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, 0);
3392 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003393
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003394 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003395 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003396 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003397
Arthur Hung95f68612022-04-07 14:08:22 +08003398 // Initial metastate is AMETA_NONE.
3399 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003400
3401 // Metakey down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003402 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003403 NotifyKeyArgs args;
3404 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3405 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003406 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003407 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003408
3409 // Key down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003410 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_A, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003411 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3412 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003413 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003414
3415 // Key up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003416 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, KEY_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003417 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3418 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003419 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003420
3421 // Metakey up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003422 process(mapper, ARBITRARY_TIME + 3, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003423 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3424 ASSERT_EQ(AMETA_NONE, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003425 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003426 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003427}
3428
3429TEST_F(KeyboardInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003430 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3431 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3432 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3433 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003434
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003435 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003436 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003437 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003438
Michael Wrighta9cf4192022-12-01 23:46:39 +00003439 prepareDisplay(ui::ROTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003440 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3441 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
3442 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3443 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
3444 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3445 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
3446 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3447 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
3448}
3449
3450TEST_F(KeyboardInputMapperTest, Process_WhenOrientationAware_ShouldRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003451 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3452 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3453 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3454 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003455
Michael Wrightd02c5b62014-02-10 15:10:22 -08003456 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003457 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003458 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003459 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003460
Michael Wrighta9cf4192022-12-01 23:46:39 +00003461 prepareDisplay(ui::ROTATION_0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003462 ASSERT_NO_FATAL_FAILURE(
3463 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3464 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3465 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3466 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3467 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3468 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3469 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003470
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003471 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003472 prepareDisplay(ui::ROTATION_90);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003473 ASSERT_NO_FATAL_FAILURE(
3474 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3475 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3476 AKEYCODE_DPAD_UP, DISPLAY_ID));
3477 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3478 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3479 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3480 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003481
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003482 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003483 prepareDisplay(ui::ROTATION_180);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003484 ASSERT_NO_FATAL_FAILURE(
3485 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3486 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3487 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3488 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3489 AKEYCODE_DPAD_UP, DISPLAY_ID));
3490 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3491 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003492
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003493 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003494 prepareDisplay(ui::ROTATION_270);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003495 ASSERT_NO_FATAL_FAILURE(
3496 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3497 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3498 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3499 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3500 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3501 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3502 AKEYCODE_DPAD_UP, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003503
3504 // Special case: if orientation changes while key is down, we still emit the same keycode
3505 // in the key up as we did in the key down.
3506 NotifyKeyArgs args;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003507 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003508 prepareDisplay(ui::ROTATION_270);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003509 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003510 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3511 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3512 ASSERT_EQ(KEY_UP, args.scanCode);
3513 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3514
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003515 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003516 prepareDisplay(ui::ROTATION_180);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003517 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003518 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3519 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3520 ASSERT_EQ(KEY_UP, args.scanCode);
3521 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3522}
3523
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003524TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_NotOrientationAware) {
3525 // If the keyboard is not orientation aware,
3526 // key events should not be associated with a specific display id
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003527 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003528
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003529 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003530 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003531 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003532 NotifyKeyArgs args;
3533
3534 // Display id should be ADISPLAY_ID_NONE without any display configuration.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003535 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003536 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003537 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003538 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3539 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3540
Michael Wrighta9cf4192022-12-01 23:46:39 +00003541 prepareDisplay(ui::ROTATION_0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003542 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003543 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003544 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003545 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3546 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3547}
3548
3549TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_OrientationAware) {
3550 // If the keyboard is orientation aware,
3551 // key events should be associated with the internal viewport
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003552 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003553
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003554 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003555 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003556 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003557 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003558 NotifyKeyArgs args;
3559
3560 // Display id should be ADISPLAY_ID_NONE without any display configuration.
3561 // ^--- already checked by the previous test
3562
Michael Wrighta9cf4192022-12-01 23:46:39 +00003563 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003564 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003565 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003566 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003567 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003568 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3569 ASSERT_EQ(DISPLAY_ID, args.displayId);
3570
3571 constexpr int32_t newDisplayId = 2;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003572 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003573 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003574 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003575 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003576 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003577 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003578 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3579 ASSERT_EQ(newDisplayId, args.displayId);
3580}
3581
Michael Wrightd02c5b62014-02-10 15:10:22 -08003582TEST_F(KeyboardInputMapperTest, GetKeyCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003583 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003584 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003585 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003586
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003587 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003588 ASSERT_EQ(1, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003589
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003590 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003591 ASSERT_EQ(0, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003592}
3593
Philip Junker4af3b3d2021-12-14 10:36:55 +01003594TEST_F(KeyboardInputMapperTest, GetKeyCodeForKeyLocation) {
3595 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003596 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Philip Junker4af3b3d2021-12-14 10:36:55 +01003597 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3598
3599 mFakeEventHub->addKeyCodeMapping(EVENTHUB_ID, AKEYCODE_Y, AKEYCODE_Z);
3600 ASSERT_EQ(AKEYCODE_Z, mapper.getKeyCodeForKeyLocation(AKEYCODE_Y))
3601 << "If a mapping is available, the result is equal to the mapping";
3602
3603 ASSERT_EQ(AKEYCODE_A, mapper.getKeyCodeForKeyLocation(AKEYCODE_A))
3604 << "If no mapping is available, the result is the key location";
3605}
3606
Michael Wrightd02c5b62014-02-10 15:10:22 -08003607TEST_F(KeyboardInputMapperTest, GetScanCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003608 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003609 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003610 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003611
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003612 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003613 ASSERT_EQ(1, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003614
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003615 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003616 ASSERT_EQ(0, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003617}
3618
3619TEST_F(KeyboardInputMapperTest, MarkSupportedKeyCodes) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003620 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003621 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003622 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003623
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003624 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003625
Michael Wrightd02c5b62014-02-10 15:10:22 -08003626 uint8_t flags[2] = { 0, 0 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07003627 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, {AKEYCODE_A, AKEYCODE_B}, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003628 ASSERT_TRUE(flags[0]);
3629 ASSERT_FALSE(flags[1]);
3630}
3631
3632TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleMetaStateAndLeds) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003633 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3634 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3635 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3636 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3637 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3638 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003639
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003640 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003641 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003642 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003643 // Initial metastate is AMETA_NONE.
3644 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003645
3646 // Initialization should have turned all of the lights off.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003647 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3648 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3649 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003650
3651 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003652 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3653 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003654 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3655 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3656 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003657 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003658
3659 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003660 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3661 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003662 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3663 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3664 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003665 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003666
3667 // Toggle caps lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003668 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3669 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003670 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3671 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3672 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003673 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003674
3675 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003676 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3677 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003678 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3679 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3680 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003681 ASSERT_EQ(AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003682
3683 // Toggle num lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003684 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3685 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003686 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3687 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3688 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003689 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003690
3691 // Toggle scroll lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003692 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3693 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003694 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3695 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3696 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003697 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003698}
3699
Chris Yea52ade12020-08-27 16:49:20 -07003700TEST_F(KeyboardInputMapperTest, NoMetaStateWhenMetaKeysNotPresent) {
3701 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_BUTTON_A, 0);
3702 mFakeEventHub->addKey(EVENTHUB_ID, BTN_B, 0, AKEYCODE_BUTTON_B, 0);
3703 mFakeEventHub->addKey(EVENTHUB_ID, BTN_X, 0, AKEYCODE_BUTTON_X, 0);
3704 mFakeEventHub->addKey(EVENTHUB_ID, BTN_Y, 0, AKEYCODE_BUTTON_Y, 0);
3705
3706 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003707 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Chris Yea52ade12020-08-27 16:49:20 -07003708 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
3709
Chris Yea52ade12020-08-27 16:49:20 -07003710 // Meta state should be AMETA_NONE after reset
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003711 std::list<NotifyArgs> unused = mapper.reset(ARBITRARY_TIME);
Chris Yea52ade12020-08-27 16:49:20 -07003712 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3713 // Meta state should be AMETA_NONE with update, as device doesn't have the keys.
3714 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
3715 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3716
3717 NotifyKeyArgs args;
3718 // Press button "A"
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003719 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_A, 1);
Chris Yea52ade12020-08-27 16:49:20 -07003720 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3721 ASSERT_EQ(AMETA_NONE, args.metaState);
3722 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3723 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3724 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3725
3726 // Button up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003727 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003728 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3729 ASSERT_EQ(AMETA_NONE, args.metaState);
3730 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3731 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3732 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3733}
3734
Arthur Hung2c9a3342019-07-23 14:18:59 +08003735TEST_F(KeyboardInputMapperTest, Configure_AssignsDisplayPort) {
3736 // keyboard 1.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003737 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3738 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3739 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3740 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003741
3742 // keyboard 2.
3743 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08003744 const std::string DEVICE_NAME2 = "KEYBOARD2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08003745 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003746 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08003747 std::shared_ptr<InputDevice> device2 =
3748 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003749 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08003750
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003751 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3752 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3753 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3754 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003755
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003756 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003757 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003758 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003759
Arpit Singh67ca6842023-04-26 14:43:16 +00003760 device2->addEmptyEventHubDevice(SECOND_EVENTHUB_ID);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003761 KeyboardInputMapper& mapper2 =
Arpit Singh67ca6842023-04-26 14:43:16 +00003762 device2->constructAndAddMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID,
3763 mFakePolicy
3764 ->getReaderConfiguration(),
3765 AINPUT_SOURCE_KEYBOARD,
3766 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003767 std::list<NotifyArgs> unused =
3768 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003769 /*changes=*/{});
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003770 unused += device2->reset(ARBITRARY_TIME);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003771
3772 // Prepared displays and associated info.
3773 constexpr uint8_t hdmi1 = 0;
3774 constexpr uint8_t hdmi2 = 1;
3775 const std::string SECONDARY_UNIQUE_ID = "local:1";
3776
3777 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
3778 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
3779
3780 // No associated display viewport found, should disable the device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003781 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003782 InputReaderConfiguration::Change::DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003783 ASSERT_FALSE(device2->isEnabled());
3784
3785 // Prepare second display.
3786 constexpr int32_t newDisplayId = 2;
Michael Wrighta9cf4192022-12-01 23:46:39 +00003787 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003788 UNIQUE_ID, hdmi1, ViewportType::INTERNAL);
Michael Wrighta9cf4192022-12-01 23:46:39 +00003789 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003790 SECONDARY_UNIQUE_ID, hdmi2, ViewportType::EXTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003791 // Default device will reconfigure above, need additional reconfiguration for another device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003792 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003793 InputReaderConfiguration::Change::DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003794
3795 // Device should be enabled after the associated display is found.
3796 ASSERT_TRUE(mDevice->isEnabled());
3797 ASSERT_TRUE(device2->isEnabled());
3798
3799 // Test pad key events
3800 ASSERT_NO_FATAL_FAILURE(
3801 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3802 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3803 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3804 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3805 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3806 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3807 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3808
3809 ASSERT_NO_FATAL_FAILURE(
3810 testDPadKeyRotation(mapper2, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, newDisplayId));
3811 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3812 AKEYCODE_DPAD_RIGHT, newDisplayId));
3813 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3814 AKEYCODE_DPAD_DOWN, newDisplayId));
3815 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3816 AKEYCODE_DPAD_LEFT, newDisplayId));
3817}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003818
arthurhungc903df12020-08-11 15:08:42 +08003819TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleAfterReattach) {
3820 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3821 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3822 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3823 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3824 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3825 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3826
3827 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003828 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
arthurhungc903df12020-08-11 15:08:42 +08003829 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003830 // Initial metastate is AMETA_NONE.
3831 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08003832
3833 // Initialization should have turned all of the lights off.
3834 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3835 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3836 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3837
3838 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003839 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3840 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003841 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3842 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
3843
3844 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003845 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3846 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003847 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3848 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
3849
3850 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003851 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3852 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003853 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3854 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
3855
3856 mFakeEventHub->removeDevice(EVENTHUB_ID);
3857 mReader->loopOnce();
3858
3859 // keyboard 2 should default toggle keys.
3860 const std::string USB2 = "USB2";
3861 const std::string DEVICE_NAME2 = "KEYBOARD2";
3862 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
3863 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
3864 std::shared_ptr<InputDevice> device2 =
3865 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003866 ftl::Flags<InputDeviceClass>(0));
arthurhungc903df12020-08-11 15:08:42 +08003867 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3868 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
3869 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3870 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3871 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3872 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3873
Arpit Singh67ca6842023-04-26 14:43:16 +00003874 device2->addEmptyEventHubDevice(SECOND_EVENTHUB_ID);
arthurhung6fe95782020-10-05 22:41:16 +08003875 KeyboardInputMapper& mapper2 =
Arpit Singh67ca6842023-04-26 14:43:16 +00003876 device2->constructAndAddMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID,
3877 mFakePolicy
3878 ->getReaderConfiguration(),
3879 AINPUT_SOURCE_KEYBOARD,
3880 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003881 std::list<NotifyArgs> unused =
3882 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003883 /*changes=*/{});
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003884 unused += device2->reset(ARBITRARY_TIME);
arthurhungc903df12020-08-11 15:08:42 +08003885
3886 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_CAPSL));
3887 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_NUML));
3888 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_SCROLLL));
arthurhung6fe95782020-10-05 22:41:16 +08003889 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON,
3890 mapper2.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08003891}
3892
Arthur Hungcb40a002021-08-03 14:31:01 +00003893TEST_F(KeyboardInputMapperTest, Process_toggleCapsLockState) {
3894 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3895 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3896 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3897
3898 // Suppose we have two mappers. (DPAD + KEYBOARD)
Arpit Singh67ca6842023-04-26 14:43:16 +00003899 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_DPAD,
Arthur Hungcb40a002021-08-03 14:31:01 +00003900 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
3901 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003902 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Arthur Hungcb40a002021-08-03 14:31:01 +00003903 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003904 // Initial metastate is AMETA_NONE.
3905 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Arthur Hungcb40a002021-08-03 14:31:01 +00003906
3907 mReader->toggleCapsLockState(DEVICE_ID);
3908 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
3909}
3910
Arthur Hungfb3cc112022-04-13 07:39:50 +00003911TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleInMultiDevices) {
3912 // keyboard 1.
3913 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3914 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3915 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3916 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3917 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3918 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3919
3920 KeyboardInputMapper& mapper1 =
Arpit Singh67ca6842023-04-26 14:43:16 +00003921 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Arthur Hungfb3cc112022-04-13 07:39:50 +00003922 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3923
3924 // keyboard 2.
3925 const std::string USB2 = "USB2";
3926 const std::string DEVICE_NAME2 = "KEYBOARD2";
3927 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
3928 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
3929 std::shared_ptr<InputDevice> device2 =
3930 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
3931 ftl::Flags<InputDeviceClass>(0));
3932 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3933 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
3934 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3935 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3936 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3937 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3938
Arpit Singh67ca6842023-04-26 14:43:16 +00003939 device2->addEmptyEventHubDevice(SECOND_EVENTHUB_ID);
Arthur Hungfb3cc112022-04-13 07:39:50 +00003940 KeyboardInputMapper& mapper2 =
Arpit Singh67ca6842023-04-26 14:43:16 +00003941 device2->constructAndAddMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID,
3942 mFakePolicy
3943 ->getReaderConfiguration(),
3944 AINPUT_SOURCE_KEYBOARD,
3945 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003946 std::list<NotifyArgs> unused =
3947 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003948 /*changes=*/{});
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003949 unused += device2->reset(ARBITRARY_TIME);
Arthur Hungfb3cc112022-04-13 07:39:50 +00003950
Arthur Hung95f68612022-04-07 14:08:22 +08003951 // Initial metastate is AMETA_NONE.
3952 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
3953 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
3954
3955 // Toggle num lock on and off.
3956 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3957 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Arthur Hungfb3cc112022-04-13 07:39:50 +00003958 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3959 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper1.getMetaState());
3960 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper2.getMetaState());
3961
3962 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3963 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
3964 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3965 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
3966 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
3967
3968 // Toggle caps lock on and off.
3969 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3970 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
3971 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3972 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper1.getMetaState());
3973 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper2.getMetaState());
3974
3975 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3976 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
3977 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3978 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
3979 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
3980
3981 // Toggle scroll lock on and off.
3982 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3983 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
3984 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3985 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper1.getMetaState());
3986 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper2.getMetaState());
3987
3988 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3989 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
3990 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3991 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
3992 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
3993}
3994
Arthur Hung2141d542022-08-23 07:45:21 +00003995TEST_F(KeyboardInputMapperTest, Process_DisabledDevice) {
3996 const int32_t USAGE_A = 0x070004;
3997 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3998 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
3999
4000 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00004001 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Arthur Hung2141d542022-08-23 07:45:21 +00004002 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
4003 // Key down by scan code.
4004 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
4005 NotifyKeyArgs args;
4006 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4007 ASSERT_EQ(DEVICE_ID, args.deviceId);
4008 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
4009 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4010 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
4011 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4012 ASSERT_EQ(KEY_HOME, args.scanCode);
4013 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
4014
4015 // Disable device, it should synthesize cancellation events for down events.
4016 mFakePolicy->addDisabledDevice(DEVICE_ID);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004017 configureDevice(InputReaderConfiguration::Change::ENABLED_STATE);
Arthur Hung2141d542022-08-23 07:45:21 +00004018
4019 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4020 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
4021 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4022 ASSERT_EQ(KEY_HOME, args.scanCode);
4023 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_CANCELED, args.flags);
4024}
4025
Zixuan Qufecb6062022-11-12 04:44:31 +00004026TEST_F(KeyboardInputMapperTest, Configure_AssignKeyboardLayoutInfo) {
Arpit Singh67ca6842023-04-26 14:43:16 +00004027 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4028 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Zixuan Qufecb6062022-11-12 04:44:31 +00004029 std::list<NotifyArgs> unused =
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004030 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
4031 /*changes=*/{});
Zixuan Qufecb6062022-11-12 04:44:31 +00004032
Vaibhav Devmurari0a6fee82023-04-11 18:53:04 +00004033 uint32_t generation = mReader->getContext()->getGeneration();
Zixuan Qufecb6062022-11-12 04:44:31 +00004034 mFakePolicy->addKeyboardLayoutAssociation(DEVICE_LOCATION, DEVICE_KEYBOARD_LAYOUT_INFO);
4035
4036 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004037 InputReaderConfiguration::Change::KEYBOARD_LAYOUT_ASSOCIATION);
Zixuan Qufecb6062022-11-12 04:44:31 +00004038
4039 InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
4040 ASSERT_EQ(DEVICE_KEYBOARD_LAYOUT_INFO.languageTag,
4041 deviceInfo.getKeyboardLayoutInfo()->languageTag);
4042 ASSERT_EQ(DEVICE_KEYBOARD_LAYOUT_INFO.layoutType,
4043 deviceInfo.getKeyboardLayoutInfo()->layoutType);
Vaibhav Devmurari0a6fee82023-04-11 18:53:04 +00004044 ASSERT_TRUE(mReader->getContext()->getGeneration() != generation);
4045
4046 // Call change layout association with the same values: Generation shouldn't change
4047 generation = mReader->getContext()->getGeneration();
4048 mFakePolicy->addKeyboardLayoutAssociation(DEVICE_LOCATION, DEVICE_KEYBOARD_LAYOUT_INFO);
4049 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
4050 InputReaderConfiguration::Change::KEYBOARD_LAYOUT_ASSOCIATION);
4051 ASSERT_TRUE(mReader->getContext()->getGeneration() == generation);
Zixuan Qufecb6062022-11-12 04:44:31 +00004052}
4053
Vaibhav Devmurari7fb41132023-01-02 13:30:26 +00004054TEST_F(KeyboardInputMapperTest, LayoutInfoCorrectlyMapped) {
4055 mFakeEventHub->setRawLayoutInfo(EVENTHUB_ID,
4056 RawLayoutInfo{.languageTag = "en", .layoutType = "extended"});
4057
4058 // Configuration
Arpit Singh67ca6842023-04-26 14:43:16 +00004059 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Vaibhav Devmurari7fb41132023-01-02 13:30:26 +00004060 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
4061 InputReaderConfiguration config;
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004062 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, config, /*changes=*/{});
Vaibhav Devmurari7fb41132023-01-02 13:30:26 +00004063
4064 ASSERT_EQ("en", mDevice->getDeviceInfo().getKeyboardLayoutInfo()->languageTag);
4065 ASSERT_EQ("extended", mDevice->getDeviceInfo().getKeyboardLayoutInfo()->layoutType);
4066}
4067
Justin Chung71ddb432023-03-27 04:29:07 +00004068TEST_F(KeyboardInputMapperTest, Process_GesureEventToSetFlagKeepTouchMode) {
4069 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, POLICY_FLAG_GESTURE);
4070 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00004071 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Justin Chung71ddb432023-03-27 04:29:07 +00004072 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
4073 NotifyKeyArgs args;
4074
4075 // Key down
4076 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_LEFT, 1);
4077 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4078 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_KEEP_TOUCH_MODE, args.flags);
4079}
4080
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004081// --- KeyboardInputMapperTest_ExternalDevice ---
4082
4083class KeyboardInputMapperTest_ExternalDevice : public InputMapperTest {
4084protected:
Chris Yea52ade12020-08-27 16:49:20 -07004085 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004086};
4087
4088TEST_F(KeyboardInputMapperTest_ExternalDevice, WakeBehavior) {
Vaibhav Devmurari16257862023-03-06 10:06:32 +00004089 // For external devices, keys will trigger wake on key down. Media keys should also trigger
4090 // wake if triggered from external devices.
Powei Fengd041c5d2019-05-03 17:11:33 -07004091
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004092 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, 0);
4093 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, 0);
4094 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAYPAUSE, 0, AKEYCODE_MEDIA_PLAY_PAUSE,
4095 POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07004096
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004097 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00004098 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004099 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07004100
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004101 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004102 NotifyKeyArgs args;
4103 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4104 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4105
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004106 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004107 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4108 ASSERT_EQ(uint32_t(0), args.policyFlags);
4109
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004110 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004111 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Vaibhav Devmurari16257862023-03-06 10:06:32 +00004112 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
Powei Fengd041c5d2019-05-03 17:11:33 -07004113
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004114 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004115 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4116 ASSERT_EQ(uint32_t(0), args.policyFlags);
4117
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004118 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004119 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4120 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4121
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004122 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004123 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4124 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4125}
4126
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004127TEST_F(KeyboardInputMapperTest_ExternalDevice, DoNotWakeByDefaultBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07004128 // Tv Remote key's wake behavior is prescribed by the keylayout file.
Powei Fengd041c5d2019-05-03 17:11:33 -07004129
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004130 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4131 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
4132 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07004133
Powei Fengd041c5d2019-05-03 17:11:33 -07004134 addConfigurationProperty("keyboard.doNotWakeByDefault", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004135 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00004136 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004137 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07004138
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004139 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004140 NotifyKeyArgs args;
4141 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4142 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4143
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004144 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004145 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4146 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4147
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004148 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_DOWN, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004149 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4150 ASSERT_EQ(uint32_t(0), args.policyFlags);
4151
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004152 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_DOWN, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004153 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4154 ASSERT_EQ(uint32_t(0), args.policyFlags);
4155
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004156 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004157 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4158 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4159
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004160 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004161 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4162 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4163}
4164
Byoungho Jungda10dd32023-10-06 17:03:45 +09004165// --- CursorInputMapperTestBase ---
Michael Wrightd02c5b62014-02-10 15:10:22 -08004166
Byoungho Jungda10dd32023-10-06 17:03:45 +09004167class CursorInputMapperTestBase : public InputMapperTest {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004168protected:
4169 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD;
4170
Michael Wright17db18e2020-06-26 20:51:44 +01004171 std::shared_ptr<FakePointerController> mFakePointerController;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004172
Chris Yea52ade12020-08-27 16:49:20 -07004173 void SetUp() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004174 InputMapperTest::SetUp();
4175
Michael Wright17db18e2020-06-26 20:51:44 +01004176 mFakePointerController = std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00004177 mFakePolicy->setPointerController(mFakePointerController);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004178 }
4179
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004180 void testMotionRotation(CursorInputMapper& mapper, int32_t originalX, int32_t originalY,
4181 int32_t rotatedX, int32_t rotatedY);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004182
Michael Wrighta9cf4192022-12-01 23:46:39 +00004183 void prepareDisplay(ui::Rotation orientation) {
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004184 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation,
4185 DISPLAY_UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
4186 }
4187
4188 void prepareSecondaryDisplay() {
4189 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +00004190 ui::ROTATION_0, SECONDARY_DISPLAY_UNIQUE_ID, NO_PORT,
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004191 ViewportType::EXTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004192 }
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004193
4194 static void assertCursorPointerCoords(const PointerCoords& coords, float x, float y,
4195 float pressure) {
4196 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(coords, x, y, pressure, 0.0f, 0.0f, 0.0f, 0.0f,
4197 0.0f, 0.0f, 0.0f, EPSILON));
4198 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08004199};
4200
Byoungho Jungda10dd32023-10-06 17:03:45 +09004201const int32_t CursorInputMapperTestBase::TRACKBALL_MOVEMENT_THRESHOLD = 6;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004202
Byoungho Jungda10dd32023-10-06 17:03:45 +09004203void CursorInputMapperTestBase::testMotionRotation(CursorInputMapper& mapper, int32_t originalX,
4204 int32_t originalY, int32_t rotatedX,
4205 int32_t rotatedY) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004206 NotifyMotionArgs args;
4207
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004208 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, originalX);
4209 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, originalY);
4210 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004211 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4212 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004213 ASSERT_NO_FATAL_FAILURE(
4214 assertCursorPointerCoords(args.pointerCoords[0],
4215 float(rotatedX) / TRACKBALL_MOVEMENT_THRESHOLD,
4216 float(rotatedY) / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004217}
4218
Byoungho Jungda10dd32023-10-06 17:03:45 +09004219// --- CursorInputMapperTest ---
4220
4221class CursorInputMapperTest : public CursorInputMapperTestBase {
4222protected:
4223 void SetUp() override {
4224 input_flags::enable_pointer_choreographer(false);
4225 CursorInputMapperTestBase::SetUp();
4226 }
4227};
4228
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004229TEST_F(CursorInputMapperTest, Process_WhenOrientationAware_ShouldNotRotateMotions) {
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004230 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004231 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004232 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
4233 // need to be rotated.
4234 addConfigurationProperty("cursor.orientationAware", "1");
Arpit Singhe036ad72023-04-27 12:48:15 +00004235 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004236
Michael Wrighta9cf4192022-12-01 23:46:39 +00004237 prepareDisplay(ui::ROTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004238 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4239 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4240 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4241 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4242 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4243 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4244 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4245 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4246}
4247
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004248TEST_F(CursorInputMapperTest, Process_WhenNotOrientationAware_ShouldRotateMotions) {
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004249 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004250 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004251 // Since InputReader works in the un-rotated coordinate space, only devices that are not
4252 // orientation-aware are affected by display rotation.
Arpit Singhe036ad72023-04-27 12:48:15 +00004253 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004254
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004255 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00004256 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004257 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4258 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4259 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4260 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4261 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4262 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4263 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4264 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4265
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004266 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00004267 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004268 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, -1, 0));
4269 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, 1));
4270 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, 1));
4271 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, 1));
4272 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 1, 0));
4273 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, -1));
4274 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, -1));
4275 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, -1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004276
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004277 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00004278 prepareDisplay(ui::ROTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004279 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, -1));
4280 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, -1));
4281 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, -1, 0));
4282 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, 1));
4283 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, 1));
4284 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, 1));
4285 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 1, 0));
4286 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, -1));
4287
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004288 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00004289 prepareDisplay(ui::ROTATION_270);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004290 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 1, 0));
4291 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, -1));
4292 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, -1));
4293 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, -1));
4294 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, -1, 0));
4295 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, 1));
4296 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, 1));
4297 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, 1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004298}
4299
Prabir Pradhan208360b2022-06-24 18:37:04 +00004300TEST_F(CursorInputMapperTest, PointerCaptureDisablesOrientationChanges) {
4301 addConfigurationProperty("cursor.mode", "pointer");
Arpit Singhe036ad72023-04-27 12:48:15 +00004302 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Prabir Pradhan208360b2022-06-24 18:37:04 +00004303
4304 NotifyDeviceResetArgs resetArgs;
4305 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4306 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4307 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4308
4309 // Ensure the display is rotated.
Michael Wrighta9cf4192022-12-01 23:46:39 +00004310 prepareDisplay(ui::ROTATION_90);
Prabir Pradhan208360b2022-06-24 18:37:04 +00004311
4312 NotifyMotionArgs args;
4313
4314 // Verify that the coordinates are rotated.
4315 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4316 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4317 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4318 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4319 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4320 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4321 ASSERT_EQ(-20, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X));
4322 ASSERT_EQ(10, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y));
4323
4324 // Enable Pointer Capture.
4325 mFakePolicy->setPointerCapture(true);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004326 configureDevice(InputReaderConfiguration::Change::POINTER_CAPTURE);
Prabir Pradhan208360b2022-06-24 18:37:04 +00004327 NotifyPointerCaptureChangedArgs captureArgs;
4328 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyCaptureWasCalled(&captureArgs));
4329 ASSERT_TRUE(captureArgs.request.enable);
4330
4331 // Move and verify rotation is not applied.
4332 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4333 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4334 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4335 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4336 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4337 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4338 ASSERT_EQ(10, args.pointerCoords[0].getX());
4339 ASSERT_EQ(20, args.pointerCoords[0].getY());
4340}
4341
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004342TEST_F(CursorInputMapperTest, ConfigureDisplayId_NoAssociatedViewport) {
Arpit Singhe036ad72023-04-27 12:48:15 +00004343 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004344
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004345 // Set up the default display.
Michael Wrighta9cf4192022-12-01 23:46:39 +00004346 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004347
4348 // Set up the secondary display as the display on which the pointer should be shown.
4349 // The InputDevice is not associated with any display.
4350 prepareSecondaryDisplay();
4351 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004352 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Garfield Tan888a6a42020-01-09 11:39:16 -08004353
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004354 mFakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004355 mFakePointerController->setPosition(100, 200);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004356
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004357 // Ensure input events are generated for the secondary display.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004358 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4359 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4360 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004361 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan739dca42022-09-09 20:12:01 +00004362 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4363 WithSource(AINPUT_SOURCE_MOUSE), WithDisplayId(SECONDARY_DISPLAY_ID),
4364 WithCoords(110.0f, 220.0f))));
Harry Cuttsce86cc32022-12-14 20:36:33 +00004365 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(110.0f, 220.0f));
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004366}
4367
4368TEST_F(CursorInputMapperTest, ConfigureDisplayId_WithAssociatedViewport) {
Arpit Singhe036ad72023-04-27 12:48:15 +00004369 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004370
4371 // Set up the default display.
Michael Wrighta9cf4192022-12-01 23:46:39 +00004372 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004373
4374 // Set up the secondary display as the display on which the pointer should be shown,
4375 // and associate the InputDevice with the secondary display.
4376 prepareSecondaryDisplay();
4377 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
4378 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, SECONDARY_DISPLAY_UNIQUE_ID);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004379 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004380
4381 mFakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
4382 mFakePointerController->setPosition(100, 200);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004383
4384 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4385 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4386 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4387 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan739dca42022-09-09 20:12:01 +00004388 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4389 WithSource(AINPUT_SOURCE_MOUSE), WithDisplayId(SECONDARY_DISPLAY_ID),
4390 WithCoords(110.0f, 220.0f))));
Harry Cuttsce86cc32022-12-14 20:36:33 +00004391 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(110.0f, 220.0f));
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004392}
4393
4394TEST_F(CursorInputMapperTest, ConfigureDisplayId_IgnoresEventsForMismatchedPointerDisplay) {
Arpit Singhe036ad72023-04-27 12:48:15 +00004395 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004396
4397 // Set up the default display as the display on which the pointer should be shown.
Michael Wrighta9cf4192022-12-01 23:46:39 +00004398 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004399 mFakePolicy->setDefaultPointerDisplayId(DISPLAY_ID);
4400
4401 // Associate the InputDevice with the secondary display.
4402 prepareSecondaryDisplay();
4403 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, SECONDARY_DISPLAY_UNIQUE_ID);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004404 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004405
4406 // The mapper should not generate any events because it is associated with a display that is
4407 // different from the pointer display.
4408 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4409 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4410 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4411 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004412}
4413
Byoungho Jungda10dd32023-10-06 17:03:45 +09004414// --- CursorInputMapperTestWithChoreographer ---
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00004415
Prabir Pradhancc7268a2023-11-16 18:54:13 +00004416// TODO(b/311416205): De-duplicate the test cases after the refactoring is complete and the flagging
4417// logic can be removed.
Byoungho Jungda10dd32023-10-06 17:03:45 +09004418class CursorInputMapperTestWithChoreographer : public CursorInputMapperTestBase {
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00004419protected:
4420 void SetUp() override {
Byoungho Jungda10dd32023-10-06 17:03:45 +09004421 input_flags::enable_pointer_choreographer(true);
4422 CursorInputMapperTestBase::SetUp();
4423 }
4424};
4425
Prabir Pradhan19767602023-11-03 16:53:31 +00004426TEST_F(CursorInputMapperTestWithChoreographer, ConfigureDisplayIdWithAssociatedViewport) {
Byoungho Jungda10dd32023-10-06 17:03:45 +09004427 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
4428
4429 // Set up the default display.
4430 prepareDisplay(ui::ROTATION_90);
4431
4432 // Set up the secondary display as the display on which the pointer should be shown,
4433 // and associate the InputDevice with the secondary display.
4434 prepareSecondaryDisplay();
4435 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
4436 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, SECONDARY_DISPLAY_UNIQUE_ID);
4437 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
4438
4439 mFakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
4440 mFakePointerController->setPosition(100, 200);
4441
4442 // Ensure input events are generated with associated display ID but not with coords,
4443 // because the coords will be decided later by PointerChoreographer.
4444 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4445 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4446 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4447 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4448 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4449 WithSource(AINPUT_SOURCE_MOUSE), WithDisplayId(SECONDARY_DISPLAY_ID),
4450 WithCoords(0.0f, 0.0f))));
4451}
4452
4453TEST_F(CursorInputMapperTestWithChoreographer,
Prabir Pradhan19767602023-11-03 16:53:31 +00004454 ConfigureDisplayIdShouldGenerateEventWithMismatchedPointerDisplay) {
Byoungho Jungda10dd32023-10-06 17:03:45 +09004455 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
4456
4457 // Set up the default display as the display on which the pointer should be shown.
4458 prepareDisplay(ui::ROTATION_90);
4459 mFakePolicy->setDefaultPointerDisplayId(DISPLAY_ID);
4460
4461 // Associate the InputDevice with the secondary display.
4462 prepareSecondaryDisplay();
4463 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, SECONDARY_DISPLAY_UNIQUE_ID);
4464 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
4465
4466 // With PointerChoreographer enabled, there could be a PointerController for the associated
4467 // display even if it is different from the pointer display. So the mapper should generate an
4468 // event.
4469 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4470 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4471 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4472 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4473 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4474 WithSource(AINPUT_SOURCE_MOUSE), WithDisplayId(SECONDARY_DISPLAY_ID),
4475 WithCoords(0.0f, 0.0f))));
4476}
4477
Michael Wrightd02c5b62014-02-10 15:10:22 -08004478// --- TouchInputMapperTest ---
4479
4480class TouchInputMapperTest : public InputMapperTest {
4481protected:
4482 static const int32_t RAW_X_MIN;
4483 static const int32_t RAW_X_MAX;
4484 static const int32_t RAW_Y_MIN;
4485 static const int32_t RAW_Y_MAX;
4486 static const int32_t RAW_TOUCH_MIN;
4487 static const int32_t RAW_TOUCH_MAX;
4488 static const int32_t RAW_TOOL_MIN;
4489 static const int32_t RAW_TOOL_MAX;
4490 static const int32_t RAW_PRESSURE_MIN;
4491 static const int32_t RAW_PRESSURE_MAX;
4492 static const int32_t RAW_ORIENTATION_MIN;
4493 static const int32_t RAW_ORIENTATION_MAX;
4494 static const int32_t RAW_DISTANCE_MIN;
4495 static const int32_t RAW_DISTANCE_MAX;
4496 static const int32_t RAW_TILT_MIN;
4497 static const int32_t RAW_TILT_MAX;
4498 static const int32_t RAW_ID_MIN;
4499 static const int32_t RAW_ID_MAX;
4500 static const int32_t RAW_SLOT_MIN;
4501 static const int32_t RAW_SLOT_MAX;
4502 static const float X_PRECISION;
4503 static const float Y_PRECISION;
Santos Cordonfa5cf462017-04-05 10:37:00 -07004504 static const float X_PRECISION_VIRTUAL;
4505 static const float Y_PRECISION_VIRTUAL;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004506
4507 static const float GEOMETRIC_SCALE;
Jason Gerecke489fda82012-09-07 17:19:40 -07004508 static const TouchAffineTransformation AFFINE_TRANSFORM;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004509
4510 static const VirtualKeyDefinition VIRTUAL_KEYS[2];
4511
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004512 const std::string UNIQUE_ID = "local:0";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004513 const std::string SECONDARY_UNIQUE_ID = "local:1";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004514
Michael Wrightd02c5b62014-02-10 15:10:22 -08004515 enum Axes {
4516 POSITION = 1 << 0,
4517 TOUCH = 1 << 1,
4518 TOOL = 1 << 2,
4519 PRESSURE = 1 << 3,
4520 ORIENTATION = 1 << 4,
4521 MINOR = 1 << 5,
4522 ID = 1 << 6,
4523 DISTANCE = 1 << 7,
4524 TILT = 1 << 8,
4525 SLOT = 1 << 9,
4526 TOOL_TYPE = 1 << 10,
4527 };
4528
Michael Wrighta9cf4192022-12-01 23:46:39 +00004529 void prepareDisplay(ui::Rotation orientation, std::optional<uint8_t> port = NO_PORT);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004530 void prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port = NO_PORT);
Michael Wrighta9cf4192022-12-01 23:46:39 +00004531 void prepareVirtualDisplay(ui::Rotation orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004532 void prepareVirtualKeys();
Jason Gerecke489fda82012-09-07 17:19:40 -07004533 void prepareLocationCalibration();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004534 int32_t toRawX(float displayX);
4535 int32_t toRawY(float displayY);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07004536 int32_t toRotatedRawX(float displayX);
4537 int32_t toRotatedRawY(float displayY);
Jason Gerecke489fda82012-09-07 17:19:40 -07004538 float toCookedX(float rawX, float rawY);
4539 float toCookedY(float rawX, float rawY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004540 float toDisplayX(int32_t rawX);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004541 float toDisplayX(int32_t rawX, int32_t displayWidth);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004542 float toDisplayY(int32_t rawY);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004543 float toDisplayY(int32_t rawY, int32_t displayHeight);
4544
Michael Wrightd02c5b62014-02-10 15:10:22 -08004545};
4546
4547const int32_t TouchInputMapperTest::RAW_X_MIN = 25;
4548const int32_t TouchInputMapperTest::RAW_X_MAX = 1019;
4549const int32_t TouchInputMapperTest::RAW_Y_MIN = 30;
4550const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009;
4551const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0;
4552const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31;
4553const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0;
4554const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15;
Michael Wrightaa449c92017-12-13 21:21:43 +00004555const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = 0;
4556const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = 255;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004557const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7;
4558const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
4559const int32_t TouchInputMapperTest::RAW_DISTANCE_MIN = 0;
4560const int32_t TouchInputMapperTest::RAW_DISTANCE_MAX = 7;
4561const int32_t TouchInputMapperTest::RAW_TILT_MIN = 0;
4562const int32_t TouchInputMapperTest::RAW_TILT_MAX = 150;
4563const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
4564const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
4565const int32_t TouchInputMapperTest::RAW_SLOT_MIN = 0;
4566const int32_t TouchInputMapperTest::RAW_SLOT_MAX = 9;
4567const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH;
4568const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT;
Santos Cordonfa5cf462017-04-05 10:37:00 -07004569const float TouchInputMapperTest::X_PRECISION_VIRTUAL =
4570 float(RAW_X_MAX - RAW_X_MIN + 1) / VIRTUAL_DISPLAY_WIDTH;
4571const float TouchInputMapperTest::Y_PRECISION_VIRTUAL =
4572 float(RAW_Y_MAX - RAW_Y_MIN + 1) / VIRTUAL_DISPLAY_HEIGHT;
Jason Gerecke489fda82012-09-07 17:19:40 -07004573const TouchAffineTransformation TouchInputMapperTest::AFFINE_TRANSFORM =
4574 TouchAffineTransformation(1, -2, 3, -4, 5, -6);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004575
4576const float TouchInputMapperTest::GEOMETRIC_SCALE =
4577 avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1),
4578 float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1));
4579
4580const VirtualKeyDefinition TouchInputMapperTest::VIRTUAL_KEYS[2] = {
4581 { KEY_HOME, 60, DISPLAY_HEIGHT + 15, 20, 20 },
4582 { KEY_MENU, DISPLAY_HEIGHT - 60, DISPLAY_WIDTH + 15, 20, 20 },
4583};
4584
Michael Wrighta9cf4192022-12-01 23:46:39 +00004585void TouchInputMapperTest::prepareDisplay(ui::Rotation orientation, std::optional<uint8_t> port) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004586 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
4587 port, ViewportType::INTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004588}
4589
4590void TouchInputMapperTest::prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port) {
4591 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +00004592 ui::ROTATION_0, SECONDARY_UNIQUE_ID, port, type);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004593}
4594
Michael Wrighta9cf4192022-12-01 23:46:39 +00004595void TouchInputMapperTest::prepareVirtualDisplay(ui::Rotation orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004596 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH, VIRTUAL_DISPLAY_HEIGHT,
4597 orientation, VIRTUAL_DISPLAY_UNIQUE_ID, NO_PORT,
4598 ViewportType::VIRTUAL);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004599}
4600
Michael Wrightd02c5b62014-02-10 15:10:22 -08004601void TouchInputMapperTest::prepareVirtualKeys() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004602 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[0]);
4603 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[1]);
4604 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4605 mFakeEventHub->addKey(EVENTHUB_ID, KEY_MENU, 0, AKEYCODE_MENU, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004606}
4607
Jason Gerecke489fda82012-09-07 17:19:40 -07004608void TouchInputMapperTest::prepareLocationCalibration() {
4609 mFakePolicy->setTouchAffineTransformation(AFFINE_TRANSFORM);
4610}
4611
Michael Wrightd02c5b62014-02-10 15:10:22 -08004612int32_t TouchInputMapperTest::toRawX(float displayX) {
4613 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN);
4614}
4615
4616int32_t TouchInputMapperTest::toRawY(float displayY) {
4617 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN);
4618}
4619
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07004620int32_t TouchInputMapperTest::toRotatedRawX(float displayX) {
4621 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_HEIGHT + RAW_X_MIN);
4622}
4623
4624int32_t TouchInputMapperTest::toRotatedRawY(float displayY) {
4625 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_WIDTH + RAW_Y_MIN);
4626}
4627
Jason Gerecke489fda82012-09-07 17:19:40 -07004628float TouchInputMapperTest::toCookedX(float rawX, float rawY) {
4629 AFFINE_TRANSFORM.applyTo(rawX, rawY);
4630 return rawX;
4631}
4632
4633float TouchInputMapperTest::toCookedY(float rawX, float rawY) {
4634 AFFINE_TRANSFORM.applyTo(rawX, rawY);
4635 return rawY;
4636}
4637
Michael Wrightd02c5b62014-02-10 15:10:22 -08004638float TouchInputMapperTest::toDisplayX(int32_t rawX) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004639 return toDisplayX(rawX, DISPLAY_WIDTH);
4640}
4641
4642float TouchInputMapperTest::toDisplayX(int32_t rawX, int32_t displayWidth) {
4643 return float(rawX - RAW_X_MIN) * displayWidth / (RAW_X_MAX - RAW_X_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004644}
4645
4646float TouchInputMapperTest::toDisplayY(int32_t rawY) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004647 return toDisplayY(rawY, DISPLAY_HEIGHT);
4648}
4649
4650float TouchInputMapperTest::toDisplayY(int32_t rawY, int32_t displayHeight) {
4651 return float(rawY - RAW_Y_MIN) * displayHeight / (RAW_Y_MAX - RAW_Y_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004652}
4653
4654
4655// --- SingleTouchInputMapperTest ---
4656
4657class SingleTouchInputMapperTest : public TouchInputMapperTest {
4658protected:
4659 void prepareButtons();
4660 void prepareAxes(int axes);
4661
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004662 void processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
4663 void processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
4664 void processUp(SingleTouchInputMapper& mappery);
4665 void processPressure(SingleTouchInputMapper& mapper, int32_t pressure);
4666 void processToolMajor(SingleTouchInputMapper& mapper, int32_t toolMajor);
4667 void processDistance(SingleTouchInputMapper& mapper, int32_t distance);
4668 void processTilt(SingleTouchInputMapper& mapper, int32_t tiltX, int32_t tiltY);
4669 void processKey(SingleTouchInputMapper& mapper, int32_t code, int32_t value);
4670 void processSync(SingleTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004671};
4672
4673void SingleTouchInputMapperTest::prepareButtons() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004674 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004675}
4676
4677void SingleTouchInputMapperTest::prepareAxes(int axes) {
4678 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004679 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
4680 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004681 }
4682 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004683 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MIN,
4684 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004685 }
4686 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004687 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TOOL_WIDTH, RAW_TOOL_MIN, RAW_TOOL_MAX, 0,
4688 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004689 }
4690 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004691 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_DISTANCE, RAW_DISTANCE_MIN,
4692 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004693 }
4694 if (axes & TILT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004695 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_X, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
4696 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_Y, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004697 }
4698}
4699
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004700void SingleTouchInputMapperTest::processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004701 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
4702 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
4703 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004704}
4705
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004706void SingleTouchInputMapperTest::processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004707 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
4708 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004709}
4710
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004711void SingleTouchInputMapperTest::processUp(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004712 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004713}
4714
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004715void SingleTouchInputMapperTest::processPressure(SingleTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004716 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004717}
4718
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004719void SingleTouchInputMapperTest::processToolMajor(SingleTouchInputMapper& mapper,
4720 int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004721 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TOOL_WIDTH, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004722}
4723
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004724void SingleTouchInputMapperTest::processDistance(SingleTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004725 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004726}
4727
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004728void SingleTouchInputMapperTest::processTilt(SingleTouchInputMapper& mapper, int32_t tiltX,
4729 int32_t tiltY) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004730 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_X, tiltX);
4731 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_Y, tiltY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004732}
4733
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004734void SingleTouchInputMapperTest::processKey(SingleTouchInputMapper& mapper, int32_t code,
4735 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004736 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004737}
4738
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004739void SingleTouchInputMapperTest::processSync(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004740 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004741}
4742
Michael Wrightd02c5b62014-02-10 15:10:22 -08004743TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004744 prepareButtons();
4745 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00004746 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004747
Josep del Río2d8c79a2023-01-23 19:33:50 +00004748 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004749}
4750
Michael Wrightd02c5b62014-02-10 15:10:22 -08004751TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004752 prepareButtons();
4753 prepareAxes(POSITION);
4754 addConfigurationProperty("touch.deviceType", "touchScreen");
Arpit Singha8c236b2023-04-25 13:56:05 +00004755 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004756
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004757 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004758}
4759
4760TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004761 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00004762 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004763 prepareButtons();
4764 prepareAxes(POSITION);
4765 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00004766 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004767
4768 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004769 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004770
4771 // Virtual key is down.
4772 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4773 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4774 processDown(mapper, x, y);
4775 processSync(mapper);
4776 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4777
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004778 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004779
4780 // Virtual key is up.
4781 processUp(mapper);
4782 processSync(mapper);
4783 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4784
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004785 ASSERT_EQ(AKEY_STATE_UP, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004786}
4787
4788TEST_F(SingleTouchInputMapperTest, GetScanCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004789 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00004790 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004791 prepareButtons();
4792 prepareAxes(POSITION);
4793 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00004794 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004795
4796 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004797 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004798
4799 // Virtual key is down.
4800 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4801 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4802 processDown(mapper, x, y);
4803 processSync(mapper);
4804 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4805
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004806 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004807
4808 // Virtual key is up.
4809 processUp(mapper);
4810 processSync(mapper);
4811 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4812
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004813 ASSERT_EQ(AKEY_STATE_UP, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004814}
4815
4816TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004817 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00004818 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004819 prepareButtons();
4820 prepareAxes(POSITION);
4821 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00004822 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004823
Michael Wrightd02c5b62014-02-10 15:10:22 -08004824 uint8_t flags[2] = { 0, 0 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07004825 ASSERT_TRUE(
4826 mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, {AKEYCODE_HOME, AKEYCODE_A}, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004827 ASSERT_TRUE(flags[0]);
4828 ASSERT_FALSE(flags[1]);
4829}
4830
4831TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004832 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00004833 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004834 prepareButtons();
4835 prepareAxes(POSITION);
4836 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00004837 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004838
arthurhungdcef2dc2020-08-11 14:47:50 +08004839 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004840
4841 NotifyKeyArgs args;
4842
4843 // Press virtual key.
4844 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4845 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4846 processDown(mapper, x, y);
4847 processSync(mapper);
4848
4849 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4850 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4851 ASSERT_EQ(DEVICE_ID, args.deviceId);
4852 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
4853 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
4854 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
4855 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
4856 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4857 ASSERT_EQ(KEY_HOME, args.scanCode);
4858 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4859 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4860
4861 // Release virtual key.
4862 processUp(mapper);
4863 processSync(mapper);
4864
4865 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4866 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4867 ASSERT_EQ(DEVICE_ID, args.deviceId);
4868 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
4869 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
4870 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
4871 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
4872 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4873 ASSERT_EQ(KEY_HOME, args.scanCode);
4874 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4875 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4876
4877 // Should not have sent any motions.
4878 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4879}
4880
4881TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004882 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00004883 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004884 prepareButtons();
4885 prepareAxes(POSITION);
4886 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00004887 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004888
arthurhungdcef2dc2020-08-11 14:47:50 +08004889 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004890
4891 NotifyKeyArgs keyArgs;
4892
4893 // Press virtual key.
4894 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4895 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4896 processDown(mapper, x, y);
4897 processSync(mapper);
4898
4899 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4900 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
4901 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
4902 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
4903 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
4904 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4905 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags);
4906 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
4907 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
4908 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
4909 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
4910
4911 // Move out of bounds. This should generate a cancel and a pointer down since we moved
4912 // into the display area.
4913 y -= 100;
4914 processMove(mapper, x, y);
4915 processSync(mapper);
4916
4917 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4918 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
4919 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
4920 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
4921 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
4922 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4923 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
4924 | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags);
4925 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
4926 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
4927 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
4928 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
4929
4930 NotifyMotionArgs motionArgs;
4931 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4932 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4933 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4934 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4935 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4936 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4937 ASSERT_EQ(0, motionArgs.flags);
4938 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4939 ASSERT_EQ(0, motionArgs.buttonState);
4940 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004941 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004942 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004943 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004944 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4945 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4946 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4947 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4948 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4949
4950 // Keep moving out of bounds. Should generate a pointer move.
4951 y -= 50;
4952 processMove(mapper, x, y);
4953 processSync(mapper);
4954
4955 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4956 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4957 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4958 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4959 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4960 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4961 ASSERT_EQ(0, motionArgs.flags);
4962 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4963 ASSERT_EQ(0, motionArgs.buttonState);
4964 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004965 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004966 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004967 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004968 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4969 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4970 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4971 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4972 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4973
4974 // Release out of bounds. Should generate a pointer up.
4975 processUp(mapper);
4976 processSync(mapper);
4977
4978 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4979 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4980 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4981 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4982 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4983 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4984 ASSERT_EQ(0, motionArgs.flags);
4985 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4986 ASSERT_EQ(0, motionArgs.buttonState);
4987 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004988 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004989 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004990 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004991 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4992 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4993 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4994 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4995 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4996
4997 // Should not have sent any more keys or motions.
4998 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4999 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5000}
5001
5002TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005003 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005004 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005005 prepareButtons();
5006 prepareAxes(POSITION);
5007 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00005008 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005009
arthurhungdcef2dc2020-08-11 14:47:50 +08005010 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005011
5012 NotifyMotionArgs motionArgs;
5013
5014 // Initially go down out of bounds.
5015 int32_t x = -10;
5016 int32_t y = -10;
5017 processDown(mapper, x, y);
5018 processSync(mapper);
5019
5020 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5021
5022 // Move into the display area. Should generate a pointer down.
5023 x = 50;
5024 y = 75;
5025 processMove(mapper, x, y);
5026 processSync(mapper);
5027
5028 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5029 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5030 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5031 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5032 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5033 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5034 ASSERT_EQ(0, motionArgs.flags);
5035 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5036 ASSERT_EQ(0, motionArgs.buttonState);
5037 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005038 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005039 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005040 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005041 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5042 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5043 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5044 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5045 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5046
5047 // Release. Should generate a pointer up.
5048 processUp(mapper);
5049 processSync(mapper);
5050
5051 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5052 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5053 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5054 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5055 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5056 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5057 ASSERT_EQ(0, motionArgs.flags);
5058 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5059 ASSERT_EQ(0, motionArgs.buttonState);
5060 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005061 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005062 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005063 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005064 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5065 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5066 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5067 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5068 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5069
5070 // Should not have sent any more keys or motions.
5071 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5072 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5073}
5074
Santos Cordonfa5cf462017-04-05 10:37:00 -07005075TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture_VirtualDisplay) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005076 addConfigurationProperty("touch.deviceType", "touchScreen");
5077 addConfigurationProperty("touch.displayId", VIRTUAL_DISPLAY_UNIQUE_ID);
5078
Michael Wrighta9cf4192022-12-01 23:46:39 +00005079 prepareVirtualDisplay(ui::ROTATION_0);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005080 prepareButtons();
5081 prepareAxes(POSITION);
5082 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00005083 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Santos Cordonfa5cf462017-04-05 10:37:00 -07005084
arthurhungdcef2dc2020-08-11 14:47:50 +08005085 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005086
5087 NotifyMotionArgs motionArgs;
5088
5089 // Down.
5090 int32_t x = 100;
5091 int32_t y = 125;
5092 processDown(mapper, x, y);
5093 processSync(mapper);
5094
5095 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5096 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5097 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5098 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5099 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5100 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5101 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5102 ASSERT_EQ(0, motionArgs.flags);
5103 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5104 ASSERT_EQ(0, motionArgs.buttonState);
5105 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005106 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Santos Cordonfa5cf462017-04-05 10:37:00 -07005107 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005108 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005109 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5110 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5111 1, 0, 0, 0, 0, 0, 0, 0));
5112 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5113 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5114 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5115
5116 // Move.
5117 x += 50;
5118 y += 75;
5119 processMove(mapper, x, y);
5120 processSync(mapper);
5121
5122 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5123 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5124 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5125 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5126 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5127 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5128 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5129 ASSERT_EQ(0, motionArgs.flags);
5130 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5131 ASSERT_EQ(0, motionArgs.buttonState);
5132 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005133 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Santos Cordonfa5cf462017-04-05 10:37:00 -07005134 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005135 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005136 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5137 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5138 1, 0, 0, 0, 0, 0, 0, 0));
5139 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5140 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5141 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5142
5143 // Up.
5144 processUp(mapper);
5145 processSync(mapper);
5146
5147 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5148 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5149 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5150 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5151 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5152 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5153 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5154 ASSERT_EQ(0, motionArgs.flags);
5155 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5156 ASSERT_EQ(0, motionArgs.buttonState);
5157 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005158 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Santos Cordonfa5cf462017-04-05 10:37:00 -07005159 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005160 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005161 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5162 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5163 1, 0, 0, 0, 0, 0, 0, 0));
5164 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5165 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5166 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5167
5168 // Should not have sent any more keys or motions.
5169 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5170 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5171}
5172
Michael Wrightd02c5b62014-02-10 15:10:22 -08005173TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005174 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005175 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005176 prepareButtons();
5177 prepareAxes(POSITION);
5178 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00005179 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005180
arthurhungdcef2dc2020-08-11 14:47:50 +08005181 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005182
5183 NotifyMotionArgs motionArgs;
5184
5185 // Down.
5186 int32_t x = 100;
5187 int32_t y = 125;
5188 processDown(mapper, x, y);
5189 processSync(mapper);
5190
5191 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5192 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5193 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5194 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5195 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5196 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5197 ASSERT_EQ(0, motionArgs.flags);
5198 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5199 ASSERT_EQ(0, motionArgs.buttonState);
5200 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005201 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005202 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005203 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005204 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5205 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5206 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5207 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5208 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5209
5210 // Move.
5211 x += 50;
5212 y += 75;
5213 processMove(mapper, x, y);
5214 processSync(mapper);
5215
5216 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5217 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5218 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5219 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5220 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5221 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5222 ASSERT_EQ(0, motionArgs.flags);
5223 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5224 ASSERT_EQ(0, motionArgs.buttonState);
5225 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005226 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005227 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005228 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005229 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5230 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5231 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5232 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5233 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5234
5235 // Up.
5236 processUp(mapper);
5237 processSync(mapper);
5238
5239 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5240 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5241 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5242 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5243 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5244 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5245 ASSERT_EQ(0, motionArgs.flags);
5246 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5247 ASSERT_EQ(0, motionArgs.buttonState);
5248 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005249 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005250 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005251 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005252 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5253 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5254 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5255 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5256 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5257
5258 // Should not have sent any more keys or motions.
5259 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5260 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5261}
5262
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005263TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_DoesNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005264 addConfigurationProperty("touch.deviceType", "touchScreen");
5265 prepareButtons();
5266 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005267 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
5268 // need to be rotated. Touchscreens are orientation-aware by default.
Arpit Singha8c236b2023-04-25 13:56:05 +00005269 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005270
5271 NotifyMotionArgs args;
5272
5273 // Rotation 90.
Michael Wrighta9cf4192022-12-01 23:46:39 +00005274 prepareDisplay(ui::ROTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005275 processDown(mapper, toRawX(50), toRawY(75));
5276 processSync(mapper);
5277
5278 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5279 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5280 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5281
5282 processUp(mapper);
5283 processSync(mapper);
5284 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5285}
5286
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005287TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_RotatesMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005288 addConfigurationProperty("touch.deviceType", "touchScreen");
5289 prepareButtons();
5290 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005291 // Since InputReader works in the un-rotated coordinate space, only devices that are not
5292 // orientation-aware are affected by display rotation.
5293 addConfigurationProperty("touch.orientationAware", "0");
Arpit Singha8c236b2023-04-25 13:56:05 +00005294 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005295
5296 NotifyMotionArgs args;
5297
5298 // Rotation 0.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005299 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005300 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005301 processDown(mapper, toRawX(50), toRawY(75));
5302 processSync(mapper);
5303
5304 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5305 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5306 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5307
5308 processUp(mapper);
5309 processSync(mapper);
5310 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5311
5312 // Rotation 90.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005313 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005314 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00005315 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005316 processSync(mapper);
5317
5318 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5319 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5320 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5321
5322 processUp(mapper);
5323 processSync(mapper);
5324 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5325
5326 // Rotation 180.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005327 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005328 prepareDisplay(ui::ROTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005329 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
5330 processSync(mapper);
5331
5332 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5333 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5334 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5335
5336 processUp(mapper);
5337 processSync(mapper);
5338 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5339
5340 // Rotation 270.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005341 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005342 prepareDisplay(ui::ROTATION_270);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00005343 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005344 processSync(mapper);
5345
5346 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5347 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5348 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5349
5350 processUp(mapper);
5351 processSync(mapper);
5352 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5353}
5354
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005355TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation0_RotatesMotions) {
5356 addConfigurationProperty("touch.deviceType", "touchScreen");
5357 prepareButtons();
5358 prepareAxes(POSITION);
5359 addConfigurationProperty("touch.orientationAware", "1");
5360 addConfigurationProperty("touch.orientation", "ORIENTATION_0");
5361 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005362 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +00005363 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005364 NotifyMotionArgs args;
5365
5366 // Orientation 0.
5367 processDown(mapper, toRawX(50), toRawY(75));
5368 processSync(mapper);
5369
5370 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5371 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5372 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5373
5374 processUp(mapper);
5375 processSync(mapper);
5376 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5377}
5378
5379TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation90_RotatesMotions) {
5380 addConfigurationProperty("touch.deviceType", "touchScreen");
5381 prepareButtons();
5382 prepareAxes(POSITION);
5383 addConfigurationProperty("touch.orientationAware", "1");
5384 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
5385 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005386 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +00005387 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005388 NotifyMotionArgs args;
5389
5390 // Orientation 90.
5391 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
5392 processSync(mapper);
5393
5394 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5395 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5396 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5397
5398 processUp(mapper);
5399 processSync(mapper);
5400 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5401}
5402
5403TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation180_RotatesMotions) {
5404 addConfigurationProperty("touch.deviceType", "touchScreen");
5405 prepareButtons();
5406 prepareAxes(POSITION);
5407 addConfigurationProperty("touch.orientationAware", "1");
5408 addConfigurationProperty("touch.orientation", "ORIENTATION_180");
5409 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005410 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +00005411 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005412 NotifyMotionArgs args;
5413
5414 // Orientation 180.
5415 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
5416 processSync(mapper);
5417
5418 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5419 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5420 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5421
5422 processUp(mapper);
5423 processSync(mapper);
5424 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5425}
5426
5427TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation270_RotatesMotions) {
5428 addConfigurationProperty("touch.deviceType", "touchScreen");
5429 prepareButtons();
5430 prepareAxes(POSITION);
5431 addConfigurationProperty("touch.orientationAware", "1");
5432 addConfigurationProperty("touch.orientation", "ORIENTATION_270");
5433 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005434 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +00005435 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005436 NotifyMotionArgs args;
5437
5438 // Orientation 270.
5439 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
5440 processSync(mapper);
5441
5442 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5443 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5444 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5445
5446 processUp(mapper);
5447 processSync(mapper);
5448 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5449}
5450
5451TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationSpecified_RotatesMotionWithDisplay) {
5452 addConfigurationProperty("touch.deviceType", "touchScreen");
5453 prepareButtons();
5454 prepareAxes(POSITION);
5455 // Since InputReader works in the un-rotated coordinate space, only devices that are not
5456 // orientation-aware are affected by display rotation.
5457 addConfigurationProperty("touch.orientationAware", "0");
5458 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
Arpit Singha8c236b2023-04-25 13:56:05 +00005459 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005460
5461 NotifyMotionArgs args;
5462
5463 // Orientation 90, Rotation 0.
5464 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005465 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005466 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
5467 processSync(mapper);
5468
5469 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5470 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5471 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5472
5473 processUp(mapper);
5474 processSync(mapper);
5475 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5476
5477 // Orientation 90, Rotation 90.
5478 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005479 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00005480 processDown(mapper, toRawX(50), toRawY(75));
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005481 processSync(mapper);
5482
5483 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5484 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5485 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5486
5487 processUp(mapper);
5488 processSync(mapper);
5489 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5490
5491 // Orientation 90, Rotation 180.
5492 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005493 prepareDisplay(ui::ROTATION_180);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005494 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
5495 processSync(mapper);
5496
5497 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5498 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5499 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5500
5501 processUp(mapper);
5502 processSync(mapper);
5503 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5504
5505 // Orientation 90, Rotation 270.
5506 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005507 prepareDisplay(ui::ROTATION_270);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00005508 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 -07005509 processSync(mapper);
5510
5511 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5512 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5513 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5514
5515 processUp(mapper);
5516 processSync(mapper);
5517 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5518}
5519
Prabir Pradhan675f25a2022-11-10 22:04:07 +00005520TEST_F(SingleTouchInputMapperTest, Process_IgnoresTouchesOutsidePhysicalFrame) {
5521 addConfigurationProperty("touch.deviceType", "touchScreen");
5522 prepareButtons();
5523 prepareAxes(POSITION);
5524 addConfigurationProperty("touch.orientationAware", "1");
5525 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +00005526 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan675f25a2022-11-10 22:04:07 +00005527
5528 // Set a physical frame in the display viewport.
5529 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
5530 viewport->physicalLeft = 20;
5531 viewport->physicalTop = 600;
5532 viewport->physicalRight = 30;
5533 viewport->physicalBottom = 610;
5534 mFakePolicy->updateViewport(*viewport);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00005535 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhan675f25a2022-11-10 22:04:07 +00005536
5537 // Start the touch.
5538 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
5539 processSync(mapper);
5540
5541 // Expect all input starting outside the physical frame to be ignored.
5542 const std::array<Point, 6> outsidePoints = {
5543 {{0, 0}, {19, 605}, {31, 605}, {25, 599}, {25, 611}, {DISPLAY_WIDTH, DISPLAY_HEIGHT}}};
5544 for (const auto& p : outsidePoints) {
5545 processMove(mapper, toRawX(p.x), toRawY(p.y));
5546 processSync(mapper);
5547 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5548 }
5549
5550 // Move the touch into the physical frame.
5551 processMove(mapper, toRawX(25), toRawY(605));
5552 processSync(mapper);
5553 NotifyMotionArgs args;
5554 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5555 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
5556 EXPECT_NEAR(25, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5557 EXPECT_NEAR(605, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5558
5559 // Once the touch down is reported, continue reporting input, even if it is outside the frame.
5560 for (const auto& p : outsidePoints) {
5561 processMove(mapper, toRawX(p.x), toRawY(p.y));
5562 processSync(mapper);
5563 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5564 EXPECT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
5565 EXPECT_NEAR(p.x, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5566 EXPECT_NEAR(p.y, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5567 }
5568
5569 processUp(mapper);
5570 processSync(mapper);
5571 EXPECT_NO_FATAL_FAILURE(
5572 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
5573}
5574
Harry Cutts1db43992023-06-19 17:05:07 +00005575TEST_F(SingleTouchInputMapperTest, Process_DoesntCheckPhysicalFrameForTouchpads) {
5576 std::shared_ptr<FakePointerController> fakePointerController =
5577 std::make_shared<FakePointerController>();
5578 mFakePolicy->setPointerController(fakePointerController);
5579
5580 addConfigurationProperty("touch.deviceType", "pointer");
5581 prepareAxes(POSITION);
5582 prepareDisplay(ui::ROTATION_0);
5583 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
5584
5585 // Set a physical frame in the display viewport.
5586 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
5587 viewport->physicalLeft = 20;
5588 viewport->physicalTop = 600;
5589 viewport->physicalRight = 30;
5590 viewport->physicalBottom = 610;
5591 mFakePolicy->updateViewport(*viewport);
5592 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
5593
5594 // Start the touch.
5595 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
5596 processSync(mapper);
5597
5598 // Expect all input starting outside the physical frame to result in NotifyMotionArgs being
5599 // produced.
5600 const std::array<Point, 6> outsidePoints = {
5601 {{0, 0}, {19, 605}, {31, 605}, {25, 599}, {25, 611}, {DISPLAY_WIDTH, DISPLAY_HEIGHT}}};
5602 for (const auto& p : outsidePoints) {
5603 processMove(mapper, toRawX(p.x), toRawY(p.y));
5604 processSync(mapper);
5605 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5606 }
5607}
5608
Michael Wrightd02c5b62014-02-10 15:10:22 -08005609TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005610 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005611 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005612 prepareButtons();
5613 prepareAxes(POSITION | PRESSURE | TOOL | DISTANCE | TILT);
Arpit Singha8c236b2023-04-25 13:56:05 +00005614 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005615
5616 // These calculations are based on the input device calibration documentation.
5617 int32_t rawX = 100;
5618 int32_t rawY = 200;
5619 int32_t rawPressure = 10;
5620 int32_t rawToolMajor = 12;
5621 int32_t rawDistance = 2;
5622 int32_t rawTiltX = 30;
5623 int32_t rawTiltY = 110;
5624
5625 float x = toDisplayX(rawX);
5626 float y = toDisplayY(rawY);
5627 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
5628 float size = float(rawToolMajor) / RAW_TOOL_MAX;
5629 float tool = float(rawToolMajor) * GEOMETRIC_SCALE;
5630 float distance = float(rawDistance);
5631
5632 float tiltCenter = (RAW_TILT_MAX + RAW_TILT_MIN) * 0.5f;
5633 float tiltScale = M_PI / 180;
5634 float tiltXAngle = (rawTiltX - tiltCenter) * tiltScale;
5635 float tiltYAngle = (rawTiltY - tiltCenter) * tiltScale;
5636 float orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
5637 float tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
5638
5639 processDown(mapper, rawX, rawY);
5640 processPressure(mapper, rawPressure);
5641 processToolMajor(mapper, rawToolMajor);
5642 processDistance(mapper, rawDistance);
5643 processTilt(mapper, rawTiltX, rawTiltY);
5644 processSync(mapper);
5645
5646 NotifyMotionArgs args;
5647 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5648 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5649 x, y, pressure, size, tool, tool, tool, tool, orientation, distance));
5650 ASSERT_EQ(tilt, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TILT));
5651}
5652
Jason Gerecke489fda82012-09-07 17:19:40 -07005653TEST_F(SingleTouchInputMapperTest, Process_XYAxes_AffineCalibration) {
Jason Gerecke489fda82012-09-07 17:19:40 -07005654 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005655 prepareDisplay(ui::ROTATION_0);
Jason Gerecke489fda82012-09-07 17:19:40 -07005656 prepareLocationCalibration();
5657 prepareButtons();
5658 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00005659 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Jason Gerecke489fda82012-09-07 17:19:40 -07005660
5661 int32_t rawX = 100;
5662 int32_t rawY = 200;
5663
5664 float x = toDisplayX(toCookedX(rawX, rawY));
5665 float y = toDisplayY(toCookedY(rawX, rawY));
5666
5667 processDown(mapper, rawX, rawY);
5668 processSync(mapper);
5669
5670 NotifyMotionArgs args;
5671 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5672 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5673 x, y, 1, 0, 0, 0, 0, 0, 0, 0));
5674}
5675
Michael Wrightd02c5b62014-02-10 15:10:22 -08005676TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005677 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005678 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005679 prepareButtons();
5680 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00005681 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005682
5683 NotifyMotionArgs motionArgs;
5684 NotifyKeyArgs keyArgs;
5685
5686 processDown(mapper, 100, 200);
5687 processSync(mapper);
5688 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5689 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5690 ASSERT_EQ(0, motionArgs.buttonState);
5691
5692 // press BTN_LEFT, release BTN_LEFT
5693 processKey(mapper, BTN_LEFT, 1);
5694 processSync(mapper);
5695 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5696 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5697 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
5698
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005699 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5700 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5701 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
5702
Michael Wrightd02c5b62014-02-10 15:10:22 -08005703 processKey(mapper, BTN_LEFT, 0);
5704 processSync(mapper);
5705 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005706 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005707 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005708
5709 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005710 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005711 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005712
5713 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
5714 processKey(mapper, BTN_RIGHT, 1);
5715 processKey(mapper, BTN_MIDDLE, 1);
5716 processSync(mapper);
5717 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5718 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5719 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5720 motionArgs.buttonState);
5721
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005722 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5723 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5724 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
5725
5726 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5727 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5728 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5729 motionArgs.buttonState);
5730
Michael Wrightd02c5b62014-02-10 15:10:22 -08005731 processKey(mapper, BTN_RIGHT, 0);
5732 processSync(mapper);
5733 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005734 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005735 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005736
5737 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005738 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005739 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005740
5741 processKey(mapper, BTN_MIDDLE, 0);
5742 processSync(mapper);
5743 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005744 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005745 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005746
5747 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005748 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005749 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005750
5751 // press BTN_BACK, release BTN_BACK
5752 processKey(mapper, BTN_BACK, 1);
5753 processSync(mapper);
5754 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5755 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5756 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005757
Michael Wrightd02c5b62014-02-10 15:10:22 -08005758 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005759 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005760 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5761
5762 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5763 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5764 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005765
5766 processKey(mapper, BTN_BACK, 0);
5767 processSync(mapper);
5768 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005769 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005770 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005771
5772 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005773 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005774 ASSERT_EQ(0, motionArgs.buttonState);
5775
Michael Wrightd02c5b62014-02-10 15:10:22 -08005776 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5777 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5778 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
5779
5780 // press BTN_SIDE, release BTN_SIDE
5781 processKey(mapper, BTN_SIDE, 1);
5782 processSync(mapper);
5783 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5784 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5785 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005786
Michael Wrightd02c5b62014-02-10 15:10:22 -08005787 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005788 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005789 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5790
5791 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5792 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5793 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005794
5795 processKey(mapper, BTN_SIDE, 0);
5796 processSync(mapper);
5797 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005798 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005799 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005800
5801 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005802 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005803 ASSERT_EQ(0, motionArgs.buttonState);
5804
Michael Wrightd02c5b62014-02-10 15:10:22 -08005805 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5806 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5807 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
5808
5809 // press BTN_FORWARD, release BTN_FORWARD
5810 processKey(mapper, BTN_FORWARD, 1);
5811 processSync(mapper);
5812 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5813 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5814 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005815
Michael Wrightd02c5b62014-02-10 15:10:22 -08005816 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005817 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005818 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5819
5820 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5821 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5822 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005823
5824 processKey(mapper, BTN_FORWARD, 0);
5825 processSync(mapper);
5826 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005827 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005828 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005829
5830 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005831 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005832 ASSERT_EQ(0, motionArgs.buttonState);
5833
Michael Wrightd02c5b62014-02-10 15:10:22 -08005834 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5835 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5836 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
5837
5838 // press BTN_EXTRA, release BTN_EXTRA
5839 processKey(mapper, BTN_EXTRA, 1);
5840 processSync(mapper);
5841 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5842 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5843 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005844
Michael Wrightd02c5b62014-02-10 15:10:22 -08005845 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005846 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005847 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5848
5849 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5850 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5851 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005852
5853 processKey(mapper, BTN_EXTRA, 0);
5854 processSync(mapper);
5855 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005856 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005857 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005858
5859 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005860 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005861 ASSERT_EQ(0, motionArgs.buttonState);
5862
Michael Wrightd02c5b62014-02-10 15:10:22 -08005863 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5864 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5865 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
5866
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005867 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5868
Michael Wrightd02c5b62014-02-10 15:10:22 -08005869 // press BTN_STYLUS, release BTN_STYLUS
5870 processKey(mapper, BTN_STYLUS, 1);
5871 processSync(mapper);
5872 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5873 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005874 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
5875
5876 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5877 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5878 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005879
5880 processKey(mapper, BTN_STYLUS, 0);
5881 processSync(mapper);
5882 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005883 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005884 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005885
5886 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005887 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005888 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005889
5890 // press BTN_STYLUS2, release BTN_STYLUS2
5891 processKey(mapper, BTN_STYLUS2, 1);
5892 processSync(mapper);
5893 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5894 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005895 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
5896
5897 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5898 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5899 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005900
5901 processKey(mapper, BTN_STYLUS2, 0);
5902 processSync(mapper);
5903 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005904 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005905 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005906
5907 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005908 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005909 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005910
5911 // release touch
5912 processUp(mapper);
5913 processSync(mapper);
5914 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5915 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5916 ASSERT_EQ(0, motionArgs.buttonState);
5917}
5918
5919TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005920 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005921 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005922 prepareButtons();
5923 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00005924 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005925
5926 NotifyMotionArgs motionArgs;
5927
5928 // default tool type is finger
5929 processDown(mapper, 100, 200);
5930 processSync(mapper);
5931 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5932 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005933 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005934
5935 // eraser
5936 processKey(mapper, BTN_TOOL_RUBBER, 1);
5937 processSync(mapper);
5938 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5939 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005940 ASSERT_EQ(ToolType::ERASER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005941
5942 // stylus
5943 processKey(mapper, BTN_TOOL_RUBBER, 0);
5944 processKey(mapper, BTN_TOOL_PEN, 1);
5945 processSync(mapper);
5946 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5947 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005948 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005949
5950 // brush
5951 processKey(mapper, BTN_TOOL_PEN, 0);
5952 processKey(mapper, BTN_TOOL_BRUSH, 1);
5953 processSync(mapper);
5954 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5955 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005956 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005957
5958 // pencil
5959 processKey(mapper, BTN_TOOL_BRUSH, 0);
5960 processKey(mapper, BTN_TOOL_PENCIL, 1);
5961 processSync(mapper);
5962 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5963 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005964 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005965
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08005966 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08005967 processKey(mapper, BTN_TOOL_PENCIL, 0);
5968 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
5969 processSync(mapper);
5970 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5971 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005972 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005973
5974 // mouse
5975 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
5976 processKey(mapper, BTN_TOOL_MOUSE, 1);
5977 processSync(mapper);
5978 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5979 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005980 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005981
5982 // lens
5983 processKey(mapper, BTN_TOOL_MOUSE, 0);
5984 processKey(mapper, BTN_TOOL_LENS, 1);
5985 processSync(mapper);
5986 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5987 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005988 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005989
5990 // double-tap
5991 processKey(mapper, BTN_TOOL_LENS, 0);
5992 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
5993 processSync(mapper);
5994 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5995 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005996 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005997
5998 // triple-tap
5999 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
6000 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
6001 processSync(mapper);
6002 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6003 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006004 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006005
6006 // quad-tap
6007 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
6008 processKey(mapper, BTN_TOOL_QUADTAP, 1);
6009 processSync(mapper);
6010 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6011 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006012 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006013
6014 // finger
6015 processKey(mapper, BTN_TOOL_QUADTAP, 0);
6016 processKey(mapper, BTN_TOOL_FINGER, 1);
6017 processSync(mapper);
6018 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6019 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006020 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006021
6022 // stylus trumps finger
6023 processKey(mapper, BTN_TOOL_PEN, 1);
6024 processSync(mapper);
6025 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6026 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006027 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006028
6029 // eraser trumps stylus
6030 processKey(mapper, BTN_TOOL_RUBBER, 1);
6031 processSync(mapper);
6032 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6033 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006034 ASSERT_EQ(ToolType::ERASER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006035
6036 // mouse trumps eraser
6037 processKey(mapper, BTN_TOOL_MOUSE, 1);
6038 processSync(mapper);
6039 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6040 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006041 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006042
6043 // back to default tool type
6044 processKey(mapper, BTN_TOOL_MOUSE, 0);
6045 processKey(mapper, BTN_TOOL_RUBBER, 0);
6046 processKey(mapper, BTN_TOOL_PEN, 0);
6047 processKey(mapper, BTN_TOOL_FINGER, 0);
6048 processSync(mapper);
6049 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6050 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006051 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006052}
6053
6054TEST_F(SingleTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006055 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006056 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006057 prepareButtons();
6058 prepareAxes(POSITION);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006059 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_FINGER, 0, AKEYCODE_UNKNOWN, 0);
Arpit Singha8c236b2023-04-25 13:56:05 +00006060 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006061
6062 NotifyMotionArgs motionArgs;
6063
6064 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
6065 processKey(mapper, BTN_TOOL_FINGER, 1);
6066 processMove(mapper, 100, 200);
6067 processSync(mapper);
6068 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6069 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6070 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6071 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6072
6073 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6074 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6075 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6076 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6077
6078 // move a little
6079 processMove(mapper, 150, 250);
6080 processSync(mapper);
6081 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6082 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6083 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6084 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6085
6086 // down when BTN_TOUCH is pressed, pressure defaults to 1
6087 processKey(mapper, BTN_TOUCH, 1);
6088 processSync(mapper);
6089 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6090 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6091 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6092 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6093
6094 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6095 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6096 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6097 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6098
6099 // up when BTN_TOUCH is released, hover restored
6100 processKey(mapper, BTN_TOUCH, 0);
6101 processSync(mapper);
6102 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6103 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6104 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6105 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6106
6107 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6108 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6109 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6110 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6111
6112 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6113 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6114 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6115 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6116
6117 // exit hover when pointer goes away
6118 processKey(mapper, BTN_TOOL_FINGER, 0);
6119 processSync(mapper);
6120 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6121 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6122 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6123 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6124}
6125
6126TEST_F(SingleTouchInputMapperTest, Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006127 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006128 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006129 prepareButtons();
6130 prepareAxes(POSITION | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +00006131 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006132
6133 NotifyMotionArgs motionArgs;
6134
6135 // initially hovering because pressure is 0
6136 processDown(mapper, 100, 200);
6137 processPressure(mapper, 0);
6138 processSync(mapper);
6139 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6140 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6141 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6142 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6143
6144 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6145 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6146 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6147 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6148
6149 // move a little
6150 processMove(mapper, 150, 250);
6151 processSync(mapper);
6152 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6153 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6154 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6155 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6156
6157 // down when pressure is non-zero
6158 processPressure(mapper, RAW_PRESSURE_MAX);
6159 processSync(mapper);
6160 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6161 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6162 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6163 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6164
6165 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6166 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6167 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6168 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6169
6170 // up when pressure becomes 0, hover restored
6171 processPressure(mapper, 0);
6172 processSync(mapper);
6173 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6174 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6175 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6176 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6177
6178 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6179 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6180 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6181 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6182
6183 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6184 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6185 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6186 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6187
6188 // exit hover when pointer goes away
6189 processUp(mapper);
6190 processSync(mapper);
6191 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6192 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6193 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6194 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6195}
6196
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00006197TEST_F(SingleTouchInputMapperTest, Reset_CancelsOngoingGesture) {
6198 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006199 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00006200 prepareButtons();
6201 prepareAxes(POSITION | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +00006202 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00006203
6204 // Touch down.
6205 processDown(mapper, 100, 200);
6206 processPressure(mapper, 1);
6207 processSync(mapper);
6208 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6209 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
6210
6211 // Reset the mapper. This should cancel the ongoing gesture.
6212 resetMapper(mapper, ARBITRARY_TIME);
6213 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6214 WithMotionAction(AMOTION_EVENT_ACTION_CANCEL)));
6215
6216 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6217}
6218
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006219TEST_F(SingleTouchInputMapperTest, Reset_RecreatesTouchState) {
6220 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006221 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006222 prepareButtons();
6223 prepareAxes(POSITION | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +00006224 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006225
6226 // Set the initial state for the touch pointer.
6227 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_X, 100);
6228 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_Y, 200);
6229 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MAX);
6230 mFakeEventHub->setScanCodeState(EVENTHUB_ID, BTN_TOUCH, 1);
6231
6232 // Reset the mapper. When the mapper is reset, we expect it to attempt to recreate the touch
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00006233 // state by reading the current axis values. Since there was no ongoing gesture, calling reset
6234 // does not generate any events.
6235 resetMapper(mapper, ARBITRARY_TIME);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006236
6237 // Send a sync to simulate an empty touch frame where nothing changes. The mapper should use
6238 // the recreated touch state to generate a down event.
6239 processSync(mapper);
6240 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6241 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithPressure(1.f))));
6242
6243 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6244}
6245
lilinnan687e58f2022-07-19 16:00:50 +08006246TEST_F(SingleTouchInputMapperTest,
6247 Process_WhenViewportDisplayIdChanged_TouchIsCanceledAndDeviceIsReset) {
6248 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006249 prepareDisplay(ui::ROTATION_0);
lilinnan687e58f2022-07-19 16:00:50 +08006250 prepareButtons();
6251 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00006252 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
lilinnan687e58f2022-07-19 16:00:50 +08006253 NotifyMotionArgs motionArgs;
6254
6255 // Down.
Prabir Pradhan3e5ec702022-07-29 16:26:24 +00006256 processDown(mapper, 100, 200);
lilinnan687e58f2022-07-19 16:00:50 +08006257 processSync(mapper);
6258
6259 // We should receive a down event
6260 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6261 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6262
6263 // Change display id
6264 clearViewports();
6265 prepareSecondaryDisplay(ViewportType::INTERNAL);
6266
6267 // We should receive a cancel event
6268 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6269 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
6270 // Then receive reset called
6271 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6272}
6273
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006274TEST_F(SingleTouchInputMapperTest,
6275 Process_WhenViewportActiveStatusChanged_TouchIsCanceledAndDeviceIsReset) {
6276 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006277 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006278 prepareButtons();
6279 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00006280 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006281 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6282 NotifyMotionArgs motionArgs;
6283
6284 // Start a new gesture.
6285 processDown(mapper, 100, 200);
6286 processSync(mapper);
6287 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6288 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6289
6290 // Make the viewport inactive. This will put the device in disabled mode.
6291 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6292 viewport->isActive = false;
6293 mFakePolicy->updateViewport(*viewport);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00006294 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006295
6296 // We should receive a cancel event for the ongoing gesture.
6297 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6298 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
6299 // Then we should be notified that the device was reset.
6300 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6301
6302 // No events are generated while the viewport is inactive.
6303 processMove(mapper, 101, 201);
6304 processSync(mapper);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006305 processUp(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006306 processSync(mapper);
6307 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6308
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006309 // Start a new gesture while the viewport is still inactive.
6310 processDown(mapper, 300, 400);
6311 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_X, 300);
6312 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_Y, 400);
6313 mFakeEventHub->setScanCodeState(EVENTHUB_ID, BTN_TOUCH, 1);
6314 processSync(mapper);
6315
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006316 // Make the viewport active again. The device should resume processing events.
6317 viewport->isActive = true;
6318 mFakePolicy->updateViewport(*viewport);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00006319 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006320
6321 // The device is reset because it changes back to direct mode, without generating any events.
6322 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6323 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6324
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006325 // In the next sync, the touch state that was recreated when the device was reset is reported.
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006326 processSync(mapper);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006327 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6328 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006329
6330 // No more events.
6331 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6332 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
6333}
6334
Prabir Pradhan211ba622022-10-31 21:09:21 +00006335TEST_F(SingleTouchInputMapperTest, ButtonIsReleasedOnTouchUp) {
6336 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006337 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan211ba622022-10-31 21:09:21 +00006338 prepareButtons();
6339 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00006340 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan211ba622022-10-31 21:09:21 +00006341 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6342
6343 // Press a stylus button.
6344 processKey(mapper, BTN_STYLUS, 1);
6345 processSync(mapper);
6346
6347 // Start a touch gesture and ensure the BUTTON_PRESS event is generated.
6348 processDown(mapper, 100, 200);
6349 processSync(mapper);
6350 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6351 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
6352 WithCoords(toDisplayX(100), toDisplayY(200)),
6353 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
6354 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6355 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
6356 WithCoords(toDisplayX(100), toDisplayY(200)),
6357 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
6358
6359 // Release the touch gesture. Ensure that the BUTTON_RELEASE event is generated even though
6360 // the button has not actually been released, since there will be no pointers through which the
6361 // button state can be reported. The event is generated at the location of the pointer before
6362 // it went up.
6363 processUp(mapper);
6364 processSync(mapper);
6365 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6366 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
6367 WithCoords(toDisplayX(100), toDisplayY(200)), WithButtonState(0))));
6368 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6369 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
6370 WithCoords(toDisplayX(100), toDisplayY(200)), WithButtonState(0))));
6371}
6372
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00006373TEST_F(SingleTouchInputMapperTest, StylusButtonMotionEventsDisabled) {
6374 addConfigurationProperty("touch.deviceType", "touchScreen");
6375 prepareDisplay(ui::ROTATION_0);
6376 prepareButtons();
6377 prepareAxes(POSITION);
6378
6379 mFakePolicy->setStylusButtonMotionEventsEnabled(false);
6380
Arpit Singha8c236b2023-04-25 13:56:05 +00006381 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00006382 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6383
6384 // Press a stylus button.
6385 processKey(mapper, BTN_STYLUS, 1);
6386 processSync(mapper);
6387
6388 // Start a touch gesture and ensure that the stylus button is not reported.
6389 processDown(mapper, 100, 200);
6390 processSync(mapper);
6391 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6392 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithButtonState(0))));
6393
6394 // Release and press the stylus button again.
6395 processKey(mapper, BTN_STYLUS, 0);
6396 processSync(mapper);
6397 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6398 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
6399 processKey(mapper, BTN_STYLUS, 1);
6400 processSync(mapper);
6401 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6402 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
6403
6404 // Release the touch gesture.
6405 processUp(mapper);
6406 processSync(mapper);
6407 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6408 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithButtonState(0))));
6409
6410 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6411}
6412
Ambrus Weisz7bc23bf2022-10-04 13:13:07 +00006413TEST_F(SingleTouchInputMapperTest, WhenDeviceTypeIsSetToTouchNavigation_setsCorrectType) {
6414 mFakePolicy->addDeviceTypeAssociation(DEVICE_LOCATION, "touchNavigation");
6415 prepareDisplay(ui::ROTATION_0);
6416 prepareButtons();
6417 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00006418 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Ambrus Weisz7bc23bf2022-10-04 13:13:07 +00006419 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6420
6421 ASSERT_EQ(AINPUT_SOURCE_TOUCH_NAVIGATION, mapper.getSources());
6422}
6423
Seunghwan Choi356026c2023-02-01 14:37:25 +09006424TEST_F(SingleTouchInputMapperTest, Process_WhenConfigEnabled_ShouldShowDirectStylusPointer) {
6425 std::shared_ptr<FakePointerController> fakePointerController =
6426 std::make_shared<FakePointerController>();
6427 addConfigurationProperty("touch.deviceType", "touchScreen");
6428 prepareDisplay(ui::ROTATION_0);
6429 prepareButtons();
6430 prepareAxes(POSITION);
6431 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
6432 mFakePolicy->setPointerController(fakePointerController);
6433 mFakePolicy->setStylusPointerIconEnabled(true);
Arpit Singha8c236b2023-04-25 13:56:05 +00006434 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Seunghwan Choi356026c2023-02-01 14:37:25 +09006435
6436 processKey(mapper, BTN_TOOL_PEN, 1);
6437 processMove(mapper, 100, 200);
6438 processSync(mapper);
6439 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6440 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006441 WithToolType(ToolType::STYLUS),
Seunghwan Choi356026c2023-02-01 14:37:25 +09006442 WithPointerCoords(0, toDisplayX(100), toDisplayY(200)))));
6443 ASSERT_TRUE(fakePointerController->isPointerShown());
6444 ASSERT_NO_FATAL_FAILURE(
6445 fakePointerController->assertPosition(toDisplayX(100), toDisplayY(200)));
6446}
6447
6448TEST_F(SingleTouchInputMapperTest, Process_WhenConfigDisabled_ShouldNotShowDirectStylusPointer) {
6449 std::shared_ptr<FakePointerController> fakePointerController =
6450 std::make_shared<FakePointerController>();
6451 addConfigurationProperty("touch.deviceType", "touchScreen");
6452 prepareDisplay(ui::ROTATION_0);
6453 prepareButtons();
6454 prepareAxes(POSITION);
6455 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
6456 mFakePolicy->setPointerController(fakePointerController);
6457 mFakePolicy->setStylusPointerIconEnabled(false);
Arpit Singha8c236b2023-04-25 13:56:05 +00006458 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Seunghwan Choi356026c2023-02-01 14:37:25 +09006459
6460 processKey(mapper, BTN_TOOL_PEN, 1);
6461 processMove(mapper, 100, 200);
6462 processSync(mapper);
6463 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6464 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006465 WithToolType(ToolType::STYLUS),
Seunghwan Choi356026c2023-02-01 14:37:25 +09006466 WithPointerCoords(0, toDisplayX(100), toDisplayY(200)))));
6467 ASSERT_FALSE(fakePointerController->isPointerShown());
6468}
6469
Ambrus Weisz7b6e16b2022-12-16 17:54:57 +00006470TEST_F(SingleTouchInputMapperTest, WhenDeviceTypeIsChangedToTouchNavigation_updatesDeviceType) {
6471 // Initialize the device without setting device source to touch navigation.
6472 addConfigurationProperty("touch.deviceType", "touchScreen");
6473 prepareDisplay(ui::ROTATION_0);
6474 prepareButtons();
6475 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00006476 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Ambrus Weisz7b6e16b2022-12-16 17:54:57 +00006477
6478 // Ensure that the device is created as a touchscreen, not touch navigation.
6479 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
6480
6481 // Add device type association after the device was created.
6482 mFakePolicy->addDeviceTypeAssociation(DEVICE_LOCATION, "touchNavigation");
6483
6484 // Send update to the mapper.
6485 std::list<NotifyArgs> unused2 =
6486 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00006487 InputReaderConfiguration::Change::DEVICE_TYPE /*changes*/);
Ambrus Weisz7b6e16b2022-12-16 17:54:57 +00006488
6489 // Check whether device type update was successful.
6490 ASSERT_EQ(AINPUT_SOURCE_TOUCH_NAVIGATION, mDevice->getSources());
6491}
6492
Prabir Pradhane1e309a2022-11-29 02:54:27 +00006493TEST_F(SingleTouchInputMapperTest, HoverEventsOutsidePhysicalFrameAreIgnored) {
6494 // Initialize the device without setting device source to touch navigation.
6495 addConfigurationProperty("touch.deviceType", "touchScreen");
6496 prepareDisplay(ui::ROTATION_0);
6497 prepareButtons();
6498 prepareAxes(POSITION);
6499 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
6500
6501 // Set a physical frame in the display viewport.
6502 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6503 viewport->physicalLeft = 0;
6504 viewport->physicalTop = 0;
6505 viewport->physicalRight = DISPLAY_WIDTH / 2;
6506 viewport->physicalBottom = DISPLAY_HEIGHT / 2;
6507 mFakePolicy->updateViewport(*viewport);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00006508 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhane1e309a2022-11-29 02:54:27 +00006509
Arpit Singha8c236b2023-04-25 13:56:05 +00006510 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhane1e309a2022-11-29 02:54:27 +00006511
6512 // Hovering inside the physical frame produces events.
6513 processKey(mapper, BTN_TOOL_PEN, 1);
6514 processMove(mapper, RAW_X_MIN + 1, RAW_Y_MIN + 1);
6515 processSync(mapper);
6516 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6517 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)));
6518 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6519 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE)));
6520
6521 // Leaving the physical frame ends the hovering gesture.
6522 processMove(mapper, RAW_X_MAX - 1, RAW_Y_MAX - 1);
6523 processSync(mapper);
6524 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6525 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT)));
6526
6527 // Moving outside the physical frame does not produce events.
6528 processMove(mapper, RAW_X_MAX - 2, RAW_Y_MAX - 2);
6529 processSync(mapper);
6530 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6531
6532 // Re-entering the physical frame produces events.
6533 processMove(mapper, RAW_X_MIN, RAW_Y_MIN);
6534 processSync(mapper);
6535 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6536 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)));
6537 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6538 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE)));
6539}
6540
Prabir Pradhan5632d622021-09-06 07:57:20 -07006541// --- TouchDisplayProjectionTest ---
6542
6543class TouchDisplayProjectionTest : public SingleTouchInputMapperTest {
6544public:
6545 // The values inside DisplayViewport are expected to be pre-rotated. This updates the current
6546 // DisplayViewport to pre-rotate the values. The viewport's physical display will be set to the
6547 // rotated equivalent of the given un-rotated physical display bounds.
Prabir Pradhana9df3162022-12-05 23:57:27 +00006548 void configurePhysicalDisplay(ui::Rotation orientation, Rect naturalPhysicalDisplay,
6549 int32_t naturalDisplayWidth = DISPLAY_WIDTH,
6550 int32_t naturalDisplayHeight = DISPLAY_HEIGHT) {
Prabir Pradhan5632d622021-09-06 07:57:20 -07006551 uint32_t inverseRotationFlags;
Prabir Pradhana9df3162022-12-05 23:57:27 +00006552 auto rotatedWidth = naturalDisplayWidth;
6553 auto rotatedHeight = naturalDisplayHeight;
Prabir Pradhan5632d622021-09-06 07:57:20 -07006554 switch (orientation) {
Michael Wrighta9cf4192022-12-01 23:46:39 +00006555 case ui::ROTATION_90:
Prabir Pradhan5632d622021-09-06 07:57:20 -07006556 inverseRotationFlags = ui::Transform::ROT_270;
Prabir Pradhana9df3162022-12-05 23:57:27 +00006557 std::swap(rotatedWidth, rotatedHeight);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006558 break;
Michael Wrighta9cf4192022-12-01 23:46:39 +00006559 case ui::ROTATION_180:
Prabir Pradhan5632d622021-09-06 07:57:20 -07006560 inverseRotationFlags = ui::Transform::ROT_180;
6561 break;
Michael Wrighta9cf4192022-12-01 23:46:39 +00006562 case ui::ROTATION_270:
Prabir Pradhan5632d622021-09-06 07:57:20 -07006563 inverseRotationFlags = ui::Transform::ROT_90;
Prabir Pradhana9df3162022-12-05 23:57:27 +00006564 std::swap(rotatedWidth, rotatedHeight);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006565 break;
Michael Wrighta9cf4192022-12-01 23:46:39 +00006566 case ui::ROTATION_0:
Prabir Pradhan5632d622021-09-06 07:57:20 -07006567 inverseRotationFlags = ui::Transform::ROT_0;
6568 break;
Prabir Pradhan5632d622021-09-06 07:57:20 -07006569 }
6570
Prabir Pradhana9df3162022-12-05 23:57:27 +00006571 const ui::Transform rotation(inverseRotationFlags, rotatedWidth, rotatedHeight);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006572 const Rect rotatedPhysicalDisplay = rotation.transform(naturalPhysicalDisplay);
6573
6574 std::optional<DisplayViewport> internalViewport =
6575 *mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6576 DisplayViewport& v = *internalViewport;
6577 v.displayId = DISPLAY_ID;
6578 v.orientation = orientation;
6579
6580 v.logicalLeft = 0;
6581 v.logicalTop = 0;
6582 v.logicalRight = 100;
6583 v.logicalBottom = 100;
6584
6585 v.physicalLeft = rotatedPhysicalDisplay.left;
6586 v.physicalTop = rotatedPhysicalDisplay.top;
6587 v.physicalRight = rotatedPhysicalDisplay.right;
6588 v.physicalBottom = rotatedPhysicalDisplay.bottom;
6589
Prabir Pradhana9df3162022-12-05 23:57:27 +00006590 v.deviceWidth = rotatedWidth;
6591 v.deviceHeight = rotatedHeight;
Prabir Pradhan5632d622021-09-06 07:57:20 -07006592
6593 v.isActive = true;
6594 v.uniqueId = UNIQUE_ID;
6595 v.type = ViewportType::INTERNAL;
6596 mFakePolicy->updateViewport(v);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00006597 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006598 }
6599
6600 void assertReceivedMove(const Point& point) {
6601 NotifyMotionArgs motionArgs;
6602 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6603 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07006604 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Prabir Pradhan5632d622021-09-06 07:57:20 -07006605 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], point.x, point.y,
6606 1, 0, 0, 0, 0, 0, 0, 0));
6607 }
6608};
6609
6610TEST_F(TouchDisplayProjectionTest, IgnoresTouchesOutsidePhysicalDisplay) {
6611 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006612 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006613
6614 prepareButtons();
6615 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00006616 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan5632d622021-09-06 07:57:20 -07006617
6618 NotifyMotionArgs motionArgs;
6619
6620 // Configure the DisplayViewport such that the logical display maps to a subsection of
6621 // the display panel called the physical display. Here, the physical display is bounded by the
6622 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
6623 static const Rect kPhysicalDisplay{10, 20, 70, 160};
6624 static const std::array<Point, 6> kPointsOutsidePhysicalDisplay{
6625 {{-10, -10}, {0, 0}, {5, 100}, {50, 15}, {75, 100}, {50, 165}}};
6626
Michael Wrighta9cf4192022-12-01 23:46:39 +00006627 for (auto orientation : {ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180, ui::ROTATION_270}) {
Prabir Pradhan5632d622021-09-06 07:57:20 -07006628 configurePhysicalDisplay(orientation, kPhysicalDisplay);
6629
6630 // Touches outside the physical display should be ignored, and should not generate any
6631 // events. Ensure touches at the following points that lie outside of the physical display
6632 // area do not generate any events.
6633 for (const auto& point : kPointsOutsidePhysicalDisplay) {
6634 processDown(mapper, toRawX(point.x), toRawY(point.y));
6635 processSync(mapper);
6636 processUp(mapper);
6637 processSync(mapper);
6638 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled())
6639 << "Unexpected event generated for touch outside physical display at point: "
6640 << point.x << ", " << point.y;
6641 }
6642 }
6643}
6644
6645TEST_F(TouchDisplayProjectionTest, EmitsTouchDownAfterEnteringPhysicalDisplay) {
6646 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006647 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006648
6649 prepareButtons();
6650 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00006651 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan5632d622021-09-06 07:57:20 -07006652
6653 NotifyMotionArgs motionArgs;
6654
6655 // Configure the DisplayViewport such that the logical display maps to a subsection of
6656 // the display panel called the physical display. Here, the physical display is bounded by the
6657 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
6658 static const Rect kPhysicalDisplay{10, 20, 70, 160};
6659
Michael Wrighta9cf4192022-12-01 23:46:39 +00006660 for (auto orientation : {ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180, ui::ROTATION_270}) {
Prabir Pradhan5632d622021-09-06 07:57:20 -07006661 configurePhysicalDisplay(orientation, kPhysicalDisplay);
6662
6663 // Touches that start outside the physical display should be ignored until it enters the
6664 // physical display bounds, at which point it should generate a down event. Start a touch at
6665 // the point (5, 100), which is outside the physical display bounds.
6666 static const Point kOutsidePoint{5, 100};
6667 processDown(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
6668 processSync(mapper);
6669 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6670
6671 // Move the touch into the physical display area. This should generate a pointer down.
6672 processMove(mapper, toRawX(11), toRawY(21));
6673 processSync(mapper);
6674 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6675 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07006676 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Prabir Pradhan5632d622021-09-06 07:57:20 -07006677 ASSERT_NO_FATAL_FAILURE(
6678 assertPointerCoords(motionArgs.pointerCoords[0], 11, 21, 1, 0, 0, 0, 0, 0, 0, 0));
6679
6680 // Move the touch inside the physical display area. This should generate a pointer move.
6681 processMove(mapper, toRawX(69), toRawY(159));
6682 processSync(mapper);
6683 assertReceivedMove({69, 159});
6684
6685 // Move outside the physical display area. Since the pointer is already down, this should
6686 // now continue generating events.
6687 processMove(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
6688 processSync(mapper);
6689 assertReceivedMove(kOutsidePoint);
6690
6691 // Release. This should generate a pointer up.
6692 processUp(mapper);
6693 processSync(mapper);
6694 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6695 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6696 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], kOutsidePoint.x,
6697 kOutsidePoint.y, 1, 0, 0, 0, 0, 0, 0, 0));
6698
6699 // Ensure no more events were generated.
6700 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6701 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6702 }
6703}
6704
Prabir Pradhana9df3162022-12-05 23:57:27 +00006705// --- TouchscreenPrecisionTests ---
6706
6707// This test suite is used to ensure that touchscreen devices are scaled and configured correctly
6708// in various orientations and with different display rotations. We configure the touchscreen to
6709// have a higher resolution than that of the display by an integer scale factor in each axis so that
6710// we can enforce that coordinates match precisely as expected.
6711class TouchscreenPrecisionTestsFixture : public TouchDisplayProjectionTest,
6712 public ::testing::WithParamInterface<ui::Rotation> {
6713public:
6714 void SetUp() override {
6715 SingleTouchInputMapperTest::SetUp();
6716
6717 // Prepare the raw axes to have twice the resolution of the display in the X axis and
6718 // four times the resolution of the display in the Y axis.
6719 prepareButtons();
6720 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, PRECISION_RAW_X_MIN, PRECISION_RAW_X_MAX,
Prabir Pradhan46211fb2022-12-17 00:30:39 +00006721 PRECISION_RAW_X_FLAT, PRECISION_RAW_X_FUZZ,
6722 PRECISION_RAW_X_RES);
Prabir Pradhana9df3162022-12-05 23:57:27 +00006723 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, PRECISION_RAW_Y_MIN, PRECISION_RAW_Y_MAX,
Prabir Pradhan46211fb2022-12-17 00:30:39 +00006724 PRECISION_RAW_Y_FLAT, PRECISION_RAW_Y_FUZZ,
6725 PRECISION_RAW_Y_RES);
Prabir Pradhana9df3162022-12-05 23:57:27 +00006726 }
6727
6728 static const int32_t PRECISION_RAW_X_MIN = TouchInputMapperTest::RAW_X_MIN;
6729 static const int32_t PRECISION_RAW_X_MAX = PRECISION_RAW_X_MIN + DISPLAY_WIDTH * 2 - 1;
6730 static const int32_t PRECISION_RAW_Y_MIN = TouchInputMapperTest::RAW_Y_MIN;
6731 static const int32_t PRECISION_RAW_Y_MAX = PRECISION_RAW_Y_MIN + DISPLAY_HEIGHT * 4 - 1;
6732
Prabir Pradhan46211fb2022-12-17 00:30:39 +00006733 static const int32_t PRECISION_RAW_X_RES = 50; // units per millimeter
6734 static const int32_t PRECISION_RAW_Y_RES = 100; // units per millimeter
6735
6736 static const int32_t PRECISION_RAW_X_FLAT = 16;
6737 static const int32_t PRECISION_RAW_Y_FLAT = 32;
6738
6739 static const int32_t PRECISION_RAW_X_FUZZ = 4;
6740 static const int32_t PRECISION_RAW_Y_FUZZ = 8;
6741
Prabir Pradhana9df3162022-12-05 23:57:27 +00006742 static const std::array<Point, 4> kRawCorners;
6743};
6744
6745const std::array<Point, 4> TouchscreenPrecisionTestsFixture::kRawCorners = {{
6746 {PRECISION_RAW_X_MIN, PRECISION_RAW_Y_MIN}, // left-top
6747 {PRECISION_RAW_X_MAX, PRECISION_RAW_Y_MIN}, // right-top
6748 {PRECISION_RAW_X_MAX, PRECISION_RAW_Y_MAX}, // right-bottom
6749 {PRECISION_RAW_X_MIN, PRECISION_RAW_Y_MAX}, // left-bottom
6750}};
6751
6752// Tests for how the touchscreen is oriented relative to the natural orientation of the display.
6753// For example, if a touchscreen is configured with an orientation of 90 degrees, it is a portrait
6754// touchscreen panel that is used on a device whose natural display orientation is in landscape.
6755TEST_P(TouchscreenPrecisionTestsFixture, OrientationPrecision) {
6756 enum class Orientation {
6757 ORIENTATION_0 = ui::toRotationInt(ui::ROTATION_0),
6758 ORIENTATION_90 = ui::toRotationInt(ui::ROTATION_90),
6759 ORIENTATION_180 = ui::toRotationInt(ui::ROTATION_180),
6760 ORIENTATION_270 = ui::toRotationInt(ui::ROTATION_270),
6761 ftl_last = ORIENTATION_270,
6762 };
6763 using Orientation::ORIENTATION_0, Orientation::ORIENTATION_90, Orientation::ORIENTATION_180,
6764 Orientation::ORIENTATION_270;
6765 static const std::map<Orientation, std::array<vec2, 4> /*mappedCorners*/> kMappedCorners = {
6766 {ORIENTATION_0, {{{0, 0}, {479.5, 0}, {479.5, 799.75}, {0, 799.75}}}},
6767 {ORIENTATION_90, {{{0, 479.5}, {0, 0}, {799.75, 0}, {799.75, 479.5}}}},
6768 {ORIENTATION_180, {{{479.5, 799.75}, {0, 799.75}, {0, 0}, {479.5, 0}}}},
6769 {ORIENTATION_270, {{{799.75, 0}, {799.75, 479.5}, {0, 479.5}, {0, 0}}}},
6770 };
6771
6772 const auto touchscreenOrientation = static_cast<Orientation>(ui::toRotationInt(GetParam()));
6773
6774 // Configure the touchscreen as being installed in the one of the four different orientations
6775 // relative to the display.
6776 addConfigurationProperty("touch.deviceType", "touchScreen");
6777 addConfigurationProperty("touch.orientation", ftl::enum_string(touchscreenOrientation).c_str());
6778 prepareDisplay(ui::ROTATION_0);
6779
Arpit Singha8c236b2023-04-25 13:56:05 +00006780 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhana9df3162022-12-05 23:57:27 +00006781
6782 // If the touchscreen is installed in a rotated orientation relative to the display (i.e. in
6783 // orientations of either 90 or 270) this means the display's natural resolution will be
6784 // flipped.
6785 const bool displayRotated =
6786 touchscreenOrientation == ORIENTATION_90 || touchscreenOrientation == ORIENTATION_270;
6787 const int32_t width = displayRotated ? DISPLAY_HEIGHT : DISPLAY_WIDTH;
6788 const int32_t height = displayRotated ? DISPLAY_WIDTH : DISPLAY_HEIGHT;
6789 const Rect physicalFrame{0, 0, width, height};
6790 configurePhysicalDisplay(ui::ROTATION_0, physicalFrame, width, height);
6791
6792 const auto& expectedPoints = kMappedCorners.at(touchscreenOrientation);
6793 const float expectedPrecisionX = displayRotated ? 4 : 2;
6794 const float expectedPrecisionY = displayRotated ? 2 : 4;
6795
6796 // Test all four corners.
6797 for (int i = 0; i < 4; i++) {
6798 const auto& raw = kRawCorners[i];
6799 processDown(mapper, raw.x, raw.y);
6800 processSync(mapper);
6801 const auto& expected = expectedPoints[i];
6802 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6803 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
6804 WithCoords(expected.x, expected.y),
6805 WithPrecision(expectedPrecisionX, expectedPrecisionY))))
6806 << "Failed to process raw point (" << raw.x << ", " << raw.y << ") "
6807 << "with touchscreen orientation "
6808 << ftl::enum_string(touchscreenOrientation).c_str() << ", expected point ("
6809 << expected.x << ", " << expected.y << ").";
6810 processUp(mapper);
6811 processSync(mapper);
6812 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6813 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
6814 WithCoords(expected.x, expected.y))));
6815 }
6816}
6817
Prabir Pradhan82687402022-12-06 01:32:53 +00006818TEST_P(TouchscreenPrecisionTestsFixture, RotationPrecisionWhenOrientationAware) {
6819 static const std::map<ui::Rotation /*rotation*/, std::array<vec2, 4> /*mappedCorners*/>
6820 kMappedCorners = {
6821 {ui::ROTATION_0, {{{0, 0}, {479.5, 0}, {479.5, 799.75}, {0, 799.75}}}},
6822 {ui::ROTATION_90, {{{0.5, 0}, {480, 0}, {480, 799.75}, {0.5, 799.75}}}},
6823 {ui::ROTATION_180, {{{0.5, 0.25}, {480, 0.25}, {480, 800}, {0.5, 800}}}},
6824 {ui::ROTATION_270, {{{0, 0.25}, {479.5, 0.25}, {479.5, 800}, {0, 800}}}},
6825 };
6826
6827 const ui::Rotation displayRotation = GetParam();
6828
6829 addConfigurationProperty("touch.deviceType", "touchScreen");
6830 prepareDisplay(displayRotation);
6831
Arpit Singha8c236b2023-04-25 13:56:05 +00006832 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan82687402022-12-06 01:32:53 +00006833
6834 const auto& expectedPoints = kMappedCorners.at(displayRotation);
6835
6836 // Test all four corners.
6837 for (int i = 0; i < 4; i++) {
6838 const auto& expected = expectedPoints[i];
6839 const auto& raw = kRawCorners[i];
6840 processDown(mapper, raw.x, raw.y);
6841 processSync(mapper);
6842 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6843 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
6844 WithCoords(expected.x, expected.y), WithPrecision(2, 4))))
6845 << "Failed to process raw point (" << raw.x << ", " << raw.y << ") "
6846 << "with display rotation " << ui::toCString(displayRotation)
6847 << ", expected point (" << expected.x << ", " << expected.y << ").";
6848 processUp(mapper);
6849 processSync(mapper);
6850 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6851 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
6852 WithCoords(expected.x, expected.y))));
6853 }
6854}
6855
Prabir Pradhan3e798762022-12-02 21:02:11 +00006856TEST_P(TouchscreenPrecisionTestsFixture, RotationPrecisionOrientationAwareInOri270) {
6857 static const std::map<ui::Rotation /*orientation*/, std::array<vec2, 4> /*mappedCorners*/>
6858 kMappedCorners = {
6859 {ui::ROTATION_0, {{{799.75, 0}, {799.75, 479.5}, {0, 479.5}, {0, 0}}}},
6860 {ui::ROTATION_90, {{{800, 0}, {800, 479.5}, {0.25, 479.5}, {0.25, 0}}}},
6861 {ui::ROTATION_180, {{{800, 0.5}, {800, 480}, {0.25, 480}, {0.25, 0.5}}}},
6862 {ui::ROTATION_270, {{{799.75, 0.5}, {799.75, 480}, {0, 480}, {0, 0.5}}}},
6863 };
6864
6865 const ui::Rotation displayRotation = GetParam();
6866
6867 addConfigurationProperty("touch.deviceType", "touchScreen");
6868 addConfigurationProperty("touch.orientation", "ORIENTATION_270");
6869
Arpit Singha8c236b2023-04-25 13:56:05 +00006870 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan3e798762022-12-02 21:02:11 +00006871
6872 // Ori 270, so width and height swapped
6873 const Rect physicalFrame{0, 0, DISPLAY_HEIGHT, DISPLAY_WIDTH};
6874 prepareDisplay(displayRotation);
6875 configurePhysicalDisplay(displayRotation, physicalFrame, DISPLAY_HEIGHT, DISPLAY_WIDTH);
6876
6877 const auto& expectedPoints = kMappedCorners.at(displayRotation);
6878
6879 // Test all four corners.
6880 for (int i = 0; i < 4; i++) {
6881 const auto& expected = expectedPoints[i];
6882 const auto& raw = kRawCorners[i];
6883 processDown(mapper, raw.x, raw.y);
6884 processSync(mapper);
6885 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6886 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
6887 WithCoords(expected.x, expected.y), WithPrecision(4, 2))))
6888 << "Failed to process raw point (" << raw.x << ", " << raw.y << ") "
6889 << "with display rotation " << ui::toCString(displayRotation)
6890 << ", expected point (" << expected.x << ", " << expected.y << ").";
6891 processUp(mapper);
6892 processSync(mapper);
6893 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6894 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
6895 WithCoords(expected.x, expected.y))));
6896 }
6897}
6898
Prabir Pradhan46211fb2022-12-17 00:30:39 +00006899TEST_P(TouchscreenPrecisionTestsFixture, MotionRangesAreOrientedInRotatedDisplay) {
6900 const ui::Rotation displayRotation = GetParam();
6901
6902 addConfigurationProperty("touch.deviceType", "touchScreen");
6903 prepareDisplay(displayRotation);
6904
6905 __attribute__((unused)) SingleTouchInputMapper& mapper =
Arpit Singha8c236b2023-04-25 13:56:05 +00006906 constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan46211fb2022-12-17 00:30:39 +00006907
6908 const InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
6909 // MotionRanges use display pixels as their units
6910 const auto* xRange = deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_X, AINPUT_SOURCE_TOUCHSCREEN);
6911 const auto* yRange = deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_Y, AINPUT_SOURCE_TOUCHSCREEN);
6912
6913 // The MotionRanges should be oriented in the rotated display's coordinate space
6914 const bool displayRotated =
6915 displayRotation == ui::ROTATION_90 || displayRotation == ui::ROTATION_270;
6916
6917 constexpr float MAX_X = 479.5;
6918 constexpr float MAX_Y = 799.75;
6919 EXPECT_EQ(xRange->min, 0.f);
6920 EXPECT_EQ(yRange->min, 0.f);
6921 EXPECT_EQ(xRange->max, displayRotated ? MAX_Y : MAX_X);
6922 EXPECT_EQ(yRange->max, displayRotated ? MAX_X : MAX_Y);
6923
6924 EXPECT_EQ(xRange->flat, 8.f);
6925 EXPECT_EQ(yRange->flat, 8.f);
6926
6927 EXPECT_EQ(xRange->fuzz, 2.f);
6928 EXPECT_EQ(yRange->fuzz, 2.f);
6929
6930 EXPECT_EQ(xRange->resolution, 25.f); // pixels per millimeter
6931 EXPECT_EQ(yRange->resolution, 25.f); // pixels per millimeter
6932}
6933
Prabir Pradhana9df3162022-12-05 23:57:27 +00006934// Run the precision tests for all rotations.
6935INSTANTIATE_TEST_SUITE_P(TouchscreenPrecisionTests, TouchscreenPrecisionTestsFixture,
6936 ::testing::Values(ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180,
6937 ui::ROTATION_270),
6938 [](const testing::TestParamInfo<ui::Rotation>& testParamInfo) {
6939 return ftl::enum_string(testParamInfo.param);
6940 });
6941
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006942// --- ExternalStylusFusionTest ---
6943
6944class ExternalStylusFusionTest : public SingleTouchInputMapperTest {
6945public:
6946 SingleTouchInputMapper& initializeInputMapperWithExternalStylus() {
6947 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006948 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006949 prepareButtons();
6950 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00006951 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006952
6953 mStylusState.when = ARBITRARY_TIME;
6954 mStylusState.pressure = 0.f;
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006955 mStylusState.toolType = ToolType::STYLUS;
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006956 mReader->getContext()->setExternalStylusDevices({mExternalStylusDeviceInfo});
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00006957 configureDevice(InputReaderConfiguration::Change::EXTERNAL_STYLUS_PRESENCE);
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006958 processExternalStylusState(mapper);
6959 return mapper;
6960 }
6961
6962 std::list<NotifyArgs> processExternalStylusState(InputMapper& mapper) {
6963 std::list<NotifyArgs> generatedArgs = mapper.updateExternalStylusState(mStylusState);
6964 for (const NotifyArgs& args : generatedArgs) {
6965 mFakeListener->notify(args);
6966 }
6967 // Loop the reader to flush the input listener queue.
6968 mReader->loopOnce();
6969 return generatedArgs;
6970 }
6971
6972protected:
6973 StylusState mStylusState{};
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006974
6975 void testStartFusedStylusGesture(SingleTouchInputMapper& mapper) {
6976 auto toolTypeSource =
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00006977 AllOf(WithSource(STYLUS_FUSION_SOURCE), WithToolType(ToolType::STYLUS));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006978
6979 // The first pointer is withheld.
6980 processDown(mapper, 100, 200);
6981 processSync(mapper);
6982 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6983 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasRequested(
6984 ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT));
6985
6986 // The external stylus reports pressure. The withheld finger pointer is released as a
6987 // stylus.
6988 mStylusState.pressure = 1.f;
6989 processExternalStylusState(mapper);
6990 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6991 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN))));
6992 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6993
6994 // Subsequent pointer events are not withheld.
6995 processMove(mapper, 101, 201);
6996 processSync(mapper);
6997 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6998 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE))));
6999
7000 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7001 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7002 }
7003
7004 void testSuccessfulFusionGesture(SingleTouchInputMapper& mapper) {
7005 ASSERT_NO_FATAL_FAILURE(testStartFusedStylusGesture(mapper));
7006
7007 // Releasing the touch pointer ends the gesture.
7008 processUp(mapper);
7009 processSync(mapper);
7010 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00007011 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithSource(STYLUS_FUSION_SOURCE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007012 WithToolType(ToolType::STYLUS))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007013
7014 mStylusState.pressure = 0.f;
7015 processExternalStylusState(mapper);
7016 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7017 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7018 }
7019
7020 void testUnsuccessfulFusionGesture(SingleTouchInputMapper& mapper) {
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00007021 // When stylus fusion is not successful, events should be reported with the original source.
7022 // In this case, it is from a touchscreen.
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007023 auto toolTypeSource =
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00007024 AllOf(WithSource(AINPUT_SOURCE_TOUCHSCREEN), WithToolType(ToolType::FINGER));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007025
7026 // The first pointer is withheld when an external stylus is connected,
7027 // and a timeout is requested.
7028 processDown(mapper, 100, 200);
7029 processSync(mapper);
7030 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7031 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasRequested(
7032 ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT));
7033
7034 // If the timeout expires early, it is requested again.
7035 handleTimeout(mapper, ARBITRARY_TIME + 1);
7036 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasRequested(
7037 ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT));
7038
7039 // When the timeout expires, the withheld touch is released as a finger pointer.
7040 handleTimeout(mapper, ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT);
7041 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7042 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN))));
7043
7044 // Subsequent pointer events are not withheld.
7045 processMove(mapper, 101, 201);
7046 processSync(mapper);
7047 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7048 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE))));
7049 processUp(mapper);
7050 processSync(mapper);
7051 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7052 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_UP))));
7053
7054 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7055 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7056 }
7057
7058private:
7059 InputDeviceInfo mExternalStylusDeviceInfo{};
7060};
7061
7062TEST_F(ExternalStylusFusionTest, UsesBluetoothStylusSource) {
7063 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00007064 ASSERT_EQ(STYLUS_FUSION_SOURCE, mapper.getSources());
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007065}
7066
7067TEST_F(ExternalStylusFusionTest, UnsuccessfulFusion) {
7068 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7069 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
7070}
7071
7072TEST_F(ExternalStylusFusionTest, SuccessfulFusion_TouchFirst) {
7073 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7074 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
7075}
7076
7077// Test a successful stylus fusion gesture where the pressure is reported by the external
7078// before the touch is reported by the touchscreen.
7079TEST_F(ExternalStylusFusionTest, SuccessfulFusion_PressureFirst) {
7080 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00007081 auto toolTypeSource = AllOf(WithSource(STYLUS_FUSION_SOURCE), WithToolType(ToolType::STYLUS));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007082
7083 // The external stylus reports pressure first. It is ignored for now.
7084 mStylusState.pressure = 1.f;
7085 processExternalStylusState(mapper);
7086 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7087 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7088
7089 // When the touch goes down afterwards, it is reported as a stylus pointer.
7090 processDown(mapper, 100, 200);
7091 processSync(mapper);
7092 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7093 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN))));
7094 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7095
7096 processMove(mapper, 101, 201);
7097 processSync(mapper);
7098 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7099 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE))));
7100 processUp(mapper);
7101 processSync(mapper);
7102 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7103 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_UP))));
7104
7105 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7106 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7107}
7108
7109TEST_F(ExternalStylusFusionTest, FusionIsRepeatedForEachNewGesture) {
7110 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7111
7112 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
7113 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
7114
7115 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
7116 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
7117 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
7118 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
7119}
7120
7121TEST_F(ExternalStylusFusionTest, FusedPointerReportsPressureChanges) {
7122 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00007123 auto toolTypeSource = AllOf(WithSource(STYLUS_FUSION_SOURCE), WithToolType(ToolType::STYLUS));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007124
7125 mStylusState.pressure = 0.8f;
7126 processExternalStylusState(mapper);
7127 processDown(mapper, 100, 200);
7128 processSync(mapper);
7129 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7130 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
7131 WithPressure(0.8f))));
7132 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7133
7134 // The external stylus reports a pressure change. We wait for some time for a touch event.
7135 mStylusState.pressure = 0.6f;
7136 processExternalStylusState(mapper);
7137 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7138 ASSERT_NO_FATAL_FAILURE(
7139 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7140
7141 // If a touch is reported within the timeout, it reports the updated pressure.
7142 processMove(mapper, 101, 201);
7143 processSync(mapper);
7144 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7145 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7146 WithPressure(0.6f))));
7147 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7148
7149 // There is another pressure change.
7150 mStylusState.pressure = 0.5f;
7151 processExternalStylusState(mapper);
7152 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7153 ASSERT_NO_FATAL_FAILURE(
7154 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7155
7156 // If a touch is not reported within the timeout, a move event is generated to report
7157 // the new pressure.
7158 handleTimeout(mapper, ARBITRARY_TIME + TOUCH_DATA_TIMEOUT);
7159 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7160 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7161 WithPressure(0.5f))));
7162
7163 // If a zero pressure is reported before the touch goes up, the previous pressure value is
7164 // repeated indefinitely.
7165 mStylusState.pressure = 0.0f;
7166 processExternalStylusState(mapper);
7167 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7168 ASSERT_NO_FATAL_FAILURE(
7169 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7170 processMove(mapper, 102, 202);
7171 processSync(mapper);
7172 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7173 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7174 WithPressure(0.5f))));
7175 processMove(mapper, 103, 203);
7176 processSync(mapper);
7177 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7178 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7179 WithPressure(0.5f))));
7180
7181 processUp(mapper);
7182 processSync(mapper);
7183 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00007184 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithSource(STYLUS_FUSION_SOURCE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007185 WithToolType(ToolType::STYLUS))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007186
7187 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7188 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7189}
7190
7191TEST_F(ExternalStylusFusionTest, FusedPointerReportsToolTypeChanges) {
7192 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00007193 auto source = WithSource(STYLUS_FUSION_SOURCE);
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007194
7195 mStylusState.pressure = 1.f;
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007196 mStylusState.toolType = ToolType::ERASER;
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007197 processExternalStylusState(mapper);
7198 processDown(mapper, 100, 200);
7199 processSync(mapper);
7200 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7201 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007202 WithToolType(ToolType::ERASER))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007203 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7204
7205 // The external stylus reports a tool change. We wait for some time for a touch event.
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007206 mStylusState.toolType = ToolType::STYLUS;
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007207 processExternalStylusState(mapper);
7208 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7209 ASSERT_NO_FATAL_FAILURE(
7210 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7211
7212 // If a touch is reported within the timeout, it reports the updated pressure.
7213 processMove(mapper, 101, 201);
7214 processSync(mapper);
7215 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7216 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007217 WithToolType(ToolType::STYLUS))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007218 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7219
7220 // There is another tool type change.
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007221 mStylusState.toolType = ToolType::FINGER;
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007222 processExternalStylusState(mapper);
7223 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7224 ASSERT_NO_FATAL_FAILURE(
7225 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7226
7227 // If a touch is not reported within the timeout, a move event is generated to report
7228 // the new tool type.
7229 handleTimeout(mapper, ARBITRARY_TIME + TOUCH_DATA_TIMEOUT);
7230 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7231 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007232 WithToolType(ToolType::FINGER))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007233
7234 processUp(mapper);
7235 processSync(mapper);
7236 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7237 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_UP),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007238 WithToolType(ToolType::FINGER))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007239
7240 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7241 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7242}
7243
7244TEST_F(ExternalStylusFusionTest, FusedPointerReportsButtons) {
7245 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00007246 auto toolTypeSource = AllOf(WithSource(STYLUS_FUSION_SOURCE), WithToolType(ToolType::STYLUS));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007247
7248 ASSERT_NO_FATAL_FAILURE(testStartFusedStylusGesture(mapper));
7249
7250 // The external stylus reports a button change. We wait for some time for a touch event.
7251 mStylusState.buttons = AMOTION_EVENT_BUTTON_STYLUS_PRIMARY;
7252 processExternalStylusState(mapper);
7253 ASSERT_NO_FATAL_FAILURE(
7254 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7255
7256 // If a touch is reported within the timeout, it reports the updated button state.
7257 processMove(mapper, 101, 201);
7258 processSync(mapper);
7259 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7260 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7261 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
7262 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7263 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
7264 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
7265 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7266
7267 // The button is now released.
7268 mStylusState.buttons = 0;
7269 processExternalStylusState(mapper);
7270 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7271 ASSERT_NO_FATAL_FAILURE(
7272 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7273
7274 // If a touch is not reported within the timeout, a move event is generated to report
7275 // the new button state.
7276 handleTimeout(mapper, ARBITRARY_TIME + TOUCH_DATA_TIMEOUT);
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007277 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7278 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
7279 WithButtonState(0))));
7280 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan124ea442022-10-28 20:27:44 +00007281 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7282 WithButtonState(0))));
7283
7284 processUp(mapper);
7285 processSync(mapper);
7286 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007287 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_UP), WithButtonState(0))));
7288
7289 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7290 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7291}
7292
Michael Wrightd02c5b62014-02-10 15:10:22 -08007293// --- MultiTouchInputMapperTest ---
7294
7295class MultiTouchInputMapperTest : public TouchInputMapperTest {
7296protected:
7297 void prepareAxes(int axes);
7298
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007299 void processPosition(MultiTouchInputMapper& mapper, int32_t x, int32_t y);
7300 void processTouchMajor(MultiTouchInputMapper& mapper, int32_t touchMajor);
7301 void processTouchMinor(MultiTouchInputMapper& mapper, int32_t touchMinor);
7302 void processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor);
7303 void processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor);
7304 void processOrientation(MultiTouchInputMapper& mapper, int32_t orientation);
7305 void processPressure(MultiTouchInputMapper& mapper, int32_t pressure);
7306 void processDistance(MultiTouchInputMapper& mapper, int32_t distance);
7307 void processId(MultiTouchInputMapper& mapper, int32_t id);
7308 void processSlot(MultiTouchInputMapper& mapper, int32_t slot);
7309 void processToolType(MultiTouchInputMapper& mapper, int32_t toolType);
7310 void processKey(MultiTouchInputMapper& mapper, int32_t code, int32_t value);
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00007311 void processHidUsage(MultiTouchInputMapper& mapper, int32_t usageCode, int32_t value);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007312 void processMTSync(MultiTouchInputMapper& mapper);
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00007313 void processSync(MultiTouchInputMapper& mapper, nsecs_t eventTime = ARBITRARY_TIME,
7314 nsecs_t readTime = READ_TIME);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007315};
7316
7317void MultiTouchInputMapperTest::prepareAxes(int axes) {
7318 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007319 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
7320 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007321 }
7322 if (axes & TOUCH) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007323 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN,
7324 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007325 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007326 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN,
7327 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007328 }
7329 }
7330 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007331 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7332 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007333 if (axes & MINOR) {
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007334 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007335 RAW_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007336 }
7337 }
7338 if (axes & ORIENTATION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007339 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_ORIENTATION, RAW_ORIENTATION_MIN,
7340 RAW_ORIENTATION_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007341 }
7342 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007343 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_PRESSURE, RAW_PRESSURE_MIN,
7344 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007345 }
7346 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007347 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_DISTANCE, RAW_DISTANCE_MIN,
7348 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007349 }
7350 if (axes & ID) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007351 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX, 0,
7352 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007353 }
7354 if (axes & SLOT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007355 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
7356 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_MT_SLOT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007357 }
7358 if (axes & TOOL_TYPE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007359 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOOL_TYPE, 0, MT_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007360 }
7361}
7362
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007363void MultiTouchInputMapperTest::processPosition(MultiTouchInputMapper& mapper, int32_t x,
7364 int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007365 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_X, x);
7366 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007367}
7368
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007369void MultiTouchInputMapperTest::processTouchMajor(MultiTouchInputMapper& mapper,
7370 int32_t touchMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007371 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007372}
7373
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007374void MultiTouchInputMapperTest::processTouchMinor(MultiTouchInputMapper& mapper,
7375 int32_t touchMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007376 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007377}
7378
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007379void MultiTouchInputMapperTest::processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007380 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007381}
7382
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007383void MultiTouchInputMapperTest::processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007384 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007385}
7386
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007387void MultiTouchInputMapperTest::processOrientation(MultiTouchInputMapper& mapper,
7388 int32_t orientation) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007389 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_ORIENTATION, orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007390}
7391
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007392void MultiTouchInputMapperTest::processPressure(MultiTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007393 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007394}
7395
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007396void MultiTouchInputMapperTest::processDistance(MultiTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007397 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007398}
7399
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007400void MultiTouchInputMapperTest::processId(MultiTouchInputMapper& mapper, int32_t id) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007401 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TRACKING_ID, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007402}
7403
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007404void MultiTouchInputMapperTest::processSlot(MultiTouchInputMapper& mapper, int32_t slot) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007405 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_SLOT, slot);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007406}
7407
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007408void MultiTouchInputMapperTest::processToolType(MultiTouchInputMapper& mapper, int32_t toolType) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007409 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007410}
7411
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007412void MultiTouchInputMapperTest::processKey(MultiTouchInputMapper& mapper, int32_t code,
7413 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007414 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007415}
7416
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00007417void MultiTouchInputMapperTest::processHidUsage(MultiTouchInputMapper& mapper, int32_t usageCode,
7418 int32_t value) {
7419 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, usageCode);
7420 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UNKNOWN, value);
7421}
7422
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007423void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007424 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_MT_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007425}
7426
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00007427void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper& mapper, nsecs_t eventTime,
7428 nsecs_t readTime) {
7429 process(mapper, eventTime, readTime, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007430}
7431
Michael Wrightd02c5b62014-02-10 15:10:22 -08007432TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007433 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007434 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007435 prepareAxes(POSITION);
7436 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00007437 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007438
arthurhungdcef2dc2020-08-11 14:47:50 +08007439 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007440
7441 NotifyMotionArgs motionArgs;
7442
7443 // Two fingers down at once.
7444 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7445 processPosition(mapper, x1, y1);
7446 processMTSync(mapper);
7447 processPosition(mapper, x2, y2);
7448 processMTSync(mapper);
7449 processSync(mapper);
7450
7451 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7452 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7453 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7454 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7455 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7456 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7457 ASSERT_EQ(0, motionArgs.flags);
7458 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7459 ASSERT_EQ(0, motionArgs.buttonState);
7460 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007461 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007462 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007463 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007464 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7465 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7466 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7467 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7468 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7469
7470 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7471 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7472 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7473 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7474 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007475 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007476 ASSERT_EQ(0, motionArgs.flags);
7477 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7478 ASSERT_EQ(0, motionArgs.buttonState);
7479 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007480 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007481 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007482 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007483 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007484 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007485 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7486 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7487 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7488 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7489 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7490 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7491 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7492
7493 // Move.
7494 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7495 processPosition(mapper, x1, y1);
7496 processMTSync(mapper);
7497 processPosition(mapper, x2, y2);
7498 processMTSync(mapper);
7499 processSync(mapper);
7500
7501 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7502 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7503 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7504 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7505 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7506 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7507 ASSERT_EQ(0, motionArgs.flags);
7508 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7509 ASSERT_EQ(0, motionArgs.buttonState);
7510 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007511 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007512 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007513 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007514 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007515 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007516 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7517 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7518 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7519 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7520 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7521 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7522 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7523
7524 // First finger up.
7525 x2 += 15; y2 -= 20;
7526 processPosition(mapper, x2, y2);
7527 processMTSync(mapper);
7528 processSync(mapper);
7529
7530 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7531 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7532 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7533 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7534 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007535 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007536 ASSERT_EQ(0, motionArgs.flags);
7537 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7538 ASSERT_EQ(0, motionArgs.buttonState);
7539 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007540 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007541 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007542 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007543 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007544 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007545 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7546 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7547 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7548 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7549 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7550 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7551 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7552
7553 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7554 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7555 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7556 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7557 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7558 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7559 ASSERT_EQ(0, motionArgs.flags);
7560 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7561 ASSERT_EQ(0, motionArgs.buttonState);
7562 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007563 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007564 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007565 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007566 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7567 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7568 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7569 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7570 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7571
7572 // Move.
7573 x2 += 20; y2 -= 25;
7574 processPosition(mapper, x2, y2);
7575 processMTSync(mapper);
7576 processSync(mapper);
7577
7578 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7579 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7580 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7581 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7582 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7583 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7584 ASSERT_EQ(0, motionArgs.flags);
7585 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7586 ASSERT_EQ(0, motionArgs.buttonState);
7587 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007588 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007589 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007590 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007591 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7592 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7593 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7594 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7595 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7596
7597 // New finger down.
7598 int32_t x3 = 700, y3 = 300;
7599 processPosition(mapper, x2, y2);
7600 processMTSync(mapper);
7601 processPosition(mapper, x3, y3);
7602 processMTSync(mapper);
7603 processSync(mapper);
7604
7605 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7606 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7607 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7608 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7609 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007610 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007611 ASSERT_EQ(0, motionArgs.flags);
7612 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7613 ASSERT_EQ(0, motionArgs.buttonState);
7614 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007615 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007616 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007617 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007618 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007619 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007620 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7621 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7622 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7623 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7624 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7625 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7626 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7627
7628 // Second finger up.
7629 x3 += 30; y3 -= 20;
7630 processPosition(mapper, x3, y3);
7631 processMTSync(mapper);
7632 processSync(mapper);
7633
7634 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7635 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7636 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7637 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7638 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007639 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007640 ASSERT_EQ(0, motionArgs.flags);
7641 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7642 ASSERT_EQ(0, motionArgs.buttonState);
7643 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007644 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007645 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007646 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007647 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007648 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007649 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7650 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7651 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7652 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7653 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7654 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7655 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7656
7657 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7658 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7659 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7660 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7661 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7662 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7663 ASSERT_EQ(0, motionArgs.flags);
7664 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7665 ASSERT_EQ(0, motionArgs.buttonState);
7666 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007667 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007668 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007669 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007670 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7671 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7672 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7673 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7674 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7675
7676 // Last finger up.
7677 processMTSync(mapper);
7678 processSync(mapper);
7679
7680 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7681 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7682 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7683 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7684 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7685 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7686 ASSERT_EQ(0, motionArgs.flags);
7687 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7688 ASSERT_EQ(0, motionArgs.buttonState);
7689 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007690 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007691 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007692 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007693 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7694 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7695 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7696 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7697 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7698
7699 // Should not have sent any more keys or motions.
7700 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7701 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7702}
7703
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007704TEST_F(MultiTouchInputMapperTest, AxisResolution_IsPopulated) {
7705 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007706 prepareDisplay(ui::ROTATION_0);
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007707
7708 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
7709 /*fuzz*/ 0, /*resolution*/ 10);
7710 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
7711 /*fuzz*/ 0, /*resolution*/ 11);
7712 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
7713 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 12);
7714 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
7715 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 13);
7716 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7717 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 14);
7718 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7719 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 15);
7720
Arpit Singha8c236b2023-04-25 13:56:05 +00007721 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007722
7723 // X and Y axes
7724 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_X, 10 / X_PRECISION);
7725 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_Y, 11 / Y_PRECISION);
7726 // Touch major and minor
7727 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR, 12 * GEOMETRIC_SCALE);
7728 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR, 13 * GEOMETRIC_SCALE);
7729 // Tool major and minor
7730 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR, 14 * GEOMETRIC_SCALE);
7731 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR, 15 * GEOMETRIC_SCALE);
7732}
7733
7734TEST_F(MultiTouchInputMapperTest, TouchMajorAndMinorAxes_DoNotAppearIfNotSupported) {
7735 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007736 prepareDisplay(ui::ROTATION_0);
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007737
7738 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
7739 /*fuzz*/ 0, /*resolution*/ 10);
7740 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
7741 /*fuzz*/ 0, /*resolution*/ 11);
7742
7743 // We do not add ABS_MT_TOUCH_MAJOR / MINOR or ABS_MT_WIDTH_MAJOR / MINOR axes
7744
Arpit Singha8c236b2023-04-25 13:56:05 +00007745 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007746
7747 // Touch major and minor
7748 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR);
7749 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR);
7750 // Tool major and minor
7751 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR);
7752 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR);
7753}
7754
Michael Wrightd02c5b62014-02-10 15:10:22 -08007755TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007756 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007757 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007758 prepareAxes(POSITION | ID);
7759 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00007760 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007761
arthurhungdcef2dc2020-08-11 14:47:50 +08007762 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007763
7764 NotifyMotionArgs motionArgs;
7765
7766 // Two fingers down at once.
7767 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7768 processPosition(mapper, x1, y1);
7769 processId(mapper, 1);
7770 processMTSync(mapper);
7771 processPosition(mapper, x2, y2);
7772 processId(mapper, 2);
7773 processMTSync(mapper);
7774 processSync(mapper);
7775
7776 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7777 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007778 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007779 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007780 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007781 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7782 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7783
7784 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007785 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007786 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007787 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007788 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007789 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007790 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007791 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7792 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7793 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7794 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7795
7796 // Move.
7797 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7798 processPosition(mapper, x1, y1);
7799 processId(mapper, 1);
7800 processMTSync(mapper);
7801 processPosition(mapper, x2, y2);
7802 processId(mapper, 2);
7803 processMTSync(mapper);
7804 processSync(mapper);
7805
7806 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7807 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007808 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007809 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007810 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007811 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007812 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007813 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7814 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7815 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7816 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7817
7818 // First finger up.
7819 x2 += 15; y2 -= 20;
7820 processPosition(mapper, x2, y2);
7821 processId(mapper, 2);
7822 processMTSync(mapper);
7823 processSync(mapper);
7824
7825 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007826 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007827 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007828 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007829 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007830 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007831 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007832 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7833 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7834 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7835 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7836
7837 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7838 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007839 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007840 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007841 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007842 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7843 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7844
7845 // Move.
7846 x2 += 20; y2 -= 25;
7847 processPosition(mapper, x2, y2);
7848 processId(mapper, 2);
7849 processMTSync(mapper);
7850 processSync(mapper);
7851
7852 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7853 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007854 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007855 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007856 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007857 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7858 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7859
7860 // New finger down.
7861 int32_t x3 = 700, y3 = 300;
7862 processPosition(mapper, x2, y2);
7863 processId(mapper, 2);
7864 processMTSync(mapper);
7865 processPosition(mapper, x3, y3);
7866 processId(mapper, 3);
7867 processMTSync(mapper);
7868 processSync(mapper);
7869
7870 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007871 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007872 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007873 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007874 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007875 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007876 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007877 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7878 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7879 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7880 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7881
7882 // Second finger up.
7883 x3 += 30; y3 -= 20;
7884 processPosition(mapper, x3, y3);
7885 processId(mapper, 3);
7886 processMTSync(mapper);
7887 processSync(mapper);
7888
7889 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007890 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007891 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007892 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007893 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007894 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007895 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007896 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7897 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7898 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7899 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7900
7901 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7902 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007903 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007904 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007905 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007906 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7907 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7908
7909 // Last finger up.
7910 processMTSync(mapper);
7911 processSync(mapper);
7912
7913 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7914 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007915 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007916 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007917 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007918 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7919 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7920
7921 // Should not have sent any more keys or motions.
7922 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7923 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7924}
7925
7926TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007927 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007928 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007929 prepareAxes(POSITION | ID | SLOT);
7930 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00007931 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007932
arthurhungdcef2dc2020-08-11 14:47:50 +08007933 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007934
7935 NotifyMotionArgs motionArgs;
7936
7937 // Two fingers down at once.
7938 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7939 processPosition(mapper, x1, y1);
7940 processId(mapper, 1);
7941 processSlot(mapper, 1);
7942 processPosition(mapper, x2, y2);
7943 processId(mapper, 2);
7944 processSync(mapper);
7945
7946 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7947 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007948 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007949 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007950 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007951 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7952 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7953
7954 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007955 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007956 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007957 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007958 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007959 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007960 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007961 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7962 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7963 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7964 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7965
7966 // Move.
7967 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7968 processSlot(mapper, 0);
7969 processPosition(mapper, x1, y1);
7970 processSlot(mapper, 1);
7971 processPosition(mapper, x2, y2);
7972 processSync(mapper);
7973
7974 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7975 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007976 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007977 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007978 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007979 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007980 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007981 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7982 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7983 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7984 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7985
7986 // First finger up.
7987 x2 += 15; y2 -= 20;
7988 processSlot(mapper, 0);
7989 processId(mapper, -1);
7990 processSlot(mapper, 1);
7991 processPosition(mapper, x2, y2);
7992 processSync(mapper);
7993
7994 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007995 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007996 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007997 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007998 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007999 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008000 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008001 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8002 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8003 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8004 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8005
8006 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8007 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008008 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008009 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008010 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008011 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8012 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8013
8014 // Move.
8015 x2 += 20; y2 -= 25;
8016 processPosition(mapper, x2, y2);
8017 processSync(mapper);
8018
8019 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8020 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008021 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008022 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008023 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008024 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8025 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8026
8027 // New finger down.
8028 int32_t x3 = 700, y3 = 300;
8029 processPosition(mapper, x2, y2);
8030 processSlot(mapper, 0);
8031 processId(mapper, 3);
8032 processPosition(mapper, x3, y3);
8033 processSync(mapper);
8034
8035 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008036 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008037 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008038 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008039 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008040 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008041 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008042 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8043 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8044 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8045 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8046
8047 // Second finger up.
8048 x3 += 30; y3 -= 20;
8049 processSlot(mapper, 1);
8050 processId(mapper, -1);
8051 processSlot(mapper, 0);
8052 processPosition(mapper, x3, y3);
8053 processSync(mapper);
8054
8055 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008056 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008057 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008058 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008059 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008060 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008061 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008062 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8063 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8064 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8065 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8066
8067 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8068 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008069 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008070 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008071 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008072 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8073 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8074
8075 // Last finger up.
8076 processId(mapper, -1);
8077 processSync(mapper);
8078
8079 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8080 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008081 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008082 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008083 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008084 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8085 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8086
8087 // Should not have sent any more keys or motions.
8088 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8089 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8090}
8091
8092TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008093 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008094 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008095 prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
Arpit Singha8c236b2023-04-25 13:56:05 +00008096 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008097
8098 // These calculations are based on the input device calibration documentation.
8099 int32_t rawX = 100;
8100 int32_t rawY = 200;
8101 int32_t rawTouchMajor = 7;
8102 int32_t rawTouchMinor = 6;
8103 int32_t rawToolMajor = 9;
8104 int32_t rawToolMinor = 8;
8105 int32_t rawPressure = 11;
8106 int32_t rawDistance = 0;
8107 int32_t rawOrientation = 3;
8108 int32_t id = 5;
8109
8110 float x = toDisplayX(rawX);
8111 float y = toDisplayY(rawY);
8112 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
8113 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
8114 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
8115 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
8116 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
8117 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
8118 float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
8119 float distance = float(rawDistance);
8120
8121 processPosition(mapper, rawX, rawY);
8122 processTouchMajor(mapper, rawTouchMajor);
8123 processTouchMinor(mapper, rawTouchMinor);
8124 processToolMajor(mapper, rawToolMajor);
8125 processToolMinor(mapper, rawToolMinor);
8126 processPressure(mapper, rawPressure);
8127 processOrientation(mapper, rawOrientation);
8128 processDistance(mapper, rawDistance);
8129 processId(mapper, id);
8130 processMTSync(mapper);
8131 processSync(mapper);
8132
8133 NotifyMotionArgs args;
8134 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8135 ASSERT_EQ(0, args.pointerProperties[0].id);
8136 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8137 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
8138 orientation, distance));
8139}
8140
8141TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008142 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008143 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008144 prepareAxes(POSITION | TOUCH | TOOL | MINOR);
8145 addConfigurationProperty("touch.size.calibration", "geometric");
Arpit Singha8c236b2023-04-25 13:56:05 +00008146 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008147
8148 // These calculations are based on the input device calibration documentation.
8149 int32_t rawX = 100;
8150 int32_t rawY = 200;
8151 int32_t rawTouchMajor = 140;
8152 int32_t rawTouchMinor = 120;
8153 int32_t rawToolMajor = 180;
8154 int32_t rawToolMinor = 160;
8155
8156 float x = toDisplayX(rawX);
8157 float y = toDisplayY(rawY);
8158 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
8159 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
8160 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
8161 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
8162 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
8163
8164 processPosition(mapper, rawX, rawY);
8165 processTouchMajor(mapper, rawTouchMajor);
8166 processTouchMinor(mapper, rawTouchMinor);
8167 processToolMajor(mapper, rawToolMajor);
8168 processToolMinor(mapper, rawToolMinor);
8169 processMTSync(mapper);
8170 processSync(mapper);
8171
8172 NotifyMotionArgs args;
8173 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8174 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8175 x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
8176}
8177
8178TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008179 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008180 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008181 prepareAxes(POSITION | TOUCH | TOOL);
8182 addConfigurationProperty("touch.size.calibration", "diameter");
8183 addConfigurationProperty("touch.size.scale", "10");
8184 addConfigurationProperty("touch.size.bias", "160");
8185 addConfigurationProperty("touch.size.isSummed", "1");
Arpit Singha8c236b2023-04-25 13:56:05 +00008186 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008187
8188 // These calculations are based on the input device calibration documentation.
8189 // Note: We only provide a single common touch/tool value because the device is assumed
8190 // not to emit separate values for each pointer (isSummed = 1).
8191 int32_t rawX = 100;
8192 int32_t rawY = 200;
8193 int32_t rawX2 = 150;
8194 int32_t rawY2 = 250;
8195 int32_t rawTouchMajor = 5;
8196 int32_t rawToolMajor = 8;
8197
8198 float x = toDisplayX(rawX);
8199 float y = toDisplayY(rawY);
8200 float x2 = toDisplayX(rawX2);
8201 float y2 = toDisplayY(rawY2);
8202 float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
8203 float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
8204 float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
8205
8206 processPosition(mapper, rawX, rawY);
8207 processTouchMajor(mapper, rawTouchMajor);
8208 processToolMajor(mapper, rawToolMajor);
8209 processMTSync(mapper);
8210 processPosition(mapper, rawX2, rawY2);
8211 processTouchMajor(mapper, rawTouchMajor);
8212 processToolMajor(mapper, rawToolMajor);
8213 processMTSync(mapper);
8214 processSync(mapper);
8215
8216 NotifyMotionArgs args;
8217 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8218 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
8219
8220 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008221 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008222 ASSERT_EQ(size_t(2), args.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008223 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8224 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
8225 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
8226 x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
8227}
8228
8229TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008230 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008231 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008232 prepareAxes(POSITION | TOUCH | TOOL);
8233 addConfigurationProperty("touch.size.calibration", "area");
8234 addConfigurationProperty("touch.size.scale", "43");
8235 addConfigurationProperty("touch.size.bias", "3");
Arpit Singha8c236b2023-04-25 13:56:05 +00008236 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008237
8238 // These calculations are based on the input device calibration documentation.
8239 int32_t rawX = 100;
8240 int32_t rawY = 200;
8241 int32_t rawTouchMajor = 5;
8242 int32_t rawToolMajor = 8;
8243
8244 float x = toDisplayX(rawX);
8245 float y = toDisplayY(rawY);
8246 float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
8247 float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
8248 float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
8249
8250 processPosition(mapper, rawX, rawY);
8251 processTouchMajor(mapper, rawTouchMajor);
8252 processToolMajor(mapper, rawToolMajor);
8253 processMTSync(mapper);
8254 processSync(mapper);
8255
8256 NotifyMotionArgs args;
8257 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8258 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8259 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
8260}
8261
8262TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008263 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008264 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008265 prepareAxes(POSITION | PRESSURE);
8266 addConfigurationProperty("touch.pressure.calibration", "amplitude");
8267 addConfigurationProperty("touch.pressure.scale", "0.01");
Arpit Singha8c236b2023-04-25 13:56:05 +00008268 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008269
Michael Wrightaa449c92017-12-13 21:21:43 +00008270 InputDeviceInfo info;
Harry Cuttsd02ea102023-03-17 18:21:30 +00008271 mapper.populateDeviceInfo(info);
Michael Wrightaa449c92017-12-13 21:21:43 +00008272 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
8273 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TOUCHSCREEN,
8274 0.0f, RAW_PRESSURE_MAX * 0.01, 0.0f, 0.0f));
8275
Michael Wrightd02c5b62014-02-10 15:10:22 -08008276 // These calculations are based on the input device calibration documentation.
8277 int32_t rawX = 100;
8278 int32_t rawY = 200;
8279 int32_t rawPressure = 60;
8280
8281 float x = toDisplayX(rawX);
8282 float y = toDisplayY(rawY);
8283 float pressure = float(rawPressure) * 0.01f;
8284
8285 processPosition(mapper, rawX, rawY);
8286 processPressure(mapper, rawPressure);
8287 processMTSync(mapper);
8288 processSync(mapper);
8289
8290 NotifyMotionArgs args;
8291 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8292 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8293 x, y, pressure, 0, 0, 0, 0, 0, 0, 0));
8294}
8295
8296TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008297 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008298 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008299 prepareAxes(POSITION | ID | SLOT);
Arpit Singha8c236b2023-04-25 13:56:05 +00008300 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008301
8302 NotifyMotionArgs motionArgs;
8303 NotifyKeyArgs keyArgs;
8304
8305 processId(mapper, 1);
8306 processPosition(mapper, 100, 200);
8307 processSync(mapper);
8308 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8309 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8310 ASSERT_EQ(0, motionArgs.buttonState);
8311
8312 // press BTN_LEFT, release BTN_LEFT
8313 processKey(mapper, BTN_LEFT, 1);
8314 processSync(mapper);
8315 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8316 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8317 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
8318
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008319 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8320 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8321 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
8322
Michael Wrightd02c5b62014-02-10 15:10:22 -08008323 processKey(mapper, BTN_LEFT, 0);
8324 processSync(mapper);
8325 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008326 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008327 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008328
8329 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008330 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008331 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008332
8333 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
8334 processKey(mapper, BTN_RIGHT, 1);
8335 processKey(mapper, BTN_MIDDLE, 1);
8336 processSync(mapper);
8337 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8338 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8339 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
8340 motionArgs.buttonState);
8341
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008342 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8343 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8344 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
8345
8346 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8347 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8348 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
8349 motionArgs.buttonState);
8350
Michael Wrightd02c5b62014-02-10 15:10:22 -08008351 processKey(mapper, BTN_RIGHT, 0);
8352 processSync(mapper);
8353 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008354 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008355 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008356
8357 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008358 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008359 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008360
8361 processKey(mapper, BTN_MIDDLE, 0);
8362 processSync(mapper);
8363 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008364 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008365 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008366
8367 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008368 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008369 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008370
8371 // press BTN_BACK, release BTN_BACK
8372 processKey(mapper, BTN_BACK, 1);
8373 processSync(mapper);
8374 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8375 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8376 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008377
Michael Wrightd02c5b62014-02-10 15:10:22 -08008378 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008379 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008380 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
8381
8382 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8383 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8384 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008385
8386 processKey(mapper, BTN_BACK, 0);
8387 processSync(mapper);
8388 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008389 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008390 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008391
8392 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008393 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008394 ASSERT_EQ(0, motionArgs.buttonState);
8395
Michael Wrightd02c5b62014-02-10 15:10:22 -08008396 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8397 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8398 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
8399
8400 // press BTN_SIDE, release BTN_SIDE
8401 processKey(mapper, BTN_SIDE, 1);
8402 processSync(mapper);
8403 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8404 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8405 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008406
Michael Wrightd02c5b62014-02-10 15:10:22 -08008407 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008408 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008409 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
8410
8411 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8412 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8413 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008414
8415 processKey(mapper, BTN_SIDE, 0);
8416 processSync(mapper);
8417 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008418 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008419 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008420
8421 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008422 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008423 ASSERT_EQ(0, motionArgs.buttonState);
8424
Michael Wrightd02c5b62014-02-10 15:10:22 -08008425 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8426 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8427 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
8428
8429 // press BTN_FORWARD, release BTN_FORWARD
8430 processKey(mapper, BTN_FORWARD, 1);
8431 processSync(mapper);
8432 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8433 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8434 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008435
Michael Wrightd02c5b62014-02-10 15:10:22 -08008436 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008437 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008438 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
8439
8440 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8441 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8442 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008443
8444 processKey(mapper, BTN_FORWARD, 0);
8445 processSync(mapper);
8446 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008447 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008448 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008449
8450 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008451 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008452 ASSERT_EQ(0, motionArgs.buttonState);
8453
Michael Wrightd02c5b62014-02-10 15:10:22 -08008454 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8455 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8456 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
8457
8458 // press BTN_EXTRA, release BTN_EXTRA
8459 processKey(mapper, BTN_EXTRA, 1);
8460 processSync(mapper);
8461 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8462 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8463 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008464
Michael Wrightd02c5b62014-02-10 15:10:22 -08008465 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008466 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008467 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
8468
8469 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8470 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8471 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008472
8473 processKey(mapper, BTN_EXTRA, 0);
8474 processSync(mapper);
8475 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008476 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008477 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008478
8479 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008480 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008481 ASSERT_EQ(0, motionArgs.buttonState);
8482
Michael Wrightd02c5b62014-02-10 15:10:22 -08008483 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8484 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8485 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
8486
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008487 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8488
Michael Wrightd02c5b62014-02-10 15:10:22 -08008489 // press BTN_STYLUS, release BTN_STYLUS
8490 processKey(mapper, BTN_STYLUS, 1);
8491 processSync(mapper);
8492 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8493 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008494 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
8495
8496 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8497 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8498 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008499
8500 processKey(mapper, BTN_STYLUS, 0);
8501 processSync(mapper);
8502 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008503 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008504 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008505
8506 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008507 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008508 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008509
8510 // press BTN_STYLUS2, release BTN_STYLUS2
8511 processKey(mapper, BTN_STYLUS2, 1);
8512 processSync(mapper);
8513 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8514 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008515 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
8516
8517 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8518 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8519 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008520
8521 processKey(mapper, BTN_STYLUS2, 0);
8522 processSync(mapper);
8523 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008524 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008525 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008526
8527 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008528 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008529 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008530
8531 // release touch
8532 processId(mapper, -1);
8533 processSync(mapper);
8534 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8535 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8536 ASSERT_EQ(0, motionArgs.buttonState);
8537}
8538
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00008539TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleMappedStylusButtons) {
8540 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008541 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00008542 prepareAxes(POSITION | ID | SLOT);
Arpit Singha8c236b2023-04-25 13:56:05 +00008543 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00008544
8545 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_STYLUS_BUTTON_PRIMARY, 0);
8546 mFakeEventHub->addKey(EVENTHUB_ID, 0, 0xabcd, AKEYCODE_STYLUS_BUTTON_SECONDARY, 0);
8547
8548 // Touch down.
8549 processId(mapper, 1);
8550 processPosition(mapper, 100, 200);
8551 processSync(mapper);
8552 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8553 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithButtonState(0))));
8554
8555 // Press and release button mapped to the primary stylus button.
8556 processKey(mapper, BTN_A, 1);
8557 processSync(mapper);
8558 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8559 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
8560 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
8561 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8562 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
8563 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
8564
8565 processKey(mapper, BTN_A, 0);
8566 processSync(mapper);
8567 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8568 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE), WithButtonState(0))));
8569 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8570 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
8571
8572 // Press and release the HID usage mapped to the secondary stylus button.
8573 processHidUsage(mapper, 0xabcd, 1);
8574 processSync(mapper);
8575 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8576 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
8577 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY))));
8578 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8579 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
8580 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY))));
8581
8582 processHidUsage(mapper, 0xabcd, 0);
8583 processSync(mapper);
8584 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8585 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE), WithButtonState(0))));
8586 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8587 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
8588
8589 // Release touch.
8590 processId(mapper, -1);
8591 processSync(mapper);
8592 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8593 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithButtonState(0))));
8594}
8595
Michael Wrightd02c5b62014-02-10 15:10:22 -08008596TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008597 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008598 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008599 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +00008600 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008601
8602 NotifyMotionArgs motionArgs;
8603
8604 // default tool type is finger
8605 processId(mapper, 1);
8606 processPosition(mapper, 100, 200);
8607 processSync(mapper);
8608 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8609 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008610 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008611
8612 // eraser
8613 processKey(mapper, BTN_TOOL_RUBBER, 1);
8614 processSync(mapper);
8615 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8616 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008617 ASSERT_EQ(ToolType::ERASER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008618
8619 // stylus
8620 processKey(mapper, BTN_TOOL_RUBBER, 0);
8621 processKey(mapper, BTN_TOOL_PEN, 1);
8622 processSync(mapper);
8623 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8624 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008625 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008626
8627 // brush
8628 processKey(mapper, BTN_TOOL_PEN, 0);
8629 processKey(mapper, BTN_TOOL_BRUSH, 1);
8630 processSync(mapper);
8631 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8632 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008633 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008634
8635 // pencil
8636 processKey(mapper, BTN_TOOL_BRUSH, 0);
8637 processKey(mapper, BTN_TOOL_PENCIL, 1);
8638 processSync(mapper);
8639 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8640 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008641 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008642
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08008643 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08008644 processKey(mapper, BTN_TOOL_PENCIL, 0);
8645 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
8646 processSync(mapper);
8647 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8648 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008649 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008650
8651 // mouse
8652 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
8653 processKey(mapper, BTN_TOOL_MOUSE, 1);
8654 processSync(mapper);
8655 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8656 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008657 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008658
8659 // lens
8660 processKey(mapper, BTN_TOOL_MOUSE, 0);
8661 processKey(mapper, BTN_TOOL_LENS, 1);
8662 processSync(mapper);
8663 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8664 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008665 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008666
8667 // double-tap
8668 processKey(mapper, BTN_TOOL_LENS, 0);
8669 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
8670 processSync(mapper);
8671 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8672 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008673 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008674
8675 // triple-tap
8676 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
8677 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
8678 processSync(mapper);
8679 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8680 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008681 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008682
8683 // quad-tap
8684 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
8685 processKey(mapper, BTN_TOOL_QUADTAP, 1);
8686 processSync(mapper);
8687 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8688 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008689 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008690
8691 // finger
8692 processKey(mapper, BTN_TOOL_QUADTAP, 0);
8693 processKey(mapper, BTN_TOOL_FINGER, 1);
8694 processSync(mapper);
8695 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8696 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008697 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008698
8699 // stylus trumps finger
8700 processKey(mapper, BTN_TOOL_PEN, 1);
8701 processSync(mapper);
8702 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8703 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008704 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008705
8706 // eraser trumps stylus
8707 processKey(mapper, BTN_TOOL_RUBBER, 1);
8708 processSync(mapper);
8709 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8710 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008711 ASSERT_EQ(ToolType::ERASER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008712
8713 // mouse trumps eraser
8714 processKey(mapper, BTN_TOOL_MOUSE, 1);
8715 processSync(mapper);
8716 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8717 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008718 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008719
8720 // MT tool type trumps BTN tool types: MT_TOOL_FINGER
8721 processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
8722 processSync(mapper);
8723 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8724 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008725 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008726
8727 // MT tool type trumps BTN tool types: MT_TOOL_PEN
8728 processToolType(mapper, MT_TOOL_PEN);
8729 processSync(mapper);
8730 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8731 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008732 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008733
8734 // back to default tool type
8735 processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
8736 processKey(mapper, BTN_TOOL_MOUSE, 0);
8737 processKey(mapper, BTN_TOOL_RUBBER, 0);
8738 processKey(mapper, BTN_TOOL_PEN, 0);
8739 processKey(mapper, BTN_TOOL_FINGER, 0);
8740 processSync(mapper);
8741 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8742 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008743 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008744}
8745
8746TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008747 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008748 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008749 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008750 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Arpit Singha8c236b2023-04-25 13:56:05 +00008751 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008752
8753 NotifyMotionArgs motionArgs;
8754
8755 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
8756 processId(mapper, 1);
8757 processPosition(mapper, 100, 200);
8758 processSync(mapper);
8759 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8760 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8761 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8762 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8763
8764 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8765 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8766 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8767 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8768
8769 // move a little
8770 processPosition(mapper, 150, 250);
8771 processSync(mapper);
8772 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8773 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8774 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8775 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8776
8777 // down when BTN_TOUCH is pressed, pressure defaults to 1
8778 processKey(mapper, BTN_TOUCH, 1);
8779 processSync(mapper);
8780 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8781 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8782 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8783 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8784
8785 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8786 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8787 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8788 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8789
8790 // up when BTN_TOUCH is released, hover restored
8791 processKey(mapper, BTN_TOUCH, 0);
8792 processSync(mapper);
8793 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8794 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8795 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8796 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8797
8798 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8799 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8800 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8801 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8802
8803 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8804 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8805 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8806 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8807
8808 // exit hover when pointer goes away
8809 processId(mapper, -1);
8810 processSync(mapper);
8811 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8812 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8813 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8814 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8815}
8816
8817TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008818 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008819 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008820 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +00008821 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008822
8823 NotifyMotionArgs motionArgs;
8824
8825 // initially hovering because pressure is 0
8826 processId(mapper, 1);
8827 processPosition(mapper, 100, 200);
8828 processPressure(mapper, 0);
8829 processSync(mapper);
8830 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8831 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8832 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8833 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8834
8835 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8836 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8837 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8838 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8839
8840 // move a little
8841 processPosition(mapper, 150, 250);
8842 processSync(mapper);
8843 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8844 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8845 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8846 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8847
8848 // down when pressure becomes non-zero
8849 processPressure(mapper, RAW_PRESSURE_MAX);
8850 processSync(mapper);
8851 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8852 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8853 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8854 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8855
8856 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8857 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8858 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8859 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8860
8861 // up when pressure becomes 0, hover restored
8862 processPressure(mapper, 0);
8863 processSync(mapper);
8864 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8865 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8866 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8867 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8868
8869 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8870 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8871 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8872 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8873
8874 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8875 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8876 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8877 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8878
8879 // exit hover when pointer goes away
8880 processId(mapper, -1);
8881 processSync(mapper);
8882 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8883 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8884 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8885 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8886}
8887
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008888/**
8889 * Set the input device port <--> display port associations, and check that the
8890 * events are routed to the display that matches the display port.
8891 * This can be checked by looking at the displayId of the resulting NotifyMotionArgs.
8892 */
8893TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayPort) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008894 const std::string usb2 = "USB2";
8895 const uint8_t hdmi1 = 0;
8896 const uint8_t hdmi2 = 1;
8897 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008898 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008899
8900 addConfigurationProperty("touch.deviceType", "touchScreen");
8901 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00008902 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008903
8904 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
8905 mFakePolicy->addInputPortAssociation(usb2, hdmi2);
8906
8907 // We are intentionally not adding the viewport for display 1 yet. Since the port association
8908 // for this input device is specified, and the matching viewport is not present,
8909 // the input device should be disabled (at the mapper level).
8910
8911 // Add viewport for display 2 on hdmi2
8912 prepareSecondaryDisplay(type, hdmi2);
8913 // Send a touch event
8914 processPosition(mapper, 100, 100);
8915 processSync(mapper);
8916 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8917
8918 // Add viewport for display 1 on hdmi1
Michael Wrighta9cf4192022-12-01 23:46:39 +00008919 prepareDisplay(ui::ROTATION_0, hdmi1);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008920 // Send a touch event again
8921 processPosition(mapper, 100, 100);
8922 processSync(mapper);
8923
8924 NotifyMotionArgs args;
8925 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8926 ASSERT_EQ(DISPLAY_ID, args.displayId);
8927}
Michael Wrightd02c5b62014-02-10 15:10:22 -08008928
Arthur Hung6d5b4b22022-01-21 07:21:10 +00008929TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayUniqueId) {
8930 addConfigurationProperty("touch.deviceType", "touchScreen");
8931 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00008932 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung6d5b4b22022-01-21 07:21:10 +00008933
8934 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
8935
Michael Wrighta9cf4192022-12-01 23:46:39 +00008936 prepareDisplay(ui::ROTATION_0);
8937 prepareVirtualDisplay(ui::ROTATION_0);
Arthur Hung6d5b4b22022-01-21 07:21:10 +00008938
8939 // Send a touch event
8940 processPosition(mapper, 100, 100);
8941 processSync(mapper);
8942
8943 NotifyMotionArgs args;
8944 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8945 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
8946}
8947
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008948TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShouldHandleDisplayId) {
Garfield Tan888a6a42020-01-09 11:39:16 -08008949 // Setup for second display.
Michael Wright17db18e2020-06-26 20:51:44 +01008950 std::shared_ptr<FakePointerController> fakePointerController =
8951 std::make_shared<FakePointerController>();
Garfield Tan888a6a42020-01-09 11:39:16 -08008952 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008953 fakePointerController->setPosition(100, 200);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00008954 mFakePolicy->setPointerController(fakePointerController);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008955
Garfield Tan888a6a42020-01-09 11:39:16 -08008956 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008957 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08008958
Michael Wrighta9cf4192022-12-01 23:46:39 +00008959 prepareDisplay(ui::ROTATION_0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008960 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00008961 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008962
Josep del Río2d8c79a2023-01-23 19:33:50 +00008963 // Check source is mouse that would obtain the PointerController.
8964 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008965
8966 NotifyMotionArgs motionArgs;
8967 processPosition(mapper, 100, 100);
8968 processSync(mapper);
8969
8970 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8971 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8972 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
8973}
8974
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00008975/**
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008976 * Ensure that the readTime is set to the SYN_REPORT value when processing touch events.
8977 */
8978TEST_F(MultiTouchInputMapperTest, Process_SendsReadTime) {
8979 addConfigurationProperty("touch.deviceType", "touchScreen");
8980 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00008981 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008982
Michael Wrighta9cf4192022-12-01 23:46:39 +00008983 prepareDisplay(ui::ROTATION_0);
Harry Cutts33476232023-01-30 19:57:29 +00008984 process(mapper, 10, /*readTime=*/11, EV_ABS, ABS_MT_TRACKING_ID, 1);
8985 process(mapper, 15, /*readTime=*/16, EV_ABS, ABS_MT_POSITION_X, 100);
8986 process(mapper, 20, /*readTime=*/21, EV_ABS, ABS_MT_POSITION_Y, 100);
8987 process(mapper, 25, /*readTime=*/26, EV_SYN, SYN_REPORT, 0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008988
8989 NotifyMotionArgs args;
8990 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8991 ASSERT_EQ(26, args.readTime);
8992
Harry Cutts33476232023-01-30 19:57:29 +00008993 process(mapper, 30, /*readTime=*/31, EV_ABS, ABS_MT_POSITION_X, 110);
8994 process(mapper, 30, /*readTime=*/32, EV_ABS, ABS_MT_POSITION_Y, 220);
8995 process(mapper, 30, /*readTime=*/33, EV_SYN, SYN_REPORT, 0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008996
8997 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8998 ASSERT_EQ(33, args.readTime);
8999}
9000
9001/**
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00009002 * When the viewport is not active (isActive=false), the touch mapper should be disabled and the
9003 * events should not be delivered to the listener.
9004 */
9005TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreDropped) {
9006 addConfigurationProperty("touch.deviceType", "touchScreen");
Yuncheol Heo50c19b12022-11-02 20:33:08 -07009007 // Don't set touch.enableForInactiveViewport to verify the default behavior.
Michael Wrighta9cf4192022-12-01 23:46:39 +00009008 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +00009009 /*isActive=*/false, UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00009010 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00009011 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00009012 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00009013
9014 NotifyMotionArgs motionArgs;
9015 processPosition(mapper, 100, 100);
9016 processSync(mapper);
9017
9018 mFakeListener->assertNotifyMotionWasNotCalled();
9019}
9020
Yuncheol Heo50c19b12022-11-02 20:33:08 -07009021/**
9022 * When the viewport is not active (isActive=false) and touch.enableForInactiveViewport is true,
9023 * the touch mapper can process the events and the events can be delivered to the listener.
9024 */
9025TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreProcessed) {
9026 addConfigurationProperty("touch.deviceType", "touchScreen");
9027 addConfigurationProperty("touch.enableForInactiveViewport", "1");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009028 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +00009029 /*isActive=*/false, UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00009030 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Yuncheol Heo50c19b12022-11-02 20:33:08 -07009031 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00009032 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Yuncheol Heo50c19b12022-11-02 20:33:08 -07009033
9034 NotifyMotionArgs motionArgs;
9035 processPosition(mapper, 100, 100);
9036 processSync(mapper);
9037
9038 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9039 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9040}
9041
Josh Thielene986aed2023-06-01 14:17:30 +00009042/**
9043 * When the viewport is deactivated (isActive transitions from true to false),
9044 * and touch.enableForInactiveViewport is false, touches prior to the transition
9045 * should be cancelled.
9046 */
Garfield Tanc734e4f2021-01-15 20:01:39 -08009047TEST_F(MultiTouchInputMapperTest, Process_DeactivateViewport_AbortTouches) {
9048 addConfigurationProperty("touch.deviceType", "touchScreen");
Yuncheol Heo50c19b12022-11-02 20:33:08 -07009049 addConfigurationProperty("touch.enableForInactiveViewport", "0");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009050 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +00009051 /*isActive=*/true, UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Garfield Tanc734e4f2021-01-15 20:01:39 -08009052 std::optional<DisplayViewport> optionalDisplayViewport =
9053 mFakePolicy->getDisplayViewportByUniqueId(UNIQUE_ID);
9054 ASSERT_TRUE(optionalDisplayViewport.has_value());
9055 DisplayViewport displayViewport = *optionalDisplayViewport;
9056
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00009057 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Garfield Tanc734e4f2021-01-15 20:01:39 -08009058 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00009059 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Garfield Tanc734e4f2021-01-15 20:01:39 -08009060
9061 // Finger down
9062 int32_t x = 100, y = 100;
9063 processPosition(mapper, x, y);
9064 processSync(mapper);
9065
9066 NotifyMotionArgs motionArgs;
9067 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9068 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9069
9070 // Deactivate display viewport
9071 displayViewport.isActive = false;
9072 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00009073 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Garfield Tanc734e4f2021-01-15 20:01:39 -08009074
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00009075 // The ongoing touch should be canceled immediately
9076 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9077 EXPECT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9078
9079 // Finger move is ignored
Garfield Tanc734e4f2021-01-15 20:01:39 -08009080 x += 10, y += 10;
9081 processPosition(mapper, x, y);
9082 processSync(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00009083 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
Garfield Tanc734e4f2021-01-15 20:01:39 -08009084
9085 // Reactivate display viewport
9086 displayViewport.isActive = true;
9087 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00009088 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Garfield Tanc734e4f2021-01-15 20:01:39 -08009089
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00009090 // Finger move again starts new gesture
Garfield Tanc734e4f2021-01-15 20:01:39 -08009091 x += 10, y += 10;
9092 processPosition(mapper, x, y);
9093 processSync(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00009094 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9095 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Garfield Tanc734e4f2021-01-15 20:01:39 -08009096}
9097
Josh Thielene986aed2023-06-01 14:17:30 +00009098/**
9099 * When the viewport is deactivated (isActive transitions from true to false),
9100 * and touch.enableForInactiveViewport is true, touches prior to the transition
9101 * should not be cancelled.
9102 */
9103TEST_F(MultiTouchInputMapperTest, Process_DeactivateViewport_TouchesNotAborted) {
9104 addConfigurationProperty("touch.deviceType", "touchScreen");
9105 addConfigurationProperty("touch.enableForInactiveViewport", "1");
9106 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
9107 /*isActive=*/true, UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
9108 std::optional<DisplayViewport> optionalDisplayViewport =
9109 mFakePolicy->getDisplayViewportByUniqueId(UNIQUE_ID);
9110 ASSERT_TRUE(optionalDisplayViewport.has_value());
9111 DisplayViewport displayViewport = *optionalDisplayViewport;
9112
9113 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
9114 prepareAxes(POSITION);
9115 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
9116
9117 // Finger down
9118 int32_t x = 100, y = 100;
9119 processPosition(mapper, x, y);
9120 processSync(mapper);
9121 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9122 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
9123
9124 // Deactivate display viewport
9125 displayViewport.isActive = false;
9126 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
9127 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
9128
9129 // The ongoing touch should not be canceled
9130 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9131
9132 // Finger move is not ignored
9133 x += 10, y += 10;
9134 processPosition(mapper, x, y);
9135 processSync(mapper);
9136 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9137 WithMotionAction(AMOTION_EVENT_ACTION_MOVE)));
9138
9139 // Reactivate display viewport
9140 displayViewport.isActive = true;
9141 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
9142 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
9143
9144 // Finger move continues and does not start new gesture
9145 x += 10, y += 10;
9146 processPosition(mapper, x, y);
9147 processSync(mapper);
9148 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9149 WithMotionAction(AMOTION_EVENT_ACTION_MOVE)));
9150}
9151
Arthur Hung7c645402019-01-25 17:45:42 +08009152TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShowTouches) {
9153 // Setup the first touch screen device.
Arthur Hung7c645402019-01-25 17:45:42 +08009154 prepareAxes(POSITION | ID | SLOT);
9155 addConfigurationProperty("touch.deviceType", "touchScreen");
Arpit Singha8c236b2023-04-25 13:56:05 +00009156 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung7c645402019-01-25 17:45:42 +08009157
9158 // Create the second touch screen device, and enable multi fingers.
9159 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08009160 const std::string DEVICE_NAME2 = "TOUCHSCREEN2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08009161 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009162 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08009163 std::shared_ptr<InputDevice> device2 =
9164 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07009165 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08009166
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009167 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
Harry Cutts33476232023-01-30 19:57:29 +00009168 /*flat=*/0, /*fuzz=*/0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009169 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
Harry Cutts33476232023-01-30 19:57:29 +00009170 /*flat=*/0, /*fuzz=*/0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009171 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX,
Harry Cutts33476232023-01-30 19:57:29 +00009172 /*flat=*/0, /*fuzz=*/0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009173 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX,
Harry Cutts33476232023-01-30 19:57:29 +00009174 /*flat=*/0, /*fuzz=*/0);
9175 mFakeEventHub->setAbsoluteAxisValue(SECOND_EVENTHUB_ID, ABS_MT_SLOT, /*value=*/0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009176 mFakeEventHub->addConfigurationProperty(SECOND_EVENTHUB_ID, String8("touch.deviceType"),
9177 String8("touchScreen"));
Arthur Hung7c645402019-01-25 17:45:42 +08009178
9179 // Setup the second touch screen device.
Arpit Singha8c236b2023-04-25 13:56:05 +00009180 device2->addEmptyEventHubDevice(SECOND_EVENTHUB_ID);
9181 MultiTouchInputMapper& mapper2 = device2->constructAndAddMapper<
9182 MultiTouchInputMapper>(SECOND_EVENTHUB_ID, mFakePolicy->getReaderConfiguration());
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07009183 std::list<NotifyArgs> unused =
9184 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00009185 /*changes=*/{});
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07009186 unused += device2->reset(ARBITRARY_TIME);
Arthur Hung7c645402019-01-25 17:45:42 +08009187
9188 // Setup PointerController.
Michael Wright17db18e2020-06-26 20:51:44 +01009189 std::shared_ptr<FakePointerController> fakePointerController =
9190 std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009191 mFakePolicy->setPointerController(fakePointerController);
Arthur Hung7c645402019-01-25 17:45:42 +08009192
9193 // Setup policy for associated displays and show touches.
9194 const uint8_t hdmi1 = 0;
9195 const uint8_t hdmi2 = 1;
9196 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
9197 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
9198 mFakePolicy->setShowTouches(true);
9199
9200 // Create displays.
Michael Wrighta9cf4192022-12-01 23:46:39 +00009201 prepareDisplay(ui::ROTATION_0, hdmi1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009202 prepareSecondaryDisplay(ViewportType::EXTERNAL, hdmi2);
Arthur Hung7c645402019-01-25 17:45:42 +08009203
9204 // Default device will reconfigure above, need additional reconfiguration for another device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07009205 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00009206 InputReaderConfiguration::Change::DISPLAY_INFO |
9207 InputReaderConfiguration::Change::SHOW_TOUCHES);
Arthur Hung7c645402019-01-25 17:45:42 +08009208
9209 // Two fingers down at default display.
9210 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
9211 processPosition(mapper, x1, y1);
9212 processId(mapper, 1);
9213 processSlot(mapper, 1);
9214 processPosition(mapper, x2, y2);
9215 processId(mapper, 2);
9216 processSync(mapper);
9217
9218 std::map<int32_t, std::vector<int32_t>>::const_iterator iter =
9219 fakePointerController->getSpots().find(DISPLAY_ID);
9220 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
9221 ASSERT_EQ(size_t(2), iter->second.size());
9222
9223 // Two fingers down at second display.
9224 processPosition(mapper2, x1, y1);
9225 processId(mapper2, 1);
9226 processSlot(mapper2, 1);
9227 processPosition(mapper2, x2, y2);
9228 processId(mapper2, 2);
9229 processSync(mapper2);
9230
9231 iter = fakePointerController->getSpots().find(SECONDARY_DISPLAY_ID);
9232 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
9233 ASSERT_EQ(size_t(2), iter->second.size());
Prabir Pradhan197e0862022-07-01 14:28:00 +00009234
9235 // Disable the show touches configuration and ensure the spots are cleared.
9236 mFakePolicy->setShowTouches(false);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07009237 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00009238 InputReaderConfiguration::Change::SHOW_TOUCHES);
Prabir Pradhan197e0862022-07-01 14:28:00 +00009239
9240 ASSERT_TRUE(fakePointerController->getSpots().empty());
Arthur Hung7c645402019-01-25 17:45:42 +08009241}
9242
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06009243TEST_F(MultiTouchInputMapperTest, VideoFrames_ReceivedByListener) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06009244 prepareAxes(POSITION);
9245 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009246 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +00009247 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06009248
9249 NotifyMotionArgs motionArgs;
9250 // Unrotated video frame
9251 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9252 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009253 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06009254 processPosition(mapper, 100, 200);
9255 processSync(mapper);
9256 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9257 ASSERT_EQ(frames, motionArgs.videoFrames);
9258
9259 // Subsequent touch events should not have any videoframes
9260 // This is implemented separately in FakeEventHub,
9261 // but that should match the behaviour of TouchVideoDevice.
9262 processPosition(mapper, 200, 200);
9263 processSync(mapper);
9264 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9265 ASSERT_EQ(std::vector<TouchVideoFrame>(), motionArgs.videoFrames);
9266}
9267
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009268TEST_F(MultiTouchInputMapperTest, VideoFrames_AreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009269 prepareAxes(POSITION);
9270 addConfigurationProperty("touch.deviceType", "touchScreen");
Arpit Singha8c236b2023-04-25 13:56:05 +00009271 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009272 // Unrotated video frame
9273 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9274 NotifyMotionArgs motionArgs;
9275
9276 // Test all 4 orientations
Michael Wrighta9cf4192022-12-01 23:46:39 +00009277 for (ui::Rotation orientation : ftl::enum_range<ui::Rotation>()) {
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009278 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
9279 clearViewports();
9280 prepareDisplay(orientation);
9281 std::vector<TouchVideoFrame> frames{frame};
9282 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
9283 processPosition(mapper, 100, 200);
9284 processSync(mapper);
9285 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9286 ASSERT_EQ(frames, motionArgs.videoFrames);
9287 }
9288}
9289
9290TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_AreRotated) {
9291 prepareAxes(POSITION);
9292 addConfigurationProperty("touch.deviceType", "touchScreen");
9293 // Since InputReader works in the un-rotated coordinate space, only devices that are not
9294 // orientation-aware are affected by display rotation.
9295 addConfigurationProperty("touch.orientationAware", "0");
Arpit Singha8c236b2023-04-25 13:56:05 +00009296 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009297 // Unrotated video frame
9298 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9299 NotifyMotionArgs motionArgs;
9300
9301 // Test all 4 orientations
Michael Wrighta9cf4192022-12-01 23:46:39 +00009302 for (ui::Rotation orientation : ftl::enum_range<ui::Rotation>()) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009303 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
9304 clearViewports();
9305 prepareDisplay(orientation);
9306 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009307 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009308 processPosition(mapper, 100, 200);
9309 processSync(mapper);
9310 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009311 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
9312 // compared to the display. This is so that when the window transform (which contains the
9313 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
9314 // window's coordinate space.
9315 frames[0].rotate(getInverseRotation(orientation));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009316 ASSERT_EQ(frames, motionArgs.videoFrames);
lilinnan687e58f2022-07-19 16:00:50 +08009317
9318 // Release finger.
9319 processSync(mapper);
9320 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009321 }
9322}
9323
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009324TEST_F(MultiTouchInputMapperTest, VideoFrames_MultipleFramesAreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009325 prepareAxes(POSITION);
9326 addConfigurationProperty("touch.deviceType", "touchScreen");
Arpit Singha8c236b2023-04-25 13:56:05 +00009327 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009328 // Unrotated video frames. There's no rule that they must all have the same dimensions,
9329 // so mix these.
9330 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9331 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
9332 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
9333 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
9334 NotifyMotionArgs motionArgs;
9335
Michael Wrighta9cf4192022-12-01 23:46:39 +00009336 prepareDisplay(ui::ROTATION_90);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009337 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009338 processPosition(mapper, 100, 200);
9339 processSync(mapper);
9340 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009341 ASSERT_EQ(frames, motionArgs.videoFrames);
9342}
9343
9344TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_MultipleFramesAreRotated) {
9345 prepareAxes(POSITION);
9346 addConfigurationProperty("touch.deviceType", "touchScreen");
9347 // Since InputReader works in the un-rotated coordinate space, only devices that are not
9348 // orientation-aware are affected by display rotation.
9349 addConfigurationProperty("touch.orientationAware", "0");
Arpit Singha8c236b2023-04-25 13:56:05 +00009350 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009351 // Unrotated video frames. There's no rule that they must all have the same dimensions,
9352 // so mix these.
9353 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9354 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
9355 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
9356 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
9357 NotifyMotionArgs motionArgs;
9358
Michael Wrighta9cf4192022-12-01 23:46:39 +00009359 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009360 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
9361 processPosition(mapper, 100, 200);
9362 processSync(mapper);
9363 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9364 std::for_each(frames.begin(), frames.end(), [](TouchVideoFrame& frame) {
9365 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
9366 // compared to the display. This is so that when the window transform (which contains the
9367 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
9368 // window's coordinate space.
Michael Wrighta9cf4192022-12-01 23:46:39 +00009369 frame.rotate(getInverseRotation(ui::ROTATION_90));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009370 });
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009371 ASSERT_EQ(frames, motionArgs.videoFrames);
9372}
9373
Arthur Hung9da14732019-09-02 16:16:58 +08009374/**
9375 * If we had defined port associations, but the viewport is not ready, the touch device would be
9376 * expected to be disabled, and it should be enabled after the viewport has found.
9377 */
9378TEST_F(MultiTouchInputMapperTest, Configure_EnabledForAssociatedDisplay) {
Arthur Hung9da14732019-09-02 16:16:58 +08009379 constexpr uint8_t hdmi2 = 1;
9380 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009381 constexpr ViewportType type = ViewportType::EXTERNAL;
Arthur Hung9da14732019-09-02 16:16:58 +08009382
9383 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi2);
9384
9385 addConfigurationProperty("touch.deviceType", "touchScreen");
9386 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00009387 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung9da14732019-09-02 16:16:58 +08009388
9389 ASSERT_EQ(mDevice->isEnabled(), false);
9390
9391 // Add display on hdmi2, the device should be enabled and can receive touch event.
9392 prepareSecondaryDisplay(type, hdmi2);
9393 ASSERT_EQ(mDevice->isEnabled(), true);
9394
9395 // Send a touch event.
9396 processPosition(mapper, 100, 100);
9397 processSync(mapper);
9398
9399 NotifyMotionArgs args;
9400 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9401 ASSERT_EQ(SECONDARY_DISPLAY_ID, args.displayId);
9402}
9403
Arthur Hung421eb1c2020-01-16 00:09:42 +08009404TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleSingleTouch) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08009405 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009406 prepareDisplay(ui::ROTATION_0);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009407 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +00009408 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08009409
9410 NotifyMotionArgs motionArgs;
9411
9412 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
9413 // finger down
9414 processId(mapper, 1);
9415 processPosition(mapper, x1, y1);
9416 processSync(mapper);
9417 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9418 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009419 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009420
9421 // finger move
9422 processId(mapper, 1);
9423 processPosition(mapper, x2, y2);
9424 processSync(mapper);
9425 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9426 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009427 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009428
9429 // finger up.
9430 processId(mapper, -1);
9431 processSync(mapper);
9432 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9433 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009434 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009435
9436 // new finger down
9437 processId(mapper, 1);
9438 processPosition(mapper, x3, y3);
9439 processSync(mapper);
9440 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9441 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009442 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009443}
9444
9445/**
arthurhungcc7f9802020-04-30 17:55:40 +08009446 * Test single touch should be canceled when received the MT_TOOL_PALM event, and the following
9447 * MOVE and UP events should be ignored.
Arthur Hung421eb1c2020-01-16 00:09:42 +08009448 */
arthurhungcc7f9802020-04-30 17:55:40 +08009449TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_SinglePointer) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08009450 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009451 prepareDisplay(ui::ROTATION_0);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009452 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +00009453 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08009454
9455 NotifyMotionArgs motionArgs;
9456
9457 // default tool type is finger
9458 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
arthurhungcc7f9802020-04-30 17:55:40 +08009459 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009460 processPosition(mapper, x1, y1);
9461 processSync(mapper);
9462 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9463 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009464 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009465
9466 // Tool changed to MT_TOOL_PALM expect sending the cancel event.
9467 processToolType(mapper, MT_TOOL_PALM);
9468 processSync(mapper);
9469 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9470 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9471
9472 // Ignore the following MOVE and UP events if had detect a palm event.
arthurhungcc7f9802020-04-30 17:55:40 +08009473 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009474 processPosition(mapper, x2, y2);
9475 processSync(mapper);
9476 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9477
9478 // finger up.
arthurhungcc7f9802020-04-30 17:55:40 +08009479 processId(mapper, INVALID_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009480 processSync(mapper);
9481 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9482
9483 // new finger down
arthurhungcc7f9802020-04-30 17:55:40 +08009484 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009485 processToolType(mapper, MT_TOOL_FINGER);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009486 processPosition(mapper, x3, y3);
9487 processSync(mapper);
9488 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9489 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009490 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009491}
9492
arthurhungbf89a482020-04-17 17:37:55 +08009493/**
arthurhungcc7f9802020-04-30 17:55:40 +08009494 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
9495 * and the rest active fingers could still be allowed to receive the events
arthurhungbf89a482020-04-17 17:37:55 +08009496 */
arthurhungcc7f9802020-04-30 17:55:40 +08009497TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_TwoPointers) {
arthurhungbf89a482020-04-17 17:37:55 +08009498 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009499 prepareDisplay(ui::ROTATION_0);
arthurhungbf89a482020-04-17 17:37:55 +08009500 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +00009501 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
arthurhungbf89a482020-04-17 17:37:55 +08009502
9503 NotifyMotionArgs motionArgs;
9504
9505 // default tool type is finger
arthurhungcc7f9802020-04-30 17:55:40 +08009506 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
9507 processId(mapper, FIRST_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009508 processPosition(mapper, x1, y1);
9509 processSync(mapper);
9510 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9511 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009512 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungbf89a482020-04-17 17:37:55 +08009513
9514 // Second finger down.
arthurhungcc7f9802020-04-30 17:55:40 +08009515 processSlot(mapper, SECOND_SLOT);
9516 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009517 processPosition(mapper, x2, y2);
arthurhungcc7f9802020-04-30 17:55:40 +08009518 processSync(mapper);
9519 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009520 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009521 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08009522
9523 // If the tool type of the first finger changes to MT_TOOL_PALM,
9524 // we expect to receive ACTION_POINTER_UP with cancel flag.
9525 processSlot(mapper, FIRST_SLOT);
9526 processId(mapper, FIRST_TRACKING_ID);
9527 processToolType(mapper, MT_TOOL_PALM);
9528 processSync(mapper);
9529 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009530 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009531 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9532
9533 // The following MOVE events of second finger should be processed.
9534 processSlot(mapper, SECOND_SLOT);
9535 processId(mapper, SECOND_TRACKING_ID);
9536 processPosition(mapper, x2 + 1, y2 + 1);
9537 processSync(mapper);
9538 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9539 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009540 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +08009541
9542 // First finger up. It used to be in palm mode, and we already generated ACTION_POINTER_UP for
9543 // it. Second finger receive move.
9544 processSlot(mapper, FIRST_SLOT);
9545 processId(mapper, INVALID_TRACKING_ID);
9546 processSync(mapper);
9547 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9548 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009549 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +08009550
9551 // Second finger keeps moving.
9552 processSlot(mapper, SECOND_SLOT);
9553 processId(mapper, SECOND_TRACKING_ID);
9554 processPosition(mapper, x2 + 2, y2 + 2);
9555 processSync(mapper);
9556 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9557 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009558 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +08009559
9560 // Second finger up.
9561 processId(mapper, INVALID_TRACKING_ID);
9562 processSync(mapper);
9563 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9564 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9565 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9566}
9567
9568/**
9569 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event, if only 1 finger
9570 * is active, it should send CANCEL after receiving the MT_TOOL_PALM event.
9571 */
9572TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_ShouldCancelWhenAllTouchIsPalm) {
9573 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009574 prepareDisplay(ui::ROTATION_0);
arthurhungcc7f9802020-04-30 17:55:40 +08009575 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +00009576 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
arthurhungcc7f9802020-04-30 17:55:40 +08009577
9578 NotifyMotionArgs motionArgs;
9579
9580 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
9581 // First finger down.
9582 processId(mapper, FIRST_TRACKING_ID);
9583 processPosition(mapper, x1, y1);
9584 processSync(mapper);
9585 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9586 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009587 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08009588
9589 // Second finger down.
9590 processSlot(mapper, SECOND_SLOT);
9591 processId(mapper, SECOND_TRACKING_ID);
9592 processPosition(mapper, x2, y2);
arthurhungbf89a482020-04-17 17:37:55 +08009593 processSync(mapper);
9594 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009595 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009596 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungbf89a482020-04-17 17:37:55 +08009597
arthurhungcc7f9802020-04-30 17:55:40 +08009598 // If the tool type of the first finger changes to MT_TOOL_PALM,
9599 // we expect to receive ACTION_POINTER_UP with cancel flag.
9600 processSlot(mapper, FIRST_SLOT);
9601 processId(mapper, FIRST_TRACKING_ID);
9602 processToolType(mapper, MT_TOOL_PALM);
9603 processSync(mapper);
9604 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009605 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009606 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9607
9608 // Second finger keeps moving.
9609 processSlot(mapper, SECOND_SLOT);
9610 processId(mapper, SECOND_TRACKING_ID);
9611 processPosition(mapper, x2 + 1, y2 + 1);
9612 processSync(mapper);
9613 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9614 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9615
9616 // second finger becomes palm, receive cancel due to only 1 finger is active.
9617 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009618 processToolType(mapper, MT_TOOL_PALM);
9619 processSync(mapper);
9620 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9621 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9622
arthurhungcc7f9802020-04-30 17:55:40 +08009623 // third finger down.
9624 processSlot(mapper, THIRD_SLOT);
9625 processId(mapper, THIRD_TRACKING_ID);
9626 processToolType(mapper, MT_TOOL_FINGER);
arthurhungbf89a482020-04-17 17:37:55 +08009627 processPosition(mapper, x3, y3);
9628 processSync(mapper);
arthurhungbf89a482020-04-17 17:37:55 +08009629 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9630 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009631 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009632 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +08009633
9634 // third finger move
9635 processId(mapper, THIRD_TRACKING_ID);
9636 processPosition(mapper, x3 + 1, y3 + 1);
9637 processSync(mapper);
9638 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9639 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9640
9641 // first finger up, third finger receive move.
9642 processSlot(mapper, FIRST_SLOT);
9643 processId(mapper, INVALID_TRACKING_ID);
9644 processSync(mapper);
9645 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9646 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009647 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +08009648
9649 // second finger up, third finger receive move.
9650 processSlot(mapper, SECOND_SLOT);
9651 processId(mapper, INVALID_TRACKING_ID);
9652 processSync(mapper);
9653 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9654 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009655 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +08009656
9657 // third finger up.
9658 processSlot(mapper, THIRD_SLOT);
9659 processId(mapper, INVALID_TRACKING_ID);
9660 processSync(mapper);
9661 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9662 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9663 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9664}
9665
9666/**
9667 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
9668 * and the active finger could still be allowed to receive the events
9669 */
9670TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_KeepFirstPointer) {
9671 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009672 prepareDisplay(ui::ROTATION_0);
arthurhungcc7f9802020-04-30 17:55:40 +08009673 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +00009674 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
arthurhungcc7f9802020-04-30 17:55:40 +08009675
9676 NotifyMotionArgs motionArgs;
9677
9678 // default tool type is finger
9679 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
9680 processId(mapper, FIRST_TRACKING_ID);
9681 processPosition(mapper, x1, y1);
9682 processSync(mapper);
9683 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9684 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009685 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08009686
9687 // Second finger down.
9688 processSlot(mapper, SECOND_SLOT);
9689 processId(mapper, SECOND_TRACKING_ID);
9690 processPosition(mapper, x2, y2);
9691 processSync(mapper);
9692 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009693 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009694 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08009695
9696 // If the tool type of the second finger changes to MT_TOOL_PALM,
9697 // we expect to receive ACTION_POINTER_UP with cancel flag.
9698 processId(mapper, SECOND_TRACKING_ID);
9699 processToolType(mapper, MT_TOOL_PALM);
9700 processSync(mapper);
9701 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009702 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009703 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9704
9705 // The following MOVE event should be processed.
9706 processSlot(mapper, FIRST_SLOT);
9707 processId(mapper, FIRST_TRACKING_ID);
9708 processPosition(mapper, x1 + 1, y1 + 1);
9709 processSync(mapper);
9710 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9711 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009712 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +08009713
9714 // second finger up.
9715 processSlot(mapper, SECOND_SLOT);
9716 processId(mapper, INVALID_TRACKING_ID);
9717 processSync(mapper);
9718 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9719 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9720
9721 // first finger keep moving
9722 processSlot(mapper, FIRST_SLOT);
9723 processId(mapper, FIRST_TRACKING_ID);
9724 processPosition(mapper, x1 + 2, y1 + 2);
9725 processSync(mapper);
9726 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9727 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9728
9729 // first finger up.
9730 processId(mapper, INVALID_TRACKING_ID);
9731 processSync(mapper);
9732 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9733 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9734 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
arthurhungbf89a482020-04-17 17:37:55 +08009735}
9736
Arthur Hung9ad18942021-06-19 02:04:46 +00009737/**
9738 * Test multi-touch should sent ACTION_POINTER_UP/ACTION_UP when received the INVALID_TRACKING_ID,
9739 * to prevent the driver side may send unexpected data after set tracking id as INVALID_TRACKING_ID
9740 * cause slot be valid again.
9741 */
9742TEST_F(MultiTouchInputMapperTest, Process_MultiTouch_WithInvalidTrackingId) {
9743 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009744 prepareDisplay(ui::ROTATION_0);
Arthur Hung9ad18942021-06-19 02:04:46 +00009745 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +00009746 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung9ad18942021-06-19 02:04:46 +00009747
9748 NotifyMotionArgs motionArgs;
9749
9750 constexpr int32_t x1 = 100, y1 = 200, x2 = 0, y2 = 0;
9751 // First finger down.
9752 processId(mapper, FIRST_TRACKING_ID);
9753 processPosition(mapper, x1, y1);
9754 processPressure(mapper, RAW_PRESSURE_MAX);
9755 processSync(mapper);
9756 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9757 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009758 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
Arthur Hung9ad18942021-06-19 02:04:46 +00009759
9760 // First finger move.
9761 processId(mapper, FIRST_TRACKING_ID);
9762 processPosition(mapper, x1 + 1, y1 + 1);
9763 processPressure(mapper, RAW_PRESSURE_MAX);
9764 processSync(mapper);
9765 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9766 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009767 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
Arthur Hung9ad18942021-06-19 02:04:46 +00009768
9769 // Second finger down.
9770 processSlot(mapper, SECOND_SLOT);
9771 processId(mapper, SECOND_TRACKING_ID);
9772 processPosition(mapper, x2, y2);
9773 processPressure(mapper, RAW_PRESSURE_MAX);
9774 processSync(mapper);
9775 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009776 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009777 ASSERT_EQ(uint32_t(2), motionArgs.getPointerCount());
Arthur Hung9ad18942021-06-19 02:04:46 +00009778
9779 // second finger up with some unexpected data.
9780 processSlot(mapper, SECOND_SLOT);
9781 processId(mapper, INVALID_TRACKING_ID);
9782 processPosition(mapper, x2, y2);
9783 processSync(mapper);
9784 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009785 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009786 ASSERT_EQ(uint32_t(2), motionArgs.getPointerCount());
Arthur Hung9ad18942021-06-19 02:04:46 +00009787
9788 // first finger up with some unexpected data.
9789 processSlot(mapper, FIRST_SLOT);
9790 processId(mapper, INVALID_TRACKING_ID);
9791 processPosition(mapper, x2, y2);
9792 processPressure(mapper, RAW_PRESSURE_MAX);
9793 processSync(mapper);
9794 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9795 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009796 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
Arthur Hung9ad18942021-06-19 02:04:46 +00009797}
9798
Arpit Singh4bb0bd52023-12-20 14:41:10 +00009799TEST_F(MultiTouchInputMapperTest, Reset_PreservesLastTouchState) {
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009800 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009801 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009802 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +00009803 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009804
9805 // First finger down.
9806 processId(mapper, FIRST_TRACKING_ID);
9807 processPosition(mapper, 100, 200);
9808 processPressure(mapper, RAW_PRESSURE_MAX);
9809 processSync(mapper);
9810 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9811 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
9812
9813 // Second finger down.
9814 processSlot(mapper, SECOND_SLOT);
9815 processId(mapper, SECOND_TRACKING_ID);
9816 processPosition(mapper, 300, 400);
9817 processPressure(mapper, RAW_PRESSURE_MAX);
9818 processSync(mapper);
9819 ASSERT_NO_FATAL_FAILURE(
9820 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(ACTION_POINTER_1_DOWN)));
9821
Arpit Singh4bb0bd52023-12-20 14:41:10 +00009822 // Reset the mapper. When the mapper is reset, we expect the current multi-touch state to be
9823 // preserved. Resetting should cancel the ongoing gesture.
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00009824 resetMapper(mapper, ARBITRARY_TIME);
9825 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9826 WithMotionAction(AMOTION_EVENT_ACTION_CANCEL)));
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009827
Arpit Singh4bb0bd52023-12-20 14:41:10 +00009828 // Send a sync to simulate an empty touch frame where nothing changes. The mapper should use
9829 // the existing touch state to generate a down event.
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009830 processPosition(mapper, 301, 302);
9831 processSync(mapper);
9832 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9833 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithPressure(1.f))));
Arpit Singh4bb0bd52023-12-20 14:41:10 +00009834 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9835 AllOf(WithMotionAction(ACTION_POINTER_1_DOWN), WithPressure(1.f))));
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009836
9837 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9838}
9839
Arpit Singh4bb0bd52023-12-20 14:41:10 +00009840TEST_F(MultiTouchInputMapperTest, Reset_PreservesLastTouchState_NoPointersDown) {
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009841 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009842 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009843 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +00009844 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009845
9846 // First finger touches down and releases.
9847 processId(mapper, FIRST_TRACKING_ID);
9848 processPosition(mapper, 100, 200);
9849 processPressure(mapper, RAW_PRESSURE_MAX);
9850 processSync(mapper);
9851 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9852 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
9853 processId(mapper, INVALID_TRACKING_ID);
9854 processSync(mapper);
9855 ASSERT_NO_FATAL_FAILURE(
9856 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
9857
9858 // Reset the mapper. When the mapper is reset, we expect it to restore the latest
9859 // raw state where no pointers are down.
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00009860 resetMapper(mapper, ARBITRARY_TIME);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009861 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9862
9863 // Send an empty sync frame. Since there are no pointers, no events are generated.
9864 processSync(mapper);
9865 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9866}
9867
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +00009868TEST_F(MultiTouchInputMapperTest, StylusSourceIsAddedDynamicallyFromToolType) {
Prabir Pradhanf9a41282022-10-25 17:15:50 +00009869 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009870 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanf9a41282022-10-25 17:15:50 +00009871 prepareAxes(POSITION | ID | SLOT | PRESSURE | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +00009872 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +00009873 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
Prabir Pradhanf9a41282022-10-25 17:15:50 +00009874
9875 // Even if the device supports reporting the ABS_MT_TOOL_TYPE axis, which could give it the
9876 // ability to report MT_TOOL_PEN, we do not report the device as coming from a stylus source.
9877 // Due to limitations in the evdev protocol, we cannot say for certain that a device is capable
9878 // of reporting stylus events just because it supports ABS_MT_TOOL_TYPE.
9879 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
9880
9881 // However, if the device ever ends up reporting an event with MT_TOOL_PEN, it should be
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +00009882 // reported with the stylus source.
Prabir Pradhanf9a41282022-10-25 17:15:50 +00009883 processId(mapper, FIRST_TRACKING_ID);
9884 processToolType(mapper, MT_TOOL_PEN);
9885 processPosition(mapper, 100, 200);
9886 processPressure(mapper, RAW_PRESSURE_MAX);
9887 processSync(mapper);
9888 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9889 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
9890 WithSource(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009891 WithToolType(ToolType::STYLUS))));
Prabir Pradhanf9a41282022-10-25 17:15:50 +00009892
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +00009893 // Now that we know the device supports styluses, ensure that the device is re-configured with
9894 // the stylus source.
9895 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS, mapper.getSources());
9896 {
9897 const auto& devices = mReader->getInputDevices();
9898 auto deviceInfo =
9899 std::find_if(devices.begin(), devices.end(),
9900 [](const InputDeviceInfo& info) { return info.getId() == DEVICE_ID; });
9901 LOG_ALWAYS_FATAL_IF(deviceInfo == devices.end(), "Cannot find InputDevice");
9902 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS, deviceInfo->getSources());
9903 }
9904
9905 // Ensure the device was not reset to prevent interruptions of any ongoing gestures.
9906 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
9907
Prabir Pradhanf9a41282022-10-25 17:15:50 +00009908 processId(mapper, INVALID_TRACKING_ID);
9909 processSync(mapper);
9910 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9911 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
9912 WithSource(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009913 WithToolType(ToolType::STYLUS))));
Prabir Pradhanf9a41282022-10-25 17:15:50 +00009914}
9915
Seunghwan Choi356026c2023-02-01 14:37:25 +09009916TEST_F(MultiTouchInputMapperTest, Process_WhenConfigEnabled_ShouldShowDirectStylusPointer) {
9917 addConfigurationProperty("touch.deviceType", "touchScreen");
9918 prepareDisplay(ui::ROTATION_0);
9919 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE | PRESSURE);
9920 // Add BTN_TOOL_PEN to statically show stylus support, since using ABS_MT_TOOL_TYPE can only
9921 // indicate stylus presence dynamically.
9922 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
9923 std::shared_ptr<FakePointerController> fakePointerController =
9924 std::make_shared<FakePointerController>();
9925 mFakePolicy->setPointerController(fakePointerController);
9926 mFakePolicy->setStylusPointerIconEnabled(true);
Arpit Singha8c236b2023-04-25 13:56:05 +00009927 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Seunghwan Choi356026c2023-02-01 14:37:25 +09009928
9929 processId(mapper, FIRST_TRACKING_ID);
9930 processPressure(mapper, RAW_PRESSURE_MIN);
9931 processPosition(mapper, 100, 200);
9932 processToolType(mapper, MT_TOOL_PEN);
9933 processSync(mapper);
9934 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9935 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009936 WithToolType(ToolType::STYLUS),
Seunghwan Choi356026c2023-02-01 14:37:25 +09009937 WithPointerCoords(0, toDisplayX(100), toDisplayY(200)))));
9938 ASSERT_TRUE(fakePointerController->isPointerShown());
9939 ASSERT_NO_FATAL_FAILURE(
9940 fakePointerController->assertPosition(toDisplayX(100), toDisplayY(200)));
9941}
9942
9943TEST_F(MultiTouchInputMapperTest, Process_WhenConfigDisabled_ShouldNotShowDirectStylusPointer) {
9944 addConfigurationProperty("touch.deviceType", "touchScreen");
9945 prepareDisplay(ui::ROTATION_0);
9946 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE | PRESSURE);
9947 // Add BTN_TOOL_PEN to statically show stylus support, since using ABS_MT_TOOL_TYPE can only
9948 // indicate stylus presence dynamically.
9949 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
9950 std::shared_ptr<FakePointerController> fakePointerController =
9951 std::make_shared<FakePointerController>();
9952 mFakePolicy->setPointerController(fakePointerController);
9953 mFakePolicy->setStylusPointerIconEnabled(false);
Arpit Singha8c236b2023-04-25 13:56:05 +00009954 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Seunghwan Choi356026c2023-02-01 14:37:25 +09009955
9956 processId(mapper, FIRST_TRACKING_ID);
9957 processPressure(mapper, RAW_PRESSURE_MIN);
9958 processPosition(mapper, 100, 200);
9959 processToolType(mapper, MT_TOOL_PEN);
9960 processSync(mapper);
9961 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9962 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009963 WithToolType(ToolType::STYLUS),
Seunghwan Choi356026c2023-02-01 14:37:25 +09009964 WithPointerCoords(0, toDisplayX(100), toDisplayY(200)))));
9965 ASSERT_FALSE(fakePointerController->isPointerShown());
9966}
9967
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009968// --- MultiTouchInputMapperTest_ExternalDevice ---
9969
9970class MultiTouchInputMapperTest_ExternalDevice : public MultiTouchInputMapperTest {
9971protected:
Chris Yea52ade12020-08-27 16:49:20 -07009972 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009973};
9974
9975/**
9976 * Expect fallback to internal viewport if device is external and external viewport is not present.
9977 */
9978TEST_F(MultiTouchInputMapperTest_ExternalDevice, Viewports_Fallback) {
9979 prepareAxes(POSITION);
9980 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009981 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +00009982 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009983
9984 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
9985
9986 NotifyMotionArgs motionArgs;
9987
9988 // Expect the event to be sent to the internal viewport,
9989 // because an external viewport is not present.
9990 processPosition(mapper, 100, 100);
9991 processSync(mapper);
9992 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9993 ASSERT_EQ(ADISPLAY_ID_DEFAULT, motionArgs.displayId);
9994
9995 // Expect the event to be sent to the external viewport if it is present.
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009996 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009997 processPosition(mapper, 100, 100);
9998 processSync(mapper);
9999 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10000 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
10001}
Arthur Hung4197f6b2020-03-16 15:39:59 +080010002
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010003TEST_F(MultiTouchInputMapperTest, Process_TouchpadCapture) {
10004 // we need a pointer controller for mouse mode of touchpad (start pointer at 0,0)
10005 std::shared_ptr<FakePointerController> fakePointerController =
10006 std::make_shared<FakePointerController>();
10007 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
10008 fakePointerController->setPosition(0, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010009
10010 // prepare device and capture
Michael Wrighta9cf4192022-12-01 23:46:39 +000010011 prepareDisplay(ui::ROTATION_0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010012 prepareAxes(POSITION | ID | SLOT);
10013 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
10014 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
10015 mFakePolicy->setPointerCapture(true);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +000010016 mFakePolicy->setPointerController(fakePointerController);
Arpit Singha8c236b2023-04-25 13:56:05 +000010017 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010018
10019 // captured touchpad should be a touchpad source
10020 NotifyDeviceResetArgs resetArgs;
10021 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
10022 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
10023
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010024 InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
Chris Yef74dc422020-09-02 22:41:50 -070010025
10026 const InputDeviceInfo::MotionRange* relRangeX =
10027 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_X, AINPUT_SOURCE_TOUCHPAD);
10028 ASSERT_NE(relRangeX, nullptr);
10029 ASSERT_EQ(relRangeX->min, -(RAW_X_MAX - RAW_X_MIN));
10030 ASSERT_EQ(relRangeX->max, RAW_X_MAX - RAW_X_MIN);
10031 const InputDeviceInfo::MotionRange* relRangeY =
10032 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_Y, AINPUT_SOURCE_TOUCHPAD);
10033 ASSERT_NE(relRangeY, nullptr);
10034 ASSERT_EQ(relRangeY->min, -(RAW_Y_MAX - RAW_Y_MIN));
10035 ASSERT_EQ(relRangeY->max, RAW_Y_MAX - RAW_Y_MIN);
10036
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010037 // run captured pointer tests - note that this is unscaled, so input listener events should be
10038 // identical to what the hardware sends (accounting for any
10039 // calibration).
10040 // FINGER 0 DOWN
Chris Ye364fdb52020-08-05 15:07:56 -070010041 processSlot(mapper, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010042 processId(mapper, 1);
10043 processPosition(mapper, 100 + RAW_X_MIN, 100 + RAW_Y_MIN);
10044 processKey(mapper, BTN_TOUCH, 1);
10045 processSync(mapper);
10046
10047 // expect coord[0] to contain initial location of touch 0
10048 NotifyMotionArgs args;
10049 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10050 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010051 ASSERT_EQ(1U, args.getPointerCount());
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010052 ASSERT_EQ(0, args.pointerProperties[0].id);
10053 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, args.source);
10054 ASSERT_NO_FATAL_FAILURE(
10055 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
10056
10057 // FINGER 1 DOWN
10058 processSlot(mapper, 1);
10059 processId(mapper, 2);
10060 processPosition(mapper, 560 + RAW_X_MIN, 154 + RAW_Y_MIN);
10061 processSync(mapper);
10062
10063 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
10064 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010065 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010066 ASSERT_EQ(2U, args.getPointerCount());
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010067 ASSERT_EQ(0, args.pointerProperties[0].id);
10068 ASSERT_EQ(1, args.pointerProperties[1].id);
10069 ASSERT_NO_FATAL_FAILURE(
10070 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
10071 ASSERT_NO_FATAL_FAILURE(
10072 assertPointerCoords(args.pointerCoords[1], 560, 154, 1, 0, 0, 0, 0, 0, 0, 0));
10073
10074 // FINGER 1 MOVE
10075 processPosition(mapper, 540 + RAW_X_MIN, 690 + RAW_Y_MIN);
10076 processSync(mapper);
10077
10078 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
10079 // from move
10080 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10081 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
10082 ASSERT_NO_FATAL_FAILURE(
10083 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
10084 ASSERT_NO_FATAL_FAILURE(
10085 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
10086
10087 // FINGER 0 MOVE
10088 processSlot(mapper, 0);
10089 processPosition(mapper, 50 + RAW_X_MIN, 800 + RAW_Y_MIN);
10090 processSync(mapper);
10091
10092 // expect coord[0] to contain new touch 0 location, coord[1] to contain previous location
10093 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10094 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
10095 ASSERT_NO_FATAL_FAILURE(
10096 assertPointerCoords(args.pointerCoords[0], 50, 800, 1, 0, 0, 0, 0, 0, 0, 0));
10097 ASSERT_NO_FATAL_FAILURE(
10098 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
10099
10100 // BUTTON DOWN
10101 processKey(mapper, BTN_LEFT, 1);
10102 processSync(mapper);
10103
10104 // touchinputmapper design sends a move before button press
10105 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10106 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
10107 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10108 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
10109
10110 // BUTTON UP
10111 processKey(mapper, BTN_LEFT, 0);
10112 processSync(mapper);
10113
10114 // touchinputmapper design sends a move after button release
10115 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10116 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
10117 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10118 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
10119
10120 // FINGER 0 UP
10121 processId(mapper, -1);
10122 processSync(mapper);
10123 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10124 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | 0x0000, args.action);
10125
10126 // FINGER 1 MOVE
10127 processSlot(mapper, 1);
10128 processPosition(mapper, 320 + RAW_X_MIN, 900 + RAW_Y_MIN);
10129 processSync(mapper);
10130
10131 // expect coord[0] to contain new location of touch 1, and properties[0].id to contain 1
10132 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10133 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010134 ASSERT_EQ(1U, args.getPointerCount());
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010135 ASSERT_EQ(1, args.pointerProperties[0].id);
10136 ASSERT_NO_FATAL_FAILURE(
10137 assertPointerCoords(args.pointerCoords[0], 320, 900, 1, 0, 0, 0, 0, 0, 0, 0));
10138
10139 // FINGER 1 UP
10140 processId(mapper, -1);
10141 processKey(mapper, BTN_TOUCH, 0);
10142 processSync(mapper);
10143 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10144 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
10145
Josep del Río2d8c79a2023-01-23 19:33:50 +000010146 // non captured touchpad should be a mouse source
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010147 mFakePolicy->setPointerCapture(false);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +000010148 configureDevice(InputReaderConfiguration::Change::POINTER_CAPTURE);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010149 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Josep del Río2d8c79a2023-01-23 19:33:50 +000010150 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010151}
10152
10153TEST_F(MultiTouchInputMapperTest, Process_UnCapturedTouchpadPointer) {
10154 std::shared_ptr<FakePointerController> fakePointerController =
10155 std::make_shared<FakePointerController>();
10156 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
10157 fakePointerController->setPosition(0, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010158
10159 // prepare device and capture
Michael Wrighta9cf4192022-12-01 23:46:39 +000010160 prepareDisplay(ui::ROTATION_0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010161 prepareAxes(POSITION | ID | SLOT);
10162 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
10163 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +000010164 mFakePolicy->setPointerController(fakePointerController);
Arpit Singha8c236b2023-04-25 13:56:05 +000010165 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010166 // run uncaptured pointer tests - pushes out generic events
10167 // FINGER 0 DOWN
10168 processId(mapper, 3);
10169 processPosition(mapper, 100, 100);
10170 processKey(mapper, BTN_TOUCH, 1);
10171 processSync(mapper);
10172
10173 // start at (100,100), cursor should be at (0,0) * scale
10174 NotifyMotionArgs args;
10175 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10176 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
10177 ASSERT_NO_FATAL_FAILURE(
10178 assertPointerCoords(args.pointerCoords[0], 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
10179
10180 // FINGER 0 MOVE
10181 processPosition(mapper, 200, 200);
10182 processSync(mapper);
10183
10184 // compute scaling to help with touch position checking
10185 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
10186 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
10187 float scale =
10188 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
10189
10190 // translate from (100,100) -> (200,200), cursor should have changed to (100,100) * scale)
10191 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10192 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
10193 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 100 * scale, 100 * scale, 0,
10194 0, 0, 0, 0, 0, 0, 0));
LiZhihong758eb562022-11-03 15:28:29 +080010195
10196 // BUTTON DOWN
10197 processKey(mapper, BTN_LEFT, 1);
10198 processSync(mapper);
10199
10200 // touchinputmapper design sends a move before button press
10201 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10202 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
10203 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10204 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
10205
10206 // BUTTON UP
10207 processKey(mapper, BTN_LEFT, 0);
10208 processSync(mapper);
10209
10210 // touchinputmapper design sends a move after button release
10211 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10212 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
10213 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10214 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010215}
10216
10217TEST_F(MultiTouchInputMapperTest, WhenCapturedAndNotCaptured_GetSources) {
10218 std::shared_ptr<FakePointerController> fakePointerController =
10219 std::make_shared<FakePointerController>();
10220
Michael Wrighta9cf4192022-12-01 23:46:39 +000010221 prepareDisplay(ui::ROTATION_0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010222 prepareAxes(POSITION | ID | SLOT);
10223 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +000010224 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010225 mFakePolicy->setPointerCapture(false);
Arpit Singha8c236b2023-04-25 13:56:05 +000010226 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010227
Josep del Río2d8c79a2023-01-23 19:33:50 +000010228 // uncaptured touchpad should be a pointer device
10229 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010230
Josep del Río2d8c79a2023-01-23 19:33:50 +000010231 // captured touchpad should be a touchpad device
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010232 mFakePolicy->setPointerCapture(true);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +000010233 configureDevice(InputReaderConfiguration::Change::POINTER_CAPTURE);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010234 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
10235}
10236
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +000010237// --- BluetoothMultiTouchInputMapperTest ---
10238
10239class BluetoothMultiTouchInputMapperTest : public MultiTouchInputMapperTest {
10240protected:
10241 void SetUp() override {
10242 InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL, BUS_BLUETOOTH);
10243 }
10244};
10245
10246TEST_F(BluetoothMultiTouchInputMapperTest, TimestampSmoothening) {
10247 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010248 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +000010249 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +000010250 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +000010251
10252 nsecs_t kernelEventTime = ARBITRARY_TIME;
10253 nsecs_t expectedEventTime = ARBITRARY_TIME;
10254 // Touch down.
10255 processId(mapper, FIRST_TRACKING_ID);
10256 processPosition(mapper, 100, 200);
10257 processPressure(mapper, RAW_PRESSURE_MAX);
10258 processSync(mapper, ARBITRARY_TIME);
10259 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10260 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithEventTime(ARBITRARY_TIME))));
10261
10262 // Process several events that come in quick succession, according to their timestamps.
10263 for (int i = 0; i < 3; i++) {
10264 constexpr static nsecs_t delta = ms2ns(1);
10265 static_assert(delta < MIN_BLUETOOTH_TIMESTAMP_DELTA);
10266 kernelEventTime += delta;
10267 expectedEventTime += MIN_BLUETOOTH_TIMESTAMP_DELTA;
10268
10269 processPosition(mapper, 101 + i, 201 + i);
10270 processSync(mapper, kernelEventTime);
10271 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10272 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
10273 WithEventTime(expectedEventTime))));
10274 }
10275
10276 // Release the touch.
10277 processId(mapper, INVALID_TRACKING_ID);
10278 processPressure(mapper, RAW_PRESSURE_MIN);
10279 processSync(mapper, ARBITRARY_TIME + ms2ns(50));
10280 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10281 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
10282 WithEventTime(ARBITRARY_TIME + ms2ns(50)))));
10283}
10284
10285// --- MultiTouchPointerModeTest ---
10286
HQ Liue6983c72022-04-19 22:14:56 +000010287class MultiTouchPointerModeTest : public MultiTouchInputMapperTest {
10288protected:
10289 float mPointerMovementScale;
10290 float mPointerXZoomScale;
10291 void preparePointerMode(int xAxisResolution, int yAxisResolution) {
10292 addConfigurationProperty("touch.deviceType", "pointer");
10293 std::shared_ptr<FakePointerController> fakePointerController =
10294 std::make_shared<FakePointerController>();
10295 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
10296 fakePointerController->setPosition(0, 0);
Michael Wrighta9cf4192022-12-01 23:46:39 +000010297 prepareDisplay(ui::ROTATION_0);
HQ Liue6983c72022-04-19 22:14:56 +000010298
10299 prepareAxes(POSITION);
10300 prepareAbsoluteAxisResolution(xAxisResolution, yAxisResolution);
10301 // In order to enable swipe and freeform gesture in pointer mode, pointer capture
10302 // needs to be disabled, and the pointer gesture needs to be enabled.
10303 mFakePolicy->setPointerCapture(false);
10304 mFakePolicy->setPointerGestureEnabled(true);
10305 mFakePolicy->setPointerController(fakePointerController);
10306
10307 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
10308 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
10309 mPointerMovementScale =
10310 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
10311 mPointerXZoomScale =
10312 mFakePolicy->getPointerGestureZoomSpeedRatio() * displayDiagonal / rawDiagonal;
10313 }
10314
10315 void prepareAbsoluteAxisResolution(int xAxisResolution, int yAxisResolution) {
10316 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
10317 /*flat*/ 0,
10318 /*fuzz*/ 0, /*resolution*/ xAxisResolution);
10319 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
10320 /*flat*/ 0,
10321 /*fuzz*/ 0, /*resolution*/ yAxisResolution);
10322 }
10323};
10324
10325/**
10326 * Two fingers down on a pointer mode touch pad. The width
10327 * of the two finger is larger than 1/4 of the touch pack diagnal length. However, it
10328 * is smaller than the fixed min physical length 30mm. Two fingers' distance must
10329 * be greater than the both value to be freeform gesture, so that after two
10330 * fingers start to move downwards, the gesture should be swipe.
10331 */
10332TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthSwipe) {
10333 // The min freeform gesture width is 25units/mm x 30mm = 750
10334 // which is greater than fraction of the diagnal length of the touchpad (349).
10335 // Thus, MaxSwipWidth is 750.
Harry Cutts33476232023-01-30 19:57:29 +000010336 preparePointerMode(/*xResolution=*/25, /*yResolution=*/25);
Arpit Singha8c236b2023-04-25 13:56:05 +000010337 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
HQ Liue6983c72022-04-19 22:14:56 +000010338 NotifyMotionArgs motionArgs;
10339
10340 // Two fingers down at once.
10341 // The two fingers are 450 units apart, expects the current gesture to be PRESS
10342 // Pointer's initial position is used the [0,0] coordinate.
10343 int32_t x1 = 100, y1 = 125, x2 = 550, y2 = 125;
10344
10345 processId(mapper, FIRST_TRACKING_ID);
10346 processPosition(mapper, x1, y1);
10347 processMTSync(mapper);
10348 processId(mapper, SECOND_TRACKING_ID);
10349 processPosition(mapper, x2, y2);
10350 processMTSync(mapper);
10351 processSync(mapper);
10352
10353 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010354 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000010355 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010356 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010357 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010358 ASSERT_NO_FATAL_FAILURE(
10359 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
10360
10361 // It should be recognized as a SWIPE gesture when two fingers start to move down,
10362 // that there should be 1 pointer.
10363 int32_t movingDistance = 200;
10364 y1 += movingDistance;
10365 y2 += movingDistance;
10366
10367 processId(mapper, FIRST_TRACKING_ID);
10368 processPosition(mapper, x1, y1);
10369 processMTSync(mapper);
10370 processId(mapper, SECOND_TRACKING_ID);
10371 processPosition(mapper, x2, y2);
10372 processMTSync(mapper);
10373 processSync(mapper);
10374
10375 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010376 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000010377 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010378 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010379 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010380 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 0,
10381 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10382 0, 0, 0, 0));
10383}
10384
10385/**
10386 * Two fingers down on a pointer mode touch pad. The width of the two finger is larger
10387 * than the minimum freeform gesture width, 30mm. However, it is smaller than 1/4 of
10388 * the touch pack diagnal length. Two fingers' distance must be greater than the both
10389 * value to be freeform gesture, so that after two fingers start to move downwards,
10390 * the gesture should be swipe.
10391 */
10392TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthLowResolutionSwipe) {
10393 // The min freeform gesture width is 5units/mm x 30mm = 150
10394 // which is greater than fraction of the diagnal length of the touchpad (349).
10395 // Thus, MaxSwipWidth is the fraction of the diagnal length, 349.
Harry Cutts33476232023-01-30 19:57:29 +000010396 preparePointerMode(/*xResolution=*/5, /*yResolution=*/5);
Arpit Singha8c236b2023-04-25 13:56:05 +000010397 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
HQ Liue6983c72022-04-19 22:14:56 +000010398 NotifyMotionArgs motionArgs;
10399
10400 // Two fingers down at once.
10401 // The two fingers are 250 units apart, expects the current gesture to be PRESS
10402 // Pointer's initial position is used the [0,0] coordinate.
10403 int32_t x1 = 100, y1 = 125, x2 = 350, y2 = 125;
10404
10405 processId(mapper, FIRST_TRACKING_ID);
10406 processPosition(mapper, x1, y1);
10407 processMTSync(mapper);
10408 processId(mapper, SECOND_TRACKING_ID);
10409 processPosition(mapper, x2, y2);
10410 processMTSync(mapper);
10411 processSync(mapper);
10412
10413 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010414 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000010415 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010416 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010417 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010418 ASSERT_NO_FATAL_FAILURE(
10419 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
10420
10421 // It should be recognized as a SWIPE gesture when two fingers start to move down,
10422 // and there should be 1 pointer.
10423 int32_t movingDistance = 200;
10424 y1 += movingDistance;
10425 y2 += movingDistance;
10426
10427 processId(mapper, FIRST_TRACKING_ID);
10428 processPosition(mapper, x1, y1);
10429 processMTSync(mapper);
10430 processId(mapper, SECOND_TRACKING_ID);
10431 processPosition(mapper, x2, y2);
10432 processMTSync(mapper);
10433 processSync(mapper);
10434
10435 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010436 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000010437 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010438 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010439 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010440 // New coordinate is the scaled relative coordinate from the initial coordinate.
10441 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 0,
10442 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10443 0, 0, 0, 0));
10444}
10445
10446/**
10447 * Touch the touch pad with two fingers with a distance wider than the minimum freeform
10448 * gesture width and 1/4 of the diagnal length of the touchpad. Expect to receive
10449 * freeform gestures after two fingers start to move downwards.
10450 */
10451TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthFreeform) {
Harry Cutts33476232023-01-30 19:57:29 +000010452 preparePointerMode(/*xResolution=*/25, /*yResolution=*/25);
Arpit Singha8c236b2023-04-25 13:56:05 +000010453 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
HQ Liue6983c72022-04-19 22:14:56 +000010454
10455 NotifyMotionArgs motionArgs;
10456
10457 // Two fingers down at once. Wider than the max swipe width.
10458 // The gesture is expected to be PRESS, then transformed to FREEFORM
10459 int32_t x1 = 100, y1 = 125, x2 = 900, y2 = 125;
10460
10461 processId(mapper, FIRST_TRACKING_ID);
10462 processPosition(mapper, x1, y1);
10463 processMTSync(mapper);
10464 processId(mapper, SECOND_TRACKING_ID);
10465 processPosition(mapper, x2, y2);
10466 processMTSync(mapper);
10467 processSync(mapper);
10468
10469 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010470 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000010471 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010472 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010473 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010474 // One pointer for PRESS, and its coordinate is used as the origin for pointer coordinates.
10475 ASSERT_NO_FATAL_FAILURE(
10476 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
10477
10478 int32_t movingDistance = 200;
10479
10480 // Move two fingers down, expect a cancel event because gesture is changing to freeform,
10481 // then two down events for two pointers.
10482 y1 += movingDistance;
10483 y2 += movingDistance;
10484
10485 processId(mapper, FIRST_TRACKING_ID);
10486 processPosition(mapper, x1, y1);
10487 processMTSync(mapper);
10488 processId(mapper, SECOND_TRACKING_ID);
10489 processPosition(mapper, x2, y2);
10490 processMTSync(mapper);
10491 processSync(mapper);
10492
10493 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10494 // The previous PRESS gesture is cancelled, because it is transformed to freeform
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010495 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000010496 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
10497 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010498 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010499 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000010500 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10501 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010502 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010503 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010504 ASSERT_EQ(2U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000010505 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN, motionArgs.action & AMOTION_EVENT_ACTION_MASK);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010506 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010507 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010508 // Two pointers' scaled relative coordinates from their initial centroid.
10509 // Initial y coordinates are 0 as y1 and y2 have the same value.
10510 float cookedX1 = (x1 - x2) / 2 * mPointerXZoomScale;
10511 float cookedX2 = (x2 - x1) / 2 * mPointerXZoomScale;
10512 // When pointers move, the new coordinates equal to the initial coordinates plus
10513 // scaled moving distance.
10514 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], cookedX1,
10515 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10516 0, 0, 0, 0));
10517 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1], cookedX2,
10518 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10519 0, 0, 0, 0));
10520
10521 // Move two fingers down again, expect one MOVE motion event.
10522 y1 += movingDistance;
10523 y2 += movingDistance;
10524
10525 processId(mapper, FIRST_TRACKING_ID);
10526 processPosition(mapper, x1, y1);
10527 processMTSync(mapper);
10528 processId(mapper, SECOND_TRACKING_ID);
10529 processPosition(mapper, x2, y2);
10530 processMTSync(mapper);
10531 processSync(mapper);
10532
10533 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010534 ASSERT_EQ(2U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000010535 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010536 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010537 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010538 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], cookedX1,
10539 movingDistance * 2 * mPointerMovementScale, 1, 0, 0,
10540 0, 0, 0, 0, 0));
10541 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1], cookedX2,
10542 movingDistance * 2 * mPointerMovementScale, 1, 0, 0,
10543 0, 0, 0, 0, 0));
10544}
10545
Harry Cutts39b7ca22022-10-05 15:55:48 +000010546TEST_F(MultiTouchPointerModeTest, TwoFingerSwipeOffsets) {
Harry Cutts33476232023-01-30 19:57:29 +000010547 preparePointerMode(/*xResolution=*/25, /*yResolution=*/25);
Arpit Singha8c236b2023-04-25 13:56:05 +000010548 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Harry Cutts39b7ca22022-10-05 15:55:48 +000010549 NotifyMotionArgs motionArgs;
10550
10551 // Place two fingers down.
10552 int32_t x1 = 100, y1 = 125, x2 = 550, y2 = 125;
10553
10554 processId(mapper, FIRST_TRACKING_ID);
10555 processPosition(mapper, x1, y1);
10556 processMTSync(mapper);
10557 processId(mapper, SECOND_TRACKING_ID);
10558 processPosition(mapper, x2, y2);
10559 processMTSync(mapper);
10560 processSync(mapper);
10561
10562 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010563 ASSERT_EQ(1U, motionArgs.getPointerCount());
Harry Cutts39b7ca22022-10-05 15:55:48 +000010564 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10565 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
10566 ASSERT_EQ(0, motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_X_OFFSET));
10567 ASSERT_EQ(0, motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_Y_OFFSET));
10568
10569 // Move the two fingers down and to the left.
10570 int32_t movingDistance = 200;
10571 x1 -= movingDistance;
10572 y1 += movingDistance;
10573 x2 -= movingDistance;
10574 y2 += movingDistance;
10575
10576 processId(mapper, FIRST_TRACKING_ID);
10577 processPosition(mapper, x1, y1);
10578 processMTSync(mapper);
10579 processId(mapper, SECOND_TRACKING_ID);
10580 processPosition(mapper, x2, y2);
10581 processMTSync(mapper);
10582 processSync(mapper);
10583
10584 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010585 ASSERT_EQ(1U, motionArgs.getPointerCount());
Harry Cutts39b7ca22022-10-05 15:55:48 +000010586 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10587 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
10588 ASSERT_LT(motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_X_OFFSET), 0);
10589 ASSERT_GT(motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_Y_OFFSET), 0);
10590}
10591
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010592TEST_F(MultiTouchPointerModeTest, WhenViewportActiveStatusChanged_PointerGestureIsReset) {
Harry Cutts33476232023-01-30 19:57:29 +000010593 preparePointerMode(/*xResolution=*/25, /*yResolution=*/25);
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010594 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
Arpit Singha8c236b2023-04-25 13:56:05 +000010595 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010596 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
10597
10598 // Start a stylus gesture.
10599 processKey(mapper, BTN_TOOL_PEN, 1);
10600 processId(mapper, FIRST_TRACKING_ID);
10601 processPosition(mapper, 100, 200);
10602 processSync(mapper);
10603 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10604 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
10605 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010606 WithToolType(ToolType::STYLUS))));
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010607 // TODO(b/257078296): Pointer mode generates extra event.
10608 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10609 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
10610 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010611 WithToolType(ToolType::STYLUS))));
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010612 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10613
10614 // Make the viewport inactive. This will put the device in disabled mode, and the ongoing stylus
10615 // gesture should be disabled.
10616 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
10617 viewport->isActive = false;
10618 mFakePolicy->updateViewport(*viewport);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +000010619 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010620 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10621 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL),
10622 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010623 WithToolType(ToolType::STYLUS))));
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010624 // TODO(b/257078296): Pointer mode generates extra event.
10625 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10626 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL),
10627 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010628 WithToolType(ToolType::STYLUS))));
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010629 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10630}
10631
Arthur Hung6d5b4b22022-01-21 07:21:10 +000010632// --- JoystickInputMapperTest ---
10633
10634class JoystickInputMapperTest : public InputMapperTest {
10635protected:
10636 static const int32_t RAW_X_MIN;
10637 static const int32_t RAW_X_MAX;
10638 static const int32_t RAW_Y_MIN;
10639 static const int32_t RAW_Y_MAX;
10640
10641 void SetUp() override {
10642 InputMapperTest::SetUp(InputDeviceClass::JOYSTICK | InputDeviceClass::EXTERNAL);
10643 }
10644 void prepareAxes() {
10645 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
10646 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
10647 }
10648
10649 void processAxis(JoystickInputMapper& mapper, int32_t axis, int32_t value) {
10650 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, axis, value);
10651 }
10652
10653 void processSync(JoystickInputMapper& mapper) {
10654 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
10655 }
10656
Michael Wrighta9cf4192022-12-01 23:46:39 +000010657 void prepareVirtualDisplay(ui::Rotation orientation) {
Arthur Hung6d5b4b22022-01-21 07:21:10 +000010658 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH,
10659 VIRTUAL_DISPLAY_HEIGHT, orientation, VIRTUAL_DISPLAY_UNIQUE_ID,
10660 NO_PORT, ViewportType::VIRTUAL);
10661 }
10662};
10663
10664const int32_t JoystickInputMapperTest::RAW_X_MIN = -32767;
10665const int32_t JoystickInputMapperTest::RAW_X_MAX = 32767;
10666const int32_t JoystickInputMapperTest::RAW_Y_MIN = -32767;
10667const int32_t JoystickInputMapperTest::RAW_Y_MAX = 32767;
10668
10669TEST_F(JoystickInputMapperTest, Configure_AssignsDisplayUniqueId) {
10670 prepareAxes();
Arpit Singhae876352023-04-26 14:16:50 +000010671 JoystickInputMapper& mapper = constructAndAddMapper<JoystickInputMapper>();
Arthur Hung6d5b4b22022-01-21 07:21:10 +000010672
10673 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
10674
Michael Wrighta9cf4192022-12-01 23:46:39 +000010675 prepareVirtualDisplay(ui::ROTATION_0);
Arthur Hung6d5b4b22022-01-21 07:21:10 +000010676
10677 // Send an axis event
10678 processAxis(mapper, ABS_X, 100);
10679 processSync(mapper);
10680
10681 NotifyMotionArgs args;
10682 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10683 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
10684
10685 // Send another axis event
10686 processAxis(mapper, ABS_Y, 100);
10687 processSync(mapper);
10688
10689 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10690 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
10691}
10692
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010693// --- PeripheralControllerTest ---
Chris Yee2b1e5c2021-03-10 22:45:12 -080010694
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010695class PeripheralControllerTest : public testing::Test {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010696protected:
10697 static const char* DEVICE_NAME;
10698 static const char* DEVICE_LOCATION;
10699 static const int32_t DEVICE_ID;
10700 static const int32_t DEVICE_GENERATION;
10701 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010702 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Chris Yee2b1e5c2021-03-10 22:45:12 -080010703 static const int32_t EVENTHUB_ID;
10704
10705 std::shared_ptr<FakeEventHub> mFakeEventHub;
10706 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010707 std::unique_ptr<TestInputListener> mFakeListener;
Chris Yee2b1e5c2021-03-10 22:45:12 -080010708 std::unique_ptr<InstrumentedInputReader> mReader;
10709 std::shared_ptr<InputDevice> mDevice;
10710
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010711 virtual void SetUp(ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010712 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -070010713 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010714 mFakeListener = std::make_unique<TestInputListener>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010715 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010716 *mFakeListener);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010717 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
10718 }
10719
10720 void SetUp() override { SetUp(DEVICE_CLASSES); }
10721
10722 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010723 mFakeListener.reset();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010724 mFakePolicy.clear();
10725 }
10726
Chris Yee2b1e5c2021-03-10 22:45:12 -080010727 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
10728 const std::string& location, int32_t eventHubId,
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010729 ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010730 InputDeviceIdentifier identifier;
10731 identifier.name = name;
10732 identifier.location = location;
10733 std::shared_ptr<InputDevice> device =
10734 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
10735 identifier);
10736 mReader->pushNextDevice(device);
10737 mFakeEventHub->addDevice(eventHubId, name, classes);
10738 mReader->loopOnce();
10739 return device;
10740 }
10741
10742 template <class T, typename... Args>
10743 T& addControllerAndConfigure(Args... args) {
10744 T& controller = mDevice->addController<T>(EVENTHUB_ID, args...);
10745
10746 return controller;
10747 }
10748};
10749
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010750const char* PeripheralControllerTest::DEVICE_NAME = "device";
10751const char* PeripheralControllerTest::DEVICE_LOCATION = "BLUETOOTH";
10752const int32_t PeripheralControllerTest::DEVICE_ID = END_RESERVED_ID + 1000;
10753const int32_t PeripheralControllerTest::DEVICE_GENERATION = 2;
10754const int32_t PeripheralControllerTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010755const ftl::Flags<InputDeviceClass> PeripheralControllerTest::DEVICE_CLASSES =
10756 ftl::Flags<InputDeviceClass>(0); // not needed for current tests
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010757const int32_t PeripheralControllerTest::EVENTHUB_ID = 1;
Chris Yee2b1e5c2021-03-10 22:45:12 -080010758
10759// --- BatteryControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010760class BatteryControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010761protected:
10762 void SetUp() override {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010763 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::BATTERY);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010764 }
10765};
10766
10767TEST_F(BatteryControllerTest, GetBatteryCapacity) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010768 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010769
Harry Cuttsa5b71292022-11-28 12:56:17 +000010770 ASSERT_TRUE(controller.getBatteryCapacity(FakeEventHub::DEFAULT_BATTERY));
10771 ASSERT_EQ(controller.getBatteryCapacity(FakeEventHub::DEFAULT_BATTERY).value_or(-1),
10772 FakeEventHub::BATTERY_CAPACITY);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010773}
10774
10775TEST_F(BatteryControllerTest, GetBatteryStatus) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010776 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010777
Harry Cuttsa5b71292022-11-28 12:56:17 +000010778 ASSERT_TRUE(controller.getBatteryStatus(FakeEventHub::DEFAULT_BATTERY));
10779 ASSERT_EQ(controller.getBatteryStatus(FakeEventHub::DEFAULT_BATTERY).value_or(-1),
10780 FakeEventHub::BATTERY_STATUS);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010781}
10782
10783// --- LightControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010784class LightControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010785protected:
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010786 void SetUp() override {
10787 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::LIGHT);
10788 }
Chris Yee2b1e5c2021-03-10 22:45:12 -080010789};
10790
Chris Ye85758332021-05-16 23:05:17 -070010791TEST_F(LightControllerTest, MonoLight) {
10792 RawLightInfo infoMono = {.id = 1,
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010793 .name = "mono_light",
Chris Ye85758332021-05-16 23:05:17 -070010794 .maxBrightness = 255,
10795 .flags = InputLightClass::BRIGHTNESS,
10796 .path = ""};
10797 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010798
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010799 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010800 InputDeviceInfo info;
10801 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010802 std::vector<InputDeviceLightInfo> lights = info.getLights();
10803 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010804 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
10805 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10806
10807 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
10808 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
10809}
10810
10811TEST_F(LightControllerTest, MonoKeyboardBacklight) {
10812 RawLightInfo infoMono = {.id = 1,
10813 .name = "mono_keyboard_backlight",
10814 .maxBrightness = 255,
10815 .flags = InputLightClass::BRIGHTNESS |
10816 InputLightClass::KEYBOARD_BACKLIGHT,
10817 .path = ""};
10818 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
10819
10820 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10821 InputDeviceInfo info;
10822 controller.populateDeviceInfo(&info);
10823 std::vector<InputDeviceLightInfo> lights = info.getLights();
10824 ASSERT_EQ(1U, lights.size());
10825 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
10826 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010827
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010828 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
10829 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010830}
10831
Vaibhav Devmurari16c24192023-05-04 15:20:12 +000010832TEST_F(LightControllerTest, Ignore_MonoLight_WithPreferredBacklightLevels) {
10833 RawLightInfo infoMono = {.id = 1,
10834 .name = "mono_light",
10835 .maxBrightness = 255,
10836 .flags = InputLightClass::BRIGHTNESS,
10837 .path = ""};
10838 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
10839 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "keyboard.backlight.brightnessLevels",
10840 "0,100,200");
10841
10842 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10843 std::list<NotifyArgs> unused =
10844 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
10845 /*changes=*/{});
10846
10847 InputDeviceInfo info;
10848 controller.populateDeviceInfo(&info);
10849 std::vector<InputDeviceLightInfo> lights = info.getLights();
10850 ASSERT_EQ(1U, lights.size());
10851 ASSERT_EQ(0U, lights[0].preferredBrightnessLevels.size());
10852}
10853
10854TEST_F(LightControllerTest, KeyboardBacklight_WithNoPreferredBacklightLevels) {
10855 RawLightInfo infoMono = {.id = 1,
10856 .name = "mono_keyboard_backlight",
10857 .maxBrightness = 255,
10858 .flags = InputLightClass::BRIGHTNESS |
10859 InputLightClass::KEYBOARD_BACKLIGHT,
10860 .path = ""};
10861 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
10862
10863 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10864 std::list<NotifyArgs> unused =
10865 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
10866 /*changes=*/{});
10867
10868 InputDeviceInfo info;
10869 controller.populateDeviceInfo(&info);
10870 std::vector<InputDeviceLightInfo> lights = info.getLights();
10871 ASSERT_EQ(1U, lights.size());
10872 ASSERT_EQ(0U, lights[0].preferredBrightnessLevels.size());
10873}
10874
10875TEST_F(LightControllerTest, KeyboardBacklight_WithPreferredBacklightLevels) {
10876 RawLightInfo infoMono = {.id = 1,
10877 .name = "mono_keyboard_backlight",
10878 .maxBrightness = 255,
10879 .flags = InputLightClass::BRIGHTNESS |
10880 InputLightClass::KEYBOARD_BACKLIGHT,
10881 .path = ""};
10882 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
10883 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "keyboard.backlight.brightnessLevels",
10884 "0,100,200");
10885
10886 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10887 std::list<NotifyArgs> unused =
10888 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
10889 /*changes=*/{});
10890
10891 InputDeviceInfo info;
10892 controller.populateDeviceInfo(&info);
10893 std::vector<InputDeviceLightInfo> lights = info.getLights();
10894 ASSERT_EQ(1U, lights.size());
10895 ASSERT_EQ(3U, lights[0].preferredBrightnessLevels.size());
10896 std::set<BrightnessLevel>::iterator it = lights[0].preferredBrightnessLevels.begin();
10897 ASSERT_EQ(BrightnessLevel(0), *it);
10898 std::advance(it, 1);
10899 ASSERT_EQ(BrightnessLevel(100), *it);
10900 std::advance(it, 1);
10901 ASSERT_EQ(BrightnessLevel(200), *it);
10902}
10903
10904TEST_F(LightControllerTest, KeyboardBacklight_WithWrongPreferredBacklightLevels) {
10905 RawLightInfo infoMono = {.id = 1,
10906 .name = "mono_keyboard_backlight",
10907 .maxBrightness = 255,
10908 .flags = InputLightClass::BRIGHTNESS |
10909 InputLightClass::KEYBOARD_BACKLIGHT,
10910 .path = ""};
10911 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
10912 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "keyboard.backlight.brightnessLevels",
10913 "0,100,200,300,400,500");
10914
10915 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10916 std::list<NotifyArgs> unused =
10917 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
10918 /*changes=*/{});
10919
10920 InputDeviceInfo info;
10921 controller.populateDeviceInfo(&info);
10922 std::vector<InputDeviceLightInfo> lights = info.getLights();
10923 ASSERT_EQ(1U, lights.size());
10924 ASSERT_EQ(0U, lights[0].preferredBrightnessLevels.size());
10925}
10926
Chris Yee2b1e5c2021-03-10 22:45:12 -080010927TEST_F(LightControllerTest, RGBLight) {
10928 RawLightInfo infoRed = {.id = 1,
10929 .name = "red",
10930 .maxBrightness = 255,
10931 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
10932 .path = ""};
10933 RawLightInfo infoGreen = {.id = 2,
10934 .name = "green",
10935 .maxBrightness = 255,
10936 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
10937 .path = ""};
10938 RawLightInfo infoBlue = {.id = 3,
10939 .name = "blue",
10940 .maxBrightness = 255,
10941 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
10942 .path = ""};
10943 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
10944 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
10945 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
10946
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010947 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010948 InputDeviceInfo info;
10949 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010950 std::vector<InputDeviceLightInfo> lights = info.getLights();
10951 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010952 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
10953 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10954 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
10955
10956 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10957 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
10958}
10959
10960TEST_F(LightControllerTest, CorrectRGBKeyboardBacklight) {
10961 RawLightInfo infoRed = {.id = 1,
10962 .name = "red_keyboard_backlight",
10963 .maxBrightness = 255,
10964 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED |
10965 InputLightClass::KEYBOARD_BACKLIGHT,
10966 .path = ""};
10967 RawLightInfo infoGreen = {.id = 2,
10968 .name = "green_keyboard_backlight",
10969 .maxBrightness = 255,
10970 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN |
10971 InputLightClass::KEYBOARD_BACKLIGHT,
10972 .path = ""};
10973 RawLightInfo infoBlue = {.id = 3,
10974 .name = "blue_keyboard_backlight",
10975 .maxBrightness = 255,
10976 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE |
10977 InputLightClass::KEYBOARD_BACKLIGHT,
10978 .path = ""};
10979 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
10980 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
10981 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
10982
10983 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10984 InputDeviceInfo info;
10985 controller.populateDeviceInfo(&info);
10986 std::vector<InputDeviceLightInfo> lights = info.getLights();
10987 ASSERT_EQ(1U, lights.size());
10988 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
10989 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10990 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
10991
10992 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10993 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
10994}
10995
10996TEST_F(LightControllerTest, IncorrectRGBKeyboardBacklight) {
10997 RawLightInfo infoRed = {.id = 1,
10998 .name = "red",
10999 .maxBrightness = 255,
11000 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
11001 .path = ""};
11002 RawLightInfo infoGreen = {.id = 2,
11003 .name = "green",
11004 .maxBrightness = 255,
11005 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
11006 .path = ""};
11007 RawLightInfo infoBlue = {.id = 3,
11008 .name = "blue",
11009 .maxBrightness = 255,
11010 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
11011 .path = ""};
11012 RawLightInfo infoGlobal = {.id = 3,
11013 .name = "global_keyboard_backlight",
11014 .maxBrightness = 255,
11015 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GLOBAL |
11016 InputLightClass::KEYBOARD_BACKLIGHT,
11017 .path = ""};
11018 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
11019 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
11020 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
11021 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoGlobal));
11022
11023 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11024 InputDeviceInfo info;
11025 controller.populateDeviceInfo(&info);
11026 std::vector<InputDeviceLightInfo> lights = info.getLights();
11027 ASSERT_EQ(1U, lights.size());
11028 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
11029 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11030 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080011031
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011032 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11033 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011034}
11035
11036TEST_F(LightControllerTest, MultiColorRGBLight) {
11037 RawLightInfo infoColor = {.id = 1,
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011038 .name = "multi_color",
Chris Yee2b1e5c2021-03-10 22:45:12 -080011039 .maxBrightness = 255,
11040 .flags = InputLightClass::BRIGHTNESS |
11041 InputLightClass::MULTI_INTENSITY |
11042 InputLightClass::MULTI_INDEX,
11043 .path = ""};
11044
11045 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
11046
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011047 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011048 InputDeviceInfo info;
11049 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011050 std::vector<InputDeviceLightInfo> lights = info.getLights();
11051 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011052 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
11053 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11054 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
11055
11056 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11057 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
11058}
11059
11060TEST_F(LightControllerTest, MultiColorRGBKeyboardBacklight) {
11061 RawLightInfo infoColor = {.id = 1,
11062 .name = "multi_color_keyboard_backlight",
11063 .maxBrightness = 255,
11064 .flags = InputLightClass::BRIGHTNESS |
11065 InputLightClass::MULTI_INTENSITY |
11066 InputLightClass::MULTI_INDEX |
11067 InputLightClass::KEYBOARD_BACKLIGHT,
11068 .path = ""};
11069
11070 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
11071
11072 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11073 InputDeviceInfo info;
11074 controller.populateDeviceInfo(&info);
11075 std::vector<InputDeviceLightInfo> lights = info.getLights();
11076 ASSERT_EQ(1U, lights.size());
11077 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
11078 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11079 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080011080
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011081 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11082 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011083}
11084
11085TEST_F(LightControllerTest, PlayerIdLight) {
11086 RawLightInfo info1 = {.id = 1,
11087 .name = "player1",
11088 .maxBrightness = 255,
11089 .flags = InputLightClass::BRIGHTNESS,
11090 .path = ""};
11091 RawLightInfo info2 = {.id = 2,
11092 .name = "player2",
11093 .maxBrightness = 255,
11094 .flags = InputLightClass::BRIGHTNESS,
11095 .path = ""};
11096 RawLightInfo info3 = {.id = 3,
11097 .name = "player3",
11098 .maxBrightness = 255,
11099 .flags = InputLightClass::BRIGHTNESS,
11100 .path = ""};
11101 RawLightInfo info4 = {.id = 4,
11102 .name = "player4",
11103 .maxBrightness = 255,
11104 .flags = InputLightClass::BRIGHTNESS,
11105 .path = ""};
11106 mFakeEventHub->addRawLightInfo(info1.id, std::move(info1));
11107 mFakeEventHub->addRawLightInfo(info2.id, std::move(info2));
11108 mFakeEventHub->addRawLightInfo(info3.id, std::move(info3));
11109 mFakeEventHub->addRawLightInfo(info4.id, std::move(info4));
11110
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011111 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011112 InputDeviceInfo info;
11113 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011114 std::vector<InputDeviceLightInfo> lights = info.getLights();
11115 ASSERT_EQ(1U, lights.size());
11116 ASSERT_EQ(InputDeviceLightType::PLAYER_ID, lights[0].type);
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011117 ASSERT_FALSE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11118 ASSERT_FALSE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080011119
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011120 ASSERT_FALSE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11121 ASSERT_TRUE(controller.setLightPlayerId(lights[0].id, LIGHT_PLAYER_ID));
11122 ASSERT_EQ(controller.getLightPlayerId(lights[0].id).value_or(-1), LIGHT_PLAYER_ID);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011123}
11124
Michael Wrightd02c5b62014-02-10 15:10:22 -080011125} // namespace android