blob: 8c39ce9101394b077d5028779f3a22a5dc26d405 [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 Wubshite03e8b12023-06-27 16:23:12 -07002709TEST_F(InputDeviceTest, WakeDevice_AddsWakeFlagToProcessNotifyArgs) {
2710 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "device.wake", "1");
2711 FakeInputMapper& mapper =
2712 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2713 AINPUT_SOURCE_KEYBOARD);
2714 NotifyMotionArgs args1;
2715 NotifySwitchArgs args2;
2716 NotifyKeyArgs args3;
2717 mapper.setProcessResult({args1, args2, args3});
2718
2719 InputReaderConfiguration config;
2720 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, config, /*changes=*/{});
2721
2722 RawEvent event;
2723 event.deviceId = EVENTHUB_ID;
2724 std::list<NotifyArgs> notifyArgs = mDevice->process(&event, 1);
2725
2726 for (auto& arg : notifyArgs) {
2727 if (const auto notifyMotionArgs = std::get_if<NotifyMotionArgs>(&arg)) {
2728 ASSERT_EQ(POLICY_FLAG_WAKE, notifyMotionArgs->policyFlags);
2729 } else if (const auto notifySwitchArgs = std::get_if<NotifySwitchArgs>(&arg)) {
2730 ASSERT_EQ(POLICY_FLAG_WAKE, notifySwitchArgs->policyFlags);
2731 } else if (const auto notifyKeyArgs = std::get_if<NotifyKeyArgs>(&arg)) {
2732 ASSERT_EQ(POLICY_FLAG_WAKE, notifyKeyArgs->policyFlags);
2733 }
2734 }
2735}
2736
2737TEST_F(InputDeviceTest, NotWakeDevice_DoesNotAddWakeFlagToProcessNotifyArgs) {
2738 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "device.wake", "0");
2739 FakeInputMapper& mapper =
2740 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2741 AINPUT_SOURCE_KEYBOARD);
2742 NotifyMotionArgs args;
2743 mapper.setProcessResult({args});
2744
2745 InputReaderConfiguration config;
2746 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, config, /*changes=*/{});
2747
2748 RawEvent event;
2749 event.deviceId = EVENTHUB_ID;
2750 std::list<NotifyArgs> notifyArgs = mDevice->process(&event, 1);
2751
2752 // POLICY_FLAG_WAKE is not added to the NotifyArgs.
2753 ASSERT_EQ(0u, std::get<NotifyMotionArgs>(notifyArgs.front()).policyFlags);
2754}
2755
2756TEST_F(InputDeviceTest, NotWakeDevice_DoesNotRemoveExistingWakeFlagFromProcessNotifyArgs) {
2757 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "device.wake", "0");
2758 FakeInputMapper& mapper =
2759 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2760 AINPUT_SOURCE_KEYBOARD);
2761 NotifyMotionArgs args;
2762 args.policyFlags = POLICY_FLAG_WAKE;
2763 mapper.setProcessResult({args});
2764
2765 InputReaderConfiguration config;
2766 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, config, /*changes=*/{});
2767
2768 RawEvent event;
2769 event.deviceId = EVENTHUB_ID;
2770 std::list<NotifyArgs> notifyArgs = mDevice->process(&event, 1);
2771
2772 // The POLICY_FLAG_WAKE is preserved, despite the device being a non-wake device.
2773 ASSERT_EQ(POLICY_FLAG_WAKE, std::get<NotifyMotionArgs>(notifyArgs.front()).policyFlags);
2774}
2775
Arthur Hung2c9a3342019-07-23 14:18:59 +08002776// A single input device is associated with a specific display. Check that:
2777// 1. Device is disabled if the viewport corresponding to the associated display is not found
Arpit Singh48189772023-05-30 14:12:49 +00002778// 2. Device is disabled when configure API is called
Arthur Hung2c9a3342019-07-23 14:18:59 +08002779TEST_F(InputDeviceTest, Configure_AssignsDisplayPort) {
Arpit Singh8e6fb252023-04-06 11:49:17 +00002780 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2781 AINPUT_SOURCE_TOUCHSCREEN);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002782
2783 // First Configuration.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002784 std::list<NotifyArgs> unused =
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002785 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2786 /*changes=*/{});
Arthur Hung2c9a3342019-07-23 14:18:59 +08002787
2788 // Device should be enabled by default.
2789 ASSERT_TRUE(mDevice->isEnabled());
2790
2791 // Prepare associated info.
2792 constexpr uint8_t hdmi = 1;
2793 const std::string UNIQUE_ID = "local:1";
2794
2795 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002796 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002797 InputReaderConfiguration::Change::DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002798 // Device should be disabled because it is associated with a specific display via
2799 // input port <-> display port association, but the corresponding display is not found
2800 ASSERT_FALSE(mDevice->isEnabled());
2801
2802 // Prepare displays.
2803 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +00002804 ui::ROTATION_0, /*isActive=*/true, UNIQUE_ID, hdmi,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002805 ViewportType::INTERNAL);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002806 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002807 InputReaderConfiguration::Change::DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002808 ASSERT_TRUE(mDevice->isEnabled());
2809
2810 // Device should be disabled after set disable.
2811 mFakePolicy->addDisabledDevice(mDevice->getId());
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002812 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002813 InputReaderConfiguration::Change::ENABLED_STATE);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002814 ASSERT_FALSE(mDevice->isEnabled());
2815
2816 // Device should still be disabled even found the associated display.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002817 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002818 InputReaderConfiguration::Change::DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002819 ASSERT_FALSE(mDevice->isEnabled());
2820}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002821
Christine Franks1ba71cc2021-04-07 14:37:42 -07002822TEST_F(InputDeviceTest, Configure_AssignsDisplayUniqueId) {
2823 // Device should be enabled by default.
2824 mFakePolicy->clearViewports();
Arpit Singh8e6fb252023-04-06 11:49:17 +00002825 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2826 AINPUT_SOURCE_KEYBOARD);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002827 std::list<NotifyArgs> unused =
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002828 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2829 /*changes=*/{});
Christine Franks1ba71cc2021-04-07 14:37:42 -07002830 ASSERT_TRUE(mDevice->isEnabled());
2831
2832 // Device should be disabled because it is associated with a specific display, but the
2833 // corresponding display is not found.
Christine Franks2a2293c2022-01-18 11:51:16 -08002834 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002835 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002836 InputReaderConfiguration::Change::DISPLAY_INFO);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002837 ASSERT_FALSE(mDevice->isEnabled());
2838
2839 // Device should be enabled when a display is found.
2840 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +00002841 ui::ROTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
Christine Franks1ba71cc2021-04-07 14:37:42 -07002842 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002843 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002844 InputReaderConfiguration::Change::DISPLAY_INFO);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002845 ASSERT_TRUE(mDevice->isEnabled());
2846
2847 // Device should be disabled after set disable.
2848 mFakePolicy->addDisabledDevice(mDevice->getId());
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002849 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002850 InputReaderConfiguration::Change::ENABLED_STATE);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002851 ASSERT_FALSE(mDevice->isEnabled());
2852
2853 // Device should still be disabled even found the associated display.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002854 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002855 InputReaderConfiguration::Change::DISPLAY_INFO);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002856 ASSERT_FALSE(mDevice->isEnabled());
2857}
2858
Christine Franks2a2293c2022-01-18 11:51:16 -08002859TEST_F(InputDeviceTest, Configure_UniqueId_CorrectlyMatches) {
2860 mFakePolicy->clearViewports();
Arpit Singh8e6fb252023-04-06 11:49:17 +00002861 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2862 AINPUT_SOURCE_KEYBOARD);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002863 std::list<NotifyArgs> unused =
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002864 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2865 /*changes=*/{});
Christine Franks2a2293c2022-01-18 11:51:16 -08002866
Christine Franks2a2293c2022-01-18 11:51:16 -08002867 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
2868 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +00002869 ui::ROTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
Christine Franks2a2293c2022-01-18 11:51:16 -08002870 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002871 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002872 InputReaderConfiguration::Change::DISPLAY_INFO);
Christine Franks2a2293c2022-01-18 11:51:16 -08002873 ASSERT_EQ(DISPLAY_UNIQUE_ID, mDevice->getAssociatedDisplayUniqueId());
2874}
2875
Siarhei Vishniakou30feb8c2022-09-28 10:48:29 -07002876/**
2877 * This test reproduces a crash caused by a dangling reference that remains after device is added
2878 * and removed. The reference is accessed in InputDevice::dump(..);
2879 */
2880TEST_F(InputDeviceTest, DumpDoesNotCrash) {
2881 constexpr int32_t TEST_EVENTHUB_ID = 10;
2882 mFakeEventHub->addDevice(TEST_EVENTHUB_ID, "Test EventHub device", InputDeviceClass::BATTERY);
2883
Harry Cutts33476232023-01-30 19:57:29 +00002884 InputDevice device(mReader->getContext(), /*id=*/1, /*generation=*/2, /*identifier=*/{});
Arpit Singh82f29a12023-06-13 15:05:53 +00002885 auto _ = device.addEventHubDevice(ARBITRARY_TIME, TEST_EVENTHUB_ID,
2886 mFakePolicy->getReaderConfiguration());
Siarhei Vishniakou30feb8c2022-09-28 10:48:29 -07002887 device.removeEventHubDevice(TEST_EVENTHUB_ID);
2888 std::string dumpStr, eventHubDevStr;
2889 device.dump(dumpStr, eventHubDevStr);
2890}
2891
Prabir Pradhanb54ffb22022-10-27 18:03:34 +00002892TEST_F(InputDeviceTest, GetBluetoothAddress) {
2893 const auto& address = mReader->getBluetoothAddress(DEVICE_ID);
2894 ASSERT_TRUE(address);
2895 ASSERT_EQ(DEVICE_BLUETOOTH_ADDRESS, *address);
2896}
2897
Prabir Pradhanf8d9e442023-12-06 22:06:13 +00002898TEST_F(InputDeviceTest, KernelBufferOverflowResetsMappers) {
2899 mFakePolicy->clearViewports();
2900 FakeInputMapper& mapper =
2901 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2902 AINPUT_SOURCE_KEYBOARD);
2903 std::list<NotifyArgs> unused =
2904 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2905 /*changes=*/{});
2906
2907 mapper.assertConfigureWasCalled();
2908 mapper.assertResetWasNotCalled();
2909
Siarhei Vishniakou1ff00cc2023-12-13 16:12:13 -08002910 RawEvent event{.when = ARBITRARY_TIME,
Prabir Pradhanf8d9e442023-12-06 22:06:13 +00002911 .readTime = ARBITRARY_TIME,
Siarhei Vishniakou1ff00cc2023-12-13 16:12:13 -08002912 .deviceId = EVENTHUB_ID,
Prabir Pradhanf8d9e442023-12-06 22:06:13 +00002913 .type = EV_SYN,
2914 .code = SYN_REPORT,
2915 .value = 0};
2916
2917 // Events are processed normally.
2918 unused = mDevice->process(&event, /*count=*/1);
2919 mapper.assertProcessWasCalled();
2920
2921 // Simulate a kernel buffer overflow, which generates a SYN_DROPPED event.
2922 // This should reset the mapper.
2923 event.type = EV_SYN;
2924 event.code = SYN_DROPPED;
2925 event.value = 0;
2926 unused = mDevice->process(&event, /*count=*/1);
2927 mapper.assertProcessWasNotCalled();
2928 mapper.assertResetWasCalled();
2929
2930 // All events until the next SYN_REPORT should be dropped.
2931 event.type = EV_KEY;
2932 event.code = KEY_A;
2933 event.value = 1;
2934 unused = mDevice->process(&event, /*count=*/1);
2935 mapper.assertProcessWasNotCalled();
2936
2937 // We get the SYN_REPORT event now, which is not forwarded to mappers.
2938 event.type = EV_SYN;
2939 event.code = SYN_REPORT;
2940 event.value = 0;
2941 unused = mDevice->process(&event, /*count=*/1);
2942 mapper.assertProcessWasNotCalled();
2943
2944 // The mapper receives events normally now.
2945 event.type = EV_KEY;
2946 event.code = KEY_B;
2947 event.value = 1;
2948 unused = mDevice->process(&event, /*count=*/1);
2949 mapper.assertProcessWasCalled();
2950}
2951
Michael Wrightd02c5b62014-02-10 15:10:22 -08002952// --- SwitchInputMapperTest ---
2953
2954class SwitchInputMapperTest : public InputMapperTest {
2955protected:
2956};
2957
2958TEST_F(SwitchInputMapperTest, GetSources) {
Arpit Singhdf992eb2023-04-26 16:12:10 +00002959 SwitchInputMapper& mapper = constructAndAddMapper<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002960
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002961 ASSERT_EQ(uint32_t(AINPUT_SOURCE_SWITCH), mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002962}
2963
2964TEST_F(SwitchInputMapperTest, GetSwitchState) {
Arpit Singhdf992eb2023-04-26 16:12:10 +00002965 SwitchInputMapper& mapper = constructAndAddMapper<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002966
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002967 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002968 ASSERT_EQ(1, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002969
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002970 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002971 ASSERT_EQ(0, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002972}
2973
2974TEST_F(SwitchInputMapperTest, Process) {
Arpit Singhdf992eb2023-04-26 16:12:10 +00002975 SwitchInputMapper& mapper = constructAndAddMapper<SwitchInputMapper>();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002976 std::list<NotifyArgs> out;
2977 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_LID, 1);
2978 ASSERT_TRUE(out.empty());
2979 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_JACK_PHYSICAL_INSERT, 1);
2980 ASSERT_TRUE(out.empty());
2981 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_HEADPHONE_INSERT, 0);
2982 ASSERT_TRUE(out.empty());
2983 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002984
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002985 ASSERT_EQ(1u, out.size());
2986 const NotifySwitchArgs& args = std::get<NotifySwitchArgs>(*out.begin());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002987 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
Dan Albert1bd2fc02016-02-02 15:11:57 -08002988 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT), args.switchValues);
2989 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT) | (1 << SW_HEADPHONE_INSERT),
Michael Wrightd02c5b62014-02-10 15:10:22 -08002990 args.switchMask);
2991 ASSERT_EQ(uint32_t(0), args.policyFlags);
2992}
2993
Chris Ye87143712020-11-10 05:05:58 +00002994// --- VibratorInputMapperTest ---
2995class VibratorInputMapperTest : public InputMapperTest {
2996protected:
2997 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::VIBRATOR); }
2998};
2999
3000TEST_F(VibratorInputMapperTest, GetSources) {
Arpit Singh0f26b302023-04-26 16:23:13 +00003001 VibratorInputMapper& mapper = constructAndAddMapper<VibratorInputMapper>();
Chris Ye87143712020-11-10 05:05:58 +00003002
3003 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mapper.getSources());
3004}
3005
3006TEST_F(VibratorInputMapperTest, GetVibratorIds) {
Arpit Singh0f26b302023-04-26 16:23:13 +00003007 VibratorInputMapper& mapper = constructAndAddMapper<VibratorInputMapper>();
Chris Ye87143712020-11-10 05:05:58 +00003008
3009 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
3010}
3011
3012TEST_F(VibratorInputMapperTest, Vibrate) {
3013 constexpr uint8_t DEFAULT_AMPLITUDE = 192;
Chris Yefb552902021-02-03 17:18:37 -08003014 constexpr int32_t VIBRATION_TOKEN = 100;
Arpit Singh0f26b302023-04-26 16:23:13 +00003015 VibratorInputMapper& mapper = constructAndAddMapper<VibratorInputMapper>();
Chris Ye87143712020-11-10 05:05:58 +00003016
3017 VibrationElement pattern(2);
3018 VibrationSequence sequence(2);
3019 pattern.duration = std::chrono::milliseconds(200);
Harry Cutts33476232023-01-30 19:57:29 +00003020 pattern.channels = {{/*vibratorId=*/0, DEFAULT_AMPLITUDE / 2},
3021 {/*vibratorId=*/1, DEFAULT_AMPLITUDE}};
Chris Ye87143712020-11-10 05:05:58 +00003022 sequence.addElement(pattern);
3023 pattern.duration = std::chrono::milliseconds(500);
Harry Cutts33476232023-01-30 19:57:29 +00003024 pattern.channels = {{/*vibratorId=*/0, DEFAULT_AMPLITUDE / 4},
3025 {/*vibratorId=*/1, DEFAULT_AMPLITUDE}};
Chris Ye87143712020-11-10 05:05:58 +00003026 sequence.addElement(pattern);
3027
3028 std::vector<int64_t> timings = {0, 1};
3029 std::vector<uint8_t> amplitudes = {DEFAULT_AMPLITUDE, DEFAULT_AMPLITUDE / 2};
3030
3031 ASSERT_FALSE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08003032 // Start vibrating
Harry Cutts33476232023-01-30 19:57:29 +00003033 std::list<NotifyArgs> out = mapper.vibrate(sequence, /*repeat=*/-1, VIBRATION_TOKEN);
Chris Ye87143712020-11-10 05:05:58 +00003034 ASSERT_TRUE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08003035 // Verify vibrator state listener was notified.
3036 mReader->loopOnce();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003037 ASSERT_EQ(1u, out.size());
3038 const NotifyVibratorStateArgs& vibrateArgs = std::get<NotifyVibratorStateArgs>(*out.begin());
3039 ASSERT_EQ(DEVICE_ID, vibrateArgs.deviceId);
3040 ASSERT_TRUE(vibrateArgs.isOn);
Chris Yefb552902021-02-03 17:18:37 -08003041 // Stop vibrating
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003042 out = mapper.cancelVibrate(VIBRATION_TOKEN);
Chris Yefb552902021-02-03 17:18:37 -08003043 ASSERT_FALSE(mapper.isVibrating());
3044 // Verify vibrator state listener was notified.
3045 mReader->loopOnce();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003046 ASSERT_EQ(1u, out.size());
3047 const NotifyVibratorStateArgs& cancelArgs = std::get<NotifyVibratorStateArgs>(*out.begin());
3048 ASSERT_EQ(DEVICE_ID, cancelArgs.deviceId);
3049 ASSERT_FALSE(cancelArgs.isOn);
Chris Ye87143712020-11-10 05:05:58 +00003050}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003051
Chris Yef59a2f42020-10-16 12:55:26 -07003052// --- SensorInputMapperTest ---
3053
3054class SensorInputMapperTest : public InputMapperTest {
3055protected:
3056 static const int32_t ACCEL_RAW_MIN;
3057 static const int32_t ACCEL_RAW_MAX;
3058 static const int32_t ACCEL_RAW_FUZZ;
3059 static const int32_t ACCEL_RAW_FLAT;
3060 static const int32_t ACCEL_RAW_RESOLUTION;
3061
3062 static const int32_t GYRO_RAW_MIN;
3063 static const int32_t GYRO_RAW_MAX;
3064 static const int32_t GYRO_RAW_FUZZ;
3065 static const int32_t GYRO_RAW_FLAT;
3066 static const int32_t GYRO_RAW_RESOLUTION;
3067
3068 static const float GRAVITY_MS2_UNIT;
3069 static const float DEGREE_RADIAN_UNIT;
3070
3071 void prepareAccelAxes();
3072 void prepareGyroAxes();
3073 void setAccelProperties();
3074 void setGyroProperties();
3075 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::SENSOR); }
3076};
3077
3078const int32_t SensorInputMapperTest::ACCEL_RAW_MIN = -32768;
3079const int32_t SensorInputMapperTest::ACCEL_RAW_MAX = 32768;
3080const int32_t SensorInputMapperTest::ACCEL_RAW_FUZZ = 16;
3081const int32_t SensorInputMapperTest::ACCEL_RAW_FLAT = 0;
3082const int32_t SensorInputMapperTest::ACCEL_RAW_RESOLUTION = 8192;
3083
3084const int32_t SensorInputMapperTest::GYRO_RAW_MIN = -2097152;
3085const int32_t SensorInputMapperTest::GYRO_RAW_MAX = 2097152;
3086const int32_t SensorInputMapperTest::GYRO_RAW_FUZZ = 16;
3087const int32_t SensorInputMapperTest::GYRO_RAW_FLAT = 0;
3088const int32_t SensorInputMapperTest::GYRO_RAW_RESOLUTION = 1024;
3089
3090const float SensorInputMapperTest::GRAVITY_MS2_UNIT = 9.80665f;
3091const float SensorInputMapperTest::DEGREE_RADIAN_UNIT = 0.0174533f;
3092
3093void SensorInputMapperTest::prepareAccelAxes() {
3094 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3095 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3096 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3097 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3098 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Z, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3099 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3100}
3101
3102void SensorInputMapperTest::prepareGyroAxes() {
3103 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RX, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3104 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3105 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RY, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3106 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3107 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RZ, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3108 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3109}
3110
3111void SensorInputMapperTest::setAccelProperties() {
3112 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 0, InputDeviceSensorType::ACCELEROMETER,
3113 /* sensorDataIndex */ 0);
3114 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 1, InputDeviceSensorType::ACCELEROMETER,
3115 /* sensorDataIndex */ 1);
3116 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 2, InputDeviceSensorType::ACCELEROMETER,
3117 /* sensorDataIndex */ 2);
3118 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
3119 addConfigurationProperty("sensor.accelerometer.reportingMode", "0");
3120 addConfigurationProperty("sensor.accelerometer.maxDelay", "100000");
3121 addConfigurationProperty("sensor.accelerometer.minDelay", "5000");
3122 addConfigurationProperty("sensor.accelerometer.power", "1.5");
3123}
3124
3125void SensorInputMapperTest::setGyroProperties() {
3126 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 3, InputDeviceSensorType::GYROSCOPE,
3127 /* sensorDataIndex */ 0);
3128 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 4, InputDeviceSensorType::GYROSCOPE,
3129 /* sensorDataIndex */ 1);
3130 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 5, InputDeviceSensorType::GYROSCOPE,
3131 /* sensorDataIndex */ 2);
3132 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
3133 addConfigurationProperty("sensor.gyroscope.reportingMode", "0");
3134 addConfigurationProperty("sensor.gyroscope.maxDelay", "100000");
3135 addConfigurationProperty("sensor.gyroscope.minDelay", "5000");
3136 addConfigurationProperty("sensor.gyroscope.power", "0.8");
3137}
3138
3139TEST_F(SensorInputMapperTest, GetSources) {
Arpit Singhfb706c32023-04-26 15:07:55 +00003140 SensorInputMapper& mapper = constructAndAddMapper<SensorInputMapper>();
Chris Yef59a2f42020-10-16 12:55:26 -07003141
3142 ASSERT_EQ(static_cast<uint32_t>(AINPUT_SOURCE_SENSOR), mapper.getSources());
3143}
3144
3145TEST_F(SensorInputMapperTest, ProcessAccelerometerSensor) {
3146 setAccelProperties();
3147 prepareAccelAxes();
Arpit Singhfb706c32023-04-26 15:07:55 +00003148 SensorInputMapper& mapper = constructAndAddMapper<SensorInputMapper>();
Chris Yef59a2f42020-10-16 12:55:26 -07003149
3150 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::ACCELEROMETER,
3151 std::chrono::microseconds(10000),
3152 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08003153 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003154 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, 20000);
3155 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, -20000);
3156 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Z, 40000);
3157 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
3158 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07003159
3160 NotifySensorArgs args;
3161 std::vector<float> values = {20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
3162 -20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
3163 40000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT};
3164
3165 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
3166 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
3167 ASSERT_EQ(args.deviceId, DEVICE_ID);
3168 ASSERT_EQ(args.sensorType, InputDeviceSensorType::ACCELEROMETER);
3169 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
3170 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
3171 ASSERT_EQ(args.values, values);
3172 mapper.flushSensor(InputDeviceSensorType::ACCELEROMETER);
3173}
3174
3175TEST_F(SensorInputMapperTest, ProcessGyroscopeSensor) {
3176 setGyroProperties();
3177 prepareGyroAxes();
Arpit Singhfb706c32023-04-26 15:07:55 +00003178 SensorInputMapper& mapper = constructAndAddMapper<SensorInputMapper>();
Chris Yef59a2f42020-10-16 12:55:26 -07003179
3180 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::GYROSCOPE,
3181 std::chrono::microseconds(10000),
3182 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08003183 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003184 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RX, 20000);
3185 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RY, -20000);
3186 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RZ, 40000);
3187 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
3188 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07003189
3190 NotifySensorArgs args;
3191 std::vector<float> values = {20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
3192 -20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
3193 40000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT};
3194
3195 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
3196 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
3197 ASSERT_EQ(args.deviceId, DEVICE_ID);
3198 ASSERT_EQ(args.sensorType, InputDeviceSensorType::GYROSCOPE);
3199 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
3200 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
3201 ASSERT_EQ(args.values, values);
3202 mapper.flushSensor(InputDeviceSensorType::GYROSCOPE);
3203}
3204
Michael Wrightd02c5b62014-02-10 15:10:22 -08003205// --- KeyboardInputMapperTest ---
3206
3207class KeyboardInputMapperTest : public InputMapperTest {
3208protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003209 const std::string UNIQUE_ID = "local:0";
Zixuan Qufecb6062022-11-12 04:44:31 +00003210 const KeyboardLayoutInfo DEVICE_KEYBOARD_LAYOUT_INFO = KeyboardLayoutInfo("en-US", "qwerty");
Michael Wrighta9cf4192022-12-01 23:46:39 +00003211 void prepareDisplay(ui::Rotation orientation);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003212
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003213 void testDPadKeyRotation(KeyboardInputMapper& mapper, int32_t originalScanCode,
Arthur Hung2c9a3342019-07-23 14:18:59 +08003214 int32_t originalKeyCode, int32_t rotatedKeyCode,
3215 int32_t displayId = ADISPLAY_ID_NONE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003216};
3217
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003218/* Similar to setDisplayInfoAndReconfigure, but pre-populates all parameters except for the
3219 * orientation.
3220 */
Michael Wrighta9cf4192022-12-01 23:46:39 +00003221void KeyboardInputMapperTest::prepareDisplay(ui::Rotation orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003222 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
3223 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003224}
3225
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003226void KeyboardInputMapperTest::testDPadKeyRotation(KeyboardInputMapper& mapper,
Arthur Hung2c9a3342019-07-23 14:18:59 +08003227 int32_t originalScanCode, int32_t originalKeyCode,
3228 int32_t rotatedKeyCode, int32_t displayId) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003229 NotifyKeyArgs args;
3230
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003231 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003232 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3233 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3234 ASSERT_EQ(originalScanCode, args.scanCode);
3235 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003236 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003237
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003238 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003239 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3240 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3241 ASSERT_EQ(originalScanCode, args.scanCode);
3242 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003243 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003244}
3245
Michael Wrightd02c5b62014-02-10 15:10:22 -08003246TEST_F(KeyboardInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003247 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003248 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003249 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003250
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003251 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003252}
3253
3254TEST_F(KeyboardInputMapperTest, Process_SimpleKeyPress) {
3255 const int32_t USAGE_A = 0x070004;
3256 const int32_t USAGE_UNKNOWN = 0x07ffff;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003257 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3258 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
Chris Yea52ade12020-08-27 16:49:20 -07003259 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, POLICY_FLAG_WAKE);
3260 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, POLICY_FLAG_WAKE);
3261 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003262
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003263 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003264 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003265 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003266 // Initial metastate is AMETA_NONE.
3267 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003268
3269 // Key down by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003270 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003271 NotifyKeyArgs args;
3272 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3273 ASSERT_EQ(DEVICE_ID, args.deviceId);
3274 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3275 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3276 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3277 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3278 ASSERT_EQ(KEY_HOME, args.scanCode);
3279 ASSERT_EQ(AMETA_NONE, args.metaState);
3280 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3281 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3282 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3283
3284 // Key up by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003285 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003286 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3287 ASSERT_EQ(DEVICE_ID, args.deviceId);
3288 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3289 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3290 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3291 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3292 ASSERT_EQ(KEY_HOME, args.scanCode);
3293 ASSERT_EQ(AMETA_NONE, args.metaState);
3294 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3295 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3296 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3297
3298 // Key down by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003299 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3300 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, 0, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003301 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3302 ASSERT_EQ(DEVICE_ID, args.deviceId);
3303 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3304 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3305 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3306 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3307 ASSERT_EQ(0, args.scanCode);
3308 ASSERT_EQ(AMETA_NONE, args.metaState);
3309 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3310 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3311 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3312
3313 // Key up by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003314 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3315 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003316 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3317 ASSERT_EQ(DEVICE_ID, args.deviceId);
3318 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3319 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3320 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3321 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3322 ASSERT_EQ(0, args.scanCode);
3323 ASSERT_EQ(AMETA_NONE, args.metaState);
3324 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3325 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3326 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3327
3328 // Key down with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003329 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3330 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UNKNOWN, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003331 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3332 ASSERT_EQ(DEVICE_ID, args.deviceId);
3333 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3334 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3335 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3336 ASSERT_EQ(0, args.keyCode);
3337 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3338 ASSERT_EQ(AMETA_NONE, args.metaState);
3339 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3340 ASSERT_EQ(0U, args.policyFlags);
3341 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3342
3343 // Key up with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003344 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3345 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003346 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3347 ASSERT_EQ(DEVICE_ID, args.deviceId);
3348 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3349 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3350 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3351 ASSERT_EQ(0, args.keyCode);
3352 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3353 ASSERT_EQ(AMETA_NONE, args.metaState);
3354 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3355 ASSERT_EQ(0U, args.policyFlags);
3356 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3357}
3358
Vaibhav Devmuraricbba14c2022-10-10 16:54:49 +00003359TEST_F(KeyboardInputMapperTest, Process_KeyRemapping) {
3360 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
3361 mFakeEventHub->addKey(EVENTHUB_ID, KEY_B, 0, AKEYCODE_B, 0);
3362 mFakeEventHub->addKeyRemapping(EVENTHUB_ID, AKEYCODE_A, AKEYCODE_B);
3363
3364 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003365 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Vaibhav Devmuraricbba14c2022-10-10 16:54:49 +00003366 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3367
3368 // Key down by scan code.
3369 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_A, 1);
3370 NotifyKeyArgs args;
3371 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3372 ASSERT_EQ(AKEYCODE_B, args.keyCode);
3373
3374 // Key up by scan code.
3375 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_A, 0);
3376 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3377 ASSERT_EQ(AKEYCODE_B, args.keyCode);
3378}
3379
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003380/**
3381 * Ensure that the readTime is set to the time when the EV_KEY is received.
3382 */
3383TEST_F(KeyboardInputMapperTest, Process_SendsReadTime) {
3384 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3385
3386 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003387 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003388 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3389 NotifyKeyArgs args;
3390
3391 // Key down
Harry Cutts33476232023-01-30 19:57:29 +00003392 process(mapper, ARBITRARY_TIME, /*readTime=*/12, EV_KEY, KEY_HOME, 1);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003393 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3394 ASSERT_EQ(12, args.readTime);
3395
3396 // Key up
Harry Cutts33476232023-01-30 19:57:29 +00003397 process(mapper, ARBITRARY_TIME, /*readTime=*/15, EV_KEY, KEY_HOME, 1);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003398 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3399 ASSERT_EQ(15, args.readTime);
3400}
3401
Michael Wrightd02c5b62014-02-10 15:10:22 -08003402TEST_F(KeyboardInputMapperTest, Process_ShouldUpdateMetaState) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003403 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFTSHIFT, 0, AKEYCODE_SHIFT_LEFT, 0);
3404 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003405 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, 0);
3406 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, 0);
3407 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003408
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003409 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003410 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003411 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003412
Arthur Hung95f68612022-04-07 14:08:22 +08003413 // Initial metastate is AMETA_NONE.
3414 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003415
3416 // Metakey down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003417 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003418 NotifyKeyArgs args;
3419 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3420 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003421 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003422 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003423
3424 // Key down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003425 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_A, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003426 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3427 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003428 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003429
3430 // Key up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003431 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, KEY_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003432 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3433 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003434 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003435
3436 // Metakey up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003437 process(mapper, ARBITRARY_TIME + 3, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003438 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3439 ASSERT_EQ(AMETA_NONE, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003440 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003441 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003442}
3443
3444TEST_F(KeyboardInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003445 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3446 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3447 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3448 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003449
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003450 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003451 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003452 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003453
Michael Wrighta9cf4192022-12-01 23:46:39 +00003454 prepareDisplay(ui::ROTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003455 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3456 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
3457 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3458 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
3459 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3460 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
3461 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3462 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
3463}
3464
3465TEST_F(KeyboardInputMapperTest, Process_WhenOrientationAware_ShouldRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003466 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3467 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3468 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3469 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003470
Michael Wrightd02c5b62014-02-10 15:10:22 -08003471 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003472 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003473 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003474 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003475
Michael Wrighta9cf4192022-12-01 23:46:39 +00003476 prepareDisplay(ui::ROTATION_0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003477 ASSERT_NO_FATAL_FAILURE(
3478 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3479 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3480 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3481 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3482 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3483 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3484 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003485
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003486 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003487 prepareDisplay(ui::ROTATION_90);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003488 ASSERT_NO_FATAL_FAILURE(
3489 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3490 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3491 AKEYCODE_DPAD_UP, DISPLAY_ID));
3492 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3493 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3494 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3495 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003496
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003497 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003498 prepareDisplay(ui::ROTATION_180);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003499 ASSERT_NO_FATAL_FAILURE(
3500 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3501 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3502 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3503 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3504 AKEYCODE_DPAD_UP, DISPLAY_ID));
3505 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3506 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003507
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003508 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003509 prepareDisplay(ui::ROTATION_270);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003510 ASSERT_NO_FATAL_FAILURE(
3511 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3512 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3513 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3514 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3515 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3516 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3517 AKEYCODE_DPAD_UP, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003518
3519 // Special case: if orientation changes while key is down, we still emit the same keycode
3520 // in the key up as we did in the key down.
3521 NotifyKeyArgs args;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003522 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003523 prepareDisplay(ui::ROTATION_270);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003524 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003525 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3526 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3527 ASSERT_EQ(KEY_UP, args.scanCode);
3528 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3529
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003530 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003531 prepareDisplay(ui::ROTATION_180);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003532 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003533 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3534 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3535 ASSERT_EQ(KEY_UP, args.scanCode);
3536 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3537}
3538
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003539TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_NotOrientationAware) {
3540 // If the keyboard is not orientation aware,
3541 // key events should not be associated with a specific display id
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003542 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003543
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003544 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003545 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003546 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003547 NotifyKeyArgs args;
3548
3549 // Display id should be ADISPLAY_ID_NONE without any display configuration.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003550 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003551 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003552 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003553 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3554 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3555
Michael Wrighta9cf4192022-12-01 23:46:39 +00003556 prepareDisplay(ui::ROTATION_0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003557 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003558 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003559 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003560 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3561 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3562}
3563
3564TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_OrientationAware) {
3565 // If the keyboard is orientation aware,
3566 // key events should be associated with the internal viewport
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
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003569 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003570 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003571 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003572 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003573 NotifyKeyArgs args;
3574
3575 // Display id should be ADISPLAY_ID_NONE without any display configuration.
3576 // ^--- already checked by the previous test
3577
Michael Wrighta9cf4192022-12-01 23:46:39 +00003578 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003579 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003580 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003581 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003582 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003583 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3584 ASSERT_EQ(DISPLAY_ID, args.displayId);
3585
3586 constexpr int32_t newDisplayId = 2;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003587 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003588 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003589 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003590 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003591 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003592 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003593 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3594 ASSERT_EQ(newDisplayId, args.displayId);
3595}
3596
Michael Wrightd02c5b62014-02-10 15:10:22 -08003597TEST_F(KeyboardInputMapperTest, GetKeyCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003598 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003599 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003600 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003601
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003602 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003603 ASSERT_EQ(1, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003604
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003605 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003606 ASSERT_EQ(0, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003607}
3608
Philip Junker4af3b3d2021-12-14 10:36:55 +01003609TEST_F(KeyboardInputMapperTest, GetKeyCodeForKeyLocation) {
3610 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003611 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Philip Junker4af3b3d2021-12-14 10:36:55 +01003612 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3613
3614 mFakeEventHub->addKeyCodeMapping(EVENTHUB_ID, AKEYCODE_Y, AKEYCODE_Z);
3615 ASSERT_EQ(AKEYCODE_Z, mapper.getKeyCodeForKeyLocation(AKEYCODE_Y))
3616 << "If a mapping is available, the result is equal to the mapping";
3617
3618 ASSERT_EQ(AKEYCODE_A, mapper.getKeyCodeForKeyLocation(AKEYCODE_A))
3619 << "If no mapping is available, the result is the key location";
3620}
3621
Michael Wrightd02c5b62014-02-10 15:10:22 -08003622TEST_F(KeyboardInputMapperTest, GetScanCodeState) {
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->setScanCodeState(EVENTHUB_ID, KEY_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003628 ASSERT_EQ(1, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003629
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003630 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003631 ASSERT_EQ(0, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003632}
3633
3634TEST_F(KeyboardInputMapperTest, MarkSupportedKeyCodes) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003635 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003636 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003637 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003638
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003639 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003640
Michael Wrightd02c5b62014-02-10 15:10:22 -08003641 uint8_t flags[2] = { 0, 0 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07003642 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, {AKEYCODE_A, AKEYCODE_B}, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003643 ASSERT_TRUE(flags[0]);
3644 ASSERT_FALSE(flags[1]);
3645}
3646
3647TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleMetaStateAndLeds) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003648 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3649 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3650 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3651 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3652 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3653 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003654
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003655 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003656 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003657 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003658 // Initial metastate is AMETA_NONE.
3659 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003660
3661 // Initialization should have turned all of the lights off.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003662 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3663 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3664 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003665
3666 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003667 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3668 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003669 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3670 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3671 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003672 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003673
3674 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003675 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3676 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003677 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3678 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3679 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003680 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003681
3682 // Toggle caps lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003683 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3684 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003685 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3686 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3687 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003688 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003689
3690 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003691 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3692 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003693 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3694 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3695 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003696 ASSERT_EQ(AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003697
3698 // Toggle num lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003699 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3700 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003701 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3702 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3703 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003704 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003705
3706 // Toggle scroll lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003707 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3708 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003709 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3710 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3711 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003712 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003713}
3714
Chris Yea52ade12020-08-27 16:49:20 -07003715TEST_F(KeyboardInputMapperTest, NoMetaStateWhenMetaKeysNotPresent) {
3716 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_BUTTON_A, 0);
3717 mFakeEventHub->addKey(EVENTHUB_ID, BTN_B, 0, AKEYCODE_BUTTON_B, 0);
3718 mFakeEventHub->addKey(EVENTHUB_ID, BTN_X, 0, AKEYCODE_BUTTON_X, 0);
3719 mFakeEventHub->addKey(EVENTHUB_ID, BTN_Y, 0, AKEYCODE_BUTTON_Y, 0);
3720
3721 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003722 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Chris Yea52ade12020-08-27 16:49:20 -07003723 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
3724
Chris Yea52ade12020-08-27 16:49:20 -07003725 // Meta state should be AMETA_NONE after reset
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003726 std::list<NotifyArgs> unused = mapper.reset(ARBITRARY_TIME);
Chris Yea52ade12020-08-27 16:49:20 -07003727 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3728 // Meta state should be AMETA_NONE with update, as device doesn't have the keys.
3729 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
3730 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3731
3732 NotifyKeyArgs args;
3733 // Press button "A"
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003734 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_A, 1);
Chris Yea52ade12020-08-27 16:49:20 -07003735 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3736 ASSERT_EQ(AMETA_NONE, args.metaState);
3737 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3738 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3739 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3740
3741 // Button up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003742 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003743 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3744 ASSERT_EQ(AMETA_NONE, args.metaState);
3745 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3746 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3747 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3748}
3749
Arthur Hung2c9a3342019-07-23 14:18:59 +08003750TEST_F(KeyboardInputMapperTest, Configure_AssignsDisplayPort) {
3751 // keyboard 1.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003752 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3753 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3754 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3755 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003756
3757 // keyboard 2.
3758 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08003759 const std::string DEVICE_NAME2 = "KEYBOARD2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08003760 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003761 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08003762 std::shared_ptr<InputDevice> device2 =
3763 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003764 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08003765
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003766 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3767 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3768 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3769 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003770
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003771 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003772 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003773 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003774
Arpit Singh67ca6842023-04-26 14:43:16 +00003775 device2->addEmptyEventHubDevice(SECOND_EVENTHUB_ID);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003776 KeyboardInputMapper& mapper2 =
Arpit Singh67ca6842023-04-26 14:43:16 +00003777 device2->constructAndAddMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID,
3778 mFakePolicy
3779 ->getReaderConfiguration(),
3780 AINPUT_SOURCE_KEYBOARD,
3781 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003782 std::list<NotifyArgs> unused =
3783 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003784 /*changes=*/{});
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003785 unused += device2->reset(ARBITRARY_TIME);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003786
3787 // Prepared displays and associated info.
3788 constexpr uint8_t hdmi1 = 0;
3789 constexpr uint8_t hdmi2 = 1;
3790 const std::string SECONDARY_UNIQUE_ID = "local:1";
3791
3792 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
3793 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
3794
3795 // No associated display viewport found, should disable the device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003796 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003797 InputReaderConfiguration::Change::DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003798 ASSERT_FALSE(device2->isEnabled());
3799
3800 // Prepare second display.
3801 constexpr int32_t newDisplayId = 2;
Michael Wrighta9cf4192022-12-01 23:46:39 +00003802 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003803 UNIQUE_ID, hdmi1, ViewportType::INTERNAL);
Michael Wrighta9cf4192022-12-01 23:46:39 +00003804 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003805 SECONDARY_UNIQUE_ID, hdmi2, ViewportType::EXTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003806 // Default device will reconfigure above, need additional reconfiguration for another device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003807 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003808 InputReaderConfiguration::Change::DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003809
3810 // Device should be enabled after the associated display is found.
3811 ASSERT_TRUE(mDevice->isEnabled());
3812 ASSERT_TRUE(device2->isEnabled());
3813
3814 // Test pad key events
3815 ASSERT_NO_FATAL_FAILURE(
3816 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3817 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3818 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3819 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3820 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3821 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3822 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3823
3824 ASSERT_NO_FATAL_FAILURE(
3825 testDPadKeyRotation(mapper2, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, newDisplayId));
3826 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3827 AKEYCODE_DPAD_RIGHT, newDisplayId));
3828 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3829 AKEYCODE_DPAD_DOWN, newDisplayId));
3830 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3831 AKEYCODE_DPAD_LEFT, newDisplayId));
3832}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003833
arthurhungc903df12020-08-11 15:08:42 +08003834TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleAfterReattach) {
3835 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3836 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3837 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3838 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3839 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3840 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3841
3842 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003843 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
arthurhungc903df12020-08-11 15:08:42 +08003844 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003845 // Initial metastate is AMETA_NONE.
3846 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08003847
3848 // Initialization should have turned all of the lights off.
3849 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3850 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3851 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3852
3853 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003854 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3855 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003856 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3857 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
3858
3859 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003860 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3861 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003862 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3863 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
3864
3865 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003866 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3867 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003868 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3869 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
3870
3871 mFakeEventHub->removeDevice(EVENTHUB_ID);
3872 mReader->loopOnce();
3873
3874 // keyboard 2 should default toggle keys.
3875 const std::string USB2 = "USB2";
3876 const std::string DEVICE_NAME2 = "KEYBOARD2";
3877 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
3878 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
3879 std::shared_ptr<InputDevice> device2 =
3880 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003881 ftl::Flags<InputDeviceClass>(0));
arthurhungc903df12020-08-11 15:08:42 +08003882 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3883 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
3884 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3885 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3886 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3887 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3888
Arpit Singh67ca6842023-04-26 14:43:16 +00003889 device2->addEmptyEventHubDevice(SECOND_EVENTHUB_ID);
arthurhung6fe95782020-10-05 22:41:16 +08003890 KeyboardInputMapper& mapper2 =
Arpit Singh67ca6842023-04-26 14:43:16 +00003891 device2->constructAndAddMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID,
3892 mFakePolicy
3893 ->getReaderConfiguration(),
3894 AINPUT_SOURCE_KEYBOARD,
3895 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003896 std::list<NotifyArgs> unused =
3897 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003898 /*changes=*/{});
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003899 unused += device2->reset(ARBITRARY_TIME);
arthurhungc903df12020-08-11 15:08:42 +08003900
3901 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_CAPSL));
3902 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_NUML));
3903 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_SCROLLL));
arthurhung6fe95782020-10-05 22:41:16 +08003904 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON,
3905 mapper2.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08003906}
3907
Arthur Hungcb40a002021-08-03 14:31:01 +00003908TEST_F(KeyboardInputMapperTest, Process_toggleCapsLockState) {
3909 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3910 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3911 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3912
3913 // Suppose we have two mappers. (DPAD + KEYBOARD)
Arpit Singh67ca6842023-04-26 14:43:16 +00003914 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_DPAD,
Arthur Hungcb40a002021-08-03 14:31:01 +00003915 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
3916 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003917 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Arthur Hungcb40a002021-08-03 14:31:01 +00003918 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003919 // Initial metastate is AMETA_NONE.
3920 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Arthur Hungcb40a002021-08-03 14:31:01 +00003921
3922 mReader->toggleCapsLockState(DEVICE_ID);
3923 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
3924}
3925
Arthur Hungfb3cc112022-04-13 07:39:50 +00003926TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleInMultiDevices) {
3927 // keyboard 1.
3928 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3929 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3930 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3931 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3932 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3933 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3934
3935 KeyboardInputMapper& mapper1 =
Arpit Singh67ca6842023-04-26 14:43:16 +00003936 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Arthur Hungfb3cc112022-04-13 07:39:50 +00003937 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3938
3939 // keyboard 2.
3940 const std::string USB2 = "USB2";
3941 const std::string DEVICE_NAME2 = "KEYBOARD2";
3942 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
3943 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
3944 std::shared_ptr<InputDevice> device2 =
3945 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
3946 ftl::Flags<InputDeviceClass>(0));
3947 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3948 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
3949 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3950 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3951 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3952 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3953
Arpit Singh67ca6842023-04-26 14:43:16 +00003954 device2->addEmptyEventHubDevice(SECOND_EVENTHUB_ID);
Arthur Hungfb3cc112022-04-13 07:39:50 +00003955 KeyboardInputMapper& mapper2 =
Arpit Singh67ca6842023-04-26 14:43:16 +00003956 device2->constructAndAddMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID,
3957 mFakePolicy
3958 ->getReaderConfiguration(),
3959 AINPUT_SOURCE_KEYBOARD,
3960 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003961 std::list<NotifyArgs> unused =
3962 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003963 /*changes=*/{});
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003964 unused += device2->reset(ARBITRARY_TIME);
Arthur Hungfb3cc112022-04-13 07:39:50 +00003965
Arthur Hung95f68612022-04-07 14:08:22 +08003966 // Initial metastate is AMETA_NONE.
3967 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
3968 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
3969
3970 // Toggle num lock on and off.
3971 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3972 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Arthur Hungfb3cc112022-04-13 07:39:50 +00003973 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3974 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper1.getMetaState());
3975 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper2.getMetaState());
3976
3977 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3978 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
3979 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3980 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
3981 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
3982
3983 // Toggle caps lock on and off.
3984 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3985 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
3986 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3987 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper1.getMetaState());
3988 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper2.getMetaState());
3989
3990 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3991 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
3992 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3993 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
3994 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
3995
3996 // Toggle scroll lock on and off.
3997 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3998 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
3999 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
4000 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper1.getMetaState());
4001 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper2.getMetaState());
4002
4003 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
4004 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
4005 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
4006 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4007 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4008}
4009
Arthur Hung2141d542022-08-23 07:45:21 +00004010TEST_F(KeyboardInputMapperTest, Process_DisabledDevice) {
4011 const int32_t USAGE_A = 0x070004;
4012 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4013 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
4014
4015 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00004016 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Arthur Hung2141d542022-08-23 07:45:21 +00004017 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
4018 // Key down by scan code.
4019 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
4020 NotifyKeyArgs args;
4021 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4022 ASSERT_EQ(DEVICE_ID, args.deviceId);
4023 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
4024 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4025 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
4026 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4027 ASSERT_EQ(KEY_HOME, args.scanCode);
4028 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
4029
4030 // Disable device, it should synthesize cancellation events for down events.
4031 mFakePolicy->addDisabledDevice(DEVICE_ID);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004032 configureDevice(InputReaderConfiguration::Change::ENABLED_STATE);
Arthur Hung2141d542022-08-23 07:45:21 +00004033
4034 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4035 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
4036 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4037 ASSERT_EQ(KEY_HOME, args.scanCode);
4038 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_CANCELED, args.flags);
4039}
4040
Zixuan Qufecb6062022-11-12 04:44:31 +00004041TEST_F(KeyboardInputMapperTest, Configure_AssignKeyboardLayoutInfo) {
Arpit Singh67ca6842023-04-26 14:43:16 +00004042 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4043 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Zixuan Qufecb6062022-11-12 04:44:31 +00004044 std::list<NotifyArgs> unused =
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004045 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
4046 /*changes=*/{});
Zixuan Qufecb6062022-11-12 04:44:31 +00004047
Vaibhav Devmurari0a6fee82023-04-11 18:53:04 +00004048 uint32_t generation = mReader->getContext()->getGeneration();
Zixuan Qufecb6062022-11-12 04:44:31 +00004049 mFakePolicy->addKeyboardLayoutAssociation(DEVICE_LOCATION, DEVICE_KEYBOARD_LAYOUT_INFO);
4050
4051 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004052 InputReaderConfiguration::Change::KEYBOARD_LAYOUT_ASSOCIATION);
Zixuan Qufecb6062022-11-12 04:44:31 +00004053
4054 InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
4055 ASSERT_EQ(DEVICE_KEYBOARD_LAYOUT_INFO.languageTag,
4056 deviceInfo.getKeyboardLayoutInfo()->languageTag);
4057 ASSERT_EQ(DEVICE_KEYBOARD_LAYOUT_INFO.layoutType,
4058 deviceInfo.getKeyboardLayoutInfo()->layoutType);
Vaibhav Devmurari0a6fee82023-04-11 18:53:04 +00004059 ASSERT_TRUE(mReader->getContext()->getGeneration() != generation);
4060
4061 // Call change layout association with the same values: Generation shouldn't change
4062 generation = mReader->getContext()->getGeneration();
4063 mFakePolicy->addKeyboardLayoutAssociation(DEVICE_LOCATION, DEVICE_KEYBOARD_LAYOUT_INFO);
4064 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
4065 InputReaderConfiguration::Change::KEYBOARD_LAYOUT_ASSOCIATION);
4066 ASSERT_TRUE(mReader->getContext()->getGeneration() == generation);
Zixuan Qufecb6062022-11-12 04:44:31 +00004067}
4068
Vaibhav Devmurari7fb41132023-01-02 13:30:26 +00004069TEST_F(KeyboardInputMapperTest, LayoutInfoCorrectlyMapped) {
4070 mFakeEventHub->setRawLayoutInfo(EVENTHUB_ID,
4071 RawLayoutInfo{.languageTag = "en", .layoutType = "extended"});
4072
4073 // Configuration
Arpit Singh67ca6842023-04-26 14:43:16 +00004074 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Vaibhav Devmurari7fb41132023-01-02 13:30:26 +00004075 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
4076 InputReaderConfiguration config;
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004077 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, config, /*changes=*/{});
Vaibhav Devmurari7fb41132023-01-02 13:30:26 +00004078
4079 ASSERT_EQ("en", mDevice->getDeviceInfo().getKeyboardLayoutInfo()->languageTag);
4080 ASSERT_EQ("extended", mDevice->getDeviceInfo().getKeyboardLayoutInfo()->layoutType);
4081}
4082
Justin Chung71ddb432023-03-27 04:29:07 +00004083TEST_F(KeyboardInputMapperTest, Process_GesureEventToSetFlagKeepTouchMode) {
4084 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, POLICY_FLAG_GESTURE);
4085 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00004086 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Justin Chung71ddb432023-03-27 04:29:07 +00004087 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
4088 NotifyKeyArgs args;
4089
4090 // Key down
4091 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_LEFT, 1);
4092 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4093 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_KEEP_TOUCH_MODE, args.flags);
4094}
4095
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004096// --- KeyboardInputMapperTest_ExternalDevice ---
4097
4098class KeyboardInputMapperTest_ExternalDevice : public InputMapperTest {
4099protected:
Chris Yea52ade12020-08-27 16:49:20 -07004100 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004101};
4102
Vaibhav Devmurari2681a812024-01-11 00:15:35 +00004103TEST_F(KeyboardInputMapperTest_ExternalDevice, WakeBehavior_AlphabeticKeyboard) {
Vaibhav Devmurari16257862023-03-06 10:06:32 +00004104 // For external devices, keys will trigger wake on key down. Media keys should also trigger
4105 // wake if triggered from external devices.
Powei Fengd041c5d2019-05-03 17:11:33 -07004106
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004107 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, 0);
4108 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, 0);
4109 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAYPAUSE, 0, AKEYCODE_MEDIA_PLAY_PAUSE,
4110 POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07004111
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004112 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00004113 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004114 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07004115
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004116 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004117 NotifyKeyArgs args;
4118 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4119 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4120
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004121 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004122 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4123 ASSERT_EQ(uint32_t(0), args.policyFlags);
4124
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004125 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004126 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Vaibhav Devmurari16257862023-03-06 10:06:32 +00004127 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
Powei Fengd041c5d2019-05-03 17:11:33 -07004128
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004129 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004130 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4131 ASSERT_EQ(uint32_t(0), args.policyFlags);
4132
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004133 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004134 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4135 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4136
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004137 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004138 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4139 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4140}
4141
Vaibhav Devmurari2681a812024-01-11 00:15:35 +00004142TEST_F(KeyboardInputMapperTest_ExternalDevice, WakeBehavior_NoneAlphabeticKeyboard) {
4143 // For external devices, keys will trigger wake on key down. Media keys should not trigger
4144 // wake if triggered from external non-alphaebtic keyboard (e.g. headsets).
4145
4146 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, 0);
4147 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAYPAUSE, 0, AKEYCODE_MEDIA_PLAY_PAUSE,
4148 POLICY_FLAG_WAKE);
4149
4150 KeyboardInputMapper& mapper =
4151 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4152 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
4153
4154 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
4155 NotifyKeyArgs args;
4156 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4157 ASSERT_EQ(uint32_t(0), args.policyFlags);
4158
4159 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
4160 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4161 ASSERT_EQ(uint32_t(0), args.policyFlags);
4162
4163 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 1);
4164 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4165 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4166
4167 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 0);
4168 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4169 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4170}
4171
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004172TEST_F(KeyboardInputMapperTest_ExternalDevice, DoNotWakeByDefaultBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07004173 // Tv Remote key's wake behavior is prescribed by the keylayout file.
Powei Fengd041c5d2019-05-03 17:11:33 -07004174
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004175 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4176 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
4177 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07004178
Powei Fengd041c5d2019-05-03 17:11:33 -07004179 addConfigurationProperty("keyboard.doNotWakeByDefault", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004180 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00004181 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004182 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07004183
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004184 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004185 NotifyKeyArgs args;
4186 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4187 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4188
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004189 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004190 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4191 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4192
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004193 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_DOWN, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004194 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4195 ASSERT_EQ(uint32_t(0), args.policyFlags);
4196
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004197 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_DOWN, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004198 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4199 ASSERT_EQ(uint32_t(0), args.policyFlags);
4200
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004201 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004202 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4203 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4204
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004205 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004206 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4207 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4208}
4209
Michael Wrightd02c5b62014-02-10 15:10:22 -08004210// --- TouchInputMapperTest ---
4211
4212class TouchInputMapperTest : public InputMapperTest {
4213protected:
4214 static const int32_t RAW_X_MIN;
4215 static const int32_t RAW_X_MAX;
4216 static const int32_t RAW_Y_MIN;
4217 static const int32_t RAW_Y_MAX;
4218 static const int32_t RAW_TOUCH_MIN;
4219 static const int32_t RAW_TOUCH_MAX;
4220 static const int32_t RAW_TOOL_MIN;
4221 static const int32_t RAW_TOOL_MAX;
4222 static const int32_t RAW_PRESSURE_MIN;
4223 static const int32_t RAW_PRESSURE_MAX;
4224 static const int32_t RAW_ORIENTATION_MIN;
4225 static const int32_t RAW_ORIENTATION_MAX;
4226 static const int32_t RAW_DISTANCE_MIN;
4227 static const int32_t RAW_DISTANCE_MAX;
4228 static const int32_t RAW_TILT_MIN;
4229 static const int32_t RAW_TILT_MAX;
4230 static const int32_t RAW_ID_MIN;
4231 static const int32_t RAW_ID_MAX;
4232 static const int32_t RAW_SLOT_MIN;
4233 static const int32_t RAW_SLOT_MAX;
4234 static const float X_PRECISION;
4235 static const float Y_PRECISION;
Santos Cordonfa5cf462017-04-05 10:37:00 -07004236 static const float X_PRECISION_VIRTUAL;
4237 static const float Y_PRECISION_VIRTUAL;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004238
4239 static const float GEOMETRIC_SCALE;
Jason Gerecke489fda82012-09-07 17:19:40 -07004240 static const TouchAffineTransformation AFFINE_TRANSFORM;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004241
4242 static const VirtualKeyDefinition VIRTUAL_KEYS[2];
4243
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004244 const std::string UNIQUE_ID = "local:0";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004245 const std::string SECONDARY_UNIQUE_ID = "local:1";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004246
Michael Wrightd02c5b62014-02-10 15:10:22 -08004247 enum Axes {
4248 POSITION = 1 << 0,
4249 TOUCH = 1 << 1,
4250 TOOL = 1 << 2,
4251 PRESSURE = 1 << 3,
4252 ORIENTATION = 1 << 4,
4253 MINOR = 1 << 5,
4254 ID = 1 << 6,
4255 DISTANCE = 1 << 7,
4256 TILT = 1 << 8,
4257 SLOT = 1 << 9,
4258 TOOL_TYPE = 1 << 10,
4259 };
4260
Michael Wrighta9cf4192022-12-01 23:46:39 +00004261 void prepareDisplay(ui::Rotation orientation, std::optional<uint8_t> port = NO_PORT);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004262 void prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port = NO_PORT);
Michael Wrighta9cf4192022-12-01 23:46:39 +00004263 void prepareVirtualDisplay(ui::Rotation orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004264 void prepareVirtualKeys();
Jason Gerecke489fda82012-09-07 17:19:40 -07004265 void prepareLocationCalibration();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004266 int32_t toRawX(float displayX);
4267 int32_t toRawY(float displayY);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07004268 int32_t toRotatedRawX(float displayX);
4269 int32_t toRotatedRawY(float displayY);
Jason Gerecke489fda82012-09-07 17:19:40 -07004270 float toCookedX(float rawX, float rawY);
4271 float toCookedY(float rawX, float rawY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004272 float toDisplayX(int32_t rawX);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004273 float toDisplayX(int32_t rawX, int32_t displayWidth);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004274 float toDisplayY(int32_t rawY);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004275 float toDisplayY(int32_t rawY, int32_t displayHeight);
4276
Michael Wrightd02c5b62014-02-10 15:10:22 -08004277};
4278
4279const int32_t TouchInputMapperTest::RAW_X_MIN = 25;
4280const int32_t TouchInputMapperTest::RAW_X_MAX = 1019;
4281const int32_t TouchInputMapperTest::RAW_Y_MIN = 30;
4282const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009;
4283const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0;
4284const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31;
4285const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0;
4286const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15;
Michael Wrightaa449c92017-12-13 21:21:43 +00004287const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = 0;
4288const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = 255;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004289const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7;
4290const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
4291const int32_t TouchInputMapperTest::RAW_DISTANCE_MIN = 0;
4292const int32_t TouchInputMapperTest::RAW_DISTANCE_MAX = 7;
4293const int32_t TouchInputMapperTest::RAW_TILT_MIN = 0;
4294const int32_t TouchInputMapperTest::RAW_TILT_MAX = 150;
4295const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
4296const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
4297const int32_t TouchInputMapperTest::RAW_SLOT_MIN = 0;
4298const int32_t TouchInputMapperTest::RAW_SLOT_MAX = 9;
4299const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH;
4300const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT;
Santos Cordonfa5cf462017-04-05 10:37:00 -07004301const float TouchInputMapperTest::X_PRECISION_VIRTUAL =
4302 float(RAW_X_MAX - RAW_X_MIN + 1) / VIRTUAL_DISPLAY_WIDTH;
4303const float TouchInputMapperTest::Y_PRECISION_VIRTUAL =
4304 float(RAW_Y_MAX - RAW_Y_MIN + 1) / VIRTUAL_DISPLAY_HEIGHT;
Jason Gerecke489fda82012-09-07 17:19:40 -07004305const TouchAffineTransformation TouchInputMapperTest::AFFINE_TRANSFORM =
4306 TouchAffineTransformation(1, -2, 3, -4, 5, -6);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004307
4308const float TouchInputMapperTest::GEOMETRIC_SCALE =
4309 avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1),
4310 float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1));
4311
4312const VirtualKeyDefinition TouchInputMapperTest::VIRTUAL_KEYS[2] = {
4313 { KEY_HOME, 60, DISPLAY_HEIGHT + 15, 20, 20 },
4314 { KEY_MENU, DISPLAY_HEIGHT - 60, DISPLAY_WIDTH + 15, 20, 20 },
4315};
4316
Michael Wrighta9cf4192022-12-01 23:46:39 +00004317void TouchInputMapperTest::prepareDisplay(ui::Rotation orientation, std::optional<uint8_t> port) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004318 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
4319 port, ViewportType::INTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004320}
4321
4322void TouchInputMapperTest::prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port) {
4323 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +00004324 ui::ROTATION_0, SECONDARY_UNIQUE_ID, port, type);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004325}
4326
Michael Wrighta9cf4192022-12-01 23:46:39 +00004327void TouchInputMapperTest::prepareVirtualDisplay(ui::Rotation orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004328 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH, VIRTUAL_DISPLAY_HEIGHT,
4329 orientation, VIRTUAL_DISPLAY_UNIQUE_ID, NO_PORT,
4330 ViewportType::VIRTUAL);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004331}
4332
Michael Wrightd02c5b62014-02-10 15:10:22 -08004333void TouchInputMapperTest::prepareVirtualKeys() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004334 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[0]);
4335 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[1]);
4336 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4337 mFakeEventHub->addKey(EVENTHUB_ID, KEY_MENU, 0, AKEYCODE_MENU, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004338}
4339
Jason Gerecke489fda82012-09-07 17:19:40 -07004340void TouchInputMapperTest::prepareLocationCalibration() {
4341 mFakePolicy->setTouchAffineTransformation(AFFINE_TRANSFORM);
4342}
4343
Michael Wrightd02c5b62014-02-10 15:10:22 -08004344int32_t TouchInputMapperTest::toRawX(float displayX) {
4345 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN);
4346}
4347
4348int32_t TouchInputMapperTest::toRawY(float displayY) {
4349 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN);
4350}
4351
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07004352int32_t TouchInputMapperTest::toRotatedRawX(float displayX) {
4353 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_HEIGHT + RAW_X_MIN);
4354}
4355
4356int32_t TouchInputMapperTest::toRotatedRawY(float displayY) {
4357 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_WIDTH + RAW_Y_MIN);
4358}
4359
Jason Gerecke489fda82012-09-07 17:19:40 -07004360float TouchInputMapperTest::toCookedX(float rawX, float rawY) {
4361 AFFINE_TRANSFORM.applyTo(rawX, rawY);
4362 return rawX;
4363}
4364
4365float TouchInputMapperTest::toCookedY(float rawX, float rawY) {
4366 AFFINE_TRANSFORM.applyTo(rawX, rawY);
4367 return rawY;
4368}
4369
Michael Wrightd02c5b62014-02-10 15:10:22 -08004370float TouchInputMapperTest::toDisplayX(int32_t rawX) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004371 return toDisplayX(rawX, DISPLAY_WIDTH);
4372}
4373
4374float TouchInputMapperTest::toDisplayX(int32_t rawX, int32_t displayWidth) {
4375 return float(rawX - RAW_X_MIN) * displayWidth / (RAW_X_MAX - RAW_X_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004376}
4377
4378float TouchInputMapperTest::toDisplayY(int32_t rawY) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004379 return toDisplayY(rawY, DISPLAY_HEIGHT);
4380}
4381
4382float TouchInputMapperTest::toDisplayY(int32_t rawY, int32_t displayHeight) {
4383 return float(rawY - RAW_Y_MIN) * displayHeight / (RAW_Y_MAX - RAW_Y_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004384}
4385
4386
4387// --- SingleTouchInputMapperTest ---
4388
4389class SingleTouchInputMapperTest : public TouchInputMapperTest {
4390protected:
4391 void prepareButtons();
4392 void prepareAxes(int axes);
4393
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004394 void processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
4395 void processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
4396 void processUp(SingleTouchInputMapper& mappery);
4397 void processPressure(SingleTouchInputMapper& mapper, int32_t pressure);
4398 void processToolMajor(SingleTouchInputMapper& mapper, int32_t toolMajor);
4399 void processDistance(SingleTouchInputMapper& mapper, int32_t distance);
4400 void processTilt(SingleTouchInputMapper& mapper, int32_t tiltX, int32_t tiltY);
4401 void processKey(SingleTouchInputMapper& mapper, int32_t code, int32_t value);
4402 void processSync(SingleTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004403};
4404
4405void SingleTouchInputMapperTest::prepareButtons() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004406 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004407}
4408
4409void SingleTouchInputMapperTest::prepareAxes(int axes) {
4410 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004411 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
4412 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004413 }
4414 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004415 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MIN,
4416 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004417 }
4418 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004419 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TOOL_WIDTH, RAW_TOOL_MIN, RAW_TOOL_MAX, 0,
4420 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004421 }
4422 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004423 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_DISTANCE, RAW_DISTANCE_MIN,
4424 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004425 }
4426 if (axes & TILT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004427 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_X, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
4428 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_Y, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004429 }
4430}
4431
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004432void SingleTouchInputMapperTest::processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004433 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
4434 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
4435 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004436}
4437
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004438void SingleTouchInputMapperTest::processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004439 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
4440 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004441}
4442
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004443void SingleTouchInputMapperTest::processUp(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004444 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004445}
4446
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004447void SingleTouchInputMapperTest::processPressure(SingleTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004448 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004449}
4450
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004451void SingleTouchInputMapperTest::processToolMajor(SingleTouchInputMapper& mapper,
4452 int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004453 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TOOL_WIDTH, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004454}
4455
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004456void SingleTouchInputMapperTest::processDistance(SingleTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004457 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004458}
4459
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004460void SingleTouchInputMapperTest::processTilt(SingleTouchInputMapper& mapper, int32_t tiltX,
4461 int32_t tiltY) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004462 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_X, tiltX);
4463 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_Y, tiltY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004464}
4465
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004466void SingleTouchInputMapperTest::processKey(SingleTouchInputMapper& mapper, int32_t code,
4467 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004468 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004469}
4470
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004471void SingleTouchInputMapperTest::processSync(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004472 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004473}
4474
Michael Wrightd02c5b62014-02-10 15:10:22 -08004475TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004476 prepareButtons();
4477 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00004478 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004479
Josep del Río2d8c79a2023-01-23 19:33:50 +00004480 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004481}
4482
Michael Wrightd02c5b62014-02-10 15:10:22 -08004483TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004484 prepareButtons();
4485 prepareAxes(POSITION);
4486 addConfigurationProperty("touch.deviceType", "touchScreen");
Arpit Singha8c236b2023-04-25 13:56:05 +00004487 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004488
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004489 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004490}
4491
4492TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004493 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00004494 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004495 prepareButtons();
4496 prepareAxes(POSITION);
4497 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00004498 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004499
4500 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004501 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004502
4503 // Virtual key is down.
4504 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4505 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4506 processDown(mapper, x, y);
4507 processSync(mapper);
4508 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4509
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004510 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004511
4512 // Virtual key is up.
4513 processUp(mapper);
4514 processSync(mapper);
4515 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4516
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004517 ASSERT_EQ(AKEY_STATE_UP, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004518}
4519
4520TEST_F(SingleTouchInputMapperTest, GetScanCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004521 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00004522 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004523 prepareButtons();
4524 prepareAxes(POSITION);
4525 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00004526 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004527
4528 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004529 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004530
4531 // Virtual key is down.
4532 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4533 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4534 processDown(mapper, x, y);
4535 processSync(mapper);
4536 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4537
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004538 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004539
4540 // Virtual key is up.
4541 processUp(mapper);
4542 processSync(mapper);
4543 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4544
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004545 ASSERT_EQ(AKEY_STATE_UP, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004546}
4547
4548TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004549 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00004550 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004551 prepareButtons();
4552 prepareAxes(POSITION);
4553 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00004554 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004555
Michael Wrightd02c5b62014-02-10 15:10:22 -08004556 uint8_t flags[2] = { 0, 0 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07004557 ASSERT_TRUE(
4558 mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, {AKEYCODE_HOME, AKEYCODE_A}, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004559 ASSERT_TRUE(flags[0]);
4560 ASSERT_FALSE(flags[1]);
4561}
4562
4563TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004564 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00004565 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004566 prepareButtons();
4567 prepareAxes(POSITION);
4568 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00004569 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004570
arthurhungdcef2dc2020-08-11 14:47:50 +08004571 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004572
4573 NotifyKeyArgs args;
4574
4575 // Press virtual key.
4576 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4577 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4578 processDown(mapper, x, y);
4579 processSync(mapper);
4580
4581 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4582 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4583 ASSERT_EQ(DEVICE_ID, args.deviceId);
4584 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
4585 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
4586 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
4587 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
4588 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4589 ASSERT_EQ(KEY_HOME, args.scanCode);
4590 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4591 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4592
4593 // Release virtual key.
4594 processUp(mapper);
4595 processSync(mapper);
4596
4597 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4598 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4599 ASSERT_EQ(DEVICE_ID, args.deviceId);
4600 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
4601 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
4602 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
4603 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
4604 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4605 ASSERT_EQ(KEY_HOME, args.scanCode);
4606 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4607 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4608
4609 // Should not have sent any motions.
4610 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4611}
4612
4613TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004614 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00004615 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004616 prepareButtons();
4617 prepareAxes(POSITION);
4618 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00004619 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004620
arthurhungdcef2dc2020-08-11 14:47:50 +08004621 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004622
4623 NotifyKeyArgs keyArgs;
4624
4625 // Press virtual key.
4626 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4627 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4628 processDown(mapper, x, y);
4629 processSync(mapper);
4630
4631 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4632 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
4633 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
4634 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
4635 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
4636 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4637 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags);
4638 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
4639 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
4640 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
4641 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
4642
4643 // Move out of bounds. This should generate a cancel and a pointer down since we moved
4644 // into the display area.
4645 y -= 100;
4646 processMove(mapper, x, y);
4647 processSync(mapper);
4648
4649 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4650 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
4651 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
4652 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
4653 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
4654 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4655 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
4656 | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags);
4657 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
4658 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
4659 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
4660 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
4661
4662 NotifyMotionArgs motionArgs;
4663 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4664 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4665 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4666 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4667 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4668 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4669 ASSERT_EQ(0, motionArgs.flags);
4670 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4671 ASSERT_EQ(0, motionArgs.buttonState);
4672 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004673 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004674 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004675 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004676 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4677 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4678 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4679 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4680 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4681
4682 // Keep moving out of bounds. Should generate a pointer move.
4683 y -= 50;
4684 processMove(mapper, x, y);
4685 processSync(mapper);
4686
4687 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4688 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4689 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4690 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4691 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4692 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4693 ASSERT_EQ(0, motionArgs.flags);
4694 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4695 ASSERT_EQ(0, motionArgs.buttonState);
4696 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004697 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004698 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004699 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004700 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4701 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4702 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4703 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4704 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4705
4706 // Release out of bounds. Should generate a pointer up.
4707 processUp(mapper);
4708 processSync(mapper);
4709
4710 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4711 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4712 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4713 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4714 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4715 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4716 ASSERT_EQ(0, motionArgs.flags);
4717 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4718 ASSERT_EQ(0, motionArgs.buttonState);
4719 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004720 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004721 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004722 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004723 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4724 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4725 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4726 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4727 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4728
4729 // Should not have sent any more keys or motions.
4730 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4731 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4732}
4733
4734TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004735 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00004736 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004737 prepareButtons();
4738 prepareAxes(POSITION);
4739 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00004740 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004741
arthurhungdcef2dc2020-08-11 14:47:50 +08004742 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004743
4744 NotifyMotionArgs motionArgs;
4745
4746 // Initially go down out of bounds.
4747 int32_t x = -10;
4748 int32_t y = -10;
4749 processDown(mapper, x, y);
4750 processSync(mapper);
4751
4752 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4753
4754 // Move into the display area. Should generate a pointer down.
4755 x = 50;
4756 y = 75;
4757 processMove(mapper, x, y);
4758 processSync(mapper);
4759
4760 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4761 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4762 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4763 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4764 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4765 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4766 ASSERT_EQ(0, motionArgs.flags);
4767 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4768 ASSERT_EQ(0, motionArgs.buttonState);
4769 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004770 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004771 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004772 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004773 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4774 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4775 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4776 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4777 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4778
4779 // Release. Should generate a pointer up.
4780 processUp(mapper);
4781 processSync(mapper);
4782
4783 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4784 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4785 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4786 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4787 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4788 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4789 ASSERT_EQ(0, motionArgs.flags);
4790 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4791 ASSERT_EQ(0, motionArgs.buttonState);
4792 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004793 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004794 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004795 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004796 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4797 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4798 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4799 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4800 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4801
4802 // Should not have sent any more keys or motions.
4803 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4804 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4805}
4806
Santos Cordonfa5cf462017-04-05 10:37:00 -07004807TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture_VirtualDisplay) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004808 addConfigurationProperty("touch.deviceType", "touchScreen");
4809 addConfigurationProperty("touch.displayId", VIRTUAL_DISPLAY_UNIQUE_ID);
4810
Michael Wrighta9cf4192022-12-01 23:46:39 +00004811 prepareVirtualDisplay(ui::ROTATION_0);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004812 prepareButtons();
4813 prepareAxes(POSITION);
4814 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00004815 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Santos Cordonfa5cf462017-04-05 10:37:00 -07004816
arthurhungdcef2dc2020-08-11 14:47:50 +08004817 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004818
4819 NotifyMotionArgs motionArgs;
4820
4821 // Down.
4822 int32_t x = 100;
4823 int32_t y = 125;
4824 processDown(mapper, x, y);
4825 processSync(mapper);
4826
4827 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4828 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4829 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4830 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
4831 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4832 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4833 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4834 ASSERT_EQ(0, motionArgs.flags);
4835 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4836 ASSERT_EQ(0, motionArgs.buttonState);
4837 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004838 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Santos Cordonfa5cf462017-04-05 10:37:00 -07004839 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004840 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004841 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4842 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
4843 1, 0, 0, 0, 0, 0, 0, 0));
4844 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
4845 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
4846 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4847
4848 // Move.
4849 x += 50;
4850 y += 75;
4851 processMove(mapper, x, y);
4852 processSync(mapper);
4853
4854 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4855 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4856 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4857 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
4858 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4859 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4860 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4861 ASSERT_EQ(0, motionArgs.flags);
4862 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4863 ASSERT_EQ(0, motionArgs.buttonState);
4864 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004865 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Santos Cordonfa5cf462017-04-05 10:37:00 -07004866 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004867 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004868 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4869 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
4870 1, 0, 0, 0, 0, 0, 0, 0));
4871 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
4872 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
4873 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4874
4875 // Up.
4876 processUp(mapper);
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_UP, 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 // Should not have sent any more keys or motions.
4901 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4902 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4903}
4904
Michael Wrightd02c5b62014-02-10 15:10:22 -08004905TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004906 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00004907 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004908 prepareButtons();
4909 prepareAxes(POSITION);
4910 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00004911 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004912
arthurhungdcef2dc2020-08-11 14:47:50 +08004913 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004914
4915 NotifyMotionArgs motionArgs;
4916
4917 // Down.
4918 int32_t x = 100;
4919 int32_t y = 125;
4920 processDown(mapper, x, y);
4921 processSync(mapper);
4922
4923 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4924 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4925 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4926 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4927 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4928 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4929 ASSERT_EQ(0, motionArgs.flags);
4930 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4931 ASSERT_EQ(0, motionArgs.buttonState);
4932 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004933 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004934 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004935 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004936 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4937 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4938 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4939 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4940 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4941
4942 // Move.
4943 x += 50;
4944 y += 75;
4945 processMove(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_MOVE, 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 // Up.
4968 processUp(mapper);
4969 processSync(mapper);
4970
4971 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4972 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4973 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4974 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4975 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4976 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4977 ASSERT_EQ(0, motionArgs.flags);
4978 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4979 ASSERT_EQ(0, motionArgs.buttonState);
4980 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004981 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004982 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004983 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004984 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4985 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4986 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4987 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4988 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4989
4990 // Should not have sent any more keys or motions.
4991 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4992 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4993}
4994
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004995TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_DoesNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004996 addConfigurationProperty("touch.deviceType", "touchScreen");
4997 prepareButtons();
4998 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004999 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
5000 // need to be rotated. Touchscreens are orientation-aware by default.
Arpit Singha8c236b2023-04-25 13:56:05 +00005001 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005002
5003 NotifyMotionArgs args;
5004
5005 // Rotation 90.
Michael Wrighta9cf4192022-12-01 23:46:39 +00005006 prepareDisplay(ui::ROTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005007 processDown(mapper, toRawX(50), toRawY(75));
5008 processSync(mapper);
5009
5010 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5011 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5012 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5013
5014 processUp(mapper);
5015 processSync(mapper);
5016 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5017}
5018
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005019TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_RotatesMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005020 addConfigurationProperty("touch.deviceType", "touchScreen");
5021 prepareButtons();
5022 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005023 // Since InputReader works in the un-rotated coordinate space, only devices that are not
5024 // orientation-aware are affected by display rotation.
5025 addConfigurationProperty("touch.orientationAware", "0");
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 0.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005031 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005032 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005033 processDown(mapper, toRawX(50), toRawY(75));
5034 processSync(mapper);
5035
5036 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5037 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5038 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5039
5040 processUp(mapper);
5041 processSync(mapper);
5042 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5043
5044 // Rotation 90.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005045 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005046 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00005047 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005048 processSync(mapper);
5049
5050 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5051 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5052 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5053
5054 processUp(mapper);
5055 processSync(mapper);
5056 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5057
5058 // Rotation 180.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005059 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005060 prepareDisplay(ui::ROTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005061 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
5062 processSync(mapper);
5063
5064 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5065 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5066 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5067
5068 processUp(mapper);
5069 processSync(mapper);
5070 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5071
5072 // Rotation 270.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005073 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005074 prepareDisplay(ui::ROTATION_270);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00005075 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005076 processSync(mapper);
5077
5078 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5079 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5080 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5081
5082 processUp(mapper);
5083 processSync(mapper);
5084 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5085}
5086
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005087TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation0_RotatesMotions) {
5088 addConfigurationProperty("touch.deviceType", "touchScreen");
5089 prepareButtons();
5090 prepareAxes(POSITION);
5091 addConfigurationProperty("touch.orientationAware", "1");
5092 addConfigurationProperty("touch.orientation", "ORIENTATION_0");
5093 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005094 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +00005095 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005096 NotifyMotionArgs args;
5097
5098 // Orientation 0.
5099 processDown(mapper, toRawX(50), toRawY(75));
5100 processSync(mapper);
5101
5102 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5103 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5104 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5105
5106 processUp(mapper);
5107 processSync(mapper);
5108 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5109}
5110
5111TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation90_RotatesMotions) {
5112 addConfigurationProperty("touch.deviceType", "touchScreen");
5113 prepareButtons();
5114 prepareAxes(POSITION);
5115 addConfigurationProperty("touch.orientationAware", "1");
5116 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
5117 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005118 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +00005119 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005120 NotifyMotionArgs args;
5121
5122 // Orientation 90.
5123 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
5124 processSync(mapper);
5125
5126 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5127 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5128 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5129
5130 processUp(mapper);
5131 processSync(mapper);
5132 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5133}
5134
5135TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation180_RotatesMotions) {
5136 addConfigurationProperty("touch.deviceType", "touchScreen");
5137 prepareButtons();
5138 prepareAxes(POSITION);
5139 addConfigurationProperty("touch.orientationAware", "1");
5140 addConfigurationProperty("touch.orientation", "ORIENTATION_180");
5141 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005142 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +00005143 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005144 NotifyMotionArgs args;
5145
5146 // Orientation 180.
5147 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
5148 processSync(mapper);
5149
5150 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5151 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5152 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5153
5154 processUp(mapper);
5155 processSync(mapper);
5156 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5157}
5158
5159TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation270_RotatesMotions) {
5160 addConfigurationProperty("touch.deviceType", "touchScreen");
5161 prepareButtons();
5162 prepareAxes(POSITION);
5163 addConfigurationProperty("touch.orientationAware", "1");
5164 addConfigurationProperty("touch.orientation", "ORIENTATION_270");
5165 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005166 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +00005167 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005168 NotifyMotionArgs args;
5169
5170 // Orientation 270.
5171 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
5172 processSync(mapper);
5173
5174 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5175 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5176 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5177
5178 processUp(mapper);
5179 processSync(mapper);
5180 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5181}
5182
5183TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationSpecified_RotatesMotionWithDisplay) {
5184 addConfigurationProperty("touch.deviceType", "touchScreen");
5185 prepareButtons();
5186 prepareAxes(POSITION);
5187 // Since InputReader works in the un-rotated coordinate space, only devices that are not
5188 // orientation-aware are affected by display rotation.
5189 addConfigurationProperty("touch.orientationAware", "0");
5190 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
Arpit Singha8c236b2023-04-25 13:56:05 +00005191 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005192
5193 NotifyMotionArgs args;
5194
5195 // Orientation 90, Rotation 0.
5196 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005197 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005198 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
5199 processSync(mapper);
5200
5201 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5202 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5203 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5204
5205 processUp(mapper);
5206 processSync(mapper);
5207 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5208
5209 // Orientation 90, Rotation 90.
5210 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005211 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00005212 processDown(mapper, toRawX(50), toRawY(75));
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005213 processSync(mapper);
5214
5215 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5216 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5217 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5218
5219 processUp(mapper);
5220 processSync(mapper);
5221 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5222
5223 // Orientation 90, Rotation 180.
5224 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005225 prepareDisplay(ui::ROTATION_180);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005226 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
5227 processSync(mapper);
5228
5229 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5230 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5231 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5232
5233 processUp(mapper);
5234 processSync(mapper);
5235 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5236
5237 // Orientation 90, Rotation 270.
5238 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005239 prepareDisplay(ui::ROTATION_270);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00005240 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 -07005241 processSync(mapper);
5242
5243 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5244 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5245 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5246
5247 processUp(mapper);
5248 processSync(mapper);
5249 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5250}
5251
Prabir Pradhan675f25a2022-11-10 22:04:07 +00005252TEST_F(SingleTouchInputMapperTest, Process_IgnoresTouchesOutsidePhysicalFrame) {
5253 addConfigurationProperty("touch.deviceType", "touchScreen");
5254 prepareButtons();
5255 prepareAxes(POSITION);
5256 addConfigurationProperty("touch.orientationAware", "1");
5257 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +00005258 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan675f25a2022-11-10 22:04:07 +00005259
5260 // Set a physical frame in the display viewport.
5261 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
5262 viewport->physicalLeft = 20;
5263 viewport->physicalTop = 600;
5264 viewport->physicalRight = 30;
5265 viewport->physicalBottom = 610;
5266 mFakePolicy->updateViewport(*viewport);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00005267 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhan675f25a2022-11-10 22:04:07 +00005268
5269 // Start the touch.
5270 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
5271 processSync(mapper);
5272
5273 // Expect all input starting outside the physical frame to be ignored.
5274 const std::array<Point, 6> outsidePoints = {
5275 {{0, 0}, {19, 605}, {31, 605}, {25, 599}, {25, 611}, {DISPLAY_WIDTH, DISPLAY_HEIGHT}}};
5276 for (const auto& p : outsidePoints) {
5277 processMove(mapper, toRawX(p.x), toRawY(p.y));
5278 processSync(mapper);
5279 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5280 }
5281
5282 // Move the touch into the physical frame.
5283 processMove(mapper, toRawX(25), toRawY(605));
5284 processSync(mapper);
5285 NotifyMotionArgs args;
5286 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5287 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
5288 EXPECT_NEAR(25, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5289 EXPECT_NEAR(605, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5290
5291 // Once the touch down is reported, continue reporting input, even if it is outside the frame.
5292 for (const auto& p : outsidePoints) {
5293 processMove(mapper, toRawX(p.x), toRawY(p.y));
5294 processSync(mapper);
5295 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5296 EXPECT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
5297 EXPECT_NEAR(p.x, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5298 EXPECT_NEAR(p.y, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5299 }
5300
5301 processUp(mapper);
5302 processSync(mapper);
5303 EXPECT_NO_FATAL_FAILURE(
5304 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
5305}
5306
Harry Cutts1db43992023-06-19 17:05:07 +00005307TEST_F(SingleTouchInputMapperTest, Process_DoesntCheckPhysicalFrameForTouchpads) {
5308 std::shared_ptr<FakePointerController> fakePointerController =
5309 std::make_shared<FakePointerController>();
5310 mFakePolicy->setPointerController(fakePointerController);
5311
5312 addConfigurationProperty("touch.deviceType", "pointer");
5313 prepareAxes(POSITION);
5314 prepareDisplay(ui::ROTATION_0);
5315 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
5316
5317 // Set a physical frame in the display viewport.
5318 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
5319 viewport->physicalLeft = 20;
5320 viewport->physicalTop = 600;
5321 viewport->physicalRight = 30;
5322 viewport->physicalBottom = 610;
5323 mFakePolicy->updateViewport(*viewport);
5324 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
5325
5326 // Start the touch.
5327 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
5328 processSync(mapper);
5329
5330 // Expect all input starting outside the physical frame to result in NotifyMotionArgs being
5331 // produced.
5332 const std::array<Point, 6> outsidePoints = {
5333 {{0, 0}, {19, 605}, {31, 605}, {25, 599}, {25, 611}, {DISPLAY_WIDTH, DISPLAY_HEIGHT}}};
5334 for (const auto& p : outsidePoints) {
5335 processMove(mapper, toRawX(p.x), toRawY(p.y));
5336 processSync(mapper);
5337 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5338 }
5339}
5340
Michael Wrightd02c5b62014-02-10 15:10:22 -08005341TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005342 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005343 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005344 prepareButtons();
5345 prepareAxes(POSITION | PRESSURE | TOOL | DISTANCE | TILT);
Arpit Singha8c236b2023-04-25 13:56:05 +00005346 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005347
5348 // These calculations are based on the input device calibration documentation.
5349 int32_t rawX = 100;
5350 int32_t rawY = 200;
5351 int32_t rawPressure = 10;
5352 int32_t rawToolMajor = 12;
5353 int32_t rawDistance = 2;
5354 int32_t rawTiltX = 30;
5355 int32_t rawTiltY = 110;
5356
5357 float x = toDisplayX(rawX);
5358 float y = toDisplayY(rawY);
5359 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
5360 float size = float(rawToolMajor) / RAW_TOOL_MAX;
5361 float tool = float(rawToolMajor) * GEOMETRIC_SCALE;
5362 float distance = float(rawDistance);
5363
5364 float tiltCenter = (RAW_TILT_MAX + RAW_TILT_MIN) * 0.5f;
5365 float tiltScale = M_PI / 180;
5366 float tiltXAngle = (rawTiltX - tiltCenter) * tiltScale;
5367 float tiltYAngle = (rawTiltY - tiltCenter) * tiltScale;
5368 float orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
5369 float tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
5370
5371 processDown(mapper, rawX, rawY);
5372 processPressure(mapper, rawPressure);
5373 processToolMajor(mapper, rawToolMajor);
5374 processDistance(mapper, rawDistance);
5375 processTilt(mapper, rawTiltX, rawTiltY);
5376 processSync(mapper);
5377
5378 NotifyMotionArgs args;
5379 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5380 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5381 x, y, pressure, size, tool, tool, tool, tool, orientation, distance));
5382 ASSERT_EQ(tilt, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TILT));
5383}
5384
Jason Gerecke489fda82012-09-07 17:19:40 -07005385TEST_F(SingleTouchInputMapperTest, Process_XYAxes_AffineCalibration) {
Jason Gerecke489fda82012-09-07 17:19:40 -07005386 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005387 prepareDisplay(ui::ROTATION_0);
Jason Gerecke489fda82012-09-07 17:19:40 -07005388 prepareLocationCalibration();
5389 prepareButtons();
5390 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00005391 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Jason Gerecke489fda82012-09-07 17:19:40 -07005392
5393 int32_t rawX = 100;
5394 int32_t rawY = 200;
5395
5396 float x = toDisplayX(toCookedX(rawX, rawY));
5397 float y = toDisplayY(toCookedY(rawX, rawY));
5398
5399 processDown(mapper, rawX, rawY);
5400 processSync(mapper);
5401
5402 NotifyMotionArgs args;
5403 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5404 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5405 x, y, 1, 0, 0, 0, 0, 0, 0, 0));
5406}
5407
Michael Wrightd02c5b62014-02-10 15:10:22 -08005408TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005409 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005410 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005411 prepareButtons();
5412 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00005413 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005414
5415 NotifyMotionArgs motionArgs;
5416 NotifyKeyArgs keyArgs;
5417
5418 processDown(mapper, 100, 200);
5419 processSync(mapper);
5420 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5421 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5422 ASSERT_EQ(0, motionArgs.buttonState);
5423
5424 // press BTN_LEFT, release BTN_LEFT
5425 processKey(mapper, BTN_LEFT, 1);
5426 processSync(mapper);
5427 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5428 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5429 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
5430
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005431 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5432 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5433 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
5434
Michael Wrightd02c5b62014-02-10 15:10:22 -08005435 processKey(mapper, BTN_LEFT, 0);
5436 processSync(mapper);
5437 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005438 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005439 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005440
5441 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005442 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005443 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005444
5445 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
5446 processKey(mapper, BTN_RIGHT, 1);
5447 processKey(mapper, BTN_MIDDLE, 1);
5448 processSync(mapper);
5449 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5450 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5451 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5452 motionArgs.buttonState);
5453
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005454 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5455 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5456 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
5457
5458 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5459 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5460 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5461 motionArgs.buttonState);
5462
Michael Wrightd02c5b62014-02-10 15:10:22 -08005463 processKey(mapper, BTN_RIGHT, 0);
5464 processSync(mapper);
5465 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005466 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005467 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005468
5469 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005470 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005471 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005472
5473 processKey(mapper, BTN_MIDDLE, 0);
5474 processSync(mapper);
5475 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005476 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005477 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005478
5479 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005480 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005481 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005482
5483 // press BTN_BACK, release BTN_BACK
5484 processKey(mapper, BTN_BACK, 1);
5485 processSync(mapper);
5486 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5487 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5488 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005489
Michael Wrightd02c5b62014-02-10 15:10:22 -08005490 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005491 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005492 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5493
5494 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5495 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5496 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005497
5498 processKey(mapper, BTN_BACK, 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);
5507
Michael Wrightd02c5b62014-02-10 15:10:22 -08005508 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5509 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5510 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
5511
5512 // press BTN_SIDE, release BTN_SIDE
5513 processKey(mapper, BTN_SIDE, 1);
5514 processSync(mapper);
5515 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5516 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5517 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005518
Michael Wrightd02c5b62014-02-10 15:10:22 -08005519 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005520 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005521 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5522
5523 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5524 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5525 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005526
5527 processKey(mapper, BTN_SIDE, 0);
5528 processSync(mapper);
5529 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005530 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005531 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005532
5533 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005534 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005535 ASSERT_EQ(0, motionArgs.buttonState);
5536
Michael Wrightd02c5b62014-02-10 15:10:22 -08005537 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5538 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5539 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
5540
5541 // press BTN_FORWARD, release BTN_FORWARD
5542 processKey(mapper, BTN_FORWARD, 1);
5543 processSync(mapper);
5544 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5545 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5546 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005547
Michael Wrightd02c5b62014-02-10 15:10:22 -08005548 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005549 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005550 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5551
5552 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5553 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5554 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005555
5556 processKey(mapper, BTN_FORWARD, 0);
5557 processSync(mapper);
5558 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005559 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005560 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005561
5562 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005563 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005564 ASSERT_EQ(0, motionArgs.buttonState);
5565
Michael Wrightd02c5b62014-02-10 15:10:22 -08005566 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5567 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5568 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
5569
5570 // press BTN_EXTRA, release BTN_EXTRA
5571 processKey(mapper, BTN_EXTRA, 1);
5572 processSync(mapper);
5573 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5574 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5575 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005576
Michael Wrightd02c5b62014-02-10 15:10:22 -08005577 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005578 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005579 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5580
5581 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5582 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5583 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005584
5585 processKey(mapper, BTN_EXTRA, 0);
5586 processSync(mapper);
5587 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005588 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005589 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005590
5591 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005592 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005593 ASSERT_EQ(0, motionArgs.buttonState);
5594
Michael Wrightd02c5b62014-02-10 15:10:22 -08005595 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5596 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5597 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
5598
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005599 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5600
Michael Wrightd02c5b62014-02-10 15:10:22 -08005601 // press BTN_STYLUS, release BTN_STYLUS
5602 processKey(mapper, BTN_STYLUS, 1);
5603 processSync(mapper);
5604 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5605 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005606 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
5607
5608 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5609 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5610 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005611
5612 processKey(mapper, BTN_STYLUS, 0);
5613 processSync(mapper);
5614 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005615 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005616 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005617
5618 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005619 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005620 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005621
5622 // press BTN_STYLUS2, release BTN_STYLUS2
5623 processKey(mapper, BTN_STYLUS2, 1);
5624 processSync(mapper);
5625 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5626 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005627 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
5628
5629 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5630 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5631 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005632
5633 processKey(mapper, BTN_STYLUS2, 0);
5634 processSync(mapper);
5635 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005636 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005637 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005638
5639 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005640 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005641 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005642
5643 // release touch
5644 processUp(mapper);
5645 processSync(mapper);
5646 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5647 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5648 ASSERT_EQ(0, motionArgs.buttonState);
5649}
5650
5651TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005652 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005653 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005654 prepareButtons();
5655 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00005656 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005657
5658 NotifyMotionArgs motionArgs;
5659
5660 // default tool type is finger
5661 processDown(mapper, 100, 200);
5662 processSync(mapper);
5663 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5664 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005665 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005666
5667 // eraser
5668 processKey(mapper, BTN_TOOL_RUBBER, 1);
5669 processSync(mapper);
5670 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5671 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005672 ASSERT_EQ(ToolType::ERASER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005673
5674 // stylus
5675 processKey(mapper, BTN_TOOL_RUBBER, 0);
5676 processKey(mapper, BTN_TOOL_PEN, 1);
5677 processSync(mapper);
5678 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5679 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005680 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005681
5682 // brush
5683 processKey(mapper, BTN_TOOL_PEN, 0);
5684 processKey(mapper, BTN_TOOL_BRUSH, 1);
5685 processSync(mapper);
5686 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5687 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005688 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005689
5690 // pencil
5691 processKey(mapper, BTN_TOOL_BRUSH, 0);
5692 processKey(mapper, BTN_TOOL_PENCIL, 1);
5693 processSync(mapper);
5694 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5695 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005696 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005697
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08005698 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08005699 processKey(mapper, BTN_TOOL_PENCIL, 0);
5700 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
5701 processSync(mapper);
5702 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5703 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005704 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005705
5706 // mouse
5707 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
5708 processKey(mapper, BTN_TOOL_MOUSE, 1);
5709 processSync(mapper);
5710 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5711 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005712 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005713
5714 // lens
5715 processKey(mapper, BTN_TOOL_MOUSE, 0);
5716 processKey(mapper, BTN_TOOL_LENS, 1);
5717 processSync(mapper);
5718 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5719 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005720 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005721
5722 // double-tap
5723 processKey(mapper, BTN_TOOL_LENS, 0);
5724 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
5725 processSync(mapper);
5726 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5727 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005728 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005729
5730 // triple-tap
5731 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
5732 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
5733 processSync(mapper);
5734 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5735 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005736 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005737
5738 // quad-tap
5739 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
5740 processKey(mapper, BTN_TOOL_QUADTAP, 1);
5741 processSync(mapper);
5742 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5743 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005744 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005745
5746 // finger
5747 processKey(mapper, BTN_TOOL_QUADTAP, 0);
5748 processKey(mapper, BTN_TOOL_FINGER, 1);
5749 processSync(mapper);
5750 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5751 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005752 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005753
5754 // stylus trumps finger
5755 processKey(mapper, BTN_TOOL_PEN, 1);
5756 processSync(mapper);
5757 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5758 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005759 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005760
5761 // eraser trumps stylus
5762 processKey(mapper, BTN_TOOL_RUBBER, 1);
5763 processSync(mapper);
5764 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5765 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005766 ASSERT_EQ(ToolType::ERASER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005767
5768 // mouse trumps eraser
5769 processKey(mapper, BTN_TOOL_MOUSE, 1);
5770 processSync(mapper);
5771 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5772 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005773 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005774
5775 // back to default tool type
5776 processKey(mapper, BTN_TOOL_MOUSE, 0);
5777 processKey(mapper, BTN_TOOL_RUBBER, 0);
5778 processKey(mapper, BTN_TOOL_PEN, 0);
5779 processKey(mapper, BTN_TOOL_FINGER, 0);
5780 processSync(mapper);
5781 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5782 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005783 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005784}
5785
5786TEST_F(SingleTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005787 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005788 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005789 prepareButtons();
5790 prepareAxes(POSITION);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005791 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_FINGER, 0, AKEYCODE_UNKNOWN, 0);
Arpit Singha8c236b2023-04-25 13:56:05 +00005792 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005793
5794 NotifyMotionArgs motionArgs;
5795
5796 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
5797 processKey(mapper, BTN_TOOL_FINGER, 1);
5798 processMove(mapper, 100, 200);
5799 processSync(mapper);
5800 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5801 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5802 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5803 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5804
5805 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5806 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5807 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5808 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5809
5810 // move a little
5811 processMove(mapper, 150, 250);
5812 processSync(mapper);
5813 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5814 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5815 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5816 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5817
5818 // down when BTN_TOUCH is pressed, pressure defaults to 1
5819 processKey(mapper, BTN_TOUCH, 1);
5820 processSync(mapper);
5821 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5822 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5823 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5824 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5825
5826 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5827 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5828 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5829 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5830
5831 // up when BTN_TOUCH is released, hover restored
5832 processKey(mapper, BTN_TOUCH, 0);
5833 processSync(mapper);
5834 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5835 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5836 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5837 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5838
5839 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5840 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5841 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5842 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5843
5844 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5845 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5846 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5847 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5848
5849 // exit hover when pointer goes away
5850 processKey(mapper, BTN_TOOL_FINGER, 0);
5851 processSync(mapper);
5852 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5853 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5854 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5855 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5856}
5857
5858TEST_F(SingleTouchInputMapperTest, Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005859 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005860 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005861 prepareButtons();
5862 prepareAxes(POSITION | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +00005863 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005864
5865 NotifyMotionArgs motionArgs;
5866
5867 // initially hovering because pressure is 0
5868 processDown(mapper, 100, 200);
5869 processPressure(mapper, 0);
5870 processSync(mapper);
5871 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5872 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5873 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5874 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5875
5876 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5877 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5878 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5879 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5880
5881 // move a little
5882 processMove(mapper, 150, 250);
5883 processSync(mapper);
5884 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5885 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5886 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5887 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5888
5889 // down when pressure is non-zero
5890 processPressure(mapper, RAW_PRESSURE_MAX);
5891 processSync(mapper);
5892 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5893 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5894 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5895 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5896
5897 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5898 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5899 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5900 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5901
5902 // up when pressure becomes 0, hover restored
5903 processPressure(mapper, 0);
5904 processSync(mapper);
5905 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5906 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5907 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5908 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5909
5910 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5911 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5912 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5913 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5914
5915 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5916 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5917 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5918 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5919
5920 // exit hover when pointer goes away
5921 processUp(mapper);
5922 processSync(mapper);
5923 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5924 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5925 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5926 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5927}
5928
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00005929TEST_F(SingleTouchInputMapperTest, Reset_CancelsOngoingGesture) {
5930 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005931 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00005932 prepareButtons();
5933 prepareAxes(POSITION | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +00005934 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00005935
5936 // Touch down.
5937 processDown(mapper, 100, 200);
5938 processPressure(mapper, 1);
5939 processSync(mapper);
5940 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
5941 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
5942
5943 // Reset the mapper. This should cancel the ongoing gesture.
5944 resetMapper(mapper, ARBITRARY_TIME);
5945 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
5946 WithMotionAction(AMOTION_EVENT_ACTION_CANCEL)));
5947
5948 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5949}
5950
Prabir Pradhanafabcde2022-09-27 19:32:43 +00005951TEST_F(SingleTouchInputMapperTest, Reset_RecreatesTouchState) {
5952 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005953 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00005954 prepareButtons();
5955 prepareAxes(POSITION | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +00005956 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanafabcde2022-09-27 19:32:43 +00005957
5958 // Set the initial state for the touch pointer.
5959 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_X, 100);
5960 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_Y, 200);
5961 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MAX);
5962 mFakeEventHub->setScanCodeState(EVENTHUB_ID, BTN_TOUCH, 1);
5963
5964 // Reset the mapper. When the mapper is reset, we expect it to attempt to recreate the touch
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00005965 // state by reading the current axis values. Since there was no ongoing gesture, calling reset
5966 // does not generate any events.
5967 resetMapper(mapper, ARBITRARY_TIME);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00005968
5969 // Send a sync to simulate an empty touch frame where nothing changes. The mapper should use
5970 // the recreated touch state to generate a down event.
5971 processSync(mapper);
5972 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
5973 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithPressure(1.f))));
5974
5975 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5976}
5977
lilinnan687e58f2022-07-19 16:00:50 +08005978TEST_F(SingleTouchInputMapperTest,
5979 Process_WhenViewportDisplayIdChanged_TouchIsCanceledAndDeviceIsReset) {
5980 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005981 prepareDisplay(ui::ROTATION_0);
lilinnan687e58f2022-07-19 16:00:50 +08005982 prepareButtons();
5983 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00005984 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
lilinnan687e58f2022-07-19 16:00:50 +08005985 NotifyMotionArgs motionArgs;
5986
5987 // Down.
Prabir Pradhan3e5ec702022-07-29 16:26:24 +00005988 processDown(mapper, 100, 200);
lilinnan687e58f2022-07-19 16:00:50 +08005989 processSync(mapper);
5990
5991 // We should receive a down event
5992 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5993 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5994
5995 // Change display id
5996 clearViewports();
5997 prepareSecondaryDisplay(ViewportType::INTERNAL);
5998
5999 // We should receive a cancel event
6000 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6001 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
6002 // Then receive reset called
6003 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6004}
6005
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006006TEST_F(SingleTouchInputMapperTest,
6007 Process_WhenViewportActiveStatusChanged_TouchIsCanceledAndDeviceIsReset) {
6008 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006009 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006010 prepareButtons();
6011 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00006012 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006013 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6014 NotifyMotionArgs motionArgs;
6015
6016 // Start a new gesture.
6017 processDown(mapper, 100, 200);
6018 processSync(mapper);
6019 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6020 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6021
6022 // Make the viewport inactive. This will put the device in disabled mode.
6023 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6024 viewport->isActive = false;
6025 mFakePolicy->updateViewport(*viewport);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00006026 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006027
6028 // We should receive a cancel event for the ongoing gesture.
6029 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6030 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
6031 // Then we should be notified that the device was reset.
6032 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6033
6034 // No events are generated while the viewport is inactive.
6035 processMove(mapper, 101, 201);
6036 processSync(mapper);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006037 processUp(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006038 processSync(mapper);
6039 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6040
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006041 // Start a new gesture while the viewport is still inactive.
6042 processDown(mapper, 300, 400);
6043 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_X, 300);
6044 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_Y, 400);
6045 mFakeEventHub->setScanCodeState(EVENTHUB_ID, BTN_TOUCH, 1);
6046 processSync(mapper);
6047
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006048 // Make the viewport active again. The device should resume processing events.
6049 viewport->isActive = true;
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 // The device is reset because it changes back to direct mode, without generating any events.
6054 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6055 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6056
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006057 // In the next sync, the touch state that was recreated when the device was reset is reported.
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006058 processSync(mapper);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006059 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6060 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006061
6062 // No more events.
6063 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6064 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
6065}
6066
Prabir Pradhan211ba622022-10-31 21:09:21 +00006067TEST_F(SingleTouchInputMapperTest, ButtonIsReleasedOnTouchUp) {
6068 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006069 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan211ba622022-10-31 21:09:21 +00006070 prepareButtons();
6071 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00006072 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan211ba622022-10-31 21:09:21 +00006073 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6074
6075 // Press a stylus button.
6076 processKey(mapper, BTN_STYLUS, 1);
6077 processSync(mapper);
6078
6079 // Start a touch gesture and ensure the BUTTON_PRESS event is generated.
6080 processDown(mapper, 100, 200);
6081 processSync(mapper);
6082 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6083 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
6084 WithCoords(toDisplayX(100), toDisplayY(200)),
6085 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
6086 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6087 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
6088 WithCoords(toDisplayX(100), toDisplayY(200)),
6089 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
6090
6091 // Release the touch gesture. Ensure that the BUTTON_RELEASE event is generated even though
6092 // the button has not actually been released, since there will be no pointers through which the
6093 // button state can be reported. The event is generated at the location of the pointer before
6094 // it went up.
6095 processUp(mapper);
6096 processSync(mapper);
6097 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6098 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
6099 WithCoords(toDisplayX(100), toDisplayY(200)), WithButtonState(0))));
6100 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6101 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
6102 WithCoords(toDisplayX(100), toDisplayY(200)), WithButtonState(0))));
6103}
6104
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00006105TEST_F(SingleTouchInputMapperTest, StylusButtonMotionEventsDisabled) {
6106 addConfigurationProperty("touch.deviceType", "touchScreen");
6107 prepareDisplay(ui::ROTATION_0);
6108 prepareButtons();
6109 prepareAxes(POSITION);
6110
6111 mFakePolicy->setStylusButtonMotionEventsEnabled(false);
6112
Arpit Singha8c236b2023-04-25 13:56:05 +00006113 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00006114 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6115
6116 // Press a stylus button.
6117 processKey(mapper, BTN_STYLUS, 1);
6118 processSync(mapper);
6119
6120 // Start a touch gesture and ensure that the stylus button is not reported.
6121 processDown(mapper, 100, 200);
6122 processSync(mapper);
6123 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6124 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithButtonState(0))));
6125
6126 // Release and press the stylus button again.
6127 processKey(mapper, BTN_STYLUS, 0);
6128 processSync(mapper);
6129 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6130 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
6131 processKey(mapper, BTN_STYLUS, 1);
6132 processSync(mapper);
6133 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6134 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
6135
6136 // Release the touch gesture.
6137 processUp(mapper);
6138 processSync(mapper);
6139 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6140 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithButtonState(0))));
6141
6142 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6143}
6144
Ambrus Weisz7bc23bf2022-10-04 13:13:07 +00006145TEST_F(SingleTouchInputMapperTest, WhenDeviceTypeIsSetToTouchNavigation_setsCorrectType) {
6146 mFakePolicy->addDeviceTypeAssociation(DEVICE_LOCATION, "touchNavigation");
6147 prepareDisplay(ui::ROTATION_0);
6148 prepareButtons();
6149 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00006150 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Ambrus Weisz7bc23bf2022-10-04 13:13:07 +00006151 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6152
6153 ASSERT_EQ(AINPUT_SOURCE_TOUCH_NAVIGATION, mapper.getSources());
6154}
6155
Seunghwan Choi356026c2023-02-01 14:37:25 +09006156TEST_F(SingleTouchInputMapperTest, Process_WhenConfigEnabled_ShouldShowDirectStylusPointer) {
6157 std::shared_ptr<FakePointerController> fakePointerController =
6158 std::make_shared<FakePointerController>();
6159 addConfigurationProperty("touch.deviceType", "touchScreen");
6160 prepareDisplay(ui::ROTATION_0);
6161 prepareButtons();
6162 prepareAxes(POSITION);
6163 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
6164 mFakePolicy->setPointerController(fakePointerController);
6165 mFakePolicy->setStylusPointerIconEnabled(true);
Arpit Singha8c236b2023-04-25 13:56:05 +00006166 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Seunghwan Choi356026c2023-02-01 14:37:25 +09006167
6168 processKey(mapper, BTN_TOOL_PEN, 1);
6169 processMove(mapper, 100, 200);
6170 processSync(mapper);
6171 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6172 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006173 WithToolType(ToolType::STYLUS),
Seunghwan Choi356026c2023-02-01 14:37:25 +09006174 WithPointerCoords(0, toDisplayX(100), toDisplayY(200)))));
6175 ASSERT_TRUE(fakePointerController->isPointerShown());
6176 ASSERT_NO_FATAL_FAILURE(
6177 fakePointerController->assertPosition(toDisplayX(100), toDisplayY(200)));
6178}
6179
6180TEST_F(SingleTouchInputMapperTest, Process_WhenConfigDisabled_ShouldNotShowDirectStylusPointer) {
6181 std::shared_ptr<FakePointerController> fakePointerController =
6182 std::make_shared<FakePointerController>();
6183 addConfigurationProperty("touch.deviceType", "touchScreen");
6184 prepareDisplay(ui::ROTATION_0);
6185 prepareButtons();
6186 prepareAxes(POSITION);
6187 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
6188 mFakePolicy->setPointerController(fakePointerController);
6189 mFakePolicy->setStylusPointerIconEnabled(false);
Arpit Singha8c236b2023-04-25 13:56:05 +00006190 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Seunghwan Choi356026c2023-02-01 14:37:25 +09006191
6192 processKey(mapper, BTN_TOOL_PEN, 1);
6193 processMove(mapper, 100, 200);
6194 processSync(mapper);
6195 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6196 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006197 WithToolType(ToolType::STYLUS),
Seunghwan Choi356026c2023-02-01 14:37:25 +09006198 WithPointerCoords(0, toDisplayX(100), toDisplayY(200)))));
6199 ASSERT_FALSE(fakePointerController->isPointerShown());
6200}
6201
Ambrus Weisz7b6e16b2022-12-16 17:54:57 +00006202TEST_F(SingleTouchInputMapperTest, WhenDeviceTypeIsChangedToTouchNavigation_updatesDeviceType) {
6203 // Initialize the device without setting device source to touch navigation.
6204 addConfigurationProperty("touch.deviceType", "touchScreen");
6205 prepareDisplay(ui::ROTATION_0);
6206 prepareButtons();
6207 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00006208 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Ambrus Weisz7b6e16b2022-12-16 17:54:57 +00006209
6210 // Ensure that the device is created as a touchscreen, not touch navigation.
6211 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
6212
6213 // Add device type association after the device was created.
6214 mFakePolicy->addDeviceTypeAssociation(DEVICE_LOCATION, "touchNavigation");
6215
6216 // Send update to the mapper.
6217 std::list<NotifyArgs> unused2 =
6218 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00006219 InputReaderConfiguration::Change::DEVICE_TYPE /*changes*/);
Ambrus Weisz7b6e16b2022-12-16 17:54:57 +00006220
6221 // Check whether device type update was successful.
6222 ASSERT_EQ(AINPUT_SOURCE_TOUCH_NAVIGATION, mDevice->getSources());
6223}
6224
Prabir Pradhane1e309a2022-11-29 02:54:27 +00006225TEST_F(SingleTouchInputMapperTest, HoverEventsOutsidePhysicalFrameAreIgnored) {
6226 // Initialize the device without setting device source to touch navigation.
6227 addConfigurationProperty("touch.deviceType", "touchScreen");
6228 prepareDisplay(ui::ROTATION_0);
6229 prepareButtons();
6230 prepareAxes(POSITION);
6231 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
6232
6233 // Set a physical frame in the display viewport.
6234 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6235 viewport->physicalLeft = 0;
6236 viewport->physicalTop = 0;
6237 viewport->physicalRight = DISPLAY_WIDTH / 2;
6238 viewport->physicalBottom = DISPLAY_HEIGHT / 2;
6239 mFakePolicy->updateViewport(*viewport);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00006240 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhane1e309a2022-11-29 02:54:27 +00006241
Arpit Singha8c236b2023-04-25 13:56:05 +00006242 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhane1e309a2022-11-29 02:54:27 +00006243
6244 // Hovering inside the physical frame produces events.
6245 processKey(mapper, BTN_TOOL_PEN, 1);
6246 processMove(mapper, RAW_X_MIN + 1, RAW_Y_MIN + 1);
6247 processSync(mapper);
6248 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6249 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)));
6250 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6251 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE)));
6252
6253 // Leaving the physical frame ends the hovering gesture.
6254 processMove(mapper, RAW_X_MAX - 1, RAW_Y_MAX - 1);
6255 processSync(mapper);
6256 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6257 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT)));
6258
6259 // Moving outside the physical frame does not produce events.
6260 processMove(mapper, RAW_X_MAX - 2, RAW_Y_MAX - 2);
6261 processSync(mapper);
6262 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6263
6264 // Re-entering the physical frame produces events.
6265 processMove(mapper, RAW_X_MIN, RAW_Y_MIN);
6266 processSync(mapper);
6267 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6268 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)));
6269 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6270 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE)));
6271}
6272
Prabir Pradhan5632d622021-09-06 07:57:20 -07006273// --- TouchDisplayProjectionTest ---
6274
6275class TouchDisplayProjectionTest : public SingleTouchInputMapperTest {
6276public:
6277 // The values inside DisplayViewport are expected to be pre-rotated. This updates the current
6278 // DisplayViewport to pre-rotate the values. The viewport's physical display will be set to the
6279 // rotated equivalent of the given un-rotated physical display bounds.
Prabir Pradhana9df3162022-12-05 23:57:27 +00006280 void configurePhysicalDisplay(ui::Rotation orientation, Rect naturalPhysicalDisplay,
6281 int32_t naturalDisplayWidth = DISPLAY_WIDTH,
6282 int32_t naturalDisplayHeight = DISPLAY_HEIGHT) {
Prabir Pradhan5632d622021-09-06 07:57:20 -07006283 uint32_t inverseRotationFlags;
Prabir Pradhana9df3162022-12-05 23:57:27 +00006284 auto rotatedWidth = naturalDisplayWidth;
6285 auto rotatedHeight = naturalDisplayHeight;
Prabir Pradhan5632d622021-09-06 07:57:20 -07006286 switch (orientation) {
Michael Wrighta9cf4192022-12-01 23:46:39 +00006287 case ui::ROTATION_90:
Prabir Pradhan5632d622021-09-06 07:57:20 -07006288 inverseRotationFlags = ui::Transform::ROT_270;
Prabir Pradhana9df3162022-12-05 23:57:27 +00006289 std::swap(rotatedWidth, rotatedHeight);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006290 break;
Michael Wrighta9cf4192022-12-01 23:46:39 +00006291 case ui::ROTATION_180:
Prabir Pradhan5632d622021-09-06 07:57:20 -07006292 inverseRotationFlags = ui::Transform::ROT_180;
6293 break;
Michael Wrighta9cf4192022-12-01 23:46:39 +00006294 case ui::ROTATION_270:
Prabir Pradhan5632d622021-09-06 07:57:20 -07006295 inverseRotationFlags = ui::Transform::ROT_90;
Prabir Pradhana9df3162022-12-05 23:57:27 +00006296 std::swap(rotatedWidth, rotatedHeight);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006297 break;
Michael Wrighta9cf4192022-12-01 23:46:39 +00006298 case ui::ROTATION_0:
Prabir Pradhan5632d622021-09-06 07:57:20 -07006299 inverseRotationFlags = ui::Transform::ROT_0;
6300 break;
Prabir Pradhan5632d622021-09-06 07:57:20 -07006301 }
6302
Prabir Pradhana9df3162022-12-05 23:57:27 +00006303 const ui::Transform rotation(inverseRotationFlags, rotatedWidth, rotatedHeight);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006304 const Rect rotatedPhysicalDisplay = rotation.transform(naturalPhysicalDisplay);
6305
6306 std::optional<DisplayViewport> internalViewport =
6307 *mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6308 DisplayViewport& v = *internalViewport;
6309 v.displayId = DISPLAY_ID;
6310 v.orientation = orientation;
6311
6312 v.logicalLeft = 0;
6313 v.logicalTop = 0;
6314 v.logicalRight = 100;
6315 v.logicalBottom = 100;
6316
6317 v.physicalLeft = rotatedPhysicalDisplay.left;
6318 v.physicalTop = rotatedPhysicalDisplay.top;
6319 v.physicalRight = rotatedPhysicalDisplay.right;
6320 v.physicalBottom = rotatedPhysicalDisplay.bottom;
6321
Prabir Pradhana9df3162022-12-05 23:57:27 +00006322 v.deviceWidth = rotatedWidth;
6323 v.deviceHeight = rotatedHeight;
Prabir Pradhan5632d622021-09-06 07:57:20 -07006324
6325 v.isActive = true;
6326 v.uniqueId = UNIQUE_ID;
6327 v.type = ViewportType::INTERNAL;
6328 mFakePolicy->updateViewport(v);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00006329 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006330 }
6331
6332 void assertReceivedMove(const Point& point) {
6333 NotifyMotionArgs motionArgs;
6334 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6335 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07006336 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Prabir Pradhan5632d622021-09-06 07:57:20 -07006337 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], point.x, point.y,
6338 1, 0, 0, 0, 0, 0, 0, 0));
6339 }
6340};
6341
6342TEST_F(TouchDisplayProjectionTest, IgnoresTouchesOutsidePhysicalDisplay) {
6343 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006344 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006345
6346 prepareButtons();
6347 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00006348 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan5632d622021-09-06 07:57:20 -07006349
6350 NotifyMotionArgs motionArgs;
6351
6352 // Configure the DisplayViewport such that the logical display maps to a subsection of
6353 // the display panel called the physical display. Here, the physical display is bounded by the
6354 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
6355 static const Rect kPhysicalDisplay{10, 20, 70, 160};
6356 static const std::array<Point, 6> kPointsOutsidePhysicalDisplay{
6357 {{-10, -10}, {0, 0}, {5, 100}, {50, 15}, {75, 100}, {50, 165}}};
6358
Michael Wrighta9cf4192022-12-01 23:46:39 +00006359 for (auto orientation : {ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180, ui::ROTATION_270}) {
Prabir Pradhan5632d622021-09-06 07:57:20 -07006360 configurePhysicalDisplay(orientation, kPhysicalDisplay);
6361
6362 // Touches outside the physical display should be ignored, and should not generate any
6363 // events. Ensure touches at the following points that lie outside of the physical display
6364 // area do not generate any events.
6365 for (const auto& point : kPointsOutsidePhysicalDisplay) {
6366 processDown(mapper, toRawX(point.x), toRawY(point.y));
6367 processSync(mapper);
6368 processUp(mapper);
6369 processSync(mapper);
6370 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled())
6371 << "Unexpected event generated for touch outside physical display at point: "
6372 << point.x << ", " << point.y;
6373 }
6374 }
6375}
6376
6377TEST_F(TouchDisplayProjectionTest, EmitsTouchDownAfterEnteringPhysicalDisplay) {
6378 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006379 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006380
6381 prepareButtons();
6382 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00006383 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan5632d622021-09-06 07:57:20 -07006384
6385 NotifyMotionArgs motionArgs;
6386
6387 // Configure the DisplayViewport such that the logical display maps to a subsection of
6388 // the display panel called the physical display. Here, the physical display is bounded by the
6389 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
6390 static const Rect kPhysicalDisplay{10, 20, 70, 160};
6391
Michael Wrighta9cf4192022-12-01 23:46:39 +00006392 for (auto orientation : {ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180, ui::ROTATION_270}) {
Prabir Pradhan5632d622021-09-06 07:57:20 -07006393 configurePhysicalDisplay(orientation, kPhysicalDisplay);
6394
6395 // Touches that start outside the physical display should be ignored until it enters the
6396 // physical display bounds, at which point it should generate a down event. Start a touch at
6397 // the point (5, 100), which is outside the physical display bounds.
6398 static const Point kOutsidePoint{5, 100};
6399 processDown(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
6400 processSync(mapper);
6401 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6402
6403 // Move the touch into the physical display area. This should generate a pointer down.
6404 processMove(mapper, toRawX(11), toRawY(21));
6405 processSync(mapper);
6406 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6407 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07006408 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Prabir Pradhan5632d622021-09-06 07:57:20 -07006409 ASSERT_NO_FATAL_FAILURE(
6410 assertPointerCoords(motionArgs.pointerCoords[0], 11, 21, 1, 0, 0, 0, 0, 0, 0, 0));
6411
6412 // Move the touch inside the physical display area. This should generate a pointer move.
6413 processMove(mapper, toRawX(69), toRawY(159));
6414 processSync(mapper);
6415 assertReceivedMove({69, 159});
6416
6417 // Move outside the physical display area. Since the pointer is already down, this should
6418 // now continue generating events.
6419 processMove(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
6420 processSync(mapper);
6421 assertReceivedMove(kOutsidePoint);
6422
6423 // Release. This should generate a pointer up.
6424 processUp(mapper);
6425 processSync(mapper);
6426 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6427 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6428 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], kOutsidePoint.x,
6429 kOutsidePoint.y, 1, 0, 0, 0, 0, 0, 0, 0));
6430
6431 // Ensure no more events were generated.
6432 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6433 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6434 }
6435}
6436
Prabir Pradhana9df3162022-12-05 23:57:27 +00006437// --- TouchscreenPrecisionTests ---
6438
6439// This test suite is used to ensure that touchscreen devices are scaled and configured correctly
6440// in various orientations and with different display rotations. We configure the touchscreen to
6441// have a higher resolution than that of the display by an integer scale factor in each axis so that
6442// we can enforce that coordinates match precisely as expected.
6443class TouchscreenPrecisionTestsFixture : public TouchDisplayProjectionTest,
6444 public ::testing::WithParamInterface<ui::Rotation> {
6445public:
6446 void SetUp() override {
6447 SingleTouchInputMapperTest::SetUp();
6448
6449 // Prepare the raw axes to have twice the resolution of the display in the X axis and
6450 // four times the resolution of the display in the Y axis.
6451 prepareButtons();
6452 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, PRECISION_RAW_X_MIN, PRECISION_RAW_X_MAX,
Prabir Pradhan46211fb2022-12-17 00:30:39 +00006453 PRECISION_RAW_X_FLAT, PRECISION_RAW_X_FUZZ,
6454 PRECISION_RAW_X_RES);
Prabir Pradhana9df3162022-12-05 23:57:27 +00006455 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, PRECISION_RAW_Y_MIN, PRECISION_RAW_Y_MAX,
Prabir Pradhan46211fb2022-12-17 00:30:39 +00006456 PRECISION_RAW_Y_FLAT, PRECISION_RAW_Y_FUZZ,
6457 PRECISION_RAW_Y_RES);
Prabir Pradhana9df3162022-12-05 23:57:27 +00006458 }
6459
6460 static const int32_t PRECISION_RAW_X_MIN = TouchInputMapperTest::RAW_X_MIN;
6461 static const int32_t PRECISION_RAW_X_MAX = PRECISION_RAW_X_MIN + DISPLAY_WIDTH * 2 - 1;
6462 static const int32_t PRECISION_RAW_Y_MIN = TouchInputMapperTest::RAW_Y_MIN;
6463 static const int32_t PRECISION_RAW_Y_MAX = PRECISION_RAW_Y_MIN + DISPLAY_HEIGHT * 4 - 1;
6464
Prabir Pradhan46211fb2022-12-17 00:30:39 +00006465 static const int32_t PRECISION_RAW_X_RES = 50; // units per millimeter
6466 static const int32_t PRECISION_RAW_Y_RES = 100; // units per millimeter
6467
6468 static const int32_t PRECISION_RAW_X_FLAT = 16;
6469 static const int32_t PRECISION_RAW_Y_FLAT = 32;
6470
6471 static const int32_t PRECISION_RAW_X_FUZZ = 4;
6472 static const int32_t PRECISION_RAW_Y_FUZZ = 8;
6473
Prabir Pradhana9df3162022-12-05 23:57:27 +00006474 static const std::array<Point, 4> kRawCorners;
6475};
6476
6477const std::array<Point, 4> TouchscreenPrecisionTestsFixture::kRawCorners = {{
6478 {PRECISION_RAW_X_MIN, PRECISION_RAW_Y_MIN}, // left-top
6479 {PRECISION_RAW_X_MAX, PRECISION_RAW_Y_MIN}, // right-top
6480 {PRECISION_RAW_X_MAX, PRECISION_RAW_Y_MAX}, // right-bottom
6481 {PRECISION_RAW_X_MIN, PRECISION_RAW_Y_MAX}, // left-bottom
6482}};
6483
6484// Tests for how the touchscreen is oriented relative to the natural orientation of the display.
6485// For example, if a touchscreen is configured with an orientation of 90 degrees, it is a portrait
6486// touchscreen panel that is used on a device whose natural display orientation is in landscape.
6487TEST_P(TouchscreenPrecisionTestsFixture, OrientationPrecision) {
6488 enum class Orientation {
6489 ORIENTATION_0 = ui::toRotationInt(ui::ROTATION_0),
6490 ORIENTATION_90 = ui::toRotationInt(ui::ROTATION_90),
6491 ORIENTATION_180 = ui::toRotationInt(ui::ROTATION_180),
6492 ORIENTATION_270 = ui::toRotationInt(ui::ROTATION_270),
6493 ftl_last = ORIENTATION_270,
6494 };
6495 using Orientation::ORIENTATION_0, Orientation::ORIENTATION_90, Orientation::ORIENTATION_180,
6496 Orientation::ORIENTATION_270;
6497 static const std::map<Orientation, std::array<vec2, 4> /*mappedCorners*/> kMappedCorners = {
6498 {ORIENTATION_0, {{{0, 0}, {479.5, 0}, {479.5, 799.75}, {0, 799.75}}}},
6499 {ORIENTATION_90, {{{0, 479.5}, {0, 0}, {799.75, 0}, {799.75, 479.5}}}},
6500 {ORIENTATION_180, {{{479.5, 799.75}, {0, 799.75}, {0, 0}, {479.5, 0}}}},
6501 {ORIENTATION_270, {{{799.75, 0}, {799.75, 479.5}, {0, 479.5}, {0, 0}}}},
6502 };
6503
6504 const auto touchscreenOrientation = static_cast<Orientation>(ui::toRotationInt(GetParam()));
6505
6506 // Configure the touchscreen as being installed in the one of the four different orientations
6507 // relative to the display.
6508 addConfigurationProperty("touch.deviceType", "touchScreen");
6509 addConfigurationProperty("touch.orientation", ftl::enum_string(touchscreenOrientation).c_str());
6510 prepareDisplay(ui::ROTATION_0);
6511
Arpit Singha8c236b2023-04-25 13:56:05 +00006512 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhana9df3162022-12-05 23:57:27 +00006513
6514 // If the touchscreen is installed in a rotated orientation relative to the display (i.e. in
6515 // orientations of either 90 or 270) this means the display's natural resolution will be
6516 // flipped.
6517 const bool displayRotated =
6518 touchscreenOrientation == ORIENTATION_90 || touchscreenOrientation == ORIENTATION_270;
6519 const int32_t width = displayRotated ? DISPLAY_HEIGHT : DISPLAY_WIDTH;
6520 const int32_t height = displayRotated ? DISPLAY_WIDTH : DISPLAY_HEIGHT;
6521 const Rect physicalFrame{0, 0, width, height};
6522 configurePhysicalDisplay(ui::ROTATION_0, physicalFrame, width, height);
6523
6524 const auto& expectedPoints = kMappedCorners.at(touchscreenOrientation);
6525 const float expectedPrecisionX = displayRotated ? 4 : 2;
6526 const float expectedPrecisionY = displayRotated ? 2 : 4;
6527
6528 // Test all four corners.
6529 for (int i = 0; i < 4; i++) {
6530 const auto& raw = kRawCorners[i];
6531 processDown(mapper, raw.x, raw.y);
6532 processSync(mapper);
6533 const auto& expected = expectedPoints[i];
6534 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6535 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
6536 WithCoords(expected.x, expected.y),
6537 WithPrecision(expectedPrecisionX, expectedPrecisionY))))
6538 << "Failed to process raw point (" << raw.x << ", " << raw.y << ") "
6539 << "with touchscreen orientation "
6540 << ftl::enum_string(touchscreenOrientation).c_str() << ", expected point ("
6541 << expected.x << ", " << expected.y << ").";
6542 processUp(mapper);
6543 processSync(mapper);
6544 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6545 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
6546 WithCoords(expected.x, expected.y))));
6547 }
6548}
6549
Prabir Pradhan82687402022-12-06 01:32:53 +00006550TEST_P(TouchscreenPrecisionTestsFixture, RotationPrecisionWhenOrientationAware) {
6551 static const std::map<ui::Rotation /*rotation*/, std::array<vec2, 4> /*mappedCorners*/>
6552 kMappedCorners = {
6553 {ui::ROTATION_0, {{{0, 0}, {479.5, 0}, {479.5, 799.75}, {0, 799.75}}}},
6554 {ui::ROTATION_90, {{{0.5, 0}, {480, 0}, {480, 799.75}, {0.5, 799.75}}}},
6555 {ui::ROTATION_180, {{{0.5, 0.25}, {480, 0.25}, {480, 800}, {0.5, 800}}}},
6556 {ui::ROTATION_270, {{{0, 0.25}, {479.5, 0.25}, {479.5, 800}, {0, 800}}}},
6557 };
6558
6559 const ui::Rotation displayRotation = GetParam();
6560
6561 addConfigurationProperty("touch.deviceType", "touchScreen");
6562 prepareDisplay(displayRotation);
6563
Arpit Singha8c236b2023-04-25 13:56:05 +00006564 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan82687402022-12-06 01:32:53 +00006565
6566 const auto& expectedPoints = kMappedCorners.at(displayRotation);
6567
6568 // Test all four corners.
6569 for (int i = 0; i < 4; i++) {
6570 const auto& expected = expectedPoints[i];
6571 const auto& raw = kRawCorners[i];
6572 processDown(mapper, raw.x, raw.y);
6573 processSync(mapper);
6574 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6575 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
6576 WithCoords(expected.x, expected.y), WithPrecision(2, 4))))
6577 << "Failed to process raw point (" << raw.x << ", " << raw.y << ") "
6578 << "with display rotation " << ui::toCString(displayRotation)
6579 << ", expected point (" << expected.x << ", " << expected.y << ").";
6580 processUp(mapper);
6581 processSync(mapper);
6582 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6583 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
6584 WithCoords(expected.x, expected.y))));
6585 }
6586}
6587
Prabir Pradhan3e798762022-12-02 21:02:11 +00006588TEST_P(TouchscreenPrecisionTestsFixture, RotationPrecisionOrientationAwareInOri270) {
6589 static const std::map<ui::Rotation /*orientation*/, std::array<vec2, 4> /*mappedCorners*/>
6590 kMappedCorners = {
6591 {ui::ROTATION_0, {{{799.75, 0}, {799.75, 479.5}, {0, 479.5}, {0, 0}}}},
6592 {ui::ROTATION_90, {{{800, 0}, {800, 479.5}, {0.25, 479.5}, {0.25, 0}}}},
6593 {ui::ROTATION_180, {{{800, 0.5}, {800, 480}, {0.25, 480}, {0.25, 0.5}}}},
6594 {ui::ROTATION_270, {{{799.75, 0.5}, {799.75, 480}, {0, 480}, {0, 0.5}}}},
6595 };
6596
6597 const ui::Rotation displayRotation = GetParam();
6598
6599 addConfigurationProperty("touch.deviceType", "touchScreen");
6600 addConfigurationProperty("touch.orientation", "ORIENTATION_270");
6601
Arpit Singha8c236b2023-04-25 13:56:05 +00006602 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan3e798762022-12-02 21:02:11 +00006603
6604 // Ori 270, so width and height swapped
6605 const Rect physicalFrame{0, 0, DISPLAY_HEIGHT, DISPLAY_WIDTH};
6606 prepareDisplay(displayRotation);
6607 configurePhysicalDisplay(displayRotation, physicalFrame, DISPLAY_HEIGHT, DISPLAY_WIDTH);
6608
6609 const auto& expectedPoints = kMappedCorners.at(displayRotation);
6610
6611 // Test all four corners.
6612 for (int i = 0; i < 4; i++) {
6613 const auto& expected = expectedPoints[i];
6614 const auto& raw = kRawCorners[i];
6615 processDown(mapper, raw.x, raw.y);
6616 processSync(mapper);
6617 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6618 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
6619 WithCoords(expected.x, expected.y), WithPrecision(4, 2))))
6620 << "Failed to process raw point (" << raw.x << ", " << raw.y << ") "
6621 << "with display rotation " << ui::toCString(displayRotation)
6622 << ", expected point (" << expected.x << ", " << expected.y << ").";
6623 processUp(mapper);
6624 processSync(mapper);
6625 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6626 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
6627 WithCoords(expected.x, expected.y))));
6628 }
6629}
6630
Prabir Pradhan46211fb2022-12-17 00:30:39 +00006631TEST_P(TouchscreenPrecisionTestsFixture, MotionRangesAreOrientedInRotatedDisplay) {
6632 const ui::Rotation displayRotation = GetParam();
6633
6634 addConfigurationProperty("touch.deviceType", "touchScreen");
6635 prepareDisplay(displayRotation);
6636
6637 __attribute__((unused)) SingleTouchInputMapper& mapper =
Arpit Singha8c236b2023-04-25 13:56:05 +00006638 constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan46211fb2022-12-17 00:30:39 +00006639
6640 const InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
6641 // MotionRanges use display pixels as their units
6642 const auto* xRange = deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_X, AINPUT_SOURCE_TOUCHSCREEN);
6643 const auto* yRange = deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_Y, AINPUT_SOURCE_TOUCHSCREEN);
6644
6645 // The MotionRanges should be oriented in the rotated display's coordinate space
6646 const bool displayRotated =
6647 displayRotation == ui::ROTATION_90 || displayRotation == ui::ROTATION_270;
6648
6649 constexpr float MAX_X = 479.5;
6650 constexpr float MAX_Y = 799.75;
6651 EXPECT_EQ(xRange->min, 0.f);
6652 EXPECT_EQ(yRange->min, 0.f);
6653 EXPECT_EQ(xRange->max, displayRotated ? MAX_Y : MAX_X);
6654 EXPECT_EQ(yRange->max, displayRotated ? MAX_X : MAX_Y);
6655
6656 EXPECT_EQ(xRange->flat, 8.f);
6657 EXPECT_EQ(yRange->flat, 8.f);
6658
6659 EXPECT_EQ(xRange->fuzz, 2.f);
6660 EXPECT_EQ(yRange->fuzz, 2.f);
6661
6662 EXPECT_EQ(xRange->resolution, 25.f); // pixels per millimeter
6663 EXPECT_EQ(yRange->resolution, 25.f); // pixels per millimeter
6664}
6665
Prabir Pradhana9df3162022-12-05 23:57:27 +00006666// Run the precision tests for all rotations.
6667INSTANTIATE_TEST_SUITE_P(TouchscreenPrecisionTests, TouchscreenPrecisionTestsFixture,
6668 ::testing::Values(ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180,
6669 ui::ROTATION_270),
6670 [](const testing::TestParamInfo<ui::Rotation>& testParamInfo) {
6671 return ftl::enum_string(testParamInfo.param);
6672 });
6673
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006674// --- ExternalStylusFusionTest ---
6675
6676class ExternalStylusFusionTest : public SingleTouchInputMapperTest {
6677public:
6678 SingleTouchInputMapper& initializeInputMapperWithExternalStylus() {
6679 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006680 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006681 prepareButtons();
6682 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00006683 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006684
6685 mStylusState.when = ARBITRARY_TIME;
6686 mStylusState.pressure = 0.f;
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006687 mStylusState.toolType = ToolType::STYLUS;
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006688 mReader->getContext()->setExternalStylusDevices({mExternalStylusDeviceInfo});
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00006689 configureDevice(InputReaderConfiguration::Change::EXTERNAL_STYLUS_PRESENCE);
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006690 processExternalStylusState(mapper);
6691 return mapper;
6692 }
6693
6694 std::list<NotifyArgs> processExternalStylusState(InputMapper& mapper) {
6695 std::list<NotifyArgs> generatedArgs = mapper.updateExternalStylusState(mStylusState);
6696 for (const NotifyArgs& args : generatedArgs) {
6697 mFakeListener->notify(args);
6698 }
6699 // Loop the reader to flush the input listener queue.
6700 mReader->loopOnce();
6701 return generatedArgs;
6702 }
6703
6704protected:
6705 StylusState mStylusState{};
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006706
6707 void testStartFusedStylusGesture(SingleTouchInputMapper& mapper) {
6708 auto toolTypeSource =
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00006709 AllOf(WithSource(STYLUS_FUSION_SOURCE), WithToolType(ToolType::STYLUS));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006710
6711 // The first pointer is withheld.
6712 processDown(mapper, 100, 200);
6713 processSync(mapper);
6714 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6715 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasRequested(
6716 ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT));
6717
6718 // The external stylus reports pressure. The withheld finger pointer is released as a
6719 // stylus.
6720 mStylusState.pressure = 1.f;
6721 processExternalStylusState(mapper);
6722 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6723 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN))));
6724 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6725
6726 // Subsequent pointer events are not withheld.
6727 processMove(mapper, 101, 201);
6728 processSync(mapper);
6729 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6730 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE))));
6731
6732 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6733 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6734 }
6735
6736 void testSuccessfulFusionGesture(SingleTouchInputMapper& mapper) {
6737 ASSERT_NO_FATAL_FAILURE(testStartFusedStylusGesture(mapper));
6738
6739 // Releasing the touch pointer ends the gesture.
6740 processUp(mapper);
6741 processSync(mapper);
6742 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00006743 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithSource(STYLUS_FUSION_SOURCE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006744 WithToolType(ToolType::STYLUS))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006745
6746 mStylusState.pressure = 0.f;
6747 processExternalStylusState(mapper);
6748 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6749 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6750 }
6751
6752 void testUnsuccessfulFusionGesture(SingleTouchInputMapper& mapper) {
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00006753 // When stylus fusion is not successful, events should be reported with the original source.
6754 // In this case, it is from a touchscreen.
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006755 auto toolTypeSource =
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00006756 AllOf(WithSource(AINPUT_SOURCE_TOUCHSCREEN), WithToolType(ToolType::FINGER));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006757
6758 // The first pointer is withheld when an external stylus is connected,
6759 // and a timeout is requested.
6760 processDown(mapper, 100, 200);
6761 processSync(mapper);
6762 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6763 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasRequested(
6764 ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT));
6765
6766 // If the timeout expires early, it is requested again.
6767 handleTimeout(mapper, ARBITRARY_TIME + 1);
6768 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasRequested(
6769 ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT));
6770
6771 // When the timeout expires, the withheld touch is released as a finger pointer.
6772 handleTimeout(mapper, ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT);
6773 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6774 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN))));
6775
6776 // Subsequent pointer events are not withheld.
6777 processMove(mapper, 101, 201);
6778 processSync(mapper);
6779 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6780 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE))));
6781 processUp(mapper);
6782 processSync(mapper);
6783 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6784 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_UP))));
6785
6786 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6787 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6788 }
6789
6790private:
6791 InputDeviceInfo mExternalStylusDeviceInfo{};
6792};
6793
6794TEST_F(ExternalStylusFusionTest, UsesBluetoothStylusSource) {
6795 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00006796 ASSERT_EQ(STYLUS_FUSION_SOURCE, mapper.getSources());
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006797}
6798
6799TEST_F(ExternalStylusFusionTest, UnsuccessfulFusion) {
6800 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
6801 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
6802}
6803
6804TEST_F(ExternalStylusFusionTest, SuccessfulFusion_TouchFirst) {
6805 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
6806 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
6807}
6808
6809// Test a successful stylus fusion gesture where the pressure is reported by the external
6810// before the touch is reported by the touchscreen.
6811TEST_F(ExternalStylusFusionTest, SuccessfulFusion_PressureFirst) {
6812 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00006813 auto toolTypeSource = AllOf(WithSource(STYLUS_FUSION_SOURCE), WithToolType(ToolType::STYLUS));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006814
6815 // The external stylus reports pressure first. It is ignored for now.
6816 mStylusState.pressure = 1.f;
6817 processExternalStylusState(mapper);
6818 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6819 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6820
6821 // When the touch goes down afterwards, it is reported as a stylus pointer.
6822 processDown(mapper, 100, 200);
6823 processSync(mapper);
6824 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6825 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN))));
6826 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6827
6828 processMove(mapper, 101, 201);
6829 processSync(mapper);
6830 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6831 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE))));
6832 processUp(mapper);
6833 processSync(mapper);
6834 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6835 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_UP))));
6836
6837 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6838 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6839}
6840
6841TEST_F(ExternalStylusFusionTest, FusionIsRepeatedForEachNewGesture) {
6842 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
6843
6844 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
6845 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
6846
6847 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
6848 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
6849 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
6850 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
6851}
6852
6853TEST_F(ExternalStylusFusionTest, FusedPointerReportsPressureChanges) {
6854 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00006855 auto toolTypeSource = AllOf(WithSource(STYLUS_FUSION_SOURCE), WithToolType(ToolType::STYLUS));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006856
6857 mStylusState.pressure = 0.8f;
6858 processExternalStylusState(mapper);
6859 processDown(mapper, 100, 200);
6860 processSync(mapper);
6861 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6862 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
6863 WithPressure(0.8f))));
6864 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6865
6866 // The external stylus reports a pressure change. We wait for some time for a touch event.
6867 mStylusState.pressure = 0.6f;
6868 processExternalStylusState(mapper);
6869 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6870 ASSERT_NO_FATAL_FAILURE(
6871 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
6872
6873 // If a touch is reported within the timeout, it reports the updated pressure.
6874 processMove(mapper, 101, 201);
6875 processSync(mapper);
6876 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6877 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
6878 WithPressure(0.6f))));
6879 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6880
6881 // There is another pressure change.
6882 mStylusState.pressure = 0.5f;
6883 processExternalStylusState(mapper);
6884 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6885 ASSERT_NO_FATAL_FAILURE(
6886 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
6887
6888 // If a touch is not reported within the timeout, a move event is generated to report
6889 // the new pressure.
6890 handleTimeout(mapper, ARBITRARY_TIME + TOUCH_DATA_TIMEOUT);
6891 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6892 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
6893 WithPressure(0.5f))));
6894
6895 // If a zero pressure is reported before the touch goes up, the previous pressure value is
6896 // repeated indefinitely.
6897 mStylusState.pressure = 0.0f;
6898 processExternalStylusState(mapper);
6899 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6900 ASSERT_NO_FATAL_FAILURE(
6901 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
6902 processMove(mapper, 102, 202);
6903 processSync(mapper);
6904 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6905 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
6906 WithPressure(0.5f))));
6907 processMove(mapper, 103, 203);
6908 processSync(mapper);
6909 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6910 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
6911 WithPressure(0.5f))));
6912
6913 processUp(mapper);
6914 processSync(mapper);
6915 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00006916 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithSource(STYLUS_FUSION_SOURCE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006917 WithToolType(ToolType::STYLUS))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006918
6919 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6920 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6921}
6922
6923TEST_F(ExternalStylusFusionTest, FusedPointerReportsToolTypeChanges) {
6924 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00006925 auto source = WithSource(STYLUS_FUSION_SOURCE);
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006926
6927 mStylusState.pressure = 1.f;
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006928 mStylusState.toolType = ToolType::ERASER;
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006929 processExternalStylusState(mapper);
6930 processDown(mapper, 100, 200);
6931 processSync(mapper);
6932 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6933 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006934 WithToolType(ToolType::ERASER))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006935 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6936
6937 // The external stylus reports a tool change. We wait for some time for a touch event.
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006938 mStylusState.toolType = ToolType::STYLUS;
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006939 processExternalStylusState(mapper);
6940 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6941 ASSERT_NO_FATAL_FAILURE(
6942 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
6943
6944 // If a touch is reported within the timeout, it reports the updated pressure.
6945 processMove(mapper, 101, 201);
6946 processSync(mapper);
6947 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6948 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006949 WithToolType(ToolType::STYLUS))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006950 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6951
6952 // There is another tool type change.
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006953 mStylusState.toolType = ToolType::FINGER;
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006954 processExternalStylusState(mapper);
6955 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6956 ASSERT_NO_FATAL_FAILURE(
6957 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
6958
6959 // If a touch is not reported within the timeout, a move event is generated to report
6960 // the new tool type.
6961 handleTimeout(mapper, ARBITRARY_TIME + TOUCH_DATA_TIMEOUT);
6962 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6963 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006964 WithToolType(ToolType::FINGER))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006965
6966 processUp(mapper);
6967 processSync(mapper);
6968 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6969 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_UP),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006970 WithToolType(ToolType::FINGER))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006971
6972 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6973 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6974}
6975
6976TEST_F(ExternalStylusFusionTest, FusedPointerReportsButtons) {
6977 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00006978 auto toolTypeSource = AllOf(WithSource(STYLUS_FUSION_SOURCE), WithToolType(ToolType::STYLUS));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006979
6980 ASSERT_NO_FATAL_FAILURE(testStartFusedStylusGesture(mapper));
6981
6982 // The external stylus reports a button change. We wait for some time for a touch event.
6983 mStylusState.buttons = AMOTION_EVENT_BUTTON_STYLUS_PRIMARY;
6984 processExternalStylusState(mapper);
6985 ASSERT_NO_FATAL_FAILURE(
6986 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
6987
6988 // If a touch is reported within the timeout, it reports the updated button state.
6989 processMove(mapper, 101, 201);
6990 processSync(mapper);
6991 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6992 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
6993 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
6994 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6995 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
6996 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
6997 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6998
6999 // The button is now released.
7000 mStylusState.buttons = 0;
7001 processExternalStylusState(mapper);
7002 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7003 ASSERT_NO_FATAL_FAILURE(
7004 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7005
7006 // If a touch is not reported within the timeout, a move event is generated to report
7007 // the new button state.
7008 handleTimeout(mapper, ARBITRARY_TIME + TOUCH_DATA_TIMEOUT);
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007009 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7010 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
7011 WithButtonState(0))));
7012 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan124ea442022-10-28 20:27:44 +00007013 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7014 WithButtonState(0))));
7015
7016 processUp(mapper);
7017 processSync(mapper);
7018 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007019 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_UP), WithButtonState(0))));
7020
7021 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7022 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7023}
7024
Michael Wrightd02c5b62014-02-10 15:10:22 -08007025// --- MultiTouchInputMapperTest ---
7026
7027class MultiTouchInputMapperTest : public TouchInputMapperTest {
7028protected:
7029 void prepareAxes(int axes);
7030
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007031 void processPosition(MultiTouchInputMapper& mapper, int32_t x, int32_t y);
7032 void processTouchMajor(MultiTouchInputMapper& mapper, int32_t touchMajor);
7033 void processTouchMinor(MultiTouchInputMapper& mapper, int32_t touchMinor);
7034 void processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor);
7035 void processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor);
7036 void processOrientation(MultiTouchInputMapper& mapper, int32_t orientation);
7037 void processPressure(MultiTouchInputMapper& mapper, int32_t pressure);
7038 void processDistance(MultiTouchInputMapper& mapper, int32_t distance);
7039 void processId(MultiTouchInputMapper& mapper, int32_t id);
7040 void processSlot(MultiTouchInputMapper& mapper, int32_t slot);
7041 void processToolType(MultiTouchInputMapper& mapper, int32_t toolType);
7042 void processKey(MultiTouchInputMapper& mapper, int32_t code, int32_t value);
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00007043 void processHidUsage(MultiTouchInputMapper& mapper, int32_t usageCode, int32_t value);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007044 void processMTSync(MultiTouchInputMapper& mapper);
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00007045 void processSync(MultiTouchInputMapper& mapper, nsecs_t eventTime = ARBITRARY_TIME,
7046 nsecs_t readTime = READ_TIME);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007047};
7048
7049void MultiTouchInputMapperTest::prepareAxes(int axes) {
7050 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007051 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
7052 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007053 }
7054 if (axes & TOUCH) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007055 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN,
7056 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007057 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007058 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN,
7059 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007060 }
7061 }
7062 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007063 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7064 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007065 if (axes & MINOR) {
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007066 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007067 RAW_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007068 }
7069 }
7070 if (axes & ORIENTATION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007071 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_ORIENTATION, RAW_ORIENTATION_MIN,
7072 RAW_ORIENTATION_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007073 }
7074 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007075 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_PRESSURE, RAW_PRESSURE_MIN,
7076 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007077 }
7078 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007079 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_DISTANCE, RAW_DISTANCE_MIN,
7080 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007081 }
7082 if (axes & ID) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007083 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX, 0,
7084 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007085 }
7086 if (axes & SLOT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007087 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
7088 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_MT_SLOT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007089 }
7090 if (axes & TOOL_TYPE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007091 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOOL_TYPE, 0, MT_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007092 }
7093}
7094
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007095void MultiTouchInputMapperTest::processPosition(MultiTouchInputMapper& mapper, int32_t x,
7096 int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007097 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_X, x);
7098 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007099}
7100
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007101void MultiTouchInputMapperTest::processTouchMajor(MultiTouchInputMapper& mapper,
7102 int32_t touchMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007103 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007104}
7105
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007106void MultiTouchInputMapperTest::processTouchMinor(MultiTouchInputMapper& mapper,
7107 int32_t touchMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007108 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007109}
7110
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007111void MultiTouchInputMapperTest::processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007112 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007113}
7114
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007115void MultiTouchInputMapperTest::processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007116 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007117}
7118
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007119void MultiTouchInputMapperTest::processOrientation(MultiTouchInputMapper& mapper,
7120 int32_t orientation) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007121 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_ORIENTATION, orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007122}
7123
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007124void MultiTouchInputMapperTest::processPressure(MultiTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007125 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007126}
7127
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007128void MultiTouchInputMapperTest::processDistance(MultiTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007129 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007130}
7131
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007132void MultiTouchInputMapperTest::processId(MultiTouchInputMapper& mapper, int32_t id) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007133 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TRACKING_ID, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007134}
7135
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007136void MultiTouchInputMapperTest::processSlot(MultiTouchInputMapper& mapper, int32_t slot) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007137 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_SLOT, slot);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007138}
7139
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007140void MultiTouchInputMapperTest::processToolType(MultiTouchInputMapper& mapper, int32_t toolType) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007141 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007142}
7143
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007144void MultiTouchInputMapperTest::processKey(MultiTouchInputMapper& mapper, int32_t code,
7145 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007146 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007147}
7148
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00007149void MultiTouchInputMapperTest::processHidUsage(MultiTouchInputMapper& mapper, int32_t usageCode,
7150 int32_t value) {
7151 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, usageCode);
7152 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UNKNOWN, value);
7153}
7154
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007155void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007156 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_MT_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007157}
7158
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00007159void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper& mapper, nsecs_t eventTime,
7160 nsecs_t readTime) {
7161 process(mapper, eventTime, readTime, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007162}
7163
Michael Wrightd02c5b62014-02-10 15:10:22 -08007164TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007165 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007166 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007167 prepareAxes(POSITION);
7168 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00007169 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007170
arthurhungdcef2dc2020-08-11 14:47:50 +08007171 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007172
7173 NotifyMotionArgs motionArgs;
7174
7175 // Two fingers down at once.
7176 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7177 processPosition(mapper, x1, y1);
7178 processMTSync(mapper);
7179 processPosition(mapper, x2, y2);
7180 processMTSync(mapper);
7181 processSync(mapper);
7182
7183 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7184 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7185 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7186 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7187 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7188 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7189 ASSERT_EQ(0, motionArgs.flags);
7190 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7191 ASSERT_EQ(0, motionArgs.buttonState);
7192 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007193 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007194 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007195 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007196 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7197 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7198 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7199 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7200 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7201
7202 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7203 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7204 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7205 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7206 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007207 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007208 ASSERT_EQ(0, motionArgs.flags);
7209 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7210 ASSERT_EQ(0, motionArgs.buttonState);
7211 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007212 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007213 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007214 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007215 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007216 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007217 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7218 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7219 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7220 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7221 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7222 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7223 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7224
7225 // Move.
7226 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7227 processPosition(mapper, x1, y1);
7228 processMTSync(mapper);
7229 processPosition(mapper, x2, y2);
7230 processMTSync(mapper);
7231 processSync(mapper);
7232
7233 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7234 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7235 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7236 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7237 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7238 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7239 ASSERT_EQ(0, motionArgs.flags);
7240 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7241 ASSERT_EQ(0, motionArgs.buttonState);
7242 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007243 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007244 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007245 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007246 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007247 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007248 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7249 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7250 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7251 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7252 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7253 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7254 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7255
7256 // First finger up.
7257 x2 += 15; y2 -= 20;
7258 processPosition(mapper, x2, y2);
7259 processMTSync(mapper);
7260 processSync(mapper);
7261
7262 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7263 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7264 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7265 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7266 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007267 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007268 ASSERT_EQ(0, motionArgs.flags);
7269 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7270 ASSERT_EQ(0, motionArgs.buttonState);
7271 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007272 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007273 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007274 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007275 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007276 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007277 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7278 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7279 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7280 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7281 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7282 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7283 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7284
7285 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7286 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7287 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7288 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7289 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7290 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7291 ASSERT_EQ(0, motionArgs.flags);
7292 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7293 ASSERT_EQ(0, motionArgs.buttonState);
7294 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007295 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007296 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007297 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007298 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7299 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7300 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7301 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7302 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7303
7304 // Move.
7305 x2 += 20; y2 -= 25;
7306 processPosition(mapper, x2, y2);
7307 processMTSync(mapper);
7308 processSync(mapper);
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 // New finger down.
7330 int32_t x3 = 700, y3 = 300;
7331 processPosition(mapper, x2, y2);
7332 processMTSync(mapper);
7333 processPosition(mapper, x3, y3);
7334 processMTSync(mapper);
7335 processSync(mapper);
7336
7337 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7338 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7339 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7340 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7341 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007342 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007343 ASSERT_EQ(0, motionArgs.flags);
7344 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7345 ASSERT_EQ(0, motionArgs.buttonState);
7346 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007347 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007348 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007349 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007350 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007351 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007352 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7353 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7354 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7355 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7356 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7357 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7358 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7359
7360 // Second finger up.
7361 x3 += 30; y3 -= 20;
7362 processPosition(mapper, x3, y3);
7363 processMTSync(mapper);
7364 processSync(mapper);
7365
7366 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7367 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7368 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7369 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7370 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007371 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007372 ASSERT_EQ(0, motionArgs.flags);
7373 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7374 ASSERT_EQ(0, motionArgs.buttonState);
7375 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007376 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007377 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007378 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007379 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007380 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007381 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7382 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7383 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7384 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7385 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7386 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7387 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7388
7389 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7390 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7391 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7392 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7393 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7394 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7395 ASSERT_EQ(0, motionArgs.flags);
7396 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7397 ASSERT_EQ(0, motionArgs.buttonState);
7398 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007399 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007400 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007401 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007402 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7403 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7404 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7405 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7406 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7407
7408 // Last finger up.
7409 processMTSync(mapper);
7410 processSync(mapper);
7411
7412 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7413 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7414 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7415 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7416 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7417 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7418 ASSERT_EQ(0, motionArgs.flags);
7419 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7420 ASSERT_EQ(0, motionArgs.buttonState);
7421 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007422 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007423 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007424 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007425 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7426 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7427 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7428 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7429 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7430
7431 // Should not have sent any more keys or motions.
7432 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7433 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7434}
7435
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007436TEST_F(MultiTouchInputMapperTest, AxisResolution_IsPopulated) {
7437 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007438 prepareDisplay(ui::ROTATION_0);
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007439
7440 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
7441 /*fuzz*/ 0, /*resolution*/ 10);
7442 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
7443 /*fuzz*/ 0, /*resolution*/ 11);
7444 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
7445 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 12);
7446 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
7447 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 13);
7448 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7449 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 14);
7450 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7451 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 15);
7452
Arpit Singha8c236b2023-04-25 13:56:05 +00007453 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007454
7455 // X and Y axes
7456 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_X, 10 / X_PRECISION);
7457 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_Y, 11 / Y_PRECISION);
7458 // Touch major and minor
7459 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR, 12 * GEOMETRIC_SCALE);
7460 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR, 13 * GEOMETRIC_SCALE);
7461 // Tool major and minor
7462 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR, 14 * GEOMETRIC_SCALE);
7463 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR, 15 * GEOMETRIC_SCALE);
7464}
7465
7466TEST_F(MultiTouchInputMapperTest, TouchMajorAndMinorAxes_DoNotAppearIfNotSupported) {
7467 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007468 prepareDisplay(ui::ROTATION_0);
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007469
7470 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
7471 /*fuzz*/ 0, /*resolution*/ 10);
7472 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
7473 /*fuzz*/ 0, /*resolution*/ 11);
7474
7475 // We do not add ABS_MT_TOUCH_MAJOR / MINOR or ABS_MT_WIDTH_MAJOR / MINOR axes
7476
Arpit Singha8c236b2023-04-25 13:56:05 +00007477 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007478
7479 // Touch major and minor
7480 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR);
7481 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR);
7482 // Tool major and minor
7483 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR);
7484 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR);
7485}
7486
Michael Wrightd02c5b62014-02-10 15:10:22 -08007487TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007488 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007489 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007490 prepareAxes(POSITION | ID);
7491 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00007492 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007493
arthurhungdcef2dc2020-08-11 14:47:50 +08007494 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007495
7496 NotifyMotionArgs motionArgs;
7497
7498 // Two fingers down at once.
7499 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7500 processPosition(mapper, x1, y1);
7501 processId(mapper, 1);
7502 processMTSync(mapper);
7503 processPosition(mapper, x2, y2);
7504 processId(mapper, 2);
7505 processMTSync(mapper);
7506 processSync(mapper);
7507
7508 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7509 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007510 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007511 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007512 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007513 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7514 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7515
7516 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007517 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007518 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007519 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007520 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007521 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007522 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007523 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7524 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7525 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7526 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7527
7528 // Move.
7529 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7530 processPosition(mapper, x1, y1);
7531 processId(mapper, 1);
7532 processMTSync(mapper);
7533 processPosition(mapper, x2, y2);
7534 processId(mapper, 2);
7535 processMTSync(mapper);
7536 processSync(mapper);
7537
7538 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7539 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007540 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007541 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007542 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007543 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007544 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007545 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7546 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7547 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7548 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7549
7550 // First finger up.
7551 x2 += 15; y2 -= 20;
7552 processPosition(mapper, x2, y2);
7553 processId(mapper, 2);
7554 processMTSync(mapper);
7555 processSync(mapper);
7556
7557 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007558 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007559 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007560 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007561 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007562 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007563 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007564 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7565 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7566 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7567 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7568
7569 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7570 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007571 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007572 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007573 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007574 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7575 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7576
7577 // Move.
7578 x2 += 20; y2 -= 25;
7579 processPosition(mapper, x2, y2);
7580 processId(mapper, 2);
7581 processMTSync(mapper);
7582 processSync(mapper);
7583
7584 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7585 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007586 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007587 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007588 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007589 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7590 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7591
7592 // New finger down.
7593 int32_t x3 = 700, y3 = 300;
7594 processPosition(mapper, x2, y2);
7595 processId(mapper, 2);
7596 processMTSync(mapper);
7597 processPosition(mapper, x3, y3);
7598 processId(mapper, 3);
7599 processMTSync(mapper);
7600 processSync(mapper);
7601
7602 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007603 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007604 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007605 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007606 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007607 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007608 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007609 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7610 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7611 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7612 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7613
7614 // Second finger up.
7615 x3 += 30; y3 -= 20;
7616 processPosition(mapper, x3, y3);
7617 processId(mapper, 3);
7618 processMTSync(mapper);
7619 processSync(mapper);
7620
7621 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007622 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007623 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007624 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007625 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007626 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007627 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007628 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7629 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7630 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7631 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7632
7633 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7634 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007635 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007636 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007637 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007638 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7639 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7640
7641 // Last finger up.
7642 processMTSync(mapper);
7643 processSync(mapper);
7644
7645 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7646 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007647 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007648 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007649 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007650 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7651 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7652
7653 // Should not have sent any more keys or motions.
7654 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7655 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7656}
7657
7658TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007659 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007660 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007661 prepareAxes(POSITION | ID | SLOT);
7662 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00007663 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007664
arthurhungdcef2dc2020-08-11 14:47:50 +08007665 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007666
7667 NotifyMotionArgs motionArgs;
7668
7669 // Two fingers down at once.
7670 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7671 processPosition(mapper, x1, y1);
7672 processId(mapper, 1);
7673 processSlot(mapper, 1);
7674 processPosition(mapper, x2, y2);
7675 processId(mapper, 2);
7676 processSync(mapper);
7677
7678 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7679 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007680 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007681 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007682 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007683 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7684 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7685
7686 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007687 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007688 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007689 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007690 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007691 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007692 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007693 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7694 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7695 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7696 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7697
7698 // Move.
7699 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7700 processSlot(mapper, 0);
7701 processPosition(mapper, x1, y1);
7702 processSlot(mapper, 1);
7703 processPosition(mapper, x2, y2);
7704 processSync(mapper);
7705
7706 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7707 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007708 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007709 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007710 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007711 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007712 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007713 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7714 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7715 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7716 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7717
7718 // First finger up.
7719 x2 += 15; y2 -= 20;
7720 processSlot(mapper, 0);
7721 processId(mapper, -1);
7722 processSlot(mapper, 1);
7723 processPosition(mapper, x2, y2);
7724 processSync(mapper);
7725
7726 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007727 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007728 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007729 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007730 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007731 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007732 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007733 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7734 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7735 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7736 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7737
7738 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7739 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007740 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007741 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007742 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007743 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7744 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7745
7746 // Move.
7747 x2 += 20; y2 -= 25;
7748 processPosition(mapper, x2, y2);
7749 processSync(mapper);
7750
7751 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7752 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007753 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007754 ASSERT_EQ(1, 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_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7757 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7758
7759 // New finger down.
7760 int32_t x3 = 700, y3 = 300;
7761 processPosition(mapper, x2, y2);
7762 processSlot(mapper, 0);
7763 processId(mapper, 3);
7764 processPosition(mapper, x3, y3);
7765 processSync(mapper);
7766
7767 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007768 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007769 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007770 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007771 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007772 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007773 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007774 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7775 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7776 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7777 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7778
7779 // Second finger up.
7780 x3 += 30; y3 -= 20;
7781 processSlot(mapper, 1);
7782 processId(mapper, -1);
7783 processSlot(mapper, 0);
7784 processPosition(mapper, x3, y3);
7785 processSync(mapper);
7786
7787 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007788 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007789 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007790 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007791 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007792 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007793 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007794 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7795 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7796 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7797 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7798
7799 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7800 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007801 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007802 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007803 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007804 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7805 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7806
7807 // Last finger up.
7808 processId(mapper, -1);
7809 processSync(mapper);
7810
7811 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7812 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007813 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007814 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007815 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007816 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7817 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7818
7819 // Should not have sent any more keys or motions.
7820 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7821 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7822}
7823
7824TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007825 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007826 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007827 prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
Arpit Singha8c236b2023-04-25 13:56:05 +00007828 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007829
7830 // These calculations are based on the input device calibration documentation.
7831 int32_t rawX = 100;
7832 int32_t rawY = 200;
7833 int32_t rawTouchMajor = 7;
7834 int32_t rawTouchMinor = 6;
7835 int32_t rawToolMajor = 9;
7836 int32_t rawToolMinor = 8;
7837 int32_t rawPressure = 11;
7838 int32_t rawDistance = 0;
7839 int32_t rawOrientation = 3;
7840 int32_t id = 5;
7841
7842 float x = toDisplayX(rawX);
7843 float y = toDisplayY(rawY);
7844 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
7845 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
7846 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
7847 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
7848 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
7849 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
7850 float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
7851 float distance = float(rawDistance);
7852
7853 processPosition(mapper, rawX, rawY);
7854 processTouchMajor(mapper, rawTouchMajor);
7855 processTouchMinor(mapper, rawTouchMinor);
7856 processToolMajor(mapper, rawToolMajor);
7857 processToolMinor(mapper, rawToolMinor);
7858 processPressure(mapper, rawPressure);
7859 processOrientation(mapper, rawOrientation);
7860 processDistance(mapper, rawDistance);
7861 processId(mapper, id);
7862 processMTSync(mapper);
7863 processSync(mapper);
7864
7865 NotifyMotionArgs args;
7866 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7867 ASSERT_EQ(0, args.pointerProperties[0].id);
7868 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7869 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
7870 orientation, distance));
7871}
7872
7873TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007874 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007875 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007876 prepareAxes(POSITION | TOUCH | TOOL | MINOR);
7877 addConfigurationProperty("touch.size.calibration", "geometric");
Arpit Singha8c236b2023-04-25 13:56:05 +00007878 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007879
7880 // These calculations are based on the input device calibration documentation.
7881 int32_t rawX = 100;
7882 int32_t rawY = 200;
7883 int32_t rawTouchMajor = 140;
7884 int32_t rawTouchMinor = 120;
7885 int32_t rawToolMajor = 180;
7886 int32_t rawToolMinor = 160;
7887
7888 float x = toDisplayX(rawX);
7889 float y = toDisplayY(rawY);
7890 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
7891 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
7892 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
7893 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
7894 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
7895
7896 processPosition(mapper, rawX, rawY);
7897 processTouchMajor(mapper, rawTouchMajor);
7898 processTouchMinor(mapper, rawTouchMinor);
7899 processToolMajor(mapper, rawToolMajor);
7900 processToolMinor(mapper, rawToolMinor);
7901 processMTSync(mapper);
7902 processSync(mapper);
7903
7904 NotifyMotionArgs args;
7905 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7906 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7907 x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
7908}
7909
7910TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007911 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007912 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007913 prepareAxes(POSITION | TOUCH | TOOL);
7914 addConfigurationProperty("touch.size.calibration", "diameter");
7915 addConfigurationProperty("touch.size.scale", "10");
7916 addConfigurationProperty("touch.size.bias", "160");
7917 addConfigurationProperty("touch.size.isSummed", "1");
Arpit Singha8c236b2023-04-25 13:56:05 +00007918 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007919
7920 // These calculations are based on the input device calibration documentation.
7921 // Note: We only provide a single common touch/tool value because the device is assumed
7922 // not to emit separate values for each pointer (isSummed = 1).
7923 int32_t rawX = 100;
7924 int32_t rawY = 200;
7925 int32_t rawX2 = 150;
7926 int32_t rawY2 = 250;
7927 int32_t rawTouchMajor = 5;
7928 int32_t rawToolMajor = 8;
7929
7930 float x = toDisplayX(rawX);
7931 float y = toDisplayY(rawY);
7932 float x2 = toDisplayX(rawX2);
7933 float y2 = toDisplayY(rawY2);
7934 float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
7935 float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
7936 float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
7937
7938 processPosition(mapper, rawX, rawY);
7939 processTouchMajor(mapper, rawTouchMajor);
7940 processToolMajor(mapper, rawToolMajor);
7941 processMTSync(mapper);
7942 processPosition(mapper, rawX2, rawY2);
7943 processTouchMajor(mapper, rawTouchMajor);
7944 processToolMajor(mapper, rawToolMajor);
7945 processMTSync(mapper);
7946 processSync(mapper);
7947
7948 NotifyMotionArgs args;
7949 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7950 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
7951
7952 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007953 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007954 ASSERT_EQ(size_t(2), args.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007955 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7956 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
7957 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
7958 x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
7959}
7960
7961TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007962 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007963 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007964 prepareAxes(POSITION | TOUCH | TOOL);
7965 addConfigurationProperty("touch.size.calibration", "area");
7966 addConfigurationProperty("touch.size.scale", "43");
7967 addConfigurationProperty("touch.size.bias", "3");
Arpit Singha8c236b2023-04-25 13:56:05 +00007968 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007969
7970 // These calculations are based on the input device calibration documentation.
7971 int32_t rawX = 100;
7972 int32_t rawY = 200;
7973 int32_t rawTouchMajor = 5;
7974 int32_t rawToolMajor = 8;
7975
7976 float x = toDisplayX(rawX);
7977 float y = toDisplayY(rawY);
7978 float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
7979 float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
7980 float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
7981
7982 processPosition(mapper, rawX, rawY);
7983 processTouchMajor(mapper, rawTouchMajor);
7984 processToolMajor(mapper, rawToolMajor);
7985 processMTSync(mapper);
7986 processSync(mapper);
7987
7988 NotifyMotionArgs args;
7989 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7990 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7991 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
7992}
7993
7994TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007995 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007996 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007997 prepareAxes(POSITION | PRESSURE);
7998 addConfigurationProperty("touch.pressure.calibration", "amplitude");
7999 addConfigurationProperty("touch.pressure.scale", "0.01");
Arpit Singha8c236b2023-04-25 13:56:05 +00008000 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008001
Michael Wrightaa449c92017-12-13 21:21:43 +00008002 InputDeviceInfo info;
Harry Cuttsd02ea102023-03-17 18:21:30 +00008003 mapper.populateDeviceInfo(info);
Michael Wrightaa449c92017-12-13 21:21:43 +00008004 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
8005 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TOUCHSCREEN,
8006 0.0f, RAW_PRESSURE_MAX * 0.01, 0.0f, 0.0f));
8007
Michael Wrightd02c5b62014-02-10 15:10:22 -08008008 // These calculations are based on the input device calibration documentation.
8009 int32_t rawX = 100;
8010 int32_t rawY = 200;
8011 int32_t rawPressure = 60;
8012
8013 float x = toDisplayX(rawX);
8014 float y = toDisplayY(rawY);
8015 float pressure = float(rawPressure) * 0.01f;
8016
8017 processPosition(mapper, rawX, rawY);
8018 processPressure(mapper, rawPressure);
8019 processMTSync(mapper);
8020 processSync(mapper);
8021
8022 NotifyMotionArgs args;
8023 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8024 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8025 x, y, pressure, 0, 0, 0, 0, 0, 0, 0));
8026}
8027
8028TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008029 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008030 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008031 prepareAxes(POSITION | ID | SLOT);
Arpit Singha8c236b2023-04-25 13:56:05 +00008032 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008033
8034 NotifyMotionArgs motionArgs;
8035 NotifyKeyArgs keyArgs;
8036
8037 processId(mapper, 1);
8038 processPosition(mapper, 100, 200);
8039 processSync(mapper);
8040 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8041 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8042 ASSERT_EQ(0, motionArgs.buttonState);
8043
8044 // press BTN_LEFT, release BTN_LEFT
8045 processKey(mapper, BTN_LEFT, 1);
8046 processSync(mapper);
8047 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8048 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8049 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
8050
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008051 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8052 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8053 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
8054
Michael Wrightd02c5b62014-02-10 15:10:22 -08008055 processKey(mapper, BTN_LEFT, 0);
8056 processSync(mapper);
8057 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008058 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008059 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008060
8061 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008062 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008063 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008064
8065 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
8066 processKey(mapper, BTN_RIGHT, 1);
8067 processKey(mapper, BTN_MIDDLE, 1);
8068 processSync(mapper);
8069 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8070 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8071 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
8072 motionArgs.buttonState);
8073
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008074 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8075 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8076 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
8077
8078 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8079 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8080 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
8081 motionArgs.buttonState);
8082
Michael Wrightd02c5b62014-02-10 15:10:22 -08008083 processKey(mapper, BTN_RIGHT, 0);
8084 processSync(mapper);
8085 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008086 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008087 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008088
8089 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008090 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008091 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008092
8093 processKey(mapper, BTN_MIDDLE, 0);
8094 processSync(mapper);
8095 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008096 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008097 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008098
8099 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008100 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008101 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008102
8103 // press BTN_BACK, release BTN_BACK
8104 processKey(mapper, BTN_BACK, 1);
8105 processSync(mapper);
8106 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8107 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8108 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008109
Michael Wrightd02c5b62014-02-10 15:10:22 -08008110 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008111 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008112 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
8113
8114 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8115 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8116 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008117
8118 processKey(mapper, BTN_BACK, 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);
8127
Michael Wrightd02c5b62014-02-10 15:10:22 -08008128 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8129 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8130 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
8131
8132 // press BTN_SIDE, release BTN_SIDE
8133 processKey(mapper, BTN_SIDE, 1);
8134 processSync(mapper);
8135 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8136 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8137 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008138
Michael Wrightd02c5b62014-02-10 15:10:22 -08008139 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008140 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008141 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
8142
8143 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8144 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8145 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008146
8147 processKey(mapper, BTN_SIDE, 0);
8148 processSync(mapper);
8149 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008150 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008151 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008152
8153 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008154 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008155 ASSERT_EQ(0, motionArgs.buttonState);
8156
Michael Wrightd02c5b62014-02-10 15:10:22 -08008157 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8158 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8159 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
8160
8161 // press BTN_FORWARD, release BTN_FORWARD
8162 processKey(mapper, BTN_FORWARD, 1);
8163 processSync(mapper);
8164 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8165 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8166 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008167
Michael Wrightd02c5b62014-02-10 15:10:22 -08008168 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008169 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008170 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
8171
8172 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8173 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8174 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008175
8176 processKey(mapper, BTN_FORWARD, 0);
8177 processSync(mapper);
8178 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008179 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008180 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008181
8182 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008183 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008184 ASSERT_EQ(0, motionArgs.buttonState);
8185
Michael Wrightd02c5b62014-02-10 15:10:22 -08008186 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8187 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8188 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
8189
8190 // press BTN_EXTRA, release BTN_EXTRA
8191 processKey(mapper, BTN_EXTRA, 1);
8192 processSync(mapper);
8193 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8194 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8195 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008196
Michael Wrightd02c5b62014-02-10 15:10:22 -08008197 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008198 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008199 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
8200
8201 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8202 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8203 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008204
8205 processKey(mapper, BTN_EXTRA, 0);
8206 processSync(mapper);
8207 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008208 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008209 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008210
8211 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008212 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008213 ASSERT_EQ(0, motionArgs.buttonState);
8214
Michael Wrightd02c5b62014-02-10 15:10:22 -08008215 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8216 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8217 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
8218
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008219 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8220
Michael Wrightd02c5b62014-02-10 15:10:22 -08008221 // press BTN_STYLUS, release BTN_STYLUS
8222 processKey(mapper, BTN_STYLUS, 1);
8223 processSync(mapper);
8224 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8225 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008226 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
8227
8228 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8229 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8230 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008231
8232 processKey(mapper, BTN_STYLUS, 0);
8233 processSync(mapper);
8234 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008235 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008236 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008237
8238 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008239 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008240 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008241
8242 // press BTN_STYLUS2, release BTN_STYLUS2
8243 processKey(mapper, BTN_STYLUS2, 1);
8244 processSync(mapper);
8245 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8246 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008247 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
8248
8249 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8250 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8251 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008252
8253 processKey(mapper, BTN_STYLUS2, 0);
8254 processSync(mapper);
8255 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008256 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008257 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008258
8259 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008260 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008261 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008262
8263 // release touch
8264 processId(mapper, -1);
8265 processSync(mapper);
8266 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8267 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8268 ASSERT_EQ(0, motionArgs.buttonState);
8269}
8270
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00008271TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleMappedStylusButtons) {
8272 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008273 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00008274 prepareAxes(POSITION | ID | SLOT);
Arpit Singha8c236b2023-04-25 13:56:05 +00008275 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00008276
8277 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_STYLUS_BUTTON_PRIMARY, 0);
8278 mFakeEventHub->addKey(EVENTHUB_ID, 0, 0xabcd, AKEYCODE_STYLUS_BUTTON_SECONDARY, 0);
8279
8280 // Touch down.
8281 processId(mapper, 1);
8282 processPosition(mapper, 100, 200);
8283 processSync(mapper);
8284 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8285 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithButtonState(0))));
8286
8287 // Press and release button mapped to the primary stylus button.
8288 processKey(mapper, BTN_A, 1);
8289 processSync(mapper);
8290 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8291 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
8292 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
8293 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8294 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
8295 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
8296
8297 processKey(mapper, BTN_A, 0);
8298 processSync(mapper);
8299 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8300 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE), WithButtonState(0))));
8301 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8302 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
8303
8304 // Press and release the HID usage mapped to the secondary stylus button.
8305 processHidUsage(mapper, 0xabcd, 1);
8306 processSync(mapper);
8307 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8308 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
8309 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY))));
8310 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8311 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
8312 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY))));
8313
8314 processHidUsage(mapper, 0xabcd, 0);
8315 processSync(mapper);
8316 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8317 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE), WithButtonState(0))));
8318 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8319 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
8320
8321 // Release touch.
8322 processId(mapper, -1);
8323 processSync(mapper);
8324 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8325 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithButtonState(0))));
8326}
8327
Michael Wrightd02c5b62014-02-10 15:10:22 -08008328TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008329 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008330 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008331 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +00008332 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008333
8334 NotifyMotionArgs motionArgs;
8335
8336 // default tool type is finger
8337 processId(mapper, 1);
8338 processPosition(mapper, 100, 200);
8339 processSync(mapper);
8340 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8341 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008342 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008343
8344 // eraser
8345 processKey(mapper, BTN_TOOL_RUBBER, 1);
8346 processSync(mapper);
8347 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8348 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008349 ASSERT_EQ(ToolType::ERASER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008350
8351 // stylus
8352 processKey(mapper, BTN_TOOL_RUBBER, 0);
8353 processKey(mapper, BTN_TOOL_PEN, 1);
8354 processSync(mapper);
8355 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8356 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008357 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008358
8359 // brush
8360 processKey(mapper, BTN_TOOL_PEN, 0);
8361 processKey(mapper, BTN_TOOL_BRUSH, 1);
8362 processSync(mapper);
8363 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8364 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008365 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008366
8367 // pencil
8368 processKey(mapper, BTN_TOOL_BRUSH, 0);
8369 processKey(mapper, BTN_TOOL_PENCIL, 1);
8370 processSync(mapper);
8371 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8372 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008373 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008374
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08008375 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08008376 processKey(mapper, BTN_TOOL_PENCIL, 0);
8377 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
8378 processSync(mapper);
8379 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8380 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008381 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008382
8383 // mouse
8384 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
8385 processKey(mapper, BTN_TOOL_MOUSE, 1);
8386 processSync(mapper);
8387 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8388 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008389 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008390
8391 // lens
8392 processKey(mapper, BTN_TOOL_MOUSE, 0);
8393 processKey(mapper, BTN_TOOL_LENS, 1);
8394 processSync(mapper);
8395 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8396 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008397 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008398
8399 // double-tap
8400 processKey(mapper, BTN_TOOL_LENS, 0);
8401 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
8402 processSync(mapper);
8403 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8404 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008405 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008406
8407 // triple-tap
8408 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
8409 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
8410 processSync(mapper);
8411 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8412 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008413 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008414
8415 // quad-tap
8416 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
8417 processKey(mapper, BTN_TOOL_QUADTAP, 1);
8418 processSync(mapper);
8419 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8420 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008421 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008422
8423 // finger
8424 processKey(mapper, BTN_TOOL_QUADTAP, 0);
8425 processKey(mapper, BTN_TOOL_FINGER, 1);
8426 processSync(mapper);
8427 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8428 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008429 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008430
8431 // stylus trumps finger
8432 processKey(mapper, BTN_TOOL_PEN, 1);
8433 processSync(mapper);
8434 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8435 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008436 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008437
8438 // eraser trumps stylus
8439 processKey(mapper, BTN_TOOL_RUBBER, 1);
8440 processSync(mapper);
8441 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8442 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008443 ASSERT_EQ(ToolType::ERASER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008444
8445 // mouse trumps eraser
8446 processKey(mapper, BTN_TOOL_MOUSE, 1);
8447 processSync(mapper);
8448 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8449 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008450 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008451
8452 // MT tool type trumps BTN tool types: MT_TOOL_FINGER
8453 processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
8454 processSync(mapper);
8455 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8456 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008457 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008458
8459 // MT tool type trumps BTN tool types: MT_TOOL_PEN
8460 processToolType(mapper, MT_TOOL_PEN);
8461 processSync(mapper);
8462 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8463 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008464 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008465
8466 // back to default tool type
8467 processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
8468 processKey(mapper, BTN_TOOL_MOUSE, 0);
8469 processKey(mapper, BTN_TOOL_RUBBER, 0);
8470 processKey(mapper, BTN_TOOL_PEN, 0);
8471 processKey(mapper, BTN_TOOL_FINGER, 0);
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::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008476}
8477
8478TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008479 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008480 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008481 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008482 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Arpit Singha8c236b2023-04-25 13:56:05 +00008483 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008484
8485 NotifyMotionArgs motionArgs;
8486
8487 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
8488 processId(mapper, 1);
8489 processPosition(mapper, 100, 200);
8490 processSync(mapper);
8491 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8492 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8493 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8494 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8495
8496 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8497 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8498 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8499 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8500
8501 // move a little
8502 processPosition(mapper, 150, 250);
8503 processSync(mapper);
8504 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8505 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8506 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8507 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8508
8509 // down when BTN_TOUCH is pressed, pressure defaults to 1
8510 processKey(mapper, BTN_TOUCH, 1);
8511 processSync(mapper);
8512 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8513 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8514 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8515 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8516
8517 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8518 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8519 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8520 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8521
8522 // up when BTN_TOUCH is released, hover restored
8523 processKey(mapper, BTN_TOUCH, 0);
8524 processSync(mapper);
8525 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8526 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8527 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8528 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8529
8530 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8531 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8532 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8533 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8534
8535 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8536 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8537 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8538 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8539
8540 // exit hover when pointer goes away
8541 processId(mapper, -1);
8542 processSync(mapper);
8543 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8544 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8545 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8546 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8547}
8548
8549TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008550 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008551 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008552 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +00008553 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008554
8555 NotifyMotionArgs motionArgs;
8556
8557 // initially hovering because pressure is 0
8558 processId(mapper, 1);
8559 processPosition(mapper, 100, 200);
8560 processPressure(mapper, 0);
8561 processSync(mapper);
8562 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8563 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8564 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8565 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8566
8567 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8568 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8569 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8570 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8571
8572 // move a little
8573 processPosition(mapper, 150, 250);
8574 processSync(mapper);
8575 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8576 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8577 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8578 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8579
8580 // down when pressure becomes non-zero
8581 processPressure(mapper, RAW_PRESSURE_MAX);
8582 processSync(mapper);
8583 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8584 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8585 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8586 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8587
8588 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8589 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8590 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8591 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8592
8593 // up when pressure becomes 0, hover restored
8594 processPressure(mapper, 0);
8595 processSync(mapper);
8596 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8597 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8598 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8599 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8600
8601 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8602 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8603 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8604 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8605
8606 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8607 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8608 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8609 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8610
8611 // exit hover when pointer goes away
8612 processId(mapper, -1);
8613 processSync(mapper);
8614 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8615 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8616 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8617 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8618}
8619
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008620/**
8621 * Set the input device port <--> display port associations, and check that the
8622 * events are routed to the display that matches the display port.
8623 * This can be checked by looking at the displayId of the resulting NotifyMotionArgs.
8624 */
8625TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayPort) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008626 const std::string usb2 = "USB2";
8627 const uint8_t hdmi1 = 0;
8628 const uint8_t hdmi2 = 1;
8629 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008630 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008631
8632 addConfigurationProperty("touch.deviceType", "touchScreen");
8633 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00008634 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008635
8636 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
8637 mFakePolicy->addInputPortAssociation(usb2, hdmi2);
8638
8639 // We are intentionally not adding the viewport for display 1 yet. Since the port association
8640 // for this input device is specified, and the matching viewport is not present,
8641 // the input device should be disabled (at the mapper level).
8642
8643 // Add viewport for display 2 on hdmi2
8644 prepareSecondaryDisplay(type, hdmi2);
8645 // Send a touch event
8646 processPosition(mapper, 100, 100);
8647 processSync(mapper);
8648 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8649
8650 // Add viewport for display 1 on hdmi1
Michael Wrighta9cf4192022-12-01 23:46:39 +00008651 prepareDisplay(ui::ROTATION_0, hdmi1);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008652 // Send a touch event again
8653 processPosition(mapper, 100, 100);
8654 processSync(mapper);
8655
8656 NotifyMotionArgs args;
8657 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8658 ASSERT_EQ(DISPLAY_ID, args.displayId);
8659}
Michael Wrightd02c5b62014-02-10 15:10:22 -08008660
Arthur Hung6d5b4b22022-01-21 07:21:10 +00008661TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayUniqueId) {
8662 addConfigurationProperty("touch.deviceType", "touchScreen");
8663 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00008664 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung6d5b4b22022-01-21 07:21:10 +00008665
8666 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
8667
Michael Wrighta9cf4192022-12-01 23:46:39 +00008668 prepareDisplay(ui::ROTATION_0);
8669 prepareVirtualDisplay(ui::ROTATION_0);
Arthur Hung6d5b4b22022-01-21 07:21:10 +00008670
8671 // Send a touch event
8672 processPosition(mapper, 100, 100);
8673 processSync(mapper);
8674
8675 NotifyMotionArgs args;
8676 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8677 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
8678}
8679
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008680TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShouldHandleDisplayId) {
Garfield Tan888a6a42020-01-09 11:39:16 -08008681 // Setup for second display.
Michael Wright17db18e2020-06-26 20:51:44 +01008682 std::shared_ptr<FakePointerController> fakePointerController =
8683 std::make_shared<FakePointerController>();
Garfield Tan888a6a42020-01-09 11:39:16 -08008684 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008685 fakePointerController->setPosition(100, 200);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00008686 mFakePolicy->setPointerController(fakePointerController);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008687
Garfield Tan888a6a42020-01-09 11:39:16 -08008688 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008689 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08008690
Michael Wrighta9cf4192022-12-01 23:46:39 +00008691 prepareDisplay(ui::ROTATION_0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008692 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00008693 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008694
Josep del Río2d8c79a2023-01-23 19:33:50 +00008695 // Check source is mouse that would obtain the PointerController.
8696 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008697
8698 NotifyMotionArgs motionArgs;
8699 processPosition(mapper, 100, 100);
8700 processSync(mapper);
8701
8702 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8703 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8704 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
8705}
8706
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00008707/**
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008708 * Ensure that the readTime is set to the SYN_REPORT value when processing touch events.
8709 */
8710TEST_F(MultiTouchInputMapperTest, Process_SendsReadTime) {
8711 addConfigurationProperty("touch.deviceType", "touchScreen");
8712 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00008713 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008714
Michael Wrighta9cf4192022-12-01 23:46:39 +00008715 prepareDisplay(ui::ROTATION_0);
Harry Cutts33476232023-01-30 19:57:29 +00008716 process(mapper, 10, /*readTime=*/11, EV_ABS, ABS_MT_TRACKING_ID, 1);
8717 process(mapper, 15, /*readTime=*/16, EV_ABS, ABS_MT_POSITION_X, 100);
8718 process(mapper, 20, /*readTime=*/21, EV_ABS, ABS_MT_POSITION_Y, 100);
8719 process(mapper, 25, /*readTime=*/26, EV_SYN, SYN_REPORT, 0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008720
8721 NotifyMotionArgs args;
8722 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8723 ASSERT_EQ(26, args.readTime);
8724
Harry Cutts33476232023-01-30 19:57:29 +00008725 process(mapper, 30, /*readTime=*/31, EV_ABS, ABS_MT_POSITION_X, 110);
8726 process(mapper, 30, /*readTime=*/32, EV_ABS, ABS_MT_POSITION_Y, 220);
8727 process(mapper, 30, /*readTime=*/33, EV_SYN, SYN_REPORT, 0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008728
8729 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8730 ASSERT_EQ(33, args.readTime);
8731}
8732
8733/**
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00008734 * When the viewport is not active (isActive=false), the touch mapper should be disabled and the
8735 * events should not be delivered to the listener.
8736 */
8737TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreDropped) {
8738 addConfigurationProperty("touch.deviceType", "touchScreen");
Yuncheol Heo50c19b12022-11-02 20:33:08 -07008739 // Don't set touch.enableForInactiveViewport to verify the default behavior.
Michael Wrighta9cf4192022-12-01 23:46:39 +00008740 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +00008741 /*isActive=*/false, UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00008742 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00008743 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00008744 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00008745
8746 NotifyMotionArgs motionArgs;
8747 processPosition(mapper, 100, 100);
8748 processSync(mapper);
8749
8750 mFakeListener->assertNotifyMotionWasNotCalled();
8751}
8752
Yuncheol Heo50c19b12022-11-02 20:33:08 -07008753/**
8754 * When the viewport is not active (isActive=false) and touch.enableForInactiveViewport is true,
8755 * the touch mapper can process the events and the events can be delivered to the listener.
8756 */
8757TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreProcessed) {
8758 addConfigurationProperty("touch.deviceType", "touchScreen");
8759 addConfigurationProperty("touch.enableForInactiveViewport", "1");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008760 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +00008761 /*isActive=*/false, UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00008762 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Yuncheol Heo50c19b12022-11-02 20:33:08 -07008763 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00008764 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Yuncheol Heo50c19b12022-11-02 20:33:08 -07008765
8766 NotifyMotionArgs motionArgs;
8767 processPosition(mapper, 100, 100);
8768 processSync(mapper);
8769
8770 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8771 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8772}
8773
Josh Thielene986aed2023-06-01 14:17:30 +00008774/**
8775 * When the viewport is deactivated (isActive transitions from true to false),
8776 * and touch.enableForInactiveViewport is false, touches prior to the transition
8777 * should be cancelled.
8778 */
Garfield Tanc734e4f2021-01-15 20:01:39 -08008779TEST_F(MultiTouchInputMapperTest, Process_DeactivateViewport_AbortTouches) {
8780 addConfigurationProperty("touch.deviceType", "touchScreen");
Yuncheol Heo50c19b12022-11-02 20:33:08 -07008781 addConfigurationProperty("touch.enableForInactiveViewport", "0");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008782 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +00008783 /*isActive=*/true, UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Garfield Tanc734e4f2021-01-15 20:01:39 -08008784 std::optional<DisplayViewport> optionalDisplayViewport =
8785 mFakePolicy->getDisplayViewportByUniqueId(UNIQUE_ID);
8786 ASSERT_TRUE(optionalDisplayViewport.has_value());
8787 DisplayViewport displayViewport = *optionalDisplayViewport;
8788
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00008789 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Garfield Tanc734e4f2021-01-15 20:01:39 -08008790 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00008791 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Garfield Tanc734e4f2021-01-15 20:01:39 -08008792
8793 // Finger down
8794 int32_t x = 100, y = 100;
8795 processPosition(mapper, x, y);
8796 processSync(mapper);
8797
8798 NotifyMotionArgs motionArgs;
8799 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8800 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8801
8802 // Deactivate display viewport
8803 displayViewport.isActive = false;
8804 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00008805 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Garfield Tanc734e4f2021-01-15 20:01:39 -08008806
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00008807 // The ongoing touch should be canceled immediately
8808 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8809 EXPECT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
8810
8811 // Finger move is ignored
Garfield Tanc734e4f2021-01-15 20:01:39 -08008812 x += 10, y += 10;
8813 processPosition(mapper, x, y);
8814 processSync(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00008815 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
Garfield Tanc734e4f2021-01-15 20:01:39 -08008816
8817 // Reactivate display viewport
8818 displayViewport.isActive = true;
8819 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00008820 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Garfield Tanc734e4f2021-01-15 20:01:39 -08008821
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00008822 // Finger move again starts new gesture
Garfield Tanc734e4f2021-01-15 20:01:39 -08008823 x += 10, y += 10;
8824 processPosition(mapper, x, y);
8825 processSync(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00008826 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8827 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Garfield Tanc734e4f2021-01-15 20:01:39 -08008828}
8829
Josh Thielene986aed2023-06-01 14:17:30 +00008830/**
8831 * When the viewport is deactivated (isActive transitions from true to false),
8832 * and touch.enableForInactiveViewport is true, touches prior to the transition
8833 * should not be cancelled.
8834 */
8835TEST_F(MultiTouchInputMapperTest, Process_DeactivateViewport_TouchesNotAborted) {
8836 addConfigurationProperty("touch.deviceType", "touchScreen");
8837 addConfigurationProperty("touch.enableForInactiveViewport", "1");
8838 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
8839 /*isActive=*/true, UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
8840 std::optional<DisplayViewport> optionalDisplayViewport =
8841 mFakePolicy->getDisplayViewportByUniqueId(UNIQUE_ID);
8842 ASSERT_TRUE(optionalDisplayViewport.has_value());
8843 DisplayViewport displayViewport = *optionalDisplayViewport;
8844
8845 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
8846 prepareAxes(POSITION);
8847 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
8848
8849 // Finger down
8850 int32_t x = 100, y = 100;
8851 processPosition(mapper, x, y);
8852 processSync(mapper);
8853 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8854 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
8855
8856 // Deactivate display viewport
8857 displayViewport.isActive = false;
8858 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
8859 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
8860
8861 // The ongoing touch should not be canceled
8862 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8863
8864 // Finger move is not ignored
8865 x += 10, y += 10;
8866 processPosition(mapper, x, y);
8867 processSync(mapper);
8868 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8869 WithMotionAction(AMOTION_EVENT_ACTION_MOVE)));
8870
8871 // Reactivate display viewport
8872 displayViewport.isActive = true;
8873 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
8874 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
8875
8876 // Finger move continues and does not start new gesture
8877 x += 10, y += 10;
8878 processPosition(mapper, x, y);
8879 processSync(mapper);
8880 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8881 WithMotionAction(AMOTION_EVENT_ACTION_MOVE)));
8882}
8883
Arthur Hung7c645402019-01-25 17:45:42 +08008884TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShowTouches) {
8885 // Setup the first touch screen device.
Arthur Hung7c645402019-01-25 17:45:42 +08008886 prepareAxes(POSITION | ID | SLOT);
8887 addConfigurationProperty("touch.deviceType", "touchScreen");
Arpit Singha8c236b2023-04-25 13:56:05 +00008888 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung7c645402019-01-25 17:45:42 +08008889
8890 // Create the second touch screen device, and enable multi fingers.
8891 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08008892 const std::string DEVICE_NAME2 = "TOUCHSCREEN2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08008893 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008894 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08008895 std::shared_ptr<InputDevice> device2 =
8896 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07008897 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08008898
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008899 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
Harry Cutts33476232023-01-30 19:57:29 +00008900 /*flat=*/0, /*fuzz=*/0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008901 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
Harry Cutts33476232023-01-30 19:57:29 +00008902 /*flat=*/0, /*fuzz=*/0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008903 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX,
Harry Cutts33476232023-01-30 19:57:29 +00008904 /*flat=*/0, /*fuzz=*/0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008905 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX,
Harry Cutts33476232023-01-30 19:57:29 +00008906 /*flat=*/0, /*fuzz=*/0);
8907 mFakeEventHub->setAbsoluteAxisValue(SECOND_EVENTHUB_ID, ABS_MT_SLOT, /*value=*/0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008908 mFakeEventHub->addConfigurationProperty(SECOND_EVENTHUB_ID, String8("touch.deviceType"),
8909 String8("touchScreen"));
Arthur Hung7c645402019-01-25 17:45:42 +08008910
8911 // Setup the second touch screen device.
Arpit Singha8c236b2023-04-25 13:56:05 +00008912 device2->addEmptyEventHubDevice(SECOND_EVENTHUB_ID);
8913 MultiTouchInputMapper& mapper2 = device2->constructAndAddMapper<
8914 MultiTouchInputMapper>(SECOND_EVENTHUB_ID, mFakePolicy->getReaderConfiguration());
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07008915 std::list<NotifyArgs> unused =
8916 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00008917 /*changes=*/{});
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07008918 unused += device2->reset(ARBITRARY_TIME);
Arthur Hung7c645402019-01-25 17:45:42 +08008919
8920 // Setup PointerController.
Michael Wright17db18e2020-06-26 20:51:44 +01008921 std::shared_ptr<FakePointerController> fakePointerController =
8922 std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00008923 mFakePolicy->setPointerController(fakePointerController);
Arthur Hung7c645402019-01-25 17:45:42 +08008924
8925 // Setup policy for associated displays and show touches.
8926 const uint8_t hdmi1 = 0;
8927 const uint8_t hdmi2 = 1;
8928 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
8929 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
8930 mFakePolicy->setShowTouches(true);
8931
8932 // Create displays.
Michael Wrighta9cf4192022-12-01 23:46:39 +00008933 prepareDisplay(ui::ROTATION_0, hdmi1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008934 prepareSecondaryDisplay(ViewportType::EXTERNAL, hdmi2);
Arthur Hung7c645402019-01-25 17:45:42 +08008935
8936 // Default device will reconfigure above, need additional reconfiguration for another device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07008937 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00008938 InputReaderConfiguration::Change::DISPLAY_INFO |
8939 InputReaderConfiguration::Change::SHOW_TOUCHES);
Arthur Hung7c645402019-01-25 17:45:42 +08008940
8941 // Two fingers down at default display.
8942 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
8943 processPosition(mapper, x1, y1);
8944 processId(mapper, 1);
8945 processSlot(mapper, 1);
8946 processPosition(mapper, x2, y2);
8947 processId(mapper, 2);
8948 processSync(mapper);
8949
8950 std::map<int32_t, std::vector<int32_t>>::const_iterator iter =
8951 fakePointerController->getSpots().find(DISPLAY_ID);
8952 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
8953 ASSERT_EQ(size_t(2), iter->second.size());
8954
8955 // Two fingers down at second display.
8956 processPosition(mapper2, x1, y1);
8957 processId(mapper2, 1);
8958 processSlot(mapper2, 1);
8959 processPosition(mapper2, x2, y2);
8960 processId(mapper2, 2);
8961 processSync(mapper2);
8962
8963 iter = fakePointerController->getSpots().find(SECONDARY_DISPLAY_ID);
8964 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
8965 ASSERT_EQ(size_t(2), iter->second.size());
Prabir Pradhan197e0862022-07-01 14:28:00 +00008966
8967 // Disable the show touches configuration and ensure the spots are cleared.
8968 mFakePolicy->setShowTouches(false);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07008969 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00008970 InputReaderConfiguration::Change::SHOW_TOUCHES);
Prabir Pradhan197e0862022-07-01 14:28:00 +00008971
8972 ASSERT_TRUE(fakePointerController->getSpots().empty());
Arthur Hung7c645402019-01-25 17:45:42 +08008973}
8974
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008975TEST_F(MultiTouchInputMapperTest, VideoFrames_ReceivedByListener) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008976 prepareAxes(POSITION);
8977 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008978 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +00008979 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008980
8981 NotifyMotionArgs motionArgs;
8982 // Unrotated video frame
8983 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8984 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008985 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008986 processPosition(mapper, 100, 200);
8987 processSync(mapper);
8988 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8989 ASSERT_EQ(frames, motionArgs.videoFrames);
8990
8991 // Subsequent touch events should not have any videoframes
8992 // This is implemented separately in FakeEventHub,
8993 // but that should match the behaviour of TouchVideoDevice.
8994 processPosition(mapper, 200, 200);
8995 processSync(mapper);
8996 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8997 ASSERT_EQ(std::vector<TouchVideoFrame>(), motionArgs.videoFrames);
8998}
8999
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009000TEST_F(MultiTouchInputMapperTest, VideoFrames_AreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009001 prepareAxes(POSITION);
9002 addConfigurationProperty("touch.deviceType", "touchScreen");
Arpit Singha8c236b2023-04-25 13:56:05 +00009003 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009004 // Unrotated video frame
9005 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9006 NotifyMotionArgs motionArgs;
9007
9008 // Test all 4 orientations
Michael Wrighta9cf4192022-12-01 23:46:39 +00009009 for (ui::Rotation orientation : ftl::enum_range<ui::Rotation>()) {
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009010 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
9011 clearViewports();
9012 prepareDisplay(orientation);
9013 std::vector<TouchVideoFrame> frames{frame};
9014 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
9015 processPosition(mapper, 100, 200);
9016 processSync(mapper);
9017 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9018 ASSERT_EQ(frames, motionArgs.videoFrames);
9019 }
9020}
9021
9022TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_AreRotated) {
9023 prepareAxes(POSITION);
9024 addConfigurationProperty("touch.deviceType", "touchScreen");
9025 // Since InputReader works in the un-rotated coordinate space, only devices that are not
9026 // orientation-aware are affected by display rotation.
9027 addConfigurationProperty("touch.orientationAware", "0");
Arpit Singha8c236b2023-04-25 13:56:05 +00009028 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009029 // 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>()) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009035 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
9036 clearViewports();
9037 prepareDisplay(orientation);
9038 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009039 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009040 processPosition(mapper, 100, 200);
9041 processSync(mapper);
9042 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009043 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
9044 // compared to the display. This is so that when the window transform (which contains the
9045 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
9046 // window's coordinate space.
9047 frames[0].rotate(getInverseRotation(orientation));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009048 ASSERT_EQ(frames, motionArgs.videoFrames);
lilinnan687e58f2022-07-19 16:00:50 +08009049
9050 // Release finger.
9051 processSync(mapper);
9052 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009053 }
9054}
9055
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009056TEST_F(MultiTouchInputMapperTest, VideoFrames_MultipleFramesAreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009057 prepareAxes(POSITION);
9058 addConfigurationProperty("touch.deviceType", "touchScreen");
Arpit Singha8c236b2023-04-25 13:56:05 +00009059 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009060 // Unrotated video frames. There's no rule that they must all have the same dimensions,
9061 // so mix these.
9062 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9063 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
9064 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
9065 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
9066 NotifyMotionArgs motionArgs;
9067
Michael Wrighta9cf4192022-12-01 23:46:39 +00009068 prepareDisplay(ui::ROTATION_90);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009069 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009070 processPosition(mapper, 100, 200);
9071 processSync(mapper);
9072 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009073 ASSERT_EQ(frames, motionArgs.videoFrames);
9074}
9075
9076TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_MultipleFramesAreRotated) {
9077 prepareAxes(POSITION);
9078 addConfigurationProperty("touch.deviceType", "touchScreen");
9079 // Since InputReader works in the un-rotated coordinate space, only devices that are not
9080 // orientation-aware are affected by display rotation.
9081 addConfigurationProperty("touch.orientationAware", "0");
Arpit Singha8c236b2023-04-25 13:56:05 +00009082 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009083 // Unrotated video frames. There's no rule that they must all have the same dimensions,
9084 // so mix these.
9085 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9086 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
9087 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
9088 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
9089 NotifyMotionArgs motionArgs;
9090
Michael Wrighta9cf4192022-12-01 23:46:39 +00009091 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009092 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
9093 processPosition(mapper, 100, 200);
9094 processSync(mapper);
9095 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9096 std::for_each(frames.begin(), frames.end(), [](TouchVideoFrame& frame) {
9097 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
9098 // compared to the display. This is so that when the window transform (which contains the
9099 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
9100 // window's coordinate space.
Michael Wrighta9cf4192022-12-01 23:46:39 +00009101 frame.rotate(getInverseRotation(ui::ROTATION_90));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009102 });
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009103 ASSERT_EQ(frames, motionArgs.videoFrames);
9104}
9105
Arthur Hung9da14732019-09-02 16:16:58 +08009106/**
9107 * If we had defined port associations, but the viewport is not ready, the touch device would be
9108 * expected to be disabled, and it should be enabled after the viewport has found.
9109 */
9110TEST_F(MultiTouchInputMapperTest, Configure_EnabledForAssociatedDisplay) {
Arthur Hung9da14732019-09-02 16:16:58 +08009111 constexpr uint8_t hdmi2 = 1;
9112 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009113 constexpr ViewportType type = ViewportType::EXTERNAL;
Arthur Hung9da14732019-09-02 16:16:58 +08009114
9115 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi2);
9116
9117 addConfigurationProperty("touch.deviceType", "touchScreen");
9118 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00009119 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung9da14732019-09-02 16:16:58 +08009120
9121 ASSERT_EQ(mDevice->isEnabled(), false);
9122
9123 // Add display on hdmi2, the device should be enabled and can receive touch event.
9124 prepareSecondaryDisplay(type, hdmi2);
9125 ASSERT_EQ(mDevice->isEnabled(), true);
9126
9127 // Send a touch event.
9128 processPosition(mapper, 100, 100);
9129 processSync(mapper);
9130
9131 NotifyMotionArgs args;
9132 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9133 ASSERT_EQ(SECONDARY_DISPLAY_ID, args.displayId);
9134}
9135
Arthur Hung421eb1c2020-01-16 00:09:42 +08009136TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleSingleTouch) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08009137 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009138 prepareDisplay(ui::ROTATION_0);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009139 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +00009140 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08009141
9142 NotifyMotionArgs motionArgs;
9143
9144 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
9145 // finger down
9146 processId(mapper, 1);
9147 processPosition(mapper, x1, y1);
9148 processSync(mapper);
9149 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9150 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009151 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009152
9153 // finger move
9154 processId(mapper, 1);
9155 processPosition(mapper, x2, y2);
9156 processSync(mapper);
9157 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9158 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009159 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009160
9161 // finger up.
9162 processId(mapper, -1);
9163 processSync(mapper);
9164 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9165 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009166 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009167
9168 // new finger down
9169 processId(mapper, 1);
9170 processPosition(mapper, x3, y3);
9171 processSync(mapper);
9172 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9173 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009174 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009175}
9176
9177/**
arthurhungcc7f9802020-04-30 17:55:40 +08009178 * Test single touch should be canceled when received the MT_TOOL_PALM event, and the following
9179 * MOVE and UP events should be ignored.
Arthur Hung421eb1c2020-01-16 00:09:42 +08009180 */
arthurhungcc7f9802020-04-30 17:55:40 +08009181TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_SinglePointer) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08009182 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009183 prepareDisplay(ui::ROTATION_0);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009184 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +00009185 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08009186
9187 NotifyMotionArgs motionArgs;
9188
9189 // default tool type is finger
9190 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
arthurhungcc7f9802020-04-30 17:55:40 +08009191 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009192 processPosition(mapper, x1, y1);
9193 processSync(mapper);
9194 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9195 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009196 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009197
9198 // Tool changed to MT_TOOL_PALM expect sending the cancel event.
9199 processToolType(mapper, MT_TOOL_PALM);
9200 processSync(mapper);
9201 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9202 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9203
9204 // Ignore the following MOVE and UP events if had detect a palm event.
arthurhungcc7f9802020-04-30 17:55:40 +08009205 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009206 processPosition(mapper, x2, y2);
9207 processSync(mapper);
9208 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9209
9210 // finger up.
arthurhungcc7f9802020-04-30 17:55:40 +08009211 processId(mapper, INVALID_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009212 processSync(mapper);
9213 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9214
9215 // new finger down
arthurhungcc7f9802020-04-30 17:55:40 +08009216 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009217 processToolType(mapper, MT_TOOL_FINGER);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009218 processPosition(mapper, x3, y3);
9219 processSync(mapper);
9220 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9221 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009222 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009223}
9224
arthurhungbf89a482020-04-17 17:37:55 +08009225/**
arthurhungcc7f9802020-04-30 17:55:40 +08009226 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
9227 * and the rest active fingers could still be allowed to receive the events
arthurhungbf89a482020-04-17 17:37:55 +08009228 */
arthurhungcc7f9802020-04-30 17:55:40 +08009229TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_TwoPointers) {
arthurhungbf89a482020-04-17 17:37:55 +08009230 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009231 prepareDisplay(ui::ROTATION_0);
arthurhungbf89a482020-04-17 17:37:55 +08009232 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +00009233 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
arthurhungbf89a482020-04-17 17:37:55 +08009234
9235 NotifyMotionArgs motionArgs;
9236
9237 // default tool type is finger
arthurhungcc7f9802020-04-30 17:55:40 +08009238 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
9239 processId(mapper, FIRST_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009240 processPosition(mapper, x1, y1);
9241 processSync(mapper);
9242 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9243 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009244 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungbf89a482020-04-17 17:37:55 +08009245
9246 // Second finger down.
arthurhungcc7f9802020-04-30 17:55:40 +08009247 processSlot(mapper, SECOND_SLOT);
9248 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009249 processPosition(mapper, x2, y2);
arthurhungcc7f9802020-04-30 17:55:40 +08009250 processSync(mapper);
9251 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009252 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009253 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08009254
9255 // If the tool type of the first finger changes to MT_TOOL_PALM,
9256 // we expect to receive ACTION_POINTER_UP with cancel flag.
9257 processSlot(mapper, FIRST_SLOT);
9258 processId(mapper, FIRST_TRACKING_ID);
9259 processToolType(mapper, MT_TOOL_PALM);
9260 processSync(mapper);
9261 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009262 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009263 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9264
9265 // The following MOVE events of second finger should be processed.
9266 processSlot(mapper, SECOND_SLOT);
9267 processId(mapper, SECOND_TRACKING_ID);
9268 processPosition(mapper, x2 + 1, y2 + 1);
9269 processSync(mapper);
9270 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9271 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009272 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +08009273
9274 // First finger up. It used to be in palm mode, and we already generated ACTION_POINTER_UP for
9275 // it. Second finger receive move.
9276 processSlot(mapper, FIRST_SLOT);
9277 processId(mapper, INVALID_TRACKING_ID);
9278 processSync(mapper);
9279 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9280 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009281 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +08009282
9283 // Second finger keeps moving.
9284 processSlot(mapper, SECOND_SLOT);
9285 processId(mapper, SECOND_TRACKING_ID);
9286 processPosition(mapper, x2 + 2, y2 + 2);
9287 processSync(mapper);
9288 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9289 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009290 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +08009291
9292 // Second finger up.
9293 processId(mapper, INVALID_TRACKING_ID);
9294 processSync(mapper);
9295 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9296 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9297 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9298}
9299
9300/**
9301 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event, if only 1 finger
9302 * is active, it should send CANCEL after receiving the MT_TOOL_PALM event.
9303 */
9304TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_ShouldCancelWhenAllTouchIsPalm) {
9305 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009306 prepareDisplay(ui::ROTATION_0);
arthurhungcc7f9802020-04-30 17:55:40 +08009307 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +00009308 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
arthurhungcc7f9802020-04-30 17:55:40 +08009309
9310 NotifyMotionArgs motionArgs;
9311
9312 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
9313 // First finger down.
9314 processId(mapper, FIRST_TRACKING_ID);
9315 processPosition(mapper, x1, y1);
9316 processSync(mapper);
9317 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9318 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009319 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08009320
9321 // Second finger down.
9322 processSlot(mapper, SECOND_SLOT);
9323 processId(mapper, SECOND_TRACKING_ID);
9324 processPosition(mapper, x2, y2);
arthurhungbf89a482020-04-17 17:37:55 +08009325 processSync(mapper);
9326 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009327 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009328 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungbf89a482020-04-17 17:37:55 +08009329
arthurhungcc7f9802020-04-30 17:55:40 +08009330 // If the tool type of the first finger changes to MT_TOOL_PALM,
9331 // we expect to receive ACTION_POINTER_UP with cancel flag.
9332 processSlot(mapper, FIRST_SLOT);
9333 processId(mapper, FIRST_TRACKING_ID);
9334 processToolType(mapper, MT_TOOL_PALM);
9335 processSync(mapper);
9336 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009337 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009338 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9339
9340 // Second finger keeps moving.
9341 processSlot(mapper, SECOND_SLOT);
9342 processId(mapper, SECOND_TRACKING_ID);
9343 processPosition(mapper, x2 + 1, y2 + 1);
9344 processSync(mapper);
9345 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9346 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9347
9348 // second finger becomes palm, receive cancel due to only 1 finger is active.
9349 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009350 processToolType(mapper, MT_TOOL_PALM);
9351 processSync(mapper);
9352 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9353 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9354
arthurhungcc7f9802020-04-30 17:55:40 +08009355 // third finger down.
9356 processSlot(mapper, THIRD_SLOT);
9357 processId(mapper, THIRD_TRACKING_ID);
9358 processToolType(mapper, MT_TOOL_FINGER);
arthurhungbf89a482020-04-17 17:37:55 +08009359 processPosition(mapper, x3, y3);
9360 processSync(mapper);
arthurhungbf89a482020-04-17 17:37:55 +08009361 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9362 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009363 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009364 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +08009365
9366 // third finger move
9367 processId(mapper, THIRD_TRACKING_ID);
9368 processPosition(mapper, x3 + 1, y3 + 1);
9369 processSync(mapper);
9370 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9371 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9372
9373 // first finger up, third finger receive move.
9374 processSlot(mapper, FIRST_SLOT);
9375 processId(mapper, INVALID_TRACKING_ID);
9376 processSync(mapper);
9377 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9378 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009379 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +08009380
9381 // second finger up, third finger receive move.
9382 processSlot(mapper, SECOND_SLOT);
9383 processId(mapper, INVALID_TRACKING_ID);
9384 processSync(mapper);
9385 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9386 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009387 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +08009388
9389 // third finger up.
9390 processSlot(mapper, THIRD_SLOT);
9391 processId(mapper, INVALID_TRACKING_ID);
9392 processSync(mapper);
9393 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9394 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9395 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9396}
9397
9398/**
9399 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
9400 * and the active finger could still be allowed to receive the events
9401 */
9402TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_KeepFirstPointer) {
9403 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009404 prepareDisplay(ui::ROTATION_0);
arthurhungcc7f9802020-04-30 17:55:40 +08009405 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +00009406 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
arthurhungcc7f9802020-04-30 17:55:40 +08009407
9408 NotifyMotionArgs motionArgs;
9409
9410 // default tool type is finger
9411 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
9412 processId(mapper, FIRST_TRACKING_ID);
9413 processPosition(mapper, x1, y1);
9414 processSync(mapper);
9415 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9416 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009417 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08009418
9419 // Second finger down.
9420 processSlot(mapper, SECOND_SLOT);
9421 processId(mapper, SECOND_TRACKING_ID);
9422 processPosition(mapper, x2, y2);
9423 processSync(mapper);
9424 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009425 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009426 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08009427
9428 // If the tool type of the second finger changes to MT_TOOL_PALM,
9429 // we expect to receive ACTION_POINTER_UP with cancel flag.
9430 processId(mapper, SECOND_TRACKING_ID);
9431 processToolType(mapper, MT_TOOL_PALM);
9432 processSync(mapper);
9433 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009434 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009435 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9436
9437 // The following MOVE event should be processed.
9438 processSlot(mapper, FIRST_SLOT);
9439 processId(mapper, FIRST_TRACKING_ID);
9440 processPosition(mapper, x1 + 1, y1 + 1);
9441 processSync(mapper);
9442 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9443 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009444 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +08009445
9446 // second finger up.
9447 processSlot(mapper, SECOND_SLOT);
9448 processId(mapper, INVALID_TRACKING_ID);
9449 processSync(mapper);
9450 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9451 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9452
9453 // first finger keep moving
9454 processSlot(mapper, FIRST_SLOT);
9455 processId(mapper, FIRST_TRACKING_ID);
9456 processPosition(mapper, x1 + 2, y1 + 2);
9457 processSync(mapper);
9458 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9459 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9460
9461 // first finger up.
9462 processId(mapper, INVALID_TRACKING_ID);
9463 processSync(mapper);
9464 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9465 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9466 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
arthurhungbf89a482020-04-17 17:37:55 +08009467}
9468
Arthur Hung9ad18942021-06-19 02:04:46 +00009469/**
9470 * Test multi-touch should sent ACTION_POINTER_UP/ACTION_UP when received the INVALID_TRACKING_ID,
9471 * to prevent the driver side may send unexpected data after set tracking id as INVALID_TRACKING_ID
9472 * cause slot be valid again.
9473 */
9474TEST_F(MultiTouchInputMapperTest, Process_MultiTouch_WithInvalidTrackingId) {
9475 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009476 prepareDisplay(ui::ROTATION_0);
Arthur Hung9ad18942021-06-19 02:04:46 +00009477 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +00009478 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung9ad18942021-06-19 02:04:46 +00009479
9480 NotifyMotionArgs motionArgs;
9481
9482 constexpr int32_t x1 = 100, y1 = 200, x2 = 0, y2 = 0;
9483 // First finger down.
9484 processId(mapper, FIRST_TRACKING_ID);
9485 processPosition(mapper, x1, y1);
9486 processPressure(mapper, RAW_PRESSURE_MAX);
9487 processSync(mapper);
9488 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9489 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009490 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
Arthur Hung9ad18942021-06-19 02:04:46 +00009491
9492 // First finger move.
9493 processId(mapper, FIRST_TRACKING_ID);
9494 processPosition(mapper, x1 + 1, y1 + 1);
9495 processPressure(mapper, RAW_PRESSURE_MAX);
9496 processSync(mapper);
9497 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9498 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009499 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
Arthur Hung9ad18942021-06-19 02:04:46 +00009500
9501 // Second finger down.
9502 processSlot(mapper, SECOND_SLOT);
9503 processId(mapper, SECOND_TRACKING_ID);
9504 processPosition(mapper, x2, y2);
9505 processPressure(mapper, RAW_PRESSURE_MAX);
9506 processSync(mapper);
9507 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009508 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009509 ASSERT_EQ(uint32_t(2), motionArgs.getPointerCount());
Arthur Hung9ad18942021-06-19 02:04:46 +00009510
9511 // second finger up with some unexpected data.
9512 processSlot(mapper, SECOND_SLOT);
9513 processId(mapper, INVALID_TRACKING_ID);
9514 processPosition(mapper, x2, y2);
9515 processSync(mapper);
9516 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009517 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009518 ASSERT_EQ(uint32_t(2), motionArgs.getPointerCount());
Arthur Hung9ad18942021-06-19 02:04:46 +00009519
9520 // first finger up with some unexpected data.
9521 processSlot(mapper, FIRST_SLOT);
9522 processId(mapper, INVALID_TRACKING_ID);
9523 processPosition(mapper, x2, y2);
9524 processPressure(mapper, RAW_PRESSURE_MAX);
9525 processSync(mapper);
9526 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9527 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009528 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
Arthur Hung9ad18942021-06-19 02:04:46 +00009529}
9530
Arpit Singh4bb0bd52023-12-20 14:41:10 +00009531TEST_F(MultiTouchInputMapperTest, Reset_PreservesLastTouchState) {
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009532 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009533 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009534 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +00009535 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009536
9537 // First finger down.
9538 processId(mapper, FIRST_TRACKING_ID);
9539 processPosition(mapper, 100, 200);
9540 processPressure(mapper, RAW_PRESSURE_MAX);
9541 processSync(mapper);
9542 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9543 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
9544
9545 // Second finger down.
9546 processSlot(mapper, SECOND_SLOT);
9547 processId(mapper, SECOND_TRACKING_ID);
9548 processPosition(mapper, 300, 400);
9549 processPressure(mapper, RAW_PRESSURE_MAX);
9550 processSync(mapper);
9551 ASSERT_NO_FATAL_FAILURE(
9552 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(ACTION_POINTER_1_DOWN)));
9553
Arpit Singh4bb0bd52023-12-20 14:41:10 +00009554 // Reset the mapper. When the mapper is reset, we expect the current multi-touch state to be
9555 // preserved. Resetting should cancel the ongoing gesture.
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00009556 resetMapper(mapper, ARBITRARY_TIME);
9557 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9558 WithMotionAction(AMOTION_EVENT_ACTION_CANCEL)));
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009559
Arpit Singh4bb0bd52023-12-20 14:41:10 +00009560 // Send a sync to simulate an empty touch frame where nothing changes. The mapper should use
9561 // the existing touch state to generate a down event.
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009562 processPosition(mapper, 301, 302);
9563 processSync(mapper);
9564 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9565 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithPressure(1.f))));
Arpit Singh4bb0bd52023-12-20 14:41:10 +00009566 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9567 AllOf(WithMotionAction(ACTION_POINTER_1_DOWN), WithPressure(1.f))));
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009568
9569 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9570}
9571
Arpit Singh4bb0bd52023-12-20 14:41:10 +00009572TEST_F(MultiTouchInputMapperTest, Reset_PreservesLastTouchState_NoPointersDown) {
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009573 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009574 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009575 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +00009576 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009577
9578 // First finger touches down and releases.
9579 processId(mapper, FIRST_TRACKING_ID);
9580 processPosition(mapper, 100, 200);
9581 processPressure(mapper, RAW_PRESSURE_MAX);
9582 processSync(mapper);
9583 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9584 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
9585 processId(mapper, INVALID_TRACKING_ID);
9586 processSync(mapper);
9587 ASSERT_NO_FATAL_FAILURE(
9588 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
9589
9590 // Reset the mapper. When the mapper is reset, we expect it to restore the latest
9591 // raw state where no pointers are down.
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00009592 resetMapper(mapper, ARBITRARY_TIME);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009593 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9594
9595 // Send an empty sync frame. Since there are no pointers, no events are generated.
9596 processSync(mapper);
9597 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9598}
9599
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +00009600TEST_F(MultiTouchInputMapperTest, StylusSourceIsAddedDynamicallyFromToolType) {
Prabir Pradhanf9a41282022-10-25 17:15:50 +00009601 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009602 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanf9a41282022-10-25 17:15:50 +00009603 prepareAxes(POSITION | ID | SLOT | PRESSURE | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +00009604 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +00009605 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
Prabir Pradhanf9a41282022-10-25 17:15:50 +00009606
9607 // Even if the device supports reporting the ABS_MT_TOOL_TYPE axis, which could give it the
9608 // ability to report MT_TOOL_PEN, we do not report the device as coming from a stylus source.
9609 // Due to limitations in the evdev protocol, we cannot say for certain that a device is capable
9610 // of reporting stylus events just because it supports ABS_MT_TOOL_TYPE.
9611 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
9612
9613 // However, if the device ever ends up reporting an event with MT_TOOL_PEN, it should be
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +00009614 // reported with the stylus source.
Prabir Pradhanf9a41282022-10-25 17:15:50 +00009615 processId(mapper, FIRST_TRACKING_ID);
9616 processToolType(mapper, MT_TOOL_PEN);
9617 processPosition(mapper, 100, 200);
9618 processPressure(mapper, RAW_PRESSURE_MAX);
9619 processSync(mapper);
9620 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9621 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
9622 WithSource(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009623 WithToolType(ToolType::STYLUS))));
Prabir Pradhanf9a41282022-10-25 17:15:50 +00009624
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +00009625 // Now that we know the device supports styluses, ensure that the device is re-configured with
9626 // the stylus source.
9627 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS, mapper.getSources());
9628 {
9629 const auto& devices = mReader->getInputDevices();
9630 auto deviceInfo =
9631 std::find_if(devices.begin(), devices.end(),
9632 [](const InputDeviceInfo& info) { return info.getId() == DEVICE_ID; });
9633 LOG_ALWAYS_FATAL_IF(deviceInfo == devices.end(), "Cannot find InputDevice");
9634 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS, deviceInfo->getSources());
9635 }
9636
9637 // Ensure the device was not reset to prevent interruptions of any ongoing gestures.
9638 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
9639
Prabir Pradhanf9a41282022-10-25 17:15:50 +00009640 processId(mapper, INVALID_TRACKING_ID);
9641 processSync(mapper);
9642 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9643 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
9644 WithSource(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009645 WithToolType(ToolType::STYLUS))));
Prabir Pradhanf9a41282022-10-25 17:15:50 +00009646}
9647
Seunghwan Choi356026c2023-02-01 14:37:25 +09009648TEST_F(MultiTouchInputMapperTest, Process_WhenConfigEnabled_ShouldShowDirectStylusPointer) {
9649 addConfigurationProperty("touch.deviceType", "touchScreen");
9650 prepareDisplay(ui::ROTATION_0);
9651 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE | PRESSURE);
9652 // Add BTN_TOOL_PEN to statically show stylus support, since using ABS_MT_TOOL_TYPE can only
9653 // indicate stylus presence dynamically.
9654 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
9655 std::shared_ptr<FakePointerController> fakePointerController =
9656 std::make_shared<FakePointerController>();
9657 mFakePolicy->setPointerController(fakePointerController);
9658 mFakePolicy->setStylusPointerIconEnabled(true);
Arpit Singha8c236b2023-04-25 13:56:05 +00009659 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Seunghwan Choi356026c2023-02-01 14:37:25 +09009660
9661 processId(mapper, FIRST_TRACKING_ID);
9662 processPressure(mapper, RAW_PRESSURE_MIN);
9663 processPosition(mapper, 100, 200);
9664 processToolType(mapper, MT_TOOL_PEN);
9665 processSync(mapper);
9666 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9667 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009668 WithToolType(ToolType::STYLUS),
Seunghwan Choi356026c2023-02-01 14:37:25 +09009669 WithPointerCoords(0, toDisplayX(100), toDisplayY(200)))));
9670 ASSERT_TRUE(fakePointerController->isPointerShown());
9671 ASSERT_NO_FATAL_FAILURE(
9672 fakePointerController->assertPosition(toDisplayX(100), toDisplayY(200)));
9673}
9674
9675TEST_F(MultiTouchInputMapperTest, Process_WhenConfigDisabled_ShouldNotShowDirectStylusPointer) {
9676 addConfigurationProperty("touch.deviceType", "touchScreen");
9677 prepareDisplay(ui::ROTATION_0);
9678 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE | PRESSURE);
9679 // Add BTN_TOOL_PEN to statically show stylus support, since using ABS_MT_TOOL_TYPE can only
9680 // indicate stylus presence dynamically.
9681 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
9682 std::shared_ptr<FakePointerController> fakePointerController =
9683 std::make_shared<FakePointerController>();
9684 mFakePolicy->setPointerController(fakePointerController);
9685 mFakePolicy->setStylusPointerIconEnabled(false);
Arpit Singha8c236b2023-04-25 13:56:05 +00009686 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Seunghwan Choi356026c2023-02-01 14:37:25 +09009687
9688 processId(mapper, FIRST_TRACKING_ID);
9689 processPressure(mapper, RAW_PRESSURE_MIN);
9690 processPosition(mapper, 100, 200);
9691 processToolType(mapper, MT_TOOL_PEN);
9692 processSync(mapper);
9693 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9694 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009695 WithToolType(ToolType::STYLUS),
Seunghwan Choi356026c2023-02-01 14:37:25 +09009696 WithPointerCoords(0, toDisplayX(100), toDisplayY(200)))));
9697 ASSERT_FALSE(fakePointerController->isPointerShown());
9698}
9699
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009700// --- MultiTouchInputMapperTest_ExternalDevice ---
9701
9702class MultiTouchInputMapperTest_ExternalDevice : public MultiTouchInputMapperTest {
9703protected:
Chris Yea52ade12020-08-27 16:49:20 -07009704 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009705};
9706
9707/**
9708 * Expect fallback to internal viewport if device is external and external viewport is not present.
9709 */
9710TEST_F(MultiTouchInputMapperTest_ExternalDevice, Viewports_Fallback) {
9711 prepareAxes(POSITION);
9712 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009713 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +00009714 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009715
9716 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
9717
9718 NotifyMotionArgs motionArgs;
9719
9720 // Expect the event to be sent to the internal viewport,
9721 // because an external viewport is not present.
9722 processPosition(mapper, 100, 100);
9723 processSync(mapper);
9724 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9725 ASSERT_EQ(ADISPLAY_ID_DEFAULT, motionArgs.displayId);
9726
9727 // Expect the event to be sent to the external viewport if it is present.
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009728 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009729 processPosition(mapper, 100, 100);
9730 processSync(mapper);
9731 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9732 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
9733}
Arthur Hung4197f6b2020-03-16 15:39:59 +08009734
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009735TEST_F(MultiTouchInputMapperTest, Process_TouchpadCapture) {
9736 // we need a pointer controller for mouse mode of touchpad (start pointer at 0,0)
9737 std::shared_ptr<FakePointerController> fakePointerController =
9738 std::make_shared<FakePointerController>();
9739 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
9740 fakePointerController->setPosition(0, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009741
9742 // prepare device and capture
Michael Wrighta9cf4192022-12-01 23:46:39 +00009743 prepareDisplay(ui::ROTATION_0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009744 prepareAxes(POSITION | ID | SLOT);
9745 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
9746 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
9747 mFakePolicy->setPointerCapture(true);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009748 mFakePolicy->setPointerController(fakePointerController);
Arpit Singha8c236b2023-04-25 13:56:05 +00009749 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009750
9751 // captured touchpad should be a touchpad source
9752 NotifyDeviceResetArgs resetArgs;
9753 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
9754 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
9755
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009756 InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
Chris Yef74dc422020-09-02 22:41:50 -07009757
9758 const InputDeviceInfo::MotionRange* relRangeX =
9759 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_X, AINPUT_SOURCE_TOUCHPAD);
9760 ASSERT_NE(relRangeX, nullptr);
9761 ASSERT_EQ(relRangeX->min, -(RAW_X_MAX - RAW_X_MIN));
9762 ASSERT_EQ(relRangeX->max, RAW_X_MAX - RAW_X_MIN);
9763 const InputDeviceInfo::MotionRange* relRangeY =
9764 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_Y, AINPUT_SOURCE_TOUCHPAD);
9765 ASSERT_NE(relRangeY, nullptr);
9766 ASSERT_EQ(relRangeY->min, -(RAW_Y_MAX - RAW_Y_MIN));
9767 ASSERT_EQ(relRangeY->max, RAW_Y_MAX - RAW_Y_MIN);
9768
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009769 // run captured pointer tests - note that this is unscaled, so input listener events should be
9770 // identical to what the hardware sends (accounting for any
9771 // calibration).
9772 // FINGER 0 DOWN
Chris Ye364fdb52020-08-05 15:07:56 -07009773 processSlot(mapper, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009774 processId(mapper, 1);
9775 processPosition(mapper, 100 + RAW_X_MIN, 100 + RAW_Y_MIN);
9776 processKey(mapper, BTN_TOUCH, 1);
9777 processSync(mapper);
9778
9779 // expect coord[0] to contain initial location of touch 0
9780 NotifyMotionArgs args;
9781 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9782 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009783 ASSERT_EQ(1U, args.getPointerCount());
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009784 ASSERT_EQ(0, args.pointerProperties[0].id);
9785 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, args.source);
9786 ASSERT_NO_FATAL_FAILURE(
9787 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
9788
9789 // FINGER 1 DOWN
9790 processSlot(mapper, 1);
9791 processId(mapper, 2);
9792 processPosition(mapper, 560 + RAW_X_MIN, 154 + RAW_Y_MIN);
9793 processSync(mapper);
9794
9795 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
9796 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009797 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009798 ASSERT_EQ(2U, args.getPointerCount());
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009799 ASSERT_EQ(0, args.pointerProperties[0].id);
9800 ASSERT_EQ(1, args.pointerProperties[1].id);
9801 ASSERT_NO_FATAL_FAILURE(
9802 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
9803 ASSERT_NO_FATAL_FAILURE(
9804 assertPointerCoords(args.pointerCoords[1], 560, 154, 1, 0, 0, 0, 0, 0, 0, 0));
9805
9806 // FINGER 1 MOVE
9807 processPosition(mapper, 540 + RAW_X_MIN, 690 + RAW_Y_MIN);
9808 processSync(mapper);
9809
9810 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
9811 // from move
9812 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9813 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9814 ASSERT_NO_FATAL_FAILURE(
9815 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
9816 ASSERT_NO_FATAL_FAILURE(
9817 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
9818
9819 // FINGER 0 MOVE
9820 processSlot(mapper, 0);
9821 processPosition(mapper, 50 + RAW_X_MIN, 800 + RAW_Y_MIN);
9822 processSync(mapper);
9823
9824 // expect coord[0] to contain new touch 0 location, coord[1] to contain previous location
9825 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9826 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9827 ASSERT_NO_FATAL_FAILURE(
9828 assertPointerCoords(args.pointerCoords[0], 50, 800, 1, 0, 0, 0, 0, 0, 0, 0));
9829 ASSERT_NO_FATAL_FAILURE(
9830 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
9831
9832 // BUTTON DOWN
9833 processKey(mapper, BTN_LEFT, 1);
9834 processSync(mapper);
9835
9836 // touchinputmapper design sends a move before button press
9837 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9838 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9839 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9840 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
9841
9842 // BUTTON UP
9843 processKey(mapper, BTN_LEFT, 0);
9844 processSync(mapper);
9845
9846 // touchinputmapper design sends a move after button release
9847 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9848 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
9849 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9850 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9851
9852 // FINGER 0 UP
9853 processId(mapper, -1);
9854 processSync(mapper);
9855 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9856 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | 0x0000, args.action);
9857
9858 // FINGER 1 MOVE
9859 processSlot(mapper, 1);
9860 processPosition(mapper, 320 + RAW_X_MIN, 900 + RAW_Y_MIN);
9861 processSync(mapper);
9862
9863 // expect coord[0] to contain new location of touch 1, and properties[0].id to contain 1
9864 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9865 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009866 ASSERT_EQ(1U, args.getPointerCount());
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009867 ASSERT_EQ(1, args.pointerProperties[0].id);
9868 ASSERT_NO_FATAL_FAILURE(
9869 assertPointerCoords(args.pointerCoords[0], 320, 900, 1, 0, 0, 0, 0, 0, 0, 0));
9870
9871 // FINGER 1 UP
9872 processId(mapper, -1);
9873 processKey(mapper, BTN_TOUCH, 0);
9874 processSync(mapper);
9875 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9876 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
9877
Josep del Río2d8c79a2023-01-23 19:33:50 +00009878 // non captured touchpad should be a mouse source
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009879 mFakePolicy->setPointerCapture(false);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00009880 configureDevice(InputReaderConfiguration::Change::POINTER_CAPTURE);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009881 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Josep del Río2d8c79a2023-01-23 19:33:50 +00009882 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009883}
9884
9885TEST_F(MultiTouchInputMapperTest, Process_UnCapturedTouchpadPointer) {
9886 std::shared_ptr<FakePointerController> fakePointerController =
9887 std::make_shared<FakePointerController>();
9888 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
9889 fakePointerController->setPosition(0, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009890
9891 // prepare device and capture
Michael Wrighta9cf4192022-12-01 23:46:39 +00009892 prepareDisplay(ui::ROTATION_0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009893 prepareAxes(POSITION | ID | SLOT);
9894 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
9895 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009896 mFakePolicy->setPointerController(fakePointerController);
Arpit Singha8c236b2023-04-25 13:56:05 +00009897 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009898 // run uncaptured pointer tests - pushes out generic events
9899 // FINGER 0 DOWN
9900 processId(mapper, 3);
9901 processPosition(mapper, 100, 100);
9902 processKey(mapper, BTN_TOUCH, 1);
9903 processSync(mapper);
9904
9905 // start at (100,100), cursor should be at (0,0) * scale
9906 NotifyMotionArgs args;
9907 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9908 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
9909 ASSERT_NO_FATAL_FAILURE(
9910 assertPointerCoords(args.pointerCoords[0], 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
9911
9912 // FINGER 0 MOVE
9913 processPosition(mapper, 200, 200);
9914 processSync(mapper);
9915
9916 // compute scaling to help with touch position checking
9917 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
9918 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
9919 float scale =
9920 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
9921
9922 // translate from (100,100) -> (200,200), cursor should have changed to (100,100) * scale)
9923 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9924 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
9925 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 100 * scale, 100 * scale, 0,
9926 0, 0, 0, 0, 0, 0, 0));
LiZhihong758eb562022-11-03 15:28:29 +08009927
9928 // BUTTON DOWN
9929 processKey(mapper, BTN_LEFT, 1);
9930 processSync(mapper);
9931
9932 // touchinputmapper design sends a move before button press
9933 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9934 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
9935 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9936 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
9937
9938 // BUTTON UP
9939 processKey(mapper, BTN_LEFT, 0);
9940 processSync(mapper);
9941
9942 // touchinputmapper design sends a move after button release
9943 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9944 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
9945 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9946 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009947}
9948
9949TEST_F(MultiTouchInputMapperTest, WhenCapturedAndNotCaptured_GetSources) {
9950 std::shared_ptr<FakePointerController> fakePointerController =
9951 std::make_shared<FakePointerController>();
9952
Michael Wrighta9cf4192022-12-01 23:46:39 +00009953 prepareDisplay(ui::ROTATION_0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009954 prepareAxes(POSITION | ID | SLOT);
9955 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009956 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009957 mFakePolicy->setPointerCapture(false);
Arpit Singha8c236b2023-04-25 13:56:05 +00009958 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009959
Josep del Río2d8c79a2023-01-23 19:33:50 +00009960 // uncaptured touchpad should be a pointer device
9961 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009962
Josep del Río2d8c79a2023-01-23 19:33:50 +00009963 // captured touchpad should be a touchpad device
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009964 mFakePolicy->setPointerCapture(true);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00009965 configureDevice(InputReaderConfiguration::Change::POINTER_CAPTURE);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009966 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
9967}
9968
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00009969// --- BluetoothMultiTouchInputMapperTest ---
9970
9971class BluetoothMultiTouchInputMapperTest : public MultiTouchInputMapperTest {
9972protected:
9973 void SetUp() override {
9974 InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL, BUS_BLUETOOTH);
9975 }
9976};
9977
9978TEST_F(BluetoothMultiTouchInputMapperTest, TimestampSmoothening) {
9979 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009980 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00009981 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +00009982 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00009983
9984 nsecs_t kernelEventTime = ARBITRARY_TIME;
9985 nsecs_t expectedEventTime = ARBITRARY_TIME;
9986 // Touch down.
9987 processId(mapper, FIRST_TRACKING_ID);
9988 processPosition(mapper, 100, 200);
9989 processPressure(mapper, RAW_PRESSURE_MAX);
9990 processSync(mapper, ARBITRARY_TIME);
9991 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9992 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithEventTime(ARBITRARY_TIME))));
9993
9994 // Process several events that come in quick succession, according to their timestamps.
9995 for (int i = 0; i < 3; i++) {
9996 constexpr static nsecs_t delta = ms2ns(1);
9997 static_assert(delta < MIN_BLUETOOTH_TIMESTAMP_DELTA);
9998 kernelEventTime += delta;
9999 expectedEventTime += MIN_BLUETOOTH_TIMESTAMP_DELTA;
10000
10001 processPosition(mapper, 101 + i, 201 + i);
10002 processSync(mapper, kernelEventTime);
10003 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10004 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
10005 WithEventTime(expectedEventTime))));
10006 }
10007
10008 // Release the touch.
10009 processId(mapper, INVALID_TRACKING_ID);
10010 processPressure(mapper, RAW_PRESSURE_MIN);
10011 processSync(mapper, ARBITRARY_TIME + ms2ns(50));
10012 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10013 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
10014 WithEventTime(ARBITRARY_TIME + ms2ns(50)))));
10015}
10016
10017// --- MultiTouchPointerModeTest ---
10018
HQ Liue6983c72022-04-19 22:14:56 +000010019class MultiTouchPointerModeTest : public MultiTouchInputMapperTest {
10020protected:
10021 float mPointerMovementScale;
10022 float mPointerXZoomScale;
10023 void preparePointerMode(int xAxisResolution, int yAxisResolution) {
10024 addConfigurationProperty("touch.deviceType", "pointer");
10025 std::shared_ptr<FakePointerController> fakePointerController =
10026 std::make_shared<FakePointerController>();
10027 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
10028 fakePointerController->setPosition(0, 0);
Michael Wrighta9cf4192022-12-01 23:46:39 +000010029 prepareDisplay(ui::ROTATION_0);
HQ Liue6983c72022-04-19 22:14:56 +000010030
10031 prepareAxes(POSITION);
10032 prepareAbsoluteAxisResolution(xAxisResolution, yAxisResolution);
10033 // In order to enable swipe and freeform gesture in pointer mode, pointer capture
10034 // needs to be disabled, and the pointer gesture needs to be enabled.
10035 mFakePolicy->setPointerCapture(false);
10036 mFakePolicy->setPointerGestureEnabled(true);
10037 mFakePolicy->setPointerController(fakePointerController);
10038
10039 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
10040 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
10041 mPointerMovementScale =
10042 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
10043 mPointerXZoomScale =
10044 mFakePolicy->getPointerGestureZoomSpeedRatio() * displayDiagonal / rawDiagonal;
10045 }
10046
10047 void prepareAbsoluteAxisResolution(int xAxisResolution, int yAxisResolution) {
10048 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
10049 /*flat*/ 0,
10050 /*fuzz*/ 0, /*resolution*/ xAxisResolution);
10051 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
10052 /*flat*/ 0,
10053 /*fuzz*/ 0, /*resolution*/ yAxisResolution);
10054 }
10055};
10056
10057/**
10058 * Two fingers down on a pointer mode touch pad. The width
10059 * of the two finger is larger than 1/4 of the touch pack diagnal length. However, it
10060 * is smaller than the fixed min physical length 30mm. Two fingers' distance must
10061 * be greater than the both value to be freeform gesture, so that after two
10062 * fingers start to move downwards, the gesture should be swipe.
10063 */
10064TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthSwipe) {
10065 // The min freeform gesture width is 25units/mm x 30mm = 750
10066 // which is greater than fraction of the diagnal length of the touchpad (349).
10067 // Thus, MaxSwipWidth is 750.
Harry Cutts33476232023-01-30 19:57:29 +000010068 preparePointerMode(/*xResolution=*/25, /*yResolution=*/25);
Arpit Singha8c236b2023-04-25 13:56:05 +000010069 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
HQ Liue6983c72022-04-19 22:14:56 +000010070 NotifyMotionArgs motionArgs;
10071
10072 // Two fingers down at once.
10073 // The two fingers are 450 units apart, expects the current gesture to be PRESS
10074 // Pointer's initial position is used the [0,0] coordinate.
10075 int32_t x1 = 100, y1 = 125, x2 = 550, y2 = 125;
10076
10077 processId(mapper, FIRST_TRACKING_ID);
10078 processPosition(mapper, x1, y1);
10079 processMTSync(mapper);
10080 processId(mapper, SECOND_TRACKING_ID);
10081 processPosition(mapper, x2, y2);
10082 processMTSync(mapper);
10083 processSync(mapper);
10084
10085 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010086 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000010087 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010088 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010089 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010090 ASSERT_NO_FATAL_FAILURE(
10091 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
10092
10093 // It should be recognized as a SWIPE gesture when two fingers start to move down,
10094 // that there should be 1 pointer.
10095 int32_t movingDistance = 200;
10096 y1 += movingDistance;
10097 y2 += movingDistance;
10098
10099 processId(mapper, FIRST_TRACKING_ID);
10100 processPosition(mapper, x1, y1);
10101 processMTSync(mapper);
10102 processId(mapper, SECOND_TRACKING_ID);
10103 processPosition(mapper, x2, y2);
10104 processMTSync(mapper);
10105 processSync(mapper);
10106
10107 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010108 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000010109 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010110 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010111 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010112 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 0,
10113 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10114 0, 0, 0, 0));
10115}
10116
10117/**
10118 * Two fingers down on a pointer mode touch pad. The width of the two finger is larger
10119 * than the minimum freeform gesture width, 30mm. However, it is smaller than 1/4 of
10120 * the touch pack diagnal length. Two fingers' distance must be greater than the both
10121 * value to be freeform gesture, so that after two fingers start to move downwards,
10122 * the gesture should be swipe.
10123 */
10124TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthLowResolutionSwipe) {
10125 // The min freeform gesture width is 5units/mm x 30mm = 150
10126 // which is greater than fraction of the diagnal length of the touchpad (349).
10127 // Thus, MaxSwipWidth is the fraction of the diagnal length, 349.
Harry Cutts33476232023-01-30 19:57:29 +000010128 preparePointerMode(/*xResolution=*/5, /*yResolution=*/5);
Arpit Singha8c236b2023-04-25 13:56:05 +000010129 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
HQ Liue6983c72022-04-19 22:14:56 +000010130 NotifyMotionArgs motionArgs;
10131
10132 // Two fingers down at once.
10133 // The two fingers are 250 units apart, expects the current gesture to be PRESS
10134 // Pointer's initial position is used the [0,0] coordinate.
10135 int32_t x1 = 100, y1 = 125, x2 = 350, y2 = 125;
10136
10137 processId(mapper, FIRST_TRACKING_ID);
10138 processPosition(mapper, x1, y1);
10139 processMTSync(mapper);
10140 processId(mapper, SECOND_TRACKING_ID);
10141 processPosition(mapper, x2, y2);
10142 processMTSync(mapper);
10143 processSync(mapper);
10144
10145 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010146 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000010147 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010148 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010149 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010150 ASSERT_NO_FATAL_FAILURE(
10151 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
10152
10153 // It should be recognized as a SWIPE gesture when two fingers start to move down,
10154 // and there should be 1 pointer.
10155 int32_t movingDistance = 200;
10156 y1 += movingDistance;
10157 y2 += movingDistance;
10158
10159 processId(mapper, FIRST_TRACKING_ID);
10160 processPosition(mapper, x1, y1);
10161 processMTSync(mapper);
10162 processId(mapper, SECOND_TRACKING_ID);
10163 processPosition(mapper, x2, y2);
10164 processMTSync(mapper);
10165 processSync(mapper);
10166
10167 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010168 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000010169 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010170 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010171 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010172 // New coordinate is the scaled relative coordinate from the initial coordinate.
10173 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 0,
10174 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10175 0, 0, 0, 0));
10176}
10177
10178/**
10179 * Touch the touch pad with two fingers with a distance wider than the minimum freeform
10180 * gesture width and 1/4 of the diagnal length of the touchpad. Expect to receive
10181 * freeform gestures after two fingers start to move downwards.
10182 */
10183TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthFreeform) {
Harry Cutts33476232023-01-30 19:57:29 +000010184 preparePointerMode(/*xResolution=*/25, /*yResolution=*/25);
Arpit Singha8c236b2023-04-25 13:56:05 +000010185 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
HQ Liue6983c72022-04-19 22:14:56 +000010186
10187 NotifyMotionArgs motionArgs;
10188
10189 // Two fingers down at once. Wider than the max swipe width.
10190 // The gesture is expected to be PRESS, then transformed to FREEFORM
10191 int32_t x1 = 100, y1 = 125, x2 = 900, y2 = 125;
10192
10193 processId(mapper, FIRST_TRACKING_ID);
10194 processPosition(mapper, x1, y1);
10195 processMTSync(mapper);
10196 processId(mapper, SECOND_TRACKING_ID);
10197 processPosition(mapper, x2, y2);
10198 processMTSync(mapper);
10199 processSync(mapper);
10200
10201 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010202 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000010203 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010204 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010205 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010206 // One pointer for PRESS, and its coordinate is used as the origin for pointer coordinates.
10207 ASSERT_NO_FATAL_FAILURE(
10208 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
10209
10210 int32_t movingDistance = 200;
10211
10212 // Move two fingers down, expect a cancel event because gesture is changing to freeform,
10213 // then two down events for two pointers.
10214 y1 += movingDistance;
10215 y2 += movingDistance;
10216
10217 processId(mapper, FIRST_TRACKING_ID);
10218 processPosition(mapper, x1, y1);
10219 processMTSync(mapper);
10220 processId(mapper, SECOND_TRACKING_ID);
10221 processPosition(mapper, x2, y2);
10222 processMTSync(mapper);
10223 processSync(mapper);
10224
10225 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10226 // The previous PRESS gesture is cancelled, because it is transformed to freeform
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010227 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000010228 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
10229 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010230 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010231 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000010232 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10233 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010234 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010235 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010236 ASSERT_EQ(2U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000010237 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN, motionArgs.action & AMOTION_EVENT_ACTION_MASK);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010238 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010239 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010240 // Two pointers' scaled relative coordinates from their initial centroid.
10241 // Initial y coordinates are 0 as y1 and y2 have the same value.
10242 float cookedX1 = (x1 - x2) / 2 * mPointerXZoomScale;
10243 float cookedX2 = (x2 - x1) / 2 * mPointerXZoomScale;
10244 // When pointers move, the new coordinates equal to the initial coordinates plus
10245 // scaled moving distance.
10246 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], cookedX1,
10247 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10248 0, 0, 0, 0));
10249 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1], cookedX2,
10250 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10251 0, 0, 0, 0));
10252
10253 // Move two fingers down again, expect one MOVE motion event.
10254 y1 += movingDistance;
10255 y2 += movingDistance;
10256
10257 processId(mapper, FIRST_TRACKING_ID);
10258 processPosition(mapper, x1, y1);
10259 processMTSync(mapper);
10260 processId(mapper, SECOND_TRACKING_ID);
10261 processPosition(mapper, x2, y2);
10262 processMTSync(mapper);
10263 processSync(mapper);
10264
10265 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010266 ASSERT_EQ(2U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000010267 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010268 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010269 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010270 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], cookedX1,
10271 movingDistance * 2 * mPointerMovementScale, 1, 0, 0,
10272 0, 0, 0, 0, 0));
10273 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1], cookedX2,
10274 movingDistance * 2 * mPointerMovementScale, 1, 0, 0,
10275 0, 0, 0, 0, 0));
10276}
10277
Harry Cutts39b7ca22022-10-05 15:55:48 +000010278TEST_F(MultiTouchPointerModeTest, TwoFingerSwipeOffsets) {
Harry Cutts33476232023-01-30 19:57:29 +000010279 preparePointerMode(/*xResolution=*/25, /*yResolution=*/25);
Arpit Singha8c236b2023-04-25 13:56:05 +000010280 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Harry Cutts39b7ca22022-10-05 15:55:48 +000010281 NotifyMotionArgs motionArgs;
10282
10283 // Place two fingers down.
10284 int32_t x1 = 100, y1 = 125, x2 = 550, y2 = 125;
10285
10286 processId(mapper, FIRST_TRACKING_ID);
10287 processPosition(mapper, x1, y1);
10288 processMTSync(mapper);
10289 processId(mapper, SECOND_TRACKING_ID);
10290 processPosition(mapper, x2, y2);
10291 processMTSync(mapper);
10292 processSync(mapper);
10293
10294 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010295 ASSERT_EQ(1U, motionArgs.getPointerCount());
Harry Cutts39b7ca22022-10-05 15:55:48 +000010296 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10297 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
10298 ASSERT_EQ(0, motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_X_OFFSET));
10299 ASSERT_EQ(0, motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_Y_OFFSET));
10300
10301 // Move the two fingers down and to the left.
10302 int32_t movingDistance = 200;
10303 x1 -= movingDistance;
10304 y1 += movingDistance;
10305 x2 -= movingDistance;
10306 y2 += movingDistance;
10307
10308 processId(mapper, FIRST_TRACKING_ID);
10309 processPosition(mapper, x1, y1);
10310 processMTSync(mapper);
10311 processId(mapper, SECOND_TRACKING_ID);
10312 processPosition(mapper, x2, y2);
10313 processMTSync(mapper);
10314 processSync(mapper);
10315
10316 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010317 ASSERT_EQ(1U, motionArgs.getPointerCount());
Harry Cutts39b7ca22022-10-05 15:55:48 +000010318 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10319 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
10320 ASSERT_LT(motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_X_OFFSET), 0);
10321 ASSERT_GT(motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_Y_OFFSET), 0);
10322}
10323
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010324TEST_F(MultiTouchPointerModeTest, WhenViewportActiveStatusChanged_PointerGestureIsReset) {
Harry Cutts33476232023-01-30 19:57:29 +000010325 preparePointerMode(/*xResolution=*/25, /*yResolution=*/25);
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010326 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
Arpit Singha8c236b2023-04-25 13:56:05 +000010327 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010328 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
10329
10330 // Start a stylus gesture.
10331 processKey(mapper, BTN_TOOL_PEN, 1);
10332 processId(mapper, FIRST_TRACKING_ID);
10333 processPosition(mapper, 100, 200);
10334 processSync(mapper);
10335 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10336 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
10337 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010338 WithToolType(ToolType::STYLUS))));
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010339 // TODO(b/257078296): Pointer mode generates extra event.
10340 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10341 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
10342 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010343 WithToolType(ToolType::STYLUS))));
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010344 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10345
10346 // Make the viewport inactive. This will put the device in disabled mode, and the ongoing stylus
10347 // gesture should be disabled.
10348 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
10349 viewport->isActive = false;
10350 mFakePolicy->updateViewport(*viewport);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +000010351 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010352 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10353 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL),
10354 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010355 WithToolType(ToolType::STYLUS))));
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010356 // TODO(b/257078296): Pointer mode generates extra event.
10357 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10358 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL),
10359 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010360 WithToolType(ToolType::STYLUS))));
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010361 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10362}
10363
Arthur Hung6d5b4b22022-01-21 07:21:10 +000010364// --- JoystickInputMapperTest ---
10365
10366class JoystickInputMapperTest : public InputMapperTest {
10367protected:
10368 static const int32_t RAW_X_MIN;
10369 static const int32_t RAW_X_MAX;
10370 static const int32_t RAW_Y_MIN;
10371 static const int32_t RAW_Y_MAX;
10372
10373 void SetUp() override {
10374 InputMapperTest::SetUp(InputDeviceClass::JOYSTICK | InputDeviceClass::EXTERNAL);
10375 }
10376 void prepareAxes() {
10377 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
10378 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
10379 }
10380
10381 void processAxis(JoystickInputMapper& mapper, int32_t axis, int32_t value) {
10382 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, axis, value);
10383 }
10384
10385 void processSync(JoystickInputMapper& mapper) {
10386 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
10387 }
10388
Michael Wrighta9cf4192022-12-01 23:46:39 +000010389 void prepareVirtualDisplay(ui::Rotation orientation) {
Arthur Hung6d5b4b22022-01-21 07:21:10 +000010390 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH,
10391 VIRTUAL_DISPLAY_HEIGHT, orientation, VIRTUAL_DISPLAY_UNIQUE_ID,
10392 NO_PORT, ViewportType::VIRTUAL);
10393 }
10394};
10395
10396const int32_t JoystickInputMapperTest::RAW_X_MIN = -32767;
10397const int32_t JoystickInputMapperTest::RAW_X_MAX = 32767;
10398const int32_t JoystickInputMapperTest::RAW_Y_MIN = -32767;
10399const int32_t JoystickInputMapperTest::RAW_Y_MAX = 32767;
10400
10401TEST_F(JoystickInputMapperTest, Configure_AssignsDisplayUniqueId) {
10402 prepareAxes();
Arpit Singhae876352023-04-26 14:16:50 +000010403 JoystickInputMapper& mapper = constructAndAddMapper<JoystickInputMapper>();
Arthur Hung6d5b4b22022-01-21 07:21:10 +000010404
10405 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
10406
Michael Wrighta9cf4192022-12-01 23:46:39 +000010407 prepareVirtualDisplay(ui::ROTATION_0);
Arthur Hung6d5b4b22022-01-21 07:21:10 +000010408
10409 // Send an axis event
10410 processAxis(mapper, ABS_X, 100);
10411 processSync(mapper);
10412
10413 NotifyMotionArgs args;
10414 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10415 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
10416
10417 // Send another axis event
10418 processAxis(mapper, ABS_Y, 100);
10419 processSync(mapper);
10420
10421 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10422 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
10423}
10424
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010425// --- PeripheralControllerTest ---
Chris Yee2b1e5c2021-03-10 22:45:12 -080010426
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010427class PeripheralControllerTest : public testing::Test {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010428protected:
10429 static const char* DEVICE_NAME;
10430 static const char* DEVICE_LOCATION;
10431 static const int32_t DEVICE_ID;
10432 static const int32_t DEVICE_GENERATION;
10433 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010434 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Chris Yee2b1e5c2021-03-10 22:45:12 -080010435 static const int32_t EVENTHUB_ID;
10436
10437 std::shared_ptr<FakeEventHub> mFakeEventHub;
10438 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010439 std::unique_ptr<TestInputListener> mFakeListener;
Chris Yee2b1e5c2021-03-10 22:45:12 -080010440 std::unique_ptr<InstrumentedInputReader> mReader;
10441 std::shared_ptr<InputDevice> mDevice;
10442
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010443 virtual void SetUp(ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010444 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -070010445 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010446 mFakeListener = std::make_unique<TestInputListener>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010447 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010448 *mFakeListener);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010449 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
10450 }
10451
10452 void SetUp() override { SetUp(DEVICE_CLASSES); }
10453
10454 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010455 mFakeListener.reset();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010456 mFakePolicy.clear();
10457 }
10458
Chris Yee2b1e5c2021-03-10 22:45:12 -080010459 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
10460 const std::string& location, int32_t eventHubId,
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010461 ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010462 InputDeviceIdentifier identifier;
10463 identifier.name = name;
10464 identifier.location = location;
10465 std::shared_ptr<InputDevice> device =
10466 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
10467 identifier);
10468 mReader->pushNextDevice(device);
10469 mFakeEventHub->addDevice(eventHubId, name, classes);
10470 mReader->loopOnce();
10471 return device;
10472 }
10473
10474 template <class T, typename... Args>
10475 T& addControllerAndConfigure(Args... args) {
10476 T& controller = mDevice->addController<T>(EVENTHUB_ID, args...);
10477
10478 return controller;
10479 }
10480};
10481
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010482const char* PeripheralControllerTest::DEVICE_NAME = "device";
10483const char* PeripheralControllerTest::DEVICE_LOCATION = "BLUETOOTH";
10484const int32_t PeripheralControllerTest::DEVICE_ID = END_RESERVED_ID + 1000;
10485const int32_t PeripheralControllerTest::DEVICE_GENERATION = 2;
10486const int32_t PeripheralControllerTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010487const ftl::Flags<InputDeviceClass> PeripheralControllerTest::DEVICE_CLASSES =
10488 ftl::Flags<InputDeviceClass>(0); // not needed for current tests
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010489const int32_t PeripheralControllerTest::EVENTHUB_ID = 1;
Chris Yee2b1e5c2021-03-10 22:45:12 -080010490
10491// --- BatteryControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010492class BatteryControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010493protected:
10494 void SetUp() override {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010495 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::BATTERY);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010496 }
10497};
10498
10499TEST_F(BatteryControllerTest, GetBatteryCapacity) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010500 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010501
Harry Cuttsa5b71292022-11-28 12:56:17 +000010502 ASSERT_TRUE(controller.getBatteryCapacity(FakeEventHub::DEFAULT_BATTERY));
10503 ASSERT_EQ(controller.getBatteryCapacity(FakeEventHub::DEFAULT_BATTERY).value_or(-1),
10504 FakeEventHub::BATTERY_CAPACITY);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010505}
10506
10507TEST_F(BatteryControllerTest, GetBatteryStatus) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010508 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010509
Harry Cuttsa5b71292022-11-28 12:56:17 +000010510 ASSERT_TRUE(controller.getBatteryStatus(FakeEventHub::DEFAULT_BATTERY));
10511 ASSERT_EQ(controller.getBatteryStatus(FakeEventHub::DEFAULT_BATTERY).value_or(-1),
10512 FakeEventHub::BATTERY_STATUS);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010513}
10514
10515// --- LightControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010516class LightControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010517protected:
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010518 void SetUp() override {
10519 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::LIGHT);
10520 }
Chris Yee2b1e5c2021-03-10 22:45:12 -080010521};
10522
Chris Ye85758332021-05-16 23:05:17 -070010523TEST_F(LightControllerTest, MonoLight) {
10524 RawLightInfo infoMono = {.id = 1,
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010525 .name = "mono_light",
Chris Ye85758332021-05-16 23:05:17 -070010526 .maxBrightness = 255,
10527 .flags = InputLightClass::BRIGHTNESS,
10528 .path = ""};
10529 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010530
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010531 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010532 InputDeviceInfo info;
10533 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010534 std::vector<InputDeviceLightInfo> lights = info.getLights();
10535 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010536 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
10537 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10538
10539 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
10540 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
10541}
10542
10543TEST_F(LightControllerTest, MonoKeyboardBacklight) {
10544 RawLightInfo infoMono = {.id = 1,
10545 .name = "mono_keyboard_backlight",
10546 .maxBrightness = 255,
10547 .flags = InputLightClass::BRIGHTNESS |
10548 InputLightClass::KEYBOARD_BACKLIGHT,
10549 .path = ""};
10550 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
10551
10552 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10553 InputDeviceInfo info;
10554 controller.populateDeviceInfo(&info);
10555 std::vector<InputDeviceLightInfo> lights = info.getLights();
10556 ASSERT_EQ(1U, lights.size());
10557 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
10558 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010559
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010560 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
10561 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010562}
10563
Vaibhav Devmurari16c24192023-05-04 15:20:12 +000010564TEST_F(LightControllerTest, Ignore_MonoLight_WithPreferredBacklightLevels) {
10565 RawLightInfo infoMono = {.id = 1,
10566 .name = "mono_light",
10567 .maxBrightness = 255,
10568 .flags = InputLightClass::BRIGHTNESS,
10569 .path = ""};
10570 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
10571 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "keyboard.backlight.brightnessLevels",
10572 "0,100,200");
10573
10574 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10575 std::list<NotifyArgs> unused =
10576 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
10577 /*changes=*/{});
10578
10579 InputDeviceInfo info;
10580 controller.populateDeviceInfo(&info);
10581 std::vector<InputDeviceLightInfo> lights = info.getLights();
10582 ASSERT_EQ(1U, lights.size());
10583 ASSERT_EQ(0U, lights[0].preferredBrightnessLevels.size());
10584}
10585
10586TEST_F(LightControllerTest, KeyboardBacklight_WithNoPreferredBacklightLevels) {
10587 RawLightInfo infoMono = {.id = 1,
10588 .name = "mono_keyboard_backlight",
10589 .maxBrightness = 255,
10590 .flags = InputLightClass::BRIGHTNESS |
10591 InputLightClass::KEYBOARD_BACKLIGHT,
10592 .path = ""};
10593 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
10594
10595 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10596 std::list<NotifyArgs> unused =
10597 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
10598 /*changes=*/{});
10599
10600 InputDeviceInfo info;
10601 controller.populateDeviceInfo(&info);
10602 std::vector<InputDeviceLightInfo> lights = info.getLights();
10603 ASSERT_EQ(1U, lights.size());
10604 ASSERT_EQ(0U, lights[0].preferredBrightnessLevels.size());
10605}
10606
10607TEST_F(LightControllerTest, KeyboardBacklight_WithPreferredBacklightLevels) {
10608 RawLightInfo infoMono = {.id = 1,
10609 .name = "mono_keyboard_backlight",
10610 .maxBrightness = 255,
10611 .flags = InputLightClass::BRIGHTNESS |
10612 InputLightClass::KEYBOARD_BACKLIGHT,
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(3U, lights[0].preferredBrightnessLevels.size());
10628 std::set<BrightnessLevel>::iterator it = lights[0].preferredBrightnessLevels.begin();
10629 ASSERT_EQ(BrightnessLevel(0), *it);
10630 std::advance(it, 1);
10631 ASSERT_EQ(BrightnessLevel(100), *it);
10632 std::advance(it, 1);
10633 ASSERT_EQ(BrightnessLevel(200), *it);
10634}
10635
10636TEST_F(LightControllerTest, KeyboardBacklight_WithWrongPreferredBacklightLevels) {
10637 RawLightInfo infoMono = {.id = 1,
10638 .name = "mono_keyboard_backlight",
10639 .maxBrightness = 255,
10640 .flags = InputLightClass::BRIGHTNESS |
10641 InputLightClass::KEYBOARD_BACKLIGHT,
10642 .path = ""};
10643 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
10644 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "keyboard.backlight.brightnessLevels",
10645 "0,100,200,300,400,500");
10646
10647 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10648 std::list<NotifyArgs> unused =
10649 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
10650 /*changes=*/{});
10651
10652 InputDeviceInfo info;
10653 controller.populateDeviceInfo(&info);
10654 std::vector<InputDeviceLightInfo> lights = info.getLights();
10655 ASSERT_EQ(1U, lights.size());
10656 ASSERT_EQ(0U, lights[0].preferredBrightnessLevels.size());
10657}
10658
Chris Yee2b1e5c2021-03-10 22:45:12 -080010659TEST_F(LightControllerTest, RGBLight) {
10660 RawLightInfo infoRed = {.id = 1,
10661 .name = "red",
10662 .maxBrightness = 255,
10663 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
10664 .path = ""};
10665 RawLightInfo infoGreen = {.id = 2,
10666 .name = "green",
10667 .maxBrightness = 255,
10668 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
10669 .path = ""};
10670 RawLightInfo infoBlue = {.id = 3,
10671 .name = "blue",
10672 .maxBrightness = 255,
10673 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
10674 .path = ""};
10675 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
10676 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
10677 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
10678
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010679 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010680 InputDeviceInfo info;
10681 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010682 std::vector<InputDeviceLightInfo> lights = info.getLights();
10683 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010684 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
10685 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10686 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
10687
10688 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10689 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
10690}
10691
10692TEST_F(LightControllerTest, CorrectRGBKeyboardBacklight) {
10693 RawLightInfo infoRed = {.id = 1,
10694 .name = "red_keyboard_backlight",
10695 .maxBrightness = 255,
10696 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED |
10697 InputLightClass::KEYBOARD_BACKLIGHT,
10698 .path = ""};
10699 RawLightInfo infoGreen = {.id = 2,
10700 .name = "green_keyboard_backlight",
10701 .maxBrightness = 255,
10702 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN |
10703 InputLightClass::KEYBOARD_BACKLIGHT,
10704 .path = ""};
10705 RawLightInfo infoBlue = {.id = 3,
10706 .name = "blue_keyboard_backlight",
10707 .maxBrightness = 255,
10708 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE |
10709 InputLightClass::KEYBOARD_BACKLIGHT,
10710 .path = ""};
10711 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
10712 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
10713 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
10714
10715 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10716 InputDeviceInfo info;
10717 controller.populateDeviceInfo(&info);
10718 std::vector<InputDeviceLightInfo> lights = info.getLights();
10719 ASSERT_EQ(1U, lights.size());
10720 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
10721 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10722 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
10723
10724 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10725 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
10726}
10727
10728TEST_F(LightControllerTest, IncorrectRGBKeyboardBacklight) {
10729 RawLightInfo infoRed = {.id = 1,
10730 .name = "red",
10731 .maxBrightness = 255,
10732 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
10733 .path = ""};
10734 RawLightInfo infoGreen = {.id = 2,
10735 .name = "green",
10736 .maxBrightness = 255,
10737 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
10738 .path = ""};
10739 RawLightInfo infoBlue = {.id = 3,
10740 .name = "blue",
10741 .maxBrightness = 255,
10742 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
10743 .path = ""};
10744 RawLightInfo infoGlobal = {.id = 3,
10745 .name = "global_keyboard_backlight",
10746 .maxBrightness = 255,
10747 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GLOBAL |
10748 InputLightClass::KEYBOARD_BACKLIGHT,
10749 .path = ""};
10750 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
10751 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
10752 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
10753 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoGlobal));
10754
10755 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10756 InputDeviceInfo info;
10757 controller.populateDeviceInfo(&info);
10758 std::vector<InputDeviceLightInfo> lights = info.getLights();
10759 ASSERT_EQ(1U, lights.size());
10760 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
10761 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10762 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010763
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010764 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10765 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010766}
10767
10768TEST_F(LightControllerTest, MultiColorRGBLight) {
10769 RawLightInfo infoColor = {.id = 1,
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010770 .name = "multi_color",
Chris Yee2b1e5c2021-03-10 22:45:12 -080010771 .maxBrightness = 255,
10772 .flags = InputLightClass::BRIGHTNESS |
10773 InputLightClass::MULTI_INTENSITY |
10774 InputLightClass::MULTI_INDEX,
10775 .path = ""};
10776
10777 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
10778
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010779 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010780 InputDeviceInfo info;
10781 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010782 std::vector<InputDeviceLightInfo> lights = info.getLights();
10783 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010784 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
10785 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10786 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
10787
10788 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10789 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
10790}
10791
10792TEST_F(LightControllerTest, MultiColorRGBKeyboardBacklight) {
10793 RawLightInfo infoColor = {.id = 1,
10794 .name = "multi_color_keyboard_backlight",
10795 .maxBrightness = 255,
10796 .flags = InputLightClass::BRIGHTNESS |
10797 InputLightClass::MULTI_INTENSITY |
10798 InputLightClass::MULTI_INDEX |
10799 InputLightClass::KEYBOARD_BACKLIGHT,
10800 .path = ""};
10801
10802 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
10803
10804 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10805 InputDeviceInfo info;
10806 controller.populateDeviceInfo(&info);
10807 std::vector<InputDeviceLightInfo> lights = info.getLights();
10808 ASSERT_EQ(1U, lights.size());
10809 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
10810 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10811 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010812
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010813 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10814 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010815}
10816
10817TEST_F(LightControllerTest, PlayerIdLight) {
10818 RawLightInfo info1 = {.id = 1,
10819 .name = "player1",
10820 .maxBrightness = 255,
10821 .flags = InputLightClass::BRIGHTNESS,
10822 .path = ""};
10823 RawLightInfo info2 = {.id = 2,
10824 .name = "player2",
10825 .maxBrightness = 255,
10826 .flags = InputLightClass::BRIGHTNESS,
10827 .path = ""};
10828 RawLightInfo info3 = {.id = 3,
10829 .name = "player3",
10830 .maxBrightness = 255,
10831 .flags = InputLightClass::BRIGHTNESS,
10832 .path = ""};
10833 RawLightInfo info4 = {.id = 4,
10834 .name = "player4",
10835 .maxBrightness = 255,
10836 .flags = InputLightClass::BRIGHTNESS,
10837 .path = ""};
10838 mFakeEventHub->addRawLightInfo(info1.id, std::move(info1));
10839 mFakeEventHub->addRawLightInfo(info2.id, std::move(info2));
10840 mFakeEventHub->addRawLightInfo(info3.id, std::move(info3));
10841 mFakeEventHub->addRawLightInfo(info4.id, std::move(info4));
10842
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010843 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010844 InputDeviceInfo info;
10845 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010846 std::vector<InputDeviceLightInfo> lights = info.getLights();
10847 ASSERT_EQ(1U, lights.size());
10848 ASSERT_EQ(InputDeviceLightType::PLAYER_ID, lights[0].type);
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010849 ASSERT_FALSE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10850 ASSERT_FALSE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010851
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010852 ASSERT_FALSE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10853 ASSERT_TRUE(controller.setLightPlayerId(lights[0].id, LIGHT_PLAYER_ID));
10854 ASSERT_EQ(controller.getLightPlayerId(lights[0].id).value_or(-1), LIGHT_PLAYER_ID);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010855}
10856
Michael Wrightd02c5b62014-02-10 15:10:22 -080010857} // namespace android