blob: c1dc7ffcfb7958c9456d071926bb2e93f15ebefe [file] [log] [blame]
Michael Wrightd02c5b62014-02-10 15:10:22 -08001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Dominik Laskowski2f01d772022-03-23 16:01:29 -070017#include <cinttypes>
18#include <memory>
Harry Cuttsf13161a2023-03-08 14:15:49 +000019#include <optional>
Dominik Laskowski2f01d772022-03-23 16:01:29 -070020
Prabir Pradhan2770d242019-09-02 18:07:11 -070021#include <CursorInputMapper.h>
22#include <InputDevice.h>
23#include <InputMapper.h>
24#include <InputReader.h>
Prabir Pradhan1aed8582019-12-30 11:46:51 -080025#include <InputReaderBase.h>
26#include <InputReaderFactory.h>
Arthur Hung6d5b4b22022-01-21 07:21:10 +000027#include <JoystickInputMapper.h>
Prabir Pradhan2770d242019-09-02 18:07:11 -070028#include <KeyboardInputMapper.h>
29#include <MultiTouchInputMapper.h>
Chris Ye1dd2e5c2021-04-04 23:12:41 -070030#include <PeripheralController.h>
Chris Yef59a2f42020-10-16 12:55:26 -070031#include <SensorInputMapper.h>
Prabir Pradhan2770d242019-09-02 18:07:11 -070032#include <SingleTouchInputMapper.h>
33#include <SwitchInputMapper.h>
Prabir Pradhane3b28dd2023-10-06 04:19:29 +000034#include <TestEventMatchers.h>
Prabir Pradhan2770d242019-09-02 18:07:11 -070035#include <TestInputListener.h>
36#include <TouchInputMapper.h>
Prabir Pradhan1aed8582019-12-30 11:46:51 -080037#include <UinputDevice.h>
Chris Ye87143712020-11-10 05:05:58 +000038#include <VibratorInputMapper.h>
Prabir Pradhan2574dfa2019-10-16 16:35:07 -070039#include <android-base/thread_annotations.h>
Byoungho Jungda10dd32023-10-06 17:03:45 +090040#include <com_android_input_flags.h>
Michael Wrighta9cf4192022-12-01 23:46:39 +000041#include <ftl/enum.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080042#include <gtest/gtest.h>
chaviw3277faf2021-05-19 16:45:23 -050043#include <gui/constants.h>
Michael Wrighta9cf4192022-12-01 23:46:39 +000044#include <ui/Rotation.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080045
Siarhei Vishniakou21e96e62022-10-27 10:23:37 -070046#include <thread>
Harry Cuttsa5b71292022-11-28 12:56:17 +000047#include "FakeEventHub.h"
Harry Cutts6b5fbc52022-11-28 16:37:43 +000048#include "FakeInputReaderPolicy.h"
Harry Cuttsb57f1702022-11-28 15:34:22 +000049#include "FakePointerController.h"
Harry Cuttse6512e12022-11-28 18:44:01 +000050#include "InputMapperTest.h"
Harry Cutts144ff542022-11-28 17:41:06 +000051#include "InstrumentedInputReader.h"
Harry Cuttsa5b71292022-11-28 12:56:17 +000052#include "TestConstants.h"
Michael Wrightdde67b82020-10-27 16:09:22 +000053#include "input/DisplayViewport.h"
54#include "input/Input.h"
Michael Wright17db18e2020-06-26 20:51:44 +010055
Michael Wrightd02c5b62014-02-10 15:10:22 -080056namespace android {
57
Dominik Laskowski2f01d772022-03-23 16:01:29 -070058using namespace ftl::flag_operators;
Prabir Pradhan739dca42022-09-09 20:12:01 +000059using testing::AllOf;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -070060using std::chrono_literals::operator""ms;
61
Michael Wrightd02c5b62014-02-10 15:10:22 -080062// Arbitrary display properties.
arthurhungcc7f9802020-04-30 17:55:40 +080063static constexpr int32_t DISPLAY_ID = 0;
Prabir Pradhanc13ff082022-09-08 22:03:30 +000064static const std::string DISPLAY_UNIQUE_ID = "local:1";
arthurhungcc7f9802020-04-30 17:55:40 +080065static constexpr int32_t SECONDARY_DISPLAY_ID = DISPLAY_ID + 1;
Prabir Pradhanc13ff082022-09-08 22:03:30 +000066static const std::string SECONDARY_DISPLAY_UNIQUE_ID = "local:2";
arthurhungcc7f9802020-04-30 17:55:40 +080067static constexpr int32_t DISPLAY_WIDTH = 480;
68static constexpr int32_t DISPLAY_HEIGHT = 800;
69static constexpr int32_t VIRTUAL_DISPLAY_ID = 1;
70static constexpr int32_t VIRTUAL_DISPLAY_WIDTH = 400;
71static constexpr int32_t VIRTUAL_DISPLAY_HEIGHT = 500;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -070072static const char* VIRTUAL_DISPLAY_UNIQUE_ID = "virtual:1";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -070073static constexpr std::optional<uint8_t> NO_PORT = std::nullopt; // no physical port is specified
Michael Wrightd02c5b62014-02-10 15:10:22 -080074
arthurhungcc7f9802020-04-30 17:55:40 +080075static constexpr int32_t FIRST_SLOT = 0;
76static constexpr int32_t SECOND_SLOT = 1;
77static constexpr int32_t THIRD_SLOT = 2;
78static constexpr int32_t INVALID_TRACKING_ID = -1;
79static constexpr int32_t FIRST_TRACKING_ID = 0;
80static constexpr int32_t SECOND_TRACKING_ID = 1;
81static constexpr int32_t THIRD_TRACKING_ID = 2;
Chris Ye3fdbfef2021-01-06 18:45:18 -080082static constexpr int32_t LIGHT_BRIGHTNESS = 0x55000000;
83static constexpr int32_t LIGHT_COLOR = 0x7F448866;
84static constexpr int32_t LIGHT_PLAYER_ID = 2;
arthurhungcc7f9802020-04-30 17:55:40 +080085
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080086static constexpr int32_t ACTION_POINTER_0_DOWN =
87 AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
88static constexpr int32_t ACTION_POINTER_0_UP =
89 AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
90static constexpr int32_t ACTION_POINTER_1_DOWN =
91 AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
92static constexpr int32_t ACTION_POINTER_1_UP =
93 AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
94
Prabir Pradhanb08a0e82023-09-14 22:28:32 +000095static constexpr uint32_t STYLUS_FUSION_SOURCE =
96 AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_BLUETOOTH_STYLUS;
97
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +000098// Minimum timestamp separation between subsequent input events from a Bluetooth device.
99static constexpr nsecs_t MIN_BLUETOOTH_TIMESTAMP_DELTA = ms2ns(4);
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +0000100
Byoungho Jungda10dd32023-10-06 17:03:45 +0900101namespace input_flags = com::android::input::flags;
102
Michael Wrightd02c5b62014-02-10 15:10:22 -0800103template<typename T>
104static inline T min(T a, T b) {
105 return a < b ? a : b;
106}
107
108static inline float avg(float x, float y) {
109 return (x + y) / 2;
110}
111
Chris Ye3fdbfef2021-01-06 18:45:18 -0800112// Mapping for light color name and the light color
113const std::unordered_map<std::string, LightColor> LIGHT_COLORS = {{"red", LightColor::RED},
114 {"green", LightColor::GREEN},
115 {"blue", LightColor::BLUE}};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800116
Michael Wrighta9cf4192022-12-01 23:46:39 +0000117static ui::Rotation getInverseRotation(ui::Rotation orientation) {
Prabir Pradhanc14266f2021-05-12 15:56:24 -0700118 switch (orientation) {
Michael Wrighta9cf4192022-12-01 23:46:39 +0000119 case ui::ROTATION_90:
120 return ui::ROTATION_270;
121 case ui::ROTATION_270:
122 return ui::ROTATION_90;
Prabir Pradhanc14266f2021-05-12 15:56:24 -0700123 default:
124 return orientation;
125 }
126}
127
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800128static void assertAxisResolution(MultiTouchInputMapper& mapper, int axis, float resolution) {
129 InputDeviceInfo info;
Harry Cuttsd02ea102023-03-17 18:21:30 +0000130 mapper.populateDeviceInfo(info);
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800131
132 const InputDeviceInfo::MotionRange* motionRange =
133 info.getMotionRange(axis, AINPUT_SOURCE_TOUCHSCREEN);
134 ASSERT_NEAR(motionRange->resolution, resolution, EPSILON);
135}
136
137static void assertAxisNotPresent(MultiTouchInputMapper& mapper, int axis) {
138 InputDeviceInfo info;
Harry Cuttsd02ea102023-03-17 18:21:30 +0000139 mapper.populateDeviceInfo(info);
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800140
141 const InputDeviceInfo::MotionRange* motionRange =
142 info.getMotionRange(axis, AINPUT_SOURCE_TOUCHSCREEN);
143 ASSERT_EQ(nullptr, motionRange);
144}
145
Siarhei Vishniakou21e96e62022-10-27 10:23:37 -0700146[[maybe_unused]] static void dumpReader(InputReader& reader) {
147 std::string dump;
148 reader.dump(dump);
149 std::istringstream iss(dump);
150 for (std::string line; std::getline(iss, line);) {
151 ALOGE("%s", line.c_str());
152 std::this_thread::sleep_for(std::chrono::milliseconds(1));
153 }
154}
155
Michael Wrightd02c5b62014-02-10 15:10:22 -0800156// --- FakeInputMapper ---
157
158class FakeInputMapper : public InputMapper {
159 uint32_t mSources;
160 int32_t mKeyboardType;
161 int32_t mMetaState;
162 KeyedVector<int32_t, int32_t> mKeyCodeStates;
163 KeyedVector<int32_t, int32_t> mScanCodeStates;
164 KeyedVector<int32_t, int32_t> mSwitchStates;
Philip Junker4af3b3d2021-12-14 10:36:55 +0100165 // fake mapping which would normally come from keyCharacterMap
166 std::unordered_map<int32_t, int32_t> mKeyCodeMapping;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800167 std::vector<int32_t> mSupportedKeyCodes;
Yeabkal Wubshite03e8b12023-06-27 16:23:12 -0700168 std::list<NotifyArgs> mProcessResult;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800169
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700170 std::mutex mLock;
171 std::condition_variable mStateChangedCondition;
172 bool mConfigureWasCalled GUARDED_BY(mLock);
173 bool mResetWasCalled GUARDED_BY(mLock);
174 bool mProcessWasCalled GUARDED_BY(mLock);
175 RawEvent mLastEvent GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800176
Arthur Hungc23540e2018-11-29 20:42:11 +0800177 std::optional<DisplayViewport> mViewport;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800178public:
Arpit Singh8e6fb252023-04-06 11:49:17 +0000179 FakeInputMapper(InputDeviceContext& deviceContext, const InputReaderConfiguration& readerConfig,
180 uint32_t sources)
181 : InputMapper(deviceContext, readerConfig),
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800182 mSources(sources),
183 mKeyboardType(AINPUT_KEYBOARD_TYPE_NONE),
Michael Wrightd02c5b62014-02-10 15:10:22 -0800184 mMetaState(0),
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800185 mConfigureWasCalled(false),
186 mResetWasCalled(false),
187 mProcessWasCalled(false) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800188
Chris Yea52ade12020-08-27 16:49:20 -0700189 virtual ~FakeInputMapper() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800190
191 void setKeyboardType(int32_t keyboardType) {
192 mKeyboardType = keyboardType;
193 }
194
195 void setMetaState(int32_t metaState) {
196 mMetaState = metaState;
197 }
198
Yeabkal Wubshite03e8b12023-06-27 16:23:12 -0700199 // Sets the return value for the `process` call.
200 void setProcessResult(std::list<NotifyArgs> notifyArgs) {
201 mProcessResult.clear();
202 for (auto notifyArg : notifyArgs) {
203 mProcessResult.push_back(notifyArg);
204 }
205 }
206
Michael Wrightd02c5b62014-02-10 15:10:22 -0800207 void assertConfigureWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700208 std::unique_lock<std::mutex> lock(mLock);
209 base::ScopedLockAssertion assumeLocked(mLock);
210 const bool configureCalled =
211 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
212 return mConfigureWasCalled;
213 });
214 if (!configureCalled) {
215 FAIL() << "Expected configure() to have been called.";
216 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800217 mConfigureWasCalled = false;
218 }
219
220 void assertResetWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700221 std::unique_lock<std::mutex> lock(mLock);
222 base::ScopedLockAssertion assumeLocked(mLock);
223 const bool resetCalled =
224 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
225 return mResetWasCalled;
226 });
227 if (!resetCalled) {
228 FAIL() << "Expected reset() to have been called.";
229 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800230 mResetWasCalled = false;
231 }
232
Prabir Pradhanf8d9e442023-12-06 22:06:13 +0000233 void assertResetWasNotCalled() {
234 std::scoped_lock lock(mLock);
235 ASSERT_FALSE(mResetWasCalled) << "Expected reset to not have been called.";
236 }
237
Yi Kong9b14ac62018-07-17 13:48:38 -0700238 void assertProcessWasCalled(RawEvent* outLastEvent = nullptr) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700239 std::unique_lock<std::mutex> lock(mLock);
240 base::ScopedLockAssertion assumeLocked(mLock);
241 const bool processCalled =
242 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
243 return mProcessWasCalled;
244 });
245 if (!processCalled) {
246 FAIL() << "Expected process() to have been called.";
247 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800248 if (outLastEvent) {
249 *outLastEvent = mLastEvent;
250 }
251 mProcessWasCalled = false;
252 }
253
Prabir Pradhanf8d9e442023-12-06 22:06:13 +0000254 void assertProcessWasNotCalled() {
255 std::scoped_lock lock(mLock);
256 ASSERT_FALSE(mProcessWasCalled) << "Expected process to not have been called.";
257 }
258
Michael Wrightd02c5b62014-02-10 15:10:22 -0800259 void setKeyCodeState(int32_t keyCode, int32_t state) {
260 mKeyCodeStates.replaceValueFor(keyCode, state);
261 }
262
263 void setScanCodeState(int32_t scanCode, int32_t state) {
264 mScanCodeStates.replaceValueFor(scanCode, state);
265 }
266
267 void setSwitchState(int32_t switchCode, int32_t state) {
268 mSwitchStates.replaceValueFor(switchCode, state);
269 }
270
271 void addSupportedKeyCode(int32_t keyCode) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800272 mSupportedKeyCodes.push_back(keyCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800273 }
274
Philip Junker4af3b3d2021-12-14 10:36:55 +0100275 void addKeyCodeMapping(int32_t fromKeyCode, int32_t toKeyCode) {
276 mKeyCodeMapping.insert_or_assign(fromKeyCode, toKeyCode);
277 }
278
Michael Wrightd02c5b62014-02-10 15:10:22 -0800279private:
Philip Junker4af3b3d2021-12-14 10:36:55 +0100280 uint32_t getSources() const override { return mSources; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800281
Harry Cuttsd02ea102023-03-17 18:21:30 +0000282 void populateDeviceInfo(InputDeviceInfo& deviceInfo) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800283 InputMapper::populateDeviceInfo(deviceInfo);
284
285 if (mKeyboardType != AINPUT_KEYBOARD_TYPE_NONE) {
Harry Cuttsd02ea102023-03-17 18:21:30 +0000286 deviceInfo.setKeyboardType(mKeyboardType);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800287 }
288 }
289
Arpit Singhed6c3de2023-04-05 19:24:37 +0000290 std::list<NotifyArgs> reconfigure(nsecs_t, const InputReaderConfiguration& config,
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000291 ConfigurationChanges changes) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700292 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800293 mConfigureWasCalled = true;
Arthur Hungc23540e2018-11-29 20:42:11 +0800294
295 // Find the associated viewport if exist.
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800296 const std::optional<uint8_t> displayPort = getDeviceContext().getAssociatedDisplayPort();
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000297 if (displayPort && changes.test(InputReaderConfiguration::Change::DISPLAY_INFO)) {
Arpit Singhed6c3de2023-04-05 19:24:37 +0000298 mViewport = config.getDisplayViewportByPort(*displayPort);
Arthur Hungc23540e2018-11-29 20:42:11 +0800299 }
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700300
301 mStateChangedCondition.notify_all();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700302 return {};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800303 }
304
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700305 std::list<NotifyArgs> reset(nsecs_t) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700306 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800307 mResetWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700308 mStateChangedCondition.notify_all();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700309 return {};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800310 }
311
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700312 std::list<NotifyArgs> process(const RawEvent* rawEvent) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700313 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800314 mLastEvent = *rawEvent;
315 mProcessWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700316 mStateChangedCondition.notify_all();
Yeabkal Wubshite03e8b12023-06-27 16:23:12 -0700317 return mProcessResult;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800318 }
319
Chris Yea52ade12020-08-27 16:49:20 -0700320 int32_t getKeyCodeState(uint32_t, int32_t keyCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800321 ssize_t index = mKeyCodeStates.indexOfKey(keyCode);
322 return index >= 0 ? mKeyCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
323 }
324
Philip Junker4af3b3d2021-12-14 10:36:55 +0100325 int32_t getKeyCodeForKeyLocation(int32_t locationKeyCode) const override {
326 auto it = mKeyCodeMapping.find(locationKeyCode);
327 return it != mKeyCodeMapping.end() ? it->second : locationKeyCode;
328 }
329
Chris Yea52ade12020-08-27 16:49:20 -0700330 int32_t getScanCodeState(uint32_t, int32_t scanCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800331 ssize_t index = mScanCodeStates.indexOfKey(scanCode);
332 return index >= 0 ? mScanCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
333 }
334
Chris Yea52ade12020-08-27 16:49:20 -0700335 int32_t getSwitchState(uint32_t, int32_t switchCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800336 ssize_t index = mSwitchStates.indexOfKey(switchCode);
337 return index >= 0 ? mSwitchStates.valueAt(index) : AKEY_STATE_UNKNOWN;
338 }
339
Chris Yea52ade12020-08-27 16:49:20 -0700340 // Return true if the device has non-empty key layout.
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700341 bool markSupportedKeyCodes(uint32_t, const std::vector<int32_t>& keyCodes,
Chris Yea52ade12020-08-27 16:49:20 -0700342 uint8_t* outFlags) override {
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700343 for (size_t i = 0; i < keyCodes.size(); i++) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800344 for (size_t j = 0; j < mSupportedKeyCodes.size(); j++) {
345 if (keyCodes[i] == mSupportedKeyCodes[j]) {
346 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800347 }
348 }
349 }
Chris Yea52ade12020-08-27 16:49:20 -0700350 bool result = mSupportedKeyCodes.size() > 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800351 return result;
352 }
353
354 virtual int32_t getMetaState() {
355 return mMetaState;
356 }
357
358 virtual void fadePointer() {
359 }
Arthur Hungc23540e2018-11-29 20:42:11 +0800360
361 virtual std::optional<int32_t> getAssociatedDisplay() {
362 if (mViewport) {
363 return std::make_optional(mViewport->displayId);
364 }
365 return std::nullopt;
366 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800367};
368
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700369// --- InputReaderPolicyTest ---
370class InputReaderPolicyTest : public testing::Test {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -0700371protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700372 sp<FakeInputReaderPolicy> mFakePolicy;
373
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -0700374 void SetUp() override { mFakePolicy = sp<FakeInputReaderPolicy>::make(); }
Chris Yea52ade12020-08-27 16:49:20 -0700375 void TearDown() override { mFakePolicy.clear(); }
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700376};
377
378/**
379 * Check that empty set of viewports is an acceptable configuration.
380 * Also try to get internal viewport two different ways - by type and by uniqueId.
381 *
382 * There will be confusion if two viewports with empty uniqueId and identical type are present.
383 * Such configuration is not currently allowed.
384 */
385TEST_F(InputReaderPolicyTest, Viewports_GetCleared) {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -0700386 static const std::string uniqueId = "local:0";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700387
388 // We didn't add any viewports yet, so there shouldn't be any.
389 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100390 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700391 ASSERT_FALSE(internalViewport);
392
393 // Add an internal viewport, then clear it
Michael Wrighta9cf4192022-12-01 23:46:39 +0000394 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +0000395 /*isActive=*/true, uniqueId, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700396
397 // Check matching by uniqueId
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700398 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700399 ASSERT_TRUE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100400 ASSERT_EQ(ViewportType::INTERNAL, internalViewport->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700401
402 // Check matching by viewport type
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100403 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700404 ASSERT_TRUE(internalViewport);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700405 ASSERT_EQ(uniqueId, internalViewport->uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700406
407 mFakePolicy->clearViewports();
408 // Make sure nothing is found after clear
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700409 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700410 ASSERT_FALSE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100411 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700412 ASSERT_FALSE(internalViewport);
413}
414
415TEST_F(InputReaderPolicyTest, Viewports_GetByType) {
416 const std::string internalUniqueId = "local:0";
417 const std::string externalUniqueId = "local:1";
418 const std::string virtualUniqueId1 = "virtual:2";
419 const std::string virtualUniqueId2 = "virtual:3";
420 constexpr int32_t virtualDisplayId1 = 2;
421 constexpr int32_t virtualDisplayId2 = 3;
422
423 // Add an internal viewport
Michael Wrighta9cf4192022-12-01 23:46:39 +0000424 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +0000425 /*isActive=*/true, internalUniqueId, NO_PORT,
Michael Wrighta9cf4192022-12-01 23:46:39 +0000426 ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700427 // Add an external viewport
Michael Wrighta9cf4192022-12-01 23:46:39 +0000428 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +0000429 /*isActive=*/true, externalUniqueId, NO_PORT,
Michael Wrighta9cf4192022-12-01 23:46:39 +0000430 ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700431 // Add an virtual viewport
432 mFakePolicy->addDisplayViewport(virtualDisplayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +0000433 ui::ROTATION_0, /*isActive=*/true, virtualUniqueId1, NO_PORT,
Michael Wrighta9cf4192022-12-01 23:46:39 +0000434 ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700435 // Add another virtual viewport
436 mFakePolicy->addDisplayViewport(virtualDisplayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +0000437 ui::ROTATION_0, /*isActive=*/true, virtualUniqueId2, NO_PORT,
Michael Wrighta9cf4192022-12-01 23:46:39 +0000438 ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700439
440 // Check matching by type for internal
441 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100442 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700443 ASSERT_TRUE(internalViewport);
444 ASSERT_EQ(internalUniqueId, internalViewport->uniqueId);
445
446 // Check matching by type for external
447 std::optional<DisplayViewport> externalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100448 mFakePolicy->getDisplayViewportByType(ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700449 ASSERT_TRUE(externalViewport);
450 ASSERT_EQ(externalUniqueId, externalViewport->uniqueId);
451
452 // Check matching by uniqueId for virtual viewport #1
453 std::optional<DisplayViewport> virtualViewport1 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700454 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700455 ASSERT_TRUE(virtualViewport1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100456 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport1->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700457 ASSERT_EQ(virtualUniqueId1, virtualViewport1->uniqueId);
458 ASSERT_EQ(virtualDisplayId1, virtualViewport1->displayId);
459
460 // Check matching by uniqueId for virtual viewport #2
461 std::optional<DisplayViewport> virtualViewport2 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700462 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700463 ASSERT_TRUE(virtualViewport2);
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100464 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport2->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700465 ASSERT_EQ(virtualUniqueId2, virtualViewport2->uniqueId);
466 ASSERT_EQ(virtualDisplayId2, virtualViewport2->displayId);
467}
468
469
470/**
471 * We can have 2 viewports of the same kind. We can distinguish them by uniqueId, and confirm
472 * that lookup works by checking display id.
473 * Check that 2 viewports of each kind is possible, for all existing viewport types.
474 */
475TEST_F(InputReaderPolicyTest, Viewports_TwoOfSameType) {
476 const std::string uniqueId1 = "uniqueId1";
477 const std::string uniqueId2 = "uniqueId2";
478 constexpr int32_t displayId1 = 2;
479 constexpr int32_t displayId2 = 3;
480
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100481 std::vector<ViewportType> types = {ViewportType::INTERNAL, ViewportType::EXTERNAL,
482 ViewportType::VIRTUAL};
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700483 for (const ViewportType& type : types) {
484 mFakePolicy->clearViewports();
485 // Add a viewport
Michael Wrighta9cf4192022-12-01 23:46:39 +0000486 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +0000487 /*isActive=*/true, uniqueId1, NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700488 // Add another viewport
Michael Wrighta9cf4192022-12-01 23:46:39 +0000489 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +0000490 /*isActive=*/true, uniqueId2, NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700491
492 // Check that correct display viewport was returned by comparing the display IDs.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700493 std::optional<DisplayViewport> viewport1 =
494 mFakePolicy->getDisplayViewportByUniqueId(uniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700495 ASSERT_TRUE(viewport1);
496 ASSERT_EQ(displayId1, viewport1->displayId);
497 ASSERT_EQ(type, viewport1->type);
498
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700499 std::optional<DisplayViewport> viewport2 =
500 mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700501 ASSERT_TRUE(viewport2);
502 ASSERT_EQ(displayId2, viewport2->displayId);
503 ASSERT_EQ(type, viewport2->type);
504
505 // When there are multiple viewports of the same kind, and uniqueId is not specified
506 // in the call to getDisplayViewport, then that situation is not supported.
507 // The viewports can be stored in any order, so we cannot rely on the order, since that
508 // is just implementation detail.
509 // However, we can check that it still returns *a* viewport, we just cannot assert
510 // which one specifically is returned.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700511 std::optional<DisplayViewport> someViewport = mFakePolicy->getDisplayViewportByType(type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700512 ASSERT_TRUE(someViewport);
513 }
514}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800515
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700516/**
Michael Wrightdde67b82020-10-27 16:09:22 +0000517 * When we have multiple internal displays make sure we always return the default display when
518 * querying by type.
519 */
520TEST_F(InputReaderPolicyTest, Viewports_ByTypeReturnsDefaultForInternal) {
521 const std::string uniqueId1 = "uniqueId1";
522 const std::string uniqueId2 = "uniqueId2";
523 constexpr int32_t nonDefaultDisplayId = 2;
524 static_assert(nonDefaultDisplayId != ADISPLAY_ID_DEFAULT,
525 "Test display ID should not be ADISPLAY_ID_DEFAULT");
526
527 // Add the default display first and ensure it gets returned.
528 mFakePolicy->clearViewports();
529 mFakePolicy->addDisplayViewport(ADISPLAY_ID_DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +0000530 ui::ROTATION_0, /*isActive=*/true, uniqueId1, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +0000531 ViewportType::INTERNAL);
532 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +0000533 ui::ROTATION_0, /*isActive=*/true, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +0000534 ViewportType::INTERNAL);
535
536 std::optional<DisplayViewport> viewport =
537 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
538 ASSERT_TRUE(viewport);
539 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
540 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
541
542 // Add the default display second to make sure order doesn't matter.
543 mFakePolicy->clearViewports();
544 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +0000545 ui::ROTATION_0, /*isActive=*/true, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +0000546 ViewportType::INTERNAL);
547 mFakePolicy->addDisplayViewport(ADISPLAY_ID_DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +0000548 ui::ROTATION_0, /*isActive=*/true, uniqueId1, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +0000549 ViewportType::INTERNAL);
550
551 viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
552 ASSERT_TRUE(viewport);
553 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
554 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
555}
556
557/**
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700558 * Check getDisplayViewportByPort
559 */
560TEST_F(InputReaderPolicyTest, Viewports_GetByPort) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100561 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700562 const std::string uniqueId1 = "uniqueId1";
563 const std::string uniqueId2 = "uniqueId2";
564 constexpr int32_t displayId1 = 1;
565 constexpr int32_t displayId2 = 2;
566 const uint8_t hdmi1 = 0;
567 const uint8_t hdmi2 = 1;
568 const uint8_t hdmi3 = 2;
569
570 mFakePolicy->clearViewports();
571 // Add a viewport that's associated with some display port that's not of interest.
Michael Wrighta9cf4192022-12-01 23:46:39 +0000572 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +0000573 /*isActive=*/true, uniqueId1, hdmi3, type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700574 // Add another viewport, connected to HDMI1 port
Michael Wrighta9cf4192022-12-01 23:46:39 +0000575 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +0000576 /*isActive=*/true, uniqueId2, hdmi1, type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700577
578 // Check that correct display viewport was returned by comparing the display ports.
579 std::optional<DisplayViewport> hdmi1Viewport = mFakePolicy->getDisplayViewportByPort(hdmi1);
580 ASSERT_TRUE(hdmi1Viewport);
581 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
582 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
583
584 // Check that we can still get the same viewport using the uniqueId
585 hdmi1Viewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
586 ASSERT_TRUE(hdmi1Viewport);
587 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
588 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
589 ASSERT_EQ(type, hdmi1Viewport->type);
590
591 // Check that we cannot find a port with "HDMI2", because we never added one
592 std::optional<DisplayViewport> hdmi2Viewport = mFakePolicy->getDisplayViewportByPort(hdmi2);
593 ASSERT_FALSE(hdmi2Viewport);
594}
595
Michael Wrightd02c5b62014-02-10 15:10:22 -0800596// --- InputReaderTest ---
597
598class InputReaderTest : public testing::Test {
599protected:
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700600 std::unique_ptr<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800601 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700602 std::shared_ptr<FakeEventHub> mFakeEventHub;
Prabir Pradhan28efc192019-11-05 01:10:04 +0000603 std::unique_ptr<InstrumentedInputReader> mReader;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800604
Chris Yea52ade12020-08-27 16:49:20 -0700605 void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700606 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -0700607 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700608 mFakeListener = std::make_unique<TestInputListener>();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800609
Prabir Pradhan28efc192019-11-05 01:10:04 +0000610 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700611 *mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800612 }
613
Chris Yea52ade12020-08-27 16:49:20 -0700614 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700615 mFakeListener.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800616 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800617 }
618
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700619 void addDevice(int32_t eventHubId, const std::string& name,
620 ftl::Flags<InputDeviceClass> classes, const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800621 mFakeEventHub->addDevice(eventHubId, name, classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800622
623 if (configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800624 mFakeEventHub->addConfigurationMap(eventHubId, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800625 }
626 mFakeEventHub->finishDeviceScan();
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000627 mReader->loopOnce();
628 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700629 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
Prabir Pradhane3da4bb2023-04-05 23:51:23 +0000630 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyInputDevicesChangedWasCalled());
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700631 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800632 }
633
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800634 void disableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700635 mFakePolicy->addDisabledDevice(deviceId);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000636 mReader->requestRefreshConfiguration(InputReaderConfiguration::Change::ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700637 }
638
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800639 void enableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700640 mFakePolicy->removeDisabledDevice(deviceId);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000641 mReader->requestRefreshConfiguration(InputReaderConfiguration::Change::ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700642 }
643
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800644 FakeInputMapper& addDeviceWithFakeInputMapper(int32_t deviceId, int32_t eventHubId,
Chris Ye1b0c7342020-07-28 21:57:03 -0700645 const std::string& name,
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700646 ftl::Flags<InputDeviceClass> classes,
647 uint32_t sources,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800648 const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800649 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, name);
Arpit Singh8e6fb252023-04-06 11:49:17 +0000650 FakeInputMapper& mapper =
651 device->addMapper<FakeInputMapper>(eventHubId,
652 mFakePolicy->getReaderConfiguration(), sources);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -0800653 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800654 addDevice(eventHubId, name, classes, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800655 return mapper;
656 }
657};
658
Chris Ye98d3f532020-10-01 21:48:59 -0700659TEST_F(InputReaderTest, PolicyGetInputDevices) {
660 ASSERT_NO_FATAL_FAILURE(addDevice(1, "keyboard", InputDeviceClass::KEYBOARD, nullptr));
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700661 ASSERT_NO_FATAL_FAILURE(addDevice(2, "ignored", ftl::Flags<InputDeviceClass>(0),
Chris Ye98d3f532020-10-01 21:48:59 -0700662 nullptr)); // no classes so device will be ignored
Michael Wrightd02c5b62014-02-10 15:10:22 -0800663
664 // Should also have received a notification describing the new input devices.
Chris Ye98d3f532020-10-01 21:48:59 -0700665 const std::vector<InputDeviceInfo>& inputDevices = mFakePolicy->getInputDevices();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800666 ASSERT_EQ(1U, inputDevices.size());
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800667 ASSERT_EQ(END_RESERVED_ID + 1, inputDevices[0].getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100668 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800669 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
670 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000671 ASSERT_EQ(0U, inputDevices[0].getMotionRanges().size());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800672}
673
Vaibhav Devmurari5fc7d852023-03-17 18:43:33 +0000674TEST_F(InputReaderTest, InputDeviceRecreatedOnSysfsNodeChanged) {
675 ASSERT_NO_FATAL_FAILURE(addDevice(1, "keyboard", InputDeviceClass::KEYBOARD, nullptr));
676 mFakeEventHub->setSysfsRootPath(1, "xyz");
677
678 // Should also have received a notification describing the new input device.
679 ASSERT_EQ(1U, mFakePolicy->getInputDevices().size());
680 InputDeviceInfo inputDevice = mFakePolicy->getInputDevices()[0];
681 ASSERT_EQ(0U, inputDevice.getLights().size());
682
683 RawLightInfo infoMonolight = {.id = 123,
684 .name = "mono_keyboard_backlight",
685 .maxBrightness = 255,
686 .flags = InputLightClass::BRIGHTNESS,
687 .path = ""};
688 mFakeEventHub->addRawLightInfo(/*rawId=*/123, std::move(infoMonolight));
689 mReader->sysfsNodeChanged("xyz");
690 mReader->loopOnce();
691
692 // Should also have received a notification describing the new recreated input device.
693 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
694 inputDevice = mFakePolicy->getInputDevices()[0];
695 ASSERT_EQ(1U, inputDevice.getLights().size());
696}
697
Chris Yee7310032020-09-22 15:36:28 -0700698TEST_F(InputReaderTest, GetMergedInputDevices) {
699 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
700 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
701 // Add two subdevices to device
702 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
703 // Must add at least one mapper or the device will be ignored!
Arpit Singh8e6fb252023-04-06 11:49:17 +0000704 device->addMapper<FakeInputMapper>(eventHubIds[0], mFakePolicy->getReaderConfiguration(),
705 AINPUT_SOURCE_KEYBOARD);
706 device->addMapper<FakeInputMapper>(eventHubIds[1], mFakePolicy->getReaderConfiguration(),
707 AINPUT_SOURCE_KEYBOARD);
Chris Yee7310032020-09-22 15:36:28 -0700708
709 // Push same device instance for next device to be added, so they'll have same identifier.
710 mReader->pushNextDevice(device);
711 mReader->pushNextDevice(device);
712 ASSERT_NO_FATAL_FAILURE(
713 addDevice(eventHubIds[0], "fake1", InputDeviceClass::KEYBOARD, nullptr));
714 ASSERT_NO_FATAL_FAILURE(
715 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
716
717 // Two devices will be merged to one input device as they have same identifier
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000718 ASSERT_EQ(1U, mFakePolicy->getInputDevices().size());
Chris Yee7310032020-09-22 15:36:28 -0700719}
720
Chris Yee14523a2020-12-19 13:46:00 -0800721TEST_F(InputReaderTest, GetMergedInputDevicesEnabled) {
722 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
723 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
724 // Add two subdevices to device
725 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
726 // Must add at least one mapper or the device will be ignored!
Arpit Singh8e6fb252023-04-06 11:49:17 +0000727 device->addMapper<FakeInputMapper>(eventHubIds[0], mFakePolicy->getReaderConfiguration(),
728 AINPUT_SOURCE_KEYBOARD);
729 device->addMapper<FakeInputMapper>(eventHubIds[1], mFakePolicy->getReaderConfiguration(),
730 AINPUT_SOURCE_KEYBOARD);
Chris Yee14523a2020-12-19 13:46:00 -0800731
732 // Push same device instance for next device to be added, so they'll have same identifier.
733 mReader->pushNextDevice(device);
734 mReader->pushNextDevice(device);
735 // Sensor device is initially disabled
736 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1",
737 InputDeviceClass::KEYBOARD | InputDeviceClass::SENSOR,
738 nullptr));
739 // Device is disabled because the only sub device is a sensor device and disabled initially.
740 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
741 ASSERT_FALSE(device->isEnabled());
742 ASSERT_NO_FATAL_FAILURE(
743 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
744 // The merged device is enabled if any sub device is enabled
745 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
746 ASSERT_TRUE(device->isEnabled());
747}
748
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700749TEST_F(InputReaderTest, WhenEnabledChanges_SendsDeviceResetNotification) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800750 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700751 constexpr ftl::Flags<InputDeviceClass> deviceClass(InputDeviceClass::KEYBOARD);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800752 constexpr int32_t eventHubId = 1;
753 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700754 // Must add at least one mapper or the device will be ignored!
Arpit Singh8e6fb252023-04-06 11:49:17 +0000755 device->addMapper<FakeInputMapper>(eventHubId, mFakePolicy->getReaderConfiguration(),
756 AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -0800757 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800758 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700759
Yi Kong9b14ac62018-07-17 13:48:38 -0700760 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700761
762 NotifyDeviceResetArgs resetArgs;
763 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700764 ASSERT_EQ(deviceId, resetArgs.deviceId);
765
766 ASSERT_EQ(device->isEnabled(), true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800767 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000768 mReader->loopOnce();
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700769
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700770 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700771 ASSERT_EQ(deviceId, resetArgs.deviceId);
772 ASSERT_EQ(device->isEnabled(), false);
773
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800774 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000775 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700776 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
777 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasNotCalled());
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700778 ASSERT_EQ(device->isEnabled(), false);
779
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800780 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000781 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700782 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700783 ASSERT_EQ(deviceId, resetArgs.deviceId);
784 ASSERT_EQ(device->isEnabled(), true);
785}
786
Michael Wrightd02c5b62014-02-10 15:10:22 -0800787TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800788 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700789 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800790 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800791 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800792 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800793 AINPUT_SOURCE_KEYBOARD, nullptr);
794 mapper.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800795
796 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(0,
797 AINPUT_SOURCE_ANY, AKEYCODE_A))
798 << "Should return unknown when the device id is >= 0 but unknown.";
799
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800800 ASSERT_EQ(AKEY_STATE_UNKNOWN,
801 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
802 << "Should return unknown when the device id is valid but the sources are not "
803 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800804
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800805 ASSERT_EQ(AKEY_STATE_DOWN,
806 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
807 AKEYCODE_A))
808 << "Should return value provided by mapper when device id is valid and the device "
809 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800810
811 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(-1,
812 AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
813 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
814
815 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getKeyCodeState(-1,
816 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
817 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
818}
819
Philip Junker4af3b3d2021-12-14 10:36:55 +0100820TEST_F(InputReaderTest, GetKeyCodeForKeyLocation_ForwardsRequestsToMappers) {
821 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
822 constexpr int32_t eventHubId = 1;
823 FakeInputMapper& mapper = addDeviceWithFakeInputMapper(deviceId, eventHubId, "keyboard",
824 InputDeviceClass::KEYBOARD,
825 AINPUT_SOURCE_KEYBOARD, nullptr);
826 mapper.addKeyCodeMapping(AKEYCODE_Y, AKEYCODE_Z);
827
828 ASSERT_EQ(AKEYCODE_UNKNOWN, mReader->getKeyCodeForKeyLocation(0, AKEYCODE_Y))
829 << "Should return unknown when the device with the specified id is not found.";
830
831 ASSERT_EQ(AKEYCODE_Z, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_Y))
832 << "Should return correct mapping when device id is valid and mapping exists.";
833
834 ASSERT_EQ(AKEYCODE_A, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_A))
835 << "Should return the location key code when device id is valid and there's no "
836 "mapping.";
837}
838
839TEST_F(InputReaderTest, GetKeyCodeForKeyLocation_NoKeyboardMapper) {
840 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
841 constexpr int32_t eventHubId = 1;
842 FakeInputMapper& mapper = addDeviceWithFakeInputMapper(deviceId, eventHubId, "joystick",
843 InputDeviceClass::JOYSTICK,
844 AINPUT_SOURCE_GAMEPAD, nullptr);
845 mapper.addKeyCodeMapping(AKEYCODE_Y, AKEYCODE_Z);
846
847 ASSERT_EQ(AKEYCODE_UNKNOWN, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_Y))
848 << "Should return unknown when the device id is valid but there is no keyboard mapper";
849}
850
Michael Wrightd02c5b62014-02-10 15:10:22 -0800851TEST_F(InputReaderTest, GetScanCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800852 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700853 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800854 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800855 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800856 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800857 AINPUT_SOURCE_KEYBOARD, nullptr);
858 mapper.setScanCodeState(KEY_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800859
860 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(0,
861 AINPUT_SOURCE_ANY, KEY_A))
862 << "Should return unknown when the device id is >= 0 but unknown.";
863
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800864 ASSERT_EQ(AKEY_STATE_UNKNOWN,
865 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, KEY_A))
866 << "Should return unknown when the device id is valid but the sources are not "
867 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800868
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800869 ASSERT_EQ(AKEY_STATE_DOWN,
870 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
871 KEY_A))
872 << "Should return value provided by mapper when device id is valid and the device "
873 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800874
875 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(-1,
876 AINPUT_SOURCE_TRACKBALL, KEY_A))
877 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
878
879 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getScanCodeState(-1,
880 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, KEY_A))
881 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
882}
883
884TEST_F(InputReaderTest, GetSwitchState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800885 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700886 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800887 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800888 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800889 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800890 AINPUT_SOURCE_KEYBOARD, nullptr);
891 mapper.setSwitchState(SW_LID, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800892
893 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(0,
894 AINPUT_SOURCE_ANY, SW_LID))
895 << "Should return unknown when the device id is >= 0 but unknown.";
896
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800897 ASSERT_EQ(AKEY_STATE_UNKNOWN,
898 mReader->getSwitchState(deviceId, AINPUT_SOURCE_TRACKBALL, SW_LID))
899 << "Should return unknown when the device id is valid but the sources are not "
900 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800901
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800902 ASSERT_EQ(AKEY_STATE_DOWN,
903 mReader->getSwitchState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
904 SW_LID))
905 << "Should return value provided by mapper when device id is valid and the device "
906 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800907
908 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(-1,
909 AINPUT_SOURCE_TRACKBALL, SW_LID))
910 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
911
912 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getSwitchState(-1,
913 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, SW_LID))
914 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
915}
916
917TEST_F(InputReaderTest, MarkSupportedKeyCodes_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800918 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700919 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800920 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800921 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800922 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800923 AINPUT_SOURCE_KEYBOARD, nullptr);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100924
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800925 mapper.addSupportedKeyCode(AKEYCODE_A);
926 mapper.addSupportedKeyCode(AKEYCODE_B);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800927
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700928 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800929 uint8_t flags[4] = { 0, 0, 0, 1 };
930
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700931 ASSERT_FALSE(mReader->hasKeys(0, AINPUT_SOURCE_ANY, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800932 << "Should return false when device id is >= 0 but unknown.";
933 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
934
935 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700936 ASSERT_FALSE(mReader->hasKeys(deviceId, AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800937 << "Should return false when device id is valid but the sources are not supported by "
938 "the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800939 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
940
941 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700942 ASSERT_TRUE(mReader->hasKeys(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800943 keyCodes, flags))
944 << "Should return value provided by mapper when device id is valid and the device "
945 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800946 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
947
948 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700949 ASSERT_FALSE(mReader->hasKeys(-1, AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
950 << "Should return false when the device id is < 0 but the sources are not supported by "
951 "any device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800952 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
953
954 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700955 ASSERT_TRUE(
956 mReader->hasKeys(-1, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
957 << "Should return value provided by mapper when device id is < 0 and one of the "
958 "devices supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800959 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
960}
961
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000962TEST_F(InputReaderTest, LoopOnce_WhenDeviceScanFinished_SendsConfigurationChanged) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800963 constexpr int32_t eventHubId = 1;
Chris Ye1b0c7342020-07-28 21:57:03 -0700964 addDevice(eventHubId, "ignored", InputDeviceClass::KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800965
966 NotifyConfigurationChangedArgs args;
967
968 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(&args));
969 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
970}
971
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000972TEST_F(InputReaderTest, LoopOnce_ForwardsRawEventsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800973 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700974 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000975 constexpr nsecs_t when = 0;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800976 constexpr int32_t eventHubId = 1;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000977 constexpr nsecs_t readTime = 2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800978 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800979 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800980 AINPUT_SOURCE_KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800981
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000982 mFakeEventHub->enqueueEvent(when, readTime, eventHubId, EV_KEY, KEY_A, 1);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000983 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800984 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
985
986 RawEvent event;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800987 ASSERT_NO_FATAL_FAILURE(mapper.assertProcessWasCalled(&event));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000988 ASSERT_EQ(when, event.when);
989 ASSERT_EQ(readTime, event.readTime);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800990 ASSERT_EQ(eventHubId, event.deviceId);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800991 ASSERT_EQ(EV_KEY, event.type);
992 ASSERT_EQ(KEY_A, event.code);
993 ASSERT_EQ(1, event.value);
994}
995
Garfield Tan1c7bc862020-01-28 13:24:04 -0800996TEST_F(InputReaderTest, DeviceReset_RandomId) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800997 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700998 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800999 constexpr int32_t eventHubId = 1;
1000 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Prabir Pradhan42611e02018-11-27 14:04:02 -08001001 // Must add at least one mapper or the device will be ignored!
Arpit Singh8e6fb252023-04-06 11:49:17 +00001002 device->addMapper<FakeInputMapper>(eventHubId, mFakePolicy->getReaderConfiguration(),
1003 AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001004 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001005 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan42611e02018-11-27 14:04:02 -08001006
1007 NotifyDeviceResetArgs resetArgs;
1008 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001009 int32_t prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001010
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001011 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001012 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001013 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001014 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001015 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001016
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001017 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001018 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001019 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001020 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001021 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001022
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001023 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001024 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001025 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001026 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001027 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001028}
1029
Garfield Tan1c7bc862020-01-28 13:24:04 -08001030TEST_F(InputReaderTest, DeviceReset_GenerateIdWithInputReaderSource) {
1031 constexpr int32_t deviceId = 1;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001032 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Garfield Tan1c7bc862020-01-28 13:24:04 -08001033 constexpr int32_t eventHubId = 1;
1034 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1035 // Must add at least one mapper or the device will be ignored!
Arpit Singh8e6fb252023-04-06 11:49:17 +00001036 device->addMapper<FakeInputMapper>(eventHubId, mFakePolicy->getReaderConfiguration(),
1037 AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001038 mReader->pushNextDevice(device);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001039 ASSERT_NO_FATAL_FAILURE(addDevice(deviceId, "fake", deviceClass, nullptr));
1040
1041 NotifyDeviceResetArgs resetArgs;
1042 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1043 ASSERT_EQ(IdGenerator::Source::INPUT_READER, IdGenerator::getSource(resetArgs.id));
1044}
1045
Arthur Hungc23540e2018-11-29 20:42:11 +08001046TEST_F(InputReaderTest, Device_CanDispatchToDisplay) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001047 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001048 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001049 constexpr int32_t eventHubId = 1;
Arthur Hungc23540e2018-11-29 20:42:11 +08001050 const char* DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001051 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1052 FakeInputMapper& mapper =
Arpit Singh8e6fb252023-04-06 11:49:17 +00001053 device->addMapper<FakeInputMapper>(eventHubId, mFakePolicy->getReaderConfiguration(),
1054 AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001055 mReader->pushNextDevice(device);
Arthur Hungc23540e2018-11-29 20:42:11 +08001056
1057 const uint8_t hdmi1 = 1;
1058
1059 // Associated touch screen with second display.
1060 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
1061
1062 // Add default and second display.
Prabir Pradhan28efc192019-11-05 01:10:04 +00001063 mFakePolicy->clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00001064 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +00001065 /*isActive=*/true, "local:0", NO_PORT, ViewportType::INTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08001066 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +00001067 ui::ROTATION_0, /*isActive=*/true, "local:1", hdmi1,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001068 ViewportType::EXTERNAL);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00001069 mReader->requestRefreshConfiguration(InputReaderConfiguration::Change::DISPLAY_INFO);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001070 mReader->loopOnce();
Prabir Pradhan28efc192019-11-05 01:10:04 +00001071
1072 // Add the device, and make sure all of the callbacks are triggered.
1073 // The device is added after the input port associations are processed since
1074 // we do not yet support dynamic device-to-display associations.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001075 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001076 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled());
Prabir Pradhan28efc192019-11-05 01:10:04 +00001077 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001078 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
Arthur Hungc23540e2018-11-29 20:42:11 +08001079
Arthur Hung2c9a3342019-07-23 14:18:59 +08001080 // Device should only dispatch to the specified display.
Arthur Hungc23540e2018-11-29 20:42:11 +08001081 ASSERT_EQ(deviceId, device->getId());
1082 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, DISPLAY_ID));
1083 ASSERT_TRUE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hung2c9a3342019-07-23 14:18:59 +08001084
1085 // Can't dispatch event from a disabled device.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001086 disableDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001087 mReader->loopOnce();
Arthur Hung2c9a3342019-07-23 14:18:59 +08001088 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hungc23540e2018-11-29 20:42:11 +08001089}
1090
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001091TEST_F(InputReaderTest, WhenEnabledChanges_AllSubdevicesAreUpdated) {
1092 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001093 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001094 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1095 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1096 // Must add at least one mapper or the device will be ignored!
Arpit Singh8e6fb252023-04-06 11:49:17 +00001097 device->addMapper<FakeInputMapper>(eventHubIds[0], mFakePolicy->getReaderConfiguration(),
1098 AINPUT_SOURCE_KEYBOARD);
1099 device->addMapper<FakeInputMapper>(eventHubIds[1], mFakePolicy->getReaderConfiguration(),
1100 AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001101 mReader->pushNextDevice(device);
1102 mReader->pushNextDevice(device);
1103 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
1104 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
1105
1106 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
1107
1108 NotifyDeviceResetArgs resetArgs;
1109 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1110 ASSERT_EQ(deviceId, resetArgs.deviceId);
1111 ASSERT_TRUE(device->isEnabled());
1112 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1113 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1114
1115 disableDevice(deviceId);
1116 mReader->loopOnce();
1117
1118 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1119 ASSERT_EQ(deviceId, resetArgs.deviceId);
1120 ASSERT_FALSE(device->isEnabled());
1121 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1122 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1123
1124 enableDevice(deviceId);
1125 mReader->loopOnce();
1126
1127 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1128 ASSERT_EQ(deviceId, resetArgs.deviceId);
1129 ASSERT_TRUE(device->isEnabled());
1130 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1131 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1132}
1133
1134TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToSubdeviceMappers) {
1135 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001136 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001137 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1138 // Add two subdevices to device
1139 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1140 FakeInputMapper& mapperDevice1 =
Arpit Singh8e6fb252023-04-06 11:49:17 +00001141 device->addMapper<FakeInputMapper>(eventHubIds[0],
1142 mFakePolicy->getReaderConfiguration(),
1143 AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001144 FakeInputMapper& mapperDevice2 =
Arpit Singh8e6fb252023-04-06 11:49:17 +00001145 device->addMapper<FakeInputMapper>(eventHubIds[1],
1146 mFakePolicy->getReaderConfiguration(),
1147 AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001148 mReader->pushNextDevice(device);
1149 mReader->pushNextDevice(device);
1150 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
1151 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
1152
1153 mapperDevice1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
1154 mapperDevice2.setKeyCodeState(AKEYCODE_B, AKEY_STATE_DOWN);
1155
1156 ASSERT_EQ(AKEY_STATE_DOWN,
1157 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_A));
1158 ASSERT_EQ(AKEY_STATE_DOWN,
1159 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_B));
1160 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1161 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_C));
1162}
1163
Prabir Pradhan7e186182020-11-10 13:56:45 -08001164TEST_F(InputReaderTest, ChangingPointerCaptureNotifiesInputListener) {
1165 NotifyPointerCaptureChangedArgs args;
1166
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001167 auto request = mFakePolicy->setPointerCapture(true);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00001168 mReader->requestRefreshConfiguration(InputReaderConfiguration::Change::POINTER_CAPTURE);
Prabir Pradhan7e186182020-11-10 13:56:45 -08001169 mReader->loopOnce();
1170 mFakeListener->assertNotifyCaptureWasCalled(&args);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001171 ASSERT_TRUE(args.request.enable) << "Pointer Capture should be enabled.";
1172 ASSERT_EQ(args.request, request) << "Pointer Capture sequence number should match.";
Prabir Pradhan7e186182020-11-10 13:56:45 -08001173
1174 mFakePolicy->setPointerCapture(false);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00001175 mReader->requestRefreshConfiguration(InputReaderConfiguration::Change::POINTER_CAPTURE);
Prabir Pradhan7e186182020-11-10 13:56:45 -08001176 mReader->loopOnce();
1177 mFakeListener->assertNotifyCaptureWasCalled(&args);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001178 ASSERT_FALSE(args.request.enable) << "Pointer Capture should be disabled.";
Prabir Pradhan7e186182020-11-10 13:56:45 -08001179
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001180 // Verify that the Pointer Capture state is not updated when the configuration value
Prabir Pradhan7e186182020-11-10 13:56:45 -08001181 // does not change.
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00001182 mReader->requestRefreshConfiguration(InputReaderConfiguration::Change::POINTER_CAPTURE);
Prabir Pradhan7e186182020-11-10 13:56:45 -08001183 mReader->loopOnce();
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001184 mFakeListener->assertNotifyCaptureWasNotCalled();
Prabir Pradhan7e186182020-11-10 13:56:45 -08001185}
1186
Chris Ye87143712020-11-10 05:05:58 +00001187class FakeVibratorInputMapper : public FakeInputMapper {
1188public:
Arpit Singh8e6fb252023-04-06 11:49:17 +00001189 FakeVibratorInputMapper(InputDeviceContext& deviceContext,
1190 const InputReaderConfiguration& readerConfig, uint32_t sources)
1191 : FakeInputMapper(deviceContext, readerConfig, sources) {}
Chris Ye87143712020-11-10 05:05:58 +00001192
1193 std::vector<int32_t> getVibratorIds() override { return getDeviceContext().getVibratorIds(); }
1194};
1195
1196TEST_F(InputReaderTest, VibratorGetVibratorIds) {
1197 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001198 ftl::Flags<InputDeviceClass> deviceClass =
1199 InputDeviceClass::KEYBOARD | InputDeviceClass::VIBRATOR;
Chris Ye87143712020-11-10 05:05:58 +00001200 constexpr int32_t eventHubId = 1;
1201 const char* DEVICE_LOCATION = "BLUETOOTH";
1202 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1203 FakeVibratorInputMapper& mapper =
Arpit Singh8e6fb252023-04-06 11:49:17 +00001204 device->addMapper<FakeVibratorInputMapper>(eventHubId,
1205 mFakePolicy->getReaderConfiguration(),
1206 AINPUT_SOURCE_KEYBOARD);
Chris Ye87143712020-11-10 05:05:58 +00001207 mReader->pushNextDevice(device);
1208
1209 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
1210 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
1211
1212 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
1213 ASSERT_EQ(mReader->getVibratorIds(deviceId).size(), 2U);
1214}
1215
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001216// --- FakePeripheralController ---
Kim Low03ea0352020-11-06 12:45:07 -08001217
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001218class FakePeripheralController : public PeripheralControllerInterface {
Chris Yee2b1e5c2021-03-10 22:45:12 -08001219public:
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001220 FakePeripheralController(InputDeviceContext& deviceContext) : mDeviceContext(deviceContext) {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08001221
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001222 ~FakePeripheralController() override {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08001223
Andy Chenf9f1a022022-08-29 20:07:10 -04001224 int32_t getEventHubId() const { return getDeviceContext().getEventHubId(); }
1225
Chris Yee2b1e5c2021-03-10 22:45:12 -08001226 void populateDeviceInfo(InputDeviceInfo* deviceInfo) override {}
1227
1228 void dump(std::string& dump) override {}
1229
1230 std::optional<int32_t> getBatteryCapacity(int32_t batteryId) override {
1231 return getDeviceContext().getBatteryCapacity(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08001232 }
1233
Chris Yee2b1e5c2021-03-10 22:45:12 -08001234 std::optional<int32_t> getBatteryStatus(int32_t batteryId) override {
1235 return getDeviceContext().getBatteryStatus(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08001236 }
Chris Ye3fdbfef2021-01-06 18:45:18 -08001237
1238 bool setLightColor(int32_t lightId, int32_t color) override {
1239 getDeviceContext().setLightBrightness(lightId, color >> 24);
1240 return true;
1241 }
1242
1243 std::optional<int32_t> getLightColor(int32_t lightId) override {
1244 std::optional<int32_t> result = getDeviceContext().getLightBrightness(lightId);
1245 if (!result.has_value()) {
1246 return std::nullopt;
1247 }
1248 return result.value() << 24;
1249 }
Chris Yee2b1e5c2021-03-10 22:45:12 -08001250
1251 bool setLightPlayerId(int32_t lightId, int32_t playerId) override { return true; }
1252
1253 std::optional<int32_t> getLightPlayerId(int32_t lightId) override { return std::nullopt; }
1254
1255private:
1256 InputDeviceContext& mDeviceContext;
1257 inline int32_t getDeviceId() { return mDeviceContext.getId(); }
1258 inline InputDeviceContext& getDeviceContext() { return mDeviceContext; }
Andy Chenf9f1a022022-08-29 20:07:10 -04001259 inline InputDeviceContext& getDeviceContext() const { return mDeviceContext; }
Chris Ye3fdbfef2021-01-06 18:45:18 -08001260};
1261
Chris Yee2b1e5c2021-03-10 22:45:12 -08001262TEST_F(InputReaderTest, BatteryGetCapacity) {
1263 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001264 ftl::Flags<InputDeviceClass> deviceClass =
1265 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
Chris Yee2b1e5c2021-03-10 22:45:12 -08001266 constexpr int32_t eventHubId = 1;
1267 const char* DEVICE_LOCATION = "BLUETOOTH";
1268 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001269 FakePeripheralController& controller =
1270 device->addController<FakePeripheralController>(eventHubId);
Chris Yee2b1e5c2021-03-10 22:45:12 -08001271 mReader->pushNextDevice(device);
1272
1273 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
1274
Harry Cuttsa5b71292022-11-28 12:56:17 +00001275 ASSERT_EQ(controller.getBatteryCapacity(FakeEventHub::DEFAULT_BATTERY),
1276 FakeEventHub::BATTERY_CAPACITY);
1277 ASSERT_EQ(mReader->getBatteryCapacity(deviceId), FakeEventHub::BATTERY_CAPACITY);
Chris Yee2b1e5c2021-03-10 22:45:12 -08001278}
1279
1280TEST_F(InputReaderTest, BatteryGetStatus) {
1281 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001282 ftl::Flags<InputDeviceClass> deviceClass =
1283 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
Chris Yee2b1e5c2021-03-10 22:45:12 -08001284 constexpr int32_t eventHubId = 1;
1285 const char* DEVICE_LOCATION = "BLUETOOTH";
1286 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001287 FakePeripheralController& controller =
1288 device->addController<FakePeripheralController>(eventHubId);
Chris Yee2b1e5c2021-03-10 22:45:12 -08001289 mReader->pushNextDevice(device);
1290
1291 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
1292
Harry Cuttsa5b71292022-11-28 12:56:17 +00001293 ASSERT_EQ(controller.getBatteryStatus(FakeEventHub::DEFAULT_BATTERY),
1294 FakeEventHub::BATTERY_STATUS);
1295 ASSERT_EQ(mReader->getBatteryStatus(deviceId), FakeEventHub::BATTERY_STATUS);
Chris Yee2b1e5c2021-03-10 22:45:12 -08001296}
1297
Prabir Pradhane287ecd2022-09-07 21:18:05 +00001298TEST_F(InputReaderTest, BatteryGetDevicePath) {
1299 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1300 ftl::Flags<InputDeviceClass> deviceClass =
1301 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
1302 constexpr int32_t eventHubId = 1;
1303 const char* DEVICE_LOCATION = "BLUETOOTH";
1304 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1305 device->addController<FakePeripheralController>(eventHubId);
1306 mReader->pushNextDevice(device);
1307
1308 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
1309
Harry Cuttsa5b71292022-11-28 12:56:17 +00001310 ASSERT_EQ(mReader->getBatteryDevicePath(deviceId), FakeEventHub::BATTERY_DEVPATH);
Prabir Pradhane287ecd2022-09-07 21:18:05 +00001311}
1312
Chris Ye3fdbfef2021-01-06 18:45:18 -08001313TEST_F(InputReaderTest, LightGetColor) {
1314 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001315 ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD | InputDeviceClass::LIGHT;
Chris Ye3fdbfef2021-01-06 18:45:18 -08001316 constexpr int32_t eventHubId = 1;
1317 const char* DEVICE_LOCATION = "BLUETOOTH";
1318 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001319 FakePeripheralController& controller =
1320 device->addController<FakePeripheralController>(eventHubId);
Chris Ye3fdbfef2021-01-06 18:45:18 -08001321 mReader->pushNextDevice(device);
1322 RawLightInfo info = {.id = 1,
1323 .name = "Mono",
1324 .maxBrightness = 255,
1325 .flags = InputLightClass::BRIGHTNESS,
1326 .path = ""};
Harry Cutts33476232023-01-30 19:57:29 +00001327 mFakeEventHub->addRawLightInfo(/*rawId=*/1, std::move(info));
1328 mFakeEventHub->fakeLightBrightness(/*rawId=*/1, 0x55);
Chris Ye3fdbfef2021-01-06 18:45:18 -08001329
1330 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Chris Ye3fdbfef2021-01-06 18:45:18 -08001331
Harry Cutts33476232023-01-30 19:57:29 +00001332 ASSERT_TRUE(controller.setLightColor(/*lightId=*/1, LIGHT_BRIGHTNESS));
1333 ASSERT_EQ(controller.getLightColor(/*lightId=*/1), LIGHT_BRIGHTNESS);
1334 ASSERT_TRUE(mReader->setLightColor(deviceId, /*lightId=*/1, LIGHT_BRIGHTNESS));
1335 ASSERT_EQ(mReader->getLightColor(deviceId, /*lightId=*/1), LIGHT_BRIGHTNESS);
Chris Ye3fdbfef2021-01-06 18:45:18 -08001336}
1337
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001338// --- InputReaderIntegrationTest ---
1339
1340// These tests create and interact with the InputReader only through its interface.
1341// The InputReader is started during SetUp(), which starts its processing in its own
1342// thread. The tests use linux uinput to emulate input devices.
1343// NOTE: Interacting with the physical device while these tests are running may cause
1344// the tests to fail.
1345class InputReaderIntegrationTest : public testing::Test {
1346protected:
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001347 std::unique_ptr<TestInputListener> mTestListener;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001348 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001349 std::unique_ptr<InputReaderInterface> mReader;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001350
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00001351 std::shared_ptr<FakePointerController> mFakePointerController;
1352
Prabir Pradhanafb7d612024-01-08 22:45:24 +00001353 constexpr static auto EVENT_HAPPENED_TIMEOUT = 2000ms;
1354 constexpr static auto EVENT_DID_NOT_HAPPEN_TIMEOUT = 30ms;
1355
Chris Yea52ade12020-08-27 16:49:20 -07001356 void SetUp() override {
Siarhei Vishniakou31977182022-09-30 08:51:23 -07001357#if !defined(__ANDROID__)
1358 GTEST_SKIP();
1359#endif
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001360 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00001361 mFakePointerController = std::make_shared<FakePointerController>();
1362 mFakePolicy->setPointerController(mFakePointerController);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001363
Arpit Singh440bf652023-08-09 09:23:43 +00001364 setupInputReader();
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001365 }
1366
Chris Yea52ade12020-08-27 16:49:20 -07001367 void TearDown() override {
Siarhei Vishniakou31977182022-09-30 08:51:23 -07001368#if !defined(__ANDROID__)
1369 return;
1370#endif
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001371 ASSERT_EQ(mReader->stop(), OK);
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001372 mReader.reset();
1373 mTestListener.reset();
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001374 mFakePolicy.clear();
1375 }
Prabir Pradhanda20b172022-09-26 17:01:18 +00001376
1377 std::optional<InputDeviceInfo> findDeviceByName(const std::string& name) {
1378 const std::vector<InputDeviceInfo> inputDevices = mFakePolicy->getInputDevices();
1379 const auto& it = std::find_if(inputDevices.begin(), inputDevices.end(),
1380 [&name](const InputDeviceInfo& info) {
1381 return info.getIdentifier().name == name;
1382 });
1383 return it != inputDevices.end() ? std::make_optional(*it) : std::nullopt;
1384 }
Arpit Singh440bf652023-08-09 09:23:43 +00001385
1386 void setupInputReader() {
Prabir Pradhanafb7d612024-01-08 22:45:24 +00001387 mTestListener = std::make_unique<TestInputListener>(EVENT_HAPPENED_TIMEOUT,
1388 EVENT_DID_NOT_HAPPEN_TIMEOUT);
Arpit Singh440bf652023-08-09 09:23:43 +00001389
1390 mReader = std::make_unique<InputReader>(std::make_shared<EventHub>(), mFakePolicy,
1391 *mTestListener);
1392 ASSERT_EQ(mReader->start(), OK);
1393
1394 // Since this test is run on a real device, all the input devices connected
1395 // to the test device will show up in mReader. We wait for those input devices to
1396 // show up before beginning the tests.
1397 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1398 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyInputDevicesChangedWasCalled());
1399 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1400 }
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001401};
1402
1403TEST_F(InputReaderIntegrationTest, TestInvalidDevice) {
1404 // An invalid input device that is only used for this test.
1405 class InvalidUinputDevice : public UinputDevice {
1406 public:
Harry Cutts33476232023-01-30 19:57:29 +00001407 InvalidUinputDevice() : UinputDevice("Invalid Device", /*productId=*/99) {}
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001408
1409 private:
1410 void configureDevice(int fd, uinput_user_dev* device) override {}
1411 };
1412
1413 const size_t numDevices = mFakePolicy->getInputDevices().size();
1414
1415 // UinputDevice does not set any event or key bits, so InputReader should not
1416 // consider it as a valid device.
1417 std::unique_ptr<UinputDevice> invalidDevice = createUinputDevice<InvalidUinputDevice>();
1418 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
1419 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
1420 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
1421
1422 invalidDevice.reset();
1423 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
1424 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
1425 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
1426}
1427
1428TEST_F(InputReaderIntegrationTest, AddNewDevice) {
1429 const size_t initialNumDevices = mFakePolicy->getInputDevices().size();
1430
1431 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
1432 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1433 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1434 ASSERT_EQ(initialNumDevices + 1, mFakePolicy->getInputDevices().size());
1435
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001436 const auto device = findDeviceByName(keyboard->getName());
1437 ASSERT_TRUE(device.has_value());
1438 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, device->getKeyboardType());
1439 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, device->getSources());
1440 ASSERT_EQ(0U, device->getMotionRanges().size());
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001441
1442 keyboard.reset();
1443 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1444 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1445 ASSERT_EQ(initialNumDevices, mFakePolicy->getInputDevices().size());
1446}
1447
1448TEST_F(InputReaderIntegrationTest, SendsEventsToInputListener) {
1449 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
1450 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1451
1452 NotifyConfigurationChangedArgs configChangedArgs;
1453 ASSERT_NO_FATAL_FAILURE(
1454 mTestListener->assertNotifyConfigurationChangedWasCalled(&configChangedArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001455 int32_t prevId = configChangedArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001456 nsecs_t prevTimestamp = configChangedArgs.eventTime;
1457
1458 NotifyKeyArgs keyArgs;
1459 keyboard->pressAndReleaseHomeKey();
1460 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
1461 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001462 ASSERT_NE(prevId, keyArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001463 prevId = keyArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001464 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001465 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001466 prevTimestamp = keyArgs.eventTime;
1467
1468 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
1469 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001470 ASSERT_NE(prevId, keyArgs.id);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001471 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001472 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001473}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001474
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001475TEST_F(InputReaderIntegrationTest, ExternalStylusesButtons) {
1476 std::unique_ptr<UinputExternalStylus> stylus = createUinputDevice<UinputExternalStylus>();
1477 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1478
1479 const auto device = findDeviceByName(stylus->getName());
1480 ASSERT_TRUE(device.has_value());
1481
Prabir Pradhana3621852022-10-14 18:57:23 +00001482 // An external stylus with buttons should also be recognized as a keyboard.
1483 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_STYLUS, device->getSources())
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001484 << "Unexpected source " << inputEventSourceToString(device->getSources()).c_str();
1485 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, device->getKeyboardType());
1486
1487 const auto DOWN =
1488 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD));
1489 const auto UP = AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD));
1490
1491 stylus->pressAndReleaseKey(BTN_STYLUS);
1492 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1493 AllOf(DOWN, WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY))));
1494 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1495 AllOf(UP, WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY))));
1496
1497 stylus->pressAndReleaseKey(BTN_STYLUS2);
1498 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1499 AllOf(DOWN, WithKeyCode(AKEYCODE_STYLUS_BUTTON_SECONDARY))));
1500 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1501 AllOf(UP, WithKeyCode(AKEYCODE_STYLUS_BUTTON_SECONDARY))));
1502
1503 stylus->pressAndReleaseKey(BTN_STYLUS3);
1504 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1505 AllOf(DOWN, WithKeyCode(AKEYCODE_STYLUS_BUTTON_TERTIARY))));
1506 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1507 AllOf(UP, WithKeyCode(AKEYCODE_STYLUS_BUTTON_TERTIARY))));
1508}
1509
Prabir Pradhan3c28b942023-08-18 20:02:01 +00001510TEST_F(InputReaderIntegrationTest, KeyboardWithStylusButtons) {
1511 std::unique_ptr<UinputKeyboard> keyboard =
1512 createUinputDevice<UinputKeyboard>("KeyboardWithStylusButtons", /*productId=*/99,
1513 std::initializer_list<int>{KEY_Q, KEY_W, KEY_E,
1514 KEY_R, KEY_T, KEY_Y,
1515 BTN_STYLUS, BTN_STYLUS2,
1516 BTN_STYLUS3});
1517 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1518
1519 const auto device = findDeviceByName(keyboard->getName());
1520 ASSERT_TRUE(device.has_value());
1521
1522 // An alphabetical keyboard that reports stylus buttons should not be recognized as a stylus.
1523 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, device->getSources())
1524 << "Unexpected source " << inputEventSourceToString(device->getSources()).c_str();
1525 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_ALPHABETIC, device->getKeyboardType());
1526}
1527
Prabir Pradhan37a819b2023-08-22 23:20:16 +00001528TEST_F(InputReaderIntegrationTest, HidUsageKeyboardIsNotAStylus) {
1529 // Create a Uinput keyboard that simulates a keyboard that can report HID usage codes. The
1530 // hid-input driver reports HID usage codes using the value for EV_MSC MSC_SCAN event.
1531 std::unique_ptr<UinputKeyboardWithHidUsage> keyboard =
1532 createUinputDevice<UinputKeyboardWithHidUsage>(
1533 std::initializer_list<int>{KEY_VOLUMEUP, KEY_VOLUMEDOWN});
1534 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1535
1536 const auto device = findDeviceByName(keyboard->getName());
1537 ASSERT_TRUE(device.has_value());
1538
1539 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, device->getSources())
1540 << "Unexpected source " << inputEventSourceToString(device->getSources()).c_str();
1541
1542 // If a device supports reporting HID usage codes, it shouldn't automatically support
1543 // stylus keys.
1544 const std::vector<int> keycodes{AKEYCODE_STYLUS_BUTTON_PRIMARY};
1545 uint8_t outFlags[] = {0};
1546 ASSERT_TRUE(mReader->hasKeys(device->getId(), AINPUT_SOURCE_KEYBOARD, keycodes, outFlags));
1547 ASSERT_EQ(0, outFlags[0]) << "Keyboard should not have stylus button";
1548}
1549
Siarhei Vishniakoua0d2b802020-05-13 14:00:31 -07001550/**
1551 * The Steam controller sends BTN_GEAR_DOWN and BTN_GEAR_UP for the two "paddle" buttons
1552 * on the back. In this test, we make sure that BTN_GEAR_DOWN / BTN_WHEEL and BTN_GEAR_UP
1553 * are passed to the listener.
1554 */
1555static_assert(BTN_GEAR_DOWN == BTN_WHEEL);
1556TEST_F(InputReaderIntegrationTest, SendsGearDownAndUpToInputListener) {
1557 std::unique_ptr<UinputSteamController> controller = createUinputDevice<UinputSteamController>();
1558 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1559 NotifyKeyArgs keyArgs;
1560
1561 controller->pressAndReleaseKey(BTN_GEAR_DOWN);
1562 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
1563 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
1564 ASSERT_EQ(BTN_GEAR_DOWN, keyArgs.scanCode);
1565
1566 controller->pressAndReleaseKey(BTN_GEAR_UP);
1567 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
1568 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
1569 ASSERT_EQ(BTN_GEAR_UP, keyArgs.scanCode);
1570}
1571
Prabir Pradhan484d55a2022-10-14 23:17:16 +00001572// --- TouchIntegrationTest ---
1573
Arpit Singh440bf652023-08-09 09:23:43 +00001574class BaseTouchIntegrationTest : public InputReaderIntegrationTest {
Arthur Hungaab25622020-01-16 11:22:11 +08001575protected:
Arthur Hungaab25622020-01-16 11:22:11 +08001576 const std::string UNIQUE_ID = "local:0";
1577
Chris Yea52ade12020-08-27 16:49:20 -07001578 void SetUp() override {
Siarhei Vishniakou31977182022-09-30 08:51:23 -07001579#if !defined(__ANDROID__)
1580 GTEST_SKIP();
1581#endif
Arthur Hungaab25622020-01-16 11:22:11 +08001582 InputReaderIntegrationTest::SetUp();
1583 // At least add an internal display.
Michael Wrighta9cf4192022-12-01 23:46:39 +00001584 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
1585 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Arthur Hungaab25622020-01-16 11:22:11 +08001586
1587 mDevice = createUinputDevice<UinputTouchScreen>(Rect(0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT));
1588 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1589 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
Prabir Pradhanda20b172022-09-26 17:01:18 +00001590 const auto info = findDeviceByName(mDevice->getName());
1591 ASSERT_TRUE(info);
1592 mDeviceInfo = *info;
Arthur Hungaab25622020-01-16 11:22:11 +08001593 }
1594
1595 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
Michael Wrighta9cf4192022-12-01 23:46:39 +00001596 ui::Rotation orientation, const std::string& uniqueId,
Arthur Hungaab25622020-01-16 11:22:11 +08001597 std::optional<uint8_t> physicalPort,
1598 ViewportType viewportType) {
Harry Cutts33476232023-01-30 19:57:29 +00001599 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, /*isActive=*/true,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001600 uniqueId, physicalPort, viewportType);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00001601 mReader->requestRefreshConfiguration(InputReaderConfiguration::Change::DISPLAY_INFO);
Arthur Hungaab25622020-01-16 11:22:11 +08001602 }
1603
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001604 void assertReceivedMotion(int32_t action, const std::vector<Point>& points) {
1605 NotifyMotionArgs args;
1606 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1607 EXPECT_EQ(action, args.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07001608 ASSERT_EQ(points.size(), args.getPointerCount());
1609 for (size_t i = 0; i < args.getPointerCount(); i++) {
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001610 EXPECT_EQ(points[i].x, args.pointerCoords[i].getX());
1611 EXPECT_EQ(points[i].y, args.pointerCoords[i].getY());
1612 }
1613 }
1614
Arthur Hungaab25622020-01-16 11:22:11 +08001615 std::unique_ptr<UinputTouchScreen> mDevice;
Prabir Pradhanda20b172022-09-26 17:01:18 +00001616 InputDeviceInfo mDeviceInfo;
Arthur Hungaab25622020-01-16 11:22:11 +08001617};
1618
Arpit Singh440bf652023-08-09 09:23:43 +00001619enum class TouchIntegrationTestDisplays { DISPLAY_INTERNAL, DISPLAY_INPUT_PORT, DISPLAY_UNIQUE_ID };
1620
1621class TouchIntegrationTest : public BaseTouchIntegrationTest,
1622 public testing::WithParamInterface<TouchIntegrationTestDisplays> {
1623protected:
1624 static constexpr std::optional<uint8_t> DISPLAY_PORT = 0;
1625 const std::string INPUT_PORT = "uinput_touch/input0";
1626
1627 void SetUp() override {
1628#if !defined(__ANDROID__)
1629 GTEST_SKIP();
1630#endif
1631 if (GetParam() == TouchIntegrationTestDisplays::DISPLAY_INTERNAL) {
1632 BaseTouchIntegrationTest::SetUp();
1633 return;
1634 }
1635
1636 // setup policy with a input-port or UniqueId association to the display
1637 bool isInputPortAssociation =
1638 GetParam() == TouchIntegrationTestDisplays::DISPLAY_INPUT_PORT;
1639
1640 mFakePolicy = sp<FakeInputReaderPolicy>::make();
1641 if (isInputPortAssociation) {
1642 mFakePolicy->addInputPortAssociation(INPUT_PORT, DISPLAY_PORT.value());
1643 } else {
1644 mFakePolicy->addInputUniqueIdAssociation(INPUT_PORT, UNIQUE_ID);
1645 }
1646 mFakePointerController = std::make_shared<FakePointerController>();
1647 mFakePolicy->setPointerController(mFakePointerController);
1648
1649 InputReaderIntegrationTest::setupInputReader();
1650
1651 mDevice = createUinputDevice<UinputTouchScreen>(Rect(0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT),
1652 INPUT_PORT);
1653 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1654
1655 // Add a display linked to a physical port or UniqueId.
1656 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
1657 UNIQUE_ID, isInputPortAssociation ? DISPLAY_PORT : NO_PORT,
1658 ViewportType::INTERNAL);
1659 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1660 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1661 const auto info = findDeviceByName(mDevice->getName());
1662 ASSERT_TRUE(info);
1663 mDeviceInfo = *info;
1664 }
1665};
1666
1667TEST_P(TouchIntegrationTest, MultiTouchDeviceSource) {
Prabir Pradhanf9a41282022-10-25 17:15:50 +00001668 // The UinputTouchScreen is an MT device that supports MT_TOOL_TYPE and also supports stylus
1669 // buttons. It should show up as a touchscreen, stylus, and keyboard (for reporting button
1670 // presses).
1671 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_KEYBOARD,
1672 mDeviceInfo.getSources());
1673}
1674
Arpit Singh440bf652023-08-09 09:23:43 +00001675TEST_P(TouchIntegrationTest, InputEvent_ProcessSingleTouch) {
Arthur Hungaab25622020-01-16 11:22:11 +08001676 NotifyMotionArgs args;
1677 const Point centerPoint = mDevice->getCenterPoint();
1678
1679 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00001680 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08001681 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001682 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001683 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1684 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
1685
1686 // ACTION_MOVE
1687 mDevice->sendMove(centerPoint + Point(1, 1));
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001688 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001689 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1690 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
1691
1692 // ACTION_UP
1693 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001694 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001695 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1696 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
1697}
1698
Arpit Singh440bf652023-08-09 09:23:43 +00001699TEST_P(TouchIntegrationTest, InputEvent_ProcessMultiTouch) {
Arthur Hungaab25622020-01-16 11:22:11 +08001700 NotifyMotionArgs args;
1701 const Point centerPoint = mDevice->getCenterPoint();
1702
1703 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00001704 mDevice->sendSlot(FIRST_SLOT);
1705 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08001706 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001707 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001708 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1709 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
1710
1711 // ACTION_POINTER_DOWN (Second slot)
1712 const Point secondPoint = centerPoint + Point(100, 100);
1713 mDevice->sendSlot(SECOND_SLOT);
1714 mDevice->sendTrackingId(SECOND_TRACKING_ID);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001715 mDevice->sendDown(secondPoint);
1716 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001717 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08001718 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001719
1720 // ACTION_MOVE (Second slot)
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001721 mDevice->sendMove(secondPoint + Point(1, 1));
1722 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001723 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1724 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
1725
1726 // ACTION_POINTER_UP (Second slot)
arthurhungcc7f9802020-04-30 17:55:40 +08001727 mDevice->sendPointerUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001728 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001729 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08001730 ASSERT_EQ(ACTION_POINTER_1_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001731
1732 // ACTION_UP
1733 mDevice->sendSlot(FIRST_SLOT);
1734 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001735 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001736 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1737 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
1738}
1739
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001740/**
1741 * What happens when a pointer goes up while another pointer moves in the same frame? Are POINTER_UP
1742 * events guaranteed to contain the same data as a preceding MOVE, or can they contain different
1743 * data?
1744 * In this test, we try to send a change in coordinates in Pointer 0 in the same frame as the
1745 * liftoff of Pointer 1. We check that POINTER_UP event is generated first, and the MOVE event
1746 * for Pointer 0 only is generated after.
1747 * Suppose we are only interested in learning the movement of Pointer 0. If we only observe MOVE
1748 * events, we will not miss any information.
1749 * Even though the Pointer 1 up event contains updated Pointer 0 coordinates, there is another MOVE
1750 * event generated afterwards that contains the newest movement of pointer 0.
1751 * This is important for palm rejection. If there is a subsequent InputListener stage that detects
1752 * palms, and wants to cancel Pointer 1, then it is safe to simply drop POINTER_1_UP event without
1753 * losing information about non-palm pointers.
1754 */
Arpit Singh440bf652023-08-09 09:23:43 +00001755TEST_P(TouchIntegrationTest, MultiTouch_PointerMoveAndSecondPointerUp) {
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001756 NotifyMotionArgs args;
1757 const Point centerPoint = mDevice->getCenterPoint();
1758
1759 // ACTION_DOWN
1760 mDevice->sendSlot(FIRST_SLOT);
1761 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1762 mDevice->sendDown(centerPoint);
1763 mDevice->sendSync();
1764 assertReceivedMotion(AMOTION_EVENT_ACTION_DOWN, {centerPoint});
1765
1766 // ACTION_POINTER_DOWN (Second slot)
1767 const Point secondPoint = centerPoint + Point(100, 100);
1768 mDevice->sendSlot(SECOND_SLOT);
1769 mDevice->sendTrackingId(SECOND_TRACKING_ID);
1770 mDevice->sendDown(secondPoint);
1771 mDevice->sendSync();
1772 assertReceivedMotion(ACTION_POINTER_1_DOWN, {centerPoint, secondPoint});
1773
1774 // ACTION_MOVE (First slot)
1775 mDevice->sendSlot(FIRST_SLOT);
1776 mDevice->sendMove(centerPoint + Point(5, 5));
1777 // ACTION_POINTER_UP (Second slot)
1778 mDevice->sendSlot(SECOND_SLOT);
1779 mDevice->sendPointerUp();
1780 // Send a single sync for the above 2 pointer updates
1781 mDevice->sendSync();
1782
1783 // First, we should get POINTER_UP for the second pointer
1784 assertReceivedMotion(ACTION_POINTER_1_UP,
1785 {/*first pointer */ centerPoint + Point(5, 5),
1786 /*second pointer*/ secondPoint});
1787
1788 // Next, the MOVE event for the first pointer
1789 assertReceivedMotion(AMOTION_EVENT_ACTION_MOVE, {centerPoint + Point(5, 5)});
1790}
1791
1792/**
1793 * Similar scenario as above. The difference is that when the second pointer goes up, it will first
1794 * move, and then it will go up, all in the same frame.
1795 * In this scenario, the movement of the second pointer just prior to liftoff is ignored, and never
1796 * gets sent to the listener.
1797 */
Arpit Singh440bf652023-08-09 09:23:43 +00001798TEST_P(TouchIntegrationTest, MultiTouch_PointerMoveAndSecondPointerMoveAndUp) {
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001799 NotifyMotionArgs args;
1800 const Point centerPoint = mDevice->getCenterPoint();
1801
1802 // ACTION_DOWN
1803 mDevice->sendSlot(FIRST_SLOT);
1804 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1805 mDevice->sendDown(centerPoint);
1806 mDevice->sendSync();
1807 assertReceivedMotion(AMOTION_EVENT_ACTION_DOWN, {centerPoint});
1808
1809 // ACTION_POINTER_DOWN (Second slot)
1810 const Point secondPoint = centerPoint + Point(100, 100);
1811 mDevice->sendSlot(SECOND_SLOT);
1812 mDevice->sendTrackingId(SECOND_TRACKING_ID);
1813 mDevice->sendDown(secondPoint);
1814 mDevice->sendSync();
1815 assertReceivedMotion(ACTION_POINTER_1_DOWN, {centerPoint, secondPoint});
1816
1817 // ACTION_MOVE (First slot)
1818 mDevice->sendSlot(FIRST_SLOT);
1819 mDevice->sendMove(centerPoint + Point(5, 5));
1820 // ACTION_POINTER_UP (Second slot)
1821 mDevice->sendSlot(SECOND_SLOT);
1822 mDevice->sendMove(secondPoint + Point(6, 6));
1823 mDevice->sendPointerUp();
1824 // Send a single sync for the above 2 pointer updates
1825 mDevice->sendSync();
1826
1827 // First, we should get POINTER_UP for the second pointer
1828 // The movement of the second pointer during the liftoff frame is ignored.
1829 // The coordinates 'secondPoint + Point(6, 6)' are never sent to the listener.
1830 assertReceivedMotion(ACTION_POINTER_1_UP,
1831 {/*first pointer */ centerPoint + Point(5, 5),
1832 /*second pointer*/ secondPoint});
1833
1834 // Next, the MOVE event for the first pointer
1835 assertReceivedMotion(AMOTION_EVENT_ACTION_MOVE, {centerPoint + Point(5, 5)});
1836}
1837
Arpit Singh440bf652023-08-09 09:23:43 +00001838TEST_P(TouchIntegrationTest, InputEvent_ProcessPalm) {
Arthur Hungaab25622020-01-16 11:22:11 +08001839 NotifyMotionArgs args;
1840 const Point centerPoint = mDevice->getCenterPoint();
1841
1842 // ACTION_DOWN
arthurhungcc7f9802020-04-30 17:55:40 +08001843 mDevice->sendSlot(FIRST_SLOT);
1844 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08001845 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001846 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001847 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1848 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
1849
arthurhungcc7f9802020-04-30 17:55:40 +08001850 // ACTION_POINTER_DOWN (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08001851 const Point secondPoint = centerPoint + Point(100, 100);
1852 mDevice->sendSlot(SECOND_SLOT);
1853 mDevice->sendTrackingId(SECOND_TRACKING_ID);
1854 mDevice->sendDown(secondPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001855 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001856 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08001857 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001858
arthurhungcc7f9802020-04-30 17:55:40 +08001859 // ACTION_MOVE (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08001860 mDevice->sendMove(secondPoint + Point(1, 1));
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001861 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001862 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1863 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
1864
arthurhungcc7f9802020-04-30 17:55:40 +08001865 // Send MT_TOOL_PALM (second slot), which indicates that the touch IC has determined this to be
1866 // a palm event.
1867 // Expect to receive the ACTION_POINTER_UP with cancel flag.
Arthur Hungaab25622020-01-16 11:22:11 +08001868 mDevice->sendToolType(MT_TOOL_PALM);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001869 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001870 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08001871 ASSERT_EQ(ACTION_POINTER_1_UP, args.action);
arthurhungcc7f9802020-04-30 17:55:40 +08001872 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, args.flags);
Arthur Hungaab25622020-01-16 11:22:11 +08001873
arthurhungcc7f9802020-04-30 17:55:40 +08001874 // Send up to second slot, expect first slot send moving.
1875 mDevice->sendPointerUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001876 mDevice->sendSync();
arthurhungcc7f9802020-04-30 17:55:40 +08001877 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1878 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001879
arthurhungcc7f9802020-04-30 17:55:40 +08001880 // Send ACTION_UP (first slot)
Arthur Hungaab25622020-01-16 11:22:11 +08001881 mDevice->sendSlot(FIRST_SLOT);
1882 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001883 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001884
arthurhungcc7f9802020-04-30 17:55:40 +08001885 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1886 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001887}
1888
Prabir Pradhanc09ec6d2023-08-14 22:31:43 +00001889/**
1890 * Some drivers historically have reported axis values outside of the range specified in the
1891 * evdev axis info. Ensure we don't crash when this happens. For example, a driver may report a
1892 * pressure value greater than the reported maximum, since it unclear what specific meaning the
1893 * maximum value for pressure has (beyond the maximum value that can be produced by a sensor),
1894 * and no units for pressure (resolution) is specified by the evdev documentation.
1895 */
1896TEST_P(TouchIntegrationTest, AcceptsAxisValuesOutsideReportedRange) {
1897 const Point centerPoint = mDevice->getCenterPoint();
1898
1899 // Down with pressure outside the reported range
1900 mDevice->sendSlot(FIRST_SLOT);
1901 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1902 mDevice->sendDown(centerPoint);
1903 mDevice->sendPressure(UinputTouchScreen::RAW_PRESSURE_MAX + 2);
1904 mDevice->sendSync();
1905 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
1906 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
1907
1908 // Move to a point outside the reported range
1909 mDevice->sendMove(Point(DISPLAY_WIDTH, DISPLAY_HEIGHT) + Point(1, 1));
1910 mDevice->sendSync();
1911 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
1912 WithMotionAction(AMOTION_EVENT_ACTION_MOVE)));
1913
1914 // Up
1915 mDevice->sendUp();
1916 mDevice->sendSync();
1917 ASSERT_NO_FATAL_FAILURE(
1918 mTestListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
1919}
1920
Arpit Singh440bf652023-08-09 09:23:43 +00001921TEST_P(TouchIntegrationTest, NotifiesPolicyWhenStylusGestureStarted) {
Prabir Pradhanda20b172022-09-26 17:01:18 +00001922 const Point centerPoint = mDevice->getCenterPoint();
1923
1924 // Send down with the pen tool selected. The policy should be notified of the stylus presence.
1925 mDevice->sendSlot(FIRST_SLOT);
1926 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1927 mDevice->sendToolType(MT_TOOL_PEN);
1928 mDevice->sendDown(centerPoint);
1929 mDevice->sendSync();
1930 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
1931 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001932 WithToolType(ToolType::STYLUS))));
Prabir Pradhanda20b172022-09-26 17:01:18 +00001933
1934 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotified(mDeviceInfo.getId()));
1935
1936 // Release the stylus touch.
1937 mDevice->sendUp();
1938 mDevice->sendSync();
1939 ASSERT_NO_FATAL_FAILURE(
1940 mTestListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
1941
1942 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotNotified());
1943
1944 // Touch down with the finger, without the pen tool selected. The policy is not notified.
1945 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1946 mDevice->sendToolType(MT_TOOL_FINGER);
1947 mDevice->sendDown(centerPoint);
1948 mDevice->sendSync();
1949 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
1950 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001951 WithToolType(ToolType::FINGER))));
Prabir Pradhanda20b172022-09-26 17:01:18 +00001952
1953 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotNotified());
1954
1955 mDevice->sendUp();
1956 mDevice->sendSync();
1957 ASSERT_NO_FATAL_FAILURE(
1958 mTestListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
1959
1960 // Send a move event with the stylus tool without BTN_TOUCH to generate a hover enter.
1961 // The policy should be notified of the stylus presence.
1962 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1963 mDevice->sendToolType(MT_TOOL_PEN);
1964 mDevice->sendMove(centerPoint);
1965 mDevice->sendSync();
1966 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
1967 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001968 WithToolType(ToolType::STYLUS))));
Prabir Pradhanda20b172022-09-26 17:01:18 +00001969
1970 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotified(mDeviceInfo.getId()));
1971}
1972
Arpit Singh440bf652023-08-09 09:23:43 +00001973TEST_P(TouchIntegrationTest, ExternalStylusConnectedDuringTouchGesture) {
Prabir Pradhan85cf63e2023-08-07 21:02:13 +00001974 const Point centerPoint = mDevice->getCenterPoint();
1975
1976 // Down
1977 mDevice->sendSlot(FIRST_SLOT);
1978 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1979 mDevice->sendDown(centerPoint);
1980 mDevice->sendSync();
1981 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
1982 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
1983
1984 // Move
1985 mDevice->sendMove(centerPoint + Point(1, 1));
1986 mDevice->sendSync();
1987 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
1988 WithMotionAction(AMOTION_EVENT_ACTION_MOVE)));
1989
1990 // Connecting an external stylus mid-gesture should not interrupt the ongoing gesture stream.
1991 auto externalStylus = createUinputDevice<UinputExternalStylus>();
1992 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1993 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1994 const auto stylusInfo = findDeviceByName(externalStylus->getName());
1995 ASSERT_TRUE(stylusInfo);
Prabir Pradhan85cf63e2023-08-07 21:02:13 +00001996
1997 // Move
1998 mDevice->sendMove(centerPoint + Point(2, 2));
1999 mDevice->sendSync();
2000 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2001 WithMotionAction(AMOTION_EVENT_ACTION_MOVE)));
2002
2003 // Disconnecting an external stylus mid-gesture should not interrupt the ongoing gesture stream.
2004 externalStylus.reset();
2005 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2006 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2007 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2008
2009 // Up
2010 mDevice->sendUp();
2011 mDevice->sendSync();
2012 ASSERT_NO_FATAL_FAILURE(
2013 mTestListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
2014
2015 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2016}
2017
Arpit Singh440bf652023-08-09 09:23:43 +00002018INSTANTIATE_TEST_SUITE_P(TouchIntegrationTestDisplayVariants, TouchIntegrationTest,
2019 testing::Values(TouchIntegrationTestDisplays::DISPLAY_INTERNAL,
2020 TouchIntegrationTestDisplays::DISPLAY_INPUT_PORT,
2021 TouchIntegrationTestDisplays::DISPLAY_UNIQUE_ID));
2022
Prabir Pradhan124ea442022-10-28 20:27:44 +00002023// --- StylusButtonIntegrationTest ---
Prabir Pradhane1a41a82022-10-14 18:06:50 +00002024
Prabir Pradhan124ea442022-10-28 20:27:44 +00002025// Verify the behavior of button presses reported by various kinds of styluses, including buttons
2026// reported by the touchscreen's device, by a fused external stylus, and by an un-fused external
2027// stylus.
2028template <typename UinputStylusDevice>
Arpit Singh440bf652023-08-09 09:23:43 +00002029class StylusButtonIntegrationTest : public BaseTouchIntegrationTest {
Prabir Pradhan124ea442022-10-28 20:27:44 +00002030protected:
2031 void SetUp() override {
2032#if !defined(__ANDROID__)
2033 GTEST_SKIP();
2034#endif
Arpit Singh440bf652023-08-09 09:23:43 +00002035 BaseTouchIntegrationTest::SetUp();
Prabir Pradhan124ea442022-10-28 20:27:44 +00002036 mTouchscreen = mDevice.get();
2037 mTouchscreenInfo = mDeviceInfo;
2038
2039 setUpStylusDevice();
2040 }
2041
2042 UinputStylusDevice* mStylus{nullptr};
2043 InputDeviceInfo mStylusInfo{};
2044
2045 UinputTouchScreen* mTouchscreen{nullptr};
2046 InputDeviceInfo mTouchscreenInfo{};
2047
2048private:
2049 // When we are attempting to test stylus button events that are sent from the touchscreen,
2050 // use the same Uinput device for the touchscreen and the stylus.
2051 template <typename T = UinputStylusDevice>
2052 std::enable_if_t<std::is_same_v<UinputTouchScreen, T>, void> setUpStylusDevice() {
2053 mStylus = mDevice.get();
2054 mStylusInfo = mDeviceInfo;
2055 }
2056
2057 // When we are attempting to stylus buttons from an external stylus being merged with touches
2058 // from a touchscreen, create a new Uinput device through which stylus buttons can be injected.
2059 template <typename T = UinputStylusDevice>
2060 std::enable_if_t<!std::is_same_v<UinputTouchScreen, T>, void> setUpStylusDevice() {
2061 mStylusDeviceLifecycleTracker = createUinputDevice<T>();
2062 mStylus = mStylusDeviceLifecycleTracker.get();
2063 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2064 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2065 const auto info = findDeviceByName(mStylus->getName());
2066 ASSERT_TRUE(info);
2067 mStylusInfo = *info;
2068 }
2069
2070 std::unique_ptr<UinputStylusDevice> mStylusDeviceLifecycleTracker{};
2071
2072 // Hide the base class's device to expose it with a different name for readability.
Arpit Singh440bf652023-08-09 09:23:43 +00002073 using BaseTouchIntegrationTest::mDevice;
2074 using BaseTouchIntegrationTest::mDeviceInfo;
Prabir Pradhan124ea442022-10-28 20:27:44 +00002075};
2076
2077using StylusButtonIntegrationTestTypes =
2078 ::testing::Types<UinputTouchScreen, UinputExternalStylus, UinputExternalStylusWithPressure>;
2079TYPED_TEST_SUITE(StylusButtonIntegrationTest, StylusButtonIntegrationTestTypes);
2080
Prabir Pradhan39d60aa2023-07-13 22:01:04 +00002081TYPED_TEST(StylusButtonIntegrationTest, StylusButtonsGenerateKeyEvents) {
Prabir Pradhan124ea442022-10-28 20:27:44 +00002082 const auto stylusId = TestFixture::mStylusInfo.getId();
2083
2084 TestFixture::mStylus->pressKey(BTN_STYLUS);
2085 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
2086 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
2087 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2088
2089 TestFixture::mStylus->releaseKey(BTN_STYLUS);
2090 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhane1a41a82022-10-14 18:06:50 +00002091 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002092 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
Prabir Pradhane1a41a82022-10-14 18:06:50 +00002093}
2094
Prabir Pradhan39d60aa2023-07-13 22:01:04 +00002095TYPED_TEST(StylusButtonIntegrationTest, StylusButtonsSurroundingTouchGesture) {
Prabir Pradhan124ea442022-10-28 20:27:44 +00002096 const Point centerPoint = TestFixture::mTouchscreen->getCenterPoint();
2097 const auto touchscreenId = TestFixture::mTouchscreenInfo.getId();
2098 const auto stylusId = TestFixture::mStylusInfo.getId();
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002099
2100 // Press the stylus button.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002101 TestFixture::mStylus->pressKey(BTN_STYLUS);
2102 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002103 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002104 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002105
2106 // Start and finish a stylus gesture.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002107 TestFixture::mTouchscreen->sendSlot(FIRST_SLOT);
2108 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
2109 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
2110 TestFixture::mTouchscreen->sendDown(centerPoint);
2111 TestFixture::mTouchscreen->sendSync();
2112 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002113 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002114 WithToolType(ToolType::STYLUS),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002115 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY),
2116 WithDeviceId(touchscreenId))));
2117 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002118 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002119 WithToolType(ToolType::STYLUS),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002120 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY),
2121 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002122
Prabir Pradhan124ea442022-10-28 20:27:44 +00002123 TestFixture::mTouchscreen->sendTrackingId(INVALID_TRACKING_ID);
2124 TestFixture::mTouchscreen->sendSync();
2125 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002126 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002127 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002128 WithDeviceId(touchscreenId))));
2129 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002130 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002131 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002132 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002133
2134 // Release the stylus button.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002135 TestFixture::mStylus->releaseKey(BTN_STYLUS);
2136 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002137 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002138 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002139}
2140
Prabir Pradhan39d60aa2023-07-13 22:01:04 +00002141TYPED_TEST(StylusButtonIntegrationTest, StylusButtonsSurroundingHoveringTouchGesture) {
Prabir Pradhan9a561c22022-11-07 16:11:23 +00002142 const Point centerPoint = TestFixture::mTouchscreen->getCenterPoint();
2143 const auto touchscreenId = TestFixture::mTouchscreenInfo.getId();
2144 const auto stylusId = TestFixture::mStylusInfo.getId();
2145 auto toolTypeDevice =
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002146 AllOf(WithToolType(ToolType::STYLUS), WithDeviceId(touchscreenId));
Prabir Pradhan9a561c22022-11-07 16:11:23 +00002147
2148 // Press the stylus button.
2149 TestFixture::mStylus->pressKey(BTN_STYLUS);
2150 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
2151 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
2152 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2153
2154 // Start hovering with the stylus.
2155 TestFixture::mTouchscreen->sendSlot(FIRST_SLOT);
2156 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
2157 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
2158 TestFixture::mTouchscreen->sendMove(centerPoint);
2159 TestFixture::mTouchscreen->sendSync();
2160 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2161 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
2162 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2163 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2164 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
2165 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2166 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2167 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
2168 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2169
2170 // Touch down with the stylus.
2171 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
2172 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
2173 TestFixture::mTouchscreen->sendDown(centerPoint);
2174 TestFixture::mTouchscreen->sendSync();
2175 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2176 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT),
2177 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2178
2179 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2180 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
2181 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2182
2183 // Stop touching with the stylus, and start hovering.
2184 TestFixture::mTouchscreen->sendUp();
2185 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
2186 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
2187 TestFixture::mTouchscreen->sendMove(centerPoint);
2188 TestFixture::mTouchscreen->sendSync();
2189 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2190 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_UP),
2191 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2192 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2193 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
2194 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2195 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2196 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
2197 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2198
2199 // Stop hovering.
2200 TestFixture::mTouchscreen->sendTrackingId(INVALID_TRACKING_ID);
2201 TestFixture::mTouchscreen->sendSync();
2202 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2203 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
2204 WithButtonState(0))));
2205 // TODO(b/257971675): Fix inconsistent button state when exiting hover.
2206 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2207 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT),
2208 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2209
2210 // Release the stylus button.
2211 TestFixture::mStylus->releaseKey(BTN_STYLUS);
2212 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
2213 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
2214 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2215}
2216
Prabir Pradhan39d60aa2023-07-13 22:01:04 +00002217TYPED_TEST(StylusButtonIntegrationTest, StylusButtonsWithinTouchGesture) {
Prabir Pradhan124ea442022-10-28 20:27:44 +00002218 const Point centerPoint = TestFixture::mTouchscreen->getCenterPoint();
2219 const auto touchscreenId = TestFixture::mTouchscreenInfo.getId();
2220 const auto stylusId = TestFixture::mStylusInfo.getId();
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002221
2222 // Start a stylus gesture.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002223 TestFixture::mTouchscreen->sendSlot(FIRST_SLOT);
2224 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
2225 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
2226 TestFixture::mTouchscreen->sendDown(centerPoint);
2227 TestFixture::mTouchscreen->sendSync();
2228 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002229 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002230 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002231 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002232
2233 // Press and release a stylus button. Each change in button state also generates a MOVE event.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002234 TestFixture::mStylus->pressKey(BTN_STYLUS);
2235 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002236 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002237 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2238 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002239 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002240 WithToolType(ToolType::STYLUS),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002241 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY),
2242 WithDeviceId(touchscreenId))));
2243 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002244 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002245 WithToolType(ToolType::STYLUS),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002246 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY),
2247 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002248
Prabir Pradhan124ea442022-10-28 20:27:44 +00002249 TestFixture::mStylus->releaseKey(BTN_STYLUS);
2250 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002251 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002252 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2253 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002254 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002255 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002256 WithDeviceId(touchscreenId))));
2257 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002258 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002259 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002260 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002261
2262 // Finish the stylus gesture.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002263 TestFixture::mTouchscreen->sendTrackingId(INVALID_TRACKING_ID);
2264 TestFixture::mTouchscreen->sendSync();
2265 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002266 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002267 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002268 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002269}
2270
Prabir Pradhan39d60aa2023-07-13 22:01:04 +00002271TYPED_TEST(StylusButtonIntegrationTest, StylusButtonMotionEventsDisabled) {
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002272 TestFixture::mFakePolicy->setStylusButtonMotionEventsEnabled(false);
2273 TestFixture::mReader->requestRefreshConfiguration(
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002274 InputReaderConfiguration::Change::STYLUS_BUTTON_REPORTING);
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002275
2276 const Point centerPoint = TestFixture::mTouchscreen->getCenterPoint();
2277 const auto touchscreenId = TestFixture::mTouchscreenInfo.getId();
2278 const auto stylusId = TestFixture::mStylusInfo.getId();
2279
2280 // Start a stylus gesture. By the time this event is processed, the configuration change that
2281 // was requested is guaranteed to be completed.
2282 TestFixture::mTouchscreen->sendSlot(FIRST_SLOT);
2283 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
2284 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
2285 TestFixture::mTouchscreen->sendDown(centerPoint);
2286 TestFixture::mTouchscreen->sendSync();
2287 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2288 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002289 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002290 WithDeviceId(touchscreenId))));
2291
2292 // Press and release a stylus button. Each change only generates a MOVE motion event.
2293 // Key events are unaffected.
2294 TestFixture::mStylus->pressKey(BTN_STYLUS);
2295 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
2296 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
2297 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2298 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2299 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002300 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002301 WithDeviceId(touchscreenId))));
2302
2303 TestFixture::mStylus->releaseKey(BTN_STYLUS);
2304 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
2305 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
2306 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2307 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2308 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002309 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002310 WithDeviceId(touchscreenId))));
2311
2312 // Finish the stylus gesture.
2313 TestFixture::mTouchscreen->sendTrackingId(INVALID_TRACKING_ID);
2314 TestFixture::mTouchscreen->sendSync();
2315 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2316 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002317 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002318 WithDeviceId(touchscreenId))));
2319}
2320
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002321// --- ExternalStylusIntegrationTest ---
2322
2323// Verify the behavior of an external stylus. An external stylus can report pressure or button
2324// data independently of the touchscreen, which is then sent as a MotionEvent as part of an
2325// ongoing stylus gesture that is being emitted by the touchscreen.
Arpit Singh440bf652023-08-09 09:23:43 +00002326using ExternalStylusIntegrationTest = BaseTouchIntegrationTest;
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002327
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00002328TEST_F(ExternalStylusIntegrationTest, ExternalStylusConnectionChangesTouchscreenSource) {
2329 // Create an external stylus capable of reporting pressure data that
2330 // should be fused with a touch pointer.
2331 std::unique_ptr<UinputExternalStylusWithPressure> stylus =
2332 createUinputDevice<UinputExternalStylusWithPressure>();
2333 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2334 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2335 const auto stylusInfo = findDeviceByName(stylus->getName());
2336 ASSERT_TRUE(stylusInfo);
2337
2338 // Connecting an external stylus changes the source of the touchscreen.
2339 const auto deviceInfo = findDeviceByName(mDevice->getName());
2340 ASSERT_TRUE(deviceInfo);
2341 ASSERT_TRUE(isFromSource(deviceInfo->getSources(), STYLUS_FUSION_SOURCE));
2342}
2343
Prabir Pradhan39d60aa2023-07-13 22:01:04 +00002344TEST_F(ExternalStylusIntegrationTest, FusedExternalStylusPressureReported) {
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002345 const Point centerPoint = mDevice->getCenterPoint();
2346
2347 // Create an external stylus capable of reporting pressure data that
2348 // should be fused with a touch pointer.
2349 std::unique_ptr<UinputExternalStylusWithPressure> stylus =
2350 createUinputDevice<UinputExternalStylusWithPressure>();
2351 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2352 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2353 const auto stylusInfo = findDeviceByName(stylus->getName());
2354 ASSERT_TRUE(stylusInfo);
2355
2356 ASSERT_EQ(AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_KEYBOARD, stylusInfo->getSources());
2357
2358 const auto touchscreenId = mDeviceInfo.getId();
2359
2360 // Set a pressure value on the stylus. It doesn't generate any events.
2361 const auto& RAW_PRESSURE_MAX = UinputExternalStylusWithPressure::RAW_PRESSURE_MAX;
2362 stylus->setPressure(100);
2363 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2364
2365 // Start a finger gesture, and ensure it shows up as stylus gesture
2366 // with the pressure set by the external stylus.
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002367 mDevice->sendSlot(FIRST_SLOT);
Chris Ye1b0c7342020-07-28 21:57:03 -07002368 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002369 mDevice->sendToolType(MT_TOOL_FINGER);
Prabir Pradhanb54ffb22022-10-27 18:03:34 +00002370 mDevice->sendDown(centerPoint);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002371 mDevice->sendSync();
2372 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00002373 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithToolType(ToolType::STYLUS),
2374 WithButtonState(0), WithSource(STYLUS_FUSION_SOURCE), WithDeviceId(touchscreenId),
2375 WithPressure(100.f / RAW_PRESSURE_MAX))));
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002376
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002377 // Change the pressure on the external stylus, and ensure the touchscreen generates a MOVE
2378 // event with the updated pressure.
2379 stylus->setPressure(200);
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +00002380 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00002381 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithToolType(ToolType::STYLUS),
2382 WithButtonState(0), WithSource(STYLUS_FUSION_SOURCE), WithDeviceId(touchscreenId),
2383 WithPressure(200.f / RAW_PRESSURE_MAX))));
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002384
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002385 // The external stylus did not generate any events.
2386 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2387 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasNotCalled());
2388}
2389
Prabir Pradhan39d60aa2023-07-13 22:01:04 +00002390TEST_F(ExternalStylusIntegrationTest, FusedExternalStylusPressureNotReported) {
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002391 const Point centerPoint = mDevice->getCenterPoint();
2392
2393 // Create an external stylus capable of reporting pressure data that
2394 // should be fused with a touch pointer.
2395 std::unique_ptr<UinputExternalStylusWithPressure> stylus =
2396 createUinputDevice<UinputExternalStylusWithPressure>();
2397 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2398 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2399 const auto stylusInfo = findDeviceByName(stylus->getName());
2400 ASSERT_TRUE(stylusInfo);
2401
2402 ASSERT_EQ(AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_KEYBOARD, stylusInfo->getSources());
2403
2404 const auto touchscreenId = mDeviceInfo.getId();
2405
2406 // Set a pressure value of 0 on the stylus. It doesn't generate any events.
2407 const auto& RAW_PRESSURE_MAX = UinputExternalStylusWithPressure::RAW_PRESSURE_MAX;
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00002408 // Send a non-zero value first to prevent the kernel from consuming the zero event.
2409 stylus->setPressure(100);
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002410 stylus->setPressure(0);
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00002411 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002412
2413 // Start a finger gesture. The touch device will withhold generating any touches for
2414 // up to 72 milliseconds while waiting for pressure data from the external stylus.
2415 mDevice->sendSlot(FIRST_SLOT);
2416 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2417 mDevice->sendToolType(MT_TOOL_FINGER);
2418 mDevice->sendDown(centerPoint);
Prabir Pradhanafb7d612024-01-08 22:45:24 +00002419 const auto syncTime = std::chrono::system_clock::now();
2420 // After 72 ms, the event *will* be generated. If we wait the full 72 ms to check that NO event
2421 // is generated in that period, there will be a race condition between the event being generated
2422 // and the test's wait timeout expiring. Thus, we wait for a shorter duration in the test, which
2423 // will reduce the liklihood of the race condition occurring.
2424 const auto waitUntilTimeForNoEvent =
2425 syncTime + std::chrono::milliseconds(ns2ms(EXTERNAL_STYLUS_DATA_TIMEOUT / 2));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07002426 mDevice->sendSync();
Prabir Pradhanafb7d612024-01-08 22:45:24 +00002427 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled(waitUntilTimeForNoEvent));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002428
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002429 // Since the external stylus did not report a pressure value within the timeout,
2430 // it shows up as a finger pointer.
Prabir Pradhanafb7d612024-01-08 22:45:24 +00002431 const auto waitUntilTimeForEvent = syncTime +
2432 std::chrono::milliseconds(ns2ms(EXTERNAL_STYLUS_DATA_TIMEOUT)) + EVENT_HAPPENED_TIMEOUT;
2433 ASSERT_NO_FATAL_FAILURE(
2434 mTestListener->assertNotifyMotionWasCalled(AllOf(WithMotionAction(
2435 AMOTION_EVENT_ACTION_DOWN),
2436 WithSource(AINPUT_SOURCE_TOUCHSCREEN |
2437 AINPUT_SOURCE_STYLUS),
2438 WithToolType(ToolType::FINGER),
2439 WithDeviceId(touchscreenId),
2440 WithPressure(1.f)),
2441 waitUntilTimeForEvent));
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002442
2443 // Change the pressure on the external stylus. Since the pressure was not present at the start
2444 // of the gesture, it is ignored for now.
2445 stylus->setPressure(200);
2446 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2447
2448 // Finish the finger gesture.
Michael Wrightd02c5b62014-02-10 15:10:22 -08002449 mDevice->sendTrackingId(INVALID_TRACKING_ID);
2450 mDevice->sendSync();
2451 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2452 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00002453 WithSource(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002454 WithToolType(ToolType::FINGER))));
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002455
2456 // Start a new gesture. Since we have a valid pressure value, it shows up as a stylus.
2457 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2458 mDevice->sendToolType(MT_TOOL_FINGER);
2459 mDevice->sendDown(centerPoint);
2460 mDevice->sendSync();
2461 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00002462 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithSource(STYLUS_FUSION_SOURCE),
2463 WithToolType(ToolType::STYLUS), WithButtonState(0), WithDeviceId(touchscreenId),
2464 WithPressure(200.f / RAW_PRESSURE_MAX))));
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002465
2466 // The external stylus did not generate any events.
2467 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2468 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasNotCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002469}
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002470
Prabir Pradhan39d60aa2023-07-13 22:01:04 +00002471TEST_F(ExternalStylusIntegrationTest, UnfusedExternalStylus) {
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00002472 const Point centerPoint = mDevice->getCenterPoint();
2473
2474 // Create an external stylus device that does not support pressure. It should not affect any
2475 // touch pointers.
2476 std::unique_ptr<UinputExternalStylus> stylus = createUinputDevice<UinputExternalStylus>();
2477 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2478 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2479 const auto stylusInfo = findDeviceByName(stylus->getName());
2480 ASSERT_TRUE(stylusInfo);
2481
2482 ASSERT_EQ(AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_KEYBOARD, stylusInfo->getSources());
2483
2484 const auto touchscreenId = mDeviceInfo.getId();
2485
2486 // Start a finger gesture and ensure a finger pointer is generated for it, without waiting for
2487 // pressure data from the external stylus.
2488 mDevice->sendSlot(FIRST_SLOT);
2489 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2490 mDevice->sendToolType(MT_TOOL_FINGER);
2491 mDevice->sendDown(centerPoint);
2492 auto waitUntil = std::chrono::system_clock::now() +
2493 std::chrono::milliseconds(ns2ms(EXTERNAL_STYLUS_DATA_TIMEOUT));
2494 mDevice->sendSync();
2495 ASSERT_NO_FATAL_FAILURE(
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00002496 mTestListener->assertNotifyMotionWasCalled(AllOf(WithMotionAction(
2497 AMOTION_EVENT_ACTION_DOWN),
2498 WithToolType(ToolType::FINGER),
2499 WithSource(AINPUT_SOURCE_TOUCHSCREEN |
2500 AINPUT_SOURCE_STYLUS),
2501 WithButtonState(0),
2502 WithDeviceId(touchscreenId),
2503 WithPressure(1.f)),
2504 waitUntil));
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00002505
2506 // The external stylus did not generate any events.
2507 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2508 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasNotCalled());
2509}
2510
Michael Wrightd02c5b62014-02-10 15:10:22 -08002511// --- InputDeviceTest ---
2512class InputDeviceTest : public testing::Test {
2513protected:
2514 static const char* DEVICE_NAME;
2515 static const char* DEVICE_LOCATION;
2516 static const int32_t DEVICE_ID;
2517 static const int32_t DEVICE_GENERATION;
2518 static const int32_t DEVICE_CONTROLLER_NUMBER;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002519 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002520 static const int32_t EVENTHUB_ID;
2521 static const std::string DEVICE_BLUETOOTH_ADDRESS;
2522
2523 std::shared_ptr<FakeEventHub> mFakeEventHub;
2524 sp<FakeInputReaderPolicy> mFakePolicy;
2525 std::unique_ptr<TestInputListener> mFakeListener;
2526 std::unique_ptr<InstrumentedInputReader> mReader;
2527 std::shared_ptr<InputDevice> mDevice;
2528
2529 void SetUp() override {
2530 mFakeEventHub = std::make_unique<FakeEventHub>();
2531 mFakePolicy = sp<FakeInputReaderPolicy>::make();
2532 mFakeListener = std::make_unique<TestInputListener>();
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002533 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Michael Wrightd02c5b62014-02-10 15:10:22 -08002534 *mFakeListener);
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002535 InputDeviceIdentifier identifier;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002536 identifier.name = DEVICE_NAME;
2537 identifier.location = DEVICE_LOCATION;
2538 identifier.bluetoothAddress = DEVICE_BLUETOOTH_ADDRESS;
2539 mDevice = std::make_shared<InputDevice>(mReader->getContext(), DEVICE_ID, DEVICE_GENERATION,
2540 identifier);
2541 mReader->pushNextDevice(mDevice);
2542 mFakeEventHub->addDevice(EVENTHUB_ID, DEVICE_NAME, ftl::Flags<InputDeviceClass>(0));
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002543 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002544 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002545
2546 void TearDown() override {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002547 mFakeListener.reset();
2548 mFakePolicy.clear();
2549 }
2550};
2551
2552const char* InputDeviceTest::DEVICE_NAME = "device";
2553const char* InputDeviceTest::DEVICE_LOCATION = "USB1";
2554const int32_t InputDeviceTest::DEVICE_ID = END_RESERVED_ID + 1000;
2555const int32_t InputDeviceTest::DEVICE_GENERATION = 2;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002556const int32_t InputDeviceTest::DEVICE_CONTROLLER_NUMBER = 0;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002557const ftl::Flags<InputDeviceClass> InputDeviceTest::DEVICE_CLASSES =
2558 InputDeviceClass::KEYBOARD | InputDeviceClass::TOUCH | InputDeviceClass::JOYSTICK;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002559const int32_t InputDeviceTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002560const std::string InputDeviceTest::DEVICE_BLUETOOTH_ADDRESS = "11:AA:22:BB:33:CC";
2561
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002562TEST_F(InputDeviceTest, ImmutableProperties) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002563 ASSERT_EQ(DEVICE_ID, mDevice->getId());
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002564 ASSERT_STREQ(DEVICE_NAME, mDevice->getName().c_str());
2565 ASSERT_EQ(ftl::Flags<InputDeviceClass>(0), mDevice->getClasses());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002566}
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002567
Michael Wrightd02c5b62014-02-10 15:10:22 -08002568TEST_F(InputDeviceTest, WhenDeviceCreated_EnabledIsFalse) {
2569 ASSERT_EQ(mDevice->isEnabled(), false);
2570}
2571
2572TEST_F(InputDeviceTest, WhenNoMappersAreRegistered_DeviceIsIgnored) {
2573 // Configuration.
2574 InputReaderConfiguration config;
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002575 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, config, /*changes=*/{});
Michael Wrightd02c5b62014-02-10 15:10:22 -08002576
2577 // Reset.
2578 unused += mDevice->reset(ARBITRARY_TIME);
2579
2580 NotifyDeviceResetArgs resetArgs;
2581 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2582 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2583 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2584
2585 // Metadata.
2586 ASSERT_TRUE(mDevice->isIgnored());
2587 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mDevice->getSources());
2588
2589 InputDeviceInfo info = mDevice->getDeviceInfo();
2590 ASSERT_EQ(DEVICE_ID, info.getId());
2591 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
2592 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NONE, info.getKeyboardType());
2593 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, info.getSources());
2594
2595 // State queries.
2596 ASSERT_EQ(0, mDevice->getMetaState());
2597
2598 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2599 << "Ignored device should return unknown key code state.";
2600 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2601 << "Ignored device should return unknown scan code state.";
2602 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 0))
2603 << "Ignored device should return unknown switch state.";
2604
2605 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B};
2606 uint8_t flags[2] = { 0, 1 };
2607 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, keyCodes, flags))
2608 << "Ignored device should never mark any key codes.";
2609 ASSERT_EQ(0, flags[0]) << "Flag for unsupported key should be unchanged.";
2610 ASSERT_EQ(1, flags[1]) << "Flag for unsupported key should be unchanged.";
2611}
2612
2613TEST_F(InputDeviceTest, WhenMappersAreRegistered_DeviceIsNotIgnoredAndForwardsRequestsToMappers) {
2614 // Configuration.
2615 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "key", "value");
2616
2617 FakeInputMapper& mapper1 =
Arpit Singh8e6fb252023-04-06 11:49:17 +00002618 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2619 AINPUT_SOURCE_KEYBOARD);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002620 mapper1.setKeyboardType(AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2621 mapper1.setMetaState(AMETA_ALT_ON);
2622 mapper1.addSupportedKeyCode(AKEYCODE_A);
2623 mapper1.addSupportedKeyCode(AKEYCODE_B);
2624 mapper1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
2625 mapper1.setKeyCodeState(AKEYCODE_B, AKEY_STATE_UP);
2626 mapper1.setScanCodeState(2, AKEY_STATE_DOWN);
2627 mapper1.setScanCodeState(3, AKEY_STATE_UP);
2628 mapper1.setSwitchState(4, AKEY_STATE_DOWN);
2629
2630 FakeInputMapper& mapper2 =
Arpit Singh8e6fb252023-04-06 11:49:17 +00002631 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2632 AINPUT_SOURCE_TOUCHSCREEN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002633 mapper2.setMetaState(AMETA_SHIFT_ON);
2634
2635 InputReaderConfiguration config;
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002636 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, config, /*changes=*/{});
Michael Wrightd02c5b62014-02-10 15:10:22 -08002637
Harry Cuttsf13161a2023-03-08 14:15:49 +00002638 std::optional<std::string> propertyValue = mDevice->getConfiguration().getString("key");
2639 ASSERT_TRUE(propertyValue.has_value())
Michael Wrightd02c5b62014-02-10 15:10:22 -08002640 << "Device should have read configuration during configuration phase.";
Harry Cuttsf13161a2023-03-08 14:15:49 +00002641 ASSERT_EQ("value", *propertyValue);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002642
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002643 ASSERT_NO_FATAL_FAILURE(mapper1.assertConfigureWasCalled());
2644 ASSERT_NO_FATAL_FAILURE(mapper2.assertConfigureWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002645
2646 // Reset
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002647 unused += mDevice->reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002648 ASSERT_NO_FATAL_FAILURE(mapper1.assertResetWasCalled());
2649 ASSERT_NO_FATAL_FAILURE(mapper2.assertResetWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002650
2651 NotifyDeviceResetArgs resetArgs;
2652 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2653 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2654 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2655
2656 // Metadata.
2657 ASSERT_FALSE(mDevice->isIgnored());
2658 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), mDevice->getSources());
2659
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002660 InputDeviceInfo info = mDevice->getDeviceInfo();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002661 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002662 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002663 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_ALPHABETIC, info.getKeyboardType());
2664 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), info.getSources());
2665
2666 // State queries.
2667 ASSERT_EQ(AMETA_ALT_ON | AMETA_SHIFT_ON, mDevice->getMetaState())
2668 << "Should query mappers and combine meta states.";
2669
2670 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2671 << "Should return unknown key code state when source not supported.";
2672 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2673 << "Should return unknown scan code state when source not supported.";
2674 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2675 << "Should return unknown switch state when source not supported.";
2676
2677 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, AKEYCODE_A))
2678 << "Should query mapper when source is supported.";
2679 ASSERT_EQ(AKEY_STATE_UP, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 3))
2680 << "Should query mapper when source is supported.";
2681 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 4))
2682 << "Should query mapper when source is supported.";
2683
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002684 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2};
Michael Wrightd02c5b62014-02-10 15:10:22 -08002685 uint8_t flags[4] = { 0, 0, 0, 1 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002686 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08002687 << "Should do nothing when source is unsupported.";
2688 ASSERT_EQ(0, flags[0]) << "Flag should be unchanged when source is unsupported.";
2689 ASSERT_EQ(0, flags[1]) << "Flag should be unchanged when source is unsupported.";
2690 ASSERT_EQ(0, flags[2]) << "Flag should be unchanged when source is unsupported.";
2691 ASSERT_EQ(1, flags[3]) << "Flag should be unchanged when source is unsupported.";
2692
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002693 ASSERT_TRUE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08002694 << "Should query mapper when source is supported.";
2695 ASSERT_EQ(1, flags[0]) << "Flag for supported key should be set.";
2696 ASSERT_EQ(1, flags[1]) << "Flag for supported key should be set.";
2697 ASSERT_EQ(0, flags[2]) << "Flag for unsupported key should be unchanged.";
2698 ASSERT_EQ(1, flags[3]) << "Flag for unsupported key should be unchanged.";
2699
2700 // Event handling.
2701 RawEvent event;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002702 event.deviceId = EVENTHUB_ID;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002703 unused += mDevice->process(&event, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002704
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002705 ASSERT_NO_FATAL_FAILURE(mapper1.assertProcessWasCalled());
2706 ASSERT_NO_FATAL_FAILURE(mapper2.assertProcessWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002707}
2708
Yeabkal Wubshitb1b96db2024-01-24 12:47:00 -08002709TEST_F(InputDeviceTest, Configure_SmoothScrollViewBehaviorNotSet) {
2710 // Set some behavior to force the configuration to be update.
2711 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "device.wake", "1");
2712 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2713 AINPUT_SOURCE_KEYBOARD);
2714
2715 std::list<NotifyArgs> unused =
2716 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2717 /*changes=*/{});
2718
2719 ASSERT_FALSE(mDevice->getDeviceInfo().getViewBehavior().shouldSmoothScroll.has_value());
2720}
2721
2722TEST_F(InputDeviceTest, Configure_SmoothScrollViewBehaviorEnabled) {
2723 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "device.viewBehavior_smoothScroll", "1");
2724 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2725 AINPUT_SOURCE_KEYBOARD);
2726
2727 std::list<NotifyArgs> unused =
2728 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2729 /*changes=*/{});
2730
2731 ASSERT_TRUE(mDevice->getDeviceInfo().getViewBehavior().shouldSmoothScroll.value_or(false));
2732}
2733
Yeabkal Wubshite03e8b12023-06-27 16:23:12 -07002734TEST_F(InputDeviceTest, WakeDevice_AddsWakeFlagToProcessNotifyArgs) {
2735 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "device.wake", "1");
2736 FakeInputMapper& mapper =
2737 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2738 AINPUT_SOURCE_KEYBOARD);
2739 NotifyMotionArgs args1;
2740 NotifySwitchArgs args2;
2741 NotifyKeyArgs args3;
2742 mapper.setProcessResult({args1, args2, args3});
2743
2744 InputReaderConfiguration config;
2745 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, config, /*changes=*/{});
2746
2747 RawEvent event;
2748 event.deviceId = EVENTHUB_ID;
2749 std::list<NotifyArgs> notifyArgs = mDevice->process(&event, 1);
2750
2751 for (auto& arg : notifyArgs) {
2752 if (const auto notifyMotionArgs = std::get_if<NotifyMotionArgs>(&arg)) {
2753 ASSERT_EQ(POLICY_FLAG_WAKE, notifyMotionArgs->policyFlags);
2754 } else if (const auto notifySwitchArgs = std::get_if<NotifySwitchArgs>(&arg)) {
2755 ASSERT_EQ(POLICY_FLAG_WAKE, notifySwitchArgs->policyFlags);
2756 } else if (const auto notifyKeyArgs = std::get_if<NotifyKeyArgs>(&arg)) {
2757 ASSERT_EQ(POLICY_FLAG_WAKE, notifyKeyArgs->policyFlags);
2758 }
2759 }
2760}
2761
2762TEST_F(InputDeviceTest, NotWakeDevice_DoesNotAddWakeFlagToProcessNotifyArgs) {
2763 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "device.wake", "0");
2764 FakeInputMapper& mapper =
2765 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2766 AINPUT_SOURCE_KEYBOARD);
2767 NotifyMotionArgs args;
2768 mapper.setProcessResult({args});
2769
2770 InputReaderConfiguration config;
2771 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, config, /*changes=*/{});
2772
2773 RawEvent event;
2774 event.deviceId = EVENTHUB_ID;
2775 std::list<NotifyArgs> notifyArgs = mDevice->process(&event, 1);
2776
2777 // POLICY_FLAG_WAKE is not added to the NotifyArgs.
2778 ASSERT_EQ(0u, std::get<NotifyMotionArgs>(notifyArgs.front()).policyFlags);
2779}
2780
2781TEST_F(InputDeviceTest, NotWakeDevice_DoesNotRemoveExistingWakeFlagFromProcessNotifyArgs) {
2782 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "device.wake", "0");
2783 FakeInputMapper& mapper =
2784 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2785 AINPUT_SOURCE_KEYBOARD);
2786 NotifyMotionArgs args;
2787 args.policyFlags = POLICY_FLAG_WAKE;
2788 mapper.setProcessResult({args});
2789
2790 InputReaderConfiguration config;
2791 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, config, /*changes=*/{});
2792
2793 RawEvent event;
2794 event.deviceId = EVENTHUB_ID;
2795 std::list<NotifyArgs> notifyArgs = mDevice->process(&event, 1);
2796
2797 // The POLICY_FLAG_WAKE is preserved, despite the device being a non-wake device.
2798 ASSERT_EQ(POLICY_FLAG_WAKE, std::get<NotifyMotionArgs>(notifyArgs.front()).policyFlags);
2799}
2800
Arthur Hung2c9a3342019-07-23 14:18:59 +08002801// A single input device is associated with a specific display. Check that:
2802// 1. Device is disabled if the viewport corresponding to the associated display is not found
Arpit Singh48189772023-05-30 14:12:49 +00002803// 2. Device is disabled when configure API is called
Arthur Hung2c9a3342019-07-23 14:18:59 +08002804TEST_F(InputDeviceTest, Configure_AssignsDisplayPort) {
Arpit Singh8e6fb252023-04-06 11:49:17 +00002805 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2806 AINPUT_SOURCE_TOUCHSCREEN);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002807
2808 // First Configuration.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002809 std::list<NotifyArgs> unused =
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002810 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2811 /*changes=*/{});
Arthur Hung2c9a3342019-07-23 14:18:59 +08002812
2813 // Device should be enabled by default.
2814 ASSERT_TRUE(mDevice->isEnabled());
2815
2816 // Prepare associated info.
2817 constexpr uint8_t hdmi = 1;
2818 const std::string UNIQUE_ID = "local:1";
2819
2820 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002821 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002822 InputReaderConfiguration::Change::DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002823 // Device should be disabled because it is associated with a specific display via
2824 // input port <-> display port association, but the corresponding display is not found
2825 ASSERT_FALSE(mDevice->isEnabled());
2826
2827 // Prepare displays.
2828 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +00002829 ui::ROTATION_0, /*isActive=*/true, UNIQUE_ID, hdmi,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002830 ViewportType::INTERNAL);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002831 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002832 InputReaderConfiguration::Change::DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002833 ASSERT_TRUE(mDevice->isEnabled());
2834
2835 // Device should be disabled after set disable.
2836 mFakePolicy->addDisabledDevice(mDevice->getId());
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002837 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002838 InputReaderConfiguration::Change::ENABLED_STATE);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002839 ASSERT_FALSE(mDevice->isEnabled());
2840
2841 // Device should still be disabled even found the associated display.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002842 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002843 InputReaderConfiguration::Change::DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002844 ASSERT_FALSE(mDevice->isEnabled());
2845}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002846
Christine Franks1ba71cc2021-04-07 14:37:42 -07002847TEST_F(InputDeviceTest, Configure_AssignsDisplayUniqueId) {
2848 // Device should be enabled by default.
2849 mFakePolicy->clearViewports();
Arpit Singh8e6fb252023-04-06 11:49:17 +00002850 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2851 AINPUT_SOURCE_KEYBOARD);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002852 std::list<NotifyArgs> unused =
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002853 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2854 /*changes=*/{});
Christine Franks1ba71cc2021-04-07 14:37:42 -07002855 ASSERT_TRUE(mDevice->isEnabled());
2856
2857 // Device should be disabled because it is associated with a specific display, but the
2858 // corresponding display is not found.
Christine Franks2a2293c2022-01-18 11:51:16 -08002859 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002860 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002861 InputReaderConfiguration::Change::DISPLAY_INFO);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002862 ASSERT_FALSE(mDevice->isEnabled());
2863
2864 // Device should be enabled when a display is found.
2865 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +00002866 ui::ROTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
Christine Franks1ba71cc2021-04-07 14:37:42 -07002867 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002868 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002869 InputReaderConfiguration::Change::DISPLAY_INFO);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002870 ASSERT_TRUE(mDevice->isEnabled());
2871
2872 // Device should be disabled after set disable.
2873 mFakePolicy->addDisabledDevice(mDevice->getId());
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002874 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002875 InputReaderConfiguration::Change::ENABLED_STATE);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002876 ASSERT_FALSE(mDevice->isEnabled());
2877
2878 // Device should still be disabled even found the associated display.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002879 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002880 InputReaderConfiguration::Change::DISPLAY_INFO);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002881 ASSERT_FALSE(mDevice->isEnabled());
2882}
2883
Christine Franks2a2293c2022-01-18 11:51:16 -08002884TEST_F(InputDeviceTest, Configure_UniqueId_CorrectlyMatches) {
2885 mFakePolicy->clearViewports();
Arpit Singh8e6fb252023-04-06 11:49:17 +00002886 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2887 AINPUT_SOURCE_KEYBOARD);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002888 std::list<NotifyArgs> unused =
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002889 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2890 /*changes=*/{});
Christine Franks2a2293c2022-01-18 11:51:16 -08002891
Christine Franks2a2293c2022-01-18 11:51:16 -08002892 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
2893 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +00002894 ui::ROTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
Christine Franks2a2293c2022-01-18 11:51:16 -08002895 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002896 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002897 InputReaderConfiguration::Change::DISPLAY_INFO);
Christine Franks2a2293c2022-01-18 11:51:16 -08002898 ASSERT_EQ(DISPLAY_UNIQUE_ID, mDevice->getAssociatedDisplayUniqueId());
2899}
2900
Siarhei Vishniakou30feb8c2022-09-28 10:48:29 -07002901/**
2902 * This test reproduces a crash caused by a dangling reference that remains after device is added
2903 * and removed. The reference is accessed in InputDevice::dump(..);
2904 */
2905TEST_F(InputDeviceTest, DumpDoesNotCrash) {
2906 constexpr int32_t TEST_EVENTHUB_ID = 10;
2907 mFakeEventHub->addDevice(TEST_EVENTHUB_ID, "Test EventHub device", InputDeviceClass::BATTERY);
2908
Harry Cutts33476232023-01-30 19:57:29 +00002909 InputDevice device(mReader->getContext(), /*id=*/1, /*generation=*/2, /*identifier=*/{});
Arpit Singh82f29a12023-06-13 15:05:53 +00002910 auto _ = device.addEventHubDevice(ARBITRARY_TIME, TEST_EVENTHUB_ID,
2911 mFakePolicy->getReaderConfiguration());
Siarhei Vishniakou30feb8c2022-09-28 10:48:29 -07002912 device.removeEventHubDevice(TEST_EVENTHUB_ID);
2913 std::string dumpStr, eventHubDevStr;
2914 device.dump(dumpStr, eventHubDevStr);
2915}
2916
Prabir Pradhanb54ffb22022-10-27 18:03:34 +00002917TEST_F(InputDeviceTest, GetBluetoothAddress) {
2918 const auto& address = mReader->getBluetoothAddress(DEVICE_ID);
2919 ASSERT_TRUE(address);
2920 ASSERT_EQ(DEVICE_BLUETOOTH_ADDRESS, *address);
2921}
2922
Prabir Pradhanf8d9e442023-12-06 22:06:13 +00002923TEST_F(InputDeviceTest, KernelBufferOverflowResetsMappers) {
2924 mFakePolicy->clearViewports();
2925 FakeInputMapper& mapper =
2926 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2927 AINPUT_SOURCE_KEYBOARD);
2928 std::list<NotifyArgs> unused =
2929 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2930 /*changes=*/{});
2931
2932 mapper.assertConfigureWasCalled();
2933 mapper.assertResetWasNotCalled();
2934
Siarhei Vishniakou1ff00cc2023-12-13 16:12:13 -08002935 RawEvent event{.when = ARBITRARY_TIME,
Prabir Pradhanf8d9e442023-12-06 22:06:13 +00002936 .readTime = ARBITRARY_TIME,
Siarhei Vishniakou1ff00cc2023-12-13 16:12:13 -08002937 .deviceId = EVENTHUB_ID,
Prabir Pradhanf8d9e442023-12-06 22:06:13 +00002938 .type = EV_SYN,
2939 .code = SYN_REPORT,
2940 .value = 0};
2941
2942 // Events are processed normally.
2943 unused = mDevice->process(&event, /*count=*/1);
2944 mapper.assertProcessWasCalled();
2945
2946 // Simulate a kernel buffer overflow, which generates a SYN_DROPPED event.
Prabir Pradhanf8d9e442023-12-06 22:06:13 +00002947 event.type = EV_SYN;
2948 event.code = SYN_DROPPED;
2949 event.value = 0;
2950 unused = mDevice->process(&event, /*count=*/1);
2951 mapper.assertProcessWasNotCalled();
Prabir Pradhanf8d9e442023-12-06 22:06:13 +00002952
2953 // All events until the next SYN_REPORT should be dropped.
2954 event.type = EV_KEY;
2955 event.code = KEY_A;
2956 event.value = 1;
2957 unused = mDevice->process(&event, /*count=*/1);
2958 mapper.assertProcessWasNotCalled();
2959
2960 // We get the SYN_REPORT event now, which is not forwarded to mappers.
Arpit Singh4b4a4572023-11-24 18:19:56 +00002961 // This should reset the mapper.
Prabir Pradhanf8d9e442023-12-06 22:06:13 +00002962 event.type = EV_SYN;
2963 event.code = SYN_REPORT;
2964 event.value = 0;
2965 unused = mDevice->process(&event, /*count=*/1);
2966 mapper.assertProcessWasNotCalled();
Arpit Singh4b4a4572023-11-24 18:19:56 +00002967 mapper.assertResetWasCalled();
Prabir Pradhanf8d9e442023-12-06 22:06:13 +00002968
2969 // The mapper receives events normally now.
2970 event.type = EV_KEY;
2971 event.code = KEY_B;
2972 event.value = 1;
2973 unused = mDevice->process(&event, /*count=*/1);
2974 mapper.assertProcessWasCalled();
2975}
2976
Michael Wrightd02c5b62014-02-10 15:10:22 -08002977// --- SwitchInputMapperTest ---
2978
2979class SwitchInputMapperTest : public InputMapperTest {
2980protected:
2981};
2982
2983TEST_F(SwitchInputMapperTest, GetSources) {
Arpit Singhdf992eb2023-04-26 16:12:10 +00002984 SwitchInputMapper& mapper = constructAndAddMapper<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002985
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002986 ASSERT_EQ(uint32_t(AINPUT_SOURCE_SWITCH), mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002987}
2988
2989TEST_F(SwitchInputMapperTest, GetSwitchState) {
Arpit Singhdf992eb2023-04-26 16:12:10 +00002990 SwitchInputMapper& mapper = constructAndAddMapper<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002991
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002992 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002993 ASSERT_EQ(1, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002994
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002995 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002996 ASSERT_EQ(0, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002997}
2998
2999TEST_F(SwitchInputMapperTest, Process) {
Arpit Singhdf992eb2023-04-26 16:12:10 +00003000 SwitchInputMapper& mapper = constructAndAddMapper<SwitchInputMapper>();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003001 std::list<NotifyArgs> out;
3002 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_LID, 1);
3003 ASSERT_TRUE(out.empty());
3004 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_JACK_PHYSICAL_INSERT, 1);
3005 ASSERT_TRUE(out.empty());
3006 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_HEADPHONE_INSERT, 0);
3007 ASSERT_TRUE(out.empty());
3008 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003009
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003010 ASSERT_EQ(1u, out.size());
3011 const NotifySwitchArgs& args = std::get<NotifySwitchArgs>(*out.begin());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003012 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
Dan Albert1bd2fc02016-02-02 15:11:57 -08003013 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT), args.switchValues);
3014 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT) | (1 << SW_HEADPHONE_INSERT),
Michael Wrightd02c5b62014-02-10 15:10:22 -08003015 args.switchMask);
3016 ASSERT_EQ(uint32_t(0), args.policyFlags);
3017}
3018
Chris Ye87143712020-11-10 05:05:58 +00003019// --- VibratorInputMapperTest ---
3020class VibratorInputMapperTest : public InputMapperTest {
3021protected:
3022 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::VIBRATOR); }
3023};
3024
3025TEST_F(VibratorInputMapperTest, GetSources) {
Arpit Singh0f26b302023-04-26 16:23:13 +00003026 VibratorInputMapper& mapper = constructAndAddMapper<VibratorInputMapper>();
Chris Ye87143712020-11-10 05:05:58 +00003027
3028 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mapper.getSources());
3029}
3030
3031TEST_F(VibratorInputMapperTest, GetVibratorIds) {
Arpit Singh0f26b302023-04-26 16:23:13 +00003032 VibratorInputMapper& mapper = constructAndAddMapper<VibratorInputMapper>();
Chris Ye87143712020-11-10 05:05:58 +00003033
3034 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
3035}
3036
3037TEST_F(VibratorInputMapperTest, Vibrate) {
3038 constexpr uint8_t DEFAULT_AMPLITUDE = 192;
Chris Yefb552902021-02-03 17:18:37 -08003039 constexpr int32_t VIBRATION_TOKEN = 100;
Arpit Singh0f26b302023-04-26 16:23:13 +00003040 VibratorInputMapper& mapper = constructAndAddMapper<VibratorInputMapper>();
Chris Ye87143712020-11-10 05:05:58 +00003041
3042 VibrationElement pattern(2);
3043 VibrationSequence sequence(2);
3044 pattern.duration = std::chrono::milliseconds(200);
Harry Cutts33476232023-01-30 19:57:29 +00003045 pattern.channels = {{/*vibratorId=*/0, DEFAULT_AMPLITUDE / 2},
3046 {/*vibratorId=*/1, DEFAULT_AMPLITUDE}};
Chris Ye87143712020-11-10 05:05:58 +00003047 sequence.addElement(pattern);
3048 pattern.duration = std::chrono::milliseconds(500);
Harry Cutts33476232023-01-30 19:57:29 +00003049 pattern.channels = {{/*vibratorId=*/0, DEFAULT_AMPLITUDE / 4},
3050 {/*vibratorId=*/1, DEFAULT_AMPLITUDE}};
Chris Ye87143712020-11-10 05:05:58 +00003051 sequence.addElement(pattern);
3052
3053 std::vector<int64_t> timings = {0, 1};
3054 std::vector<uint8_t> amplitudes = {DEFAULT_AMPLITUDE, DEFAULT_AMPLITUDE / 2};
3055
3056 ASSERT_FALSE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08003057 // Start vibrating
Harry Cutts33476232023-01-30 19:57:29 +00003058 std::list<NotifyArgs> out = mapper.vibrate(sequence, /*repeat=*/-1, VIBRATION_TOKEN);
Chris Ye87143712020-11-10 05:05:58 +00003059 ASSERT_TRUE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08003060 // Verify vibrator state listener was notified.
3061 mReader->loopOnce();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003062 ASSERT_EQ(1u, out.size());
3063 const NotifyVibratorStateArgs& vibrateArgs = std::get<NotifyVibratorStateArgs>(*out.begin());
3064 ASSERT_EQ(DEVICE_ID, vibrateArgs.deviceId);
3065 ASSERT_TRUE(vibrateArgs.isOn);
Chris Yefb552902021-02-03 17:18:37 -08003066 // Stop vibrating
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003067 out = mapper.cancelVibrate(VIBRATION_TOKEN);
Chris Yefb552902021-02-03 17:18:37 -08003068 ASSERT_FALSE(mapper.isVibrating());
3069 // Verify vibrator state listener was notified.
3070 mReader->loopOnce();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003071 ASSERT_EQ(1u, out.size());
3072 const NotifyVibratorStateArgs& cancelArgs = std::get<NotifyVibratorStateArgs>(*out.begin());
3073 ASSERT_EQ(DEVICE_ID, cancelArgs.deviceId);
3074 ASSERT_FALSE(cancelArgs.isOn);
Chris Ye87143712020-11-10 05:05:58 +00003075}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003076
Chris Yef59a2f42020-10-16 12:55:26 -07003077// --- SensorInputMapperTest ---
3078
3079class SensorInputMapperTest : public InputMapperTest {
3080protected:
3081 static const int32_t ACCEL_RAW_MIN;
3082 static const int32_t ACCEL_RAW_MAX;
3083 static const int32_t ACCEL_RAW_FUZZ;
3084 static const int32_t ACCEL_RAW_FLAT;
3085 static const int32_t ACCEL_RAW_RESOLUTION;
3086
3087 static const int32_t GYRO_RAW_MIN;
3088 static const int32_t GYRO_RAW_MAX;
3089 static const int32_t GYRO_RAW_FUZZ;
3090 static const int32_t GYRO_RAW_FLAT;
3091 static const int32_t GYRO_RAW_RESOLUTION;
3092
3093 static const float GRAVITY_MS2_UNIT;
3094 static const float DEGREE_RADIAN_UNIT;
3095
3096 void prepareAccelAxes();
3097 void prepareGyroAxes();
3098 void setAccelProperties();
3099 void setGyroProperties();
3100 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::SENSOR); }
3101};
3102
3103const int32_t SensorInputMapperTest::ACCEL_RAW_MIN = -32768;
3104const int32_t SensorInputMapperTest::ACCEL_RAW_MAX = 32768;
3105const int32_t SensorInputMapperTest::ACCEL_RAW_FUZZ = 16;
3106const int32_t SensorInputMapperTest::ACCEL_RAW_FLAT = 0;
3107const int32_t SensorInputMapperTest::ACCEL_RAW_RESOLUTION = 8192;
3108
3109const int32_t SensorInputMapperTest::GYRO_RAW_MIN = -2097152;
3110const int32_t SensorInputMapperTest::GYRO_RAW_MAX = 2097152;
3111const int32_t SensorInputMapperTest::GYRO_RAW_FUZZ = 16;
3112const int32_t SensorInputMapperTest::GYRO_RAW_FLAT = 0;
3113const int32_t SensorInputMapperTest::GYRO_RAW_RESOLUTION = 1024;
3114
3115const float SensorInputMapperTest::GRAVITY_MS2_UNIT = 9.80665f;
3116const float SensorInputMapperTest::DEGREE_RADIAN_UNIT = 0.0174533f;
3117
3118void SensorInputMapperTest::prepareAccelAxes() {
3119 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3120 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3121 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3122 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3123 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Z, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3124 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3125}
3126
3127void SensorInputMapperTest::prepareGyroAxes() {
3128 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RX, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3129 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3130 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RY, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3131 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3132 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RZ, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3133 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3134}
3135
3136void SensorInputMapperTest::setAccelProperties() {
3137 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 0, InputDeviceSensorType::ACCELEROMETER,
3138 /* sensorDataIndex */ 0);
3139 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 1, InputDeviceSensorType::ACCELEROMETER,
3140 /* sensorDataIndex */ 1);
3141 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 2, InputDeviceSensorType::ACCELEROMETER,
3142 /* sensorDataIndex */ 2);
3143 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
3144 addConfigurationProperty("sensor.accelerometer.reportingMode", "0");
3145 addConfigurationProperty("sensor.accelerometer.maxDelay", "100000");
3146 addConfigurationProperty("sensor.accelerometer.minDelay", "5000");
3147 addConfigurationProperty("sensor.accelerometer.power", "1.5");
3148}
3149
3150void SensorInputMapperTest::setGyroProperties() {
3151 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 3, InputDeviceSensorType::GYROSCOPE,
3152 /* sensorDataIndex */ 0);
3153 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 4, InputDeviceSensorType::GYROSCOPE,
3154 /* sensorDataIndex */ 1);
3155 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 5, InputDeviceSensorType::GYROSCOPE,
3156 /* sensorDataIndex */ 2);
3157 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
3158 addConfigurationProperty("sensor.gyroscope.reportingMode", "0");
3159 addConfigurationProperty("sensor.gyroscope.maxDelay", "100000");
3160 addConfigurationProperty("sensor.gyroscope.minDelay", "5000");
3161 addConfigurationProperty("sensor.gyroscope.power", "0.8");
3162}
3163
3164TEST_F(SensorInputMapperTest, GetSources) {
Arpit Singhfb706c32023-04-26 15:07:55 +00003165 SensorInputMapper& mapper = constructAndAddMapper<SensorInputMapper>();
Chris Yef59a2f42020-10-16 12:55:26 -07003166
3167 ASSERT_EQ(static_cast<uint32_t>(AINPUT_SOURCE_SENSOR), mapper.getSources());
3168}
3169
3170TEST_F(SensorInputMapperTest, ProcessAccelerometerSensor) {
3171 setAccelProperties();
3172 prepareAccelAxes();
Arpit Singhfb706c32023-04-26 15:07:55 +00003173 SensorInputMapper& mapper = constructAndAddMapper<SensorInputMapper>();
Chris Yef59a2f42020-10-16 12:55:26 -07003174
3175 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::ACCELEROMETER,
3176 std::chrono::microseconds(10000),
3177 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08003178 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003179 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, 20000);
3180 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, -20000);
3181 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Z, 40000);
3182 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
3183 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07003184
3185 NotifySensorArgs args;
3186 std::vector<float> values = {20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
3187 -20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
3188 40000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT};
3189
3190 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
3191 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
3192 ASSERT_EQ(args.deviceId, DEVICE_ID);
3193 ASSERT_EQ(args.sensorType, InputDeviceSensorType::ACCELEROMETER);
3194 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
3195 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
3196 ASSERT_EQ(args.values, values);
3197 mapper.flushSensor(InputDeviceSensorType::ACCELEROMETER);
3198}
3199
3200TEST_F(SensorInputMapperTest, ProcessGyroscopeSensor) {
3201 setGyroProperties();
3202 prepareGyroAxes();
Arpit Singhfb706c32023-04-26 15:07:55 +00003203 SensorInputMapper& mapper = constructAndAddMapper<SensorInputMapper>();
Chris Yef59a2f42020-10-16 12:55:26 -07003204
3205 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::GYROSCOPE,
3206 std::chrono::microseconds(10000),
3207 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08003208 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003209 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RX, 20000);
3210 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RY, -20000);
3211 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RZ, 40000);
3212 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
3213 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07003214
3215 NotifySensorArgs args;
3216 std::vector<float> values = {20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
3217 -20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
3218 40000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT};
3219
3220 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
3221 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
3222 ASSERT_EQ(args.deviceId, DEVICE_ID);
3223 ASSERT_EQ(args.sensorType, InputDeviceSensorType::GYROSCOPE);
3224 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
3225 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
3226 ASSERT_EQ(args.values, values);
3227 mapper.flushSensor(InputDeviceSensorType::GYROSCOPE);
3228}
3229
Michael Wrightd02c5b62014-02-10 15:10:22 -08003230// --- KeyboardInputMapperTest ---
3231
3232class KeyboardInputMapperTest : public InputMapperTest {
3233protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003234 const std::string UNIQUE_ID = "local:0";
Zixuan Qufecb6062022-11-12 04:44:31 +00003235 const KeyboardLayoutInfo DEVICE_KEYBOARD_LAYOUT_INFO = KeyboardLayoutInfo("en-US", "qwerty");
Michael Wrighta9cf4192022-12-01 23:46:39 +00003236 void prepareDisplay(ui::Rotation orientation);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003237
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003238 void testDPadKeyRotation(KeyboardInputMapper& mapper, int32_t originalScanCode,
Arthur Hung2c9a3342019-07-23 14:18:59 +08003239 int32_t originalKeyCode, int32_t rotatedKeyCode,
3240 int32_t displayId = ADISPLAY_ID_NONE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003241};
3242
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003243/* Similar to setDisplayInfoAndReconfigure, but pre-populates all parameters except for the
3244 * orientation.
3245 */
Michael Wrighta9cf4192022-12-01 23:46:39 +00003246void KeyboardInputMapperTest::prepareDisplay(ui::Rotation orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003247 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
3248 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003249}
3250
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003251void KeyboardInputMapperTest::testDPadKeyRotation(KeyboardInputMapper& mapper,
Arthur Hung2c9a3342019-07-23 14:18:59 +08003252 int32_t originalScanCode, int32_t originalKeyCode,
3253 int32_t rotatedKeyCode, int32_t displayId) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003254 NotifyKeyArgs args;
3255
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003256 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003257 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3258 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3259 ASSERT_EQ(originalScanCode, args.scanCode);
3260 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003261 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003262
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003263 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003264 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3265 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3266 ASSERT_EQ(originalScanCode, args.scanCode);
3267 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003268 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003269}
3270
Michael Wrightd02c5b62014-02-10 15:10:22 -08003271TEST_F(KeyboardInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003272 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003273 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003274 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003275
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003276 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003277}
3278
3279TEST_F(KeyboardInputMapperTest, Process_SimpleKeyPress) {
3280 const int32_t USAGE_A = 0x070004;
3281 const int32_t USAGE_UNKNOWN = 0x07ffff;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003282 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3283 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
Chris Yea52ade12020-08-27 16:49:20 -07003284 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, POLICY_FLAG_WAKE);
3285 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, POLICY_FLAG_WAKE);
3286 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003287
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003288 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003289 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003290 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003291 // Initial metastate is AMETA_NONE.
3292 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003293
3294 // Key down by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003295 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003296 NotifyKeyArgs args;
3297 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3298 ASSERT_EQ(DEVICE_ID, args.deviceId);
3299 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3300 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3301 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3302 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3303 ASSERT_EQ(KEY_HOME, args.scanCode);
3304 ASSERT_EQ(AMETA_NONE, args.metaState);
3305 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3306 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3307 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3308
3309 // Key up by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003310 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003311 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3312 ASSERT_EQ(DEVICE_ID, args.deviceId);
3313 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3314 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3315 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3316 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3317 ASSERT_EQ(KEY_HOME, args.scanCode);
3318 ASSERT_EQ(AMETA_NONE, args.metaState);
3319 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3320 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3321 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3322
3323 // Key down by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003324 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3325 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, 0, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003326 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3327 ASSERT_EQ(DEVICE_ID, args.deviceId);
3328 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3329 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3330 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3331 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3332 ASSERT_EQ(0, args.scanCode);
3333 ASSERT_EQ(AMETA_NONE, args.metaState);
3334 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3335 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3336 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3337
3338 // Key up by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003339 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3340 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003341 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3342 ASSERT_EQ(DEVICE_ID, args.deviceId);
3343 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3344 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3345 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3346 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3347 ASSERT_EQ(0, args.scanCode);
3348 ASSERT_EQ(AMETA_NONE, args.metaState);
3349 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3350 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3351 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3352
3353 // Key down with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003354 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3355 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UNKNOWN, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003356 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3357 ASSERT_EQ(DEVICE_ID, args.deviceId);
3358 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3359 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3360 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3361 ASSERT_EQ(0, args.keyCode);
3362 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3363 ASSERT_EQ(AMETA_NONE, args.metaState);
3364 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3365 ASSERT_EQ(0U, args.policyFlags);
3366 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3367
3368 // Key up with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003369 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3370 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003371 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3372 ASSERT_EQ(DEVICE_ID, args.deviceId);
3373 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3374 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3375 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3376 ASSERT_EQ(0, args.keyCode);
3377 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3378 ASSERT_EQ(AMETA_NONE, args.metaState);
3379 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3380 ASSERT_EQ(0U, args.policyFlags);
3381 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3382}
3383
Vaibhav Devmuraricbba14c2022-10-10 16:54:49 +00003384TEST_F(KeyboardInputMapperTest, Process_KeyRemapping) {
3385 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
3386 mFakeEventHub->addKey(EVENTHUB_ID, KEY_B, 0, AKEYCODE_B, 0);
3387 mFakeEventHub->addKeyRemapping(EVENTHUB_ID, AKEYCODE_A, AKEYCODE_B);
3388
3389 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003390 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Vaibhav Devmuraricbba14c2022-10-10 16:54:49 +00003391 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3392
3393 // Key down by scan code.
3394 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_A, 1);
3395 NotifyKeyArgs args;
3396 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3397 ASSERT_EQ(AKEYCODE_B, args.keyCode);
3398
3399 // Key up by scan code.
3400 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_A, 0);
3401 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3402 ASSERT_EQ(AKEYCODE_B, args.keyCode);
3403}
3404
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003405/**
3406 * Ensure that the readTime is set to the time when the EV_KEY is received.
3407 */
3408TEST_F(KeyboardInputMapperTest, Process_SendsReadTime) {
3409 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3410
3411 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003412 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003413 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3414 NotifyKeyArgs args;
3415
3416 // Key down
Harry Cutts33476232023-01-30 19:57:29 +00003417 process(mapper, ARBITRARY_TIME, /*readTime=*/12, EV_KEY, KEY_HOME, 1);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003418 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3419 ASSERT_EQ(12, args.readTime);
3420
3421 // Key up
Harry Cutts33476232023-01-30 19:57:29 +00003422 process(mapper, ARBITRARY_TIME, /*readTime=*/15, EV_KEY, KEY_HOME, 1);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003423 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3424 ASSERT_EQ(15, args.readTime);
3425}
3426
Michael Wrightd02c5b62014-02-10 15:10:22 -08003427TEST_F(KeyboardInputMapperTest, Process_ShouldUpdateMetaState) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003428 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFTSHIFT, 0, AKEYCODE_SHIFT_LEFT, 0);
3429 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003430 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, 0);
3431 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, 0);
3432 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003433
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003434 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003435 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003436 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003437
Arthur Hung95f68612022-04-07 14:08:22 +08003438 // Initial metastate is AMETA_NONE.
3439 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003440
3441 // Metakey down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003442 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003443 NotifyKeyArgs args;
3444 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3445 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003446 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003447 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003448
3449 // Key down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003450 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_A, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003451 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3452 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003453 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003454
3455 // Key up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003456 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, KEY_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003457 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3458 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003459 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003460
3461 // Metakey up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003462 process(mapper, ARBITRARY_TIME + 3, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003463 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3464 ASSERT_EQ(AMETA_NONE, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003465 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003466 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003467}
3468
3469TEST_F(KeyboardInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003470 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3471 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3472 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3473 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003474
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003475 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003476 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003477 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003478
Michael Wrighta9cf4192022-12-01 23:46:39 +00003479 prepareDisplay(ui::ROTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003480 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3481 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
3482 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3483 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
3484 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3485 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
3486 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3487 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
3488}
3489
3490TEST_F(KeyboardInputMapperTest, Process_WhenOrientationAware_ShouldRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003491 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3492 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3493 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3494 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003495
Michael Wrightd02c5b62014-02-10 15:10:22 -08003496 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003497 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003498 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003499 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003500
Michael Wrighta9cf4192022-12-01 23:46:39 +00003501 prepareDisplay(ui::ROTATION_0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003502 ASSERT_NO_FATAL_FAILURE(
3503 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3504 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3505 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3506 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3507 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3508 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3509 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003510
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003511 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003512 prepareDisplay(ui::ROTATION_90);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003513 ASSERT_NO_FATAL_FAILURE(
3514 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3515 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3516 AKEYCODE_DPAD_UP, DISPLAY_ID));
3517 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3518 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3519 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3520 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003521
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003522 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003523 prepareDisplay(ui::ROTATION_180);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003524 ASSERT_NO_FATAL_FAILURE(
3525 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3526 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3527 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3528 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3529 AKEYCODE_DPAD_UP, DISPLAY_ID));
3530 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3531 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003532
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003533 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003534 prepareDisplay(ui::ROTATION_270);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003535 ASSERT_NO_FATAL_FAILURE(
3536 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3537 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3538 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3539 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3540 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3541 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3542 AKEYCODE_DPAD_UP, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003543
3544 // Special case: if orientation changes while key is down, we still emit the same keycode
3545 // in the key up as we did in the key down.
3546 NotifyKeyArgs args;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003547 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003548 prepareDisplay(ui::ROTATION_270);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003549 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003550 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3551 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3552 ASSERT_EQ(KEY_UP, args.scanCode);
3553 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3554
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003555 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003556 prepareDisplay(ui::ROTATION_180);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003557 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003558 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3559 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3560 ASSERT_EQ(KEY_UP, args.scanCode);
3561 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3562}
3563
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003564TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_NotOrientationAware) {
3565 // If the keyboard is not orientation aware,
3566 // key events should not be associated with a specific display id
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003567 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003568
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003569 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003570 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003571 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003572 NotifyKeyArgs args;
3573
3574 // Display id should be ADISPLAY_ID_NONE without any display configuration.
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(ADISPLAY_ID_NONE, args.displayId);
3580
Michael Wrighta9cf4192022-12-01 23:46:39 +00003581 prepareDisplay(ui::ROTATION_0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003582 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003583 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003584 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003585 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3586 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3587}
3588
3589TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_OrientationAware) {
3590 // If the keyboard is orientation aware,
3591 // key events should be associated with the internal viewport
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003592 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003593
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003594 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003595 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003596 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003597 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003598 NotifyKeyArgs args;
3599
3600 // Display id should be ADISPLAY_ID_NONE without any display configuration.
3601 // ^--- already checked by the previous test
3602
Michael Wrighta9cf4192022-12-01 23:46:39 +00003603 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003604 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003605 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003606 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003607 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003608 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3609 ASSERT_EQ(DISPLAY_ID, args.displayId);
3610
3611 constexpr int32_t newDisplayId = 2;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003612 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003613 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003614 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003615 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003616 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003617 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003618 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3619 ASSERT_EQ(newDisplayId, args.displayId);
3620}
3621
Michael Wrightd02c5b62014-02-10 15:10:22 -08003622TEST_F(KeyboardInputMapperTest, GetKeyCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003623 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003624 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003625 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003626
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003627 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003628 ASSERT_EQ(1, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003629
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003630 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003631 ASSERT_EQ(0, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003632}
3633
Philip Junker4af3b3d2021-12-14 10:36:55 +01003634TEST_F(KeyboardInputMapperTest, GetKeyCodeForKeyLocation) {
3635 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003636 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Philip Junker4af3b3d2021-12-14 10:36:55 +01003637 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3638
3639 mFakeEventHub->addKeyCodeMapping(EVENTHUB_ID, AKEYCODE_Y, AKEYCODE_Z);
3640 ASSERT_EQ(AKEYCODE_Z, mapper.getKeyCodeForKeyLocation(AKEYCODE_Y))
3641 << "If a mapping is available, the result is equal to the mapping";
3642
3643 ASSERT_EQ(AKEYCODE_A, mapper.getKeyCodeForKeyLocation(AKEYCODE_A))
3644 << "If no mapping is available, the result is the key location";
3645}
3646
Michael Wrightd02c5b62014-02-10 15:10:22 -08003647TEST_F(KeyboardInputMapperTest, GetScanCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003648 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003649 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003650 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003651
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003652 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003653 ASSERT_EQ(1, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003654
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003655 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003656 ASSERT_EQ(0, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003657}
3658
3659TEST_F(KeyboardInputMapperTest, MarkSupportedKeyCodes) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003660 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003661 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003662 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003663
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003664 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003665
Michael Wrightd02c5b62014-02-10 15:10:22 -08003666 uint8_t flags[2] = { 0, 0 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07003667 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, {AKEYCODE_A, AKEYCODE_B}, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003668 ASSERT_TRUE(flags[0]);
3669 ASSERT_FALSE(flags[1]);
3670}
3671
3672TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleMetaStateAndLeds) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003673 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3674 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3675 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3676 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3677 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3678 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003679
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003680 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003681 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003682 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003683 // Initial metastate is AMETA_NONE.
3684 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003685
3686 // Initialization should have turned all of the lights off.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003687 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3688 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3689 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003690
3691 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003692 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3693 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003694 ASSERT_TRUE(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_CAPS_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003698
3699 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003700 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3701 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003702 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3703 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3704 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003705 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003706
3707 // Toggle caps lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003708 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3709 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003710 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3711 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3712 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003713 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003714
3715 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003716 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3717 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003718 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3719 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3720 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003721 ASSERT_EQ(AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003722
3723 // Toggle num lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003724 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3725 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003726 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3727 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3728 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003729 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003730
3731 // Toggle scroll lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003732 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3733 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003734 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3735 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3736 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003737 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003738}
3739
Chris Yea52ade12020-08-27 16:49:20 -07003740TEST_F(KeyboardInputMapperTest, NoMetaStateWhenMetaKeysNotPresent) {
3741 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_BUTTON_A, 0);
3742 mFakeEventHub->addKey(EVENTHUB_ID, BTN_B, 0, AKEYCODE_BUTTON_B, 0);
3743 mFakeEventHub->addKey(EVENTHUB_ID, BTN_X, 0, AKEYCODE_BUTTON_X, 0);
3744 mFakeEventHub->addKey(EVENTHUB_ID, BTN_Y, 0, AKEYCODE_BUTTON_Y, 0);
3745
3746 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003747 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Chris Yea52ade12020-08-27 16:49:20 -07003748 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
3749
Chris Yea52ade12020-08-27 16:49:20 -07003750 // Meta state should be AMETA_NONE after reset
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003751 std::list<NotifyArgs> unused = mapper.reset(ARBITRARY_TIME);
Chris Yea52ade12020-08-27 16:49:20 -07003752 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3753 // Meta state should be AMETA_NONE with update, as device doesn't have the keys.
3754 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
3755 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3756
3757 NotifyKeyArgs args;
3758 // Press button "A"
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003759 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_A, 1);
Chris Yea52ade12020-08-27 16:49:20 -07003760 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3761 ASSERT_EQ(AMETA_NONE, args.metaState);
3762 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3763 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3764 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3765
3766 // Button up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003767 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003768 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3769 ASSERT_EQ(AMETA_NONE, args.metaState);
3770 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3771 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3772 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3773}
3774
Arthur Hung2c9a3342019-07-23 14:18:59 +08003775TEST_F(KeyboardInputMapperTest, Configure_AssignsDisplayPort) {
3776 // keyboard 1.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003777 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3778 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3779 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3780 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003781
3782 // keyboard 2.
3783 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08003784 const std::string DEVICE_NAME2 = "KEYBOARD2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08003785 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003786 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08003787 std::shared_ptr<InputDevice> device2 =
3788 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003789 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08003790
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003791 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3792 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3793 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3794 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003795
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003796 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003797 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003798 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003799
Arpit Singh67ca6842023-04-26 14:43:16 +00003800 device2->addEmptyEventHubDevice(SECOND_EVENTHUB_ID);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003801 KeyboardInputMapper& mapper2 =
Arpit Singh67ca6842023-04-26 14:43:16 +00003802 device2->constructAndAddMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID,
3803 mFakePolicy
3804 ->getReaderConfiguration(),
3805 AINPUT_SOURCE_KEYBOARD,
3806 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003807 std::list<NotifyArgs> unused =
3808 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003809 /*changes=*/{});
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003810 unused += device2->reset(ARBITRARY_TIME);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003811
3812 // Prepared displays and associated info.
3813 constexpr uint8_t hdmi1 = 0;
3814 constexpr uint8_t hdmi2 = 1;
3815 const std::string SECONDARY_UNIQUE_ID = "local:1";
3816
3817 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
3818 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
3819
3820 // No associated display viewport found, should disable the device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003821 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003822 InputReaderConfiguration::Change::DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003823 ASSERT_FALSE(device2->isEnabled());
3824
3825 // Prepare second display.
3826 constexpr int32_t newDisplayId = 2;
Michael Wrighta9cf4192022-12-01 23:46:39 +00003827 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003828 UNIQUE_ID, hdmi1, ViewportType::INTERNAL);
Michael Wrighta9cf4192022-12-01 23:46:39 +00003829 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003830 SECONDARY_UNIQUE_ID, hdmi2, ViewportType::EXTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003831 // Default device will reconfigure above, need additional reconfiguration for another device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003832 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003833 InputReaderConfiguration::Change::DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003834
3835 // Device should be enabled after the associated display is found.
3836 ASSERT_TRUE(mDevice->isEnabled());
3837 ASSERT_TRUE(device2->isEnabled());
3838
3839 // Test pad key events
3840 ASSERT_NO_FATAL_FAILURE(
3841 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3842 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3843 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3844 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3845 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3846 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3847 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3848
3849 ASSERT_NO_FATAL_FAILURE(
3850 testDPadKeyRotation(mapper2, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, newDisplayId));
3851 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3852 AKEYCODE_DPAD_RIGHT, newDisplayId));
3853 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3854 AKEYCODE_DPAD_DOWN, newDisplayId));
3855 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3856 AKEYCODE_DPAD_LEFT, newDisplayId));
3857}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003858
arthurhungc903df12020-08-11 15:08:42 +08003859TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleAfterReattach) {
3860 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3861 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3862 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3863 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3864 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3865 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3866
3867 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003868 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
arthurhungc903df12020-08-11 15:08:42 +08003869 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003870 // Initial metastate is AMETA_NONE.
3871 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08003872
3873 // Initialization should have turned all of the lights off.
3874 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3875 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3876 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3877
3878 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003879 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3880 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003881 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3882 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
3883
3884 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003885 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3886 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003887 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3888 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
3889
3890 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003891 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3892 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003893 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3894 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
3895
3896 mFakeEventHub->removeDevice(EVENTHUB_ID);
3897 mReader->loopOnce();
3898
3899 // keyboard 2 should default toggle keys.
3900 const std::string USB2 = "USB2";
3901 const std::string DEVICE_NAME2 = "KEYBOARD2";
3902 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
3903 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
3904 std::shared_ptr<InputDevice> device2 =
3905 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003906 ftl::Flags<InputDeviceClass>(0));
arthurhungc903df12020-08-11 15:08:42 +08003907 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3908 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
3909 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3910 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3911 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3912 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3913
Arpit Singh67ca6842023-04-26 14:43:16 +00003914 device2->addEmptyEventHubDevice(SECOND_EVENTHUB_ID);
arthurhung6fe95782020-10-05 22:41:16 +08003915 KeyboardInputMapper& mapper2 =
Arpit Singh67ca6842023-04-26 14:43:16 +00003916 device2->constructAndAddMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID,
3917 mFakePolicy
3918 ->getReaderConfiguration(),
3919 AINPUT_SOURCE_KEYBOARD,
3920 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003921 std::list<NotifyArgs> unused =
3922 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003923 /*changes=*/{});
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003924 unused += device2->reset(ARBITRARY_TIME);
arthurhungc903df12020-08-11 15:08:42 +08003925
3926 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_CAPSL));
3927 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_NUML));
3928 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_SCROLLL));
arthurhung6fe95782020-10-05 22:41:16 +08003929 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON,
3930 mapper2.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08003931}
3932
Arthur Hungcb40a002021-08-03 14:31:01 +00003933TEST_F(KeyboardInputMapperTest, Process_toggleCapsLockState) {
3934 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3935 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3936 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3937
3938 // Suppose we have two mappers. (DPAD + KEYBOARD)
Arpit Singh67ca6842023-04-26 14:43:16 +00003939 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_DPAD,
Arthur Hungcb40a002021-08-03 14:31:01 +00003940 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
3941 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003942 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Arthur Hungcb40a002021-08-03 14:31:01 +00003943 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003944 // Initial metastate is AMETA_NONE.
3945 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Arthur Hungcb40a002021-08-03 14:31:01 +00003946
3947 mReader->toggleCapsLockState(DEVICE_ID);
3948 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
3949}
3950
Arthur Hungfb3cc112022-04-13 07:39:50 +00003951TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleInMultiDevices) {
3952 // keyboard 1.
3953 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3954 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3955 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3956 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3957 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3958 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3959
3960 KeyboardInputMapper& mapper1 =
Arpit Singh67ca6842023-04-26 14:43:16 +00003961 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Arthur Hungfb3cc112022-04-13 07:39:50 +00003962 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3963
3964 // keyboard 2.
3965 const std::string USB2 = "USB2";
3966 const std::string DEVICE_NAME2 = "KEYBOARD2";
3967 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
3968 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
3969 std::shared_ptr<InputDevice> device2 =
3970 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
3971 ftl::Flags<InputDeviceClass>(0));
3972 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3973 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
3974 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3975 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3976 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3977 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3978
Arpit Singh67ca6842023-04-26 14:43:16 +00003979 device2->addEmptyEventHubDevice(SECOND_EVENTHUB_ID);
Arthur Hungfb3cc112022-04-13 07:39:50 +00003980 KeyboardInputMapper& mapper2 =
Arpit Singh67ca6842023-04-26 14:43:16 +00003981 device2->constructAndAddMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID,
3982 mFakePolicy
3983 ->getReaderConfiguration(),
3984 AINPUT_SOURCE_KEYBOARD,
3985 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003986 std::list<NotifyArgs> unused =
3987 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003988 /*changes=*/{});
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003989 unused += device2->reset(ARBITRARY_TIME);
Arthur Hungfb3cc112022-04-13 07:39:50 +00003990
Arthur Hung95f68612022-04-07 14:08:22 +08003991 // Initial metastate is AMETA_NONE.
3992 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
3993 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
3994
3995 // Toggle num lock on and off.
3996 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3997 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Arthur Hungfb3cc112022-04-13 07:39:50 +00003998 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3999 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper1.getMetaState());
4000 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper2.getMetaState());
4001
4002 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
4003 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
4004 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4005 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4006 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4007
4008 // Toggle caps lock on and off.
4009 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
4010 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
4011 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4012 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper1.getMetaState());
4013 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper2.getMetaState());
4014
4015 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
4016 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
4017 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4018 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4019 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4020
4021 // Toggle scroll lock on and off.
4022 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
4023 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
4024 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
4025 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper1.getMetaState());
4026 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper2.getMetaState());
4027
4028 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
4029 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
4030 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
4031 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4032 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4033}
4034
Arthur Hung2141d542022-08-23 07:45:21 +00004035TEST_F(KeyboardInputMapperTest, Process_DisabledDevice) {
4036 const int32_t USAGE_A = 0x070004;
4037 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4038 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
4039
4040 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00004041 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Arthur Hung2141d542022-08-23 07:45:21 +00004042 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
4043 // Key down by scan code.
4044 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
4045 NotifyKeyArgs args;
4046 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4047 ASSERT_EQ(DEVICE_ID, args.deviceId);
4048 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
4049 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4050 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
4051 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4052 ASSERT_EQ(KEY_HOME, args.scanCode);
4053 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
4054
4055 // Disable device, it should synthesize cancellation events for down events.
4056 mFakePolicy->addDisabledDevice(DEVICE_ID);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004057 configureDevice(InputReaderConfiguration::Change::ENABLED_STATE);
Arthur Hung2141d542022-08-23 07:45:21 +00004058
4059 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4060 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
4061 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4062 ASSERT_EQ(KEY_HOME, args.scanCode);
4063 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_CANCELED, args.flags);
4064}
4065
Zixuan Qufecb6062022-11-12 04:44:31 +00004066TEST_F(KeyboardInputMapperTest, Configure_AssignKeyboardLayoutInfo) {
Arpit Singh67ca6842023-04-26 14:43:16 +00004067 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4068 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Zixuan Qufecb6062022-11-12 04:44:31 +00004069 std::list<NotifyArgs> unused =
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004070 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
4071 /*changes=*/{});
Zixuan Qufecb6062022-11-12 04:44:31 +00004072
Vaibhav Devmurari0a6fee82023-04-11 18:53:04 +00004073 uint32_t generation = mReader->getContext()->getGeneration();
Zixuan Qufecb6062022-11-12 04:44:31 +00004074 mFakePolicy->addKeyboardLayoutAssociation(DEVICE_LOCATION, DEVICE_KEYBOARD_LAYOUT_INFO);
4075
4076 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004077 InputReaderConfiguration::Change::KEYBOARD_LAYOUT_ASSOCIATION);
Zixuan Qufecb6062022-11-12 04:44:31 +00004078
4079 InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
4080 ASSERT_EQ(DEVICE_KEYBOARD_LAYOUT_INFO.languageTag,
4081 deviceInfo.getKeyboardLayoutInfo()->languageTag);
4082 ASSERT_EQ(DEVICE_KEYBOARD_LAYOUT_INFO.layoutType,
4083 deviceInfo.getKeyboardLayoutInfo()->layoutType);
Vaibhav Devmurari0a6fee82023-04-11 18:53:04 +00004084 ASSERT_TRUE(mReader->getContext()->getGeneration() != generation);
4085
4086 // Call change layout association with the same values: Generation shouldn't change
4087 generation = mReader->getContext()->getGeneration();
4088 mFakePolicy->addKeyboardLayoutAssociation(DEVICE_LOCATION, DEVICE_KEYBOARD_LAYOUT_INFO);
4089 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
4090 InputReaderConfiguration::Change::KEYBOARD_LAYOUT_ASSOCIATION);
4091 ASSERT_TRUE(mReader->getContext()->getGeneration() == generation);
Zixuan Qufecb6062022-11-12 04:44:31 +00004092}
4093
Vaibhav Devmurari7fb41132023-01-02 13:30:26 +00004094TEST_F(KeyboardInputMapperTest, LayoutInfoCorrectlyMapped) {
4095 mFakeEventHub->setRawLayoutInfo(EVENTHUB_ID,
4096 RawLayoutInfo{.languageTag = "en", .layoutType = "extended"});
4097
4098 // Configuration
Arpit Singh67ca6842023-04-26 14:43:16 +00004099 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Vaibhav Devmurari7fb41132023-01-02 13:30:26 +00004100 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
4101 InputReaderConfiguration config;
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004102 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, config, /*changes=*/{});
Vaibhav Devmurari7fb41132023-01-02 13:30:26 +00004103
4104 ASSERT_EQ("en", mDevice->getDeviceInfo().getKeyboardLayoutInfo()->languageTag);
4105 ASSERT_EQ("extended", mDevice->getDeviceInfo().getKeyboardLayoutInfo()->layoutType);
4106}
4107
Justin Chung71ddb432023-03-27 04:29:07 +00004108TEST_F(KeyboardInputMapperTest, Process_GesureEventToSetFlagKeepTouchMode) {
4109 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, POLICY_FLAG_GESTURE);
4110 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00004111 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Justin Chung71ddb432023-03-27 04:29:07 +00004112 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
4113 NotifyKeyArgs args;
4114
4115 // Key down
4116 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_LEFT, 1);
4117 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4118 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_KEEP_TOUCH_MODE, args.flags);
4119}
4120
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004121// --- KeyboardInputMapperTest_ExternalDevice ---
4122
4123class KeyboardInputMapperTest_ExternalDevice : public InputMapperTest {
4124protected:
Chris Yea52ade12020-08-27 16:49:20 -07004125 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004126};
4127
Vaibhav Devmurari2681a812024-01-11 00:15:35 +00004128TEST_F(KeyboardInputMapperTest_ExternalDevice, WakeBehavior_AlphabeticKeyboard) {
Vaibhav Devmurari16257862023-03-06 10:06:32 +00004129 // For external devices, keys will trigger wake on key down. Media keys should also trigger
4130 // wake if triggered from external devices.
Powei Fengd041c5d2019-05-03 17:11:33 -07004131
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004132 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, 0);
4133 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, 0);
4134 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAYPAUSE, 0, AKEYCODE_MEDIA_PLAY_PAUSE,
4135 POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07004136
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004137 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00004138 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004139 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07004140
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004141 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004142 NotifyKeyArgs args;
4143 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4144 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4145
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004146 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004147 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4148 ASSERT_EQ(uint32_t(0), args.policyFlags);
4149
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004150 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004151 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Vaibhav Devmurari16257862023-03-06 10:06:32 +00004152 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
Powei Fengd041c5d2019-05-03 17:11:33 -07004153
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004154 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004155 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4156 ASSERT_EQ(uint32_t(0), args.policyFlags);
4157
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004158 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004159 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4160 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4161
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004162 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004163 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4164 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4165}
4166
Vaibhav Devmurari2681a812024-01-11 00:15:35 +00004167TEST_F(KeyboardInputMapperTest_ExternalDevice, WakeBehavior_NoneAlphabeticKeyboard) {
4168 // For external devices, keys will trigger wake on key down. Media keys should not trigger
4169 // wake if triggered from external non-alphaebtic keyboard (e.g. headsets).
4170
4171 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, 0);
4172 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAYPAUSE, 0, AKEYCODE_MEDIA_PLAY_PAUSE,
4173 POLICY_FLAG_WAKE);
4174
4175 KeyboardInputMapper& mapper =
4176 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4177 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
4178
4179 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
4180 NotifyKeyArgs args;
4181 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4182 ASSERT_EQ(uint32_t(0), args.policyFlags);
4183
4184 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
4185 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4186 ASSERT_EQ(uint32_t(0), args.policyFlags);
4187
4188 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 1);
4189 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4190 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4191
4192 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 0);
4193 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4194 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4195}
4196
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004197TEST_F(KeyboardInputMapperTest_ExternalDevice, DoNotWakeByDefaultBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07004198 // Tv Remote key's wake behavior is prescribed by the keylayout file.
Powei Fengd041c5d2019-05-03 17:11:33 -07004199
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004200 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4201 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
4202 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07004203
Powei Fengd041c5d2019-05-03 17:11:33 -07004204 addConfigurationProperty("keyboard.doNotWakeByDefault", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004205 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00004206 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004207 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07004208
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004209 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004210 NotifyKeyArgs args;
4211 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4212 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4213
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004214 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004215 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4216 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4217
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004218 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_DOWN, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004219 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4220 ASSERT_EQ(uint32_t(0), args.policyFlags);
4221
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004222 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_DOWN, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004223 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4224 ASSERT_EQ(uint32_t(0), args.policyFlags);
4225
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004226 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004227 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4228 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4229
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004230 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004231 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4232 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4233}
4234
Michael Wrightd02c5b62014-02-10 15:10:22 -08004235// --- TouchInputMapperTest ---
4236
4237class TouchInputMapperTest : public InputMapperTest {
4238protected:
4239 static const int32_t RAW_X_MIN;
4240 static const int32_t RAW_X_MAX;
4241 static const int32_t RAW_Y_MIN;
4242 static const int32_t RAW_Y_MAX;
4243 static const int32_t RAW_TOUCH_MIN;
4244 static const int32_t RAW_TOUCH_MAX;
4245 static const int32_t RAW_TOOL_MIN;
4246 static const int32_t RAW_TOOL_MAX;
4247 static const int32_t RAW_PRESSURE_MIN;
4248 static const int32_t RAW_PRESSURE_MAX;
4249 static const int32_t RAW_ORIENTATION_MIN;
4250 static const int32_t RAW_ORIENTATION_MAX;
4251 static const int32_t RAW_DISTANCE_MIN;
4252 static const int32_t RAW_DISTANCE_MAX;
4253 static const int32_t RAW_TILT_MIN;
4254 static const int32_t RAW_TILT_MAX;
4255 static const int32_t RAW_ID_MIN;
4256 static const int32_t RAW_ID_MAX;
4257 static const int32_t RAW_SLOT_MIN;
4258 static const int32_t RAW_SLOT_MAX;
4259 static const float X_PRECISION;
4260 static const float Y_PRECISION;
Santos Cordonfa5cf462017-04-05 10:37:00 -07004261 static const float X_PRECISION_VIRTUAL;
4262 static const float Y_PRECISION_VIRTUAL;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004263
4264 static const float GEOMETRIC_SCALE;
Jason Gerecke489fda82012-09-07 17:19:40 -07004265 static const TouchAffineTransformation AFFINE_TRANSFORM;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004266
4267 static const VirtualKeyDefinition VIRTUAL_KEYS[2];
4268
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004269 const std::string UNIQUE_ID = "local:0";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004270 const std::string SECONDARY_UNIQUE_ID = "local:1";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004271
Michael Wrightd02c5b62014-02-10 15:10:22 -08004272 enum Axes {
4273 POSITION = 1 << 0,
4274 TOUCH = 1 << 1,
4275 TOOL = 1 << 2,
4276 PRESSURE = 1 << 3,
4277 ORIENTATION = 1 << 4,
4278 MINOR = 1 << 5,
4279 ID = 1 << 6,
4280 DISTANCE = 1 << 7,
4281 TILT = 1 << 8,
4282 SLOT = 1 << 9,
4283 TOOL_TYPE = 1 << 10,
4284 };
4285
Michael Wrighta9cf4192022-12-01 23:46:39 +00004286 void prepareDisplay(ui::Rotation orientation, std::optional<uint8_t> port = NO_PORT);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004287 void prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port = NO_PORT);
Michael Wrighta9cf4192022-12-01 23:46:39 +00004288 void prepareVirtualDisplay(ui::Rotation orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004289 void prepareVirtualKeys();
Jason Gerecke489fda82012-09-07 17:19:40 -07004290 void prepareLocationCalibration();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004291 int32_t toRawX(float displayX);
4292 int32_t toRawY(float displayY);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07004293 int32_t toRotatedRawX(float displayX);
4294 int32_t toRotatedRawY(float displayY);
Jason Gerecke489fda82012-09-07 17:19:40 -07004295 float toCookedX(float rawX, float rawY);
4296 float toCookedY(float rawX, float rawY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004297 float toDisplayX(int32_t rawX);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004298 float toDisplayX(int32_t rawX, int32_t displayWidth);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004299 float toDisplayY(int32_t rawY);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004300 float toDisplayY(int32_t rawY, int32_t displayHeight);
4301
Michael Wrightd02c5b62014-02-10 15:10:22 -08004302};
4303
4304const int32_t TouchInputMapperTest::RAW_X_MIN = 25;
4305const int32_t TouchInputMapperTest::RAW_X_MAX = 1019;
4306const int32_t TouchInputMapperTest::RAW_Y_MIN = 30;
4307const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009;
4308const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0;
4309const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31;
4310const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0;
4311const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15;
Michael Wrightaa449c92017-12-13 21:21:43 +00004312const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = 0;
4313const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = 255;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004314const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7;
4315const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
4316const int32_t TouchInputMapperTest::RAW_DISTANCE_MIN = 0;
4317const int32_t TouchInputMapperTest::RAW_DISTANCE_MAX = 7;
4318const int32_t TouchInputMapperTest::RAW_TILT_MIN = 0;
4319const int32_t TouchInputMapperTest::RAW_TILT_MAX = 150;
4320const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
4321const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
4322const int32_t TouchInputMapperTest::RAW_SLOT_MIN = 0;
4323const int32_t TouchInputMapperTest::RAW_SLOT_MAX = 9;
4324const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH;
4325const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT;
Santos Cordonfa5cf462017-04-05 10:37:00 -07004326const float TouchInputMapperTest::X_PRECISION_VIRTUAL =
4327 float(RAW_X_MAX - RAW_X_MIN + 1) / VIRTUAL_DISPLAY_WIDTH;
4328const float TouchInputMapperTest::Y_PRECISION_VIRTUAL =
4329 float(RAW_Y_MAX - RAW_Y_MIN + 1) / VIRTUAL_DISPLAY_HEIGHT;
Jason Gerecke489fda82012-09-07 17:19:40 -07004330const TouchAffineTransformation TouchInputMapperTest::AFFINE_TRANSFORM =
4331 TouchAffineTransformation(1, -2, 3, -4, 5, -6);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004332
4333const float TouchInputMapperTest::GEOMETRIC_SCALE =
4334 avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1),
4335 float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1));
4336
4337const VirtualKeyDefinition TouchInputMapperTest::VIRTUAL_KEYS[2] = {
4338 { KEY_HOME, 60, DISPLAY_HEIGHT + 15, 20, 20 },
4339 { KEY_MENU, DISPLAY_HEIGHT - 60, DISPLAY_WIDTH + 15, 20, 20 },
4340};
4341
Michael Wrighta9cf4192022-12-01 23:46:39 +00004342void TouchInputMapperTest::prepareDisplay(ui::Rotation orientation, std::optional<uint8_t> port) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004343 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
4344 port, ViewportType::INTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004345}
4346
4347void TouchInputMapperTest::prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port) {
4348 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +00004349 ui::ROTATION_0, SECONDARY_UNIQUE_ID, port, type);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004350}
4351
Michael Wrighta9cf4192022-12-01 23:46:39 +00004352void TouchInputMapperTest::prepareVirtualDisplay(ui::Rotation orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004353 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH, VIRTUAL_DISPLAY_HEIGHT,
4354 orientation, VIRTUAL_DISPLAY_UNIQUE_ID, NO_PORT,
4355 ViewportType::VIRTUAL);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004356}
4357
Michael Wrightd02c5b62014-02-10 15:10:22 -08004358void TouchInputMapperTest::prepareVirtualKeys() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004359 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[0]);
4360 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[1]);
4361 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4362 mFakeEventHub->addKey(EVENTHUB_ID, KEY_MENU, 0, AKEYCODE_MENU, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004363}
4364
Jason Gerecke489fda82012-09-07 17:19:40 -07004365void TouchInputMapperTest::prepareLocationCalibration() {
4366 mFakePolicy->setTouchAffineTransformation(AFFINE_TRANSFORM);
4367}
4368
Michael Wrightd02c5b62014-02-10 15:10:22 -08004369int32_t TouchInputMapperTest::toRawX(float displayX) {
4370 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN);
4371}
4372
4373int32_t TouchInputMapperTest::toRawY(float displayY) {
4374 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN);
4375}
4376
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07004377int32_t TouchInputMapperTest::toRotatedRawX(float displayX) {
4378 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_HEIGHT + RAW_X_MIN);
4379}
4380
4381int32_t TouchInputMapperTest::toRotatedRawY(float displayY) {
4382 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_WIDTH + RAW_Y_MIN);
4383}
4384
Jason Gerecke489fda82012-09-07 17:19:40 -07004385float TouchInputMapperTest::toCookedX(float rawX, float rawY) {
4386 AFFINE_TRANSFORM.applyTo(rawX, rawY);
4387 return rawX;
4388}
4389
4390float TouchInputMapperTest::toCookedY(float rawX, float rawY) {
4391 AFFINE_TRANSFORM.applyTo(rawX, rawY);
4392 return rawY;
4393}
4394
Michael Wrightd02c5b62014-02-10 15:10:22 -08004395float TouchInputMapperTest::toDisplayX(int32_t rawX) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004396 return toDisplayX(rawX, DISPLAY_WIDTH);
4397}
4398
4399float TouchInputMapperTest::toDisplayX(int32_t rawX, int32_t displayWidth) {
4400 return float(rawX - RAW_X_MIN) * displayWidth / (RAW_X_MAX - RAW_X_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004401}
4402
4403float TouchInputMapperTest::toDisplayY(int32_t rawY) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004404 return toDisplayY(rawY, DISPLAY_HEIGHT);
4405}
4406
4407float TouchInputMapperTest::toDisplayY(int32_t rawY, int32_t displayHeight) {
4408 return float(rawY - RAW_Y_MIN) * displayHeight / (RAW_Y_MAX - RAW_Y_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004409}
4410
4411
4412// --- SingleTouchInputMapperTest ---
4413
4414class SingleTouchInputMapperTest : public TouchInputMapperTest {
4415protected:
4416 void prepareButtons();
4417 void prepareAxes(int axes);
4418
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004419 void processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
4420 void processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
4421 void processUp(SingleTouchInputMapper& mappery);
4422 void processPressure(SingleTouchInputMapper& mapper, int32_t pressure);
4423 void processToolMajor(SingleTouchInputMapper& mapper, int32_t toolMajor);
4424 void processDistance(SingleTouchInputMapper& mapper, int32_t distance);
4425 void processTilt(SingleTouchInputMapper& mapper, int32_t tiltX, int32_t tiltY);
4426 void processKey(SingleTouchInputMapper& mapper, int32_t code, int32_t value);
4427 void processSync(SingleTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004428};
4429
4430void SingleTouchInputMapperTest::prepareButtons() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004431 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004432}
4433
4434void SingleTouchInputMapperTest::prepareAxes(int axes) {
4435 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004436 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
4437 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004438 }
4439 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004440 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MIN,
4441 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004442 }
4443 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004444 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TOOL_WIDTH, RAW_TOOL_MIN, RAW_TOOL_MAX, 0,
4445 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004446 }
4447 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004448 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_DISTANCE, RAW_DISTANCE_MIN,
4449 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004450 }
4451 if (axes & TILT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004452 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_X, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
4453 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_Y, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004454 }
4455}
4456
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004457void SingleTouchInputMapperTest::processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004458 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
4459 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
4460 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004461}
4462
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004463void SingleTouchInputMapperTest::processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004464 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
4465 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004466}
4467
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004468void SingleTouchInputMapperTest::processUp(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004469 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004470}
4471
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004472void SingleTouchInputMapperTest::processPressure(SingleTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004473 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004474}
4475
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004476void SingleTouchInputMapperTest::processToolMajor(SingleTouchInputMapper& mapper,
4477 int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004478 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TOOL_WIDTH, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004479}
4480
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004481void SingleTouchInputMapperTest::processDistance(SingleTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004482 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004483}
4484
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004485void SingleTouchInputMapperTest::processTilt(SingleTouchInputMapper& mapper, int32_t tiltX,
4486 int32_t tiltY) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004487 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_X, tiltX);
4488 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_Y, tiltY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004489}
4490
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004491void SingleTouchInputMapperTest::processKey(SingleTouchInputMapper& mapper, int32_t code,
4492 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004493 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004494}
4495
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004496void SingleTouchInputMapperTest::processSync(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004497 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004498}
4499
Michael Wrightd02c5b62014-02-10 15:10:22 -08004500TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004501 prepareButtons();
4502 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00004503 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004504
Josep del Río2d8c79a2023-01-23 19:33:50 +00004505 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004506}
4507
Michael Wrightd02c5b62014-02-10 15:10:22 -08004508TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004509 prepareButtons();
4510 prepareAxes(POSITION);
4511 addConfigurationProperty("touch.deviceType", "touchScreen");
Arpit Singha8c236b2023-04-25 13:56:05 +00004512 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004513
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004514 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004515}
4516
4517TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004518 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00004519 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004520 prepareButtons();
4521 prepareAxes(POSITION);
4522 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00004523 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004524
4525 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004526 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004527
4528 // Virtual key is down.
4529 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4530 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4531 processDown(mapper, x, y);
4532 processSync(mapper);
4533 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4534
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004535 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004536
4537 // Virtual key is up.
4538 processUp(mapper);
4539 processSync(mapper);
4540 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4541
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004542 ASSERT_EQ(AKEY_STATE_UP, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004543}
4544
4545TEST_F(SingleTouchInputMapperTest, GetScanCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004546 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00004547 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004548 prepareButtons();
4549 prepareAxes(POSITION);
4550 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00004551 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004552
4553 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004554 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004555
4556 // Virtual key is down.
4557 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4558 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4559 processDown(mapper, x, y);
4560 processSync(mapper);
4561 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4562
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004563 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004564
4565 // Virtual key is up.
4566 processUp(mapper);
4567 processSync(mapper);
4568 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4569
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004570 ASSERT_EQ(AKEY_STATE_UP, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004571}
4572
4573TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004574 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00004575 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004576 prepareButtons();
4577 prepareAxes(POSITION);
4578 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00004579 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004580
Michael Wrightd02c5b62014-02-10 15:10:22 -08004581 uint8_t flags[2] = { 0, 0 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07004582 ASSERT_TRUE(
4583 mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, {AKEYCODE_HOME, AKEYCODE_A}, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004584 ASSERT_TRUE(flags[0]);
4585 ASSERT_FALSE(flags[1]);
4586}
4587
4588TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004589 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00004590 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004591 prepareButtons();
4592 prepareAxes(POSITION);
4593 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00004594 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004595
arthurhungdcef2dc2020-08-11 14:47:50 +08004596 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004597
4598 NotifyKeyArgs args;
4599
4600 // Press virtual key.
4601 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4602 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4603 processDown(mapper, x, y);
4604 processSync(mapper);
4605
4606 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4607 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4608 ASSERT_EQ(DEVICE_ID, args.deviceId);
4609 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
4610 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
4611 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
4612 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
4613 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4614 ASSERT_EQ(KEY_HOME, args.scanCode);
4615 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4616 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4617
4618 // Release virtual key.
4619 processUp(mapper);
4620 processSync(mapper);
4621
4622 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4623 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4624 ASSERT_EQ(DEVICE_ID, args.deviceId);
4625 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
4626 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
4627 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
4628 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
4629 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4630 ASSERT_EQ(KEY_HOME, args.scanCode);
4631 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4632 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4633
4634 // Should not have sent any motions.
4635 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4636}
4637
4638TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004639 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00004640 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004641 prepareButtons();
4642 prepareAxes(POSITION);
4643 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00004644 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004645
arthurhungdcef2dc2020-08-11 14:47:50 +08004646 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004647
4648 NotifyKeyArgs keyArgs;
4649
4650 // Press virtual key.
4651 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4652 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4653 processDown(mapper, x, y);
4654 processSync(mapper);
4655
4656 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4657 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
4658 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
4659 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
4660 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
4661 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4662 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags);
4663 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
4664 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
4665 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
4666 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
4667
4668 // Move out of bounds. This should generate a cancel and a pointer down since we moved
4669 // into the display area.
4670 y -= 100;
4671 processMove(mapper, x, y);
4672 processSync(mapper);
4673
4674 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4675 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
4676 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
4677 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
4678 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
4679 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4680 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
4681 | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags);
4682 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
4683 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
4684 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
4685 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
4686
4687 NotifyMotionArgs motionArgs;
4688 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4689 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4690 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4691 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4692 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4693 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4694 ASSERT_EQ(0, motionArgs.flags);
4695 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4696 ASSERT_EQ(0, motionArgs.buttonState);
4697 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004698 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004699 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004700 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004701 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4702 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4703 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4704 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4705 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4706
4707 // Keep moving out of bounds. Should generate a pointer move.
4708 y -= 50;
4709 processMove(mapper, x, y);
4710 processSync(mapper);
4711
4712 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4713 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4714 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4715 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4716 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4717 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4718 ASSERT_EQ(0, motionArgs.flags);
4719 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4720 ASSERT_EQ(0, motionArgs.buttonState);
4721 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004722 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004723 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004724 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004725 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4726 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4727 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4728 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4729 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4730
4731 // Release out of bounds. Should generate a pointer up.
4732 processUp(mapper);
4733 processSync(mapper);
4734
4735 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4736 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4737 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4738 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4739 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4740 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4741 ASSERT_EQ(0, motionArgs.flags);
4742 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4743 ASSERT_EQ(0, motionArgs.buttonState);
4744 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004745 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004746 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004747 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004748 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4749 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4750 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4751 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4752 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4753
4754 // Should not have sent any more keys or motions.
4755 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4756 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4757}
4758
4759TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004760 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00004761 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004762 prepareButtons();
4763 prepareAxes(POSITION);
4764 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00004765 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004766
arthurhungdcef2dc2020-08-11 14:47:50 +08004767 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004768
4769 NotifyMotionArgs motionArgs;
4770
4771 // Initially go down out of bounds.
4772 int32_t x = -10;
4773 int32_t y = -10;
4774 processDown(mapper, x, y);
4775 processSync(mapper);
4776
4777 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4778
4779 // Move into the display area. Should generate a pointer down.
4780 x = 50;
4781 y = 75;
4782 processMove(mapper, x, y);
4783 processSync(mapper);
4784
4785 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4786 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4787 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4788 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4789 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4790 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4791 ASSERT_EQ(0, motionArgs.flags);
4792 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4793 ASSERT_EQ(0, motionArgs.buttonState);
4794 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004795 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004796 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004797 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004798 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4799 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4800 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4801 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4802 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4803
4804 // Release. Should generate a pointer up.
4805 processUp(mapper);
4806 processSync(mapper);
4807
4808 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4809 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4810 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4811 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4812 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4813 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4814 ASSERT_EQ(0, motionArgs.flags);
4815 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4816 ASSERT_EQ(0, motionArgs.buttonState);
4817 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004818 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004819 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004820 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004821 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4822 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4823 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4824 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4825 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4826
4827 // Should not have sent any more keys or motions.
4828 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4829 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4830}
4831
Santos Cordonfa5cf462017-04-05 10:37:00 -07004832TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture_VirtualDisplay) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004833 addConfigurationProperty("touch.deviceType", "touchScreen");
4834 addConfigurationProperty("touch.displayId", VIRTUAL_DISPLAY_UNIQUE_ID);
4835
Michael Wrighta9cf4192022-12-01 23:46:39 +00004836 prepareVirtualDisplay(ui::ROTATION_0);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004837 prepareButtons();
4838 prepareAxes(POSITION);
4839 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00004840 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Santos Cordonfa5cf462017-04-05 10:37:00 -07004841
arthurhungdcef2dc2020-08-11 14:47:50 +08004842 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004843
4844 NotifyMotionArgs motionArgs;
4845
4846 // Down.
4847 int32_t x = 100;
4848 int32_t y = 125;
4849 processDown(mapper, x, y);
4850 processSync(mapper);
4851
4852 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4853 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4854 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4855 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
4856 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4857 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4858 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4859 ASSERT_EQ(0, motionArgs.flags);
4860 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4861 ASSERT_EQ(0, motionArgs.buttonState);
4862 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004863 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Santos Cordonfa5cf462017-04-05 10:37:00 -07004864 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004865 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004866 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4867 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
4868 1, 0, 0, 0, 0, 0, 0, 0));
4869 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
4870 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
4871 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4872
4873 // Move.
4874 x += 50;
4875 y += 75;
4876 processMove(mapper, x, y);
4877 processSync(mapper);
4878
4879 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4880 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4881 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4882 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
4883 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4884 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4885 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4886 ASSERT_EQ(0, motionArgs.flags);
4887 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4888 ASSERT_EQ(0, motionArgs.buttonState);
4889 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004890 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Santos Cordonfa5cf462017-04-05 10:37:00 -07004891 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004892 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004893 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4894 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
4895 1, 0, 0, 0, 0, 0, 0, 0));
4896 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
4897 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
4898 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4899
4900 // Up.
4901 processUp(mapper);
4902 processSync(mapper);
4903
4904 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4905 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4906 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4907 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
4908 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4909 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4910 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4911 ASSERT_EQ(0, motionArgs.flags);
4912 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4913 ASSERT_EQ(0, motionArgs.buttonState);
4914 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004915 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Santos Cordonfa5cf462017-04-05 10:37:00 -07004916 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004917 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004918 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4919 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
4920 1, 0, 0, 0, 0, 0, 0, 0));
4921 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
4922 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
4923 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4924
4925 // Should not have sent any more keys or motions.
4926 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4927 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4928}
4929
Michael Wrightd02c5b62014-02-10 15:10:22 -08004930TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004931 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00004932 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004933 prepareButtons();
4934 prepareAxes(POSITION);
4935 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00004936 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004937
arthurhungdcef2dc2020-08-11 14:47:50 +08004938 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004939
4940 NotifyMotionArgs motionArgs;
4941
4942 // Down.
4943 int32_t x = 100;
4944 int32_t y = 125;
4945 processDown(mapper, x, y);
4946 processSync(mapper);
4947
4948 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4949 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4950 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4951 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4952 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4953 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4954 ASSERT_EQ(0, motionArgs.flags);
4955 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4956 ASSERT_EQ(0, motionArgs.buttonState);
4957 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004958 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004959 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004960 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004961 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4962 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4963 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4964 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4965 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4966
4967 // Move.
4968 x += 50;
4969 y += 75;
4970 processMove(mapper, x, y);
4971 processSync(mapper);
4972
4973 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4974 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4975 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4976 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4977 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4978 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4979 ASSERT_EQ(0, motionArgs.flags);
4980 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4981 ASSERT_EQ(0, motionArgs.buttonState);
4982 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004983 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004984 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004985 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004986 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4987 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4988 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4989 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4990 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4991
4992 // Up.
4993 processUp(mapper);
4994 processSync(mapper);
4995
4996 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4997 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4998 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4999 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5000 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5001 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5002 ASSERT_EQ(0, motionArgs.flags);
5003 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5004 ASSERT_EQ(0, motionArgs.buttonState);
5005 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005006 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005007 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005008 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005009 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5010 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5011 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5012 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5013 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5014
5015 // Should not have sent any more keys or motions.
5016 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5017 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5018}
5019
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005020TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_DoesNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005021 addConfigurationProperty("touch.deviceType", "touchScreen");
5022 prepareButtons();
5023 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005024 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
5025 // need to be rotated. Touchscreens are orientation-aware by default.
Arpit Singha8c236b2023-04-25 13:56:05 +00005026 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005027
5028 NotifyMotionArgs args;
5029
5030 // Rotation 90.
Michael Wrighta9cf4192022-12-01 23:46:39 +00005031 prepareDisplay(ui::ROTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005032 processDown(mapper, toRawX(50), toRawY(75));
5033 processSync(mapper);
5034
5035 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5036 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5037 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5038
5039 processUp(mapper);
5040 processSync(mapper);
5041 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5042}
5043
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005044TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_RotatesMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005045 addConfigurationProperty("touch.deviceType", "touchScreen");
5046 prepareButtons();
5047 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005048 // Since InputReader works in the un-rotated coordinate space, only devices that are not
5049 // orientation-aware are affected by display rotation.
5050 addConfigurationProperty("touch.orientationAware", "0");
Arpit Singha8c236b2023-04-25 13:56:05 +00005051 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005052
5053 NotifyMotionArgs args;
5054
5055 // Rotation 0.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005056 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005057 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005058 processDown(mapper, toRawX(50), toRawY(75));
5059 processSync(mapper);
5060
5061 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5062 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5063 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5064
5065 processUp(mapper);
5066 processSync(mapper);
5067 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5068
5069 // Rotation 90.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005070 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005071 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00005072 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005073 processSync(mapper);
5074
5075 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5076 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5077 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5078
5079 processUp(mapper);
5080 processSync(mapper);
5081 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5082
5083 // Rotation 180.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005084 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005085 prepareDisplay(ui::ROTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005086 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
5087 processSync(mapper);
5088
5089 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5090 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5091 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5092
5093 processUp(mapper);
5094 processSync(mapper);
5095 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5096
5097 // Rotation 270.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005098 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005099 prepareDisplay(ui::ROTATION_270);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00005100 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005101 processSync(mapper);
5102
5103 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5104 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5105 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5106
5107 processUp(mapper);
5108 processSync(mapper);
5109 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5110}
5111
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005112TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation0_RotatesMotions) {
5113 addConfigurationProperty("touch.deviceType", "touchScreen");
5114 prepareButtons();
5115 prepareAxes(POSITION);
5116 addConfigurationProperty("touch.orientationAware", "1");
5117 addConfigurationProperty("touch.orientation", "ORIENTATION_0");
5118 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005119 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +00005120 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005121 NotifyMotionArgs args;
5122
5123 // Orientation 0.
5124 processDown(mapper, toRawX(50), toRawY(75));
5125 processSync(mapper);
5126
5127 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5128 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5129 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5130
5131 processUp(mapper);
5132 processSync(mapper);
5133 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5134}
5135
5136TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation90_RotatesMotions) {
5137 addConfigurationProperty("touch.deviceType", "touchScreen");
5138 prepareButtons();
5139 prepareAxes(POSITION);
5140 addConfigurationProperty("touch.orientationAware", "1");
5141 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
5142 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005143 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +00005144 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005145 NotifyMotionArgs args;
5146
5147 // Orientation 90.
5148 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
5149 processSync(mapper);
5150
5151 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5152 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5153 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5154
5155 processUp(mapper);
5156 processSync(mapper);
5157 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5158}
5159
5160TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation180_RotatesMotions) {
5161 addConfigurationProperty("touch.deviceType", "touchScreen");
5162 prepareButtons();
5163 prepareAxes(POSITION);
5164 addConfigurationProperty("touch.orientationAware", "1");
5165 addConfigurationProperty("touch.orientation", "ORIENTATION_180");
5166 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005167 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +00005168 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005169 NotifyMotionArgs args;
5170
5171 // Orientation 180.
5172 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
5173 processSync(mapper);
5174
5175 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5176 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5177 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5178
5179 processUp(mapper);
5180 processSync(mapper);
5181 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5182}
5183
5184TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation270_RotatesMotions) {
5185 addConfigurationProperty("touch.deviceType", "touchScreen");
5186 prepareButtons();
5187 prepareAxes(POSITION);
5188 addConfigurationProperty("touch.orientationAware", "1");
5189 addConfigurationProperty("touch.orientation", "ORIENTATION_270");
5190 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005191 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +00005192 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005193 NotifyMotionArgs args;
5194
5195 // Orientation 270.
5196 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
5197 processSync(mapper);
5198
5199 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5200 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5201 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5202
5203 processUp(mapper);
5204 processSync(mapper);
5205 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5206}
5207
5208TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationSpecified_RotatesMotionWithDisplay) {
5209 addConfigurationProperty("touch.deviceType", "touchScreen");
5210 prepareButtons();
5211 prepareAxes(POSITION);
5212 // Since InputReader works in the un-rotated coordinate space, only devices that are not
5213 // orientation-aware are affected by display rotation.
5214 addConfigurationProperty("touch.orientationAware", "0");
5215 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
Arpit Singha8c236b2023-04-25 13:56:05 +00005216 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005217
5218 NotifyMotionArgs args;
5219
5220 // Orientation 90, Rotation 0.
5221 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005222 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005223 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
5224 processSync(mapper);
5225
5226 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5227 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5228 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5229
5230 processUp(mapper);
5231 processSync(mapper);
5232 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5233
5234 // Orientation 90, Rotation 90.
5235 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005236 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00005237 processDown(mapper, toRawX(50), toRawY(75));
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005238 processSync(mapper);
5239
5240 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5241 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5242 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5243
5244 processUp(mapper);
5245 processSync(mapper);
5246 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5247
5248 // Orientation 90, Rotation 180.
5249 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005250 prepareDisplay(ui::ROTATION_180);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005251 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
5252 processSync(mapper);
5253
5254 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5255 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5256 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5257
5258 processUp(mapper);
5259 processSync(mapper);
5260 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5261
5262 // Orientation 90, Rotation 270.
5263 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005264 prepareDisplay(ui::ROTATION_270);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00005265 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 -07005266 processSync(mapper);
5267
5268 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5269 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5270 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5271
5272 processUp(mapper);
5273 processSync(mapper);
5274 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5275}
5276
Prabir Pradhan675f25a2022-11-10 22:04:07 +00005277TEST_F(SingleTouchInputMapperTest, Process_IgnoresTouchesOutsidePhysicalFrame) {
5278 addConfigurationProperty("touch.deviceType", "touchScreen");
5279 prepareButtons();
5280 prepareAxes(POSITION);
5281 addConfigurationProperty("touch.orientationAware", "1");
5282 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +00005283 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan675f25a2022-11-10 22:04:07 +00005284
5285 // Set a physical frame in the display viewport.
5286 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
5287 viewport->physicalLeft = 20;
5288 viewport->physicalTop = 600;
5289 viewport->physicalRight = 30;
5290 viewport->physicalBottom = 610;
5291 mFakePolicy->updateViewport(*viewport);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00005292 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhan675f25a2022-11-10 22:04:07 +00005293
5294 // Start the touch.
5295 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
5296 processSync(mapper);
5297
5298 // Expect all input starting outside the physical frame to be ignored.
5299 const std::array<Point, 6> outsidePoints = {
5300 {{0, 0}, {19, 605}, {31, 605}, {25, 599}, {25, 611}, {DISPLAY_WIDTH, DISPLAY_HEIGHT}}};
5301 for (const auto& p : outsidePoints) {
5302 processMove(mapper, toRawX(p.x), toRawY(p.y));
5303 processSync(mapper);
5304 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5305 }
5306
5307 // Move the touch into the physical frame.
5308 processMove(mapper, toRawX(25), toRawY(605));
5309 processSync(mapper);
5310 NotifyMotionArgs args;
5311 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5312 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
5313 EXPECT_NEAR(25, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5314 EXPECT_NEAR(605, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5315
5316 // Once the touch down is reported, continue reporting input, even if it is outside the frame.
5317 for (const auto& p : outsidePoints) {
5318 processMove(mapper, toRawX(p.x), toRawY(p.y));
5319 processSync(mapper);
5320 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5321 EXPECT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
5322 EXPECT_NEAR(p.x, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5323 EXPECT_NEAR(p.y, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5324 }
5325
5326 processUp(mapper);
5327 processSync(mapper);
5328 EXPECT_NO_FATAL_FAILURE(
5329 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
5330}
5331
Harry Cutts1db43992023-06-19 17:05:07 +00005332TEST_F(SingleTouchInputMapperTest, Process_DoesntCheckPhysicalFrameForTouchpads) {
5333 std::shared_ptr<FakePointerController> fakePointerController =
5334 std::make_shared<FakePointerController>();
5335 mFakePolicy->setPointerController(fakePointerController);
5336
5337 addConfigurationProperty("touch.deviceType", "pointer");
5338 prepareAxes(POSITION);
5339 prepareDisplay(ui::ROTATION_0);
5340 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
5341
5342 // Set a physical frame in the display viewport.
5343 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
5344 viewport->physicalLeft = 20;
5345 viewport->physicalTop = 600;
5346 viewport->physicalRight = 30;
5347 viewport->physicalBottom = 610;
5348 mFakePolicy->updateViewport(*viewport);
5349 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
5350
5351 // Start the touch.
5352 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
5353 processSync(mapper);
5354
5355 // Expect all input starting outside the physical frame to result in NotifyMotionArgs being
5356 // produced.
5357 const std::array<Point, 6> outsidePoints = {
5358 {{0, 0}, {19, 605}, {31, 605}, {25, 599}, {25, 611}, {DISPLAY_WIDTH, DISPLAY_HEIGHT}}};
5359 for (const auto& p : outsidePoints) {
5360 processMove(mapper, toRawX(p.x), toRawY(p.y));
5361 processSync(mapper);
5362 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5363 }
5364}
5365
Michael Wrightd02c5b62014-02-10 15:10:22 -08005366TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005367 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005368 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005369 prepareButtons();
5370 prepareAxes(POSITION | PRESSURE | TOOL | DISTANCE | TILT);
Arpit Singha8c236b2023-04-25 13:56:05 +00005371 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005372
5373 // These calculations are based on the input device calibration documentation.
5374 int32_t rawX = 100;
5375 int32_t rawY = 200;
5376 int32_t rawPressure = 10;
5377 int32_t rawToolMajor = 12;
5378 int32_t rawDistance = 2;
5379 int32_t rawTiltX = 30;
5380 int32_t rawTiltY = 110;
5381
5382 float x = toDisplayX(rawX);
5383 float y = toDisplayY(rawY);
5384 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
5385 float size = float(rawToolMajor) / RAW_TOOL_MAX;
5386 float tool = float(rawToolMajor) * GEOMETRIC_SCALE;
5387 float distance = float(rawDistance);
5388
5389 float tiltCenter = (RAW_TILT_MAX + RAW_TILT_MIN) * 0.5f;
5390 float tiltScale = M_PI / 180;
5391 float tiltXAngle = (rawTiltX - tiltCenter) * tiltScale;
5392 float tiltYAngle = (rawTiltY - tiltCenter) * tiltScale;
5393 float orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
5394 float tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
5395
5396 processDown(mapper, rawX, rawY);
5397 processPressure(mapper, rawPressure);
5398 processToolMajor(mapper, rawToolMajor);
5399 processDistance(mapper, rawDistance);
5400 processTilt(mapper, rawTiltX, rawTiltY);
5401 processSync(mapper);
5402
5403 NotifyMotionArgs args;
5404 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5405 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5406 x, y, pressure, size, tool, tool, tool, tool, orientation, distance));
5407 ASSERT_EQ(tilt, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TILT));
5408}
5409
Jason Gerecke489fda82012-09-07 17:19:40 -07005410TEST_F(SingleTouchInputMapperTest, Process_XYAxes_AffineCalibration) {
Jason Gerecke489fda82012-09-07 17:19:40 -07005411 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005412 prepareDisplay(ui::ROTATION_0);
Jason Gerecke489fda82012-09-07 17:19:40 -07005413 prepareLocationCalibration();
5414 prepareButtons();
5415 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00005416 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Jason Gerecke489fda82012-09-07 17:19:40 -07005417
5418 int32_t rawX = 100;
5419 int32_t rawY = 200;
5420
5421 float x = toDisplayX(toCookedX(rawX, rawY));
5422 float y = toDisplayY(toCookedY(rawX, rawY));
5423
5424 processDown(mapper, rawX, rawY);
5425 processSync(mapper);
5426
5427 NotifyMotionArgs args;
5428 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5429 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5430 x, y, 1, 0, 0, 0, 0, 0, 0, 0));
5431}
5432
Michael Wrightd02c5b62014-02-10 15:10:22 -08005433TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005434 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005435 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005436 prepareButtons();
5437 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00005438 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005439
5440 NotifyMotionArgs motionArgs;
5441 NotifyKeyArgs keyArgs;
5442
5443 processDown(mapper, 100, 200);
5444 processSync(mapper);
5445 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5446 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5447 ASSERT_EQ(0, motionArgs.buttonState);
5448
5449 // press BTN_LEFT, release BTN_LEFT
5450 processKey(mapper, BTN_LEFT, 1);
5451 processSync(mapper);
5452 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5453 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5454 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
5455
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005456 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5457 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5458 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
5459
Michael Wrightd02c5b62014-02-10 15:10:22 -08005460 processKey(mapper, BTN_LEFT, 0);
5461 processSync(mapper);
5462 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005463 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005464 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005465
5466 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005467 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005468 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005469
5470 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
5471 processKey(mapper, BTN_RIGHT, 1);
5472 processKey(mapper, BTN_MIDDLE, 1);
5473 processSync(mapper);
5474 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5475 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5476 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5477 motionArgs.buttonState);
5478
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005479 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5480 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5481 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
5482
5483 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5484 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5485 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5486 motionArgs.buttonState);
5487
Michael Wrightd02c5b62014-02-10 15:10:22 -08005488 processKey(mapper, BTN_RIGHT, 0);
5489 processSync(mapper);
5490 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005491 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005492 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005493
5494 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005495 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005496 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005497
5498 processKey(mapper, BTN_MIDDLE, 0);
5499 processSync(mapper);
5500 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005501 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005502 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005503
5504 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005505 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005506 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005507
5508 // press BTN_BACK, release BTN_BACK
5509 processKey(mapper, BTN_BACK, 1);
5510 processSync(mapper);
5511 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5512 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5513 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005514
Michael Wrightd02c5b62014-02-10 15:10:22 -08005515 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005516 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005517 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5518
5519 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5520 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5521 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005522
5523 processKey(mapper, BTN_BACK, 0);
5524 processSync(mapper);
5525 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005526 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005527 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005528
5529 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005530 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005531 ASSERT_EQ(0, motionArgs.buttonState);
5532
Michael Wrightd02c5b62014-02-10 15:10:22 -08005533 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5534 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5535 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
5536
5537 // press BTN_SIDE, release BTN_SIDE
5538 processKey(mapper, BTN_SIDE, 1);
5539 processSync(mapper);
5540 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5541 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5542 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005543
Michael Wrightd02c5b62014-02-10 15:10:22 -08005544 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005545 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005546 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5547
5548 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5549 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5550 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005551
5552 processKey(mapper, BTN_SIDE, 0);
5553 processSync(mapper);
5554 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005555 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005556 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005557
5558 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005559 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005560 ASSERT_EQ(0, motionArgs.buttonState);
5561
Michael Wrightd02c5b62014-02-10 15:10:22 -08005562 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5563 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5564 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
5565
5566 // press BTN_FORWARD, release BTN_FORWARD
5567 processKey(mapper, BTN_FORWARD, 1);
5568 processSync(mapper);
5569 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5570 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5571 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005572
Michael Wrightd02c5b62014-02-10 15:10:22 -08005573 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005574 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005575 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5576
5577 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5578 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5579 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005580
5581 processKey(mapper, BTN_FORWARD, 0);
5582 processSync(mapper);
5583 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005584 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005585 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005586
5587 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005588 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005589 ASSERT_EQ(0, motionArgs.buttonState);
5590
Michael Wrightd02c5b62014-02-10 15:10:22 -08005591 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5592 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5593 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
5594
5595 // press BTN_EXTRA, release BTN_EXTRA
5596 processKey(mapper, BTN_EXTRA, 1);
5597 processSync(mapper);
5598 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5599 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5600 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005601
Michael Wrightd02c5b62014-02-10 15:10:22 -08005602 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005603 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005604 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5605
5606 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5607 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5608 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005609
5610 processKey(mapper, BTN_EXTRA, 0);
5611 processSync(mapper);
5612 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005613 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005614 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005615
5616 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005617 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005618 ASSERT_EQ(0, motionArgs.buttonState);
5619
Michael Wrightd02c5b62014-02-10 15:10:22 -08005620 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5621 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5622 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
5623
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005624 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5625
Michael Wrightd02c5b62014-02-10 15:10:22 -08005626 // press BTN_STYLUS, release BTN_STYLUS
5627 processKey(mapper, BTN_STYLUS, 1);
5628 processSync(mapper);
5629 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5630 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005631 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
5632
5633 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5634 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5635 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005636
5637 processKey(mapper, BTN_STYLUS, 0);
5638 processSync(mapper);
5639 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005640 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005641 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005642
5643 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005644 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005645 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005646
5647 // press BTN_STYLUS2, release BTN_STYLUS2
5648 processKey(mapper, BTN_STYLUS2, 1);
5649 processSync(mapper);
5650 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5651 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005652 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
5653
5654 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5655 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5656 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005657
5658 processKey(mapper, BTN_STYLUS2, 0);
5659 processSync(mapper);
5660 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005661 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005662 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005663
5664 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005665 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005666 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005667
5668 // release touch
5669 processUp(mapper);
5670 processSync(mapper);
5671 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5672 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5673 ASSERT_EQ(0, motionArgs.buttonState);
5674}
5675
5676TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
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
5685 // default tool type is finger
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);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005690 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005691
5692 // eraser
5693 processKey(mapper, BTN_TOOL_RUBBER, 1);
5694 processSync(mapper);
5695 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5696 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005697 ASSERT_EQ(ToolType::ERASER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005698
5699 // stylus
5700 processKey(mapper, BTN_TOOL_RUBBER, 0);
5701 processKey(mapper, BTN_TOOL_PEN, 1);
5702 processSync(mapper);
5703 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5704 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005705 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005706
5707 // brush
5708 processKey(mapper, BTN_TOOL_PEN, 0);
5709 processKey(mapper, BTN_TOOL_BRUSH, 1);
5710 processSync(mapper);
5711 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5712 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005713 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005714
5715 // pencil
5716 processKey(mapper, BTN_TOOL_BRUSH, 0);
5717 processKey(mapper, BTN_TOOL_PENCIL, 1);
5718 processSync(mapper);
5719 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5720 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005721 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005722
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08005723 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08005724 processKey(mapper, BTN_TOOL_PENCIL, 0);
5725 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
5726 processSync(mapper);
5727 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5728 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005729 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005730
5731 // mouse
5732 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
5733 processKey(mapper, BTN_TOOL_MOUSE, 1);
5734 processSync(mapper);
5735 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5736 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005737 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005738
5739 // lens
5740 processKey(mapper, BTN_TOOL_MOUSE, 0);
5741 processKey(mapper, BTN_TOOL_LENS, 1);
5742 processSync(mapper);
5743 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5744 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005745 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005746
5747 // double-tap
5748 processKey(mapper, BTN_TOOL_LENS, 0);
5749 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
5750 processSync(mapper);
5751 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5752 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005753 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005754
5755 // triple-tap
5756 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
5757 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
5758 processSync(mapper);
5759 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5760 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005761 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005762
5763 // quad-tap
5764 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
5765 processKey(mapper, BTN_TOOL_QUADTAP, 1);
5766 processSync(mapper);
5767 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5768 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005769 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005770
5771 // finger
5772 processKey(mapper, BTN_TOOL_QUADTAP, 0);
5773 processKey(mapper, BTN_TOOL_FINGER, 1);
5774 processSync(mapper);
5775 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5776 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005777 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005778
5779 // stylus trumps finger
5780 processKey(mapper, BTN_TOOL_PEN, 1);
5781 processSync(mapper);
5782 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5783 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005784 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005785
5786 // eraser trumps stylus
5787 processKey(mapper, BTN_TOOL_RUBBER, 1);
5788 processSync(mapper);
5789 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5790 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005791 ASSERT_EQ(ToolType::ERASER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005792
5793 // mouse trumps eraser
5794 processKey(mapper, BTN_TOOL_MOUSE, 1);
5795 processSync(mapper);
5796 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5797 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005798 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005799
5800 // back to default tool type
5801 processKey(mapper, BTN_TOOL_MOUSE, 0);
5802 processKey(mapper, BTN_TOOL_RUBBER, 0);
5803 processKey(mapper, BTN_TOOL_PEN, 0);
5804 processKey(mapper, BTN_TOOL_FINGER, 0);
5805 processSync(mapper);
5806 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5807 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005808 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005809}
5810
5811TEST_F(SingleTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005812 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005813 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005814 prepareButtons();
5815 prepareAxes(POSITION);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005816 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_FINGER, 0, AKEYCODE_UNKNOWN, 0);
Arpit Singha8c236b2023-04-25 13:56:05 +00005817 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005818
5819 NotifyMotionArgs motionArgs;
5820
5821 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
5822 processKey(mapper, BTN_TOOL_FINGER, 1);
5823 processMove(mapper, 100, 200);
5824 processSync(mapper);
5825 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5826 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5827 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5828 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5829
5830 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5831 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5832 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5833 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5834
5835 // move a little
5836 processMove(mapper, 150, 250);
5837 processSync(mapper);
5838 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5839 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5840 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5841 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5842
5843 // down when BTN_TOUCH is pressed, pressure defaults to 1
5844 processKey(mapper, BTN_TOUCH, 1);
5845 processSync(mapper);
5846 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5847 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5848 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5849 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5850
5851 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5852 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5853 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5854 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5855
5856 // up when BTN_TOUCH is released, hover restored
5857 processKey(mapper, BTN_TOUCH, 0);
5858 processSync(mapper);
5859 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5860 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5861 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5862 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5863
5864 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5865 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5866 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5867 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5868
5869 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5870 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5871 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5872 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5873
5874 // exit hover when pointer goes away
5875 processKey(mapper, BTN_TOOL_FINGER, 0);
5876 processSync(mapper);
5877 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5878 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5879 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5880 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5881}
5882
5883TEST_F(SingleTouchInputMapperTest, Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005884 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005885 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005886 prepareButtons();
5887 prepareAxes(POSITION | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +00005888 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005889
5890 NotifyMotionArgs motionArgs;
5891
5892 // initially hovering because pressure is 0
5893 processDown(mapper, 100, 200);
5894 processPressure(mapper, 0);
5895 processSync(mapper);
5896 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5897 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5898 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5899 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5900
5901 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5902 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5903 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5904 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5905
5906 // move a little
5907 processMove(mapper, 150, 250);
5908 processSync(mapper);
5909 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5910 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5911 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5912 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5913
5914 // down when pressure is non-zero
5915 processPressure(mapper, RAW_PRESSURE_MAX);
5916 processSync(mapper);
5917 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5918 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5919 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5920 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5921
5922 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5923 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5924 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5925 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5926
5927 // up when pressure becomes 0, hover restored
5928 processPressure(mapper, 0);
5929 processSync(mapper);
5930 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5931 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5932 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5933 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5934
5935 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5936 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5937 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5938 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5939
5940 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5941 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5942 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5943 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5944
5945 // exit hover when pointer goes away
5946 processUp(mapper);
5947 processSync(mapper);
5948 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5949 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5950 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5951 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5952}
5953
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00005954TEST_F(SingleTouchInputMapperTest, Reset_CancelsOngoingGesture) {
5955 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005956 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00005957 prepareButtons();
5958 prepareAxes(POSITION | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +00005959 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00005960
5961 // Touch down.
5962 processDown(mapper, 100, 200);
5963 processPressure(mapper, 1);
5964 processSync(mapper);
5965 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
5966 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
5967
5968 // Reset the mapper. This should cancel the ongoing gesture.
5969 resetMapper(mapper, ARBITRARY_TIME);
5970 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
5971 WithMotionAction(AMOTION_EVENT_ACTION_CANCEL)));
5972
5973 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5974}
5975
Prabir Pradhanafabcde2022-09-27 19:32:43 +00005976TEST_F(SingleTouchInputMapperTest, Reset_RecreatesTouchState) {
5977 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005978 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00005979 prepareButtons();
5980 prepareAxes(POSITION | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +00005981 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanafabcde2022-09-27 19:32:43 +00005982
5983 // Set the initial state for the touch pointer.
5984 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_X, 100);
5985 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_Y, 200);
5986 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MAX);
5987 mFakeEventHub->setScanCodeState(EVENTHUB_ID, BTN_TOUCH, 1);
5988
5989 // Reset the mapper. When the mapper is reset, we expect it to attempt to recreate the touch
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00005990 // state by reading the current axis values. Since there was no ongoing gesture, calling reset
5991 // does not generate any events.
5992 resetMapper(mapper, ARBITRARY_TIME);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00005993
5994 // Send a sync to simulate an empty touch frame where nothing changes. The mapper should use
5995 // the recreated touch state to generate a down event.
5996 processSync(mapper);
5997 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
5998 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithPressure(1.f))));
5999
6000 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6001}
6002
lilinnan687e58f2022-07-19 16:00:50 +08006003TEST_F(SingleTouchInputMapperTest,
6004 Process_WhenViewportDisplayIdChanged_TouchIsCanceledAndDeviceIsReset) {
6005 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006006 prepareDisplay(ui::ROTATION_0);
lilinnan687e58f2022-07-19 16:00:50 +08006007 prepareButtons();
6008 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00006009 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
lilinnan687e58f2022-07-19 16:00:50 +08006010 NotifyMotionArgs motionArgs;
6011
6012 // Down.
Prabir Pradhan3e5ec702022-07-29 16:26:24 +00006013 processDown(mapper, 100, 200);
lilinnan687e58f2022-07-19 16:00:50 +08006014 processSync(mapper);
6015
6016 // We should receive a down event
6017 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6018 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6019
6020 // Change display id
6021 clearViewports();
6022 prepareSecondaryDisplay(ViewportType::INTERNAL);
6023
6024 // We should receive a cancel event
6025 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6026 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
6027 // Then receive reset called
6028 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6029}
6030
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006031TEST_F(SingleTouchInputMapperTest,
6032 Process_WhenViewportActiveStatusChanged_TouchIsCanceledAndDeviceIsReset) {
6033 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006034 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006035 prepareButtons();
6036 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00006037 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006038 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6039 NotifyMotionArgs motionArgs;
6040
6041 // Start a new gesture.
6042 processDown(mapper, 100, 200);
6043 processSync(mapper);
6044 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6045 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6046
6047 // Make the viewport inactive. This will put the device in disabled mode.
6048 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6049 viewport->isActive = false;
6050 mFakePolicy->updateViewport(*viewport);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00006051 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006052
6053 // We should receive a cancel event for the ongoing gesture.
6054 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6055 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
6056 // Then we should be notified that the device was reset.
6057 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6058
6059 // No events are generated while the viewport is inactive.
6060 processMove(mapper, 101, 201);
6061 processSync(mapper);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006062 processUp(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006063 processSync(mapper);
6064 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6065
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006066 // Start a new gesture while the viewport is still inactive.
6067 processDown(mapper, 300, 400);
6068 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_X, 300);
6069 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_Y, 400);
6070 mFakeEventHub->setScanCodeState(EVENTHUB_ID, BTN_TOUCH, 1);
6071 processSync(mapper);
6072
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006073 // Make the viewport active again. The device should resume processing events.
6074 viewport->isActive = true;
6075 mFakePolicy->updateViewport(*viewport);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00006076 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006077
6078 // The device is reset because it changes back to direct mode, without generating any events.
6079 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6080 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6081
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006082 // In the next sync, the touch state that was recreated when the device was reset is reported.
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006083 processSync(mapper);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006084 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6085 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006086
6087 // No more events.
6088 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6089 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
6090}
6091
Prabir Pradhan211ba622022-10-31 21:09:21 +00006092TEST_F(SingleTouchInputMapperTest, ButtonIsReleasedOnTouchUp) {
6093 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006094 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan211ba622022-10-31 21:09:21 +00006095 prepareButtons();
6096 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00006097 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan211ba622022-10-31 21:09:21 +00006098 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6099
6100 // Press a stylus button.
6101 processKey(mapper, BTN_STYLUS, 1);
6102 processSync(mapper);
6103
6104 // Start a touch gesture and ensure the BUTTON_PRESS event is generated.
6105 processDown(mapper, 100, 200);
6106 processSync(mapper);
6107 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6108 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
6109 WithCoords(toDisplayX(100), toDisplayY(200)),
6110 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
6111 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6112 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
6113 WithCoords(toDisplayX(100), toDisplayY(200)),
6114 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
6115
6116 // Release the touch gesture. Ensure that the BUTTON_RELEASE event is generated even though
6117 // the button has not actually been released, since there will be no pointers through which the
6118 // button state can be reported. The event is generated at the location of the pointer before
6119 // it went up.
6120 processUp(mapper);
6121 processSync(mapper);
6122 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6123 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
6124 WithCoords(toDisplayX(100), toDisplayY(200)), WithButtonState(0))));
6125 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6126 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
6127 WithCoords(toDisplayX(100), toDisplayY(200)), WithButtonState(0))));
6128}
6129
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00006130TEST_F(SingleTouchInputMapperTest, StylusButtonMotionEventsDisabled) {
6131 addConfigurationProperty("touch.deviceType", "touchScreen");
6132 prepareDisplay(ui::ROTATION_0);
6133 prepareButtons();
6134 prepareAxes(POSITION);
6135
6136 mFakePolicy->setStylusButtonMotionEventsEnabled(false);
6137
Arpit Singha8c236b2023-04-25 13:56:05 +00006138 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00006139 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6140
6141 // Press a stylus button.
6142 processKey(mapper, BTN_STYLUS, 1);
6143 processSync(mapper);
6144
6145 // Start a touch gesture and ensure that the stylus button is not reported.
6146 processDown(mapper, 100, 200);
6147 processSync(mapper);
6148 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6149 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithButtonState(0))));
6150
6151 // Release and press the stylus button again.
6152 processKey(mapper, BTN_STYLUS, 0);
6153 processSync(mapper);
6154 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6155 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
6156 processKey(mapper, BTN_STYLUS, 1);
6157 processSync(mapper);
6158 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6159 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
6160
6161 // Release the touch gesture.
6162 processUp(mapper);
6163 processSync(mapper);
6164 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6165 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithButtonState(0))));
6166
6167 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6168}
6169
Ambrus Weisz7bc23bf2022-10-04 13:13:07 +00006170TEST_F(SingleTouchInputMapperTest, WhenDeviceTypeIsSetToTouchNavigation_setsCorrectType) {
6171 mFakePolicy->addDeviceTypeAssociation(DEVICE_LOCATION, "touchNavigation");
6172 prepareDisplay(ui::ROTATION_0);
6173 prepareButtons();
6174 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00006175 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Ambrus Weisz7bc23bf2022-10-04 13:13:07 +00006176 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6177
6178 ASSERT_EQ(AINPUT_SOURCE_TOUCH_NAVIGATION, mapper.getSources());
6179}
6180
Seunghwan Choi356026c2023-02-01 14:37:25 +09006181TEST_F(SingleTouchInputMapperTest, Process_WhenConfigEnabled_ShouldShowDirectStylusPointer) {
6182 std::shared_ptr<FakePointerController> fakePointerController =
6183 std::make_shared<FakePointerController>();
6184 addConfigurationProperty("touch.deviceType", "touchScreen");
6185 prepareDisplay(ui::ROTATION_0);
6186 prepareButtons();
6187 prepareAxes(POSITION);
6188 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
6189 mFakePolicy->setPointerController(fakePointerController);
6190 mFakePolicy->setStylusPointerIconEnabled(true);
Arpit Singha8c236b2023-04-25 13:56:05 +00006191 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Seunghwan Choi356026c2023-02-01 14:37:25 +09006192
6193 processKey(mapper, BTN_TOOL_PEN, 1);
6194 processMove(mapper, 100, 200);
6195 processSync(mapper);
6196 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6197 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006198 WithToolType(ToolType::STYLUS),
Seunghwan Choi356026c2023-02-01 14:37:25 +09006199 WithPointerCoords(0, toDisplayX(100), toDisplayY(200)))));
6200 ASSERT_TRUE(fakePointerController->isPointerShown());
6201 ASSERT_NO_FATAL_FAILURE(
6202 fakePointerController->assertPosition(toDisplayX(100), toDisplayY(200)));
6203}
6204
6205TEST_F(SingleTouchInputMapperTest, Process_WhenConfigDisabled_ShouldNotShowDirectStylusPointer) {
6206 std::shared_ptr<FakePointerController> fakePointerController =
6207 std::make_shared<FakePointerController>();
6208 addConfigurationProperty("touch.deviceType", "touchScreen");
6209 prepareDisplay(ui::ROTATION_0);
6210 prepareButtons();
6211 prepareAxes(POSITION);
6212 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
6213 mFakePolicy->setPointerController(fakePointerController);
6214 mFakePolicy->setStylusPointerIconEnabled(false);
Arpit Singha8c236b2023-04-25 13:56:05 +00006215 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Seunghwan Choi356026c2023-02-01 14:37:25 +09006216
6217 processKey(mapper, BTN_TOOL_PEN, 1);
6218 processMove(mapper, 100, 200);
6219 processSync(mapper);
6220 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6221 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006222 WithToolType(ToolType::STYLUS),
Seunghwan Choi356026c2023-02-01 14:37:25 +09006223 WithPointerCoords(0, toDisplayX(100), toDisplayY(200)))));
6224 ASSERT_FALSE(fakePointerController->isPointerShown());
6225}
6226
Ambrus Weisz7b6e16b2022-12-16 17:54:57 +00006227TEST_F(SingleTouchInputMapperTest, WhenDeviceTypeIsChangedToTouchNavigation_updatesDeviceType) {
6228 // Initialize the device without setting device source to touch navigation.
6229 addConfigurationProperty("touch.deviceType", "touchScreen");
6230 prepareDisplay(ui::ROTATION_0);
6231 prepareButtons();
6232 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00006233 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Ambrus Weisz7b6e16b2022-12-16 17:54:57 +00006234
6235 // Ensure that the device is created as a touchscreen, not touch navigation.
6236 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
6237
6238 // Add device type association after the device was created.
6239 mFakePolicy->addDeviceTypeAssociation(DEVICE_LOCATION, "touchNavigation");
6240
6241 // Send update to the mapper.
6242 std::list<NotifyArgs> unused2 =
6243 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00006244 InputReaderConfiguration::Change::DEVICE_TYPE /*changes*/);
Ambrus Weisz7b6e16b2022-12-16 17:54:57 +00006245
6246 // Check whether device type update was successful.
6247 ASSERT_EQ(AINPUT_SOURCE_TOUCH_NAVIGATION, mDevice->getSources());
6248}
6249
Prabir Pradhane1e309a2022-11-29 02:54:27 +00006250TEST_F(SingleTouchInputMapperTest, HoverEventsOutsidePhysicalFrameAreIgnored) {
6251 // Initialize the device without setting device source to touch navigation.
6252 addConfigurationProperty("touch.deviceType", "touchScreen");
6253 prepareDisplay(ui::ROTATION_0);
6254 prepareButtons();
6255 prepareAxes(POSITION);
6256 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
6257
6258 // Set a physical frame in the display viewport.
6259 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6260 viewport->physicalLeft = 0;
6261 viewport->physicalTop = 0;
6262 viewport->physicalRight = DISPLAY_WIDTH / 2;
6263 viewport->physicalBottom = DISPLAY_HEIGHT / 2;
6264 mFakePolicy->updateViewport(*viewport);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00006265 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhane1e309a2022-11-29 02:54:27 +00006266
Arpit Singha8c236b2023-04-25 13:56:05 +00006267 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhane1e309a2022-11-29 02:54:27 +00006268
6269 // Hovering inside the physical frame produces events.
6270 processKey(mapper, BTN_TOOL_PEN, 1);
6271 processMove(mapper, RAW_X_MIN + 1, RAW_Y_MIN + 1);
6272 processSync(mapper);
6273 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6274 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)));
6275 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6276 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE)));
6277
6278 // Leaving the physical frame ends the hovering gesture.
6279 processMove(mapper, RAW_X_MAX - 1, RAW_Y_MAX - 1);
6280 processSync(mapper);
6281 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6282 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT)));
6283
6284 // Moving outside the physical frame does not produce events.
6285 processMove(mapper, RAW_X_MAX - 2, RAW_Y_MAX - 2);
6286 processSync(mapper);
6287 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6288
6289 // Re-entering the physical frame produces events.
6290 processMove(mapper, RAW_X_MIN, RAW_Y_MIN);
6291 processSync(mapper);
6292 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6293 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)));
6294 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6295 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE)));
6296}
6297
Prabir Pradhan5632d622021-09-06 07:57:20 -07006298// --- TouchDisplayProjectionTest ---
6299
6300class TouchDisplayProjectionTest : public SingleTouchInputMapperTest {
6301public:
6302 // The values inside DisplayViewport are expected to be pre-rotated. This updates the current
6303 // DisplayViewport to pre-rotate the values. The viewport's physical display will be set to the
6304 // rotated equivalent of the given un-rotated physical display bounds.
Prabir Pradhana9df3162022-12-05 23:57:27 +00006305 void configurePhysicalDisplay(ui::Rotation orientation, Rect naturalPhysicalDisplay,
6306 int32_t naturalDisplayWidth = DISPLAY_WIDTH,
6307 int32_t naturalDisplayHeight = DISPLAY_HEIGHT) {
Prabir Pradhan5632d622021-09-06 07:57:20 -07006308 uint32_t inverseRotationFlags;
Prabir Pradhana9df3162022-12-05 23:57:27 +00006309 auto rotatedWidth = naturalDisplayWidth;
6310 auto rotatedHeight = naturalDisplayHeight;
Prabir Pradhan5632d622021-09-06 07:57:20 -07006311 switch (orientation) {
Michael Wrighta9cf4192022-12-01 23:46:39 +00006312 case ui::ROTATION_90:
Prabir Pradhan5632d622021-09-06 07:57:20 -07006313 inverseRotationFlags = ui::Transform::ROT_270;
Prabir Pradhana9df3162022-12-05 23:57:27 +00006314 std::swap(rotatedWidth, rotatedHeight);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006315 break;
Michael Wrighta9cf4192022-12-01 23:46:39 +00006316 case ui::ROTATION_180:
Prabir Pradhan5632d622021-09-06 07:57:20 -07006317 inverseRotationFlags = ui::Transform::ROT_180;
6318 break;
Michael Wrighta9cf4192022-12-01 23:46:39 +00006319 case ui::ROTATION_270:
Prabir Pradhan5632d622021-09-06 07:57:20 -07006320 inverseRotationFlags = ui::Transform::ROT_90;
Prabir Pradhana9df3162022-12-05 23:57:27 +00006321 std::swap(rotatedWidth, rotatedHeight);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006322 break;
Michael Wrighta9cf4192022-12-01 23:46:39 +00006323 case ui::ROTATION_0:
Prabir Pradhan5632d622021-09-06 07:57:20 -07006324 inverseRotationFlags = ui::Transform::ROT_0;
6325 break;
Prabir Pradhan5632d622021-09-06 07:57:20 -07006326 }
6327
Prabir Pradhana9df3162022-12-05 23:57:27 +00006328 const ui::Transform rotation(inverseRotationFlags, rotatedWidth, rotatedHeight);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006329 const Rect rotatedPhysicalDisplay = rotation.transform(naturalPhysicalDisplay);
6330
6331 std::optional<DisplayViewport> internalViewport =
6332 *mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6333 DisplayViewport& v = *internalViewport;
6334 v.displayId = DISPLAY_ID;
6335 v.orientation = orientation;
6336
6337 v.logicalLeft = 0;
6338 v.logicalTop = 0;
6339 v.logicalRight = 100;
6340 v.logicalBottom = 100;
6341
6342 v.physicalLeft = rotatedPhysicalDisplay.left;
6343 v.physicalTop = rotatedPhysicalDisplay.top;
6344 v.physicalRight = rotatedPhysicalDisplay.right;
6345 v.physicalBottom = rotatedPhysicalDisplay.bottom;
6346
Prabir Pradhana9df3162022-12-05 23:57:27 +00006347 v.deviceWidth = rotatedWidth;
6348 v.deviceHeight = rotatedHeight;
Prabir Pradhan5632d622021-09-06 07:57:20 -07006349
6350 v.isActive = true;
6351 v.uniqueId = UNIQUE_ID;
6352 v.type = ViewportType::INTERNAL;
6353 mFakePolicy->updateViewport(v);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00006354 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006355 }
6356
6357 void assertReceivedMove(const Point& point) {
6358 NotifyMotionArgs motionArgs;
6359 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6360 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07006361 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Prabir Pradhan5632d622021-09-06 07:57:20 -07006362 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], point.x, point.y,
6363 1, 0, 0, 0, 0, 0, 0, 0));
6364 }
6365};
6366
6367TEST_F(TouchDisplayProjectionTest, IgnoresTouchesOutsidePhysicalDisplay) {
6368 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006369 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006370
6371 prepareButtons();
6372 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00006373 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan5632d622021-09-06 07:57:20 -07006374
6375 NotifyMotionArgs motionArgs;
6376
6377 // Configure the DisplayViewport such that the logical display maps to a subsection of
6378 // the display panel called the physical display. Here, the physical display is bounded by the
6379 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
6380 static const Rect kPhysicalDisplay{10, 20, 70, 160};
6381 static const std::array<Point, 6> kPointsOutsidePhysicalDisplay{
6382 {{-10, -10}, {0, 0}, {5, 100}, {50, 15}, {75, 100}, {50, 165}}};
6383
Michael Wrighta9cf4192022-12-01 23:46:39 +00006384 for (auto orientation : {ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180, ui::ROTATION_270}) {
Prabir Pradhan5632d622021-09-06 07:57:20 -07006385 configurePhysicalDisplay(orientation, kPhysicalDisplay);
6386
6387 // Touches outside the physical display should be ignored, and should not generate any
6388 // events. Ensure touches at the following points that lie outside of the physical display
6389 // area do not generate any events.
6390 for (const auto& point : kPointsOutsidePhysicalDisplay) {
6391 processDown(mapper, toRawX(point.x), toRawY(point.y));
6392 processSync(mapper);
6393 processUp(mapper);
6394 processSync(mapper);
6395 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled())
6396 << "Unexpected event generated for touch outside physical display at point: "
6397 << point.x << ", " << point.y;
6398 }
6399 }
6400}
6401
6402TEST_F(TouchDisplayProjectionTest, EmitsTouchDownAfterEnteringPhysicalDisplay) {
6403 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006404 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006405
6406 prepareButtons();
6407 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00006408 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan5632d622021-09-06 07:57:20 -07006409
6410 NotifyMotionArgs motionArgs;
6411
6412 // Configure the DisplayViewport such that the logical display maps to a subsection of
6413 // the display panel called the physical display. Here, the physical display is bounded by the
6414 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
6415 static const Rect kPhysicalDisplay{10, 20, 70, 160};
6416
Michael Wrighta9cf4192022-12-01 23:46:39 +00006417 for (auto orientation : {ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180, ui::ROTATION_270}) {
Prabir Pradhan5632d622021-09-06 07:57:20 -07006418 configurePhysicalDisplay(orientation, kPhysicalDisplay);
6419
6420 // Touches that start outside the physical display should be ignored until it enters the
6421 // physical display bounds, at which point it should generate a down event. Start a touch at
6422 // the point (5, 100), which is outside the physical display bounds.
6423 static const Point kOutsidePoint{5, 100};
6424 processDown(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
6425 processSync(mapper);
6426 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6427
6428 // Move the touch into the physical display area. This should generate a pointer down.
6429 processMove(mapper, toRawX(11), toRawY(21));
6430 processSync(mapper);
6431 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6432 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07006433 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Prabir Pradhan5632d622021-09-06 07:57:20 -07006434 ASSERT_NO_FATAL_FAILURE(
6435 assertPointerCoords(motionArgs.pointerCoords[0], 11, 21, 1, 0, 0, 0, 0, 0, 0, 0));
6436
6437 // Move the touch inside the physical display area. This should generate a pointer move.
6438 processMove(mapper, toRawX(69), toRawY(159));
6439 processSync(mapper);
6440 assertReceivedMove({69, 159});
6441
6442 // Move outside the physical display area. Since the pointer is already down, this should
6443 // now continue generating events.
6444 processMove(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
6445 processSync(mapper);
6446 assertReceivedMove(kOutsidePoint);
6447
6448 // Release. This should generate a pointer up.
6449 processUp(mapper);
6450 processSync(mapper);
6451 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6452 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6453 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], kOutsidePoint.x,
6454 kOutsidePoint.y, 1, 0, 0, 0, 0, 0, 0, 0));
6455
6456 // Ensure no more events were generated.
6457 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6458 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6459 }
6460}
6461
Prabir Pradhana9df3162022-12-05 23:57:27 +00006462// --- TouchscreenPrecisionTests ---
6463
6464// This test suite is used to ensure that touchscreen devices are scaled and configured correctly
6465// in various orientations and with different display rotations. We configure the touchscreen to
6466// have a higher resolution than that of the display by an integer scale factor in each axis so that
6467// we can enforce that coordinates match precisely as expected.
6468class TouchscreenPrecisionTestsFixture : public TouchDisplayProjectionTest,
6469 public ::testing::WithParamInterface<ui::Rotation> {
6470public:
6471 void SetUp() override {
6472 SingleTouchInputMapperTest::SetUp();
6473
6474 // Prepare the raw axes to have twice the resolution of the display in the X axis and
6475 // four times the resolution of the display in the Y axis.
6476 prepareButtons();
6477 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, PRECISION_RAW_X_MIN, PRECISION_RAW_X_MAX,
Prabir Pradhan46211fb2022-12-17 00:30:39 +00006478 PRECISION_RAW_X_FLAT, PRECISION_RAW_X_FUZZ,
6479 PRECISION_RAW_X_RES);
Prabir Pradhana9df3162022-12-05 23:57:27 +00006480 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, PRECISION_RAW_Y_MIN, PRECISION_RAW_Y_MAX,
Prabir Pradhan46211fb2022-12-17 00:30:39 +00006481 PRECISION_RAW_Y_FLAT, PRECISION_RAW_Y_FUZZ,
6482 PRECISION_RAW_Y_RES);
Prabir Pradhana9df3162022-12-05 23:57:27 +00006483 }
6484
6485 static const int32_t PRECISION_RAW_X_MIN = TouchInputMapperTest::RAW_X_MIN;
6486 static const int32_t PRECISION_RAW_X_MAX = PRECISION_RAW_X_MIN + DISPLAY_WIDTH * 2 - 1;
6487 static const int32_t PRECISION_RAW_Y_MIN = TouchInputMapperTest::RAW_Y_MIN;
6488 static const int32_t PRECISION_RAW_Y_MAX = PRECISION_RAW_Y_MIN + DISPLAY_HEIGHT * 4 - 1;
6489
Prabir Pradhan46211fb2022-12-17 00:30:39 +00006490 static const int32_t PRECISION_RAW_X_RES = 50; // units per millimeter
6491 static const int32_t PRECISION_RAW_Y_RES = 100; // units per millimeter
6492
6493 static const int32_t PRECISION_RAW_X_FLAT = 16;
6494 static const int32_t PRECISION_RAW_Y_FLAT = 32;
6495
6496 static const int32_t PRECISION_RAW_X_FUZZ = 4;
6497 static const int32_t PRECISION_RAW_Y_FUZZ = 8;
6498
Prabir Pradhana9df3162022-12-05 23:57:27 +00006499 static const std::array<Point, 4> kRawCorners;
6500};
6501
6502const std::array<Point, 4> TouchscreenPrecisionTestsFixture::kRawCorners = {{
6503 {PRECISION_RAW_X_MIN, PRECISION_RAW_Y_MIN}, // left-top
6504 {PRECISION_RAW_X_MAX, PRECISION_RAW_Y_MIN}, // right-top
6505 {PRECISION_RAW_X_MAX, PRECISION_RAW_Y_MAX}, // right-bottom
6506 {PRECISION_RAW_X_MIN, PRECISION_RAW_Y_MAX}, // left-bottom
6507}};
6508
6509// Tests for how the touchscreen is oriented relative to the natural orientation of the display.
6510// For example, if a touchscreen is configured with an orientation of 90 degrees, it is a portrait
6511// touchscreen panel that is used on a device whose natural display orientation is in landscape.
6512TEST_P(TouchscreenPrecisionTestsFixture, OrientationPrecision) {
6513 enum class Orientation {
6514 ORIENTATION_0 = ui::toRotationInt(ui::ROTATION_0),
6515 ORIENTATION_90 = ui::toRotationInt(ui::ROTATION_90),
6516 ORIENTATION_180 = ui::toRotationInt(ui::ROTATION_180),
6517 ORIENTATION_270 = ui::toRotationInt(ui::ROTATION_270),
6518 ftl_last = ORIENTATION_270,
6519 };
6520 using Orientation::ORIENTATION_0, Orientation::ORIENTATION_90, Orientation::ORIENTATION_180,
6521 Orientation::ORIENTATION_270;
6522 static const std::map<Orientation, std::array<vec2, 4> /*mappedCorners*/> kMappedCorners = {
6523 {ORIENTATION_0, {{{0, 0}, {479.5, 0}, {479.5, 799.75}, {0, 799.75}}}},
6524 {ORIENTATION_90, {{{0, 479.5}, {0, 0}, {799.75, 0}, {799.75, 479.5}}}},
6525 {ORIENTATION_180, {{{479.5, 799.75}, {0, 799.75}, {0, 0}, {479.5, 0}}}},
6526 {ORIENTATION_270, {{{799.75, 0}, {799.75, 479.5}, {0, 479.5}, {0, 0}}}},
6527 };
6528
6529 const auto touchscreenOrientation = static_cast<Orientation>(ui::toRotationInt(GetParam()));
6530
6531 // Configure the touchscreen as being installed in the one of the four different orientations
6532 // relative to the display.
6533 addConfigurationProperty("touch.deviceType", "touchScreen");
6534 addConfigurationProperty("touch.orientation", ftl::enum_string(touchscreenOrientation).c_str());
6535 prepareDisplay(ui::ROTATION_0);
6536
Arpit Singha8c236b2023-04-25 13:56:05 +00006537 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhana9df3162022-12-05 23:57:27 +00006538
6539 // If the touchscreen is installed in a rotated orientation relative to the display (i.e. in
6540 // orientations of either 90 or 270) this means the display's natural resolution will be
6541 // flipped.
6542 const bool displayRotated =
6543 touchscreenOrientation == ORIENTATION_90 || touchscreenOrientation == ORIENTATION_270;
6544 const int32_t width = displayRotated ? DISPLAY_HEIGHT : DISPLAY_WIDTH;
6545 const int32_t height = displayRotated ? DISPLAY_WIDTH : DISPLAY_HEIGHT;
6546 const Rect physicalFrame{0, 0, width, height};
6547 configurePhysicalDisplay(ui::ROTATION_0, physicalFrame, width, height);
6548
6549 const auto& expectedPoints = kMappedCorners.at(touchscreenOrientation);
6550 const float expectedPrecisionX = displayRotated ? 4 : 2;
6551 const float expectedPrecisionY = displayRotated ? 2 : 4;
6552
6553 // Test all four corners.
6554 for (int i = 0; i < 4; i++) {
6555 const auto& raw = kRawCorners[i];
6556 processDown(mapper, raw.x, raw.y);
6557 processSync(mapper);
6558 const auto& expected = expectedPoints[i];
6559 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6560 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
6561 WithCoords(expected.x, expected.y),
6562 WithPrecision(expectedPrecisionX, expectedPrecisionY))))
6563 << "Failed to process raw point (" << raw.x << ", " << raw.y << ") "
6564 << "with touchscreen orientation "
6565 << ftl::enum_string(touchscreenOrientation).c_str() << ", expected point ("
6566 << expected.x << ", " << expected.y << ").";
6567 processUp(mapper);
6568 processSync(mapper);
6569 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6570 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
6571 WithCoords(expected.x, expected.y))));
6572 }
6573}
6574
Prabir Pradhan82687402022-12-06 01:32:53 +00006575TEST_P(TouchscreenPrecisionTestsFixture, RotationPrecisionWhenOrientationAware) {
6576 static const std::map<ui::Rotation /*rotation*/, std::array<vec2, 4> /*mappedCorners*/>
6577 kMappedCorners = {
6578 {ui::ROTATION_0, {{{0, 0}, {479.5, 0}, {479.5, 799.75}, {0, 799.75}}}},
6579 {ui::ROTATION_90, {{{0.5, 0}, {480, 0}, {480, 799.75}, {0.5, 799.75}}}},
6580 {ui::ROTATION_180, {{{0.5, 0.25}, {480, 0.25}, {480, 800}, {0.5, 800}}}},
6581 {ui::ROTATION_270, {{{0, 0.25}, {479.5, 0.25}, {479.5, 800}, {0, 800}}}},
6582 };
6583
6584 const ui::Rotation displayRotation = GetParam();
6585
6586 addConfigurationProperty("touch.deviceType", "touchScreen");
6587 prepareDisplay(displayRotation);
6588
Arpit Singha8c236b2023-04-25 13:56:05 +00006589 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan82687402022-12-06 01:32:53 +00006590
6591 const auto& expectedPoints = kMappedCorners.at(displayRotation);
6592
6593 // Test all four corners.
6594 for (int i = 0; i < 4; i++) {
6595 const auto& expected = expectedPoints[i];
6596 const auto& raw = kRawCorners[i];
6597 processDown(mapper, raw.x, raw.y);
6598 processSync(mapper);
6599 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6600 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
6601 WithCoords(expected.x, expected.y), WithPrecision(2, 4))))
6602 << "Failed to process raw point (" << raw.x << ", " << raw.y << ") "
6603 << "with display rotation " << ui::toCString(displayRotation)
6604 << ", expected point (" << expected.x << ", " << expected.y << ").";
6605 processUp(mapper);
6606 processSync(mapper);
6607 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6608 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
6609 WithCoords(expected.x, expected.y))));
6610 }
6611}
6612
Prabir Pradhan3e798762022-12-02 21:02:11 +00006613TEST_P(TouchscreenPrecisionTestsFixture, RotationPrecisionOrientationAwareInOri270) {
6614 static const std::map<ui::Rotation /*orientation*/, std::array<vec2, 4> /*mappedCorners*/>
6615 kMappedCorners = {
6616 {ui::ROTATION_0, {{{799.75, 0}, {799.75, 479.5}, {0, 479.5}, {0, 0}}}},
6617 {ui::ROTATION_90, {{{800, 0}, {800, 479.5}, {0.25, 479.5}, {0.25, 0}}}},
6618 {ui::ROTATION_180, {{{800, 0.5}, {800, 480}, {0.25, 480}, {0.25, 0.5}}}},
6619 {ui::ROTATION_270, {{{799.75, 0.5}, {799.75, 480}, {0, 480}, {0, 0.5}}}},
6620 };
6621
6622 const ui::Rotation displayRotation = GetParam();
6623
6624 addConfigurationProperty("touch.deviceType", "touchScreen");
6625 addConfigurationProperty("touch.orientation", "ORIENTATION_270");
6626
Arpit Singha8c236b2023-04-25 13:56:05 +00006627 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan3e798762022-12-02 21:02:11 +00006628
6629 // Ori 270, so width and height swapped
6630 const Rect physicalFrame{0, 0, DISPLAY_HEIGHT, DISPLAY_WIDTH};
6631 prepareDisplay(displayRotation);
6632 configurePhysicalDisplay(displayRotation, physicalFrame, DISPLAY_HEIGHT, DISPLAY_WIDTH);
6633
6634 const auto& expectedPoints = kMappedCorners.at(displayRotation);
6635
6636 // Test all four corners.
6637 for (int i = 0; i < 4; i++) {
6638 const auto& expected = expectedPoints[i];
6639 const auto& raw = kRawCorners[i];
6640 processDown(mapper, raw.x, raw.y);
6641 processSync(mapper);
6642 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6643 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
6644 WithCoords(expected.x, expected.y), WithPrecision(4, 2))))
6645 << "Failed to process raw point (" << raw.x << ", " << raw.y << ") "
6646 << "with display rotation " << ui::toCString(displayRotation)
6647 << ", expected point (" << expected.x << ", " << expected.y << ").";
6648 processUp(mapper);
6649 processSync(mapper);
6650 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6651 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
6652 WithCoords(expected.x, expected.y))));
6653 }
6654}
6655
Prabir Pradhan46211fb2022-12-17 00:30:39 +00006656TEST_P(TouchscreenPrecisionTestsFixture, MotionRangesAreOrientedInRotatedDisplay) {
6657 const ui::Rotation displayRotation = GetParam();
6658
6659 addConfigurationProperty("touch.deviceType", "touchScreen");
6660 prepareDisplay(displayRotation);
6661
6662 __attribute__((unused)) SingleTouchInputMapper& mapper =
Arpit Singha8c236b2023-04-25 13:56:05 +00006663 constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan46211fb2022-12-17 00:30:39 +00006664
6665 const InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
6666 // MotionRanges use display pixels as their units
6667 const auto* xRange = deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_X, AINPUT_SOURCE_TOUCHSCREEN);
6668 const auto* yRange = deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_Y, AINPUT_SOURCE_TOUCHSCREEN);
6669
6670 // The MotionRanges should be oriented in the rotated display's coordinate space
6671 const bool displayRotated =
6672 displayRotation == ui::ROTATION_90 || displayRotation == ui::ROTATION_270;
6673
6674 constexpr float MAX_X = 479.5;
6675 constexpr float MAX_Y = 799.75;
6676 EXPECT_EQ(xRange->min, 0.f);
6677 EXPECT_EQ(yRange->min, 0.f);
6678 EXPECT_EQ(xRange->max, displayRotated ? MAX_Y : MAX_X);
6679 EXPECT_EQ(yRange->max, displayRotated ? MAX_X : MAX_Y);
6680
6681 EXPECT_EQ(xRange->flat, 8.f);
6682 EXPECT_EQ(yRange->flat, 8.f);
6683
6684 EXPECT_EQ(xRange->fuzz, 2.f);
6685 EXPECT_EQ(yRange->fuzz, 2.f);
6686
6687 EXPECT_EQ(xRange->resolution, 25.f); // pixels per millimeter
6688 EXPECT_EQ(yRange->resolution, 25.f); // pixels per millimeter
6689}
6690
Prabir Pradhana9df3162022-12-05 23:57:27 +00006691// Run the precision tests for all rotations.
6692INSTANTIATE_TEST_SUITE_P(TouchscreenPrecisionTests, TouchscreenPrecisionTestsFixture,
6693 ::testing::Values(ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180,
6694 ui::ROTATION_270),
6695 [](const testing::TestParamInfo<ui::Rotation>& testParamInfo) {
6696 return ftl::enum_string(testParamInfo.param);
6697 });
6698
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006699// --- ExternalStylusFusionTest ---
6700
6701class ExternalStylusFusionTest : public SingleTouchInputMapperTest {
6702public:
6703 SingleTouchInputMapper& initializeInputMapperWithExternalStylus() {
6704 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006705 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006706 prepareButtons();
6707 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00006708 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006709
6710 mStylusState.when = ARBITRARY_TIME;
6711 mStylusState.pressure = 0.f;
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006712 mStylusState.toolType = ToolType::STYLUS;
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006713 mReader->getContext()->setExternalStylusDevices({mExternalStylusDeviceInfo});
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00006714 configureDevice(InputReaderConfiguration::Change::EXTERNAL_STYLUS_PRESENCE);
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006715 processExternalStylusState(mapper);
6716 return mapper;
6717 }
6718
6719 std::list<NotifyArgs> processExternalStylusState(InputMapper& mapper) {
6720 std::list<NotifyArgs> generatedArgs = mapper.updateExternalStylusState(mStylusState);
6721 for (const NotifyArgs& args : generatedArgs) {
6722 mFakeListener->notify(args);
6723 }
6724 // Loop the reader to flush the input listener queue.
6725 mReader->loopOnce();
6726 return generatedArgs;
6727 }
6728
6729protected:
6730 StylusState mStylusState{};
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006731
6732 void testStartFusedStylusGesture(SingleTouchInputMapper& mapper) {
6733 auto toolTypeSource =
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00006734 AllOf(WithSource(STYLUS_FUSION_SOURCE), WithToolType(ToolType::STYLUS));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006735
6736 // The first pointer is withheld.
6737 processDown(mapper, 100, 200);
6738 processSync(mapper);
6739 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6740 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasRequested(
6741 ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT));
6742
6743 // The external stylus reports pressure. The withheld finger pointer is released as a
6744 // stylus.
6745 mStylusState.pressure = 1.f;
6746 processExternalStylusState(mapper);
6747 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6748 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN))));
6749 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6750
6751 // Subsequent pointer events are not withheld.
6752 processMove(mapper, 101, 201);
6753 processSync(mapper);
6754 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6755 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE))));
6756
6757 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6758 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6759 }
6760
6761 void testSuccessfulFusionGesture(SingleTouchInputMapper& mapper) {
6762 ASSERT_NO_FATAL_FAILURE(testStartFusedStylusGesture(mapper));
6763
6764 // Releasing the touch pointer ends the gesture.
6765 processUp(mapper);
6766 processSync(mapper);
6767 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00006768 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithSource(STYLUS_FUSION_SOURCE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006769 WithToolType(ToolType::STYLUS))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006770
6771 mStylusState.pressure = 0.f;
6772 processExternalStylusState(mapper);
6773 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6774 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6775 }
6776
6777 void testUnsuccessfulFusionGesture(SingleTouchInputMapper& mapper) {
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00006778 // When stylus fusion is not successful, events should be reported with the original source.
6779 // In this case, it is from a touchscreen.
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006780 auto toolTypeSource =
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00006781 AllOf(WithSource(AINPUT_SOURCE_TOUCHSCREEN), WithToolType(ToolType::FINGER));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006782
6783 // The first pointer is withheld when an external stylus is connected,
6784 // and a timeout is requested.
6785 processDown(mapper, 100, 200);
6786 processSync(mapper);
6787 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6788 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasRequested(
6789 ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT));
6790
6791 // If the timeout expires early, it is requested again.
6792 handleTimeout(mapper, ARBITRARY_TIME + 1);
6793 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasRequested(
6794 ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT));
6795
6796 // When the timeout expires, the withheld touch is released as a finger pointer.
6797 handleTimeout(mapper, ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT);
6798 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6799 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN))));
6800
6801 // Subsequent pointer events are not withheld.
6802 processMove(mapper, 101, 201);
6803 processSync(mapper);
6804 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6805 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE))));
6806 processUp(mapper);
6807 processSync(mapper);
6808 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6809 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_UP))));
6810
6811 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6812 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6813 }
6814
6815private:
6816 InputDeviceInfo mExternalStylusDeviceInfo{};
6817};
6818
6819TEST_F(ExternalStylusFusionTest, UsesBluetoothStylusSource) {
6820 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00006821 ASSERT_EQ(STYLUS_FUSION_SOURCE, mapper.getSources());
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006822}
6823
6824TEST_F(ExternalStylusFusionTest, UnsuccessfulFusion) {
6825 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
6826 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
6827}
6828
6829TEST_F(ExternalStylusFusionTest, SuccessfulFusion_TouchFirst) {
6830 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
6831 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
6832}
6833
6834// Test a successful stylus fusion gesture where the pressure is reported by the external
6835// before the touch is reported by the touchscreen.
6836TEST_F(ExternalStylusFusionTest, SuccessfulFusion_PressureFirst) {
6837 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00006838 auto toolTypeSource = AllOf(WithSource(STYLUS_FUSION_SOURCE), WithToolType(ToolType::STYLUS));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006839
6840 // The external stylus reports pressure first. It is ignored for now.
6841 mStylusState.pressure = 1.f;
6842 processExternalStylusState(mapper);
6843 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6844 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6845
6846 // When the touch goes down afterwards, it is reported as a stylus pointer.
6847 processDown(mapper, 100, 200);
6848 processSync(mapper);
6849 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6850 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN))));
6851 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6852
6853 processMove(mapper, 101, 201);
6854 processSync(mapper);
6855 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6856 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE))));
6857 processUp(mapper);
6858 processSync(mapper);
6859 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6860 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_UP))));
6861
6862 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6863 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6864}
6865
6866TEST_F(ExternalStylusFusionTest, FusionIsRepeatedForEachNewGesture) {
6867 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
6868
6869 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
6870 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
6871
6872 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
6873 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
6874 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
6875 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
6876}
6877
6878TEST_F(ExternalStylusFusionTest, FusedPointerReportsPressureChanges) {
6879 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00006880 auto toolTypeSource = AllOf(WithSource(STYLUS_FUSION_SOURCE), WithToolType(ToolType::STYLUS));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006881
6882 mStylusState.pressure = 0.8f;
6883 processExternalStylusState(mapper);
6884 processDown(mapper, 100, 200);
6885 processSync(mapper);
6886 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6887 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
6888 WithPressure(0.8f))));
6889 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6890
6891 // The external stylus reports a pressure change. We wait for some time for a touch event.
6892 mStylusState.pressure = 0.6f;
6893 processExternalStylusState(mapper);
6894 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6895 ASSERT_NO_FATAL_FAILURE(
6896 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
6897
6898 // If a touch is reported within the timeout, it reports the updated pressure.
6899 processMove(mapper, 101, 201);
6900 processSync(mapper);
6901 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6902 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
6903 WithPressure(0.6f))));
6904 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6905
6906 // There is another pressure change.
6907 mStylusState.pressure = 0.5f;
6908 processExternalStylusState(mapper);
6909 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6910 ASSERT_NO_FATAL_FAILURE(
6911 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
6912
6913 // If a touch is not reported within the timeout, a move event is generated to report
6914 // the new pressure.
6915 handleTimeout(mapper, ARBITRARY_TIME + TOUCH_DATA_TIMEOUT);
6916 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6917 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
6918 WithPressure(0.5f))));
6919
6920 // If a zero pressure is reported before the touch goes up, the previous pressure value is
6921 // repeated indefinitely.
6922 mStylusState.pressure = 0.0f;
6923 processExternalStylusState(mapper);
6924 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6925 ASSERT_NO_FATAL_FAILURE(
6926 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
6927 processMove(mapper, 102, 202);
6928 processSync(mapper);
6929 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6930 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
6931 WithPressure(0.5f))));
6932 processMove(mapper, 103, 203);
6933 processSync(mapper);
6934 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6935 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
6936 WithPressure(0.5f))));
6937
6938 processUp(mapper);
6939 processSync(mapper);
6940 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00006941 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithSource(STYLUS_FUSION_SOURCE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006942 WithToolType(ToolType::STYLUS))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006943
6944 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6945 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6946}
6947
6948TEST_F(ExternalStylusFusionTest, FusedPointerReportsToolTypeChanges) {
6949 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00006950 auto source = WithSource(STYLUS_FUSION_SOURCE);
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006951
6952 mStylusState.pressure = 1.f;
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006953 mStylusState.toolType = ToolType::ERASER;
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006954 processExternalStylusState(mapper);
6955 processDown(mapper, 100, 200);
6956 processSync(mapper);
6957 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6958 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006959 WithToolType(ToolType::ERASER))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006960 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6961
6962 // The external stylus reports a tool change. We wait for some time for a touch event.
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006963 mStylusState.toolType = ToolType::STYLUS;
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006964 processExternalStylusState(mapper);
6965 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6966 ASSERT_NO_FATAL_FAILURE(
6967 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
6968
6969 // If a touch is reported within the timeout, it reports the updated pressure.
6970 processMove(mapper, 101, 201);
6971 processSync(mapper);
6972 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6973 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006974 WithToolType(ToolType::STYLUS))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006975 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6976
6977 // There is another tool type change.
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006978 mStylusState.toolType = ToolType::FINGER;
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006979 processExternalStylusState(mapper);
6980 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6981 ASSERT_NO_FATAL_FAILURE(
6982 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
6983
6984 // If a touch is not reported within the timeout, a move event is generated to report
6985 // the new tool type.
6986 handleTimeout(mapper, ARBITRARY_TIME + TOUCH_DATA_TIMEOUT);
6987 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6988 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006989 WithToolType(ToolType::FINGER))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006990
6991 processUp(mapper);
6992 processSync(mapper);
6993 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6994 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_UP),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006995 WithToolType(ToolType::FINGER))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006996
6997 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6998 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6999}
7000
7001TEST_F(ExternalStylusFusionTest, FusedPointerReportsButtons) {
7002 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00007003 auto toolTypeSource = AllOf(WithSource(STYLUS_FUSION_SOURCE), WithToolType(ToolType::STYLUS));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007004
7005 ASSERT_NO_FATAL_FAILURE(testStartFusedStylusGesture(mapper));
7006
7007 // The external stylus reports a button change. We wait for some time for a touch event.
7008 mStylusState.buttons = AMOTION_EVENT_BUTTON_STYLUS_PRIMARY;
7009 processExternalStylusState(mapper);
7010 ASSERT_NO_FATAL_FAILURE(
7011 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7012
7013 // If a touch is reported within the timeout, it reports the updated button state.
7014 processMove(mapper, 101, 201);
7015 processSync(mapper);
7016 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7017 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7018 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
7019 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7020 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
7021 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
7022 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7023
7024 // The button is now released.
7025 mStylusState.buttons = 0;
7026 processExternalStylusState(mapper);
7027 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7028 ASSERT_NO_FATAL_FAILURE(
7029 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7030
7031 // If a touch is not reported within the timeout, a move event is generated to report
7032 // the new button state.
7033 handleTimeout(mapper, ARBITRARY_TIME + TOUCH_DATA_TIMEOUT);
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007034 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7035 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
7036 WithButtonState(0))));
7037 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan124ea442022-10-28 20:27:44 +00007038 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7039 WithButtonState(0))));
7040
7041 processUp(mapper);
7042 processSync(mapper);
7043 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007044 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_UP), WithButtonState(0))));
7045
7046 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7047 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7048}
7049
Michael Wrightd02c5b62014-02-10 15:10:22 -08007050// --- MultiTouchInputMapperTest ---
7051
7052class MultiTouchInputMapperTest : public TouchInputMapperTest {
7053protected:
7054 void prepareAxes(int axes);
7055
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007056 void processPosition(MultiTouchInputMapper& mapper, int32_t x, int32_t y);
7057 void processTouchMajor(MultiTouchInputMapper& mapper, int32_t touchMajor);
7058 void processTouchMinor(MultiTouchInputMapper& mapper, int32_t touchMinor);
7059 void processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor);
7060 void processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor);
7061 void processOrientation(MultiTouchInputMapper& mapper, int32_t orientation);
7062 void processPressure(MultiTouchInputMapper& mapper, int32_t pressure);
7063 void processDistance(MultiTouchInputMapper& mapper, int32_t distance);
7064 void processId(MultiTouchInputMapper& mapper, int32_t id);
7065 void processSlot(MultiTouchInputMapper& mapper, int32_t slot);
7066 void processToolType(MultiTouchInputMapper& mapper, int32_t toolType);
7067 void processKey(MultiTouchInputMapper& mapper, int32_t code, int32_t value);
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00007068 void processHidUsage(MultiTouchInputMapper& mapper, int32_t usageCode, int32_t value);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007069 void processMTSync(MultiTouchInputMapper& mapper);
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00007070 void processSync(MultiTouchInputMapper& mapper, nsecs_t eventTime = ARBITRARY_TIME,
7071 nsecs_t readTime = READ_TIME);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007072};
7073
7074void MultiTouchInputMapperTest::prepareAxes(int axes) {
7075 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007076 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
7077 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007078 }
7079 if (axes & TOUCH) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007080 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN,
7081 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007082 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007083 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN,
7084 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007085 }
7086 }
7087 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007088 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7089 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007090 if (axes & MINOR) {
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007091 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007092 RAW_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007093 }
7094 }
7095 if (axes & ORIENTATION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007096 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_ORIENTATION, RAW_ORIENTATION_MIN,
7097 RAW_ORIENTATION_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007098 }
7099 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007100 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_PRESSURE, RAW_PRESSURE_MIN,
7101 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007102 }
7103 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007104 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_DISTANCE, RAW_DISTANCE_MIN,
7105 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007106 }
7107 if (axes & ID) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007108 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX, 0,
7109 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007110 }
7111 if (axes & SLOT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007112 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
7113 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_MT_SLOT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007114 }
7115 if (axes & TOOL_TYPE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007116 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOOL_TYPE, 0, MT_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007117 }
7118}
7119
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007120void MultiTouchInputMapperTest::processPosition(MultiTouchInputMapper& mapper, int32_t x,
7121 int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007122 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_X, x);
7123 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007124}
7125
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007126void MultiTouchInputMapperTest::processTouchMajor(MultiTouchInputMapper& mapper,
7127 int32_t touchMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007128 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007129}
7130
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007131void MultiTouchInputMapperTest::processTouchMinor(MultiTouchInputMapper& mapper,
7132 int32_t touchMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007133 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007134}
7135
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007136void MultiTouchInputMapperTest::processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007137 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007138}
7139
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007140void MultiTouchInputMapperTest::processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007141 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007142}
7143
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007144void MultiTouchInputMapperTest::processOrientation(MultiTouchInputMapper& mapper,
7145 int32_t orientation) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007146 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_ORIENTATION, orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007147}
7148
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007149void MultiTouchInputMapperTest::processPressure(MultiTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007150 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007151}
7152
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007153void MultiTouchInputMapperTest::processDistance(MultiTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007154 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007155}
7156
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007157void MultiTouchInputMapperTest::processId(MultiTouchInputMapper& mapper, int32_t id) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007158 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TRACKING_ID, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007159}
7160
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007161void MultiTouchInputMapperTest::processSlot(MultiTouchInputMapper& mapper, int32_t slot) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007162 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_SLOT, slot);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007163}
7164
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007165void MultiTouchInputMapperTest::processToolType(MultiTouchInputMapper& mapper, int32_t toolType) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007166 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007167}
7168
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007169void MultiTouchInputMapperTest::processKey(MultiTouchInputMapper& mapper, int32_t code,
7170 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007171 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007172}
7173
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00007174void MultiTouchInputMapperTest::processHidUsage(MultiTouchInputMapper& mapper, int32_t usageCode,
7175 int32_t value) {
7176 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, usageCode);
7177 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UNKNOWN, value);
7178}
7179
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007180void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007181 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_MT_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007182}
7183
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00007184void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper& mapper, nsecs_t eventTime,
7185 nsecs_t readTime) {
7186 process(mapper, eventTime, readTime, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007187}
7188
Michael Wrightd02c5b62014-02-10 15:10:22 -08007189TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007190 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007191 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007192 prepareAxes(POSITION);
7193 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00007194 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007195
arthurhungdcef2dc2020-08-11 14:47:50 +08007196 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007197
7198 NotifyMotionArgs motionArgs;
7199
7200 // Two fingers down at once.
7201 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7202 processPosition(mapper, x1, y1);
7203 processMTSync(mapper);
7204 processPosition(mapper, x2, y2);
7205 processMTSync(mapper);
7206 processSync(mapper);
7207
7208 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7209 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7210 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7211 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7212 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7213 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7214 ASSERT_EQ(0, motionArgs.flags);
7215 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7216 ASSERT_EQ(0, motionArgs.buttonState);
7217 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007218 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007219 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007220 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007221 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7222 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7223 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7224 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7225 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7226
7227 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7228 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7229 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7230 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7231 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007232 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007233 ASSERT_EQ(0, motionArgs.flags);
7234 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7235 ASSERT_EQ(0, motionArgs.buttonState);
7236 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007237 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007238 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007239 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007240 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007241 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007242 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7243 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7244 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7245 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7246 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7247 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7248 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7249
7250 // Move.
7251 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7252 processPosition(mapper, x1, y1);
7253 processMTSync(mapper);
7254 processPosition(mapper, x2, y2);
7255 processMTSync(mapper);
7256 processSync(mapper);
7257
7258 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7259 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7260 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7261 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7262 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7263 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7264 ASSERT_EQ(0, motionArgs.flags);
7265 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7266 ASSERT_EQ(0, motionArgs.buttonState);
7267 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007268 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007269 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007270 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007271 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007272 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007273 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7274 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7275 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7276 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7277 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7278 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7279 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7280
7281 // First finger up.
7282 x2 += 15; y2 -= 20;
7283 processPosition(mapper, x2, y2);
7284 processMTSync(mapper);
7285 processSync(mapper);
7286
7287 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7288 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7289 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7290 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7291 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007292 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007293 ASSERT_EQ(0, motionArgs.flags);
7294 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7295 ASSERT_EQ(0, motionArgs.buttonState);
7296 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007297 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007298 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007299 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007300 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007301 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007302 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7303 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7304 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7305 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7306 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7307 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7308 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7309
7310 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7311 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7312 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7313 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7314 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7315 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7316 ASSERT_EQ(0, motionArgs.flags);
7317 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7318 ASSERT_EQ(0, motionArgs.buttonState);
7319 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007320 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007321 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007322 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007323 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7324 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7325 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7326 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7327 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7328
7329 // Move.
7330 x2 += 20; y2 -= 25;
7331 processPosition(mapper, x2, y2);
7332 processMTSync(mapper);
7333 processSync(mapper);
7334
7335 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7336 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7337 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7338 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7339 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7340 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7341 ASSERT_EQ(0, motionArgs.flags);
7342 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7343 ASSERT_EQ(0, motionArgs.buttonState);
7344 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007345 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007346 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007347 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007348 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7349 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7350 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7351 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7352 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7353
7354 // New finger down.
7355 int32_t x3 = 700, y3 = 300;
7356 processPosition(mapper, x2, y2);
7357 processMTSync(mapper);
7358 processPosition(mapper, x3, y3);
7359 processMTSync(mapper);
7360 processSync(mapper);
7361
7362 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7363 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7364 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7365 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7366 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007367 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007368 ASSERT_EQ(0, motionArgs.flags);
7369 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7370 ASSERT_EQ(0, motionArgs.buttonState);
7371 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007372 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007373 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007374 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007375 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007376 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007377 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7378 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7379 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7380 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7381 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7382 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7383 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7384
7385 // Second finger up.
7386 x3 += 30; y3 -= 20;
7387 processPosition(mapper, x3, y3);
7388 processMTSync(mapper);
7389 processSync(mapper);
7390
7391 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7392 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7393 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7394 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7395 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007396 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007397 ASSERT_EQ(0, motionArgs.flags);
7398 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7399 ASSERT_EQ(0, motionArgs.buttonState);
7400 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007401 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007402 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007403 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007404 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007405 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007406 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7407 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7408 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7409 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7410 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7411 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7412 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7413
7414 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7415 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7416 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7417 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7418 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7419 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7420 ASSERT_EQ(0, motionArgs.flags);
7421 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7422 ASSERT_EQ(0, motionArgs.buttonState);
7423 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007424 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007425 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007426 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007427 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7428 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7429 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7430 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7431 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7432
7433 // Last finger up.
7434 processMTSync(mapper);
7435 processSync(mapper);
7436
7437 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7438 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7439 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7440 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7441 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7442 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7443 ASSERT_EQ(0, motionArgs.flags);
7444 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7445 ASSERT_EQ(0, motionArgs.buttonState);
7446 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007447 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007448 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007449 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007450 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7451 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7452 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7453 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7454 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7455
7456 // Should not have sent any more keys or motions.
7457 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7458 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7459}
7460
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007461TEST_F(MultiTouchInputMapperTest, AxisResolution_IsPopulated) {
7462 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007463 prepareDisplay(ui::ROTATION_0);
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007464
7465 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
7466 /*fuzz*/ 0, /*resolution*/ 10);
7467 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
7468 /*fuzz*/ 0, /*resolution*/ 11);
7469 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
7470 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 12);
7471 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
7472 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 13);
7473 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7474 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 14);
7475 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7476 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 15);
7477
Arpit Singha8c236b2023-04-25 13:56:05 +00007478 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007479
7480 // X and Y axes
7481 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_X, 10 / X_PRECISION);
7482 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_Y, 11 / Y_PRECISION);
7483 // Touch major and minor
7484 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR, 12 * GEOMETRIC_SCALE);
7485 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR, 13 * GEOMETRIC_SCALE);
7486 // Tool major and minor
7487 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR, 14 * GEOMETRIC_SCALE);
7488 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR, 15 * GEOMETRIC_SCALE);
7489}
7490
7491TEST_F(MultiTouchInputMapperTest, TouchMajorAndMinorAxes_DoNotAppearIfNotSupported) {
7492 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007493 prepareDisplay(ui::ROTATION_0);
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007494
7495 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
7496 /*fuzz*/ 0, /*resolution*/ 10);
7497 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
7498 /*fuzz*/ 0, /*resolution*/ 11);
7499
7500 // We do not add ABS_MT_TOUCH_MAJOR / MINOR or ABS_MT_WIDTH_MAJOR / MINOR axes
7501
Arpit Singha8c236b2023-04-25 13:56:05 +00007502 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007503
7504 // Touch major and minor
7505 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR);
7506 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR);
7507 // Tool major and minor
7508 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR);
7509 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR);
7510}
7511
Michael Wrightd02c5b62014-02-10 15:10:22 -08007512TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007513 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007514 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007515 prepareAxes(POSITION | ID);
7516 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00007517 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007518
arthurhungdcef2dc2020-08-11 14:47:50 +08007519 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007520
7521 NotifyMotionArgs motionArgs;
7522
7523 // Two fingers down at once.
7524 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7525 processPosition(mapper, x1, y1);
7526 processId(mapper, 1);
7527 processMTSync(mapper);
7528 processPosition(mapper, x2, y2);
7529 processId(mapper, 2);
7530 processMTSync(mapper);
7531 processSync(mapper);
7532
7533 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7534 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007535 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007536 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007537 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007538 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7539 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7540
7541 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007542 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007543 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007544 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007545 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007546 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007547 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007548 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7549 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7550 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7551 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7552
7553 // Move.
7554 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7555 processPosition(mapper, x1, y1);
7556 processId(mapper, 1);
7557 processMTSync(mapper);
7558 processPosition(mapper, x2, y2);
7559 processId(mapper, 2);
7560 processMTSync(mapper);
7561 processSync(mapper);
7562
7563 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7564 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007565 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007566 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007567 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007568 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007569 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007570 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7571 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7572 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7573 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7574
7575 // First finger up.
7576 x2 += 15; y2 -= 20;
7577 processPosition(mapper, x2, y2);
7578 processId(mapper, 2);
7579 processMTSync(mapper);
7580 processSync(mapper);
7581
7582 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007583 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007584 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007585 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007586 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007587 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007588 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007589 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7590 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7591 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7592 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7593
7594 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7595 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007596 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007597 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007598 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007599 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7600 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7601
7602 // Move.
7603 x2 += 20; y2 -= 25;
7604 processPosition(mapper, x2, y2);
7605 processId(mapper, 2);
7606 processMTSync(mapper);
7607 processSync(mapper);
7608
7609 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7610 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007611 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007612 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007613 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007614 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7615 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7616
7617 // New finger down.
7618 int32_t x3 = 700, y3 = 300;
7619 processPosition(mapper, x2, y2);
7620 processId(mapper, 2);
7621 processMTSync(mapper);
7622 processPosition(mapper, x3, y3);
7623 processId(mapper, 3);
7624 processMTSync(mapper);
7625 processSync(mapper);
7626
7627 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007628 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007629 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007630 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007631 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007632 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007633 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007634 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7635 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7636 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7637 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7638
7639 // Second finger up.
7640 x3 += 30; y3 -= 20;
7641 processPosition(mapper, x3, y3);
7642 processId(mapper, 3);
7643 processMTSync(mapper);
7644 processSync(mapper);
7645
7646 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007647 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007648 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007649 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007650 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007651 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007652 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007653 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7654 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7655 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7656 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7657
7658 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7659 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007660 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007661 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007662 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007663 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7664 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7665
7666 // Last finger up.
7667 processMTSync(mapper);
7668 processSync(mapper);
7669
7670 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7671 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007672 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007673 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007674 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007675 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7676 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7677
7678 // Should not have sent any more keys or motions.
7679 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7680 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7681}
7682
7683TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007684 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007685 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007686 prepareAxes(POSITION | ID | SLOT);
7687 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00007688 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007689
arthurhungdcef2dc2020-08-11 14:47:50 +08007690 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007691
7692 NotifyMotionArgs motionArgs;
7693
7694 // Two fingers down at once.
7695 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7696 processPosition(mapper, x1, y1);
7697 processId(mapper, 1);
7698 processSlot(mapper, 1);
7699 processPosition(mapper, x2, y2);
7700 processId(mapper, 2);
7701 processSync(mapper);
7702
7703 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7704 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007705 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007706 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007707 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007708 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7709 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7710
7711 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007712 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007713 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007714 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007715 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007716 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007717 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007718 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7719 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7720 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7721 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7722
7723 // Move.
7724 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7725 processSlot(mapper, 0);
7726 processPosition(mapper, x1, y1);
7727 processSlot(mapper, 1);
7728 processPosition(mapper, x2, y2);
7729 processSync(mapper);
7730
7731 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7732 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007733 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007734 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007735 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007736 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007737 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007738 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7739 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7740 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7741 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7742
7743 // First finger up.
7744 x2 += 15; y2 -= 20;
7745 processSlot(mapper, 0);
7746 processId(mapper, -1);
7747 processSlot(mapper, 1);
7748 processPosition(mapper, x2, y2);
7749 processSync(mapper);
7750
7751 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007752 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007753 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007754 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007755 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007756 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007757 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007758 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7759 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7760 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7761 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7762
7763 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7764 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007765 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007766 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007767 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007768 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7769 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7770
7771 // Move.
7772 x2 += 20; y2 -= 25;
7773 processPosition(mapper, x2, y2);
7774 processSync(mapper);
7775
7776 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7777 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, 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(1, 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(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7783
7784 // New finger down.
7785 int32_t x3 = 700, y3 = 300;
7786 processPosition(mapper, x2, y2);
7787 processSlot(mapper, 0);
7788 processId(mapper, 3);
7789 processPosition(mapper, x3, y3);
7790 processSync(mapper);
7791
7792 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007793 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007794 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007795 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007796 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007797 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007798 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007799 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7800 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7801 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7802 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7803
7804 // Second finger up.
7805 x3 += 30; y3 -= 20;
7806 processSlot(mapper, 1);
7807 processId(mapper, -1);
7808 processSlot(mapper, 0);
7809 processPosition(mapper, x3, y3);
7810 processSync(mapper);
7811
7812 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007813 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007814 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007815 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007816 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007817 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007818 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007819 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7820 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7821 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7822 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7823
7824 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7825 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007826 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007827 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007828 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007829 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7830 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7831
7832 // Last finger up.
7833 processId(mapper, -1);
7834 processSync(mapper);
7835
7836 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7837 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007838 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007839 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007840 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007841 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7842 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7843
7844 // Should not have sent any more keys or motions.
7845 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7846 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7847}
7848
7849TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007850 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007851 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007852 prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
Arpit Singha8c236b2023-04-25 13:56:05 +00007853 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007854
7855 // These calculations are based on the input device calibration documentation.
7856 int32_t rawX = 100;
7857 int32_t rawY = 200;
7858 int32_t rawTouchMajor = 7;
7859 int32_t rawTouchMinor = 6;
7860 int32_t rawToolMajor = 9;
7861 int32_t rawToolMinor = 8;
7862 int32_t rawPressure = 11;
7863 int32_t rawDistance = 0;
7864 int32_t rawOrientation = 3;
7865 int32_t id = 5;
7866
7867 float x = toDisplayX(rawX);
7868 float y = toDisplayY(rawY);
7869 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
7870 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
7871 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
7872 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
7873 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
7874 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
7875 float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
7876 float distance = float(rawDistance);
7877
7878 processPosition(mapper, rawX, rawY);
7879 processTouchMajor(mapper, rawTouchMajor);
7880 processTouchMinor(mapper, rawTouchMinor);
7881 processToolMajor(mapper, rawToolMajor);
7882 processToolMinor(mapper, rawToolMinor);
7883 processPressure(mapper, rawPressure);
7884 processOrientation(mapper, rawOrientation);
7885 processDistance(mapper, rawDistance);
7886 processId(mapper, id);
7887 processMTSync(mapper);
7888 processSync(mapper);
7889
7890 NotifyMotionArgs args;
7891 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7892 ASSERT_EQ(0, args.pointerProperties[0].id);
7893 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7894 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
7895 orientation, distance));
7896}
7897
7898TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007899 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007900 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007901 prepareAxes(POSITION | TOUCH | TOOL | MINOR);
7902 addConfigurationProperty("touch.size.calibration", "geometric");
Arpit Singha8c236b2023-04-25 13:56:05 +00007903 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007904
7905 // These calculations are based on the input device calibration documentation.
7906 int32_t rawX = 100;
7907 int32_t rawY = 200;
7908 int32_t rawTouchMajor = 140;
7909 int32_t rawTouchMinor = 120;
7910 int32_t rawToolMajor = 180;
7911 int32_t rawToolMinor = 160;
7912
7913 float x = toDisplayX(rawX);
7914 float y = toDisplayY(rawY);
7915 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
7916 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
7917 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
7918 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
7919 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
7920
7921 processPosition(mapper, rawX, rawY);
7922 processTouchMajor(mapper, rawTouchMajor);
7923 processTouchMinor(mapper, rawTouchMinor);
7924 processToolMajor(mapper, rawToolMajor);
7925 processToolMinor(mapper, rawToolMinor);
7926 processMTSync(mapper);
7927 processSync(mapper);
7928
7929 NotifyMotionArgs args;
7930 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7931 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7932 x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
7933}
7934
7935TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007936 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007937 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007938 prepareAxes(POSITION | TOUCH | TOOL);
7939 addConfigurationProperty("touch.size.calibration", "diameter");
7940 addConfigurationProperty("touch.size.scale", "10");
7941 addConfigurationProperty("touch.size.bias", "160");
7942 addConfigurationProperty("touch.size.isSummed", "1");
Arpit Singha8c236b2023-04-25 13:56:05 +00007943 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007944
7945 // These calculations are based on the input device calibration documentation.
7946 // Note: We only provide a single common touch/tool value because the device is assumed
7947 // not to emit separate values for each pointer (isSummed = 1).
7948 int32_t rawX = 100;
7949 int32_t rawY = 200;
7950 int32_t rawX2 = 150;
7951 int32_t rawY2 = 250;
7952 int32_t rawTouchMajor = 5;
7953 int32_t rawToolMajor = 8;
7954
7955 float x = toDisplayX(rawX);
7956 float y = toDisplayY(rawY);
7957 float x2 = toDisplayX(rawX2);
7958 float y2 = toDisplayY(rawY2);
7959 float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
7960 float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
7961 float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
7962
7963 processPosition(mapper, rawX, rawY);
7964 processTouchMajor(mapper, rawTouchMajor);
7965 processToolMajor(mapper, rawToolMajor);
7966 processMTSync(mapper);
7967 processPosition(mapper, rawX2, rawY2);
7968 processTouchMajor(mapper, rawTouchMajor);
7969 processToolMajor(mapper, rawToolMajor);
7970 processMTSync(mapper);
7971 processSync(mapper);
7972
7973 NotifyMotionArgs args;
7974 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7975 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
7976
7977 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007978 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007979 ASSERT_EQ(size_t(2), args.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007980 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7981 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
7982 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
7983 x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
7984}
7985
7986TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007987 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007988 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007989 prepareAxes(POSITION | TOUCH | TOOL);
7990 addConfigurationProperty("touch.size.calibration", "area");
7991 addConfigurationProperty("touch.size.scale", "43");
7992 addConfigurationProperty("touch.size.bias", "3");
Arpit Singha8c236b2023-04-25 13:56:05 +00007993 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007994
7995 // These calculations are based on the input device calibration documentation.
7996 int32_t rawX = 100;
7997 int32_t rawY = 200;
7998 int32_t rawTouchMajor = 5;
7999 int32_t rawToolMajor = 8;
8000
8001 float x = toDisplayX(rawX);
8002 float y = toDisplayY(rawY);
8003 float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
8004 float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
8005 float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
8006
8007 processPosition(mapper, rawX, rawY);
8008 processTouchMajor(mapper, rawTouchMajor);
8009 processToolMajor(mapper, rawToolMajor);
8010 processMTSync(mapper);
8011 processSync(mapper);
8012
8013 NotifyMotionArgs args;
8014 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8015 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8016 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
8017}
8018
8019TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008020 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008021 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008022 prepareAxes(POSITION | PRESSURE);
8023 addConfigurationProperty("touch.pressure.calibration", "amplitude");
8024 addConfigurationProperty("touch.pressure.scale", "0.01");
Arpit Singha8c236b2023-04-25 13:56:05 +00008025 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008026
Michael Wrightaa449c92017-12-13 21:21:43 +00008027 InputDeviceInfo info;
Harry Cuttsd02ea102023-03-17 18:21:30 +00008028 mapper.populateDeviceInfo(info);
Michael Wrightaa449c92017-12-13 21:21:43 +00008029 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
8030 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TOUCHSCREEN,
8031 0.0f, RAW_PRESSURE_MAX * 0.01, 0.0f, 0.0f));
8032
Michael Wrightd02c5b62014-02-10 15:10:22 -08008033 // These calculations are based on the input device calibration documentation.
8034 int32_t rawX = 100;
8035 int32_t rawY = 200;
8036 int32_t rawPressure = 60;
8037
8038 float x = toDisplayX(rawX);
8039 float y = toDisplayY(rawY);
8040 float pressure = float(rawPressure) * 0.01f;
8041
8042 processPosition(mapper, rawX, rawY);
8043 processPressure(mapper, rawPressure);
8044 processMTSync(mapper);
8045 processSync(mapper);
8046
8047 NotifyMotionArgs args;
8048 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8049 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8050 x, y, pressure, 0, 0, 0, 0, 0, 0, 0));
8051}
8052
8053TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008054 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008055 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008056 prepareAxes(POSITION | ID | SLOT);
Arpit Singha8c236b2023-04-25 13:56:05 +00008057 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008058
8059 NotifyMotionArgs motionArgs;
8060 NotifyKeyArgs keyArgs;
8061
8062 processId(mapper, 1);
8063 processPosition(mapper, 100, 200);
8064 processSync(mapper);
8065 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8066 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8067 ASSERT_EQ(0, motionArgs.buttonState);
8068
8069 // press BTN_LEFT, release BTN_LEFT
8070 processKey(mapper, BTN_LEFT, 1);
8071 processSync(mapper);
8072 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8073 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8074 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
8075
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008076 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8077 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8078 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
8079
Michael Wrightd02c5b62014-02-10 15:10:22 -08008080 processKey(mapper, BTN_LEFT, 0);
8081 processSync(mapper);
8082 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008083 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008084 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008085
8086 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008087 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008088 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008089
8090 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
8091 processKey(mapper, BTN_RIGHT, 1);
8092 processKey(mapper, BTN_MIDDLE, 1);
8093 processSync(mapper);
8094 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8095 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8096 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
8097 motionArgs.buttonState);
8098
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008099 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8100 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8101 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
8102
8103 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8104 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8105 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
8106 motionArgs.buttonState);
8107
Michael Wrightd02c5b62014-02-10 15:10:22 -08008108 processKey(mapper, BTN_RIGHT, 0);
8109 processSync(mapper);
8110 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008111 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008112 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008113
8114 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008115 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008116 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008117
8118 processKey(mapper, BTN_MIDDLE, 0);
8119 processSync(mapper);
8120 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008121 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008122 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008123
8124 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008125 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008126 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008127
8128 // press BTN_BACK, release BTN_BACK
8129 processKey(mapper, BTN_BACK, 1);
8130 processSync(mapper);
8131 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8132 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8133 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008134
Michael Wrightd02c5b62014-02-10 15:10:22 -08008135 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008136 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008137 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
8138
8139 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8140 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8141 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008142
8143 processKey(mapper, BTN_BACK, 0);
8144 processSync(mapper);
8145 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008146 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008147 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008148
8149 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008150 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008151 ASSERT_EQ(0, motionArgs.buttonState);
8152
Michael Wrightd02c5b62014-02-10 15:10:22 -08008153 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8154 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8155 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
8156
8157 // press BTN_SIDE, release BTN_SIDE
8158 processKey(mapper, BTN_SIDE, 1);
8159 processSync(mapper);
8160 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8161 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8162 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008163
Michael Wrightd02c5b62014-02-10 15:10:22 -08008164 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008165 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008166 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
8167
8168 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8169 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8170 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008171
8172 processKey(mapper, BTN_SIDE, 0);
8173 processSync(mapper);
8174 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008175 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008176 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008177
8178 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008179 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008180 ASSERT_EQ(0, motionArgs.buttonState);
8181
Michael Wrightd02c5b62014-02-10 15:10:22 -08008182 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8183 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8184 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
8185
8186 // press BTN_FORWARD, release BTN_FORWARD
8187 processKey(mapper, BTN_FORWARD, 1);
8188 processSync(mapper);
8189 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8190 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8191 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008192
Michael Wrightd02c5b62014-02-10 15:10:22 -08008193 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008194 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008195 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
8196
8197 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8198 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8199 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008200
8201 processKey(mapper, BTN_FORWARD, 0);
8202 processSync(mapper);
8203 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008204 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008205 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008206
8207 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008208 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008209 ASSERT_EQ(0, motionArgs.buttonState);
8210
Michael Wrightd02c5b62014-02-10 15:10:22 -08008211 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8212 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8213 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
8214
8215 // press BTN_EXTRA, release BTN_EXTRA
8216 processKey(mapper, BTN_EXTRA, 1);
8217 processSync(mapper);
8218 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8219 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8220 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008221
Michael Wrightd02c5b62014-02-10 15:10:22 -08008222 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008223 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008224 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
8225
8226 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8227 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8228 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008229
8230 processKey(mapper, BTN_EXTRA, 0);
8231 processSync(mapper);
8232 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008233 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008234 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008235
8236 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008237 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008238 ASSERT_EQ(0, motionArgs.buttonState);
8239
Michael Wrightd02c5b62014-02-10 15:10:22 -08008240 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8241 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8242 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
8243
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008244 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8245
Michael Wrightd02c5b62014-02-10 15:10:22 -08008246 // press BTN_STYLUS, release BTN_STYLUS
8247 processKey(mapper, BTN_STYLUS, 1);
8248 processSync(mapper);
8249 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8250 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008251 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
8252
8253 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8254 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8255 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008256
8257 processKey(mapper, BTN_STYLUS, 0);
8258 processSync(mapper);
8259 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008260 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008261 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008262
8263 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008264 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008265 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008266
8267 // press BTN_STYLUS2, release BTN_STYLUS2
8268 processKey(mapper, BTN_STYLUS2, 1);
8269 processSync(mapper);
8270 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8271 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008272 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
8273
8274 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8275 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8276 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008277
8278 processKey(mapper, BTN_STYLUS2, 0);
8279 processSync(mapper);
8280 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008281 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008282 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008283
8284 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008285 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008286 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008287
8288 // release touch
8289 processId(mapper, -1);
8290 processSync(mapper);
8291 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8292 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8293 ASSERT_EQ(0, motionArgs.buttonState);
8294}
8295
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00008296TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleMappedStylusButtons) {
8297 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008298 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00008299 prepareAxes(POSITION | ID | SLOT);
Arpit Singha8c236b2023-04-25 13:56:05 +00008300 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00008301
8302 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_STYLUS_BUTTON_PRIMARY, 0);
8303 mFakeEventHub->addKey(EVENTHUB_ID, 0, 0xabcd, AKEYCODE_STYLUS_BUTTON_SECONDARY, 0);
8304
8305 // Touch down.
8306 processId(mapper, 1);
8307 processPosition(mapper, 100, 200);
8308 processSync(mapper);
8309 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8310 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithButtonState(0))));
8311
8312 // Press and release button mapped to the primary stylus button.
8313 processKey(mapper, BTN_A, 1);
8314 processSync(mapper);
8315 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8316 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
8317 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
8318 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8319 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
8320 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
8321
8322 processKey(mapper, BTN_A, 0);
8323 processSync(mapper);
8324 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8325 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE), WithButtonState(0))));
8326 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8327 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
8328
8329 // Press and release the HID usage mapped to the secondary stylus button.
8330 processHidUsage(mapper, 0xabcd, 1);
8331 processSync(mapper);
8332 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8333 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
8334 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY))));
8335 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8336 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
8337 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY))));
8338
8339 processHidUsage(mapper, 0xabcd, 0);
8340 processSync(mapper);
8341 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8342 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE), WithButtonState(0))));
8343 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8344 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
8345
8346 // Release touch.
8347 processId(mapper, -1);
8348 processSync(mapper);
8349 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8350 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithButtonState(0))));
8351}
8352
Michael Wrightd02c5b62014-02-10 15:10:22 -08008353TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008354 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008355 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008356 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +00008357 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008358
8359 NotifyMotionArgs motionArgs;
8360
8361 // default tool type is finger
8362 processId(mapper, 1);
8363 processPosition(mapper, 100, 200);
8364 processSync(mapper);
8365 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8366 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008367 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008368
8369 // eraser
8370 processKey(mapper, BTN_TOOL_RUBBER, 1);
8371 processSync(mapper);
8372 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8373 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008374 ASSERT_EQ(ToolType::ERASER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008375
8376 // stylus
8377 processKey(mapper, BTN_TOOL_RUBBER, 0);
8378 processKey(mapper, BTN_TOOL_PEN, 1);
8379 processSync(mapper);
8380 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8381 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008382 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008383
8384 // brush
8385 processKey(mapper, BTN_TOOL_PEN, 0);
8386 processKey(mapper, BTN_TOOL_BRUSH, 1);
8387 processSync(mapper);
8388 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8389 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008390 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008391
8392 // pencil
8393 processKey(mapper, BTN_TOOL_BRUSH, 0);
8394 processKey(mapper, BTN_TOOL_PENCIL, 1);
8395 processSync(mapper);
8396 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8397 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008398 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008399
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08008400 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08008401 processKey(mapper, BTN_TOOL_PENCIL, 0);
8402 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
8403 processSync(mapper);
8404 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8405 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008406 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008407
8408 // mouse
8409 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
8410 processKey(mapper, BTN_TOOL_MOUSE, 1);
8411 processSync(mapper);
8412 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8413 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008414 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008415
8416 // lens
8417 processKey(mapper, BTN_TOOL_MOUSE, 0);
8418 processKey(mapper, BTN_TOOL_LENS, 1);
8419 processSync(mapper);
8420 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8421 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008422 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008423
8424 // double-tap
8425 processKey(mapper, BTN_TOOL_LENS, 0);
8426 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
8427 processSync(mapper);
8428 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8429 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008430 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008431
8432 // triple-tap
8433 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
8434 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
8435 processSync(mapper);
8436 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8437 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008438 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008439
8440 // quad-tap
8441 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
8442 processKey(mapper, BTN_TOOL_QUADTAP, 1);
8443 processSync(mapper);
8444 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8445 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008446 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008447
8448 // finger
8449 processKey(mapper, BTN_TOOL_QUADTAP, 0);
8450 processKey(mapper, BTN_TOOL_FINGER, 1);
8451 processSync(mapper);
8452 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8453 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008454 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008455
8456 // stylus trumps finger
8457 processKey(mapper, BTN_TOOL_PEN, 1);
8458 processSync(mapper);
8459 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8460 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008461 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008462
8463 // eraser trumps stylus
8464 processKey(mapper, BTN_TOOL_RUBBER, 1);
8465 processSync(mapper);
8466 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8467 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008468 ASSERT_EQ(ToolType::ERASER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008469
8470 // mouse trumps eraser
8471 processKey(mapper, BTN_TOOL_MOUSE, 1);
8472 processSync(mapper);
8473 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8474 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008475 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008476
8477 // MT tool type trumps BTN tool types: MT_TOOL_FINGER
8478 processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
8479 processSync(mapper);
8480 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8481 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008482 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008483
8484 // MT tool type trumps BTN tool types: MT_TOOL_PEN
8485 processToolType(mapper, MT_TOOL_PEN);
8486 processSync(mapper);
8487 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8488 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008489 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008490
8491 // back to default tool type
8492 processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
8493 processKey(mapper, BTN_TOOL_MOUSE, 0);
8494 processKey(mapper, BTN_TOOL_RUBBER, 0);
8495 processKey(mapper, BTN_TOOL_PEN, 0);
8496 processKey(mapper, BTN_TOOL_FINGER, 0);
8497 processSync(mapper);
8498 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8499 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008500 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008501}
8502
8503TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008504 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008505 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008506 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008507 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Arpit Singha8c236b2023-04-25 13:56:05 +00008508 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008509
8510 NotifyMotionArgs motionArgs;
8511
8512 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
8513 processId(mapper, 1);
8514 processPosition(mapper, 100, 200);
8515 processSync(mapper);
8516 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8517 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8518 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8519 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8520
8521 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8522 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8523 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8524 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8525
8526 // move a little
8527 processPosition(mapper, 150, 250);
8528 processSync(mapper);
8529 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8530 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8531 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8532 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8533
8534 // down when BTN_TOUCH is pressed, pressure defaults to 1
8535 processKey(mapper, BTN_TOUCH, 1);
8536 processSync(mapper);
8537 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8538 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8539 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8540 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8541
8542 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8543 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8544 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8545 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8546
8547 // up when BTN_TOUCH is released, hover restored
8548 processKey(mapper, BTN_TOUCH, 0);
8549 processSync(mapper);
8550 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8551 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8552 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8553 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8554
8555 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8556 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8557 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8558 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8559
8560 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8561 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8562 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8563 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8564
8565 // exit hover when pointer goes away
8566 processId(mapper, -1);
8567 processSync(mapper);
8568 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8569 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8570 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8571 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8572}
8573
8574TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008575 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008576 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008577 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +00008578 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008579
8580 NotifyMotionArgs motionArgs;
8581
8582 // initially hovering because pressure is 0
8583 processId(mapper, 1);
8584 processPosition(mapper, 100, 200);
8585 processPressure(mapper, 0);
8586 processSync(mapper);
8587 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8588 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8589 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8590 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8591
8592 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8593 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8594 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8595 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8596
8597 // move a little
8598 processPosition(mapper, 150, 250);
8599 processSync(mapper);
8600 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8601 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8602 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8603 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8604
8605 // down when pressure becomes non-zero
8606 processPressure(mapper, RAW_PRESSURE_MAX);
8607 processSync(mapper);
8608 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8609 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8610 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8611 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8612
8613 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8614 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8615 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8616 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8617
8618 // up when pressure becomes 0, hover restored
8619 processPressure(mapper, 0);
8620 processSync(mapper);
8621 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8622 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8623 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8624 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8625
8626 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8627 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8628 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8629 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8630
8631 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8632 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8633 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8634 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8635
8636 // exit hover when pointer goes away
8637 processId(mapper, -1);
8638 processSync(mapper);
8639 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8640 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8641 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8642 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8643}
8644
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008645/**
8646 * Set the input device port <--> display port associations, and check that the
8647 * events are routed to the display that matches the display port.
8648 * This can be checked by looking at the displayId of the resulting NotifyMotionArgs.
8649 */
8650TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayPort) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008651 const std::string usb2 = "USB2";
8652 const uint8_t hdmi1 = 0;
8653 const uint8_t hdmi2 = 1;
8654 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008655 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008656
8657 addConfigurationProperty("touch.deviceType", "touchScreen");
8658 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00008659 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008660
8661 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
8662 mFakePolicy->addInputPortAssociation(usb2, hdmi2);
8663
8664 // We are intentionally not adding the viewport for display 1 yet. Since the port association
8665 // for this input device is specified, and the matching viewport is not present,
8666 // the input device should be disabled (at the mapper level).
8667
8668 // Add viewport for display 2 on hdmi2
8669 prepareSecondaryDisplay(type, hdmi2);
8670 // Send a touch event
8671 processPosition(mapper, 100, 100);
8672 processSync(mapper);
8673 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8674
8675 // Add viewport for display 1 on hdmi1
Michael Wrighta9cf4192022-12-01 23:46:39 +00008676 prepareDisplay(ui::ROTATION_0, hdmi1);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008677 // Send a touch event again
8678 processPosition(mapper, 100, 100);
8679 processSync(mapper);
8680
8681 NotifyMotionArgs args;
8682 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8683 ASSERT_EQ(DISPLAY_ID, args.displayId);
8684}
Michael Wrightd02c5b62014-02-10 15:10:22 -08008685
Arthur Hung6d5b4b22022-01-21 07:21:10 +00008686TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayUniqueId) {
8687 addConfigurationProperty("touch.deviceType", "touchScreen");
8688 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00008689 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung6d5b4b22022-01-21 07:21:10 +00008690
8691 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
8692
Michael Wrighta9cf4192022-12-01 23:46:39 +00008693 prepareDisplay(ui::ROTATION_0);
8694 prepareVirtualDisplay(ui::ROTATION_0);
Arthur Hung6d5b4b22022-01-21 07:21:10 +00008695
8696 // Send a touch event
8697 processPosition(mapper, 100, 100);
8698 processSync(mapper);
8699
8700 NotifyMotionArgs args;
8701 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8702 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
8703}
8704
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008705TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShouldHandleDisplayId) {
Garfield Tan888a6a42020-01-09 11:39:16 -08008706 // Setup for second display.
Michael Wright17db18e2020-06-26 20:51:44 +01008707 std::shared_ptr<FakePointerController> fakePointerController =
8708 std::make_shared<FakePointerController>();
Garfield Tan888a6a42020-01-09 11:39:16 -08008709 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008710 fakePointerController->setPosition(100, 200);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00008711 mFakePolicy->setPointerController(fakePointerController);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008712
Garfield Tan888a6a42020-01-09 11:39:16 -08008713 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008714 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08008715
Michael Wrighta9cf4192022-12-01 23:46:39 +00008716 prepareDisplay(ui::ROTATION_0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008717 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00008718 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008719
Josep del Río2d8c79a2023-01-23 19:33:50 +00008720 // Check source is mouse that would obtain the PointerController.
8721 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008722
8723 NotifyMotionArgs motionArgs;
8724 processPosition(mapper, 100, 100);
8725 processSync(mapper);
8726
8727 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8728 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8729 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
8730}
8731
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00008732/**
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008733 * Ensure that the readTime is set to the SYN_REPORT value when processing touch events.
8734 */
8735TEST_F(MultiTouchInputMapperTest, Process_SendsReadTime) {
8736 addConfigurationProperty("touch.deviceType", "touchScreen");
8737 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00008738 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008739
Michael Wrighta9cf4192022-12-01 23:46:39 +00008740 prepareDisplay(ui::ROTATION_0);
Harry Cutts33476232023-01-30 19:57:29 +00008741 process(mapper, 10, /*readTime=*/11, EV_ABS, ABS_MT_TRACKING_ID, 1);
8742 process(mapper, 15, /*readTime=*/16, EV_ABS, ABS_MT_POSITION_X, 100);
8743 process(mapper, 20, /*readTime=*/21, EV_ABS, ABS_MT_POSITION_Y, 100);
8744 process(mapper, 25, /*readTime=*/26, EV_SYN, SYN_REPORT, 0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008745
8746 NotifyMotionArgs args;
8747 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8748 ASSERT_EQ(26, args.readTime);
8749
Harry Cutts33476232023-01-30 19:57:29 +00008750 process(mapper, 30, /*readTime=*/31, EV_ABS, ABS_MT_POSITION_X, 110);
8751 process(mapper, 30, /*readTime=*/32, EV_ABS, ABS_MT_POSITION_Y, 220);
8752 process(mapper, 30, /*readTime=*/33, EV_SYN, SYN_REPORT, 0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008753
8754 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8755 ASSERT_EQ(33, args.readTime);
8756}
8757
8758/**
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00008759 * When the viewport is not active (isActive=false), the touch mapper should be disabled and the
8760 * events should not be delivered to the listener.
8761 */
8762TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreDropped) {
8763 addConfigurationProperty("touch.deviceType", "touchScreen");
Yuncheol Heo50c19b12022-11-02 20:33:08 -07008764 // Don't set touch.enableForInactiveViewport to verify the default behavior.
Michael Wrighta9cf4192022-12-01 23:46:39 +00008765 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +00008766 /*isActive=*/false, UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00008767 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00008768 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00008769 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00008770
8771 NotifyMotionArgs motionArgs;
8772 processPosition(mapper, 100, 100);
8773 processSync(mapper);
8774
8775 mFakeListener->assertNotifyMotionWasNotCalled();
8776}
8777
Yuncheol Heo50c19b12022-11-02 20:33:08 -07008778/**
8779 * When the viewport is not active (isActive=false) and touch.enableForInactiveViewport is true,
8780 * the touch mapper can process the events and the events can be delivered to the listener.
8781 */
8782TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreProcessed) {
8783 addConfigurationProperty("touch.deviceType", "touchScreen");
8784 addConfigurationProperty("touch.enableForInactiveViewport", "1");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008785 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +00008786 /*isActive=*/false, UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00008787 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Yuncheol Heo50c19b12022-11-02 20:33:08 -07008788 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00008789 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Yuncheol Heo50c19b12022-11-02 20:33:08 -07008790
8791 NotifyMotionArgs motionArgs;
8792 processPosition(mapper, 100, 100);
8793 processSync(mapper);
8794
8795 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8796 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8797}
8798
Josh Thielene986aed2023-06-01 14:17:30 +00008799/**
8800 * When the viewport is deactivated (isActive transitions from true to false),
8801 * and touch.enableForInactiveViewport is false, touches prior to the transition
8802 * should be cancelled.
8803 */
Garfield Tanc734e4f2021-01-15 20:01:39 -08008804TEST_F(MultiTouchInputMapperTest, Process_DeactivateViewport_AbortTouches) {
8805 addConfigurationProperty("touch.deviceType", "touchScreen");
Yuncheol Heo50c19b12022-11-02 20:33:08 -07008806 addConfigurationProperty("touch.enableForInactiveViewport", "0");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008807 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +00008808 /*isActive=*/true, UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Garfield Tanc734e4f2021-01-15 20:01:39 -08008809 std::optional<DisplayViewport> optionalDisplayViewport =
8810 mFakePolicy->getDisplayViewportByUniqueId(UNIQUE_ID);
8811 ASSERT_TRUE(optionalDisplayViewport.has_value());
8812 DisplayViewport displayViewport = *optionalDisplayViewport;
8813
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00008814 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Garfield Tanc734e4f2021-01-15 20:01:39 -08008815 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00008816 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Garfield Tanc734e4f2021-01-15 20:01:39 -08008817
8818 // Finger down
8819 int32_t x = 100, y = 100;
8820 processPosition(mapper, x, y);
8821 processSync(mapper);
8822
8823 NotifyMotionArgs motionArgs;
8824 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8825 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8826
8827 // Deactivate display viewport
8828 displayViewport.isActive = false;
8829 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00008830 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Garfield Tanc734e4f2021-01-15 20:01:39 -08008831
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00008832 // The ongoing touch should be canceled immediately
8833 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8834 EXPECT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
8835
8836 // Finger move is ignored
Garfield Tanc734e4f2021-01-15 20:01:39 -08008837 x += 10, y += 10;
8838 processPosition(mapper, x, y);
8839 processSync(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00008840 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
Garfield Tanc734e4f2021-01-15 20:01:39 -08008841
8842 // Reactivate display viewport
8843 displayViewport.isActive = true;
8844 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00008845 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Garfield Tanc734e4f2021-01-15 20:01:39 -08008846
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00008847 // Finger move again starts new gesture
Garfield Tanc734e4f2021-01-15 20:01:39 -08008848 x += 10, y += 10;
8849 processPosition(mapper, x, y);
8850 processSync(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00008851 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8852 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Garfield Tanc734e4f2021-01-15 20:01:39 -08008853}
8854
Josh Thielene986aed2023-06-01 14:17:30 +00008855/**
8856 * When the viewport is deactivated (isActive transitions from true to false),
8857 * and touch.enableForInactiveViewport is true, touches prior to the transition
8858 * should not be cancelled.
8859 */
8860TEST_F(MultiTouchInputMapperTest, Process_DeactivateViewport_TouchesNotAborted) {
8861 addConfigurationProperty("touch.deviceType", "touchScreen");
8862 addConfigurationProperty("touch.enableForInactiveViewport", "1");
8863 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
8864 /*isActive=*/true, UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
8865 std::optional<DisplayViewport> optionalDisplayViewport =
8866 mFakePolicy->getDisplayViewportByUniqueId(UNIQUE_ID);
8867 ASSERT_TRUE(optionalDisplayViewport.has_value());
8868 DisplayViewport displayViewport = *optionalDisplayViewport;
8869
8870 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
8871 prepareAxes(POSITION);
8872 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
8873
8874 // Finger down
8875 int32_t x = 100, y = 100;
8876 processPosition(mapper, x, y);
8877 processSync(mapper);
8878 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8879 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
8880
8881 // Deactivate display viewport
8882 displayViewport.isActive = false;
8883 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
8884 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
8885
8886 // The ongoing touch should not be canceled
8887 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8888
8889 // Finger move is not ignored
8890 x += 10, y += 10;
8891 processPosition(mapper, x, y);
8892 processSync(mapper);
8893 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8894 WithMotionAction(AMOTION_EVENT_ACTION_MOVE)));
8895
8896 // Reactivate display viewport
8897 displayViewport.isActive = true;
8898 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
8899 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
8900
8901 // Finger move continues and does not start new gesture
8902 x += 10, y += 10;
8903 processPosition(mapper, x, y);
8904 processSync(mapper);
8905 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8906 WithMotionAction(AMOTION_EVENT_ACTION_MOVE)));
8907}
8908
Arthur Hung7c645402019-01-25 17:45:42 +08008909TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShowTouches) {
8910 // Setup the first touch screen device.
Arthur Hung7c645402019-01-25 17:45:42 +08008911 prepareAxes(POSITION | ID | SLOT);
8912 addConfigurationProperty("touch.deviceType", "touchScreen");
Arpit Singha8c236b2023-04-25 13:56:05 +00008913 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung7c645402019-01-25 17:45:42 +08008914
8915 // Create the second touch screen device, and enable multi fingers.
8916 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08008917 const std::string DEVICE_NAME2 = "TOUCHSCREEN2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08008918 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008919 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08008920 std::shared_ptr<InputDevice> device2 =
8921 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07008922 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08008923
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008924 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
Harry Cutts33476232023-01-30 19:57:29 +00008925 /*flat=*/0, /*fuzz=*/0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008926 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
Harry Cutts33476232023-01-30 19:57:29 +00008927 /*flat=*/0, /*fuzz=*/0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008928 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX,
Harry Cutts33476232023-01-30 19:57:29 +00008929 /*flat=*/0, /*fuzz=*/0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008930 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX,
Harry Cutts33476232023-01-30 19:57:29 +00008931 /*flat=*/0, /*fuzz=*/0);
8932 mFakeEventHub->setAbsoluteAxisValue(SECOND_EVENTHUB_ID, ABS_MT_SLOT, /*value=*/0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008933 mFakeEventHub->addConfigurationProperty(SECOND_EVENTHUB_ID, String8("touch.deviceType"),
8934 String8("touchScreen"));
Arthur Hung7c645402019-01-25 17:45:42 +08008935
8936 // Setup the second touch screen device.
Arpit Singha8c236b2023-04-25 13:56:05 +00008937 device2->addEmptyEventHubDevice(SECOND_EVENTHUB_ID);
8938 MultiTouchInputMapper& mapper2 = device2->constructAndAddMapper<
8939 MultiTouchInputMapper>(SECOND_EVENTHUB_ID, mFakePolicy->getReaderConfiguration());
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07008940 std::list<NotifyArgs> unused =
8941 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00008942 /*changes=*/{});
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07008943 unused += device2->reset(ARBITRARY_TIME);
Arthur Hung7c645402019-01-25 17:45:42 +08008944
8945 // Setup PointerController.
Michael Wright17db18e2020-06-26 20:51:44 +01008946 std::shared_ptr<FakePointerController> fakePointerController =
8947 std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00008948 mFakePolicy->setPointerController(fakePointerController);
Arthur Hung7c645402019-01-25 17:45:42 +08008949
8950 // Setup policy for associated displays and show touches.
8951 const uint8_t hdmi1 = 0;
8952 const uint8_t hdmi2 = 1;
8953 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
8954 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
8955 mFakePolicy->setShowTouches(true);
8956
8957 // Create displays.
Michael Wrighta9cf4192022-12-01 23:46:39 +00008958 prepareDisplay(ui::ROTATION_0, hdmi1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008959 prepareSecondaryDisplay(ViewportType::EXTERNAL, hdmi2);
Arthur Hung7c645402019-01-25 17:45:42 +08008960
8961 // Default device will reconfigure above, need additional reconfiguration for another device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07008962 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00008963 InputReaderConfiguration::Change::DISPLAY_INFO |
8964 InputReaderConfiguration::Change::SHOW_TOUCHES);
Arthur Hung7c645402019-01-25 17:45:42 +08008965
8966 // Two fingers down at default display.
8967 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
8968 processPosition(mapper, x1, y1);
8969 processId(mapper, 1);
8970 processSlot(mapper, 1);
8971 processPosition(mapper, x2, y2);
8972 processId(mapper, 2);
8973 processSync(mapper);
8974
8975 std::map<int32_t, std::vector<int32_t>>::const_iterator iter =
8976 fakePointerController->getSpots().find(DISPLAY_ID);
8977 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
8978 ASSERT_EQ(size_t(2), iter->second.size());
8979
8980 // Two fingers down at second display.
8981 processPosition(mapper2, x1, y1);
8982 processId(mapper2, 1);
8983 processSlot(mapper2, 1);
8984 processPosition(mapper2, x2, y2);
8985 processId(mapper2, 2);
8986 processSync(mapper2);
8987
8988 iter = fakePointerController->getSpots().find(SECONDARY_DISPLAY_ID);
8989 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
8990 ASSERT_EQ(size_t(2), iter->second.size());
Prabir Pradhan197e0862022-07-01 14:28:00 +00008991
8992 // Disable the show touches configuration and ensure the spots are cleared.
8993 mFakePolicy->setShowTouches(false);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07008994 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00008995 InputReaderConfiguration::Change::SHOW_TOUCHES);
Prabir Pradhan197e0862022-07-01 14:28:00 +00008996
8997 ASSERT_TRUE(fakePointerController->getSpots().empty());
Arthur Hung7c645402019-01-25 17:45:42 +08008998}
8999
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06009000TEST_F(MultiTouchInputMapperTest, VideoFrames_ReceivedByListener) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06009001 prepareAxes(POSITION);
9002 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009003 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +00009004 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06009005
9006 NotifyMotionArgs motionArgs;
9007 // Unrotated video frame
9008 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9009 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009010 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06009011 processPosition(mapper, 100, 200);
9012 processSync(mapper);
9013 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9014 ASSERT_EQ(frames, motionArgs.videoFrames);
9015
9016 // Subsequent touch events should not have any videoframes
9017 // This is implemented separately in FakeEventHub,
9018 // but that should match the behaviour of TouchVideoDevice.
9019 processPosition(mapper, 200, 200);
9020 processSync(mapper);
9021 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9022 ASSERT_EQ(std::vector<TouchVideoFrame>(), motionArgs.videoFrames);
9023}
9024
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009025TEST_F(MultiTouchInputMapperTest, VideoFrames_AreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009026 prepareAxes(POSITION);
9027 addConfigurationProperty("touch.deviceType", "touchScreen");
Arpit Singha8c236b2023-04-25 13:56:05 +00009028 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009029 // Unrotated video frame
9030 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9031 NotifyMotionArgs motionArgs;
9032
9033 // Test all 4 orientations
Michael Wrighta9cf4192022-12-01 23:46:39 +00009034 for (ui::Rotation orientation : ftl::enum_range<ui::Rotation>()) {
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009035 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
9036 clearViewports();
9037 prepareDisplay(orientation);
9038 std::vector<TouchVideoFrame> frames{frame};
9039 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
9040 processPosition(mapper, 100, 200);
9041 processSync(mapper);
9042 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9043 ASSERT_EQ(frames, motionArgs.videoFrames);
9044 }
9045}
9046
9047TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_AreRotated) {
9048 prepareAxes(POSITION);
9049 addConfigurationProperty("touch.deviceType", "touchScreen");
9050 // Since InputReader works in the un-rotated coordinate space, only devices that are not
9051 // orientation-aware are affected by display rotation.
9052 addConfigurationProperty("touch.orientationAware", "0");
Arpit Singha8c236b2023-04-25 13:56:05 +00009053 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009054 // Unrotated video frame
9055 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9056 NotifyMotionArgs motionArgs;
9057
9058 // Test all 4 orientations
Michael Wrighta9cf4192022-12-01 23:46:39 +00009059 for (ui::Rotation orientation : ftl::enum_range<ui::Rotation>()) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009060 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
9061 clearViewports();
9062 prepareDisplay(orientation);
9063 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009064 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009065 processPosition(mapper, 100, 200);
9066 processSync(mapper);
9067 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009068 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
9069 // compared to the display. This is so that when the window transform (which contains the
9070 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
9071 // window's coordinate space.
9072 frames[0].rotate(getInverseRotation(orientation));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009073 ASSERT_EQ(frames, motionArgs.videoFrames);
lilinnan687e58f2022-07-19 16:00:50 +08009074
9075 // Release finger.
9076 processSync(mapper);
9077 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009078 }
9079}
9080
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009081TEST_F(MultiTouchInputMapperTest, VideoFrames_MultipleFramesAreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009082 prepareAxes(POSITION);
9083 addConfigurationProperty("touch.deviceType", "touchScreen");
Arpit Singha8c236b2023-04-25 13:56:05 +00009084 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009085 // Unrotated video frames. There's no rule that they must all have the same dimensions,
9086 // so mix these.
9087 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9088 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
9089 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
9090 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
9091 NotifyMotionArgs motionArgs;
9092
Michael Wrighta9cf4192022-12-01 23:46:39 +00009093 prepareDisplay(ui::ROTATION_90);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009094 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009095 processPosition(mapper, 100, 200);
9096 processSync(mapper);
9097 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009098 ASSERT_EQ(frames, motionArgs.videoFrames);
9099}
9100
9101TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_MultipleFramesAreRotated) {
9102 prepareAxes(POSITION);
9103 addConfigurationProperty("touch.deviceType", "touchScreen");
9104 // Since InputReader works in the un-rotated coordinate space, only devices that are not
9105 // orientation-aware are affected by display rotation.
9106 addConfigurationProperty("touch.orientationAware", "0");
Arpit Singha8c236b2023-04-25 13:56:05 +00009107 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009108 // Unrotated video frames. There's no rule that they must all have the same dimensions,
9109 // so mix these.
9110 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9111 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
9112 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
9113 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
9114 NotifyMotionArgs motionArgs;
9115
Michael Wrighta9cf4192022-12-01 23:46:39 +00009116 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009117 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
9118 processPosition(mapper, 100, 200);
9119 processSync(mapper);
9120 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9121 std::for_each(frames.begin(), frames.end(), [](TouchVideoFrame& frame) {
9122 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
9123 // compared to the display. This is so that when the window transform (which contains the
9124 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
9125 // window's coordinate space.
Michael Wrighta9cf4192022-12-01 23:46:39 +00009126 frame.rotate(getInverseRotation(ui::ROTATION_90));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009127 });
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009128 ASSERT_EQ(frames, motionArgs.videoFrames);
9129}
9130
Arthur Hung9da14732019-09-02 16:16:58 +08009131/**
9132 * If we had defined port associations, but the viewport is not ready, the touch device would be
9133 * expected to be disabled, and it should be enabled after the viewport has found.
9134 */
9135TEST_F(MultiTouchInputMapperTest, Configure_EnabledForAssociatedDisplay) {
Arthur Hung9da14732019-09-02 16:16:58 +08009136 constexpr uint8_t hdmi2 = 1;
9137 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009138 constexpr ViewportType type = ViewportType::EXTERNAL;
Arthur Hung9da14732019-09-02 16:16:58 +08009139
9140 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi2);
9141
9142 addConfigurationProperty("touch.deviceType", "touchScreen");
9143 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00009144 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung9da14732019-09-02 16:16:58 +08009145
9146 ASSERT_EQ(mDevice->isEnabled(), false);
9147
9148 // Add display on hdmi2, the device should be enabled and can receive touch event.
9149 prepareSecondaryDisplay(type, hdmi2);
9150 ASSERT_EQ(mDevice->isEnabled(), true);
9151
9152 // Send a touch event.
9153 processPosition(mapper, 100, 100);
9154 processSync(mapper);
9155
9156 NotifyMotionArgs args;
9157 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9158 ASSERT_EQ(SECONDARY_DISPLAY_ID, args.displayId);
9159}
9160
Arthur Hung421eb1c2020-01-16 00:09:42 +08009161TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleSingleTouch) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08009162 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009163 prepareDisplay(ui::ROTATION_0);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009164 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +00009165 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08009166
9167 NotifyMotionArgs motionArgs;
9168
9169 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
9170 // finger down
9171 processId(mapper, 1);
9172 processPosition(mapper, x1, y1);
9173 processSync(mapper);
9174 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9175 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009176 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009177
9178 // finger move
9179 processId(mapper, 1);
9180 processPosition(mapper, x2, y2);
9181 processSync(mapper);
9182 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9183 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009184 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009185
9186 // finger up.
9187 processId(mapper, -1);
9188 processSync(mapper);
9189 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9190 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009191 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009192
9193 // new finger down
9194 processId(mapper, 1);
9195 processPosition(mapper, x3, y3);
9196 processSync(mapper);
9197 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9198 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009199 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009200}
9201
9202/**
arthurhungcc7f9802020-04-30 17:55:40 +08009203 * Test single touch should be canceled when received the MT_TOOL_PALM event, and the following
9204 * MOVE and UP events should be ignored.
Arthur Hung421eb1c2020-01-16 00:09:42 +08009205 */
arthurhungcc7f9802020-04-30 17:55:40 +08009206TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_SinglePointer) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08009207 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009208 prepareDisplay(ui::ROTATION_0);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009209 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +00009210 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08009211
9212 NotifyMotionArgs motionArgs;
9213
9214 // default tool type is finger
9215 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
arthurhungcc7f9802020-04-30 17:55:40 +08009216 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009217 processPosition(mapper, x1, y1);
9218 processSync(mapper);
9219 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9220 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009221 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009222
9223 // Tool changed to MT_TOOL_PALM expect sending the cancel event.
9224 processToolType(mapper, MT_TOOL_PALM);
9225 processSync(mapper);
9226 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9227 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9228
9229 // Ignore the following MOVE and UP events if had detect a palm event.
arthurhungcc7f9802020-04-30 17:55:40 +08009230 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009231 processPosition(mapper, x2, y2);
9232 processSync(mapper);
9233 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9234
9235 // finger up.
arthurhungcc7f9802020-04-30 17:55:40 +08009236 processId(mapper, INVALID_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009237 processSync(mapper);
9238 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9239
9240 // new finger down
arthurhungcc7f9802020-04-30 17:55:40 +08009241 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009242 processToolType(mapper, MT_TOOL_FINGER);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009243 processPosition(mapper, x3, y3);
9244 processSync(mapper);
9245 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9246 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009247 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009248}
9249
arthurhungbf89a482020-04-17 17:37:55 +08009250/**
arthurhungcc7f9802020-04-30 17:55:40 +08009251 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
9252 * and the rest active fingers could still be allowed to receive the events
arthurhungbf89a482020-04-17 17:37:55 +08009253 */
arthurhungcc7f9802020-04-30 17:55:40 +08009254TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_TwoPointers) {
arthurhungbf89a482020-04-17 17:37:55 +08009255 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009256 prepareDisplay(ui::ROTATION_0);
arthurhungbf89a482020-04-17 17:37:55 +08009257 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +00009258 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
arthurhungbf89a482020-04-17 17:37:55 +08009259
9260 NotifyMotionArgs motionArgs;
9261
9262 // default tool type is finger
arthurhungcc7f9802020-04-30 17:55:40 +08009263 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
9264 processId(mapper, FIRST_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009265 processPosition(mapper, x1, y1);
9266 processSync(mapper);
9267 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9268 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009269 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungbf89a482020-04-17 17:37:55 +08009270
9271 // Second finger down.
arthurhungcc7f9802020-04-30 17:55:40 +08009272 processSlot(mapper, SECOND_SLOT);
9273 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009274 processPosition(mapper, x2, y2);
arthurhungcc7f9802020-04-30 17:55:40 +08009275 processSync(mapper);
9276 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009277 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009278 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08009279
9280 // If the tool type of the first finger changes to MT_TOOL_PALM,
9281 // we expect to receive ACTION_POINTER_UP with cancel flag.
9282 processSlot(mapper, FIRST_SLOT);
9283 processId(mapper, FIRST_TRACKING_ID);
9284 processToolType(mapper, MT_TOOL_PALM);
9285 processSync(mapper);
9286 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009287 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009288 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9289
9290 // The following MOVE events of second finger should be processed.
9291 processSlot(mapper, SECOND_SLOT);
9292 processId(mapper, SECOND_TRACKING_ID);
9293 processPosition(mapper, x2 + 1, y2 + 1);
9294 processSync(mapper);
9295 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9296 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009297 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +08009298
9299 // First finger up. It used to be in palm mode, and we already generated ACTION_POINTER_UP for
9300 // it. Second finger receive move.
9301 processSlot(mapper, FIRST_SLOT);
9302 processId(mapper, INVALID_TRACKING_ID);
9303 processSync(mapper);
9304 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9305 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009306 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +08009307
9308 // Second finger keeps moving.
9309 processSlot(mapper, SECOND_SLOT);
9310 processId(mapper, SECOND_TRACKING_ID);
9311 processPosition(mapper, x2 + 2, y2 + 2);
9312 processSync(mapper);
9313 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9314 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009315 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +08009316
9317 // Second finger up.
9318 processId(mapper, INVALID_TRACKING_ID);
9319 processSync(mapper);
9320 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9321 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9322 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9323}
9324
9325/**
9326 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event, if only 1 finger
9327 * is active, it should send CANCEL after receiving the MT_TOOL_PALM event.
9328 */
9329TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_ShouldCancelWhenAllTouchIsPalm) {
9330 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009331 prepareDisplay(ui::ROTATION_0);
arthurhungcc7f9802020-04-30 17:55:40 +08009332 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +00009333 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
arthurhungcc7f9802020-04-30 17:55:40 +08009334
9335 NotifyMotionArgs motionArgs;
9336
9337 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
9338 // First finger down.
9339 processId(mapper, FIRST_TRACKING_ID);
9340 processPosition(mapper, x1, y1);
9341 processSync(mapper);
9342 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9343 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009344 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08009345
9346 // Second finger down.
9347 processSlot(mapper, SECOND_SLOT);
9348 processId(mapper, SECOND_TRACKING_ID);
9349 processPosition(mapper, x2, y2);
arthurhungbf89a482020-04-17 17:37:55 +08009350 processSync(mapper);
9351 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009352 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009353 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungbf89a482020-04-17 17:37:55 +08009354
arthurhungcc7f9802020-04-30 17:55:40 +08009355 // If the tool type of the first finger changes to MT_TOOL_PALM,
9356 // we expect to receive ACTION_POINTER_UP with cancel flag.
9357 processSlot(mapper, FIRST_SLOT);
9358 processId(mapper, FIRST_TRACKING_ID);
9359 processToolType(mapper, MT_TOOL_PALM);
9360 processSync(mapper);
9361 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009362 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009363 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9364
9365 // Second finger keeps moving.
9366 processSlot(mapper, SECOND_SLOT);
9367 processId(mapper, SECOND_TRACKING_ID);
9368 processPosition(mapper, x2 + 1, y2 + 1);
9369 processSync(mapper);
9370 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9371 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9372
9373 // second finger becomes palm, receive cancel due to only 1 finger is active.
9374 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009375 processToolType(mapper, MT_TOOL_PALM);
9376 processSync(mapper);
9377 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9378 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9379
arthurhungcc7f9802020-04-30 17:55:40 +08009380 // third finger down.
9381 processSlot(mapper, THIRD_SLOT);
9382 processId(mapper, THIRD_TRACKING_ID);
9383 processToolType(mapper, MT_TOOL_FINGER);
arthurhungbf89a482020-04-17 17:37:55 +08009384 processPosition(mapper, x3, y3);
9385 processSync(mapper);
arthurhungbf89a482020-04-17 17:37:55 +08009386 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9387 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009388 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009389 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +08009390
9391 // third finger move
9392 processId(mapper, THIRD_TRACKING_ID);
9393 processPosition(mapper, x3 + 1, y3 + 1);
9394 processSync(mapper);
9395 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9396 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9397
9398 // first finger up, third finger receive move.
9399 processSlot(mapper, FIRST_SLOT);
9400 processId(mapper, INVALID_TRACKING_ID);
9401 processSync(mapper);
9402 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9403 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009404 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +08009405
9406 // second finger up, third finger receive move.
9407 processSlot(mapper, SECOND_SLOT);
9408 processId(mapper, INVALID_TRACKING_ID);
9409 processSync(mapper);
9410 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9411 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009412 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +08009413
9414 // third finger up.
9415 processSlot(mapper, THIRD_SLOT);
9416 processId(mapper, INVALID_TRACKING_ID);
9417 processSync(mapper);
9418 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9419 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9420 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9421}
9422
9423/**
9424 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
9425 * and the active finger could still be allowed to receive the events
9426 */
9427TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_KeepFirstPointer) {
9428 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009429 prepareDisplay(ui::ROTATION_0);
arthurhungcc7f9802020-04-30 17:55:40 +08009430 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +00009431 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
arthurhungcc7f9802020-04-30 17:55:40 +08009432
9433 NotifyMotionArgs motionArgs;
9434
9435 // default tool type is finger
9436 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
9437 processId(mapper, FIRST_TRACKING_ID);
9438 processPosition(mapper, x1, y1);
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);
arthurhungcc7f9802020-04-30 17:55:40 +08009443
9444 // Second finger down.
9445 processSlot(mapper, SECOND_SLOT);
9446 processId(mapper, SECOND_TRACKING_ID);
9447 processPosition(mapper, x2, y2);
9448 processSync(mapper);
9449 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009450 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009451 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08009452
9453 // If the tool type of the second finger changes to MT_TOOL_PALM,
9454 // we expect to receive ACTION_POINTER_UP with cancel flag.
9455 processId(mapper, SECOND_TRACKING_ID);
9456 processToolType(mapper, MT_TOOL_PALM);
9457 processSync(mapper);
9458 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009459 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009460 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9461
9462 // The following MOVE event should be processed.
9463 processSlot(mapper, FIRST_SLOT);
9464 processId(mapper, FIRST_TRACKING_ID);
9465 processPosition(mapper, x1 + 1, y1 + 1);
9466 processSync(mapper);
9467 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9468 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009469 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +08009470
9471 // second finger up.
9472 processSlot(mapper, SECOND_SLOT);
9473 processId(mapper, INVALID_TRACKING_ID);
9474 processSync(mapper);
9475 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9476 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9477
9478 // first finger keep moving
9479 processSlot(mapper, FIRST_SLOT);
9480 processId(mapper, FIRST_TRACKING_ID);
9481 processPosition(mapper, x1 + 2, y1 + 2);
9482 processSync(mapper);
9483 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9484 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9485
9486 // first finger up.
9487 processId(mapper, INVALID_TRACKING_ID);
9488 processSync(mapper);
9489 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9490 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9491 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
arthurhungbf89a482020-04-17 17:37:55 +08009492}
9493
Arthur Hung9ad18942021-06-19 02:04:46 +00009494/**
9495 * Test multi-touch should sent ACTION_POINTER_UP/ACTION_UP when received the INVALID_TRACKING_ID,
9496 * to prevent the driver side may send unexpected data after set tracking id as INVALID_TRACKING_ID
9497 * cause slot be valid again.
9498 */
9499TEST_F(MultiTouchInputMapperTest, Process_MultiTouch_WithInvalidTrackingId) {
9500 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009501 prepareDisplay(ui::ROTATION_0);
Arthur Hung9ad18942021-06-19 02:04:46 +00009502 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +00009503 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung9ad18942021-06-19 02:04:46 +00009504
9505 NotifyMotionArgs motionArgs;
9506
9507 constexpr int32_t x1 = 100, y1 = 200, x2 = 0, y2 = 0;
9508 // First finger down.
9509 processId(mapper, FIRST_TRACKING_ID);
9510 processPosition(mapper, x1, y1);
9511 processPressure(mapper, RAW_PRESSURE_MAX);
9512 processSync(mapper);
9513 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9514 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009515 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
Arthur Hung9ad18942021-06-19 02:04:46 +00009516
9517 // First finger move.
9518 processId(mapper, FIRST_TRACKING_ID);
9519 processPosition(mapper, x1 + 1, y1 + 1);
9520 processPressure(mapper, RAW_PRESSURE_MAX);
9521 processSync(mapper);
9522 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9523 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009524 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
Arthur Hung9ad18942021-06-19 02:04:46 +00009525
9526 // Second finger down.
9527 processSlot(mapper, SECOND_SLOT);
9528 processId(mapper, SECOND_TRACKING_ID);
9529 processPosition(mapper, x2, y2);
9530 processPressure(mapper, RAW_PRESSURE_MAX);
9531 processSync(mapper);
9532 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009533 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009534 ASSERT_EQ(uint32_t(2), motionArgs.getPointerCount());
Arthur Hung9ad18942021-06-19 02:04:46 +00009535
9536 // second finger up with some unexpected data.
9537 processSlot(mapper, SECOND_SLOT);
9538 processId(mapper, INVALID_TRACKING_ID);
9539 processPosition(mapper, x2, y2);
9540 processSync(mapper);
9541 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009542 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009543 ASSERT_EQ(uint32_t(2), motionArgs.getPointerCount());
Arthur Hung9ad18942021-06-19 02:04:46 +00009544
9545 // first finger up with some unexpected data.
9546 processSlot(mapper, FIRST_SLOT);
9547 processId(mapper, INVALID_TRACKING_ID);
9548 processPosition(mapper, x2, y2);
9549 processPressure(mapper, RAW_PRESSURE_MAX);
9550 processSync(mapper);
9551 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9552 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009553 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
Arthur Hung9ad18942021-06-19 02:04:46 +00009554}
9555
Arpit Singh4b4a4572023-11-24 18:19:56 +00009556TEST_F(MultiTouchInputMapperTest, Reset_RepopulatesMultiTouchState) {
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009557 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009558 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009559 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +00009560 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009561
9562 // First finger down.
Arpit Singh4b4a4572023-11-24 18:19:56 +00009563 constexpr int32_t x1 = 100, y1 = 200, x2 = 300, y2 = 400;
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009564 processId(mapper, FIRST_TRACKING_ID);
Arpit Singh4b4a4572023-11-24 18:19:56 +00009565 processPosition(mapper, x1, y1);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009566 processPressure(mapper, RAW_PRESSURE_MAX);
9567 processSync(mapper);
9568 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9569 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
9570
9571 // Second finger down.
9572 processSlot(mapper, SECOND_SLOT);
9573 processId(mapper, SECOND_TRACKING_ID);
Arpit Singh4b4a4572023-11-24 18:19:56 +00009574 processPosition(mapper, x2, y2);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009575 processPressure(mapper, RAW_PRESSURE_MAX);
9576 processSync(mapper);
9577 ASSERT_NO_FATAL_FAILURE(
9578 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(ACTION_POINTER_1_DOWN)));
9579
Arpit Singh4b4a4572023-11-24 18:19:56 +00009580 // Set MT Slot state to be repopulated for the required slots
9581 std::vector<int32_t> mtSlotValues(RAW_SLOT_MAX + 1, -1);
9582 mtSlotValues[0] = FIRST_TRACKING_ID;
9583 mtSlotValues[1] = SECOND_TRACKING_ID;
9584 mFakeEventHub->setMtSlotValues(EVENTHUB_ID, ABS_MT_TRACKING_ID, mtSlotValues);
9585
9586 mtSlotValues[0] = x1;
9587 mtSlotValues[1] = x2;
9588 mFakeEventHub->setMtSlotValues(EVENTHUB_ID, ABS_MT_POSITION_X, mtSlotValues);
9589
9590 mtSlotValues[0] = y1;
9591 mtSlotValues[1] = y2;
9592 mFakeEventHub->setMtSlotValues(EVENTHUB_ID, ABS_MT_POSITION_Y, mtSlotValues);
9593
9594 mtSlotValues[0] = RAW_PRESSURE_MAX;
9595 mtSlotValues[1] = RAW_PRESSURE_MAX;
9596 mFakeEventHub->setMtSlotValues(EVENTHUB_ID, ABS_MT_PRESSURE, mtSlotValues);
9597
Arpit Singh4bb0bd52023-12-20 14:41:10 +00009598 // Reset the mapper. When the mapper is reset, we expect the current multi-touch state to be
Arpit Singh4b4a4572023-11-24 18:19:56 +00009599 // repopulated. Resetting should cancel the ongoing gesture.
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00009600 resetMapper(mapper, ARBITRARY_TIME);
9601 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9602 WithMotionAction(AMOTION_EVENT_ACTION_CANCEL)));
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009603
Arpit Singh4bb0bd52023-12-20 14:41:10 +00009604 // Send a sync to simulate an empty touch frame where nothing changes. The mapper should use
9605 // the existing touch state to generate a down event.
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009606 processPosition(mapper, 301, 302);
9607 processSync(mapper);
9608 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9609 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithPressure(1.f))));
Arpit Singh4bb0bd52023-12-20 14:41:10 +00009610 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9611 AllOf(WithMotionAction(ACTION_POINTER_1_DOWN), WithPressure(1.f))));
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009612
9613 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9614}
9615
Arpit Singh4bb0bd52023-12-20 14:41:10 +00009616TEST_F(MultiTouchInputMapperTest, Reset_PreservesLastTouchState_NoPointersDown) {
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009617 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009618 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009619 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +00009620 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009621
9622 // First finger touches down and releases.
9623 processId(mapper, FIRST_TRACKING_ID);
9624 processPosition(mapper, 100, 200);
9625 processPressure(mapper, RAW_PRESSURE_MAX);
9626 processSync(mapper);
9627 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9628 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
9629 processId(mapper, INVALID_TRACKING_ID);
9630 processSync(mapper);
9631 ASSERT_NO_FATAL_FAILURE(
9632 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
9633
9634 // Reset the mapper. When the mapper is reset, we expect it to restore the latest
9635 // raw state where no pointers are down.
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00009636 resetMapper(mapper, ARBITRARY_TIME);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009637 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9638
9639 // Send an empty sync frame. Since there are no pointers, no events are generated.
9640 processSync(mapper);
9641 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9642}
9643
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +00009644TEST_F(MultiTouchInputMapperTest, StylusSourceIsAddedDynamicallyFromToolType) {
Prabir Pradhanf9a41282022-10-25 17:15:50 +00009645 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009646 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanf9a41282022-10-25 17:15:50 +00009647 prepareAxes(POSITION | ID | SLOT | PRESSURE | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +00009648 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +00009649 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
Prabir Pradhanf9a41282022-10-25 17:15:50 +00009650
9651 // Even if the device supports reporting the ABS_MT_TOOL_TYPE axis, which could give it the
9652 // ability to report MT_TOOL_PEN, we do not report the device as coming from a stylus source.
9653 // Due to limitations in the evdev protocol, we cannot say for certain that a device is capable
9654 // of reporting stylus events just because it supports ABS_MT_TOOL_TYPE.
9655 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
9656
9657 // However, if the device ever ends up reporting an event with MT_TOOL_PEN, it should be
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +00009658 // reported with the stylus source.
Prabir Pradhanf9a41282022-10-25 17:15:50 +00009659 processId(mapper, FIRST_TRACKING_ID);
9660 processToolType(mapper, MT_TOOL_PEN);
9661 processPosition(mapper, 100, 200);
9662 processPressure(mapper, RAW_PRESSURE_MAX);
9663 processSync(mapper);
9664 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9665 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
9666 WithSource(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009667 WithToolType(ToolType::STYLUS))));
Prabir Pradhanf9a41282022-10-25 17:15:50 +00009668
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +00009669 // Now that we know the device supports styluses, ensure that the device is re-configured with
9670 // the stylus source.
9671 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS, mapper.getSources());
9672 {
9673 const auto& devices = mReader->getInputDevices();
9674 auto deviceInfo =
9675 std::find_if(devices.begin(), devices.end(),
9676 [](const InputDeviceInfo& info) { return info.getId() == DEVICE_ID; });
9677 LOG_ALWAYS_FATAL_IF(deviceInfo == devices.end(), "Cannot find InputDevice");
9678 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS, deviceInfo->getSources());
9679 }
9680
9681 // Ensure the device was not reset to prevent interruptions of any ongoing gestures.
9682 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
9683
Prabir Pradhanf9a41282022-10-25 17:15:50 +00009684 processId(mapper, INVALID_TRACKING_ID);
9685 processSync(mapper);
9686 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9687 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
9688 WithSource(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009689 WithToolType(ToolType::STYLUS))));
Prabir Pradhanf9a41282022-10-25 17:15:50 +00009690}
9691
Seunghwan Choi356026c2023-02-01 14:37:25 +09009692TEST_F(MultiTouchInputMapperTest, Process_WhenConfigEnabled_ShouldShowDirectStylusPointer) {
9693 addConfigurationProperty("touch.deviceType", "touchScreen");
9694 prepareDisplay(ui::ROTATION_0);
9695 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE | PRESSURE);
9696 // Add BTN_TOOL_PEN to statically show stylus support, since using ABS_MT_TOOL_TYPE can only
9697 // indicate stylus presence dynamically.
9698 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
9699 std::shared_ptr<FakePointerController> fakePointerController =
9700 std::make_shared<FakePointerController>();
9701 mFakePolicy->setPointerController(fakePointerController);
9702 mFakePolicy->setStylusPointerIconEnabled(true);
Arpit Singha8c236b2023-04-25 13:56:05 +00009703 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Seunghwan Choi356026c2023-02-01 14:37:25 +09009704
9705 processId(mapper, FIRST_TRACKING_ID);
9706 processPressure(mapper, RAW_PRESSURE_MIN);
9707 processPosition(mapper, 100, 200);
9708 processToolType(mapper, MT_TOOL_PEN);
9709 processSync(mapper);
9710 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9711 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009712 WithToolType(ToolType::STYLUS),
Seunghwan Choi356026c2023-02-01 14:37:25 +09009713 WithPointerCoords(0, toDisplayX(100), toDisplayY(200)))));
9714 ASSERT_TRUE(fakePointerController->isPointerShown());
9715 ASSERT_NO_FATAL_FAILURE(
9716 fakePointerController->assertPosition(toDisplayX(100), toDisplayY(200)));
9717}
9718
9719TEST_F(MultiTouchInputMapperTest, Process_WhenConfigDisabled_ShouldNotShowDirectStylusPointer) {
9720 addConfigurationProperty("touch.deviceType", "touchScreen");
9721 prepareDisplay(ui::ROTATION_0);
9722 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE | PRESSURE);
9723 // Add BTN_TOOL_PEN to statically show stylus support, since using ABS_MT_TOOL_TYPE can only
9724 // indicate stylus presence dynamically.
9725 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
9726 std::shared_ptr<FakePointerController> fakePointerController =
9727 std::make_shared<FakePointerController>();
9728 mFakePolicy->setPointerController(fakePointerController);
9729 mFakePolicy->setStylusPointerIconEnabled(false);
Arpit Singha8c236b2023-04-25 13:56:05 +00009730 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Seunghwan Choi356026c2023-02-01 14:37:25 +09009731
9732 processId(mapper, FIRST_TRACKING_ID);
9733 processPressure(mapper, RAW_PRESSURE_MIN);
9734 processPosition(mapper, 100, 200);
9735 processToolType(mapper, MT_TOOL_PEN);
9736 processSync(mapper);
9737 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9738 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009739 WithToolType(ToolType::STYLUS),
Seunghwan Choi356026c2023-02-01 14:37:25 +09009740 WithPointerCoords(0, toDisplayX(100), toDisplayY(200)))));
9741 ASSERT_FALSE(fakePointerController->isPointerShown());
9742}
9743
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009744// --- MultiTouchInputMapperTest_ExternalDevice ---
9745
9746class MultiTouchInputMapperTest_ExternalDevice : public MultiTouchInputMapperTest {
9747protected:
Chris Yea52ade12020-08-27 16:49:20 -07009748 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009749};
9750
9751/**
9752 * Expect fallback to internal viewport if device is external and external viewport is not present.
9753 */
9754TEST_F(MultiTouchInputMapperTest_ExternalDevice, Viewports_Fallback) {
9755 prepareAxes(POSITION);
9756 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009757 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +00009758 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009759
9760 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
9761
9762 NotifyMotionArgs motionArgs;
9763
9764 // Expect the event to be sent to the internal viewport,
9765 // because an external viewport is not present.
9766 processPosition(mapper, 100, 100);
9767 processSync(mapper);
9768 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9769 ASSERT_EQ(ADISPLAY_ID_DEFAULT, motionArgs.displayId);
9770
9771 // Expect the event to be sent to the external viewport if it is present.
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009772 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009773 processPosition(mapper, 100, 100);
9774 processSync(mapper);
9775 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9776 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
9777}
Arthur Hung4197f6b2020-03-16 15:39:59 +08009778
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009779TEST_F(MultiTouchInputMapperTest, Process_TouchpadCapture) {
9780 // we need a pointer controller for mouse mode of touchpad (start pointer at 0,0)
9781 std::shared_ptr<FakePointerController> fakePointerController =
9782 std::make_shared<FakePointerController>();
9783 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
9784 fakePointerController->setPosition(0, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009785
9786 // prepare device and capture
Michael Wrighta9cf4192022-12-01 23:46:39 +00009787 prepareDisplay(ui::ROTATION_0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009788 prepareAxes(POSITION | ID | SLOT);
9789 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
9790 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
9791 mFakePolicy->setPointerCapture(true);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009792 mFakePolicy->setPointerController(fakePointerController);
Arpit Singha8c236b2023-04-25 13:56:05 +00009793 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009794
9795 // captured touchpad should be a touchpad source
9796 NotifyDeviceResetArgs resetArgs;
9797 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
9798 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
9799
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009800 InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
Chris Yef74dc422020-09-02 22:41:50 -07009801
9802 const InputDeviceInfo::MotionRange* relRangeX =
9803 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_X, AINPUT_SOURCE_TOUCHPAD);
9804 ASSERT_NE(relRangeX, nullptr);
9805 ASSERT_EQ(relRangeX->min, -(RAW_X_MAX - RAW_X_MIN));
9806 ASSERT_EQ(relRangeX->max, RAW_X_MAX - RAW_X_MIN);
9807 const InputDeviceInfo::MotionRange* relRangeY =
9808 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_Y, AINPUT_SOURCE_TOUCHPAD);
9809 ASSERT_NE(relRangeY, nullptr);
9810 ASSERT_EQ(relRangeY->min, -(RAW_Y_MAX - RAW_Y_MIN));
9811 ASSERT_EQ(relRangeY->max, RAW_Y_MAX - RAW_Y_MIN);
9812
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009813 // run captured pointer tests - note that this is unscaled, so input listener events should be
9814 // identical to what the hardware sends (accounting for any
9815 // calibration).
9816 // FINGER 0 DOWN
Chris Ye364fdb52020-08-05 15:07:56 -07009817 processSlot(mapper, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009818 processId(mapper, 1);
9819 processPosition(mapper, 100 + RAW_X_MIN, 100 + RAW_Y_MIN);
9820 processKey(mapper, BTN_TOUCH, 1);
9821 processSync(mapper);
9822
9823 // expect coord[0] to contain initial location of touch 0
9824 NotifyMotionArgs args;
9825 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9826 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009827 ASSERT_EQ(1U, args.getPointerCount());
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009828 ASSERT_EQ(0, args.pointerProperties[0].id);
9829 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, args.source);
9830 ASSERT_NO_FATAL_FAILURE(
9831 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
9832
9833 // FINGER 1 DOWN
9834 processSlot(mapper, 1);
9835 processId(mapper, 2);
9836 processPosition(mapper, 560 + RAW_X_MIN, 154 + RAW_Y_MIN);
9837 processSync(mapper);
9838
9839 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
9840 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009841 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009842 ASSERT_EQ(2U, args.getPointerCount());
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009843 ASSERT_EQ(0, args.pointerProperties[0].id);
9844 ASSERT_EQ(1, args.pointerProperties[1].id);
9845 ASSERT_NO_FATAL_FAILURE(
9846 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
9847 ASSERT_NO_FATAL_FAILURE(
9848 assertPointerCoords(args.pointerCoords[1], 560, 154, 1, 0, 0, 0, 0, 0, 0, 0));
9849
9850 // FINGER 1 MOVE
9851 processPosition(mapper, 540 + RAW_X_MIN, 690 + RAW_Y_MIN);
9852 processSync(mapper);
9853
9854 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
9855 // from move
9856 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9857 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9858 ASSERT_NO_FATAL_FAILURE(
9859 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
9860 ASSERT_NO_FATAL_FAILURE(
9861 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
9862
9863 // FINGER 0 MOVE
9864 processSlot(mapper, 0);
9865 processPosition(mapper, 50 + RAW_X_MIN, 800 + RAW_Y_MIN);
9866 processSync(mapper);
9867
9868 // expect coord[0] to contain new touch 0 location, coord[1] to contain previous location
9869 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9870 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9871 ASSERT_NO_FATAL_FAILURE(
9872 assertPointerCoords(args.pointerCoords[0], 50, 800, 1, 0, 0, 0, 0, 0, 0, 0));
9873 ASSERT_NO_FATAL_FAILURE(
9874 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
9875
9876 // BUTTON DOWN
9877 processKey(mapper, BTN_LEFT, 1);
9878 processSync(mapper);
9879
9880 // touchinputmapper design sends a move before button press
9881 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9882 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9883 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9884 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
9885
9886 // BUTTON UP
9887 processKey(mapper, BTN_LEFT, 0);
9888 processSync(mapper);
9889
9890 // touchinputmapper design sends a move after button release
9891 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9892 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
9893 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9894 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9895
9896 // FINGER 0 UP
9897 processId(mapper, -1);
9898 processSync(mapper);
9899 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9900 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | 0x0000, args.action);
9901
9902 // FINGER 1 MOVE
9903 processSlot(mapper, 1);
9904 processPosition(mapper, 320 + RAW_X_MIN, 900 + RAW_Y_MIN);
9905 processSync(mapper);
9906
9907 // expect coord[0] to contain new location of touch 1, and properties[0].id to contain 1
9908 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9909 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009910 ASSERT_EQ(1U, args.getPointerCount());
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009911 ASSERT_EQ(1, args.pointerProperties[0].id);
9912 ASSERT_NO_FATAL_FAILURE(
9913 assertPointerCoords(args.pointerCoords[0], 320, 900, 1, 0, 0, 0, 0, 0, 0, 0));
9914
9915 // FINGER 1 UP
9916 processId(mapper, -1);
9917 processKey(mapper, BTN_TOUCH, 0);
9918 processSync(mapper);
9919 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9920 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
9921
Josep del Río2d8c79a2023-01-23 19:33:50 +00009922 // non captured touchpad should be a mouse source
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009923 mFakePolicy->setPointerCapture(false);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00009924 configureDevice(InputReaderConfiguration::Change::POINTER_CAPTURE);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009925 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Josep del Río2d8c79a2023-01-23 19:33:50 +00009926 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009927}
9928
9929TEST_F(MultiTouchInputMapperTest, Process_UnCapturedTouchpadPointer) {
9930 std::shared_ptr<FakePointerController> fakePointerController =
9931 std::make_shared<FakePointerController>();
9932 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
9933 fakePointerController->setPosition(0, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009934
9935 // prepare device and capture
Michael Wrighta9cf4192022-12-01 23:46:39 +00009936 prepareDisplay(ui::ROTATION_0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009937 prepareAxes(POSITION | ID | SLOT);
9938 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
9939 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009940 mFakePolicy->setPointerController(fakePointerController);
Arpit Singha8c236b2023-04-25 13:56:05 +00009941 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009942 // run uncaptured pointer tests - pushes out generic events
9943 // FINGER 0 DOWN
9944 processId(mapper, 3);
9945 processPosition(mapper, 100, 100);
9946 processKey(mapper, BTN_TOUCH, 1);
9947 processSync(mapper);
9948
9949 // start at (100,100), cursor should be at (0,0) * scale
9950 NotifyMotionArgs args;
9951 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9952 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
9953 ASSERT_NO_FATAL_FAILURE(
9954 assertPointerCoords(args.pointerCoords[0], 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
9955
9956 // FINGER 0 MOVE
9957 processPosition(mapper, 200, 200);
9958 processSync(mapper);
9959
9960 // compute scaling to help with touch position checking
9961 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
9962 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
9963 float scale =
9964 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
9965
9966 // translate from (100,100) -> (200,200), cursor should have changed to (100,100) * scale)
9967 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9968 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
9969 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 100 * scale, 100 * scale, 0,
9970 0, 0, 0, 0, 0, 0, 0));
LiZhihong758eb562022-11-03 15:28:29 +08009971
9972 // BUTTON DOWN
9973 processKey(mapper, BTN_LEFT, 1);
9974 processSync(mapper);
9975
9976 // touchinputmapper design sends a move before button press
9977 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9978 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
9979 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9980 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
9981
9982 // BUTTON UP
9983 processKey(mapper, BTN_LEFT, 0);
9984 processSync(mapper);
9985
9986 // touchinputmapper design sends a move after button release
9987 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9988 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
9989 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9990 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009991}
9992
9993TEST_F(MultiTouchInputMapperTest, WhenCapturedAndNotCaptured_GetSources) {
9994 std::shared_ptr<FakePointerController> fakePointerController =
9995 std::make_shared<FakePointerController>();
9996
Michael Wrighta9cf4192022-12-01 23:46:39 +00009997 prepareDisplay(ui::ROTATION_0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009998 prepareAxes(POSITION | ID | SLOT);
9999 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +000010000 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010001 mFakePolicy->setPointerCapture(false);
Arpit Singha8c236b2023-04-25 13:56:05 +000010002 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010003
Josep del Río2d8c79a2023-01-23 19:33:50 +000010004 // uncaptured touchpad should be a pointer device
10005 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010006
Josep del Río2d8c79a2023-01-23 19:33:50 +000010007 // captured touchpad should be a touchpad device
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010008 mFakePolicy->setPointerCapture(true);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +000010009 configureDevice(InputReaderConfiguration::Change::POINTER_CAPTURE);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010010 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
10011}
10012
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +000010013// --- BluetoothMultiTouchInputMapperTest ---
10014
10015class BluetoothMultiTouchInputMapperTest : public MultiTouchInputMapperTest {
10016protected:
10017 void SetUp() override {
10018 InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL, BUS_BLUETOOTH);
10019 }
10020};
10021
10022TEST_F(BluetoothMultiTouchInputMapperTest, TimestampSmoothening) {
10023 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010024 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +000010025 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +000010026 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +000010027
10028 nsecs_t kernelEventTime = ARBITRARY_TIME;
10029 nsecs_t expectedEventTime = ARBITRARY_TIME;
10030 // Touch down.
10031 processId(mapper, FIRST_TRACKING_ID);
10032 processPosition(mapper, 100, 200);
10033 processPressure(mapper, RAW_PRESSURE_MAX);
10034 processSync(mapper, ARBITRARY_TIME);
10035 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10036 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithEventTime(ARBITRARY_TIME))));
10037
10038 // Process several events that come in quick succession, according to their timestamps.
10039 for (int i = 0; i < 3; i++) {
10040 constexpr static nsecs_t delta = ms2ns(1);
10041 static_assert(delta < MIN_BLUETOOTH_TIMESTAMP_DELTA);
10042 kernelEventTime += delta;
10043 expectedEventTime += MIN_BLUETOOTH_TIMESTAMP_DELTA;
10044
10045 processPosition(mapper, 101 + i, 201 + i);
10046 processSync(mapper, kernelEventTime);
10047 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10048 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
10049 WithEventTime(expectedEventTime))));
10050 }
10051
10052 // Release the touch.
10053 processId(mapper, INVALID_TRACKING_ID);
10054 processPressure(mapper, RAW_PRESSURE_MIN);
10055 processSync(mapper, ARBITRARY_TIME + ms2ns(50));
10056 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10057 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
10058 WithEventTime(ARBITRARY_TIME + ms2ns(50)))));
10059}
10060
10061// --- MultiTouchPointerModeTest ---
10062
HQ Liue6983c72022-04-19 22:14:56 +000010063class MultiTouchPointerModeTest : public MultiTouchInputMapperTest {
10064protected:
10065 float mPointerMovementScale;
10066 float mPointerXZoomScale;
10067 void preparePointerMode(int xAxisResolution, int yAxisResolution) {
10068 addConfigurationProperty("touch.deviceType", "pointer");
10069 std::shared_ptr<FakePointerController> fakePointerController =
10070 std::make_shared<FakePointerController>();
10071 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
10072 fakePointerController->setPosition(0, 0);
Michael Wrighta9cf4192022-12-01 23:46:39 +000010073 prepareDisplay(ui::ROTATION_0);
HQ Liue6983c72022-04-19 22:14:56 +000010074
10075 prepareAxes(POSITION);
10076 prepareAbsoluteAxisResolution(xAxisResolution, yAxisResolution);
10077 // In order to enable swipe and freeform gesture in pointer mode, pointer capture
10078 // needs to be disabled, and the pointer gesture needs to be enabled.
10079 mFakePolicy->setPointerCapture(false);
10080 mFakePolicy->setPointerGestureEnabled(true);
10081 mFakePolicy->setPointerController(fakePointerController);
10082
10083 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
10084 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
10085 mPointerMovementScale =
10086 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
10087 mPointerXZoomScale =
10088 mFakePolicy->getPointerGestureZoomSpeedRatio() * displayDiagonal / rawDiagonal;
10089 }
10090
10091 void prepareAbsoluteAxisResolution(int xAxisResolution, int yAxisResolution) {
10092 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
10093 /*flat*/ 0,
10094 /*fuzz*/ 0, /*resolution*/ xAxisResolution);
10095 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
10096 /*flat*/ 0,
10097 /*fuzz*/ 0, /*resolution*/ yAxisResolution);
10098 }
10099};
10100
10101/**
10102 * Two fingers down on a pointer mode touch pad. The width
10103 * of the two finger is larger than 1/4 of the touch pack diagnal length. However, it
10104 * is smaller than the fixed min physical length 30mm. Two fingers' distance must
10105 * be greater than the both value to be freeform gesture, so that after two
10106 * fingers start to move downwards, the gesture should be swipe.
10107 */
10108TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthSwipe) {
10109 // The min freeform gesture width is 25units/mm x 30mm = 750
10110 // which is greater than fraction of the diagnal length of the touchpad (349).
10111 // Thus, MaxSwipWidth is 750.
Harry Cutts33476232023-01-30 19:57:29 +000010112 preparePointerMode(/*xResolution=*/25, /*yResolution=*/25);
Arpit Singha8c236b2023-04-25 13:56:05 +000010113 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
HQ Liue6983c72022-04-19 22:14:56 +000010114 NotifyMotionArgs motionArgs;
10115
10116 // Two fingers down at once.
10117 // The two fingers are 450 units apart, expects the current gesture to be PRESS
10118 // Pointer's initial position is used the [0,0] coordinate.
10119 int32_t x1 = 100, y1 = 125, x2 = 550, y2 = 125;
10120
10121 processId(mapper, FIRST_TRACKING_ID);
10122 processPosition(mapper, x1, y1);
10123 processMTSync(mapper);
10124 processId(mapper, SECOND_TRACKING_ID);
10125 processPosition(mapper, x2, y2);
10126 processMTSync(mapper);
10127 processSync(mapper);
10128
10129 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010130 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000010131 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010132 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010133 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010134 ASSERT_NO_FATAL_FAILURE(
10135 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
10136
10137 // It should be recognized as a SWIPE gesture when two fingers start to move down,
10138 // that there should be 1 pointer.
10139 int32_t movingDistance = 200;
10140 y1 += movingDistance;
10141 y2 += movingDistance;
10142
10143 processId(mapper, FIRST_TRACKING_ID);
10144 processPosition(mapper, x1, y1);
10145 processMTSync(mapper);
10146 processId(mapper, SECOND_TRACKING_ID);
10147 processPosition(mapper, x2, y2);
10148 processMTSync(mapper);
10149 processSync(mapper);
10150
10151 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010152 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000010153 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010154 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010155 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010156 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 0,
10157 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10158 0, 0, 0, 0));
10159}
10160
10161/**
10162 * Two fingers down on a pointer mode touch pad. The width of the two finger is larger
10163 * than the minimum freeform gesture width, 30mm. However, it is smaller than 1/4 of
10164 * the touch pack diagnal length. Two fingers' distance must be greater than the both
10165 * value to be freeform gesture, so that after two fingers start to move downwards,
10166 * the gesture should be swipe.
10167 */
10168TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthLowResolutionSwipe) {
10169 // The min freeform gesture width is 5units/mm x 30mm = 150
10170 // which is greater than fraction of the diagnal length of the touchpad (349).
10171 // Thus, MaxSwipWidth is the fraction of the diagnal length, 349.
Harry Cutts33476232023-01-30 19:57:29 +000010172 preparePointerMode(/*xResolution=*/5, /*yResolution=*/5);
Arpit Singha8c236b2023-04-25 13:56:05 +000010173 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
HQ Liue6983c72022-04-19 22:14:56 +000010174 NotifyMotionArgs motionArgs;
10175
10176 // Two fingers down at once.
10177 // The two fingers are 250 units apart, expects the current gesture to be PRESS
10178 // Pointer's initial position is used the [0,0] coordinate.
10179 int32_t x1 = 100, y1 = 125, x2 = 350, y2 = 125;
10180
10181 processId(mapper, FIRST_TRACKING_ID);
10182 processPosition(mapper, x1, y1);
10183 processMTSync(mapper);
10184 processId(mapper, SECOND_TRACKING_ID);
10185 processPosition(mapper, x2, y2);
10186 processMTSync(mapper);
10187 processSync(mapper);
10188
10189 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010190 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000010191 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010192 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010193 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010194 ASSERT_NO_FATAL_FAILURE(
10195 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
10196
10197 // It should be recognized as a SWIPE gesture when two fingers start to move down,
10198 // and there should be 1 pointer.
10199 int32_t movingDistance = 200;
10200 y1 += movingDistance;
10201 y2 += movingDistance;
10202
10203 processId(mapper, FIRST_TRACKING_ID);
10204 processPosition(mapper, x1, y1);
10205 processMTSync(mapper);
10206 processId(mapper, SECOND_TRACKING_ID);
10207 processPosition(mapper, x2, y2);
10208 processMTSync(mapper);
10209 processSync(mapper);
10210
10211 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010212 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000010213 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010214 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010215 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010216 // New coordinate is the scaled relative coordinate from the initial coordinate.
10217 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 0,
10218 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10219 0, 0, 0, 0));
10220}
10221
10222/**
10223 * Touch the touch pad with two fingers with a distance wider than the minimum freeform
10224 * gesture width and 1/4 of the diagnal length of the touchpad. Expect to receive
10225 * freeform gestures after two fingers start to move downwards.
10226 */
10227TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthFreeform) {
Harry Cutts33476232023-01-30 19:57:29 +000010228 preparePointerMode(/*xResolution=*/25, /*yResolution=*/25);
Arpit Singha8c236b2023-04-25 13:56:05 +000010229 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
HQ Liue6983c72022-04-19 22:14:56 +000010230
10231 NotifyMotionArgs motionArgs;
10232
10233 // Two fingers down at once. Wider than the max swipe width.
10234 // The gesture is expected to be PRESS, then transformed to FREEFORM
10235 int32_t x1 = 100, y1 = 125, x2 = 900, y2 = 125;
10236
10237 processId(mapper, FIRST_TRACKING_ID);
10238 processPosition(mapper, x1, y1);
10239 processMTSync(mapper);
10240 processId(mapper, SECOND_TRACKING_ID);
10241 processPosition(mapper, x2, y2);
10242 processMTSync(mapper);
10243 processSync(mapper);
10244
10245 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010246 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000010247 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010248 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010249 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010250 // One pointer for PRESS, and its coordinate is used as the origin for pointer coordinates.
10251 ASSERT_NO_FATAL_FAILURE(
10252 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
10253
10254 int32_t movingDistance = 200;
10255
10256 // Move two fingers down, expect a cancel event because gesture is changing to freeform,
10257 // then two down events for two pointers.
10258 y1 += movingDistance;
10259 y2 += movingDistance;
10260
10261 processId(mapper, FIRST_TRACKING_ID);
10262 processPosition(mapper, x1, y1);
10263 processMTSync(mapper);
10264 processId(mapper, SECOND_TRACKING_ID);
10265 processPosition(mapper, x2, y2);
10266 processMTSync(mapper);
10267 processSync(mapper);
10268
10269 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10270 // The previous PRESS gesture is cancelled, because it is transformed to freeform
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010271 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000010272 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
10273 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010274 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010275 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000010276 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10277 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010278 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010279 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010280 ASSERT_EQ(2U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000010281 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN, motionArgs.action & AMOTION_EVENT_ACTION_MASK);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010282 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010283 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010284 // Two pointers' scaled relative coordinates from their initial centroid.
10285 // Initial y coordinates are 0 as y1 and y2 have the same value.
10286 float cookedX1 = (x1 - x2) / 2 * mPointerXZoomScale;
10287 float cookedX2 = (x2 - x1) / 2 * mPointerXZoomScale;
10288 // When pointers move, the new coordinates equal to the initial coordinates plus
10289 // scaled moving distance.
10290 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], cookedX1,
10291 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10292 0, 0, 0, 0));
10293 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1], cookedX2,
10294 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10295 0, 0, 0, 0));
10296
10297 // Move two fingers down again, expect one MOVE motion event.
10298 y1 += movingDistance;
10299 y2 += movingDistance;
10300
10301 processId(mapper, FIRST_TRACKING_ID);
10302 processPosition(mapper, x1, y1);
10303 processMTSync(mapper);
10304 processId(mapper, SECOND_TRACKING_ID);
10305 processPosition(mapper, x2, y2);
10306 processMTSync(mapper);
10307 processSync(mapper);
10308
10309 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010310 ASSERT_EQ(2U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000010311 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010312 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010313 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010314 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], cookedX1,
10315 movingDistance * 2 * mPointerMovementScale, 1, 0, 0,
10316 0, 0, 0, 0, 0));
10317 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1], cookedX2,
10318 movingDistance * 2 * mPointerMovementScale, 1, 0, 0,
10319 0, 0, 0, 0, 0));
10320}
10321
Harry Cutts39b7ca22022-10-05 15:55:48 +000010322TEST_F(MultiTouchPointerModeTest, TwoFingerSwipeOffsets) {
Harry Cutts33476232023-01-30 19:57:29 +000010323 preparePointerMode(/*xResolution=*/25, /*yResolution=*/25);
Arpit Singha8c236b2023-04-25 13:56:05 +000010324 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Harry Cutts39b7ca22022-10-05 15:55:48 +000010325 NotifyMotionArgs motionArgs;
10326
10327 // Place two fingers down.
10328 int32_t x1 = 100, y1 = 125, x2 = 550, y2 = 125;
10329
10330 processId(mapper, FIRST_TRACKING_ID);
10331 processPosition(mapper, x1, y1);
10332 processMTSync(mapper);
10333 processId(mapper, SECOND_TRACKING_ID);
10334 processPosition(mapper, x2, y2);
10335 processMTSync(mapper);
10336 processSync(mapper);
10337
10338 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010339 ASSERT_EQ(1U, motionArgs.getPointerCount());
Harry Cutts39b7ca22022-10-05 15:55:48 +000010340 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10341 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
10342 ASSERT_EQ(0, motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_X_OFFSET));
10343 ASSERT_EQ(0, motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_Y_OFFSET));
10344
10345 // Move the two fingers down and to the left.
10346 int32_t movingDistance = 200;
10347 x1 -= movingDistance;
10348 y1 += movingDistance;
10349 x2 -= movingDistance;
10350 y2 += movingDistance;
10351
10352 processId(mapper, FIRST_TRACKING_ID);
10353 processPosition(mapper, x1, y1);
10354 processMTSync(mapper);
10355 processId(mapper, SECOND_TRACKING_ID);
10356 processPosition(mapper, x2, y2);
10357 processMTSync(mapper);
10358 processSync(mapper);
10359
10360 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010361 ASSERT_EQ(1U, motionArgs.getPointerCount());
Harry Cutts39b7ca22022-10-05 15:55:48 +000010362 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10363 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
10364 ASSERT_LT(motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_X_OFFSET), 0);
10365 ASSERT_GT(motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_Y_OFFSET), 0);
10366}
10367
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010368TEST_F(MultiTouchPointerModeTest, WhenViewportActiveStatusChanged_PointerGestureIsReset) {
Harry Cutts33476232023-01-30 19:57:29 +000010369 preparePointerMode(/*xResolution=*/25, /*yResolution=*/25);
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010370 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
Arpit Singha8c236b2023-04-25 13:56:05 +000010371 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010372 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
10373
10374 // Start a stylus gesture.
10375 processKey(mapper, BTN_TOOL_PEN, 1);
10376 processId(mapper, FIRST_TRACKING_ID);
10377 processPosition(mapper, 100, 200);
10378 processSync(mapper);
10379 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10380 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
10381 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010382 WithToolType(ToolType::STYLUS))));
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010383 // TODO(b/257078296): Pointer mode generates extra event.
10384 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10385 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
10386 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010387 WithToolType(ToolType::STYLUS))));
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010388 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10389
10390 // Make the viewport inactive. This will put the device in disabled mode, and the ongoing stylus
10391 // gesture should be disabled.
10392 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
10393 viewport->isActive = false;
10394 mFakePolicy->updateViewport(*viewport);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +000010395 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010396 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10397 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL),
10398 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010399 WithToolType(ToolType::STYLUS))));
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010400 // TODO(b/257078296): Pointer mode generates extra event.
10401 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10402 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL),
10403 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010404 WithToolType(ToolType::STYLUS))));
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010405 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10406}
10407
Arthur Hung6d5b4b22022-01-21 07:21:10 +000010408// --- JoystickInputMapperTest ---
10409
10410class JoystickInputMapperTest : public InputMapperTest {
10411protected:
10412 static const int32_t RAW_X_MIN;
10413 static const int32_t RAW_X_MAX;
10414 static const int32_t RAW_Y_MIN;
10415 static const int32_t RAW_Y_MAX;
10416
10417 void SetUp() override {
10418 InputMapperTest::SetUp(InputDeviceClass::JOYSTICK | InputDeviceClass::EXTERNAL);
10419 }
10420 void prepareAxes() {
10421 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
10422 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
10423 }
10424
10425 void processAxis(JoystickInputMapper& mapper, int32_t axis, int32_t value) {
10426 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, axis, value);
10427 }
10428
10429 void processSync(JoystickInputMapper& mapper) {
10430 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
10431 }
10432
Michael Wrighta9cf4192022-12-01 23:46:39 +000010433 void prepareVirtualDisplay(ui::Rotation orientation) {
Arthur Hung6d5b4b22022-01-21 07:21:10 +000010434 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH,
10435 VIRTUAL_DISPLAY_HEIGHT, orientation, VIRTUAL_DISPLAY_UNIQUE_ID,
10436 NO_PORT, ViewportType::VIRTUAL);
10437 }
10438};
10439
10440const int32_t JoystickInputMapperTest::RAW_X_MIN = -32767;
10441const int32_t JoystickInputMapperTest::RAW_X_MAX = 32767;
10442const int32_t JoystickInputMapperTest::RAW_Y_MIN = -32767;
10443const int32_t JoystickInputMapperTest::RAW_Y_MAX = 32767;
10444
10445TEST_F(JoystickInputMapperTest, Configure_AssignsDisplayUniqueId) {
10446 prepareAxes();
Arpit Singhae876352023-04-26 14:16:50 +000010447 JoystickInputMapper& mapper = constructAndAddMapper<JoystickInputMapper>();
Arthur Hung6d5b4b22022-01-21 07:21:10 +000010448
10449 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
10450
Michael Wrighta9cf4192022-12-01 23:46:39 +000010451 prepareVirtualDisplay(ui::ROTATION_0);
Arthur Hung6d5b4b22022-01-21 07:21:10 +000010452
10453 // Send an axis event
10454 processAxis(mapper, ABS_X, 100);
10455 processSync(mapper);
10456
10457 NotifyMotionArgs args;
10458 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10459 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
10460
10461 // Send another axis event
10462 processAxis(mapper, ABS_Y, 100);
10463 processSync(mapper);
10464
10465 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10466 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
10467}
10468
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010469// --- PeripheralControllerTest ---
Chris Yee2b1e5c2021-03-10 22:45:12 -080010470
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010471class PeripheralControllerTest : public testing::Test {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010472protected:
10473 static const char* DEVICE_NAME;
10474 static const char* DEVICE_LOCATION;
10475 static const int32_t DEVICE_ID;
10476 static const int32_t DEVICE_GENERATION;
10477 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010478 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Chris Yee2b1e5c2021-03-10 22:45:12 -080010479 static const int32_t EVENTHUB_ID;
10480
10481 std::shared_ptr<FakeEventHub> mFakeEventHub;
10482 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010483 std::unique_ptr<TestInputListener> mFakeListener;
Chris Yee2b1e5c2021-03-10 22:45:12 -080010484 std::unique_ptr<InstrumentedInputReader> mReader;
10485 std::shared_ptr<InputDevice> mDevice;
10486
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010487 virtual void SetUp(ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010488 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -070010489 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010490 mFakeListener = std::make_unique<TestInputListener>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010491 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010492 *mFakeListener);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010493 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
10494 }
10495
10496 void SetUp() override { SetUp(DEVICE_CLASSES); }
10497
10498 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010499 mFakeListener.reset();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010500 mFakePolicy.clear();
10501 }
10502
Chris Yee2b1e5c2021-03-10 22:45:12 -080010503 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
10504 const std::string& location, int32_t eventHubId,
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010505 ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010506 InputDeviceIdentifier identifier;
10507 identifier.name = name;
10508 identifier.location = location;
10509 std::shared_ptr<InputDevice> device =
10510 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
10511 identifier);
10512 mReader->pushNextDevice(device);
10513 mFakeEventHub->addDevice(eventHubId, name, classes);
10514 mReader->loopOnce();
10515 return device;
10516 }
10517
10518 template <class T, typename... Args>
10519 T& addControllerAndConfigure(Args... args) {
10520 T& controller = mDevice->addController<T>(EVENTHUB_ID, args...);
10521
10522 return controller;
10523 }
10524};
10525
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010526const char* PeripheralControllerTest::DEVICE_NAME = "device";
10527const char* PeripheralControllerTest::DEVICE_LOCATION = "BLUETOOTH";
10528const int32_t PeripheralControllerTest::DEVICE_ID = END_RESERVED_ID + 1000;
10529const int32_t PeripheralControllerTest::DEVICE_GENERATION = 2;
10530const int32_t PeripheralControllerTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010531const ftl::Flags<InputDeviceClass> PeripheralControllerTest::DEVICE_CLASSES =
10532 ftl::Flags<InputDeviceClass>(0); // not needed for current tests
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010533const int32_t PeripheralControllerTest::EVENTHUB_ID = 1;
Chris Yee2b1e5c2021-03-10 22:45:12 -080010534
10535// --- BatteryControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010536class BatteryControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010537protected:
10538 void SetUp() override {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010539 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::BATTERY);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010540 }
10541};
10542
10543TEST_F(BatteryControllerTest, GetBatteryCapacity) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010544 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010545
Harry Cuttsa5b71292022-11-28 12:56:17 +000010546 ASSERT_TRUE(controller.getBatteryCapacity(FakeEventHub::DEFAULT_BATTERY));
10547 ASSERT_EQ(controller.getBatteryCapacity(FakeEventHub::DEFAULT_BATTERY).value_or(-1),
10548 FakeEventHub::BATTERY_CAPACITY);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010549}
10550
10551TEST_F(BatteryControllerTest, GetBatteryStatus) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010552 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010553
Harry Cuttsa5b71292022-11-28 12:56:17 +000010554 ASSERT_TRUE(controller.getBatteryStatus(FakeEventHub::DEFAULT_BATTERY));
10555 ASSERT_EQ(controller.getBatteryStatus(FakeEventHub::DEFAULT_BATTERY).value_or(-1),
10556 FakeEventHub::BATTERY_STATUS);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010557}
10558
10559// --- LightControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010560class LightControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010561protected:
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010562 void SetUp() override {
10563 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::LIGHT);
10564 }
Chris Yee2b1e5c2021-03-10 22:45:12 -080010565};
10566
Chris Ye85758332021-05-16 23:05:17 -070010567TEST_F(LightControllerTest, MonoLight) {
10568 RawLightInfo infoMono = {.id = 1,
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010569 .name = "mono_light",
Chris Ye85758332021-05-16 23:05:17 -070010570 .maxBrightness = 255,
10571 .flags = InputLightClass::BRIGHTNESS,
10572 .path = ""};
10573 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010574
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010575 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010576 InputDeviceInfo info;
10577 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010578 std::vector<InputDeviceLightInfo> lights = info.getLights();
10579 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010580 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
10581 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10582
10583 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
10584 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
10585}
10586
10587TEST_F(LightControllerTest, MonoKeyboardBacklight) {
10588 RawLightInfo infoMono = {.id = 1,
10589 .name = "mono_keyboard_backlight",
10590 .maxBrightness = 255,
10591 .flags = InputLightClass::BRIGHTNESS |
10592 InputLightClass::KEYBOARD_BACKLIGHT,
10593 .path = ""};
10594 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
10595
10596 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10597 InputDeviceInfo info;
10598 controller.populateDeviceInfo(&info);
10599 std::vector<InputDeviceLightInfo> lights = info.getLights();
10600 ASSERT_EQ(1U, lights.size());
10601 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
10602 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010603
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010604 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
10605 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010606}
10607
Vaibhav Devmurari16c24192023-05-04 15:20:12 +000010608TEST_F(LightControllerTest, Ignore_MonoLight_WithPreferredBacklightLevels) {
10609 RawLightInfo infoMono = {.id = 1,
10610 .name = "mono_light",
10611 .maxBrightness = 255,
10612 .flags = InputLightClass::BRIGHTNESS,
10613 .path = ""};
10614 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
10615 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "keyboard.backlight.brightnessLevels",
10616 "0,100,200");
10617
10618 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10619 std::list<NotifyArgs> unused =
10620 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
10621 /*changes=*/{});
10622
10623 InputDeviceInfo info;
10624 controller.populateDeviceInfo(&info);
10625 std::vector<InputDeviceLightInfo> lights = info.getLights();
10626 ASSERT_EQ(1U, lights.size());
10627 ASSERT_EQ(0U, lights[0].preferredBrightnessLevels.size());
10628}
10629
10630TEST_F(LightControllerTest, KeyboardBacklight_WithNoPreferredBacklightLevels) {
10631 RawLightInfo infoMono = {.id = 1,
10632 .name = "mono_keyboard_backlight",
10633 .maxBrightness = 255,
10634 .flags = InputLightClass::BRIGHTNESS |
10635 InputLightClass::KEYBOARD_BACKLIGHT,
10636 .path = ""};
10637 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
10638
10639 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10640 std::list<NotifyArgs> unused =
10641 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
10642 /*changes=*/{});
10643
10644 InputDeviceInfo info;
10645 controller.populateDeviceInfo(&info);
10646 std::vector<InputDeviceLightInfo> lights = info.getLights();
10647 ASSERT_EQ(1U, lights.size());
10648 ASSERT_EQ(0U, lights[0].preferredBrightnessLevels.size());
10649}
10650
10651TEST_F(LightControllerTest, KeyboardBacklight_WithPreferredBacklightLevels) {
10652 RawLightInfo infoMono = {.id = 1,
10653 .name = "mono_keyboard_backlight",
10654 .maxBrightness = 255,
10655 .flags = InputLightClass::BRIGHTNESS |
10656 InputLightClass::KEYBOARD_BACKLIGHT,
10657 .path = ""};
10658 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
10659 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "keyboard.backlight.brightnessLevels",
10660 "0,100,200");
10661
10662 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10663 std::list<NotifyArgs> unused =
10664 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
10665 /*changes=*/{});
10666
10667 InputDeviceInfo info;
10668 controller.populateDeviceInfo(&info);
10669 std::vector<InputDeviceLightInfo> lights = info.getLights();
10670 ASSERT_EQ(1U, lights.size());
10671 ASSERT_EQ(3U, lights[0].preferredBrightnessLevels.size());
10672 std::set<BrightnessLevel>::iterator it = lights[0].preferredBrightnessLevels.begin();
10673 ASSERT_EQ(BrightnessLevel(0), *it);
10674 std::advance(it, 1);
10675 ASSERT_EQ(BrightnessLevel(100), *it);
10676 std::advance(it, 1);
10677 ASSERT_EQ(BrightnessLevel(200), *it);
10678}
10679
10680TEST_F(LightControllerTest, KeyboardBacklight_WithWrongPreferredBacklightLevels) {
10681 RawLightInfo infoMono = {.id = 1,
10682 .name = "mono_keyboard_backlight",
10683 .maxBrightness = 255,
10684 .flags = InputLightClass::BRIGHTNESS |
10685 InputLightClass::KEYBOARD_BACKLIGHT,
10686 .path = ""};
10687 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
10688 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "keyboard.backlight.brightnessLevels",
10689 "0,100,200,300,400,500");
10690
10691 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10692 std::list<NotifyArgs> unused =
10693 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
10694 /*changes=*/{});
10695
10696 InputDeviceInfo info;
10697 controller.populateDeviceInfo(&info);
10698 std::vector<InputDeviceLightInfo> lights = info.getLights();
10699 ASSERT_EQ(1U, lights.size());
10700 ASSERT_EQ(0U, lights[0].preferredBrightnessLevels.size());
10701}
10702
Chris Yee2b1e5c2021-03-10 22:45:12 -080010703TEST_F(LightControllerTest, RGBLight) {
10704 RawLightInfo infoRed = {.id = 1,
10705 .name = "red",
10706 .maxBrightness = 255,
10707 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
10708 .path = ""};
10709 RawLightInfo infoGreen = {.id = 2,
10710 .name = "green",
10711 .maxBrightness = 255,
10712 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
10713 .path = ""};
10714 RawLightInfo infoBlue = {.id = 3,
10715 .name = "blue",
10716 .maxBrightness = 255,
10717 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
10718 .path = ""};
10719 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
10720 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
10721 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
10722
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010723 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010724 InputDeviceInfo info;
10725 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010726 std::vector<InputDeviceLightInfo> lights = info.getLights();
10727 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010728 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
10729 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10730 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
10731
10732 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10733 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
10734}
10735
10736TEST_F(LightControllerTest, CorrectRGBKeyboardBacklight) {
10737 RawLightInfo infoRed = {.id = 1,
10738 .name = "red_keyboard_backlight",
10739 .maxBrightness = 255,
10740 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED |
10741 InputLightClass::KEYBOARD_BACKLIGHT,
10742 .path = ""};
10743 RawLightInfo infoGreen = {.id = 2,
10744 .name = "green_keyboard_backlight",
10745 .maxBrightness = 255,
10746 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN |
10747 InputLightClass::KEYBOARD_BACKLIGHT,
10748 .path = ""};
10749 RawLightInfo infoBlue = {.id = 3,
10750 .name = "blue_keyboard_backlight",
10751 .maxBrightness = 255,
10752 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE |
10753 InputLightClass::KEYBOARD_BACKLIGHT,
10754 .path = ""};
10755 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
10756 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
10757 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
10758
10759 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10760 InputDeviceInfo info;
10761 controller.populateDeviceInfo(&info);
10762 std::vector<InputDeviceLightInfo> lights = info.getLights();
10763 ASSERT_EQ(1U, lights.size());
10764 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
10765 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10766 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
10767
10768 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10769 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
10770}
10771
10772TEST_F(LightControllerTest, IncorrectRGBKeyboardBacklight) {
10773 RawLightInfo infoRed = {.id = 1,
10774 .name = "red",
10775 .maxBrightness = 255,
10776 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
10777 .path = ""};
10778 RawLightInfo infoGreen = {.id = 2,
10779 .name = "green",
10780 .maxBrightness = 255,
10781 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
10782 .path = ""};
10783 RawLightInfo infoBlue = {.id = 3,
10784 .name = "blue",
10785 .maxBrightness = 255,
10786 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
10787 .path = ""};
10788 RawLightInfo infoGlobal = {.id = 3,
10789 .name = "global_keyboard_backlight",
10790 .maxBrightness = 255,
10791 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GLOBAL |
10792 InputLightClass::KEYBOARD_BACKLIGHT,
10793 .path = ""};
10794 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
10795 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
10796 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
10797 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoGlobal));
10798
10799 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10800 InputDeviceInfo info;
10801 controller.populateDeviceInfo(&info);
10802 std::vector<InputDeviceLightInfo> lights = info.getLights();
10803 ASSERT_EQ(1U, lights.size());
10804 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
10805 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10806 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010807
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010808 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10809 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010810}
10811
10812TEST_F(LightControllerTest, MultiColorRGBLight) {
10813 RawLightInfo infoColor = {.id = 1,
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010814 .name = "multi_color",
Chris Yee2b1e5c2021-03-10 22:45:12 -080010815 .maxBrightness = 255,
10816 .flags = InputLightClass::BRIGHTNESS |
10817 InputLightClass::MULTI_INTENSITY |
10818 InputLightClass::MULTI_INDEX,
10819 .path = ""};
10820
10821 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
10822
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010823 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010824 InputDeviceInfo info;
10825 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010826 std::vector<InputDeviceLightInfo> lights = info.getLights();
10827 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010828 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
10829 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10830 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
10831
10832 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10833 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
10834}
10835
10836TEST_F(LightControllerTest, MultiColorRGBKeyboardBacklight) {
10837 RawLightInfo infoColor = {.id = 1,
10838 .name = "multi_color_keyboard_backlight",
10839 .maxBrightness = 255,
10840 .flags = InputLightClass::BRIGHTNESS |
10841 InputLightClass::MULTI_INTENSITY |
10842 InputLightClass::MULTI_INDEX |
10843 InputLightClass::KEYBOARD_BACKLIGHT,
10844 .path = ""};
10845
10846 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
10847
10848 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10849 InputDeviceInfo info;
10850 controller.populateDeviceInfo(&info);
10851 std::vector<InputDeviceLightInfo> lights = info.getLights();
10852 ASSERT_EQ(1U, lights.size());
10853 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
10854 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10855 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010856
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010857 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10858 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010859}
10860
10861TEST_F(LightControllerTest, PlayerIdLight) {
10862 RawLightInfo info1 = {.id = 1,
10863 .name = "player1",
10864 .maxBrightness = 255,
10865 .flags = InputLightClass::BRIGHTNESS,
10866 .path = ""};
10867 RawLightInfo info2 = {.id = 2,
10868 .name = "player2",
10869 .maxBrightness = 255,
10870 .flags = InputLightClass::BRIGHTNESS,
10871 .path = ""};
10872 RawLightInfo info3 = {.id = 3,
10873 .name = "player3",
10874 .maxBrightness = 255,
10875 .flags = InputLightClass::BRIGHTNESS,
10876 .path = ""};
10877 RawLightInfo info4 = {.id = 4,
10878 .name = "player4",
10879 .maxBrightness = 255,
10880 .flags = InputLightClass::BRIGHTNESS,
10881 .path = ""};
10882 mFakeEventHub->addRawLightInfo(info1.id, std::move(info1));
10883 mFakeEventHub->addRawLightInfo(info2.id, std::move(info2));
10884 mFakeEventHub->addRawLightInfo(info3.id, std::move(info3));
10885 mFakeEventHub->addRawLightInfo(info4.id, std::move(info4));
10886
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010887 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010888 InputDeviceInfo info;
10889 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010890 std::vector<InputDeviceLightInfo> lights = info.getLights();
10891 ASSERT_EQ(1U, lights.size());
10892 ASSERT_EQ(InputDeviceLightType::PLAYER_ID, lights[0].type);
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010893 ASSERT_FALSE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10894 ASSERT_FALSE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010895
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010896 ASSERT_FALSE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10897 ASSERT_TRUE(controller.setLightPlayerId(lights[0].id, LIGHT_PLAYER_ID));
10898 ASSERT_EQ(controller.getLightPlayerId(lights[0].id).value_or(-1), LIGHT_PLAYER_ID);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010899}
10900
Michael Wrightd02c5b62014-02-10 15:10:22 -080010901} // namespace android