blob: cf59c7fc74cac62654a2c714adb88be6a68697d8 [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>
19
Prabir Pradhan2770d242019-09-02 18:07:11 -070020#include <CursorInputMapper.h>
21#include <InputDevice.h>
22#include <InputMapper.h>
23#include <InputReader.h>
Prabir Pradhan1aed8582019-12-30 11:46:51 -080024#include <InputReaderBase.h>
25#include <InputReaderFactory.h>
Arthur Hung6d5b4b22022-01-21 07:21:10 +000026#include <JoystickInputMapper.h>
Prabir Pradhan2770d242019-09-02 18:07:11 -070027#include <KeyboardInputMapper.h>
28#include <MultiTouchInputMapper.h>
Chris Ye1dd2e5c2021-04-04 23:12:41 -070029#include <PeripheralController.h>
Chris Yef59a2f42020-10-16 12:55:26 -070030#include <SensorInputMapper.h>
Prabir Pradhan2770d242019-09-02 18:07:11 -070031#include <SingleTouchInputMapper.h>
32#include <SwitchInputMapper.h>
33#include <TestInputListener.h>
34#include <TouchInputMapper.h>
Prabir Pradhan1aed8582019-12-30 11:46:51 -080035#include <UinputDevice.h>
Chris Ye87143712020-11-10 05:05:58 +000036#include <VibratorInputMapper.h>
Prabir Pradhan2574dfa2019-10-16 16:35:07 -070037#include <android-base/thread_annotations.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080038#include <gtest/gtest.h>
chaviw3277faf2021-05-19 16:45:23 -050039#include <gui/constants.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080040
Michael Wrightdde67b82020-10-27 16:09:22 +000041#include "input/DisplayViewport.h"
42#include "input/Input.h"
Michael Wright17db18e2020-06-26 20:51:44 +010043
Michael Wrightd02c5b62014-02-10 15:10:22 -080044namespace android {
45
Dominik Laskowski2f01d772022-03-23 16:01:29 -070046using namespace ftl::flag_operators;
47
Prabir Pradhan2574dfa2019-10-16 16:35:07 -070048using std::chrono_literals::operator""ms;
49
50// Timeout for waiting for an expected event
51static constexpr std::chrono::duration WAIT_TIMEOUT = 100ms;
52
Michael Wrightd02c5b62014-02-10 15:10:22 -080053// An arbitrary time value.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +000054static constexpr nsecs_t ARBITRARY_TIME = 1234;
55static constexpr nsecs_t READ_TIME = 4321;
Michael Wrightd02c5b62014-02-10 15:10:22 -080056
57// Arbitrary display properties.
arthurhungcc7f9802020-04-30 17:55:40 +080058static constexpr int32_t DISPLAY_ID = 0;
59static constexpr int32_t SECONDARY_DISPLAY_ID = DISPLAY_ID + 1;
60static constexpr int32_t DISPLAY_WIDTH = 480;
61static constexpr int32_t DISPLAY_HEIGHT = 800;
62static constexpr int32_t VIRTUAL_DISPLAY_ID = 1;
63static constexpr int32_t VIRTUAL_DISPLAY_WIDTH = 400;
64static constexpr int32_t VIRTUAL_DISPLAY_HEIGHT = 500;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -070065static const char* VIRTUAL_DISPLAY_UNIQUE_ID = "virtual:1";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -070066static constexpr std::optional<uint8_t> NO_PORT = std::nullopt; // no physical port is specified
Michael Wrightd02c5b62014-02-10 15:10:22 -080067
arthurhungcc7f9802020-04-30 17:55:40 +080068static constexpr int32_t FIRST_SLOT = 0;
69static constexpr int32_t SECOND_SLOT = 1;
70static constexpr int32_t THIRD_SLOT = 2;
71static constexpr int32_t INVALID_TRACKING_ID = -1;
72static constexpr int32_t FIRST_TRACKING_ID = 0;
73static constexpr int32_t SECOND_TRACKING_ID = 1;
74static constexpr int32_t THIRD_TRACKING_ID = 2;
Chris Yee2b1e5c2021-03-10 22:45:12 -080075static constexpr int32_t DEFAULT_BATTERY = 1;
Kim Low03ea0352020-11-06 12:45:07 -080076static constexpr int32_t BATTERY_STATUS = 4;
77static constexpr int32_t BATTERY_CAPACITY = 66;
Chris Ye3fdbfef2021-01-06 18:45:18 -080078static constexpr int32_t LIGHT_BRIGHTNESS = 0x55000000;
79static constexpr int32_t LIGHT_COLOR = 0x7F448866;
80static constexpr int32_t LIGHT_PLAYER_ID = 2;
arthurhungcc7f9802020-04-30 17:55:40 +080081
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080082static constexpr int32_t ACTION_POINTER_0_DOWN =
83 AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
84static constexpr int32_t ACTION_POINTER_0_UP =
85 AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
86static constexpr int32_t ACTION_POINTER_1_DOWN =
87 AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
88static constexpr int32_t ACTION_POINTER_1_UP =
89 AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
90
Michael Wrightd02c5b62014-02-10 15:10:22 -080091// Error tolerance for floating point assertions.
92static const float EPSILON = 0.001f;
93
94template<typename T>
95static inline T min(T a, T b) {
96 return a < b ? a : b;
97}
98
99static inline float avg(float x, float y) {
100 return (x + y) / 2;
101}
102
Chris Ye3fdbfef2021-01-06 18:45:18 -0800103// Mapping for light color name and the light color
104const std::unordered_map<std::string, LightColor> LIGHT_COLORS = {{"red", LightColor::RED},
105 {"green", LightColor::GREEN},
106 {"blue", LightColor::BLUE}};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800107
Prabir Pradhanc14266f2021-05-12 15:56:24 -0700108static int32_t getInverseRotation(int32_t orientation) {
109 switch (orientation) {
110 case DISPLAY_ORIENTATION_90:
111 return DISPLAY_ORIENTATION_270;
112 case DISPLAY_ORIENTATION_270:
113 return DISPLAY_ORIENTATION_90;
114 default:
115 return orientation;
116 }
117}
118
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800119static void assertAxisResolution(MultiTouchInputMapper& mapper, int axis, float resolution) {
120 InputDeviceInfo info;
121 mapper.populateDeviceInfo(&info);
122
123 const InputDeviceInfo::MotionRange* motionRange =
124 info.getMotionRange(axis, AINPUT_SOURCE_TOUCHSCREEN);
125 ASSERT_NEAR(motionRange->resolution, resolution, EPSILON);
126}
127
128static void assertAxisNotPresent(MultiTouchInputMapper& mapper, int axis) {
129 InputDeviceInfo info;
130 mapper.populateDeviceInfo(&info);
131
132 const InputDeviceInfo::MotionRange* motionRange =
133 info.getMotionRange(axis, AINPUT_SOURCE_TOUCHSCREEN);
134 ASSERT_EQ(nullptr, motionRange);
135}
136
Michael Wrightd02c5b62014-02-10 15:10:22 -0800137// --- FakePointerController ---
138
139class FakePointerController : public PointerControllerInterface {
140 bool mHaveBounds;
141 float mMinX, mMinY, mMaxX, mMaxY;
142 float mX, mY;
143 int32_t mButtonState;
Arthur Hungc7ad2d02018-12-18 17:41:29 +0800144 int32_t mDisplayId;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800145
Michael Wrightd02c5b62014-02-10 15:10:22 -0800146public:
147 FakePointerController() :
148 mHaveBounds(false), mMinX(0), mMinY(0), mMaxX(0), mMaxY(0), mX(0), mY(0),
Arthur Hungc7ad2d02018-12-18 17:41:29 +0800149 mButtonState(0), mDisplayId(ADISPLAY_ID_DEFAULT) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800150 }
151
Michael Wright17db18e2020-06-26 20:51:44 +0100152 virtual ~FakePointerController() {}
153
Michael Wrightd02c5b62014-02-10 15:10:22 -0800154 void setBounds(float minX, float minY, float maxX, float maxY) {
155 mHaveBounds = true;
156 mMinX = minX;
157 mMinY = minY;
158 mMaxX = maxX;
159 mMaxY = maxY;
160 }
161
Chris Yea52ade12020-08-27 16:49:20 -0700162 void setPosition(float x, float y) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800163 mX = x;
164 mY = y;
165 }
166
Chris Yea52ade12020-08-27 16:49:20 -0700167 void setButtonState(int32_t buttonState) override { mButtonState = buttonState; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800168
Chris Yea52ade12020-08-27 16:49:20 -0700169 int32_t getButtonState() const override { return mButtonState; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800170
Chris Yea52ade12020-08-27 16:49:20 -0700171 void getPosition(float* outX, float* outY) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800172 *outX = mX;
173 *outY = mY;
174 }
175
Chris Yea52ade12020-08-27 16:49:20 -0700176 int32_t getDisplayId() const override { return mDisplayId; }
Arthur Hungc7ad2d02018-12-18 17:41:29 +0800177
Chris Yea52ade12020-08-27 16:49:20 -0700178 void setDisplayViewport(const DisplayViewport& viewport) override {
Garfield Tan888a6a42020-01-09 11:39:16 -0800179 mDisplayId = viewport.displayId;
180 }
181
Arthur Hung7c645402019-01-25 17:45:42 +0800182 const std::map<int32_t, std::vector<int32_t>>& getSpots() {
183 return mSpotsByDisplay;
184 }
185
Michael Wrightd02c5b62014-02-10 15:10:22 -0800186private:
Chris Yea52ade12020-08-27 16:49:20 -0700187 bool getBounds(float* outMinX, float* outMinY, float* outMaxX, float* outMaxY) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800188 *outMinX = mMinX;
189 *outMinY = mMinY;
190 *outMaxX = mMaxX;
191 *outMaxY = mMaxY;
192 return mHaveBounds;
193 }
194
Chris Yea52ade12020-08-27 16:49:20 -0700195 void move(float deltaX, float deltaY) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800196 mX += deltaX;
197 if (mX < mMinX) mX = mMinX;
198 if (mX > mMaxX) mX = mMaxX;
199 mY += deltaY;
200 if (mY < mMinY) mY = mMinY;
201 if (mY > mMaxY) mY = mMaxY;
202 }
203
Chris Yea52ade12020-08-27 16:49:20 -0700204 void fade(Transition) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800205
Chris Yea52ade12020-08-27 16:49:20 -0700206 void unfade(Transition) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800207
Chris Yea52ade12020-08-27 16:49:20 -0700208 void setPresentation(Presentation) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800209
Chris Yea52ade12020-08-27 16:49:20 -0700210 void setSpots(const PointerCoords*, const uint32_t*, BitSet32 spotIdBits,
211 int32_t displayId) override {
Arthur Hung7c645402019-01-25 17:45:42 +0800212 std::vector<int32_t> newSpots;
213 // Add spots for fingers that are down.
214 for (BitSet32 idBits(spotIdBits); !idBits.isEmpty(); ) {
215 uint32_t id = idBits.clearFirstMarkedBit();
216 newSpots.push_back(id);
217 }
218
219 mSpotsByDisplay[displayId] = newSpots;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800220 }
221
Chris Yea52ade12020-08-27 16:49:20 -0700222 void clearSpots() override {}
Arthur Hung7c645402019-01-25 17:45:42 +0800223
224 std::map<int32_t, std::vector<int32_t>> mSpotsByDisplay;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800225};
226
227
228// --- FakeInputReaderPolicy ---
229
230class FakeInputReaderPolicy : public InputReaderPolicyInterface {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700231 std::mutex mLock;
232 std::condition_variable mDevicesChangedCondition;
233
Michael Wrightd02c5b62014-02-10 15:10:22 -0800234 InputReaderConfiguration mConfig;
Prabir Pradhan2853b7a2021-08-23 14:08:51 +0000235 std::shared_ptr<FakePointerController> mPointerController;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700236 std::vector<InputDeviceInfo> mInputDevices GUARDED_BY(mLock);
237 bool mInputDevicesChanged GUARDED_BY(mLock){false};
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100238 std::vector<DisplayViewport> mViewports;
Jason Gerecke489fda82012-09-07 17:19:40 -0700239 TouchAffineTransformation transform;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800240
241protected:
Chris Yea52ade12020-08-27 16:49:20 -0700242 virtual ~FakeInputReaderPolicy() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800243
244public:
245 FakeInputReaderPolicy() {
246 }
247
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700248 void assertInputDevicesChanged() {
Prabir Pradhan1aed8582019-12-30 11:46:51 -0800249 waitForInputDevices([](bool devicesChanged) {
250 if (!devicesChanged) {
251 FAIL() << "Timed out waiting for notifyInputDevicesChanged() to be called.";
252 }
253 });
254 }
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700255
Prabir Pradhan1aed8582019-12-30 11:46:51 -0800256 void assertInputDevicesNotChanged() {
257 waitForInputDevices([](bool devicesChanged) {
258 if (devicesChanged) {
259 FAIL() << "Expected notifyInputDevicesChanged() to not be called.";
260 }
261 });
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700262 }
263
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700264 virtual void clearViewports() {
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100265 mViewports.clear();
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100266 mConfig.setDisplayViewports(mViewports);
Santos Cordonfa5cf462017-04-05 10:37:00 -0700267 }
268
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700269 std::optional<DisplayViewport> getDisplayViewportByUniqueId(const std::string& uniqueId) const {
270 return mConfig.getDisplayViewportByUniqueId(uniqueId);
271 }
272 std::optional<DisplayViewport> getDisplayViewportByType(ViewportType type) const {
273 return mConfig.getDisplayViewportByType(type);
274 }
275
276 std::optional<DisplayViewport> getDisplayViewportByPort(uint8_t displayPort) const {
277 return mConfig.getDisplayViewportByPort(displayPort);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700278 }
279
Prabir Pradhan5632d622021-09-06 07:57:20 -0700280 void addDisplayViewport(DisplayViewport viewport) {
281 mViewports.push_back(std::move(viewport));
282 mConfig.setDisplayViewports(mViewports);
283 }
284
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700285 void addDisplayViewport(int32_t displayId, int32_t width, int32_t height, int32_t orientation,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +0000286 bool isActive, const std::string& uniqueId,
Prabir Pradhan5632d622021-09-06 07:57:20 -0700287 std::optional<uint8_t> physicalPort, ViewportType type) {
288 const bool isRotated =
289 (orientation == DISPLAY_ORIENTATION_90 || orientation == DISPLAY_ORIENTATION_270);
290 DisplayViewport v;
291 v.displayId = displayId;
292 v.orientation = orientation;
293 v.logicalLeft = 0;
294 v.logicalTop = 0;
295 v.logicalRight = isRotated ? height : width;
296 v.logicalBottom = isRotated ? width : height;
297 v.physicalLeft = 0;
298 v.physicalTop = 0;
299 v.physicalRight = isRotated ? height : width;
300 v.physicalBottom = isRotated ? width : height;
301 v.deviceWidth = isRotated ? height : width;
302 v.deviceHeight = isRotated ? width : height;
303 v.isActive = isActive;
304 v.uniqueId = uniqueId;
305 v.physicalPort = physicalPort;
306 v.type = type;
307
308 addDisplayViewport(v);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800309 }
310
Arthur Hung6cd19a42019-08-30 19:04:12 +0800311 bool updateViewport(const DisplayViewport& viewport) {
312 size_t count = mViewports.size();
313 for (size_t i = 0; i < count; i++) {
314 const DisplayViewport& currentViewport = mViewports[i];
315 if (currentViewport.displayId == viewport.displayId) {
316 mViewports[i] = viewport;
317 mConfig.setDisplayViewports(mViewports);
318 return true;
319 }
320 }
321 // no viewport found.
322 return false;
323 }
324
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100325 void addExcludedDeviceName(const std::string& deviceName) {
326 mConfig.excludedDeviceNames.push_back(deviceName);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800327 }
328
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700329 void addInputPortAssociation(const std::string& inputPort, uint8_t displayPort) {
330 mConfig.portAssociations.insert({inputPort, displayPort});
331 }
332
Christine Franks1ba71cc2021-04-07 14:37:42 -0700333 void addInputUniqueIdAssociation(const std::string& inputUniqueId,
334 const std::string& displayUniqueId) {
335 mConfig.uniqueIdAssociations.insert({inputUniqueId, displayUniqueId});
336 }
337
Siarhei Vishniakouc6f61192019-07-23 18:12:31 +0000338 void addDisabledDevice(int32_t deviceId) { mConfig.disabledDevices.insert(deviceId); }
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700339
Siarhei Vishniakouc6f61192019-07-23 18:12:31 +0000340 void removeDisabledDevice(int32_t deviceId) { mConfig.disabledDevices.erase(deviceId); }
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700341
Prabir Pradhan2853b7a2021-08-23 14:08:51 +0000342 void setPointerController(std::shared_ptr<FakePointerController> controller) {
343 mPointerController = std::move(controller);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800344 }
345
346 const InputReaderConfiguration* getReaderConfiguration() const {
347 return &mConfig;
348 }
349
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800350 const std::vector<InputDeviceInfo>& getInputDevices() const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800351 return mInputDevices;
352 }
353
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100354 TouchAffineTransformation getTouchAffineTransformation(const std::string& inputDeviceDescriptor,
Jason Gerecke71b16e82014-03-10 09:47:59 -0700355 int32_t surfaceRotation) {
Jason Gerecke489fda82012-09-07 17:19:40 -0700356 return transform;
357 }
358
359 void setTouchAffineTransformation(const TouchAffineTransformation t) {
360 transform = t;
Jason Gerecke12d6baa2014-01-27 18:34:20 -0800361 }
362
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000363 PointerCaptureRequest setPointerCapture(bool enabled) {
364 mConfig.pointerCaptureRequest = {enabled, mNextPointerCaptureSequenceNumber++};
365 return mConfig.pointerCaptureRequest;
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -0800366 }
367
Arthur Hung7c645402019-01-25 17:45:42 +0800368 void setShowTouches(bool enabled) {
369 mConfig.showTouches = enabled;
370 }
371
Garfield Tan888a6a42020-01-09 11:39:16 -0800372 void setDefaultPointerDisplayId(int32_t pointerDisplayId) {
373 mConfig.defaultPointerDisplayId = pointerDisplayId;
374 }
375
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -0800376 float getPointerGestureMovementSpeedRatio() { return mConfig.pointerGestureMovementSpeedRatio; }
377
Michael Wrightd02c5b62014-02-10 15:10:22 -0800378private:
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000379 uint32_t mNextPointerCaptureSequenceNumber = 0;
380
Chris Yea52ade12020-08-27 16:49:20 -0700381 void getReaderConfiguration(InputReaderConfiguration* outConfig) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800382 *outConfig = mConfig;
383 }
384
Prabir Pradhan2853b7a2021-08-23 14:08:51 +0000385 std::shared_ptr<PointerControllerInterface> obtainPointerController(
386 int32_t /*deviceId*/) override {
387 return mPointerController;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800388 }
389
Chris Yea52ade12020-08-27 16:49:20 -0700390 void notifyInputDevicesChanged(const std::vector<InputDeviceInfo>& inputDevices) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700391 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800392 mInputDevices = inputDevices;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700393 mInputDevicesChanged = true;
394 mDevicesChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800395 }
396
Chris Yea52ade12020-08-27 16:49:20 -0700397 std::shared_ptr<KeyCharacterMap> getKeyboardLayoutOverlay(
398 const InputDeviceIdentifier&) override {
Yi Kong9b14ac62018-07-17 13:48:38 -0700399 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800400 }
401
Chris Yea52ade12020-08-27 16:49:20 -0700402 std::string getDeviceAlias(const InputDeviceIdentifier&) override { return ""; }
Prabir Pradhan1aed8582019-12-30 11:46:51 -0800403
404 void waitForInputDevices(std::function<void(bool)> processDevicesChanged) {
405 std::unique_lock<std::mutex> lock(mLock);
406 base::ScopedLockAssertion assumeLocked(mLock);
407
408 const bool devicesChanged =
409 mDevicesChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
410 return mInputDevicesChanged;
411 });
412 ASSERT_NO_FATAL_FAILURE(processDevicesChanged(devicesChanged));
413 mInputDevicesChanged = false;
414 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800415};
416
Michael Wrightd02c5b62014-02-10 15:10:22 -0800417// --- FakeEventHub ---
418
419class FakeEventHub : public EventHubInterface {
420 struct KeyInfo {
421 int32_t keyCode;
422 uint32_t flags;
423 };
424
Chris Yef59a2f42020-10-16 12:55:26 -0700425 struct SensorInfo {
426 InputDeviceSensorType sensorType;
427 int32_t sensorDataIndex;
428 };
429
Michael Wrightd02c5b62014-02-10 15:10:22 -0800430 struct Device {
431 InputDeviceIdentifier identifier;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700432 ftl::Flags<InputDeviceClass> classes;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800433 PropertyMap configuration;
434 KeyedVector<int, RawAbsoluteAxisInfo> absoluteAxes;
435 KeyedVector<int, bool> relativeAxes;
436 KeyedVector<int32_t, int32_t> keyCodeStates;
437 KeyedVector<int32_t, int32_t> scanCodeStates;
438 KeyedVector<int32_t, int32_t> switchStates;
439 KeyedVector<int32_t, int32_t> absoluteAxisValue;
440 KeyedVector<int32_t, KeyInfo> keysByScanCode;
441 KeyedVector<int32_t, KeyInfo> keysByUsageCode;
442 KeyedVector<int32_t, bool> leds;
Philip Junker4af3b3d2021-12-14 10:36:55 +0100443 // fake mapping which would normally come from keyCharacterMap
444 std::unordered_map<int32_t, int32_t> keyCodeMapping;
Chris Yef59a2f42020-10-16 12:55:26 -0700445 std::unordered_map<int32_t, SensorInfo> sensorsByAbsCode;
446 BitArray<MSC_MAX> mscBitmask;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800447 std::vector<VirtualKeyDefinition> virtualKeys;
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700448 bool enabled;
449
450 status_t enable() {
451 enabled = true;
452 return OK;
453 }
454
455 status_t disable() {
456 enabled = false;
457 return OK;
458 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800459
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700460 explicit Device(ftl::Flags<InputDeviceClass> classes) : classes(classes), enabled(true) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800461 };
462
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700463 std::mutex mLock;
464 std::condition_variable mEventsCondition;
465
Michael Wrightd02c5b62014-02-10 15:10:22 -0800466 KeyedVector<int32_t, Device*> mDevices;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100467 std::vector<std::string> mExcludedDevices;
Siarhei Vishniakou370039c2021-02-04 22:09:01 +0000468 std::vector<RawEvent> mEvents GUARDED_BY(mLock);
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600469 std::unordered_map<int32_t /*deviceId*/, std::vector<TouchVideoFrame>> mVideoFrames;
Chris Ye87143712020-11-10 05:05:58 +0000470 std::vector<int32_t> mVibrators = {0, 1};
Chris Ye3fdbfef2021-01-06 18:45:18 -0800471 std::unordered_map<int32_t, RawLightInfo> mRawLightInfos;
472 // Simulates a device light brightness, from light id to light brightness.
473 std::unordered_map<int32_t /* lightId */, int32_t /* brightness*/> mLightBrightness;
474 // Simulates a device light intensities, from light id to light intensities map.
475 std::unordered_map<int32_t /* lightId */, std::unordered_map<LightColor, int32_t>>
476 mLightIntensities;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800477
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700478public:
Michael Wrightd02c5b62014-02-10 15:10:22 -0800479 virtual ~FakeEventHub() {
480 for (size_t i = 0; i < mDevices.size(); i++) {
481 delete mDevices.valueAt(i);
482 }
483 }
484
Michael Wrightd02c5b62014-02-10 15:10:22 -0800485 FakeEventHub() { }
486
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700487 void addDevice(int32_t deviceId, const std::string& name,
488 ftl::Flags<InputDeviceClass> classes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800489 Device* device = new Device(classes);
490 device->identifier.name = name;
491 mDevices.add(deviceId, device);
492
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000493 enqueueEvent(ARBITRARY_TIME, READ_TIME, deviceId, EventHubInterface::DEVICE_ADDED, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800494 }
495
496 void removeDevice(int32_t deviceId) {
497 delete mDevices.valueFor(deviceId);
498 mDevices.removeItem(deviceId);
499
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000500 enqueueEvent(ARBITRARY_TIME, READ_TIME, deviceId, EventHubInterface::DEVICE_REMOVED, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800501 }
502
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700503 bool isDeviceEnabled(int32_t deviceId) {
504 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700505 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700506 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
507 return false;
508 }
509 return device->enabled;
510 }
511
512 status_t enableDevice(int32_t deviceId) {
513 status_t result;
514 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700515 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700516 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
517 return BAD_VALUE;
518 }
519 if (device->enabled) {
520 ALOGW("Duplicate call to %s, device %" PRId32 " already enabled", __func__, deviceId);
521 return OK;
522 }
523 result = device->enable();
524 return result;
525 }
526
527 status_t disableDevice(int32_t deviceId) {
528 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700529 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700530 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
531 return BAD_VALUE;
532 }
533 if (!device->enabled) {
534 ALOGW("Duplicate call to %s, device %" PRId32 " already disabled", __func__, deviceId);
535 return OK;
536 }
537 return device->disable();
538 }
539
Michael Wrightd02c5b62014-02-10 15:10:22 -0800540 void finishDeviceScan() {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000541 enqueueEvent(ARBITRARY_TIME, READ_TIME, 0, EventHubInterface::FINISHED_DEVICE_SCAN, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800542 }
543
544 void addConfigurationProperty(int32_t deviceId, const String8& key, const String8& value) {
545 Device* device = getDevice(deviceId);
546 device->configuration.addProperty(key, value);
547 }
548
549 void addConfigurationMap(int32_t deviceId, const PropertyMap* configuration) {
550 Device* device = getDevice(deviceId);
551 device->configuration.addAll(configuration);
552 }
553
554 void addAbsoluteAxis(int32_t deviceId, int axis,
555 int32_t minValue, int32_t maxValue, int flat, int fuzz, int resolution = 0) {
556 Device* device = getDevice(deviceId);
557
558 RawAbsoluteAxisInfo info;
559 info.valid = true;
560 info.minValue = minValue;
561 info.maxValue = maxValue;
562 info.flat = flat;
563 info.fuzz = fuzz;
564 info.resolution = resolution;
565 device->absoluteAxes.add(axis, info);
566 }
567
568 void addRelativeAxis(int32_t deviceId, int32_t axis) {
569 Device* device = getDevice(deviceId);
570 device->relativeAxes.add(axis, true);
571 }
572
573 void setKeyCodeState(int32_t deviceId, int32_t keyCode, int32_t state) {
574 Device* device = getDevice(deviceId);
575 device->keyCodeStates.replaceValueFor(keyCode, state);
576 }
577
578 void setScanCodeState(int32_t deviceId, int32_t scanCode, int32_t state) {
579 Device* device = getDevice(deviceId);
580 device->scanCodeStates.replaceValueFor(scanCode, state);
581 }
582
583 void setSwitchState(int32_t deviceId, int32_t switchCode, int32_t state) {
584 Device* device = getDevice(deviceId);
585 device->switchStates.replaceValueFor(switchCode, state);
586 }
587
588 void setAbsoluteAxisValue(int32_t deviceId, int32_t axis, int32_t value) {
589 Device* device = getDevice(deviceId);
590 device->absoluteAxisValue.replaceValueFor(axis, value);
591 }
592
593 void addKey(int32_t deviceId, int32_t scanCode, int32_t usageCode,
594 int32_t keyCode, uint32_t flags) {
595 Device* device = getDevice(deviceId);
596 KeyInfo info;
597 info.keyCode = keyCode;
598 info.flags = flags;
599 if (scanCode) {
600 device->keysByScanCode.add(scanCode, info);
601 }
602 if (usageCode) {
603 device->keysByUsageCode.add(usageCode, info);
604 }
605 }
606
Philip Junker4af3b3d2021-12-14 10:36:55 +0100607 void addKeyCodeMapping(int32_t deviceId, int32_t fromKeyCode, int32_t toKeyCode) {
608 Device* device = getDevice(deviceId);
609 device->keyCodeMapping.insert_or_assign(fromKeyCode, toKeyCode);
610 }
611
Michael Wrightd02c5b62014-02-10 15:10:22 -0800612 void addLed(int32_t deviceId, int32_t led, bool initialState) {
613 Device* device = getDevice(deviceId);
614 device->leds.add(led, initialState);
615 }
616
Chris Yef59a2f42020-10-16 12:55:26 -0700617 void addSensorAxis(int32_t deviceId, int32_t absCode, InputDeviceSensorType sensorType,
618 int32_t sensorDataIndex) {
619 Device* device = getDevice(deviceId);
620 SensorInfo info;
621 info.sensorType = sensorType;
622 info.sensorDataIndex = sensorDataIndex;
623 device->sensorsByAbsCode.emplace(absCode, info);
624 }
625
626 void setMscEvent(int32_t deviceId, int32_t mscEvent) {
627 Device* device = getDevice(deviceId);
628 typename BitArray<MSC_MAX>::Buffer buffer;
629 buffer[mscEvent / 32] = 1 << mscEvent % 32;
630 device->mscBitmask.loadFromBuffer(buffer);
631 }
632
Chris Ye3fdbfef2021-01-06 18:45:18 -0800633 void addRawLightInfo(int32_t rawId, RawLightInfo&& info) {
634 mRawLightInfos.emplace(rawId, std::move(info));
635 }
636
637 void fakeLightBrightness(int32_t rawId, int32_t brightness) {
638 mLightBrightness.emplace(rawId, brightness);
639 }
640
641 void fakeLightIntensities(int32_t rawId,
642 const std::unordered_map<LightColor, int32_t> intensities) {
643 mLightIntensities.emplace(rawId, std::move(intensities));
644 }
645
Michael Wrightd02c5b62014-02-10 15:10:22 -0800646 bool getLedState(int32_t deviceId, int32_t led) {
647 Device* device = getDevice(deviceId);
648 return device->leds.valueFor(led);
649 }
650
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100651 std::vector<std::string>& getExcludedDevices() {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800652 return mExcludedDevices;
653 }
654
655 void addVirtualKeyDefinition(int32_t deviceId, const VirtualKeyDefinition& definition) {
656 Device* device = getDevice(deviceId);
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800657 device->virtualKeys.push_back(definition);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800658 }
659
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000660 void enqueueEvent(nsecs_t when, nsecs_t readTime, int32_t deviceId, int32_t type, int32_t code,
661 int32_t value) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700662 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800663 RawEvent event;
664 event.when = when;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000665 event.readTime = readTime;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800666 event.deviceId = deviceId;
667 event.type = type;
668 event.code = code;
669 event.value = value;
670 mEvents.push_back(event);
671
672 if (type == EV_ABS) {
673 setAbsoluteAxisValue(deviceId, code, value);
674 }
675 }
676
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600677 void setVideoFrames(std::unordered_map<int32_t /*deviceId*/,
678 std::vector<TouchVideoFrame>> videoFrames) {
679 mVideoFrames = std::move(videoFrames);
680 }
681
Michael Wrightd02c5b62014-02-10 15:10:22 -0800682 void assertQueueIsEmpty() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700683 std::unique_lock<std::mutex> lock(mLock);
684 base::ScopedLockAssertion assumeLocked(mLock);
685 const bool queueIsEmpty =
686 mEventsCondition.wait_for(lock, WAIT_TIMEOUT,
687 [this]() REQUIRES(mLock) { return mEvents.size() == 0; });
688 if (!queueIsEmpty) {
689 FAIL() << "Timed out waiting for EventHub queue to be emptied.";
690 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800691 }
692
693private:
694 Device* getDevice(int32_t deviceId) const {
695 ssize_t index = mDevices.indexOfKey(deviceId);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100696 return index >= 0 ? mDevices.valueAt(index) : nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800697 }
698
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700699 ftl::Flags<InputDeviceClass> getDeviceClasses(int32_t deviceId) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800700 Device* device = getDevice(deviceId);
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700701 return device ? device->classes : ftl::Flags<InputDeviceClass>(0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800702 }
703
Chris Yea52ade12020-08-27 16:49:20 -0700704 InputDeviceIdentifier getDeviceIdentifier(int32_t deviceId) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800705 Device* device = getDevice(deviceId);
706 return device ? device->identifier : InputDeviceIdentifier();
707 }
708
Chris Yea52ade12020-08-27 16:49:20 -0700709 int32_t getDeviceControllerNumber(int32_t) const override { return 0; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800710
Chris Yea52ade12020-08-27 16:49:20 -0700711 void getConfiguration(int32_t deviceId, PropertyMap* outConfiguration) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800712 Device* device = getDevice(deviceId);
713 if (device) {
714 *outConfiguration = device->configuration;
715 }
716 }
717
Chris Yea52ade12020-08-27 16:49:20 -0700718 status_t getAbsoluteAxisInfo(int32_t deviceId, int axis,
719 RawAbsoluteAxisInfo* outAxisInfo) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800720 Device* device = getDevice(deviceId);
Arthur Hung9da14732019-09-02 16:16:58 +0800721 if (device && device->enabled) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800722 ssize_t index = device->absoluteAxes.indexOfKey(axis);
723 if (index >= 0) {
724 *outAxisInfo = device->absoluteAxes.valueAt(index);
725 return OK;
726 }
727 }
728 outAxisInfo->clear();
729 return -1;
730 }
731
Chris Yea52ade12020-08-27 16:49:20 -0700732 bool hasRelativeAxis(int32_t deviceId, int axis) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800733 Device* device = getDevice(deviceId);
734 if (device) {
735 return device->relativeAxes.indexOfKey(axis) >= 0;
736 }
737 return false;
738 }
739
Chris Yea52ade12020-08-27 16:49:20 -0700740 bool hasInputProperty(int32_t, int) const override { return false; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800741
Chris Yef59a2f42020-10-16 12:55:26 -0700742 bool hasMscEvent(int32_t deviceId, int mscEvent) const override final {
743 Device* device = getDevice(deviceId);
744 if (device) {
745 return mscEvent >= 0 && mscEvent <= MSC_MAX ? device->mscBitmask.test(mscEvent) : false;
746 }
747 return false;
748 }
749
Chris Yea52ade12020-08-27 16:49:20 -0700750 status_t mapKey(int32_t deviceId, int32_t scanCode, int32_t usageCode, int32_t metaState,
751 int32_t* outKeycode, int32_t* outMetaState, uint32_t* outFlags) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800752 Device* device = getDevice(deviceId);
753 if (device) {
754 const KeyInfo* key = getKey(device, scanCode, usageCode);
755 if (key) {
756 if (outKeycode) {
757 *outKeycode = key->keyCode;
758 }
759 if (outFlags) {
760 *outFlags = key->flags;
761 }
Dmitry Torokhov0faaa0b2015-09-24 13:13:55 -0700762 if (outMetaState) {
763 *outMetaState = metaState;
764 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800765 return OK;
766 }
767 }
768 return NAME_NOT_FOUND;
769 }
770
771 const KeyInfo* getKey(Device* device, int32_t scanCode, int32_t usageCode) const {
772 if (usageCode) {
773 ssize_t index = device->keysByUsageCode.indexOfKey(usageCode);
774 if (index >= 0) {
775 return &device->keysByUsageCode.valueAt(index);
776 }
777 }
778 if (scanCode) {
779 ssize_t index = device->keysByScanCode.indexOfKey(scanCode);
780 if (index >= 0) {
781 return &device->keysByScanCode.valueAt(index);
782 }
783 }
Yi Kong9b14ac62018-07-17 13:48:38 -0700784 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800785 }
786
Chris Yea52ade12020-08-27 16:49:20 -0700787 status_t mapAxis(int32_t, int32_t, AxisInfo*) const override { return NAME_NOT_FOUND; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800788
Chris Yef59a2f42020-10-16 12:55:26 -0700789 base::Result<std::pair<InputDeviceSensorType, int32_t>> mapSensor(int32_t deviceId,
790 int32_t absCode) {
791 Device* device = getDevice(deviceId);
792 if (!device) {
793 return Errorf("Sensor device not found.");
794 }
795 auto it = device->sensorsByAbsCode.find(absCode);
796 if (it == device->sensorsByAbsCode.end()) {
797 return Errorf("Sensor map not found.");
798 }
799 const SensorInfo& info = it->second;
800 return std::make_pair(info.sensorType, info.sensorDataIndex);
801 }
802
Chris Yea52ade12020-08-27 16:49:20 -0700803 void setExcludedDevices(const std::vector<std::string>& devices) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800804 mExcludedDevices = devices;
805 }
806
Siarhei Vishniakou370039c2021-02-04 22:09:01 +0000807 size_t getEvents(int, RawEvent* buffer, size_t bufferSize) override {
808 std::scoped_lock lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800809
Siarhei Vishniakou370039c2021-02-04 22:09:01 +0000810 const size_t filledSize = std::min(mEvents.size(), bufferSize);
811 std::copy(mEvents.begin(), mEvents.begin() + filledSize, buffer);
812
813 mEvents.erase(mEvents.begin(), mEvents.begin() + filledSize);
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700814 mEventsCondition.notify_all();
Siarhei Vishniakou370039c2021-02-04 22:09:01 +0000815 return filledSize;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800816 }
817
Chris Yea52ade12020-08-27 16:49:20 -0700818 std::vector<TouchVideoFrame> getVideoFrames(int32_t deviceId) override {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600819 auto it = mVideoFrames.find(deviceId);
820 if (it != mVideoFrames.end()) {
821 std::vector<TouchVideoFrame> frames = std::move(it->second);
822 mVideoFrames.erase(deviceId);
823 return frames;
824 }
Siarhei Vishniakouadd89292018-12-13 19:23:36 -0800825 return {};
826 }
827
Chris Yea52ade12020-08-27 16:49:20 -0700828 int32_t getScanCodeState(int32_t deviceId, int32_t scanCode) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800829 Device* device = getDevice(deviceId);
830 if (device) {
831 ssize_t index = device->scanCodeStates.indexOfKey(scanCode);
832 if (index >= 0) {
833 return device->scanCodeStates.valueAt(index);
834 }
835 }
836 return AKEY_STATE_UNKNOWN;
837 }
838
Chris Yea52ade12020-08-27 16:49:20 -0700839 int32_t getKeyCodeState(int32_t deviceId, int32_t keyCode) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800840 Device* device = getDevice(deviceId);
841 if (device) {
842 ssize_t index = device->keyCodeStates.indexOfKey(keyCode);
843 if (index >= 0) {
844 return device->keyCodeStates.valueAt(index);
845 }
846 }
847 return AKEY_STATE_UNKNOWN;
848 }
849
Chris Yea52ade12020-08-27 16:49:20 -0700850 int32_t getSwitchState(int32_t deviceId, int32_t sw) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800851 Device* device = getDevice(deviceId);
852 if (device) {
853 ssize_t index = device->switchStates.indexOfKey(sw);
854 if (index >= 0) {
855 return device->switchStates.valueAt(index);
856 }
857 }
858 return AKEY_STATE_UNKNOWN;
859 }
860
Chris Yea52ade12020-08-27 16:49:20 -0700861 status_t getAbsoluteAxisValue(int32_t deviceId, int32_t axis,
862 int32_t* outValue) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800863 Device* device = getDevice(deviceId);
864 if (device) {
865 ssize_t index = device->absoluteAxisValue.indexOfKey(axis);
866 if (index >= 0) {
867 *outValue = device->absoluteAxisValue.valueAt(index);
868 return OK;
869 }
870 }
871 *outValue = 0;
872 return -1;
873 }
874
Philip Junker4af3b3d2021-12-14 10:36:55 +0100875 int32_t getKeyCodeForKeyLocation(int32_t deviceId, int32_t locationKeyCode) const override {
876 Device* device = getDevice(deviceId);
877 if (!device) {
878 return AKEYCODE_UNKNOWN;
879 }
880 auto it = device->keyCodeMapping.find(locationKeyCode);
881 return it != device->keyCodeMapping.end() ? it->second : locationKeyCode;
882 }
883
Chris Yea52ade12020-08-27 16:49:20 -0700884 // Return true if the device has non-empty key layout.
885 bool markSupportedKeyCodes(int32_t deviceId, size_t numCodes, const int32_t* keyCodes,
886 uint8_t* outFlags) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800887 bool result = false;
888 Device* device = getDevice(deviceId);
889 if (device) {
Chris Yea52ade12020-08-27 16:49:20 -0700890 result = device->keysByScanCode.size() > 0 || device->keysByUsageCode.size() > 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800891 for (size_t i = 0; i < numCodes; i++) {
892 for (size_t j = 0; j < device->keysByScanCode.size(); j++) {
893 if (keyCodes[i] == device->keysByScanCode.valueAt(j).keyCode) {
894 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800895 }
896 }
897 for (size_t j = 0; j < device->keysByUsageCode.size(); j++) {
898 if (keyCodes[i] == device->keysByUsageCode.valueAt(j).keyCode) {
899 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800900 }
901 }
902 }
903 }
904 return result;
905 }
906
Chris Yea52ade12020-08-27 16:49:20 -0700907 bool hasScanCode(int32_t deviceId, int32_t scanCode) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800908 Device* device = getDevice(deviceId);
909 if (device) {
910 ssize_t index = device->keysByScanCode.indexOfKey(scanCode);
911 return index >= 0;
912 }
913 return false;
914 }
915
Arthur Hungcb40a002021-08-03 14:31:01 +0000916 bool hasKeyCode(int32_t deviceId, int32_t keyCode) const override {
917 Device* device = getDevice(deviceId);
918 if (!device) {
919 return false;
920 }
921 for (size_t i = 0; i < device->keysByScanCode.size(); i++) {
922 if (keyCode == device->keysByScanCode.valueAt(i).keyCode) {
923 return true;
924 }
925 }
926 for (size_t j = 0; j < device->keysByUsageCode.size(); j++) {
927 if (keyCode == device->keysByUsageCode.valueAt(j).keyCode) {
928 return true;
929 }
930 }
931 return false;
932 }
933
Chris Yea52ade12020-08-27 16:49:20 -0700934 bool hasLed(int32_t deviceId, int32_t led) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800935 Device* device = getDevice(deviceId);
936 return device && device->leds.indexOfKey(led) >= 0;
937 }
938
Chris Yea52ade12020-08-27 16:49:20 -0700939 void setLedState(int32_t deviceId, int32_t led, bool on) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800940 Device* device = getDevice(deviceId);
941 if (device) {
942 ssize_t index = device->leds.indexOfKey(led);
943 if (index >= 0) {
944 device->leds.replaceValueAt(led, on);
945 } else {
946 ADD_FAILURE()
947 << "Attempted to set the state of an LED that the EventHub declared "
948 "was not present. led=" << led;
949 }
950 }
951 }
952
Chris Yea52ade12020-08-27 16:49:20 -0700953 void getVirtualKeyDefinitions(
954 int32_t deviceId, std::vector<VirtualKeyDefinition>& outVirtualKeys) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800955 outVirtualKeys.clear();
956
957 Device* device = getDevice(deviceId);
958 if (device) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800959 outVirtualKeys = device->virtualKeys;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800960 }
961 }
962
Chris Yea52ade12020-08-27 16:49:20 -0700963 const std::shared_ptr<KeyCharacterMap> getKeyCharacterMap(int32_t) const override {
Yi Kong9b14ac62018-07-17 13:48:38 -0700964 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800965 }
966
Chris Yea52ade12020-08-27 16:49:20 -0700967 bool setKeyboardLayoutOverlay(int32_t, std::shared_ptr<KeyCharacterMap>) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800968 return false;
969 }
970
Chris Yea52ade12020-08-27 16:49:20 -0700971 void vibrate(int32_t, const VibrationElement&) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800972
Chris Yea52ade12020-08-27 16:49:20 -0700973 void cancelVibrate(int32_t) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800974
Chris Ye87143712020-11-10 05:05:58 +0000975 std::vector<int32_t> getVibratorIds(int32_t deviceId) override { return mVibrators; };
976
Chris Yee2b1e5c2021-03-10 22:45:12 -0800977 std::optional<int32_t> getBatteryCapacity(int32_t, int32_t) const override {
978 return BATTERY_CAPACITY;
979 }
Kim Low03ea0352020-11-06 12:45:07 -0800980
Chris Yee2b1e5c2021-03-10 22:45:12 -0800981 std::optional<int32_t> getBatteryStatus(int32_t, int32_t) const override {
982 return BATTERY_STATUS;
983 }
984
985 const std::vector<int32_t> getRawBatteryIds(int32_t deviceId) { return {}; }
986
987 std::optional<RawBatteryInfo> getRawBatteryInfo(int32_t deviceId, int32_t batteryId) {
988 return std::nullopt;
989 }
Kim Low03ea0352020-11-06 12:45:07 -0800990
Chris Ye3fdbfef2021-01-06 18:45:18 -0800991 const std::vector<int32_t> getRawLightIds(int32_t deviceId) override {
992 std::vector<int32_t> ids;
993 for (const auto& [rawId, info] : mRawLightInfos) {
994 ids.push_back(rawId);
995 }
996 return ids;
997 }
998
999 std::optional<RawLightInfo> getRawLightInfo(int32_t deviceId, int32_t lightId) override {
1000 auto it = mRawLightInfos.find(lightId);
1001 if (it == mRawLightInfos.end()) {
1002 return std::nullopt;
1003 }
1004 return it->second;
1005 }
1006
1007 void setLightBrightness(int32_t deviceId, int32_t lightId, int32_t brightness) override {
1008 mLightBrightness.emplace(lightId, brightness);
1009 }
1010
1011 void setLightIntensities(int32_t deviceId, int32_t lightId,
1012 std::unordered_map<LightColor, int32_t> intensities) override {
1013 mLightIntensities.emplace(lightId, intensities);
1014 };
1015
1016 std::optional<int32_t> getLightBrightness(int32_t deviceId, int32_t lightId) override {
1017 auto lightIt = mLightBrightness.find(lightId);
1018 if (lightIt == mLightBrightness.end()) {
1019 return std::nullopt;
1020 }
1021 return lightIt->second;
1022 }
1023
1024 std::optional<std::unordered_map<LightColor, int32_t>> getLightIntensities(
1025 int32_t deviceId, int32_t lightId) override {
1026 auto lightIt = mLightIntensities.find(lightId);
1027 if (lightIt == mLightIntensities.end()) {
1028 return std::nullopt;
1029 }
1030 return lightIt->second;
1031 };
1032
Narayan Kamath39efe3e2014-10-17 10:37:08 +01001033 virtual bool isExternal(int32_t) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001034 return false;
1035 }
1036
Chris Yea52ade12020-08-27 16:49:20 -07001037 void dump(std::string&) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001038
Chris Yea52ade12020-08-27 16:49:20 -07001039 void monitor() override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001040
Chris Yea52ade12020-08-27 16:49:20 -07001041 void requestReopenDevices() override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001042
Chris Yea52ade12020-08-27 16:49:20 -07001043 void wake() override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001044};
1045
Michael Wrightd02c5b62014-02-10 15:10:22 -08001046// --- FakeInputMapper ---
1047
1048class FakeInputMapper : public InputMapper {
1049 uint32_t mSources;
1050 int32_t mKeyboardType;
1051 int32_t mMetaState;
1052 KeyedVector<int32_t, int32_t> mKeyCodeStates;
1053 KeyedVector<int32_t, int32_t> mScanCodeStates;
1054 KeyedVector<int32_t, int32_t> mSwitchStates;
Philip Junker4af3b3d2021-12-14 10:36:55 +01001055 // fake mapping which would normally come from keyCharacterMap
1056 std::unordered_map<int32_t, int32_t> mKeyCodeMapping;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001057 std::vector<int32_t> mSupportedKeyCodes;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001058
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001059 std::mutex mLock;
1060 std::condition_variable mStateChangedCondition;
1061 bool mConfigureWasCalled GUARDED_BY(mLock);
1062 bool mResetWasCalled GUARDED_BY(mLock);
1063 bool mProcessWasCalled GUARDED_BY(mLock);
1064 RawEvent mLastEvent GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001065
Arthur Hungc23540e2018-11-29 20:42:11 +08001066 std::optional<DisplayViewport> mViewport;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001067public:
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001068 FakeInputMapper(InputDeviceContext& deviceContext, uint32_t sources)
1069 : InputMapper(deviceContext),
1070 mSources(sources),
1071 mKeyboardType(AINPUT_KEYBOARD_TYPE_NONE),
Michael Wrightd02c5b62014-02-10 15:10:22 -08001072 mMetaState(0),
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001073 mConfigureWasCalled(false),
1074 mResetWasCalled(false),
1075 mProcessWasCalled(false) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001076
Chris Yea52ade12020-08-27 16:49:20 -07001077 virtual ~FakeInputMapper() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001078
1079 void setKeyboardType(int32_t keyboardType) {
1080 mKeyboardType = keyboardType;
1081 }
1082
1083 void setMetaState(int32_t metaState) {
1084 mMetaState = metaState;
1085 }
1086
1087 void assertConfigureWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001088 std::unique_lock<std::mutex> lock(mLock);
1089 base::ScopedLockAssertion assumeLocked(mLock);
1090 const bool configureCalled =
1091 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1092 return mConfigureWasCalled;
1093 });
1094 if (!configureCalled) {
1095 FAIL() << "Expected configure() to have been called.";
1096 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001097 mConfigureWasCalled = false;
1098 }
1099
1100 void assertResetWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001101 std::unique_lock<std::mutex> lock(mLock);
1102 base::ScopedLockAssertion assumeLocked(mLock);
1103 const bool resetCalled =
1104 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1105 return mResetWasCalled;
1106 });
1107 if (!resetCalled) {
1108 FAIL() << "Expected reset() to have been called.";
1109 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001110 mResetWasCalled = false;
1111 }
1112
Yi Kong9b14ac62018-07-17 13:48:38 -07001113 void assertProcessWasCalled(RawEvent* outLastEvent = nullptr) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001114 std::unique_lock<std::mutex> lock(mLock);
1115 base::ScopedLockAssertion assumeLocked(mLock);
1116 const bool processCalled =
1117 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1118 return mProcessWasCalled;
1119 });
1120 if (!processCalled) {
1121 FAIL() << "Expected process() to have been called.";
1122 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001123 if (outLastEvent) {
1124 *outLastEvent = mLastEvent;
1125 }
1126 mProcessWasCalled = false;
1127 }
1128
1129 void setKeyCodeState(int32_t keyCode, int32_t state) {
1130 mKeyCodeStates.replaceValueFor(keyCode, state);
1131 }
1132
1133 void setScanCodeState(int32_t scanCode, int32_t state) {
1134 mScanCodeStates.replaceValueFor(scanCode, state);
1135 }
1136
1137 void setSwitchState(int32_t switchCode, int32_t state) {
1138 mSwitchStates.replaceValueFor(switchCode, state);
1139 }
1140
1141 void addSupportedKeyCode(int32_t keyCode) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001142 mSupportedKeyCodes.push_back(keyCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001143 }
1144
Philip Junker4af3b3d2021-12-14 10:36:55 +01001145 void addKeyCodeMapping(int32_t fromKeyCode, int32_t toKeyCode) {
1146 mKeyCodeMapping.insert_or_assign(fromKeyCode, toKeyCode);
1147 }
1148
Michael Wrightd02c5b62014-02-10 15:10:22 -08001149private:
Philip Junker4af3b3d2021-12-14 10:36:55 +01001150 uint32_t getSources() const override { return mSources; }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001151
Chris Yea52ade12020-08-27 16:49:20 -07001152 void populateDeviceInfo(InputDeviceInfo* deviceInfo) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001153 InputMapper::populateDeviceInfo(deviceInfo);
1154
1155 if (mKeyboardType != AINPUT_KEYBOARD_TYPE_NONE) {
1156 deviceInfo->setKeyboardType(mKeyboardType);
1157 }
1158 }
1159
Chris Yea52ade12020-08-27 16:49:20 -07001160 void configure(nsecs_t, const InputReaderConfiguration* config, uint32_t changes) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001161 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001162 mConfigureWasCalled = true;
Arthur Hungc23540e2018-11-29 20:42:11 +08001163
1164 // Find the associated viewport if exist.
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001165 const std::optional<uint8_t> displayPort = getDeviceContext().getAssociatedDisplayPort();
Arthur Hungc23540e2018-11-29 20:42:11 +08001166 if (displayPort && (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
1167 mViewport = config->getDisplayViewportByPort(*displayPort);
1168 }
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001169
1170 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001171 }
1172
Chris Yea52ade12020-08-27 16:49:20 -07001173 void reset(nsecs_t) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001174 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001175 mResetWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001176 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001177 }
1178
Chris Yea52ade12020-08-27 16:49:20 -07001179 void process(const RawEvent* rawEvent) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001180 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001181 mLastEvent = *rawEvent;
1182 mProcessWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001183 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001184 }
1185
Chris Yea52ade12020-08-27 16:49:20 -07001186 int32_t getKeyCodeState(uint32_t, int32_t keyCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001187 ssize_t index = mKeyCodeStates.indexOfKey(keyCode);
1188 return index >= 0 ? mKeyCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1189 }
1190
Philip Junker4af3b3d2021-12-14 10:36:55 +01001191 int32_t getKeyCodeForKeyLocation(int32_t locationKeyCode) const override {
1192 auto it = mKeyCodeMapping.find(locationKeyCode);
1193 return it != mKeyCodeMapping.end() ? it->second : locationKeyCode;
1194 }
1195
Chris Yea52ade12020-08-27 16:49:20 -07001196 int32_t getScanCodeState(uint32_t, int32_t scanCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001197 ssize_t index = mScanCodeStates.indexOfKey(scanCode);
1198 return index >= 0 ? mScanCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1199 }
1200
Chris Yea52ade12020-08-27 16:49:20 -07001201 int32_t getSwitchState(uint32_t, int32_t switchCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001202 ssize_t index = mSwitchStates.indexOfKey(switchCode);
1203 return index >= 0 ? mSwitchStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1204 }
1205
Chris Yea52ade12020-08-27 16:49:20 -07001206 // Return true if the device has non-empty key layout.
1207 bool markSupportedKeyCodes(uint32_t, size_t numCodes, const int32_t* keyCodes,
1208 uint8_t* outFlags) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001209 for (size_t i = 0; i < numCodes; i++) {
1210 for (size_t j = 0; j < mSupportedKeyCodes.size(); j++) {
1211 if (keyCodes[i] == mSupportedKeyCodes[j]) {
1212 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001213 }
1214 }
1215 }
Chris Yea52ade12020-08-27 16:49:20 -07001216 bool result = mSupportedKeyCodes.size() > 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001217 return result;
1218 }
1219
1220 virtual int32_t getMetaState() {
1221 return mMetaState;
1222 }
1223
1224 virtual void fadePointer() {
1225 }
Arthur Hungc23540e2018-11-29 20:42:11 +08001226
1227 virtual std::optional<int32_t> getAssociatedDisplay() {
1228 if (mViewport) {
1229 return std::make_optional(mViewport->displayId);
1230 }
1231 return std::nullopt;
1232 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001233};
1234
1235
1236// --- InstrumentedInputReader ---
1237
1238class InstrumentedInputReader : public InputReader {
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001239 std::queue<std::shared_ptr<InputDevice>> mNextDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001240
1241public:
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001242 InstrumentedInputReader(std::shared_ptr<EventHubInterface> eventHub,
1243 const sp<InputReaderPolicyInterface>& policy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001244 InputListenerInterface& listener)
arthurhungdcef2dc2020-08-11 14:47:50 +08001245 : InputReader(eventHub, policy, listener), mFakeContext(this) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001246
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001247 virtual ~InstrumentedInputReader() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001248
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001249 void pushNextDevice(std::shared_ptr<InputDevice> device) { mNextDevices.push(device); }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001250
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001251 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001252 const std::string& location = "") {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001253 InputDeviceIdentifier identifier;
1254 identifier.name = name;
Arthur Hungc23540e2018-11-29 20:42:11 +08001255 identifier.location = location;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001256 int32_t generation = deviceId + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08001257 return std::make_shared<InputDevice>(&mFakeContext, deviceId, generation, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001258 }
1259
Prabir Pradhan28efc192019-11-05 01:10:04 +00001260 // Make the protected loopOnce method accessible to tests.
1261 using InputReader::loopOnce;
1262
Michael Wrightd02c5b62014-02-10 15:10:22 -08001263protected:
Chris Ye1c2e0892020-11-30 21:41:44 -08001264 virtual std::shared_ptr<InputDevice> createDeviceLocked(int32_t eventHubId,
1265 const InputDeviceIdentifier& identifier)
1266 REQUIRES(mLock) {
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001267 if (!mNextDevices.empty()) {
1268 std::shared_ptr<InputDevice> device(std::move(mNextDevices.front()));
1269 mNextDevices.pop();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001270 return device;
1271 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001272 return InputReader::createDeviceLocked(eventHubId, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001273 }
1274
arthurhungdcef2dc2020-08-11 14:47:50 +08001275 // --- FakeInputReaderContext ---
1276 class FakeInputReaderContext : public ContextImpl {
1277 int32_t mGlobalMetaState;
1278 bool mUpdateGlobalMetaStateWasCalled;
1279 int32_t mGeneration;
1280
1281 public:
1282 FakeInputReaderContext(InputReader* reader)
1283 : ContextImpl(reader),
1284 mGlobalMetaState(0),
1285 mUpdateGlobalMetaStateWasCalled(false),
1286 mGeneration(1) {}
1287
1288 virtual ~FakeInputReaderContext() {}
1289
1290 void assertUpdateGlobalMetaStateWasCalled() {
1291 ASSERT_TRUE(mUpdateGlobalMetaStateWasCalled)
1292 << "Expected updateGlobalMetaState() to have been called.";
1293 mUpdateGlobalMetaStateWasCalled = false;
1294 }
1295
1296 void setGlobalMetaState(int32_t state) { mGlobalMetaState = state; }
1297
1298 uint32_t getGeneration() { return mGeneration; }
1299
1300 void updateGlobalMetaState() override {
1301 mUpdateGlobalMetaStateWasCalled = true;
1302 ContextImpl::updateGlobalMetaState();
1303 }
1304
1305 int32_t getGlobalMetaState() override {
1306 return mGlobalMetaState | ContextImpl::getGlobalMetaState();
1307 }
1308
1309 int32_t bumpGeneration() override {
1310 mGeneration = ContextImpl::bumpGeneration();
1311 return mGeneration;
1312 }
1313 } mFakeContext;
1314
Michael Wrightd02c5b62014-02-10 15:10:22 -08001315 friend class InputReaderTest;
arthurhungdcef2dc2020-08-11 14:47:50 +08001316
1317public:
1318 FakeInputReaderContext* getContext() { return &mFakeContext; }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001319};
1320
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001321// --- InputReaderPolicyTest ---
1322class InputReaderPolicyTest : public testing::Test {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001323protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001324 sp<FakeInputReaderPolicy> mFakePolicy;
1325
Chris Yea52ade12020-08-27 16:49:20 -07001326 void SetUp() override { mFakePolicy = new FakeInputReaderPolicy(); }
1327 void TearDown() override { mFakePolicy.clear(); }
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001328};
1329
1330/**
1331 * Check that empty set of viewports is an acceptable configuration.
1332 * Also try to get internal viewport two different ways - by type and by uniqueId.
1333 *
1334 * There will be confusion if two viewports with empty uniqueId and identical type are present.
1335 * Such configuration is not currently allowed.
1336 */
1337TEST_F(InputReaderPolicyTest, Viewports_GetCleared) {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001338 static const std::string uniqueId = "local:0";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001339
1340 // We didn't add any viewports yet, so there shouldn't be any.
1341 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001342 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001343 ASSERT_FALSE(internalViewport);
1344
1345 // Add an internal viewport, then clear it
1346 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001347 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId, NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001348 ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001349
1350 // Check matching by uniqueId
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001351 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001352 ASSERT_TRUE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001353 ASSERT_EQ(ViewportType::INTERNAL, internalViewport->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001354
1355 // Check matching by viewport type
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001356 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001357 ASSERT_TRUE(internalViewport);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001358 ASSERT_EQ(uniqueId, internalViewport->uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001359
1360 mFakePolicy->clearViewports();
1361 // Make sure nothing is found after clear
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001362 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001363 ASSERT_FALSE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001364 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001365 ASSERT_FALSE(internalViewport);
1366}
1367
1368TEST_F(InputReaderPolicyTest, Viewports_GetByType) {
1369 const std::string internalUniqueId = "local:0";
1370 const std::string externalUniqueId = "local:1";
1371 const std::string virtualUniqueId1 = "virtual:2";
1372 const std::string virtualUniqueId2 = "virtual:3";
1373 constexpr int32_t virtualDisplayId1 = 2;
1374 constexpr int32_t virtualDisplayId2 = 3;
1375
1376 // Add an internal viewport
1377 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001378 DISPLAY_ORIENTATION_0, true /*isActive*/, internalUniqueId,
1379 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001380 // Add an external viewport
1381 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001382 DISPLAY_ORIENTATION_0, true /*isActive*/, externalUniqueId,
1383 NO_PORT, ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001384 // Add an virtual viewport
1385 mFakePolicy->addDisplayViewport(virtualDisplayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001386 DISPLAY_ORIENTATION_0, true /*isActive*/, virtualUniqueId1,
1387 NO_PORT, ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001388 // Add another virtual viewport
1389 mFakePolicy->addDisplayViewport(virtualDisplayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001390 DISPLAY_ORIENTATION_0, true /*isActive*/, virtualUniqueId2,
1391 NO_PORT, ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001392
1393 // Check matching by type for internal
1394 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001395 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001396 ASSERT_TRUE(internalViewport);
1397 ASSERT_EQ(internalUniqueId, internalViewport->uniqueId);
1398
1399 // Check matching by type for external
1400 std::optional<DisplayViewport> externalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001401 mFakePolicy->getDisplayViewportByType(ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001402 ASSERT_TRUE(externalViewport);
1403 ASSERT_EQ(externalUniqueId, externalViewport->uniqueId);
1404
1405 // Check matching by uniqueId for virtual viewport #1
1406 std::optional<DisplayViewport> virtualViewport1 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001407 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001408 ASSERT_TRUE(virtualViewport1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001409 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport1->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001410 ASSERT_EQ(virtualUniqueId1, virtualViewport1->uniqueId);
1411 ASSERT_EQ(virtualDisplayId1, virtualViewport1->displayId);
1412
1413 // Check matching by uniqueId for virtual viewport #2
1414 std::optional<DisplayViewport> virtualViewport2 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001415 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001416 ASSERT_TRUE(virtualViewport2);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001417 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport2->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001418 ASSERT_EQ(virtualUniqueId2, virtualViewport2->uniqueId);
1419 ASSERT_EQ(virtualDisplayId2, virtualViewport2->displayId);
1420}
1421
1422
1423/**
1424 * We can have 2 viewports of the same kind. We can distinguish them by uniqueId, and confirm
1425 * that lookup works by checking display id.
1426 * Check that 2 viewports of each kind is possible, for all existing viewport types.
1427 */
1428TEST_F(InputReaderPolicyTest, Viewports_TwoOfSameType) {
1429 const std::string uniqueId1 = "uniqueId1";
1430 const std::string uniqueId2 = "uniqueId2";
1431 constexpr int32_t displayId1 = 2;
1432 constexpr int32_t displayId2 = 3;
1433
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001434 std::vector<ViewportType> types = {ViewportType::INTERNAL, ViewportType::EXTERNAL,
1435 ViewportType::VIRTUAL};
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001436 for (const ViewportType& type : types) {
1437 mFakePolicy->clearViewports();
1438 // Add a viewport
1439 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001440 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1,
1441 NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001442 // Add another viewport
1443 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001444 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2,
1445 NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001446
1447 // Check that correct display viewport was returned by comparing the display IDs.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001448 std::optional<DisplayViewport> viewport1 =
1449 mFakePolicy->getDisplayViewportByUniqueId(uniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001450 ASSERT_TRUE(viewport1);
1451 ASSERT_EQ(displayId1, viewport1->displayId);
1452 ASSERT_EQ(type, viewport1->type);
1453
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001454 std::optional<DisplayViewport> viewport2 =
1455 mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001456 ASSERT_TRUE(viewport2);
1457 ASSERT_EQ(displayId2, viewport2->displayId);
1458 ASSERT_EQ(type, viewport2->type);
1459
1460 // When there are multiple viewports of the same kind, and uniqueId is not specified
1461 // in the call to getDisplayViewport, then that situation is not supported.
1462 // The viewports can be stored in any order, so we cannot rely on the order, since that
1463 // is just implementation detail.
1464 // However, we can check that it still returns *a* viewport, we just cannot assert
1465 // which one specifically is returned.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001466 std::optional<DisplayViewport> someViewport = mFakePolicy->getDisplayViewportByType(type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001467 ASSERT_TRUE(someViewport);
1468 }
1469}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001470
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001471/**
Michael Wrightdde67b82020-10-27 16:09:22 +00001472 * When we have multiple internal displays make sure we always return the default display when
1473 * querying by type.
1474 */
1475TEST_F(InputReaderPolicyTest, Viewports_ByTypeReturnsDefaultForInternal) {
1476 const std::string uniqueId1 = "uniqueId1";
1477 const std::string uniqueId2 = "uniqueId2";
1478 constexpr int32_t nonDefaultDisplayId = 2;
1479 static_assert(nonDefaultDisplayId != ADISPLAY_ID_DEFAULT,
1480 "Test display ID should not be ADISPLAY_ID_DEFAULT");
1481
1482 // Add the default display first and ensure it gets returned.
1483 mFakePolicy->clearViewports();
1484 mFakePolicy->addDisplayViewport(ADISPLAY_ID_DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001485 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001486 ViewportType::INTERNAL);
1487 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001488 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001489 ViewportType::INTERNAL);
1490
1491 std::optional<DisplayViewport> viewport =
1492 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
1493 ASSERT_TRUE(viewport);
1494 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
1495 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
1496
1497 // Add the default display second to make sure order doesn't matter.
1498 mFakePolicy->clearViewports();
1499 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001500 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001501 ViewportType::INTERNAL);
1502 mFakePolicy->addDisplayViewport(ADISPLAY_ID_DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001503 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001504 ViewportType::INTERNAL);
1505
1506 viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
1507 ASSERT_TRUE(viewport);
1508 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
1509 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
1510}
1511
1512/**
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001513 * Check getDisplayViewportByPort
1514 */
1515TEST_F(InputReaderPolicyTest, Viewports_GetByPort) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001516 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001517 const std::string uniqueId1 = "uniqueId1";
1518 const std::string uniqueId2 = "uniqueId2";
1519 constexpr int32_t displayId1 = 1;
1520 constexpr int32_t displayId2 = 2;
1521 const uint8_t hdmi1 = 0;
1522 const uint8_t hdmi2 = 1;
1523 const uint8_t hdmi3 = 2;
1524
1525 mFakePolicy->clearViewports();
1526 // Add a viewport that's associated with some display port that's not of interest.
1527 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001528 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, hdmi3,
1529 type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001530 // Add another viewport, connected to HDMI1 port
1531 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001532 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, hdmi1,
1533 type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001534
1535 // Check that correct display viewport was returned by comparing the display ports.
1536 std::optional<DisplayViewport> hdmi1Viewport = mFakePolicy->getDisplayViewportByPort(hdmi1);
1537 ASSERT_TRUE(hdmi1Viewport);
1538 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1539 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1540
1541 // Check that we can still get the same viewport using the uniqueId
1542 hdmi1Viewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
1543 ASSERT_TRUE(hdmi1Viewport);
1544 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1545 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1546 ASSERT_EQ(type, hdmi1Viewport->type);
1547
1548 // Check that we cannot find a port with "HDMI2", because we never added one
1549 std::optional<DisplayViewport> hdmi2Viewport = mFakePolicy->getDisplayViewportByPort(hdmi2);
1550 ASSERT_FALSE(hdmi2Viewport);
1551}
1552
Michael Wrightd02c5b62014-02-10 15:10:22 -08001553// --- InputReaderTest ---
1554
1555class InputReaderTest : public testing::Test {
1556protected:
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001557 std::unique_ptr<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001558 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001559 std::shared_ptr<FakeEventHub> mFakeEventHub;
Prabir Pradhan28efc192019-11-05 01:10:04 +00001560 std::unique_ptr<InstrumentedInputReader> mReader;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001561
Chris Yea52ade12020-08-27 16:49:20 -07001562 void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001563 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001564 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001565 mFakeListener = std::make_unique<TestInputListener>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001566
Prabir Pradhan28efc192019-11-05 01:10:04 +00001567 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001568 *mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001569 }
1570
Chris Yea52ade12020-08-27 16:49:20 -07001571 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001572 mFakeListener.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001573 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001574 }
1575
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001576 void addDevice(int32_t eventHubId, const std::string& name,
1577 ftl::Flags<InputDeviceClass> classes, const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001578 mFakeEventHub->addDevice(eventHubId, name, classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001579
1580 if (configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001581 mFakeEventHub->addConfigurationMap(eventHubId, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001582 }
1583 mFakeEventHub->finishDeviceScan();
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001584 mReader->loopOnce();
1585 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001586 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1587 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001588 }
1589
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001590 void disableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001591 mFakePolicy->addDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001592 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001593 }
1594
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001595 void enableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001596 mFakePolicy->removeDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001597 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001598 }
1599
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001600 FakeInputMapper& addDeviceWithFakeInputMapper(int32_t deviceId, int32_t eventHubId,
Chris Ye1b0c7342020-07-28 21:57:03 -07001601 const std::string& name,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001602 ftl::Flags<InputDeviceClass> classes,
1603 uint32_t sources,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001604 const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001605 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, name);
1606 FakeInputMapper& mapper = device->addMapper<FakeInputMapper>(eventHubId, sources);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001607 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001608 addDevice(eventHubId, name, classes, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001609 return mapper;
1610 }
1611};
1612
Chris Ye98d3f532020-10-01 21:48:59 -07001613TEST_F(InputReaderTest, PolicyGetInputDevices) {
1614 ASSERT_NO_FATAL_FAILURE(addDevice(1, "keyboard", InputDeviceClass::KEYBOARD, nullptr));
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001615 ASSERT_NO_FATAL_FAILURE(addDevice(2, "ignored", ftl::Flags<InputDeviceClass>(0),
Chris Ye98d3f532020-10-01 21:48:59 -07001616 nullptr)); // no classes so device will be ignored
Michael Wrightd02c5b62014-02-10 15:10:22 -08001617
1618 // Should also have received a notification describing the new input devices.
Chris Ye98d3f532020-10-01 21:48:59 -07001619 const std::vector<InputDeviceInfo>& inputDevices = mFakePolicy->getInputDevices();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001620 ASSERT_EQ(1U, inputDevices.size());
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001621 ASSERT_EQ(END_RESERVED_ID + 1, inputDevices[0].getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001622 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001623 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
1624 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00001625 ASSERT_EQ(0U, inputDevices[0].getMotionRanges().size());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001626}
1627
Chris Yee7310032020-09-22 15:36:28 -07001628TEST_F(InputReaderTest, GetMergedInputDevices) {
1629 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1630 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1631 // Add two subdevices to device
1632 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1633 // Must add at least one mapper or the device will be ignored!
1634 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1635 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1636
1637 // Push same device instance for next device to be added, so they'll have same identifier.
1638 mReader->pushNextDevice(device);
1639 mReader->pushNextDevice(device);
1640 ASSERT_NO_FATAL_FAILURE(
1641 addDevice(eventHubIds[0], "fake1", InputDeviceClass::KEYBOARD, nullptr));
1642 ASSERT_NO_FATAL_FAILURE(
1643 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
1644
1645 // Two devices will be merged to one input device as they have same identifier
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00001646 ASSERT_EQ(1U, mFakePolicy->getInputDevices().size());
Chris Yee7310032020-09-22 15:36:28 -07001647}
1648
Chris Yee14523a2020-12-19 13:46:00 -08001649TEST_F(InputReaderTest, GetMergedInputDevicesEnabled) {
1650 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1651 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1652 // Add two subdevices to device
1653 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1654 // Must add at least one mapper or the device will be ignored!
1655 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1656 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1657
1658 // Push same device instance for next device to be added, so they'll have same identifier.
1659 mReader->pushNextDevice(device);
1660 mReader->pushNextDevice(device);
1661 // Sensor device is initially disabled
1662 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1",
1663 InputDeviceClass::KEYBOARD | InputDeviceClass::SENSOR,
1664 nullptr));
1665 // Device is disabled because the only sub device is a sensor device and disabled initially.
1666 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1667 ASSERT_FALSE(device->isEnabled());
1668 ASSERT_NO_FATAL_FAILURE(
1669 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
1670 // The merged device is enabled if any sub device is enabled
1671 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1672 ASSERT_TRUE(device->isEnabled());
1673}
1674
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001675TEST_F(InputReaderTest, WhenEnabledChanges_SendsDeviceResetNotification) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001676 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001677 constexpr ftl::Flags<InputDeviceClass> deviceClass(InputDeviceClass::KEYBOARD);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001678 constexpr int32_t eventHubId = 1;
1679 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001680 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001681 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001682 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001683 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001684
Yi Kong9b14ac62018-07-17 13:48:38 -07001685 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001686
1687 NotifyDeviceResetArgs resetArgs;
1688 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001689 ASSERT_EQ(deviceId, resetArgs.deviceId);
1690
1691 ASSERT_EQ(device->isEnabled(), true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001692 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001693 mReader->loopOnce();
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001694
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001695 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001696 ASSERT_EQ(deviceId, resetArgs.deviceId);
1697 ASSERT_EQ(device->isEnabled(), false);
1698
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001699 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001700 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001701 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
1702 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasNotCalled());
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001703 ASSERT_EQ(device->isEnabled(), false);
1704
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001705 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001706 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001707 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001708 ASSERT_EQ(deviceId, resetArgs.deviceId);
1709 ASSERT_EQ(device->isEnabled(), true);
1710}
1711
Michael Wrightd02c5b62014-02-10 15:10:22 -08001712TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001713 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001714 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001715 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001716 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001717 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001718 AINPUT_SOURCE_KEYBOARD, nullptr);
1719 mapper.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001720
1721 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(0,
1722 AINPUT_SOURCE_ANY, AKEYCODE_A))
1723 << "Should return unknown when the device id is >= 0 but unknown.";
1724
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001725 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1726 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1727 << "Should return unknown when the device id is valid but the sources are not "
1728 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001729
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001730 ASSERT_EQ(AKEY_STATE_DOWN,
1731 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1732 AKEYCODE_A))
1733 << "Should return value provided by mapper when device id is valid and the device "
1734 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001735
1736 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(-1,
1737 AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1738 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1739
1740 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getKeyCodeState(-1,
1741 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1742 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1743}
1744
Philip Junker4af3b3d2021-12-14 10:36:55 +01001745TEST_F(InputReaderTest, GetKeyCodeForKeyLocation_ForwardsRequestsToMappers) {
1746 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1747 constexpr int32_t eventHubId = 1;
1748 FakeInputMapper& mapper = addDeviceWithFakeInputMapper(deviceId, eventHubId, "keyboard",
1749 InputDeviceClass::KEYBOARD,
1750 AINPUT_SOURCE_KEYBOARD, nullptr);
1751 mapper.addKeyCodeMapping(AKEYCODE_Y, AKEYCODE_Z);
1752
1753 ASSERT_EQ(AKEYCODE_UNKNOWN, mReader->getKeyCodeForKeyLocation(0, AKEYCODE_Y))
1754 << "Should return unknown when the device with the specified id is not found.";
1755
1756 ASSERT_EQ(AKEYCODE_Z, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_Y))
1757 << "Should return correct mapping when device id is valid and mapping exists.";
1758
1759 ASSERT_EQ(AKEYCODE_A, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_A))
1760 << "Should return the location key code when device id is valid and there's no "
1761 "mapping.";
1762}
1763
1764TEST_F(InputReaderTest, GetKeyCodeForKeyLocation_NoKeyboardMapper) {
1765 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1766 constexpr int32_t eventHubId = 1;
1767 FakeInputMapper& mapper = addDeviceWithFakeInputMapper(deviceId, eventHubId, "joystick",
1768 InputDeviceClass::JOYSTICK,
1769 AINPUT_SOURCE_GAMEPAD, nullptr);
1770 mapper.addKeyCodeMapping(AKEYCODE_Y, AKEYCODE_Z);
1771
1772 ASSERT_EQ(AKEYCODE_UNKNOWN, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_Y))
1773 << "Should return unknown when the device id is valid but there is no keyboard mapper";
1774}
1775
Michael Wrightd02c5b62014-02-10 15:10:22 -08001776TEST_F(InputReaderTest, GetScanCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001777 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001778 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001779 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001780 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001781 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001782 AINPUT_SOURCE_KEYBOARD, nullptr);
1783 mapper.setScanCodeState(KEY_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001784
1785 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(0,
1786 AINPUT_SOURCE_ANY, KEY_A))
1787 << "Should return unknown when the device id is >= 0 but unknown.";
1788
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001789 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1790 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, KEY_A))
1791 << "Should return unknown when the device id is valid but the sources are not "
1792 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001793
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001794 ASSERT_EQ(AKEY_STATE_DOWN,
1795 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1796 KEY_A))
1797 << "Should return value provided by mapper when device id is valid and the device "
1798 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001799
1800 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(-1,
1801 AINPUT_SOURCE_TRACKBALL, KEY_A))
1802 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1803
1804 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getScanCodeState(-1,
1805 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, KEY_A))
1806 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1807}
1808
1809TEST_F(InputReaderTest, GetSwitchState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001810 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001811 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001812 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001813 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001814 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001815 AINPUT_SOURCE_KEYBOARD, nullptr);
1816 mapper.setSwitchState(SW_LID, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001817
1818 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(0,
1819 AINPUT_SOURCE_ANY, SW_LID))
1820 << "Should return unknown when the device id is >= 0 but unknown.";
1821
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001822 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1823 mReader->getSwitchState(deviceId, AINPUT_SOURCE_TRACKBALL, SW_LID))
1824 << "Should return unknown when the device id is valid but the sources are not "
1825 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001826
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001827 ASSERT_EQ(AKEY_STATE_DOWN,
1828 mReader->getSwitchState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1829 SW_LID))
1830 << "Should return value provided by mapper when device id is valid and the device "
1831 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001832
1833 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(-1,
1834 AINPUT_SOURCE_TRACKBALL, SW_LID))
1835 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1836
1837 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getSwitchState(-1,
1838 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, SW_LID))
1839 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1840}
1841
1842TEST_F(InputReaderTest, MarkSupportedKeyCodes_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001843 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001844 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001845 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001846 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001847 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001848 AINPUT_SOURCE_KEYBOARD, nullptr);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001849
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001850 mapper.addSupportedKeyCode(AKEYCODE_A);
1851 mapper.addSupportedKeyCode(AKEYCODE_B);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001852
1853 const int32_t keyCodes[4] = { AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2 };
1854 uint8_t flags[4] = { 0, 0, 0, 1 };
1855
1856 ASSERT_FALSE(mReader->hasKeys(0, AINPUT_SOURCE_ANY, 4, keyCodes, flags))
1857 << "Should return false when device id is >= 0 but unknown.";
1858 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1859
1860 flags[3] = 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001861 ASSERT_FALSE(mReader->hasKeys(deviceId, AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1862 << "Should return false when device id is valid but the sources are not supported by "
1863 "the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001864 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1865
1866 flags[3] = 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001867 ASSERT_TRUE(mReader->hasKeys(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, 4,
1868 keyCodes, flags))
1869 << "Should return value provided by mapper when device id is valid and the device "
1870 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001871 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1872
1873 flags[3] = 1;
1874 ASSERT_FALSE(mReader->hasKeys(-1, AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1875 << "Should return false when the device id is < 0 but the sources are not supported by any device.";
1876 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1877
1878 flags[3] = 1;
1879 ASSERT_TRUE(mReader->hasKeys(-1, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1880 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1881 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1882}
1883
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001884TEST_F(InputReaderTest, LoopOnce_WhenDeviceScanFinished_SendsConfigurationChanged) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001885 constexpr int32_t eventHubId = 1;
Chris Ye1b0c7342020-07-28 21:57:03 -07001886 addDevice(eventHubId, "ignored", InputDeviceClass::KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001887
1888 NotifyConfigurationChangedArgs args;
1889
1890 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(&args));
1891 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
1892}
1893
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001894TEST_F(InputReaderTest, LoopOnce_ForwardsRawEventsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001895 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001896 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001897 constexpr nsecs_t when = 0;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001898 constexpr int32_t eventHubId = 1;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001899 constexpr nsecs_t readTime = 2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001900 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001901 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001902 AINPUT_SOURCE_KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001903
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001904 mFakeEventHub->enqueueEvent(when, readTime, eventHubId, EV_KEY, KEY_A, 1);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001905 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001906 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
1907
1908 RawEvent event;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001909 ASSERT_NO_FATAL_FAILURE(mapper.assertProcessWasCalled(&event));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001910 ASSERT_EQ(when, event.when);
1911 ASSERT_EQ(readTime, event.readTime);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001912 ASSERT_EQ(eventHubId, event.deviceId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001913 ASSERT_EQ(EV_KEY, event.type);
1914 ASSERT_EQ(KEY_A, event.code);
1915 ASSERT_EQ(1, event.value);
1916}
1917
Garfield Tan1c7bc862020-01-28 13:24:04 -08001918TEST_F(InputReaderTest, DeviceReset_RandomId) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001919 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001920 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001921 constexpr int32_t eventHubId = 1;
1922 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Prabir Pradhan42611e02018-11-27 14:04:02 -08001923 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001924 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001925 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001926 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan42611e02018-11-27 14:04:02 -08001927
1928 NotifyDeviceResetArgs resetArgs;
1929 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001930 int32_t prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001931
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001932 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001933 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001934 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001935 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001936 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001937
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001938 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001939 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001940 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001941 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001942 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001943
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001944 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001945 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001946 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001947 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001948 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001949}
1950
Garfield Tan1c7bc862020-01-28 13:24:04 -08001951TEST_F(InputReaderTest, DeviceReset_GenerateIdWithInputReaderSource) {
1952 constexpr int32_t deviceId = 1;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001953 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Garfield Tan1c7bc862020-01-28 13:24:04 -08001954 constexpr int32_t eventHubId = 1;
1955 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1956 // Must add at least one mapper or the device will be ignored!
1957 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001958 mReader->pushNextDevice(device);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001959 ASSERT_NO_FATAL_FAILURE(addDevice(deviceId, "fake", deviceClass, nullptr));
1960
1961 NotifyDeviceResetArgs resetArgs;
1962 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1963 ASSERT_EQ(IdGenerator::Source::INPUT_READER, IdGenerator::getSource(resetArgs.id));
1964}
1965
Arthur Hungc23540e2018-11-29 20:42:11 +08001966TEST_F(InputReaderTest, Device_CanDispatchToDisplay) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001967 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001968 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001969 constexpr int32_t eventHubId = 1;
Arthur Hungc23540e2018-11-29 20:42:11 +08001970 const char* DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001971 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1972 FakeInputMapper& mapper =
1973 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001974 mReader->pushNextDevice(device);
Arthur Hungc23540e2018-11-29 20:42:11 +08001975
1976 const uint8_t hdmi1 = 1;
1977
1978 // Associated touch screen with second display.
1979 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
1980
1981 // Add default and second display.
Prabir Pradhan28efc192019-11-05 01:10:04 +00001982 mFakePolicy->clearViewports();
Arthur Hungc23540e2018-11-29 20:42:11 +08001983 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001984 DISPLAY_ORIENTATION_0, true /*isActive*/, "local:0", NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001985 ViewportType::INTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08001986 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001987 DISPLAY_ORIENTATION_0, true /*isActive*/, "local:1", hdmi1,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001988 ViewportType::EXTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08001989 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001990 mReader->loopOnce();
Prabir Pradhan28efc192019-11-05 01:10:04 +00001991
1992 // Add the device, and make sure all of the callbacks are triggered.
1993 // The device is added after the input port associations are processed since
1994 // we do not yet support dynamic device-to-display associations.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001995 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001996 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled());
Prabir Pradhan28efc192019-11-05 01:10:04 +00001997 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001998 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
Arthur Hungc23540e2018-11-29 20:42:11 +08001999
Arthur Hung2c9a3342019-07-23 14:18:59 +08002000 // Device should only dispatch to the specified display.
Arthur Hungc23540e2018-11-29 20:42:11 +08002001 ASSERT_EQ(deviceId, device->getId());
2002 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, DISPLAY_ID));
2003 ASSERT_TRUE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hung2c9a3342019-07-23 14:18:59 +08002004
2005 // Can't dispatch event from a disabled device.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002006 disableDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00002007 mReader->loopOnce();
Arthur Hung2c9a3342019-07-23 14:18:59 +08002008 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hungc23540e2018-11-29 20:42:11 +08002009}
2010
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08002011TEST_F(InputReaderTest, WhenEnabledChanges_AllSubdevicesAreUpdated) {
2012 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002013 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08002014 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
2015 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
2016 // Must add at least one mapper or the device will be ignored!
2017 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
2018 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
2019 mReader->pushNextDevice(device);
2020 mReader->pushNextDevice(device);
2021 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
2022 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
2023
2024 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
2025
2026 NotifyDeviceResetArgs resetArgs;
2027 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2028 ASSERT_EQ(deviceId, resetArgs.deviceId);
2029 ASSERT_TRUE(device->isEnabled());
2030 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
2031 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
2032
2033 disableDevice(deviceId);
2034 mReader->loopOnce();
2035
2036 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2037 ASSERT_EQ(deviceId, resetArgs.deviceId);
2038 ASSERT_FALSE(device->isEnabled());
2039 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
2040 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
2041
2042 enableDevice(deviceId);
2043 mReader->loopOnce();
2044
2045 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2046 ASSERT_EQ(deviceId, resetArgs.deviceId);
2047 ASSERT_TRUE(device->isEnabled());
2048 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
2049 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
2050}
2051
2052TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToSubdeviceMappers) {
2053 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002054 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08002055 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
2056 // Add two subdevices to device
2057 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
2058 FakeInputMapper& mapperDevice1 =
2059 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
2060 FakeInputMapper& mapperDevice2 =
2061 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
2062 mReader->pushNextDevice(device);
2063 mReader->pushNextDevice(device);
2064 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
2065 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
2066
2067 mapperDevice1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
2068 mapperDevice2.setKeyCodeState(AKEYCODE_B, AKEY_STATE_DOWN);
2069
2070 ASSERT_EQ(AKEY_STATE_DOWN,
2071 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_A));
2072 ASSERT_EQ(AKEY_STATE_DOWN,
2073 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_B));
2074 ASSERT_EQ(AKEY_STATE_UNKNOWN,
2075 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_C));
2076}
2077
Prabir Pradhan7e186182020-11-10 13:56:45 -08002078TEST_F(InputReaderTest, ChangingPointerCaptureNotifiesInputListener) {
2079 NotifyPointerCaptureChangedArgs args;
2080
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002081 auto request = mFakePolicy->setPointerCapture(true);
Prabir Pradhan7e186182020-11-10 13:56:45 -08002082 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
2083 mReader->loopOnce();
2084 mFakeListener->assertNotifyCaptureWasCalled(&args);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002085 ASSERT_TRUE(args.request.enable) << "Pointer Capture should be enabled.";
2086 ASSERT_EQ(args.request, request) << "Pointer Capture sequence number should match.";
Prabir Pradhan7e186182020-11-10 13:56:45 -08002087
2088 mFakePolicy->setPointerCapture(false);
2089 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
2090 mReader->loopOnce();
2091 mFakeListener->assertNotifyCaptureWasCalled(&args);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002092 ASSERT_FALSE(args.request.enable) << "Pointer Capture should be disabled.";
Prabir Pradhan7e186182020-11-10 13:56:45 -08002093
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002094 // Verify that the Pointer Capture state is not updated when the configuration value
Prabir Pradhan7e186182020-11-10 13:56:45 -08002095 // does not change.
2096 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
2097 mReader->loopOnce();
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002098 mFakeListener->assertNotifyCaptureWasNotCalled();
Prabir Pradhan7e186182020-11-10 13:56:45 -08002099}
2100
Chris Ye87143712020-11-10 05:05:58 +00002101class FakeVibratorInputMapper : public FakeInputMapper {
2102public:
2103 FakeVibratorInputMapper(InputDeviceContext& deviceContext, uint32_t sources)
2104 : FakeInputMapper(deviceContext, sources) {}
2105
2106 std::vector<int32_t> getVibratorIds() override { return getDeviceContext().getVibratorIds(); }
2107};
2108
2109TEST_F(InputReaderTest, VibratorGetVibratorIds) {
2110 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002111 ftl::Flags<InputDeviceClass> deviceClass =
2112 InputDeviceClass::KEYBOARD | InputDeviceClass::VIBRATOR;
Chris Ye87143712020-11-10 05:05:58 +00002113 constexpr int32_t eventHubId = 1;
2114 const char* DEVICE_LOCATION = "BLUETOOTH";
2115 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
2116 FakeVibratorInputMapper& mapper =
2117 device->addMapper<FakeVibratorInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
2118 mReader->pushNextDevice(device);
2119
2120 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2121 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
2122
2123 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
2124 ASSERT_EQ(mReader->getVibratorIds(deviceId).size(), 2U);
2125}
2126
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002127// --- FakePeripheralController ---
Kim Low03ea0352020-11-06 12:45:07 -08002128
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002129class FakePeripheralController : public PeripheralControllerInterface {
Chris Yee2b1e5c2021-03-10 22:45:12 -08002130public:
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002131 FakePeripheralController(InputDeviceContext& deviceContext) : mDeviceContext(deviceContext) {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08002132
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002133 ~FakePeripheralController() override {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08002134
2135 void populateDeviceInfo(InputDeviceInfo* deviceInfo) override {}
2136
2137 void dump(std::string& dump) override {}
2138
2139 std::optional<int32_t> getBatteryCapacity(int32_t batteryId) override {
2140 return getDeviceContext().getBatteryCapacity(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08002141 }
2142
Chris Yee2b1e5c2021-03-10 22:45:12 -08002143 std::optional<int32_t> getBatteryStatus(int32_t batteryId) override {
2144 return getDeviceContext().getBatteryStatus(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08002145 }
Chris Ye3fdbfef2021-01-06 18:45:18 -08002146
2147 bool setLightColor(int32_t lightId, int32_t color) override {
2148 getDeviceContext().setLightBrightness(lightId, color >> 24);
2149 return true;
2150 }
2151
2152 std::optional<int32_t> getLightColor(int32_t lightId) override {
2153 std::optional<int32_t> result = getDeviceContext().getLightBrightness(lightId);
2154 if (!result.has_value()) {
2155 return std::nullopt;
2156 }
2157 return result.value() << 24;
2158 }
Chris Yee2b1e5c2021-03-10 22:45:12 -08002159
2160 bool setLightPlayerId(int32_t lightId, int32_t playerId) override { return true; }
2161
2162 std::optional<int32_t> getLightPlayerId(int32_t lightId) override { return std::nullopt; }
2163
2164private:
2165 InputDeviceContext& mDeviceContext;
2166 inline int32_t getDeviceId() { return mDeviceContext.getId(); }
2167 inline InputDeviceContext& getDeviceContext() { return mDeviceContext; }
Chris Ye3fdbfef2021-01-06 18:45:18 -08002168};
2169
Chris Yee2b1e5c2021-03-10 22:45:12 -08002170TEST_F(InputReaderTest, BatteryGetCapacity) {
2171 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002172 ftl::Flags<InputDeviceClass> deviceClass =
2173 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
Chris Yee2b1e5c2021-03-10 22:45:12 -08002174 constexpr int32_t eventHubId = 1;
2175 const char* DEVICE_LOCATION = "BLUETOOTH";
2176 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002177 FakePeripheralController& controller =
2178 device->addController<FakePeripheralController>(eventHubId);
Chris Yee2b1e5c2021-03-10 22:45:12 -08002179 mReader->pushNextDevice(device);
2180
2181 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2182
2183 ASSERT_EQ(controller.getBatteryCapacity(DEFAULT_BATTERY), BATTERY_CAPACITY);
2184 ASSERT_EQ(mReader->getBatteryCapacity(deviceId), BATTERY_CAPACITY);
2185}
2186
2187TEST_F(InputReaderTest, BatteryGetStatus) {
2188 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002189 ftl::Flags<InputDeviceClass> deviceClass =
2190 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
Chris Yee2b1e5c2021-03-10 22:45:12 -08002191 constexpr int32_t eventHubId = 1;
2192 const char* DEVICE_LOCATION = "BLUETOOTH";
2193 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002194 FakePeripheralController& controller =
2195 device->addController<FakePeripheralController>(eventHubId);
Chris Yee2b1e5c2021-03-10 22:45:12 -08002196 mReader->pushNextDevice(device);
2197
2198 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2199
2200 ASSERT_EQ(controller.getBatteryStatus(DEFAULT_BATTERY), BATTERY_STATUS);
2201 ASSERT_EQ(mReader->getBatteryStatus(deviceId), BATTERY_STATUS);
2202}
2203
Chris Ye3fdbfef2021-01-06 18:45:18 -08002204TEST_F(InputReaderTest, LightGetColor) {
2205 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002206 ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD | InputDeviceClass::LIGHT;
Chris Ye3fdbfef2021-01-06 18:45:18 -08002207 constexpr int32_t eventHubId = 1;
2208 const char* DEVICE_LOCATION = "BLUETOOTH";
2209 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002210 FakePeripheralController& controller =
2211 device->addController<FakePeripheralController>(eventHubId);
Chris Ye3fdbfef2021-01-06 18:45:18 -08002212 mReader->pushNextDevice(device);
2213 RawLightInfo info = {.id = 1,
2214 .name = "Mono",
2215 .maxBrightness = 255,
2216 .flags = InputLightClass::BRIGHTNESS,
2217 .path = ""};
2218 mFakeEventHub->addRawLightInfo(1 /* rawId */, std::move(info));
2219 mFakeEventHub->fakeLightBrightness(1 /* rawId */, 0x55);
2220
2221 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Chris Ye3fdbfef2021-01-06 18:45:18 -08002222
Chris Yee2b1e5c2021-03-10 22:45:12 -08002223 ASSERT_TRUE(controller.setLightColor(1 /* lightId */, LIGHT_BRIGHTNESS));
2224 ASSERT_EQ(controller.getLightColor(1 /* lightId */), LIGHT_BRIGHTNESS);
Chris Ye3fdbfef2021-01-06 18:45:18 -08002225 ASSERT_TRUE(mReader->setLightColor(deviceId, 1 /* lightId */, LIGHT_BRIGHTNESS));
2226 ASSERT_EQ(mReader->getLightColor(deviceId, 1 /* lightId */), LIGHT_BRIGHTNESS);
2227}
2228
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002229// --- InputReaderIntegrationTest ---
2230
2231// These tests create and interact with the InputReader only through its interface.
2232// The InputReader is started during SetUp(), which starts its processing in its own
2233// thread. The tests use linux uinput to emulate input devices.
2234// NOTE: Interacting with the physical device while these tests are running may cause
2235// the tests to fail.
2236class InputReaderIntegrationTest : public testing::Test {
2237protected:
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002238 std::unique_ptr<TestInputListener> mTestListener;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002239 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002240 std::unique_ptr<InputReaderInterface> mReader;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002241
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00002242 std::shared_ptr<FakePointerController> mFakePointerController;
2243
Chris Yea52ade12020-08-27 16:49:20 -07002244 void SetUp() override {
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002245 mFakePolicy = new FakeInputReaderPolicy();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00002246 mFakePointerController = std::make_shared<FakePointerController>();
2247 mFakePolicy->setPointerController(mFakePointerController);
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002248 mTestListener = std::make_unique<TestInputListener>(2000ms /*eventHappenedTimeout*/,
2249 30ms /*eventDidNotHappenTimeout*/);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002250
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002251 mReader = std::make_unique<InputReader>(std::make_shared<EventHub>(), mFakePolicy,
2252 *mTestListener);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002253 ASSERT_EQ(mReader->start(), OK);
2254
2255 // Since this test is run on a real device, all the input devices connected
2256 // to the test device will show up in mReader. We wait for those input devices to
2257 // show up before beginning the tests.
2258 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2259 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2260 }
2261
Chris Yea52ade12020-08-27 16:49:20 -07002262 void TearDown() override {
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002263 ASSERT_EQ(mReader->stop(), OK);
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002264 mReader.reset();
2265 mTestListener.reset();
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002266 mFakePolicy.clear();
2267 }
2268};
2269
2270TEST_F(InputReaderIntegrationTest, TestInvalidDevice) {
2271 // An invalid input device that is only used for this test.
2272 class InvalidUinputDevice : public UinputDevice {
2273 public:
2274 InvalidUinputDevice() : UinputDevice("Invalid Device") {}
2275
2276 private:
2277 void configureDevice(int fd, uinput_user_dev* device) override {}
2278 };
2279
2280 const size_t numDevices = mFakePolicy->getInputDevices().size();
2281
2282 // UinputDevice does not set any event or key bits, so InputReader should not
2283 // consider it as a valid device.
2284 std::unique_ptr<UinputDevice> invalidDevice = createUinputDevice<InvalidUinputDevice>();
2285 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
2286 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
2287 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
2288
2289 invalidDevice.reset();
2290 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
2291 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
2292 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
2293}
2294
2295TEST_F(InputReaderIntegrationTest, AddNewDevice) {
2296 const size_t initialNumDevices = mFakePolicy->getInputDevices().size();
2297
2298 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
2299 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2300 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2301 ASSERT_EQ(initialNumDevices + 1, mFakePolicy->getInputDevices().size());
2302
2303 // Find the test device by its name.
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002304 const std::vector<InputDeviceInfo> inputDevices = mFakePolicy->getInputDevices();
Chris Ye98d3f532020-10-01 21:48:59 -07002305 const auto& it =
2306 std::find_if(inputDevices.begin(), inputDevices.end(),
2307 [&keyboard](const InputDeviceInfo& info) {
2308 return info.getIdentifier().name == keyboard->getName();
2309 });
2310
2311 ASSERT_NE(it, inputDevices.end());
2312 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, it->getKeyboardType());
2313 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, it->getSources());
2314 ASSERT_EQ(0U, it->getMotionRanges().size());
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002315
2316 keyboard.reset();
2317 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2318 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2319 ASSERT_EQ(initialNumDevices, mFakePolicy->getInputDevices().size());
2320}
2321
2322TEST_F(InputReaderIntegrationTest, SendsEventsToInputListener) {
2323 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
2324 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2325
2326 NotifyConfigurationChangedArgs configChangedArgs;
2327 ASSERT_NO_FATAL_FAILURE(
2328 mTestListener->assertNotifyConfigurationChangedWasCalled(&configChangedArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002329 int32_t prevId = configChangedArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002330 nsecs_t prevTimestamp = configChangedArgs.eventTime;
2331
2332 NotifyKeyArgs keyArgs;
2333 keyboard->pressAndReleaseHomeKey();
2334 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
2335 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002336 ASSERT_NE(prevId, keyArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002337 prevId = keyArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002338 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002339 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002340 prevTimestamp = keyArgs.eventTime;
2341
2342 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
2343 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002344 ASSERT_NE(prevId, keyArgs.id);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002345 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002346 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002347}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002348
Siarhei Vishniakoua0d2b802020-05-13 14:00:31 -07002349/**
2350 * The Steam controller sends BTN_GEAR_DOWN and BTN_GEAR_UP for the two "paddle" buttons
2351 * on the back. In this test, we make sure that BTN_GEAR_DOWN / BTN_WHEEL and BTN_GEAR_UP
2352 * are passed to the listener.
2353 */
2354static_assert(BTN_GEAR_DOWN == BTN_WHEEL);
2355TEST_F(InputReaderIntegrationTest, SendsGearDownAndUpToInputListener) {
2356 std::unique_ptr<UinputSteamController> controller = createUinputDevice<UinputSteamController>();
2357 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2358 NotifyKeyArgs keyArgs;
2359
2360 controller->pressAndReleaseKey(BTN_GEAR_DOWN);
2361 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
2362 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
2363 ASSERT_EQ(BTN_GEAR_DOWN, keyArgs.scanCode);
2364
2365 controller->pressAndReleaseKey(BTN_GEAR_UP);
2366 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
2367 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
2368 ASSERT_EQ(BTN_GEAR_UP, keyArgs.scanCode);
2369}
2370
Arthur Hungaab25622020-01-16 11:22:11 +08002371// --- TouchProcessTest ---
2372class TouchIntegrationTest : public InputReaderIntegrationTest {
2373protected:
Arthur Hungaab25622020-01-16 11:22:11 +08002374 const std::string UNIQUE_ID = "local:0";
2375
Chris Yea52ade12020-08-27 16:49:20 -07002376 void SetUp() override {
Arthur Hungaab25622020-01-16 11:22:11 +08002377 InputReaderIntegrationTest::SetUp();
2378 // At least add an internal display.
2379 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2380 DISPLAY_ORIENTATION_0, UNIQUE_ID, NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002381 ViewportType::INTERNAL);
Arthur Hungaab25622020-01-16 11:22:11 +08002382
2383 mDevice = createUinputDevice<UinputTouchScreen>(Rect(0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT));
2384 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2385 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2386 }
2387
2388 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
2389 int32_t orientation, const std::string& uniqueId,
2390 std::optional<uint8_t> physicalPort,
2391 ViewportType viewportType) {
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002392 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, true /*isActive*/,
2393 uniqueId, physicalPort, viewportType);
Arthur Hungaab25622020-01-16 11:22:11 +08002394 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2395 }
2396
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002397 void assertReceivedMotion(int32_t action, const std::vector<Point>& points) {
2398 NotifyMotionArgs args;
2399 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2400 EXPECT_EQ(action, args.action);
2401 ASSERT_EQ(points.size(), args.pointerCount);
2402 for (size_t i = 0; i < args.pointerCount; i++) {
2403 EXPECT_EQ(points[i].x, args.pointerCoords[i].getX());
2404 EXPECT_EQ(points[i].y, args.pointerCoords[i].getY());
2405 }
2406 }
2407
Arthur Hungaab25622020-01-16 11:22:11 +08002408 std::unique_ptr<UinputTouchScreen> mDevice;
2409};
2410
2411TEST_F(TouchIntegrationTest, InputEvent_ProcessSingleTouch) {
2412 NotifyMotionArgs args;
2413 const Point centerPoint = mDevice->getCenterPoint();
2414
2415 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00002416 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002417 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002418 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002419 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2420 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2421
2422 // ACTION_MOVE
2423 mDevice->sendMove(centerPoint + Point(1, 1));
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002424 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002425 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2426 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2427
2428 // ACTION_UP
2429 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002430 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002431 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2432 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2433}
2434
2435TEST_F(TouchIntegrationTest, InputEvent_ProcessMultiTouch) {
2436 NotifyMotionArgs args;
2437 const Point centerPoint = mDevice->getCenterPoint();
2438
2439 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00002440 mDevice->sendSlot(FIRST_SLOT);
2441 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002442 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002443 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002444 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2445 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2446
2447 // ACTION_POINTER_DOWN (Second slot)
2448 const Point secondPoint = centerPoint + Point(100, 100);
2449 mDevice->sendSlot(SECOND_SLOT);
2450 mDevice->sendTrackingId(SECOND_TRACKING_ID);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002451 mDevice->sendDown(secondPoint);
2452 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002453 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08002454 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002455
2456 // ACTION_MOVE (Second slot)
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002457 mDevice->sendMove(secondPoint + Point(1, 1));
2458 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002459 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2460 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2461
2462 // ACTION_POINTER_UP (Second slot)
arthurhungcc7f9802020-04-30 17:55:40 +08002463 mDevice->sendPointerUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002464 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002465 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08002466 ASSERT_EQ(ACTION_POINTER_1_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002467
2468 // ACTION_UP
2469 mDevice->sendSlot(FIRST_SLOT);
2470 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002471 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002472 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2473 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2474}
2475
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002476/**
2477 * What happens when a pointer goes up while another pointer moves in the same frame? Are POINTER_UP
2478 * events guaranteed to contain the same data as a preceding MOVE, or can they contain different
2479 * data?
2480 * In this test, we try to send a change in coordinates in Pointer 0 in the same frame as the
2481 * liftoff of Pointer 1. We check that POINTER_UP event is generated first, and the MOVE event
2482 * for Pointer 0 only is generated after.
2483 * Suppose we are only interested in learning the movement of Pointer 0. If we only observe MOVE
2484 * events, we will not miss any information.
2485 * Even though the Pointer 1 up event contains updated Pointer 0 coordinates, there is another MOVE
2486 * event generated afterwards that contains the newest movement of pointer 0.
2487 * This is important for palm rejection. If there is a subsequent InputListener stage that detects
2488 * palms, and wants to cancel Pointer 1, then it is safe to simply drop POINTER_1_UP event without
2489 * losing information about non-palm pointers.
2490 */
2491TEST_F(TouchIntegrationTest, MultiTouch_PointerMoveAndSecondPointerUp) {
2492 NotifyMotionArgs args;
2493 const Point centerPoint = mDevice->getCenterPoint();
2494
2495 // ACTION_DOWN
2496 mDevice->sendSlot(FIRST_SLOT);
2497 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2498 mDevice->sendDown(centerPoint);
2499 mDevice->sendSync();
2500 assertReceivedMotion(AMOTION_EVENT_ACTION_DOWN, {centerPoint});
2501
2502 // ACTION_POINTER_DOWN (Second slot)
2503 const Point secondPoint = centerPoint + Point(100, 100);
2504 mDevice->sendSlot(SECOND_SLOT);
2505 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2506 mDevice->sendDown(secondPoint);
2507 mDevice->sendSync();
2508 assertReceivedMotion(ACTION_POINTER_1_DOWN, {centerPoint, secondPoint});
2509
2510 // ACTION_MOVE (First slot)
2511 mDevice->sendSlot(FIRST_SLOT);
2512 mDevice->sendMove(centerPoint + Point(5, 5));
2513 // ACTION_POINTER_UP (Second slot)
2514 mDevice->sendSlot(SECOND_SLOT);
2515 mDevice->sendPointerUp();
2516 // Send a single sync for the above 2 pointer updates
2517 mDevice->sendSync();
2518
2519 // First, we should get POINTER_UP for the second pointer
2520 assertReceivedMotion(ACTION_POINTER_1_UP,
2521 {/*first pointer */ centerPoint + Point(5, 5),
2522 /*second pointer*/ secondPoint});
2523
2524 // Next, the MOVE event for the first pointer
2525 assertReceivedMotion(AMOTION_EVENT_ACTION_MOVE, {centerPoint + Point(5, 5)});
2526}
2527
2528/**
2529 * Similar scenario as above. The difference is that when the second pointer goes up, it will first
2530 * move, and then it will go up, all in the same frame.
2531 * In this scenario, the movement of the second pointer just prior to liftoff is ignored, and never
2532 * gets sent to the listener.
2533 */
2534TEST_F(TouchIntegrationTest, MultiTouch_PointerMoveAndSecondPointerMoveAndUp) {
2535 NotifyMotionArgs args;
2536 const Point centerPoint = mDevice->getCenterPoint();
2537
2538 // ACTION_DOWN
2539 mDevice->sendSlot(FIRST_SLOT);
2540 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2541 mDevice->sendDown(centerPoint);
2542 mDevice->sendSync();
2543 assertReceivedMotion(AMOTION_EVENT_ACTION_DOWN, {centerPoint});
2544
2545 // ACTION_POINTER_DOWN (Second slot)
2546 const Point secondPoint = centerPoint + Point(100, 100);
2547 mDevice->sendSlot(SECOND_SLOT);
2548 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2549 mDevice->sendDown(secondPoint);
2550 mDevice->sendSync();
2551 assertReceivedMotion(ACTION_POINTER_1_DOWN, {centerPoint, secondPoint});
2552
2553 // ACTION_MOVE (First slot)
2554 mDevice->sendSlot(FIRST_SLOT);
2555 mDevice->sendMove(centerPoint + Point(5, 5));
2556 // ACTION_POINTER_UP (Second slot)
2557 mDevice->sendSlot(SECOND_SLOT);
2558 mDevice->sendMove(secondPoint + Point(6, 6));
2559 mDevice->sendPointerUp();
2560 // Send a single sync for the above 2 pointer updates
2561 mDevice->sendSync();
2562
2563 // First, we should get POINTER_UP for the second pointer
2564 // The movement of the second pointer during the liftoff frame is ignored.
2565 // The coordinates 'secondPoint + Point(6, 6)' are never sent to the listener.
2566 assertReceivedMotion(ACTION_POINTER_1_UP,
2567 {/*first pointer */ centerPoint + Point(5, 5),
2568 /*second pointer*/ secondPoint});
2569
2570 // Next, the MOVE event for the first pointer
2571 assertReceivedMotion(AMOTION_EVENT_ACTION_MOVE, {centerPoint + Point(5, 5)});
2572}
2573
Arthur Hungaab25622020-01-16 11:22:11 +08002574TEST_F(TouchIntegrationTest, InputEvent_ProcessPalm) {
2575 NotifyMotionArgs args;
2576 const Point centerPoint = mDevice->getCenterPoint();
2577
2578 // ACTION_DOWN
arthurhungcc7f9802020-04-30 17:55:40 +08002579 mDevice->sendSlot(FIRST_SLOT);
2580 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002581 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002582 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002583 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2584 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2585
arthurhungcc7f9802020-04-30 17:55:40 +08002586 // ACTION_POINTER_DOWN (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002587 const Point secondPoint = centerPoint + Point(100, 100);
2588 mDevice->sendSlot(SECOND_SLOT);
2589 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2590 mDevice->sendDown(secondPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002591 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002592 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08002593 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002594
arthurhungcc7f9802020-04-30 17:55:40 +08002595 // ACTION_MOVE (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002596 mDevice->sendMove(secondPoint + Point(1, 1));
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002597 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002598 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2599 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2600
arthurhungcc7f9802020-04-30 17:55:40 +08002601 // Send MT_TOOL_PALM (second slot), which indicates that the touch IC has determined this to be
2602 // a palm event.
2603 // Expect to receive the ACTION_POINTER_UP with cancel flag.
Arthur Hungaab25622020-01-16 11:22:11 +08002604 mDevice->sendToolType(MT_TOOL_PALM);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002605 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002606 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08002607 ASSERT_EQ(ACTION_POINTER_1_UP, args.action);
arthurhungcc7f9802020-04-30 17:55:40 +08002608 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, args.flags);
Arthur Hungaab25622020-01-16 11:22:11 +08002609
arthurhungcc7f9802020-04-30 17:55:40 +08002610 // Send up to second slot, expect first slot send moving.
2611 mDevice->sendPointerUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002612 mDevice->sendSync();
arthurhungcc7f9802020-04-30 17:55:40 +08002613 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2614 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002615
arthurhungcc7f9802020-04-30 17:55:40 +08002616 // Send ACTION_UP (first slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002617 mDevice->sendSlot(FIRST_SLOT);
2618 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002619 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002620
arthurhungcc7f9802020-04-30 17:55:40 +08002621 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2622 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002623}
2624
Michael Wrightd02c5b62014-02-10 15:10:22 -08002625// --- InputDeviceTest ---
Michael Wrightd02c5b62014-02-10 15:10:22 -08002626class InputDeviceTest : public testing::Test {
2627protected:
2628 static const char* DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002629 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002630 static const int32_t DEVICE_ID;
2631 static const int32_t DEVICE_GENERATION;
2632 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002633 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002634 static const int32_t EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002635
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002636 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002637 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002638 std::unique_ptr<TestInputListener> mFakeListener;
arthurhungdcef2dc2020-08-11 14:47:50 +08002639 std::unique_ptr<InstrumentedInputReader> mReader;
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00002640 std::shared_ptr<InputDevice> mDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002641
Chris Yea52ade12020-08-27 16:49:20 -07002642 void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002643 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002644 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002645 mFakeListener = std::make_unique<TestInputListener>();
arthurhungdcef2dc2020-08-11 14:47:50 +08002646 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002647 *mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002648 InputDeviceIdentifier identifier;
2649 identifier.name = DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002650 identifier.location = DEVICE_LOCATION;
arthurhungdcef2dc2020-08-11 14:47:50 +08002651 mDevice = std::make_shared<InputDevice>(mReader->getContext(), DEVICE_ID, DEVICE_GENERATION,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002652 identifier);
arthurhungdcef2dc2020-08-11 14:47:50 +08002653 mReader->pushNextDevice(mDevice);
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002654 mFakeEventHub->addDevice(EVENTHUB_ID, DEVICE_NAME, ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08002655 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002656 }
2657
Chris Yea52ade12020-08-27 16:49:20 -07002658 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002659 mFakeListener.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002660 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002661 }
2662};
2663
2664const char* InputDeviceTest::DEVICE_NAME = "device";
Arthur Hung2c9a3342019-07-23 14:18:59 +08002665const char* InputDeviceTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002666const int32_t InputDeviceTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002667const int32_t InputDeviceTest::DEVICE_GENERATION = 2;
2668const int32_t InputDeviceTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002669const ftl::Flags<InputDeviceClass> InputDeviceTest::DEVICE_CLASSES =
Chris Ye1b0c7342020-07-28 21:57:03 -07002670 InputDeviceClass::KEYBOARD | InputDeviceClass::TOUCH | InputDeviceClass::JOYSTICK;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002671const int32_t InputDeviceTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002672
2673TEST_F(InputDeviceTest, ImmutableProperties) {
2674 ASSERT_EQ(DEVICE_ID, mDevice->getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002675 ASSERT_STREQ(DEVICE_NAME, mDevice->getName().c_str());
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002676 ASSERT_EQ(ftl::Flags<InputDeviceClass>(0), mDevice->getClasses());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002677}
2678
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002679TEST_F(InputDeviceTest, WhenDeviceCreated_EnabledIsFalse) {
2680 ASSERT_EQ(mDevice->isEnabled(), false);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07002681}
2682
Michael Wrightd02c5b62014-02-10 15:10:22 -08002683TEST_F(InputDeviceTest, WhenNoMappersAreRegistered_DeviceIsIgnored) {
2684 // Configuration.
2685 InputReaderConfiguration config;
2686 mDevice->configure(ARBITRARY_TIME, &config, 0);
2687
2688 // Reset.
2689 mDevice->reset(ARBITRARY_TIME);
2690
2691 NotifyDeviceResetArgs resetArgs;
2692 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2693 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2694 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2695
2696 // Metadata.
2697 ASSERT_TRUE(mDevice->isIgnored());
2698 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mDevice->getSources());
2699
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002700 InputDeviceInfo info = mDevice->getDeviceInfo();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002701 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002702 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002703 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NONE, info.getKeyboardType());
2704 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, info.getSources());
2705
2706 // State queries.
2707 ASSERT_EQ(0, mDevice->getMetaState());
2708
2709 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2710 << "Ignored device should return unknown key code state.";
2711 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2712 << "Ignored device should return unknown scan code state.";
2713 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 0))
2714 << "Ignored device should return unknown switch state.";
2715
2716 const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
2717 uint8_t flags[2] = { 0, 1 };
2718 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, 2, keyCodes, flags))
2719 << "Ignored device should never mark any key codes.";
2720 ASSERT_EQ(0, flags[0]) << "Flag for unsupported key should be unchanged.";
2721 ASSERT_EQ(1, flags[1]) << "Flag for unsupported key should be unchanged.";
2722}
2723
2724TEST_F(InputDeviceTest, WhenMappersAreRegistered_DeviceIsNotIgnoredAndForwardsRequestsToMappers) {
2725 // Configuration.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002726 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, String8("key"), String8("value"));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002727
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002728 FakeInputMapper& mapper1 =
2729 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002730 mapper1.setKeyboardType(AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2731 mapper1.setMetaState(AMETA_ALT_ON);
2732 mapper1.addSupportedKeyCode(AKEYCODE_A);
2733 mapper1.addSupportedKeyCode(AKEYCODE_B);
2734 mapper1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
2735 mapper1.setKeyCodeState(AKEYCODE_B, AKEY_STATE_UP);
2736 mapper1.setScanCodeState(2, AKEY_STATE_DOWN);
2737 mapper1.setScanCodeState(3, AKEY_STATE_UP);
2738 mapper1.setSwitchState(4, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002739
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002740 FakeInputMapper& mapper2 =
2741 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002742 mapper2.setMetaState(AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002743
2744 InputReaderConfiguration config;
2745 mDevice->configure(ARBITRARY_TIME, &config, 0);
2746
2747 String8 propertyValue;
2748 ASSERT_TRUE(mDevice->getConfiguration().tryGetProperty(String8("key"), propertyValue))
2749 << "Device should have read configuration during configuration phase.";
2750 ASSERT_STREQ("value", propertyValue.string());
2751
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002752 ASSERT_NO_FATAL_FAILURE(mapper1.assertConfigureWasCalled());
2753 ASSERT_NO_FATAL_FAILURE(mapper2.assertConfigureWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002754
2755 // Reset
2756 mDevice->reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002757 ASSERT_NO_FATAL_FAILURE(mapper1.assertResetWasCalled());
2758 ASSERT_NO_FATAL_FAILURE(mapper2.assertResetWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002759
2760 NotifyDeviceResetArgs resetArgs;
2761 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2762 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2763 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2764
2765 // Metadata.
2766 ASSERT_FALSE(mDevice->isIgnored());
2767 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), mDevice->getSources());
2768
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002769 InputDeviceInfo info = mDevice->getDeviceInfo();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002770 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002771 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002772 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_ALPHABETIC, info.getKeyboardType());
2773 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), info.getSources());
2774
2775 // State queries.
2776 ASSERT_EQ(AMETA_ALT_ON | AMETA_SHIFT_ON, mDevice->getMetaState())
2777 << "Should query mappers and combine meta states.";
2778
2779 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2780 << "Should return unknown key code state when source not supported.";
2781 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2782 << "Should return unknown scan code state when source not supported.";
2783 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2784 << "Should return unknown switch state when source not supported.";
2785
2786 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, AKEYCODE_A))
2787 << "Should query mapper when source is supported.";
2788 ASSERT_EQ(AKEY_STATE_UP, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 3))
2789 << "Should query mapper when source is supported.";
2790 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 4))
2791 << "Should query mapper when source is supported.";
2792
2793 const int32_t keyCodes[4] = { AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2 };
2794 uint8_t flags[4] = { 0, 0, 0, 1 };
2795 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
2796 << "Should do nothing when source is unsupported.";
2797 ASSERT_EQ(0, flags[0]) << "Flag should be unchanged when source is unsupported.";
2798 ASSERT_EQ(0, flags[1]) << "Flag should be unchanged when source is unsupported.";
2799 ASSERT_EQ(0, flags[2]) << "Flag should be unchanged when source is unsupported.";
2800 ASSERT_EQ(1, flags[3]) << "Flag should be unchanged when source is unsupported.";
2801
2802 ASSERT_TRUE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, 4, keyCodes, flags))
2803 << "Should query mapper when source is supported.";
2804 ASSERT_EQ(1, flags[0]) << "Flag for supported key should be set.";
2805 ASSERT_EQ(1, flags[1]) << "Flag for supported key should be set.";
2806 ASSERT_EQ(0, flags[2]) << "Flag for unsupported key should be unchanged.";
2807 ASSERT_EQ(1, flags[3]) << "Flag for unsupported key should be unchanged.";
2808
2809 // Event handling.
2810 RawEvent event;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002811 event.deviceId = EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002812 mDevice->process(&event, 1);
2813
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002814 ASSERT_NO_FATAL_FAILURE(mapper1.assertProcessWasCalled());
2815 ASSERT_NO_FATAL_FAILURE(mapper2.assertProcessWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002816}
2817
Arthur Hung2c9a3342019-07-23 14:18:59 +08002818// A single input device is associated with a specific display. Check that:
2819// 1. Device is disabled if the viewport corresponding to the associated display is not found
2820// 2. Device is disabled when setEnabled API is called
2821TEST_F(InputDeviceTest, Configure_AssignsDisplayPort) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002822 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002823
2824 // First Configuration.
2825 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
2826
2827 // Device should be enabled by default.
2828 ASSERT_TRUE(mDevice->isEnabled());
2829
2830 // Prepare associated info.
2831 constexpr uint8_t hdmi = 1;
2832 const std::string UNIQUE_ID = "local:1";
2833
2834 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi);
2835 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2836 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2837 // Device should be disabled because it is associated with a specific display via
2838 // input port <-> display port association, but the corresponding display is not found
2839 ASSERT_FALSE(mDevice->isEnabled());
2840
2841 // Prepare displays.
2842 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002843 DISPLAY_ORIENTATION_0, true /*isActive*/, UNIQUE_ID, hdmi,
2844 ViewportType::INTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002845 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2846 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2847 ASSERT_TRUE(mDevice->isEnabled());
2848
2849 // Device should be disabled after set disable.
2850 mFakePolicy->addDisabledDevice(mDevice->getId());
2851 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2852 InputReaderConfiguration::CHANGE_ENABLED_STATE);
2853 ASSERT_FALSE(mDevice->isEnabled());
2854
2855 // Device should still be disabled even found the associated display.
2856 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2857 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2858 ASSERT_FALSE(mDevice->isEnabled());
2859}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002860
Christine Franks1ba71cc2021-04-07 14:37:42 -07002861TEST_F(InputDeviceTest, Configure_AssignsDisplayUniqueId) {
2862 // Device should be enabled by default.
2863 mFakePolicy->clearViewports();
2864 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
2865 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
2866 ASSERT_TRUE(mDevice->isEnabled());
2867
2868 // Device should be disabled because it is associated with a specific display, but the
2869 // corresponding display is not found.
2870 const std::string DISPLAY_UNIQUE_ID = "displayUniqueId";
Christine Franks2a2293c2022-01-18 11:51:16 -08002871 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002872 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2873 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2874 ASSERT_FALSE(mDevice->isEnabled());
2875
2876 // Device should be enabled when a display is found.
2877 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2878 DISPLAY_ORIENTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
2879 NO_PORT, ViewportType::INTERNAL);
2880 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2881 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2882 ASSERT_TRUE(mDevice->isEnabled());
2883
2884 // Device should be disabled after set disable.
2885 mFakePolicy->addDisabledDevice(mDevice->getId());
2886 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2887 InputReaderConfiguration::CHANGE_ENABLED_STATE);
2888 ASSERT_FALSE(mDevice->isEnabled());
2889
2890 // Device should still be disabled even found the associated display.
2891 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2892 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2893 ASSERT_FALSE(mDevice->isEnabled());
2894}
2895
Christine Franks2a2293c2022-01-18 11:51:16 -08002896TEST_F(InputDeviceTest, Configure_UniqueId_CorrectlyMatches) {
2897 mFakePolicy->clearViewports();
2898 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
2899 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
2900
2901 const std::string DISPLAY_UNIQUE_ID = "displayUniqueId";
2902 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
2903 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2904 DISPLAY_ORIENTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
2905 NO_PORT, ViewportType::INTERNAL);
2906 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2907 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2908 ASSERT_EQ(DISPLAY_UNIQUE_ID, mDevice->getAssociatedDisplayUniqueId());
2909}
2910
Michael Wrightd02c5b62014-02-10 15:10:22 -08002911// --- InputMapperTest ---
2912
2913class InputMapperTest : public testing::Test {
2914protected:
2915 static const char* DEVICE_NAME;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002916 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002917 static const int32_t DEVICE_ID;
2918 static const int32_t DEVICE_GENERATION;
2919 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002920 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002921 static const int32_t EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002922
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002923 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002924 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002925 std::unique_ptr<TestInputListener> mFakeListener;
arthurhungdcef2dc2020-08-11 14:47:50 +08002926 std::unique_ptr<InstrumentedInputReader> mReader;
2927 std::shared_ptr<InputDevice> mDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002928
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002929 virtual void SetUp(ftl::Flags<InputDeviceClass> classes) {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002930 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002931 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002932 mFakeListener = std::make_unique<TestInputListener>();
arthurhungdcef2dc2020-08-11 14:47:50 +08002933 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002934 *mFakeListener);
arthurhungdcef2dc2020-08-11 14:47:50 +08002935 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002936 }
2937
Prabir Pradhanc14266f2021-05-12 15:56:24 -07002938 void SetUp() override {
Prabir Pradhanc14266f2021-05-12 15:56:24 -07002939 SetUp(DEVICE_CLASSES);
2940 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002941
Chris Yea52ade12020-08-27 16:49:20 -07002942 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002943 mFakeListener.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002944 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002945 }
2946
2947 void addConfigurationProperty(const char* key, const char* value) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002948 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, String8(key), String8(value));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002949 }
2950
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002951 void configureDevice(uint32_t changes) {
Prabir Pradhanf467f032022-04-19 10:37:19 +00002952 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
arthurhungdcef2dc2020-08-11 14:47:50 +08002953 mReader->requestRefreshConfiguration(changes);
2954 mReader->loopOnce();
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -08002955 }
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002956 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
2957 }
2958
arthurhungdcef2dc2020-08-11 14:47:50 +08002959 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
2960 const std::string& location, int32_t eventHubId,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002961 ftl::Flags<InputDeviceClass> classes) {
arthurhungdcef2dc2020-08-11 14:47:50 +08002962 InputDeviceIdentifier identifier;
2963 identifier.name = name;
2964 identifier.location = location;
2965 std::shared_ptr<InputDevice> device =
2966 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
2967 identifier);
2968 mReader->pushNextDevice(device);
2969 mFakeEventHub->addDevice(eventHubId, name, classes);
2970 mReader->loopOnce();
2971 return device;
2972 }
2973
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002974 template <class T, typename... Args>
2975 T& addMapperAndConfigure(Args... args) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002976 T& mapper = mDevice->addMapper<T>(EVENTHUB_ID, args...);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002977 configureDevice(0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002978 mDevice->reset(ARBITRARY_TIME);
Chris Ye42b06822020-08-07 11:39:33 -07002979 mapper.reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002980 return mapper;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002981 }
2982
2983 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002984 int32_t orientation, const std::string& uniqueId,
2985 std::optional<uint8_t> physicalPort, ViewportType viewportType) {
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002986 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, true /*isActive*/,
2987 uniqueId, physicalPort, viewportType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07002988 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2989 }
2990
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002991 void clearViewports() {
2992 mFakePolicy->clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002993 }
2994
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002995 void process(InputMapper& mapper, nsecs_t when, nsecs_t readTime, int32_t type, int32_t code,
2996 int32_t value) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002997 RawEvent event;
2998 event.when = when;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002999 event.readTime = readTime;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003000 event.deviceId = mapper.getDeviceContext().getEventHubId();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003001 event.type = type;
3002 event.code = code;
3003 event.value = value;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003004 mapper.process(&event);
arthurhungdcef2dc2020-08-11 14:47:50 +08003005 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003006 }
3007
3008 static void assertMotionRange(const InputDeviceInfo& info,
3009 int32_t axis, uint32_t source, float min, float max, float flat, float fuzz) {
3010 const InputDeviceInfo::MotionRange* range = info.getMotionRange(axis, source);
Yi Kong9b14ac62018-07-17 13:48:38 -07003011 ASSERT_TRUE(range != nullptr) << "Axis: " << axis << " Source: " << source;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003012 ASSERT_EQ(axis, range->axis) << "Axis: " << axis << " Source: " << source;
3013 ASSERT_EQ(source, range->source) << "Axis: " << axis << " Source: " << source;
3014 ASSERT_NEAR(min, range->min, EPSILON) << "Axis: " << axis << " Source: " << source;
3015 ASSERT_NEAR(max, range->max, EPSILON) << "Axis: " << axis << " Source: " << source;
3016 ASSERT_NEAR(flat, range->flat, EPSILON) << "Axis: " << axis << " Source: " << source;
3017 ASSERT_NEAR(fuzz, range->fuzz, EPSILON) << "Axis: " << axis << " Source: " << source;
3018 }
3019
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003020 static void assertPointerCoords(const PointerCoords& coords, float x, float y, float pressure,
3021 float size, float touchMajor, float touchMinor, float toolMajor,
3022 float toolMinor, float orientation, float distance,
3023 float scaledAxisEpsilon = 1.f) {
3024 ASSERT_NEAR(x, coords.getAxisValue(AMOTION_EVENT_AXIS_X), scaledAxisEpsilon);
3025 ASSERT_NEAR(y, coords.getAxisValue(AMOTION_EVENT_AXIS_Y), scaledAxisEpsilon);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003026 ASSERT_NEAR(pressure, coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), EPSILON);
3027 ASSERT_NEAR(size, coords.getAxisValue(AMOTION_EVENT_AXIS_SIZE), EPSILON);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003028 ASSERT_NEAR(touchMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR),
3029 scaledAxisEpsilon);
3030 ASSERT_NEAR(touchMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR),
3031 scaledAxisEpsilon);
3032 ASSERT_NEAR(toolMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR),
3033 scaledAxisEpsilon);
3034 ASSERT_NEAR(toolMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR),
3035 scaledAxisEpsilon);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003036 ASSERT_NEAR(orientation, coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION), EPSILON);
3037 ASSERT_NEAR(distance, coords.getAxisValue(AMOTION_EVENT_AXIS_DISTANCE), EPSILON);
3038 }
3039
Michael Wright17db18e2020-06-26 20:51:44 +01003040 static void assertPosition(const FakePointerController& controller, float x, float y) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003041 float actualX, actualY;
Michael Wright17db18e2020-06-26 20:51:44 +01003042 controller.getPosition(&actualX, &actualY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003043 ASSERT_NEAR(x, actualX, 1);
3044 ASSERT_NEAR(y, actualY, 1);
3045 }
3046};
3047
3048const char* InputMapperTest::DEVICE_NAME = "device";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003049const char* InputMapperTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003050const int32_t InputMapperTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003051const int32_t InputMapperTest::DEVICE_GENERATION = 2;
3052const int32_t InputMapperTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003053const ftl::Flags<InputDeviceClass> InputMapperTest::DEVICE_CLASSES =
3054 ftl::Flags<InputDeviceClass>(0); // not needed for current tests
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003055const int32_t InputMapperTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003056
3057// --- SwitchInputMapperTest ---
3058
3059class SwitchInputMapperTest : public InputMapperTest {
3060protected:
3061};
3062
3063TEST_F(SwitchInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003064 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003065
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003066 ASSERT_EQ(uint32_t(AINPUT_SOURCE_SWITCH), mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003067}
3068
3069TEST_F(SwitchInputMapperTest, GetSwitchState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003070 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003071
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003072 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003073 ASSERT_EQ(1, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003074
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003075 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003076 ASSERT_EQ(0, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003077}
3078
3079TEST_F(SwitchInputMapperTest, Process) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003080 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003081
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003082 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_LID, 1);
3083 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_JACK_PHYSICAL_INSERT, 1);
3084 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_HEADPHONE_INSERT, 0);
3085 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003086
3087 NotifySwitchArgs args;
3088 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySwitchWasCalled(&args));
3089 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
Dan Albert1bd2fc02016-02-02 15:11:57 -08003090 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT), args.switchValues);
3091 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT) | (1 << SW_HEADPHONE_INSERT),
Michael Wrightd02c5b62014-02-10 15:10:22 -08003092 args.switchMask);
3093 ASSERT_EQ(uint32_t(0), args.policyFlags);
3094}
3095
Chris Ye87143712020-11-10 05:05:58 +00003096// --- VibratorInputMapperTest ---
3097class VibratorInputMapperTest : public InputMapperTest {
3098protected:
3099 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::VIBRATOR); }
3100};
3101
3102TEST_F(VibratorInputMapperTest, GetSources) {
3103 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
3104
3105 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mapper.getSources());
3106}
3107
3108TEST_F(VibratorInputMapperTest, GetVibratorIds) {
3109 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
3110
3111 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
3112}
3113
3114TEST_F(VibratorInputMapperTest, Vibrate) {
3115 constexpr uint8_t DEFAULT_AMPLITUDE = 192;
Chris Yefb552902021-02-03 17:18:37 -08003116 constexpr int32_t VIBRATION_TOKEN = 100;
Chris Ye87143712020-11-10 05:05:58 +00003117 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
3118
3119 VibrationElement pattern(2);
3120 VibrationSequence sequence(2);
3121 pattern.duration = std::chrono::milliseconds(200);
3122 pattern.channels = {{0 /* vibratorId */, DEFAULT_AMPLITUDE / 2},
3123 {1 /* vibratorId */, DEFAULT_AMPLITUDE}};
3124 sequence.addElement(pattern);
3125 pattern.duration = std::chrono::milliseconds(500);
3126 pattern.channels = {{0 /* vibratorId */, DEFAULT_AMPLITUDE / 4},
3127 {1 /* vibratorId */, DEFAULT_AMPLITUDE}};
3128 sequence.addElement(pattern);
3129
3130 std::vector<int64_t> timings = {0, 1};
3131 std::vector<uint8_t> amplitudes = {DEFAULT_AMPLITUDE, DEFAULT_AMPLITUDE / 2};
3132
3133 ASSERT_FALSE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08003134 // Start vibrating
3135 mapper.vibrate(sequence, -1 /* repeat */, VIBRATION_TOKEN);
Chris Ye87143712020-11-10 05:05:58 +00003136 ASSERT_TRUE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08003137 // Verify vibrator state listener was notified.
3138 mReader->loopOnce();
3139 NotifyVibratorStateArgs args;
3140 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyVibratorStateWasCalled(&args));
3141 ASSERT_EQ(DEVICE_ID, args.deviceId);
3142 ASSERT_TRUE(args.isOn);
3143 // Stop vibrating
3144 mapper.cancelVibrate(VIBRATION_TOKEN);
3145 ASSERT_FALSE(mapper.isVibrating());
3146 // Verify vibrator state listener was notified.
3147 mReader->loopOnce();
3148 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyVibratorStateWasCalled(&args));
3149 ASSERT_EQ(DEVICE_ID, args.deviceId);
3150 ASSERT_FALSE(args.isOn);
Chris Ye87143712020-11-10 05:05:58 +00003151}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003152
Chris Yef59a2f42020-10-16 12:55:26 -07003153// --- SensorInputMapperTest ---
3154
3155class SensorInputMapperTest : public InputMapperTest {
3156protected:
3157 static const int32_t ACCEL_RAW_MIN;
3158 static const int32_t ACCEL_RAW_MAX;
3159 static const int32_t ACCEL_RAW_FUZZ;
3160 static const int32_t ACCEL_RAW_FLAT;
3161 static const int32_t ACCEL_RAW_RESOLUTION;
3162
3163 static const int32_t GYRO_RAW_MIN;
3164 static const int32_t GYRO_RAW_MAX;
3165 static const int32_t GYRO_RAW_FUZZ;
3166 static const int32_t GYRO_RAW_FLAT;
3167 static const int32_t GYRO_RAW_RESOLUTION;
3168
3169 static const float GRAVITY_MS2_UNIT;
3170 static const float DEGREE_RADIAN_UNIT;
3171
3172 void prepareAccelAxes();
3173 void prepareGyroAxes();
3174 void setAccelProperties();
3175 void setGyroProperties();
3176 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::SENSOR); }
3177};
3178
3179const int32_t SensorInputMapperTest::ACCEL_RAW_MIN = -32768;
3180const int32_t SensorInputMapperTest::ACCEL_RAW_MAX = 32768;
3181const int32_t SensorInputMapperTest::ACCEL_RAW_FUZZ = 16;
3182const int32_t SensorInputMapperTest::ACCEL_RAW_FLAT = 0;
3183const int32_t SensorInputMapperTest::ACCEL_RAW_RESOLUTION = 8192;
3184
3185const int32_t SensorInputMapperTest::GYRO_RAW_MIN = -2097152;
3186const int32_t SensorInputMapperTest::GYRO_RAW_MAX = 2097152;
3187const int32_t SensorInputMapperTest::GYRO_RAW_FUZZ = 16;
3188const int32_t SensorInputMapperTest::GYRO_RAW_FLAT = 0;
3189const int32_t SensorInputMapperTest::GYRO_RAW_RESOLUTION = 1024;
3190
3191const float SensorInputMapperTest::GRAVITY_MS2_UNIT = 9.80665f;
3192const float SensorInputMapperTest::DEGREE_RADIAN_UNIT = 0.0174533f;
3193
3194void SensorInputMapperTest::prepareAccelAxes() {
3195 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3196 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3197 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3198 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3199 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Z, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3200 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3201}
3202
3203void SensorInputMapperTest::prepareGyroAxes() {
3204 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RX, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3205 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3206 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RY, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3207 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3208 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RZ, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3209 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3210}
3211
3212void SensorInputMapperTest::setAccelProperties() {
3213 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 0, InputDeviceSensorType::ACCELEROMETER,
3214 /* sensorDataIndex */ 0);
3215 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 1, InputDeviceSensorType::ACCELEROMETER,
3216 /* sensorDataIndex */ 1);
3217 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 2, InputDeviceSensorType::ACCELEROMETER,
3218 /* sensorDataIndex */ 2);
3219 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
3220 addConfigurationProperty("sensor.accelerometer.reportingMode", "0");
3221 addConfigurationProperty("sensor.accelerometer.maxDelay", "100000");
3222 addConfigurationProperty("sensor.accelerometer.minDelay", "5000");
3223 addConfigurationProperty("sensor.accelerometer.power", "1.5");
3224}
3225
3226void SensorInputMapperTest::setGyroProperties() {
3227 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 3, InputDeviceSensorType::GYROSCOPE,
3228 /* sensorDataIndex */ 0);
3229 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 4, InputDeviceSensorType::GYROSCOPE,
3230 /* sensorDataIndex */ 1);
3231 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 5, InputDeviceSensorType::GYROSCOPE,
3232 /* sensorDataIndex */ 2);
3233 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
3234 addConfigurationProperty("sensor.gyroscope.reportingMode", "0");
3235 addConfigurationProperty("sensor.gyroscope.maxDelay", "100000");
3236 addConfigurationProperty("sensor.gyroscope.minDelay", "5000");
3237 addConfigurationProperty("sensor.gyroscope.power", "0.8");
3238}
3239
3240TEST_F(SensorInputMapperTest, GetSources) {
3241 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3242
3243 ASSERT_EQ(static_cast<uint32_t>(AINPUT_SOURCE_SENSOR), mapper.getSources());
3244}
3245
3246TEST_F(SensorInputMapperTest, ProcessAccelerometerSensor) {
3247 setAccelProperties();
3248 prepareAccelAxes();
3249 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3250
3251 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::ACCELEROMETER,
3252 std::chrono::microseconds(10000),
3253 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08003254 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003255 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, 20000);
3256 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, -20000);
3257 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Z, 40000);
3258 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
3259 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07003260
3261 NotifySensorArgs args;
3262 std::vector<float> values = {20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
3263 -20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
3264 40000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT};
3265
3266 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
3267 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
3268 ASSERT_EQ(args.deviceId, DEVICE_ID);
3269 ASSERT_EQ(args.sensorType, InputDeviceSensorType::ACCELEROMETER);
3270 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
3271 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
3272 ASSERT_EQ(args.values, values);
3273 mapper.flushSensor(InputDeviceSensorType::ACCELEROMETER);
3274}
3275
3276TEST_F(SensorInputMapperTest, ProcessGyroscopeSensor) {
3277 setGyroProperties();
3278 prepareGyroAxes();
3279 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3280
3281 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::GYROSCOPE,
3282 std::chrono::microseconds(10000),
3283 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08003284 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003285 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RX, 20000);
3286 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RY, -20000);
3287 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RZ, 40000);
3288 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
3289 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07003290
3291 NotifySensorArgs args;
3292 std::vector<float> values = {20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
3293 -20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
3294 40000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT};
3295
3296 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
3297 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
3298 ASSERT_EQ(args.deviceId, DEVICE_ID);
3299 ASSERT_EQ(args.sensorType, InputDeviceSensorType::GYROSCOPE);
3300 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
3301 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
3302 ASSERT_EQ(args.values, values);
3303 mapper.flushSensor(InputDeviceSensorType::GYROSCOPE);
3304}
3305
Michael Wrightd02c5b62014-02-10 15:10:22 -08003306// --- KeyboardInputMapperTest ---
3307
3308class KeyboardInputMapperTest : public InputMapperTest {
3309protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003310 const std::string UNIQUE_ID = "local:0";
3311
3312 void prepareDisplay(int32_t orientation);
3313
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003314 void testDPadKeyRotation(KeyboardInputMapper& mapper, int32_t originalScanCode,
Arthur Hung2c9a3342019-07-23 14:18:59 +08003315 int32_t originalKeyCode, int32_t rotatedKeyCode,
3316 int32_t displayId = ADISPLAY_ID_NONE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003317};
3318
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003319/* Similar to setDisplayInfoAndReconfigure, but pre-populates all parameters except for the
3320 * orientation.
3321 */
3322void KeyboardInputMapperTest::prepareDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003323 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
3324 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003325}
3326
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003327void KeyboardInputMapperTest::testDPadKeyRotation(KeyboardInputMapper& mapper,
Arthur Hung2c9a3342019-07-23 14:18:59 +08003328 int32_t originalScanCode, int32_t originalKeyCode,
3329 int32_t rotatedKeyCode, int32_t displayId) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003330 NotifyKeyArgs args;
3331
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003332 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003333 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3334 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3335 ASSERT_EQ(originalScanCode, args.scanCode);
3336 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003337 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003338
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003339 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003340 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3341 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3342 ASSERT_EQ(originalScanCode, args.scanCode);
3343 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003344 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003345}
3346
Michael Wrightd02c5b62014-02-10 15:10:22 -08003347TEST_F(KeyboardInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003348 KeyboardInputMapper& mapper =
3349 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3350 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003351
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003352 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003353}
3354
3355TEST_F(KeyboardInputMapperTest, Process_SimpleKeyPress) {
3356 const int32_t USAGE_A = 0x070004;
3357 const int32_t USAGE_UNKNOWN = 0x07ffff;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003358 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3359 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
Chris Yea52ade12020-08-27 16:49:20 -07003360 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, POLICY_FLAG_WAKE);
3361 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, POLICY_FLAG_WAKE);
3362 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003363
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003364 KeyboardInputMapper& mapper =
3365 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3366 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003367 // Initial metastate is AMETA_NONE.
3368 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003369
3370 // Key down by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003371 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003372 NotifyKeyArgs args;
3373 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3374 ASSERT_EQ(DEVICE_ID, args.deviceId);
3375 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3376 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3377 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3378 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3379 ASSERT_EQ(KEY_HOME, args.scanCode);
3380 ASSERT_EQ(AMETA_NONE, args.metaState);
3381 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3382 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3383 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3384
3385 // Key up by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003386 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003387 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3388 ASSERT_EQ(DEVICE_ID, args.deviceId);
3389 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3390 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3391 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3392 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3393 ASSERT_EQ(KEY_HOME, args.scanCode);
3394 ASSERT_EQ(AMETA_NONE, args.metaState);
3395 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3396 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3397 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3398
3399 // Key down by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003400 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3401 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, 0, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003402 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3403 ASSERT_EQ(DEVICE_ID, args.deviceId);
3404 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3405 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3406 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3407 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3408 ASSERT_EQ(0, args.scanCode);
3409 ASSERT_EQ(AMETA_NONE, args.metaState);
3410 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3411 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3412 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3413
3414 // Key up by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003415 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3416 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003417 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3418 ASSERT_EQ(DEVICE_ID, args.deviceId);
3419 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3420 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3421 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3422 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3423 ASSERT_EQ(0, args.scanCode);
3424 ASSERT_EQ(AMETA_NONE, args.metaState);
3425 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3426 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3427 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3428
3429 // Key down with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003430 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3431 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UNKNOWN, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003432 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3433 ASSERT_EQ(DEVICE_ID, args.deviceId);
3434 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3435 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3436 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3437 ASSERT_EQ(0, args.keyCode);
3438 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3439 ASSERT_EQ(AMETA_NONE, args.metaState);
3440 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3441 ASSERT_EQ(0U, args.policyFlags);
3442 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3443
3444 // Key up with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003445 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3446 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003447 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3448 ASSERT_EQ(DEVICE_ID, args.deviceId);
3449 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3450 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3451 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3452 ASSERT_EQ(0, args.keyCode);
3453 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3454 ASSERT_EQ(AMETA_NONE, args.metaState);
3455 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3456 ASSERT_EQ(0U, args.policyFlags);
3457 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3458}
3459
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003460/**
3461 * Ensure that the readTime is set to the time when the EV_KEY is received.
3462 */
3463TEST_F(KeyboardInputMapperTest, Process_SendsReadTime) {
3464 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3465
3466 KeyboardInputMapper& mapper =
3467 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3468 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3469 NotifyKeyArgs args;
3470
3471 // Key down
3472 process(mapper, ARBITRARY_TIME, 12 /*readTime*/, EV_KEY, KEY_HOME, 1);
3473 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3474 ASSERT_EQ(12, args.readTime);
3475
3476 // Key up
3477 process(mapper, ARBITRARY_TIME, 15 /*readTime*/, EV_KEY, KEY_HOME, 1);
3478 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3479 ASSERT_EQ(15, args.readTime);
3480}
3481
Michael Wrightd02c5b62014-02-10 15:10:22 -08003482TEST_F(KeyboardInputMapperTest, Process_ShouldUpdateMetaState) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003483 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFTSHIFT, 0, AKEYCODE_SHIFT_LEFT, 0);
3484 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003485 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, 0);
3486 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, 0);
3487 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003488
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003489 KeyboardInputMapper& mapper =
3490 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3491 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003492
Arthur Hung95f68612022-04-07 14:08:22 +08003493 // Initial metastate is AMETA_NONE.
3494 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003495
3496 // Metakey down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003497 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003498 NotifyKeyArgs args;
3499 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3500 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003501 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003502 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003503
3504 // Key down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003505 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_A, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003506 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3507 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003508 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003509
3510 // Key up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003511 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, KEY_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003512 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3513 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003514 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003515
3516 // Metakey up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003517 process(mapper, ARBITRARY_TIME + 3, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003518 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3519 ASSERT_EQ(AMETA_NONE, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003520 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003521 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003522}
3523
3524TEST_F(KeyboardInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003525 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3526 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3527 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3528 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003529
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003530 KeyboardInputMapper& mapper =
3531 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3532 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003533
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003534 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003535 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3536 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
3537 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3538 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
3539 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3540 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
3541 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3542 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
3543}
3544
3545TEST_F(KeyboardInputMapperTest, Process_WhenOrientationAware_ShouldRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003546 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3547 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3548 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3549 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003550
Michael Wrightd02c5b62014-02-10 15:10:22 -08003551 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003552 KeyboardInputMapper& mapper =
3553 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3554 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003555
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003556 prepareDisplay(DISPLAY_ORIENTATION_0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003557 ASSERT_NO_FATAL_FAILURE(
3558 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3559 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3560 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3561 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3562 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3563 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3564 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003565
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003566 clearViewports();
3567 prepareDisplay(DISPLAY_ORIENTATION_90);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003568 ASSERT_NO_FATAL_FAILURE(
3569 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3570 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3571 AKEYCODE_DPAD_UP, DISPLAY_ID));
3572 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3573 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3574 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3575 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003576
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003577 clearViewports();
3578 prepareDisplay(DISPLAY_ORIENTATION_180);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003579 ASSERT_NO_FATAL_FAILURE(
3580 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3581 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3582 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3583 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3584 AKEYCODE_DPAD_UP, DISPLAY_ID));
3585 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3586 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003587
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003588 clearViewports();
3589 prepareDisplay(DISPLAY_ORIENTATION_270);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003590 ASSERT_NO_FATAL_FAILURE(
3591 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3592 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3593 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3594 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3595 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3596 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3597 AKEYCODE_DPAD_UP, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003598
3599 // Special case: if orientation changes while key is down, we still emit the same keycode
3600 // in the key up as we did in the key down.
3601 NotifyKeyArgs args;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003602 clearViewports();
3603 prepareDisplay(DISPLAY_ORIENTATION_270);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003604 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003605 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3606 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3607 ASSERT_EQ(KEY_UP, args.scanCode);
3608 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3609
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003610 clearViewports();
3611 prepareDisplay(DISPLAY_ORIENTATION_180);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003612 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003613 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3614 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3615 ASSERT_EQ(KEY_UP, args.scanCode);
3616 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3617}
3618
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003619TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_NotOrientationAware) {
3620 // If the keyboard is not orientation aware,
3621 // key events should not be associated with a specific display id
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003622 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003623
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003624 KeyboardInputMapper& mapper =
3625 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3626 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003627 NotifyKeyArgs args;
3628
3629 // Display id should be ADISPLAY_ID_NONE without any display configuration.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003630 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003631 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003632 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003633 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3634 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3635
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003636 prepareDisplay(DISPLAY_ORIENTATION_0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003637 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003638 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003639 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003640 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3641 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3642}
3643
3644TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_OrientationAware) {
3645 // If the keyboard is orientation aware,
3646 // key events should be associated with the internal viewport
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003647 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003648
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003649 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003650 KeyboardInputMapper& mapper =
3651 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3652 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003653 NotifyKeyArgs args;
3654
3655 // Display id should be ADISPLAY_ID_NONE without any display configuration.
3656 // ^--- already checked by the previous test
3657
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003658 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003659 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003660 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003661 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003662 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003663 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3664 ASSERT_EQ(DISPLAY_ID, args.displayId);
3665
3666 constexpr int32_t newDisplayId = 2;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003667 clearViewports();
3668 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003669 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003670 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003671 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003672 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003673 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3674 ASSERT_EQ(newDisplayId, args.displayId);
3675}
3676
Michael Wrightd02c5b62014-02-10 15:10:22 -08003677TEST_F(KeyboardInputMapperTest, GetKeyCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003678 KeyboardInputMapper& mapper =
3679 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3680 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003681
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003682 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003683 ASSERT_EQ(1, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003684
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003685 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003686 ASSERT_EQ(0, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003687}
3688
Philip Junker4af3b3d2021-12-14 10:36:55 +01003689TEST_F(KeyboardInputMapperTest, GetKeyCodeForKeyLocation) {
3690 KeyboardInputMapper& mapper =
3691 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3692 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3693
3694 mFakeEventHub->addKeyCodeMapping(EVENTHUB_ID, AKEYCODE_Y, AKEYCODE_Z);
3695 ASSERT_EQ(AKEYCODE_Z, mapper.getKeyCodeForKeyLocation(AKEYCODE_Y))
3696 << "If a mapping is available, the result is equal to the mapping";
3697
3698 ASSERT_EQ(AKEYCODE_A, mapper.getKeyCodeForKeyLocation(AKEYCODE_A))
3699 << "If no mapping is available, the result is the key location";
3700}
3701
Michael Wrightd02c5b62014-02-10 15:10:22 -08003702TEST_F(KeyboardInputMapperTest, GetScanCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003703 KeyboardInputMapper& mapper =
3704 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3705 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003706
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003707 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003708 ASSERT_EQ(1, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003709
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003710 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003711 ASSERT_EQ(0, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003712}
3713
3714TEST_F(KeyboardInputMapperTest, MarkSupportedKeyCodes) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003715 KeyboardInputMapper& mapper =
3716 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3717 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003718
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003719 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003720
3721 const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
3722 uint8_t flags[2] = { 0, 0 };
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003723 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, 1, keyCodes, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003724 ASSERT_TRUE(flags[0]);
3725 ASSERT_FALSE(flags[1]);
3726}
3727
3728TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleMetaStateAndLeds) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003729 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3730 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3731 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3732 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3733 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3734 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003735
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003736 KeyboardInputMapper& mapper =
3737 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3738 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003739 // Initial metastate is AMETA_NONE.
3740 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003741
3742 // Initialization should have turned all of the lights off.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003743 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3744 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3745 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003746
3747 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003748 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3749 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003750 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3751 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3752 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003753 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003754
3755 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003756 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3757 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003758 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3759 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3760 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003761 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003762
3763 // Toggle caps lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003764 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3765 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003766 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3767 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3768 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003769 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003770
3771 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003772 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3773 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003774 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3775 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3776 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003777 ASSERT_EQ(AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003778
3779 // Toggle num lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003780 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3781 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003782 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3783 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3784 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003785 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003786
3787 // Toggle scroll lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003788 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3789 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003790 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3791 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3792 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003793 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003794}
3795
Chris Yea52ade12020-08-27 16:49:20 -07003796TEST_F(KeyboardInputMapperTest, NoMetaStateWhenMetaKeysNotPresent) {
3797 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_BUTTON_A, 0);
3798 mFakeEventHub->addKey(EVENTHUB_ID, BTN_B, 0, AKEYCODE_BUTTON_B, 0);
3799 mFakeEventHub->addKey(EVENTHUB_ID, BTN_X, 0, AKEYCODE_BUTTON_X, 0);
3800 mFakeEventHub->addKey(EVENTHUB_ID, BTN_Y, 0, AKEYCODE_BUTTON_Y, 0);
3801
3802 KeyboardInputMapper& mapper =
3803 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3804 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
3805
Chris Yea52ade12020-08-27 16:49:20 -07003806 // Meta state should be AMETA_NONE after reset
3807 mapper.reset(ARBITRARY_TIME);
3808 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3809 // Meta state should be AMETA_NONE with update, as device doesn't have the keys.
3810 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
3811 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3812
3813 NotifyKeyArgs args;
3814 // Press button "A"
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003815 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_A, 1);
Chris Yea52ade12020-08-27 16:49:20 -07003816 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3817 ASSERT_EQ(AMETA_NONE, args.metaState);
3818 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3819 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3820 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3821
3822 // Button up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003823 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003824 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3825 ASSERT_EQ(AMETA_NONE, args.metaState);
3826 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3827 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3828 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3829}
3830
Arthur Hung2c9a3342019-07-23 14:18:59 +08003831TEST_F(KeyboardInputMapperTest, Configure_AssignsDisplayPort) {
3832 // keyboard 1.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003833 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3834 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3835 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3836 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003837
3838 // keyboard 2.
3839 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08003840 const std::string DEVICE_NAME2 = "KEYBOARD2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08003841 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003842 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08003843 std::shared_ptr<InputDevice> device2 =
3844 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003845 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08003846
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003847 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3848 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3849 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3850 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003851
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003852 KeyboardInputMapper& mapper =
3853 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3854 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003855
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003856 KeyboardInputMapper& mapper2 =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003857 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003858 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003859 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
3860 device2->reset(ARBITRARY_TIME);
3861
3862 // Prepared displays and associated info.
3863 constexpr uint8_t hdmi1 = 0;
3864 constexpr uint8_t hdmi2 = 1;
3865 const std::string SECONDARY_UNIQUE_ID = "local:1";
3866
3867 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
3868 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
3869
3870 // No associated display viewport found, should disable the device.
3871 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3872 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3873 ASSERT_FALSE(device2->isEnabled());
3874
3875 // Prepare second display.
3876 constexpr int32_t newDisplayId = 2;
3877 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003878 UNIQUE_ID, hdmi1, ViewportType::INTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003879 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003880 SECONDARY_UNIQUE_ID, hdmi2, ViewportType::EXTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003881 // Default device will reconfigure above, need additional reconfiguration for another device.
3882 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3883 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3884
3885 // Device should be enabled after the associated display is found.
3886 ASSERT_TRUE(mDevice->isEnabled());
3887 ASSERT_TRUE(device2->isEnabled());
3888
3889 // Test pad key events
3890 ASSERT_NO_FATAL_FAILURE(
3891 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3892 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3893 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3894 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3895 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3896 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3897 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3898
3899 ASSERT_NO_FATAL_FAILURE(
3900 testDPadKeyRotation(mapper2, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, newDisplayId));
3901 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3902 AKEYCODE_DPAD_RIGHT, newDisplayId));
3903 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3904 AKEYCODE_DPAD_DOWN, newDisplayId));
3905 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3906 AKEYCODE_DPAD_LEFT, newDisplayId));
3907}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003908
arthurhungc903df12020-08-11 15:08:42 +08003909TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleAfterReattach) {
3910 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3911 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3912 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3913 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3914 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3915 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3916
3917 KeyboardInputMapper& mapper =
3918 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3919 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003920 // Initial metastate is AMETA_NONE.
3921 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08003922
3923 // Initialization should have turned all of the lights off.
3924 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3925 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3926 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3927
3928 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003929 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3930 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003931 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3932 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
3933
3934 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003935 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3936 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003937 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3938 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
3939
3940 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003941 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3942 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003943 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3944 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
3945
3946 mFakeEventHub->removeDevice(EVENTHUB_ID);
3947 mReader->loopOnce();
3948
3949 // keyboard 2 should default toggle keys.
3950 const std::string USB2 = "USB2";
3951 const std::string DEVICE_NAME2 = "KEYBOARD2";
3952 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
3953 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
3954 std::shared_ptr<InputDevice> device2 =
3955 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003956 ftl::Flags<InputDeviceClass>(0));
arthurhungc903df12020-08-11 15:08:42 +08003957 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3958 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
3959 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3960 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3961 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3962 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3963
arthurhung6fe95782020-10-05 22:41:16 +08003964 KeyboardInputMapper& mapper2 =
3965 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
3966 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
arthurhungc903df12020-08-11 15:08:42 +08003967 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
3968 device2->reset(ARBITRARY_TIME);
3969
3970 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_CAPSL));
3971 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_NUML));
3972 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_SCROLLL));
arthurhung6fe95782020-10-05 22:41:16 +08003973 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON,
3974 mapper2.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08003975}
3976
Arthur Hungcb40a002021-08-03 14:31:01 +00003977TEST_F(KeyboardInputMapperTest, Process_toggleCapsLockState) {
3978 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3979 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3980 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3981
3982 // Suppose we have two mappers. (DPAD + KEYBOARD)
3983 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_DPAD,
3984 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
3985 KeyboardInputMapper& mapper =
3986 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3987 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003988 // Initial metastate is AMETA_NONE.
3989 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Arthur Hungcb40a002021-08-03 14:31:01 +00003990
3991 mReader->toggleCapsLockState(DEVICE_ID);
3992 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
3993}
3994
Arthur Hungfb3cc112022-04-13 07:39:50 +00003995TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleInMultiDevices) {
3996 // keyboard 1.
3997 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3998 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3999 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
4000 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4001 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4002 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
4003
4004 KeyboardInputMapper& mapper1 =
4005 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4006 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
4007
4008 // keyboard 2.
4009 const std::string USB2 = "USB2";
4010 const std::string DEVICE_NAME2 = "KEYBOARD2";
4011 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
4012 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
4013 std::shared_ptr<InputDevice> device2 =
4014 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
4015 ftl::Flags<InputDeviceClass>(0));
4016 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
4017 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
4018 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
4019 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4020 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4021 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
4022
4023 KeyboardInputMapper& mapper2 =
4024 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
4025 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
4026 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
4027 device2->reset(ARBITRARY_TIME);
4028
Arthur Hung95f68612022-04-07 14:08:22 +08004029 // Initial metastate is AMETA_NONE.
4030 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4031 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4032
4033 // Toggle num lock on and off.
4034 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
4035 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Arthur Hungfb3cc112022-04-13 07:39:50 +00004036 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4037 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper1.getMetaState());
4038 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper2.getMetaState());
4039
4040 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
4041 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
4042 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4043 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4044 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4045
4046 // Toggle caps lock on and off.
4047 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
4048 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
4049 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4050 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper1.getMetaState());
4051 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper2.getMetaState());
4052
4053 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
4054 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
4055 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4056 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4057 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4058
4059 // Toggle scroll lock on and off.
4060 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
4061 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
4062 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
4063 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper1.getMetaState());
4064 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper2.getMetaState());
4065
4066 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
4067 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
4068 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
4069 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4070 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4071}
4072
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004073// --- KeyboardInputMapperTest_ExternalDevice ---
4074
4075class KeyboardInputMapperTest_ExternalDevice : public InputMapperTest {
4076protected:
Chris Yea52ade12020-08-27 16:49:20 -07004077 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004078};
4079
4080TEST_F(KeyboardInputMapperTest_ExternalDevice, WakeBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07004081 // For external devices, non-media keys will trigger wake on key down. Media keys need to be
4082 // marked as WAKE in the keylayout file to trigger wake.
Powei Fengd041c5d2019-05-03 17:11:33 -07004083
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004084 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, 0);
4085 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, 0);
4086 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAYPAUSE, 0, AKEYCODE_MEDIA_PLAY_PAUSE,
4087 POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07004088
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004089 KeyboardInputMapper& mapper =
4090 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4091 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07004092
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004093 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004094 NotifyKeyArgs args;
4095 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4096 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4097
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004098 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004099 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4100 ASSERT_EQ(uint32_t(0), args.policyFlags);
4101
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004102 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004103 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4104 ASSERT_EQ(uint32_t(0), args.policyFlags);
4105
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004106 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004107 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4108 ASSERT_EQ(uint32_t(0), args.policyFlags);
4109
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004110 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004111 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4112 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4113
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004114 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004115 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4116 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4117}
4118
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004119TEST_F(KeyboardInputMapperTest_ExternalDevice, DoNotWakeByDefaultBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07004120 // Tv Remote key's wake behavior is prescribed by the keylayout file.
Powei Fengd041c5d2019-05-03 17:11:33 -07004121
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004122 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4123 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
4124 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07004125
Powei Fengd041c5d2019-05-03 17:11:33 -07004126 addConfigurationProperty("keyboard.doNotWakeByDefault", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004127 KeyboardInputMapper& mapper =
4128 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4129 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07004130
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004131 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004132 NotifyKeyArgs args;
4133 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4134 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4135
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004136 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004137 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4138 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4139
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004140 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_DOWN, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004141 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4142 ASSERT_EQ(uint32_t(0), args.policyFlags);
4143
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004144 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_DOWN, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004145 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4146 ASSERT_EQ(uint32_t(0), args.policyFlags);
4147
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004148 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004149 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4150 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4151
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004152 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004153 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4154 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4155}
4156
Michael Wrightd02c5b62014-02-10 15:10:22 -08004157// --- CursorInputMapperTest ---
4158
4159class CursorInputMapperTest : public InputMapperTest {
4160protected:
4161 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD;
4162
Michael Wright17db18e2020-06-26 20:51:44 +01004163 std::shared_ptr<FakePointerController> mFakePointerController;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004164
Chris Yea52ade12020-08-27 16:49:20 -07004165 void SetUp() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004166 InputMapperTest::SetUp();
4167
Michael Wright17db18e2020-06-26 20:51:44 +01004168 mFakePointerController = std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00004169 mFakePolicy->setPointerController(mFakePointerController);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004170 }
4171
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004172 void testMotionRotation(CursorInputMapper& mapper, int32_t originalX, int32_t originalY,
4173 int32_t rotatedX, int32_t rotatedY);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004174
4175 void prepareDisplay(int32_t orientation) {
4176 const std::string uniqueId = "local:0";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004177 const ViewportType viewportType = ViewportType::INTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004178 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
4179 orientation, uniqueId, NO_PORT, viewportType);
4180 }
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004181
4182 static void assertCursorPointerCoords(const PointerCoords& coords, float x, float y,
4183 float pressure) {
4184 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(coords, x, y, pressure, 0.0f, 0.0f, 0.0f, 0.0f,
4185 0.0f, 0.0f, 0.0f, EPSILON));
4186 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08004187};
4188
4189const int32_t CursorInputMapperTest::TRACKBALL_MOVEMENT_THRESHOLD = 6;
4190
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004191void CursorInputMapperTest::testMotionRotation(CursorInputMapper& mapper, int32_t originalX,
4192 int32_t originalY, int32_t rotatedX,
4193 int32_t rotatedY) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004194 NotifyMotionArgs args;
4195
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004196 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, originalX);
4197 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, originalY);
4198 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004199 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4200 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004201 ASSERT_NO_FATAL_FAILURE(
4202 assertCursorPointerCoords(args.pointerCoords[0],
4203 float(rotatedX) / TRACKBALL_MOVEMENT_THRESHOLD,
4204 float(rotatedY) / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004205}
4206
4207TEST_F(CursorInputMapperTest, WhenModeIsPointer_GetSources_ReturnsMouse) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004208 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004209 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004210
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004211 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004212}
4213
4214TEST_F(CursorInputMapperTest, WhenModeIsNavigation_GetSources_ReturnsTrackball) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004215 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004216 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004217
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004218 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004219}
4220
4221TEST_F(CursorInputMapperTest, WhenModeIsPointer_PopulateDeviceInfo_ReturnsRangeFromPointerController) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004222 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004223 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004224
4225 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004226 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004227
4228 // Initially there may not be a valid motion range.
Yi Kong9b14ac62018-07-17 13:48:38 -07004229 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE));
4230 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004231 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4232 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE, 0.0f, 1.0f, 0.0f, 0.0f));
4233
4234 // When the bounds are set, then there should be a valid motion range.
4235 mFakePointerController->setBounds(1, 2, 800 - 1, 480 - 1);
4236
4237 InputDeviceInfo info2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004238 mapper.populateDeviceInfo(&info2);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004239
4240 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
4241 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE,
4242 1, 800 - 1, 0.0f, 0.0f));
4243 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
4244 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE,
4245 2, 480 - 1, 0.0f, 0.0f));
4246 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
4247 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE,
4248 0.0f, 1.0f, 0.0f, 0.0f));
4249}
4250
4251TEST_F(CursorInputMapperTest, WhenModeIsNavigation_PopulateDeviceInfo_ReturnsScaledRange) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004252 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004253 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004254
4255 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004256 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004257
4258 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4259 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_TRACKBALL,
4260 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
4261 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4262 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_TRACKBALL,
4263 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
4264 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4265 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TRACKBALL,
4266 0.0f, 1.0f, 0.0f, 0.0f));
4267}
4268
4269TEST_F(CursorInputMapperTest, Process_ShouldSetAllFieldsAndIncludeGlobalMetaState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004270 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004271 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004272
arthurhungdcef2dc2020-08-11 14:47:50 +08004273 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004274
4275 NotifyMotionArgs args;
4276
4277 // Button press.
4278 // Mostly testing non x/y behavior here so we don't need to check again elsewhere.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004279 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4280 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004281 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4282 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4283 ASSERT_EQ(DEVICE_ID, args.deviceId);
4284 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4285 ASSERT_EQ(uint32_t(0), args.policyFlags);
4286 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
4287 ASSERT_EQ(0, args.flags);
4288 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4289 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
4290 ASSERT_EQ(0, args.edgeFlags);
4291 ASSERT_EQ(uint32_t(1), args.pointerCount);
4292 ASSERT_EQ(0, args.pointerProperties[0].id);
4293 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004294 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004295 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4296 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4297 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4298
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004299 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4300 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4301 ASSERT_EQ(DEVICE_ID, args.deviceId);
4302 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4303 ASSERT_EQ(uint32_t(0), args.policyFlags);
4304 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
4305 ASSERT_EQ(0, args.flags);
4306 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4307 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
4308 ASSERT_EQ(0, args.edgeFlags);
4309 ASSERT_EQ(uint32_t(1), args.pointerCount);
4310 ASSERT_EQ(0, args.pointerProperties[0].id);
4311 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004312 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004313 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4314 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4315 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4316
Michael Wrightd02c5b62014-02-10 15:10:22 -08004317 // Button release. Should have same down time.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004318 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4319 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004320 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4321 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
4322 ASSERT_EQ(DEVICE_ID, args.deviceId);
4323 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4324 ASSERT_EQ(uint32_t(0), args.policyFlags);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004325 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
4326 ASSERT_EQ(0, args.flags);
4327 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4328 ASSERT_EQ(0, args.buttonState);
4329 ASSERT_EQ(0, args.edgeFlags);
4330 ASSERT_EQ(uint32_t(1), args.pointerCount);
4331 ASSERT_EQ(0, args.pointerProperties[0].id);
4332 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004333 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004334 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4335 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4336 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4337
4338 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4339 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
4340 ASSERT_EQ(DEVICE_ID, args.deviceId);
4341 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4342 ASSERT_EQ(uint32_t(0), args.policyFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004343 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
4344 ASSERT_EQ(0, args.flags);
4345 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4346 ASSERT_EQ(0, args.buttonState);
4347 ASSERT_EQ(0, args.edgeFlags);
4348 ASSERT_EQ(uint32_t(1), args.pointerCount);
4349 ASSERT_EQ(0, args.pointerProperties[0].id);
4350 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004351 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004352 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4353 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4354 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4355}
4356
4357TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentXYUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004358 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004359 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004360
4361 NotifyMotionArgs args;
4362
4363 // Motion in X but not Y.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004364 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4365 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004366 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4367 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004368 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4369 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f,
4370 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004371
4372 // Motion in Y but not X.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004373 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
4374 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004375 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4376 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004377 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f,
4378 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004379}
4380
4381TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004382 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004383 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004384
4385 NotifyMotionArgs args;
4386
4387 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004388 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4389 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004390 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4391 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004392 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004393
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004394 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4395 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004396 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004397
Michael Wrightd02c5b62014-02-10 15:10:22 -08004398 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004399 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4400 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004401 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004402 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004403 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004404
4405 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004406 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004407 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004408}
4409
4410TEST_F(CursorInputMapperTest, Process_ShouldHandleCombinedXYAndButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004411 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004412 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004413
4414 NotifyMotionArgs args;
4415
4416 // Combined X, Y and Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004417 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4418 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
4419 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4420 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004421 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4422 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004423 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4424 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4425 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004426
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004427 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4428 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004429 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4430 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4431 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004432
Michael Wrightd02c5b62014-02-10 15:10:22 -08004433 // Move X, Y a bit while pressed.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004434 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 2);
4435 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 1);
4436 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004437 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4438 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004439 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4440 2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4441 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004442
4443 // Release Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004444 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4445 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004446 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004447 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004448 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004449
4450 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004451 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004452 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004453}
4454
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004455TEST_F(CursorInputMapperTest, Process_WhenOrientationAware_ShouldNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004456 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004457 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
4458 // need to be rotated.
4459 addConfigurationProperty("cursor.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004460 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004461
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004462 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004463 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4464 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4465 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4466 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4467 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4468 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4469 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4470 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4471}
4472
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004473TEST_F(CursorInputMapperTest, Process_WhenNotOrientationAware_ShouldRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004474 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004475 // Since InputReader works in the un-rotated coordinate space, only devices that are not
4476 // orientation-aware are affected by display rotation.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004477 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004478
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004479 prepareDisplay(DISPLAY_ORIENTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004480 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4481 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4482 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4483 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4484 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4485 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4486 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4487 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4488
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004489 prepareDisplay(DISPLAY_ORIENTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004490 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, -1, 0));
4491 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, 1));
4492 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, 1));
4493 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, 1));
4494 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 1, 0));
4495 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, -1));
4496 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, -1));
4497 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, -1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004498
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004499 prepareDisplay(DISPLAY_ORIENTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004500 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, -1));
4501 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, -1));
4502 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, -1, 0));
4503 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, 1));
4504 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, 1));
4505 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, 1));
4506 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 1, 0));
4507 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, -1));
4508
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004509 prepareDisplay(DISPLAY_ORIENTATION_270);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004510 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 1, 0));
4511 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, -1));
4512 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, -1));
4513 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, -1));
4514 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, -1, 0));
4515 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, 1));
4516 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, 1));
4517 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, 1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004518}
4519
4520TEST_F(CursorInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004521 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004522 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004523
4524 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4525 mFakePointerController->setPosition(100, 200);
4526 mFakePointerController->setButtonState(0);
4527
4528 NotifyMotionArgs motionArgs;
4529 NotifyKeyArgs keyArgs;
4530
4531 // press BTN_LEFT, release BTN_LEFT
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004532 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 1);
4533 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004534 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4535 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4536 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4537 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004538 ASSERT_NO_FATAL_FAILURE(
4539 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004540
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004541 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4542 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4543 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4544 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004545 ASSERT_NO_FATAL_FAILURE(
4546 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004547
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004548 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 0);
4549 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004550 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004551 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004552 ASSERT_EQ(0, motionArgs.buttonState);
4553 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004554 ASSERT_NO_FATAL_FAILURE(
4555 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004556
4557 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004558 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004559 ASSERT_EQ(0, motionArgs.buttonState);
4560 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004561 ASSERT_NO_FATAL_FAILURE(
4562 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004563
4564 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004565 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004566 ASSERT_EQ(0, motionArgs.buttonState);
4567 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004568 ASSERT_NO_FATAL_FAILURE(
4569 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004570
4571 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004572 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 1);
4573 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 1);
4574 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004575 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4576 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4577 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4578 motionArgs.buttonState);
4579 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4580 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004581 ASSERT_NO_FATAL_FAILURE(
4582 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004583
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004584 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4585 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4586 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4587 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4588 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004589 ASSERT_NO_FATAL_FAILURE(
4590 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004591
4592 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4593 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4594 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4595 motionArgs.buttonState);
4596 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4597 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004598 ASSERT_NO_FATAL_FAILURE(
4599 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004600
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004601 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 0);
4602 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004603 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004604 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004605 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4606 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004607 ASSERT_NO_FATAL_FAILURE(
4608 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004609
4610 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004611 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004612 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4613 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004614 ASSERT_NO_FATAL_FAILURE(
4615 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004616
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004617 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4618 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004619 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004620 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
4621 ASSERT_EQ(0, motionArgs.buttonState);
4622 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004623 ASSERT_NO_FATAL_FAILURE(
4624 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004625 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4626 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004627
4628 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004629 ASSERT_EQ(0, motionArgs.buttonState);
4630 ASSERT_EQ(0, mFakePointerController->getButtonState());
4631 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004632 ASSERT_NO_FATAL_FAILURE(
4633 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004634
Michael Wrightd02c5b62014-02-10 15:10:22 -08004635 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4636 ASSERT_EQ(0, motionArgs.buttonState);
4637 ASSERT_EQ(0, mFakePointerController->getButtonState());
4638 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004639 ASSERT_NO_FATAL_FAILURE(
4640 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004641
4642 // press BTN_BACK, release BTN_BACK
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004643 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 1);
4644 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004645 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4646 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4647 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004648
Michael Wrightd02c5b62014-02-10 15:10:22 -08004649 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004650 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004651 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4652 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004653 ASSERT_NO_FATAL_FAILURE(
4654 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004655
4656 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4657 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4658 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4659 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004660 ASSERT_NO_FATAL_FAILURE(
4661 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004662
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004663 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 0);
4664 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004665 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004666 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004667 ASSERT_EQ(0, motionArgs.buttonState);
4668 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004669 ASSERT_NO_FATAL_FAILURE(
4670 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004671
4672 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004673 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004674 ASSERT_EQ(0, motionArgs.buttonState);
4675 ASSERT_EQ(0, mFakePointerController->getButtonState());
4676
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004677 ASSERT_NO_FATAL_FAILURE(
4678 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004679 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4680 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4681 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4682
4683 // press BTN_SIDE, release BTN_SIDE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004684 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 1);
4685 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004686 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4687 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4688 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004689
Michael Wrightd02c5b62014-02-10 15:10:22 -08004690 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004691 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004692 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4693 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004694 ASSERT_NO_FATAL_FAILURE(
4695 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004696
4697 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4698 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4699 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4700 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004701 ASSERT_NO_FATAL_FAILURE(
4702 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004703
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004704 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 0);
4705 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004706 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004707 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004708 ASSERT_EQ(0, motionArgs.buttonState);
4709 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004710 ASSERT_NO_FATAL_FAILURE(
4711 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004712
4713 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4714 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4715 ASSERT_EQ(0, motionArgs.buttonState);
4716 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004717 ASSERT_NO_FATAL_FAILURE(
4718 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004719
Michael Wrightd02c5b62014-02-10 15:10:22 -08004720 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4721 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4722 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4723
4724 // press BTN_FORWARD, release BTN_FORWARD
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004725 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 1);
4726 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004727 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4728 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4729 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004730
Michael Wrightd02c5b62014-02-10 15:10:22 -08004731 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004732 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004733 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4734 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004735 ASSERT_NO_FATAL_FAILURE(
4736 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004737
4738 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4739 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4740 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4741 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004742 ASSERT_NO_FATAL_FAILURE(
4743 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004744
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004745 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 0);
4746 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004747 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004748 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004749 ASSERT_EQ(0, motionArgs.buttonState);
4750 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004751 ASSERT_NO_FATAL_FAILURE(
4752 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004753
4754 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4755 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4756 ASSERT_EQ(0, motionArgs.buttonState);
4757 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004758 ASSERT_NO_FATAL_FAILURE(
4759 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004760
Michael Wrightd02c5b62014-02-10 15:10:22 -08004761 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4762 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4763 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4764
4765 // press BTN_EXTRA, release BTN_EXTRA
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004766 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 1);
4767 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004768 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4769 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4770 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004771
Michael Wrightd02c5b62014-02-10 15:10:22 -08004772 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004773 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004774 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4775 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004776 ASSERT_NO_FATAL_FAILURE(
4777 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004778
4779 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4780 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4781 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4782 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004783 ASSERT_NO_FATAL_FAILURE(
4784 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004785
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004786 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 0);
4787 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004788 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004789 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004790 ASSERT_EQ(0, motionArgs.buttonState);
4791 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004792 ASSERT_NO_FATAL_FAILURE(
4793 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004794
4795 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4796 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4797 ASSERT_EQ(0, motionArgs.buttonState);
4798 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004799 ASSERT_NO_FATAL_FAILURE(
4800 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004801
Michael Wrightd02c5b62014-02-10 15:10:22 -08004802 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4803 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4804 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4805}
4806
4807TEST_F(CursorInputMapperTest, Process_WhenModeIsPointer_ShouldMoveThePointerAround) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004808 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004809 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004810
4811 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4812 mFakePointerController->setPosition(100, 200);
4813 mFakePointerController->setButtonState(0);
4814
4815 NotifyMotionArgs args;
4816
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004817 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4818 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4819 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004820 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004821 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4822 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4823 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4824 110.0f, 220.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
Michael Wright17db18e2020-06-26 20:51:44 +01004825 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004826}
4827
4828TEST_F(CursorInputMapperTest, Process_PointerCapture) {
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004829 addConfigurationProperty("cursor.mode", "pointer");
4830 mFakePolicy->setPointerCapture(true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004831 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004832
4833 NotifyDeviceResetArgs resetArgs;
4834 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4835 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4836 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4837
4838 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4839 mFakePointerController->setPosition(100, 200);
4840 mFakePointerController->setButtonState(0);
4841
4842 NotifyMotionArgs args;
4843
4844 // Move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004845 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4846 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4847 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004848 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4849 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4850 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4851 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4852 10.0f, 20.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
Michael Wright17db18e2020-06-26 20:51:44 +01004853 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004854
4855 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004856 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4857 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004858 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4859 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4860 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
4861 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4862 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4863 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4864 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4865 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
4866 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4867 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4868
4869 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004870 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4871 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004872 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4873 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4874 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
4875 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4876 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4877 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4878 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4879 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
4880 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4881 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4882
4883 // Another move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004884 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 30);
4885 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 40);
4886 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004887 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4888 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4889 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4890 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4891 30.0f, 40.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
Michael Wright17db18e2020-06-26 20:51:44 +01004892 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004893
4894 // Disable pointer capture and check that the device generation got bumped
4895 // and events are generated the usual way.
arthurhungdcef2dc2020-08-11 14:47:50 +08004896 const uint32_t generation = mReader->getContext()->getGeneration();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004897 mFakePolicy->setPointerCapture(false);
4898 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
arthurhungdcef2dc2020-08-11 14:47:50 +08004899 ASSERT_TRUE(mReader->getContext()->getGeneration() != generation);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004900
4901 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4902 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4903 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4904
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004905 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4906 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4907 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004908 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4909 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004910 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4911 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4912 110.0f, 220.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
Michael Wright17db18e2020-06-26 20:51:44 +01004913 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004914}
4915
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004916TEST_F(CursorInputMapperTest, Process_ShouldHandleDisplayId) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004917 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004918
Garfield Tan888a6a42020-01-09 11:39:16 -08004919 // Setup for second display.
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004920 constexpr int32_t SECOND_DISPLAY_ID = 1;
Garfield Tan888a6a42020-01-09 11:39:16 -08004921 const std::string SECOND_DISPLAY_UNIQUE_ID = "local:1";
4922 mFakePolicy->addDisplayViewport(SECOND_DISPLAY_ID, 800, 480, DISPLAY_ORIENTATION_0,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00004923 true /*isActive*/, SECOND_DISPLAY_UNIQUE_ID, NO_PORT,
4924 ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08004925 mFakePolicy->setDefaultPointerDisplayId(SECOND_DISPLAY_ID);
4926 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
4927
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004928 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4929 mFakePointerController->setPosition(100, 200);
4930 mFakePointerController->setButtonState(0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004931
4932 NotifyMotionArgs args;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004933 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4934 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4935 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004936 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4937 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4938 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4939 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4940 110.0f, 220.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
Michael Wright17db18e2020-06-26 20:51:44 +01004941 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004942 ASSERT_EQ(SECOND_DISPLAY_ID, args.displayId);
4943}
4944
Michael Wrightd02c5b62014-02-10 15:10:22 -08004945// --- TouchInputMapperTest ---
4946
4947class TouchInputMapperTest : public InputMapperTest {
4948protected:
4949 static const int32_t RAW_X_MIN;
4950 static const int32_t RAW_X_MAX;
4951 static const int32_t RAW_Y_MIN;
4952 static const int32_t RAW_Y_MAX;
4953 static const int32_t RAW_TOUCH_MIN;
4954 static const int32_t RAW_TOUCH_MAX;
4955 static const int32_t RAW_TOOL_MIN;
4956 static const int32_t RAW_TOOL_MAX;
4957 static const int32_t RAW_PRESSURE_MIN;
4958 static const int32_t RAW_PRESSURE_MAX;
4959 static const int32_t RAW_ORIENTATION_MIN;
4960 static const int32_t RAW_ORIENTATION_MAX;
4961 static const int32_t RAW_DISTANCE_MIN;
4962 static const int32_t RAW_DISTANCE_MAX;
4963 static const int32_t RAW_TILT_MIN;
4964 static const int32_t RAW_TILT_MAX;
4965 static const int32_t RAW_ID_MIN;
4966 static const int32_t RAW_ID_MAX;
4967 static const int32_t RAW_SLOT_MIN;
4968 static const int32_t RAW_SLOT_MAX;
4969 static const float X_PRECISION;
4970 static const float Y_PRECISION;
Santos Cordonfa5cf462017-04-05 10:37:00 -07004971 static const float X_PRECISION_VIRTUAL;
4972 static const float Y_PRECISION_VIRTUAL;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004973
4974 static const float GEOMETRIC_SCALE;
Jason Gerecke489fda82012-09-07 17:19:40 -07004975 static const TouchAffineTransformation AFFINE_TRANSFORM;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004976
4977 static const VirtualKeyDefinition VIRTUAL_KEYS[2];
4978
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004979 const std::string UNIQUE_ID = "local:0";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004980 const std::string SECONDARY_UNIQUE_ID = "local:1";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004981
Michael Wrightd02c5b62014-02-10 15:10:22 -08004982 enum Axes {
4983 POSITION = 1 << 0,
4984 TOUCH = 1 << 1,
4985 TOOL = 1 << 2,
4986 PRESSURE = 1 << 3,
4987 ORIENTATION = 1 << 4,
4988 MINOR = 1 << 5,
4989 ID = 1 << 6,
4990 DISTANCE = 1 << 7,
4991 TILT = 1 << 8,
4992 SLOT = 1 << 9,
4993 TOOL_TYPE = 1 << 10,
4994 };
4995
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004996 void prepareDisplay(int32_t orientation, std::optional<uint8_t> port = NO_PORT);
4997 void prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port = NO_PORT);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004998 void prepareVirtualDisplay(int32_t orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004999 void prepareVirtualKeys();
Jason Gerecke489fda82012-09-07 17:19:40 -07005000 void prepareLocationCalibration();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005001 int32_t toRawX(float displayX);
5002 int32_t toRawY(float displayY);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005003 int32_t toRotatedRawX(float displayX);
5004 int32_t toRotatedRawY(float displayY);
Jason Gerecke489fda82012-09-07 17:19:40 -07005005 float toCookedX(float rawX, float rawY);
5006 float toCookedY(float rawX, float rawY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005007 float toDisplayX(int32_t rawX);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005008 float toDisplayX(int32_t rawX, int32_t displayWidth);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005009 float toDisplayY(int32_t rawY);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005010 float toDisplayY(int32_t rawY, int32_t displayHeight);
5011
Michael Wrightd02c5b62014-02-10 15:10:22 -08005012};
5013
5014const int32_t TouchInputMapperTest::RAW_X_MIN = 25;
5015const int32_t TouchInputMapperTest::RAW_X_MAX = 1019;
5016const int32_t TouchInputMapperTest::RAW_Y_MIN = 30;
5017const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009;
5018const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0;
5019const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31;
5020const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0;
5021const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15;
Michael Wrightaa449c92017-12-13 21:21:43 +00005022const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = 0;
5023const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = 255;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005024const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7;
5025const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
5026const int32_t TouchInputMapperTest::RAW_DISTANCE_MIN = 0;
5027const int32_t TouchInputMapperTest::RAW_DISTANCE_MAX = 7;
5028const int32_t TouchInputMapperTest::RAW_TILT_MIN = 0;
5029const int32_t TouchInputMapperTest::RAW_TILT_MAX = 150;
5030const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
5031const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
5032const int32_t TouchInputMapperTest::RAW_SLOT_MIN = 0;
5033const int32_t TouchInputMapperTest::RAW_SLOT_MAX = 9;
5034const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH;
5035const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT;
Santos Cordonfa5cf462017-04-05 10:37:00 -07005036const float TouchInputMapperTest::X_PRECISION_VIRTUAL =
5037 float(RAW_X_MAX - RAW_X_MIN + 1) / VIRTUAL_DISPLAY_WIDTH;
5038const float TouchInputMapperTest::Y_PRECISION_VIRTUAL =
5039 float(RAW_Y_MAX - RAW_Y_MIN + 1) / VIRTUAL_DISPLAY_HEIGHT;
Jason Gerecke489fda82012-09-07 17:19:40 -07005040const TouchAffineTransformation TouchInputMapperTest::AFFINE_TRANSFORM =
5041 TouchAffineTransformation(1, -2, 3, -4, 5, -6);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005042
5043const float TouchInputMapperTest::GEOMETRIC_SCALE =
5044 avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1),
5045 float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1));
5046
5047const VirtualKeyDefinition TouchInputMapperTest::VIRTUAL_KEYS[2] = {
5048 { KEY_HOME, 60, DISPLAY_HEIGHT + 15, 20, 20 },
5049 { KEY_MENU, DISPLAY_HEIGHT - 60, DISPLAY_WIDTH + 15, 20, 20 },
5050};
5051
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005052void TouchInputMapperTest::prepareDisplay(int32_t orientation, std::optional<uint8_t> port) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01005053 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
5054 port, ViewportType::INTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005055}
5056
5057void TouchInputMapperTest::prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port) {
5058 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
5059 DISPLAY_ORIENTATION_0, SECONDARY_UNIQUE_ID, port, type);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005060}
5061
Santos Cordonfa5cf462017-04-05 10:37:00 -07005062void TouchInputMapperTest::prepareVirtualDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01005063 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH, VIRTUAL_DISPLAY_HEIGHT,
5064 orientation, VIRTUAL_DISPLAY_UNIQUE_ID, NO_PORT,
5065 ViewportType::VIRTUAL);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005066}
5067
Michael Wrightd02c5b62014-02-10 15:10:22 -08005068void TouchInputMapperTest::prepareVirtualKeys() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005069 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[0]);
5070 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[1]);
5071 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
5072 mFakeEventHub->addKey(EVENTHUB_ID, KEY_MENU, 0, AKEYCODE_MENU, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005073}
5074
Jason Gerecke489fda82012-09-07 17:19:40 -07005075void TouchInputMapperTest::prepareLocationCalibration() {
5076 mFakePolicy->setTouchAffineTransformation(AFFINE_TRANSFORM);
5077}
5078
Michael Wrightd02c5b62014-02-10 15:10:22 -08005079int32_t TouchInputMapperTest::toRawX(float displayX) {
5080 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN);
5081}
5082
5083int32_t TouchInputMapperTest::toRawY(float displayY) {
5084 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN);
5085}
5086
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005087int32_t TouchInputMapperTest::toRotatedRawX(float displayX) {
5088 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_HEIGHT + RAW_X_MIN);
5089}
5090
5091int32_t TouchInputMapperTest::toRotatedRawY(float displayY) {
5092 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_WIDTH + RAW_Y_MIN);
5093}
5094
Jason Gerecke489fda82012-09-07 17:19:40 -07005095float TouchInputMapperTest::toCookedX(float rawX, float rawY) {
5096 AFFINE_TRANSFORM.applyTo(rawX, rawY);
5097 return rawX;
5098}
5099
5100float TouchInputMapperTest::toCookedY(float rawX, float rawY) {
5101 AFFINE_TRANSFORM.applyTo(rawX, rawY);
5102 return rawY;
5103}
5104
Michael Wrightd02c5b62014-02-10 15:10:22 -08005105float TouchInputMapperTest::toDisplayX(int32_t rawX) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005106 return toDisplayX(rawX, DISPLAY_WIDTH);
5107}
5108
5109float TouchInputMapperTest::toDisplayX(int32_t rawX, int32_t displayWidth) {
5110 return float(rawX - RAW_X_MIN) * displayWidth / (RAW_X_MAX - RAW_X_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005111}
5112
5113float TouchInputMapperTest::toDisplayY(int32_t rawY) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005114 return toDisplayY(rawY, DISPLAY_HEIGHT);
5115}
5116
5117float TouchInputMapperTest::toDisplayY(int32_t rawY, int32_t displayHeight) {
5118 return float(rawY - RAW_Y_MIN) * displayHeight / (RAW_Y_MAX - RAW_Y_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005119}
5120
5121
5122// --- SingleTouchInputMapperTest ---
5123
5124class SingleTouchInputMapperTest : public TouchInputMapperTest {
5125protected:
5126 void prepareButtons();
5127 void prepareAxes(int axes);
5128
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005129 void processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
5130 void processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
5131 void processUp(SingleTouchInputMapper& mappery);
5132 void processPressure(SingleTouchInputMapper& mapper, int32_t pressure);
5133 void processToolMajor(SingleTouchInputMapper& mapper, int32_t toolMajor);
5134 void processDistance(SingleTouchInputMapper& mapper, int32_t distance);
5135 void processTilt(SingleTouchInputMapper& mapper, int32_t tiltX, int32_t tiltY);
5136 void processKey(SingleTouchInputMapper& mapper, int32_t code, int32_t value);
5137 void processSync(SingleTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005138};
5139
5140void SingleTouchInputMapperTest::prepareButtons() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005141 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005142}
5143
5144void SingleTouchInputMapperTest::prepareAxes(int axes) {
5145 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005146 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
5147 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005148 }
5149 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005150 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MIN,
5151 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005152 }
5153 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005154 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TOOL_WIDTH, RAW_TOOL_MIN, RAW_TOOL_MAX, 0,
5155 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005156 }
5157 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005158 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_DISTANCE, RAW_DISTANCE_MIN,
5159 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005160 }
5161 if (axes & TILT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005162 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_X, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
5163 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_Y, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005164 }
5165}
5166
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005167void SingleTouchInputMapperTest::processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005168 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
5169 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
5170 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005171}
5172
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005173void SingleTouchInputMapperTest::processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005174 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
5175 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005176}
5177
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005178void SingleTouchInputMapperTest::processUp(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005179 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005180}
5181
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005182void SingleTouchInputMapperTest::processPressure(SingleTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005183 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005184}
5185
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005186void SingleTouchInputMapperTest::processToolMajor(SingleTouchInputMapper& mapper,
5187 int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005188 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TOOL_WIDTH, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005189}
5190
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005191void SingleTouchInputMapperTest::processDistance(SingleTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005192 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005193}
5194
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005195void SingleTouchInputMapperTest::processTilt(SingleTouchInputMapper& mapper, int32_t tiltX,
5196 int32_t tiltY) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005197 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_X, tiltX);
5198 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_Y, tiltY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005199}
5200
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005201void SingleTouchInputMapperTest::processKey(SingleTouchInputMapper& mapper, int32_t code,
5202 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005203 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005204}
5205
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005206void SingleTouchInputMapperTest::processSync(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005207 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005208}
5209
Michael Wrightd02c5b62014-02-10 15:10:22 -08005210TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005211 prepareButtons();
5212 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005213 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005214
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005215 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005216}
5217
5218TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndIsACursor_ReturnsTouchPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005219 mFakeEventHub->addRelativeAxis(EVENTHUB_ID, REL_X);
5220 mFakeEventHub->addRelativeAxis(EVENTHUB_ID, REL_Y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005221 prepareButtons();
5222 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005223 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005224
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005225 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005226}
5227
5228TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchPad_ReturnsTouchPad) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005229 prepareButtons();
5230 prepareAxes(POSITION);
5231 addConfigurationProperty("touch.deviceType", "touchPad");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005232 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005233
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005234 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005235}
5236
5237TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005238 prepareButtons();
5239 prepareAxes(POSITION);
5240 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005241 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005242
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005243 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005244}
5245
5246TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005247 addConfigurationProperty("touch.deviceType", "touchScreen");
5248 prepareDisplay(DISPLAY_ORIENTATION_0);
5249 prepareButtons();
5250 prepareAxes(POSITION);
5251 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005252 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005253
5254 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005255 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005256
5257 // Virtual key is down.
5258 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5259 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5260 processDown(mapper, x, y);
5261 processSync(mapper);
5262 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5263
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005264 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005265
5266 // Virtual key is up.
5267 processUp(mapper);
5268 processSync(mapper);
5269 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5270
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005271 ASSERT_EQ(AKEY_STATE_UP, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005272}
5273
5274TEST_F(SingleTouchInputMapperTest, GetScanCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005275 addConfigurationProperty("touch.deviceType", "touchScreen");
5276 prepareDisplay(DISPLAY_ORIENTATION_0);
5277 prepareButtons();
5278 prepareAxes(POSITION);
5279 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005280 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005281
5282 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005283 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005284
5285 // Virtual key is down.
5286 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5287 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5288 processDown(mapper, x, y);
5289 processSync(mapper);
5290 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5291
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005292 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005293
5294 // Virtual key is up.
5295 processUp(mapper);
5296 processSync(mapper);
5297 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5298
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005299 ASSERT_EQ(AKEY_STATE_UP, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005300}
5301
5302TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005303 addConfigurationProperty("touch.deviceType", "touchScreen");
5304 prepareDisplay(DISPLAY_ORIENTATION_0);
5305 prepareButtons();
5306 prepareAxes(POSITION);
5307 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005308 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005309
5310 const int32_t keys[2] = { AKEYCODE_HOME, AKEYCODE_A };
5311 uint8_t flags[2] = { 0, 0 };
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005312 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, 2, keys, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005313 ASSERT_TRUE(flags[0]);
5314 ASSERT_FALSE(flags[1]);
5315}
5316
5317TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005318 addConfigurationProperty("touch.deviceType", "touchScreen");
5319 prepareDisplay(DISPLAY_ORIENTATION_0);
5320 prepareButtons();
5321 prepareAxes(POSITION);
5322 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005323 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005324
arthurhungdcef2dc2020-08-11 14:47:50 +08005325 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005326
5327 NotifyKeyArgs args;
5328
5329 // Press virtual key.
5330 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5331 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5332 processDown(mapper, x, y);
5333 processSync(mapper);
5334
5335 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
5336 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5337 ASSERT_EQ(DEVICE_ID, args.deviceId);
5338 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
5339 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
5340 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
5341 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5342 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5343 ASSERT_EQ(KEY_HOME, args.scanCode);
5344 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5345 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5346
5347 // Release virtual key.
5348 processUp(mapper);
5349 processSync(mapper);
5350
5351 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
5352 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5353 ASSERT_EQ(DEVICE_ID, args.deviceId);
5354 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
5355 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
5356 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
5357 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5358 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5359 ASSERT_EQ(KEY_HOME, args.scanCode);
5360 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5361 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5362
5363 // Should not have sent any motions.
5364 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5365}
5366
5367TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005368 addConfigurationProperty("touch.deviceType", "touchScreen");
5369 prepareDisplay(DISPLAY_ORIENTATION_0);
5370 prepareButtons();
5371 prepareAxes(POSITION);
5372 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005373 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005374
arthurhungdcef2dc2020-08-11 14:47:50 +08005375 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005376
5377 NotifyKeyArgs keyArgs;
5378
5379 // Press virtual key.
5380 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5381 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5382 processDown(mapper, x, y);
5383 processSync(mapper);
5384
5385 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5386 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5387 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5388 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5389 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5390 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5391 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags);
5392 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5393 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5394 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5395 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5396
5397 // Move out of bounds. This should generate a cancel and a pointer down since we moved
5398 // into the display area.
5399 y -= 100;
5400 processMove(mapper, x, y);
5401 processSync(mapper);
5402
5403 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5404 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5405 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5406 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5407 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5408 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5409 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
5410 | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags);
5411 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5412 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5413 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5414 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5415
5416 NotifyMotionArgs motionArgs;
5417 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5418 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5419 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5420 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5421 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5422 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5423 ASSERT_EQ(0, motionArgs.flags);
5424 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5425 ASSERT_EQ(0, motionArgs.buttonState);
5426 ASSERT_EQ(0, motionArgs.edgeFlags);
5427 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5428 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5429 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5430 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5431 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5432 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5433 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5434 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5435
5436 // Keep moving out of bounds. Should generate a pointer move.
5437 y -= 50;
5438 processMove(mapper, x, y);
5439 processSync(mapper);
5440
5441 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5442 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5443 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5444 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5445 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5446 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5447 ASSERT_EQ(0, motionArgs.flags);
5448 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5449 ASSERT_EQ(0, motionArgs.buttonState);
5450 ASSERT_EQ(0, motionArgs.edgeFlags);
5451 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5452 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5453 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5454 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5455 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5456 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5457 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5458 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5459
5460 // Release out of bounds. Should generate a pointer up.
5461 processUp(mapper);
5462 processSync(mapper);
5463
5464 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5465 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5466 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5467 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5468 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5469 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5470 ASSERT_EQ(0, motionArgs.flags);
5471 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5472 ASSERT_EQ(0, motionArgs.buttonState);
5473 ASSERT_EQ(0, motionArgs.edgeFlags);
5474 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5475 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5476 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5477 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5478 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5479 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5480 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5481 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5482
5483 // Should not have sent any more keys or motions.
5484 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5485 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5486}
5487
5488TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005489 addConfigurationProperty("touch.deviceType", "touchScreen");
5490 prepareDisplay(DISPLAY_ORIENTATION_0);
5491 prepareButtons();
5492 prepareAxes(POSITION);
5493 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005494 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005495
arthurhungdcef2dc2020-08-11 14:47:50 +08005496 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005497
5498 NotifyMotionArgs motionArgs;
5499
5500 // Initially go down out of bounds.
5501 int32_t x = -10;
5502 int32_t y = -10;
5503 processDown(mapper, x, y);
5504 processSync(mapper);
5505
5506 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5507
5508 // Move into the display area. Should generate a pointer down.
5509 x = 50;
5510 y = 75;
5511 processMove(mapper, x, y);
5512 processSync(mapper);
5513
5514 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5515 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5516 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5517 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5518 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5519 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5520 ASSERT_EQ(0, motionArgs.flags);
5521 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5522 ASSERT_EQ(0, motionArgs.buttonState);
5523 ASSERT_EQ(0, motionArgs.edgeFlags);
5524 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5525 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5526 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5527 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5528 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5529 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5530 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5531 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5532
5533 // Release. Should generate a pointer up.
5534 processUp(mapper);
5535 processSync(mapper);
5536
5537 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5538 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5539 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5540 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5541 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5542 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5543 ASSERT_EQ(0, motionArgs.flags);
5544 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5545 ASSERT_EQ(0, motionArgs.buttonState);
5546 ASSERT_EQ(0, motionArgs.edgeFlags);
5547 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5548 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5549 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5550 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5551 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5552 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5553 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5554 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5555
5556 // Should not have sent any more keys or motions.
5557 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5558 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5559}
5560
Santos Cordonfa5cf462017-04-05 10:37:00 -07005561TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture_VirtualDisplay) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005562 addConfigurationProperty("touch.deviceType", "touchScreen");
5563 addConfigurationProperty("touch.displayId", VIRTUAL_DISPLAY_UNIQUE_ID);
5564
5565 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
5566 prepareButtons();
5567 prepareAxes(POSITION);
5568 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005569 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Santos Cordonfa5cf462017-04-05 10:37:00 -07005570
arthurhungdcef2dc2020-08-11 14:47:50 +08005571 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005572
5573 NotifyMotionArgs motionArgs;
5574
5575 // Down.
5576 int32_t x = 100;
5577 int32_t y = 125;
5578 processDown(mapper, x, y);
5579 processSync(mapper);
5580
5581 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5582 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5583 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5584 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5585 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5586 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5587 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5588 ASSERT_EQ(0, motionArgs.flags);
5589 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5590 ASSERT_EQ(0, motionArgs.buttonState);
5591 ASSERT_EQ(0, motionArgs.edgeFlags);
5592 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5593 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5594 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5595 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5596 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5597 1, 0, 0, 0, 0, 0, 0, 0));
5598 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5599 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5600 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5601
5602 // Move.
5603 x += 50;
5604 y += 75;
5605 processMove(mapper, x, y);
5606 processSync(mapper);
5607
5608 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5609 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5610 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5611 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5612 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5613 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5614 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5615 ASSERT_EQ(0, motionArgs.flags);
5616 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5617 ASSERT_EQ(0, motionArgs.buttonState);
5618 ASSERT_EQ(0, motionArgs.edgeFlags);
5619 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5620 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5621 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5622 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5623 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5624 1, 0, 0, 0, 0, 0, 0, 0));
5625 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5626 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5627 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5628
5629 // Up.
5630 processUp(mapper);
5631 processSync(mapper);
5632
5633 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5634 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5635 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5636 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5637 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5638 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5639 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5640 ASSERT_EQ(0, motionArgs.flags);
5641 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5642 ASSERT_EQ(0, motionArgs.buttonState);
5643 ASSERT_EQ(0, motionArgs.edgeFlags);
5644 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5645 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5646 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5647 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5648 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5649 1, 0, 0, 0, 0, 0, 0, 0));
5650 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5651 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5652 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5653
5654 // Should not have sent any more keys or motions.
5655 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5656 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5657}
5658
Michael Wrightd02c5b62014-02-10 15:10:22 -08005659TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005660 addConfigurationProperty("touch.deviceType", "touchScreen");
5661 prepareDisplay(DISPLAY_ORIENTATION_0);
5662 prepareButtons();
5663 prepareAxes(POSITION);
5664 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005665 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005666
arthurhungdcef2dc2020-08-11 14:47:50 +08005667 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005668
5669 NotifyMotionArgs motionArgs;
5670
5671 // Down.
5672 int32_t x = 100;
5673 int32_t y = 125;
5674 processDown(mapper, x, y);
5675 processSync(mapper);
5676
5677 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5678 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5679 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5680 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5681 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5682 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5683 ASSERT_EQ(0, motionArgs.flags);
5684 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5685 ASSERT_EQ(0, motionArgs.buttonState);
5686 ASSERT_EQ(0, motionArgs.edgeFlags);
5687 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5688 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5689 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5690 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5691 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5692 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5693 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5694 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5695
5696 // Move.
5697 x += 50;
5698 y += 75;
5699 processMove(mapper, x, y);
5700 processSync(mapper);
5701
5702 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5703 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5704 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5705 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5706 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5707 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5708 ASSERT_EQ(0, motionArgs.flags);
5709 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5710 ASSERT_EQ(0, motionArgs.buttonState);
5711 ASSERT_EQ(0, motionArgs.edgeFlags);
5712 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5713 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5714 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5715 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5716 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5717 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5718 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5719 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5720
5721 // Up.
5722 processUp(mapper);
5723 processSync(mapper);
5724
5725 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5726 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5727 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5728 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5729 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5730 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5731 ASSERT_EQ(0, motionArgs.flags);
5732 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5733 ASSERT_EQ(0, motionArgs.buttonState);
5734 ASSERT_EQ(0, motionArgs.edgeFlags);
5735 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5736 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5737 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5738 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5739 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5740 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5741 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5742 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5743
5744 // Should not have sent any more keys or motions.
5745 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5746 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5747}
5748
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005749TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_DoesNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005750 addConfigurationProperty("touch.deviceType", "touchScreen");
5751 prepareButtons();
5752 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005753 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
5754 // need to be rotated. Touchscreens are orientation-aware by default.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005755 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005756
5757 NotifyMotionArgs args;
5758
5759 // Rotation 90.
5760 prepareDisplay(DISPLAY_ORIENTATION_90);
5761 processDown(mapper, toRawX(50), toRawY(75));
5762 processSync(mapper);
5763
5764 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5765 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5766 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5767
5768 processUp(mapper);
5769 processSync(mapper);
5770 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5771}
5772
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005773TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_RotatesMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005774 addConfigurationProperty("touch.deviceType", "touchScreen");
5775 prepareButtons();
5776 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005777 // Since InputReader works in the un-rotated coordinate space, only devices that are not
5778 // orientation-aware are affected by display rotation.
5779 addConfigurationProperty("touch.orientationAware", "0");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005780 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005781
5782 NotifyMotionArgs args;
5783
5784 // Rotation 0.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005785 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005786 prepareDisplay(DISPLAY_ORIENTATION_0);
5787 processDown(mapper, toRawX(50), toRawY(75));
5788 processSync(mapper);
5789
5790 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5791 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5792 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5793
5794 processUp(mapper);
5795 processSync(mapper);
5796 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5797
5798 // Rotation 90.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005799 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005800 prepareDisplay(DISPLAY_ORIENTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005801 processDown(mapper, toRawX(75), RAW_Y_MAX - toRawY(50) + RAW_Y_MIN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005802 processSync(mapper);
5803
5804 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5805 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5806 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5807
5808 processUp(mapper);
5809 processSync(mapper);
5810 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5811
5812 // Rotation 180.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005813 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005814 prepareDisplay(DISPLAY_ORIENTATION_180);
5815 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
5816 processSync(mapper);
5817
5818 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5819 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5820 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5821
5822 processUp(mapper);
5823 processSync(mapper);
5824 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5825
5826 // Rotation 270.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005827 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005828 prepareDisplay(DISPLAY_ORIENTATION_270);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005829 processDown(mapper, RAW_X_MAX - toRawX(75) + RAW_X_MIN, toRawY(50));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005830 processSync(mapper);
5831
5832 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5833 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5834 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5835
5836 processUp(mapper);
5837 processSync(mapper);
5838 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5839}
5840
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005841TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation0_RotatesMotions) {
5842 addConfigurationProperty("touch.deviceType", "touchScreen");
5843 prepareButtons();
5844 prepareAxes(POSITION);
5845 addConfigurationProperty("touch.orientationAware", "1");
5846 addConfigurationProperty("touch.orientation", "ORIENTATION_0");
5847 clearViewports();
5848 prepareDisplay(DISPLAY_ORIENTATION_0);
5849 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5850 NotifyMotionArgs args;
5851
5852 // Orientation 0.
5853 processDown(mapper, toRawX(50), toRawY(75));
5854 processSync(mapper);
5855
5856 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5857 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5858 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5859
5860 processUp(mapper);
5861 processSync(mapper);
5862 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5863}
5864
5865TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation90_RotatesMotions) {
5866 addConfigurationProperty("touch.deviceType", "touchScreen");
5867 prepareButtons();
5868 prepareAxes(POSITION);
5869 addConfigurationProperty("touch.orientationAware", "1");
5870 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
5871 clearViewports();
5872 prepareDisplay(DISPLAY_ORIENTATION_0);
5873 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5874 NotifyMotionArgs args;
5875
5876 // Orientation 90.
5877 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
5878 processSync(mapper);
5879
5880 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5881 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5882 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5883
5884 processUp(mapper);
5885 processSync(mapper);
5886 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5887}
5888
5889TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation180_RotatesMotions) {
5890 addConfigurationProperty("touch.deviceType", "touchScreen");
5891 prepareButtons();
5892 prepareAxes(POSITION);
5893 addConfigurationProperty("touch.orientationAware", "1");
5894 addConfigurationProperty("touch.orientation", "ORIENTATION_180");
5895 clearViewports();
5896 prepareDisplay(DISPLAY_ORIENTATION_0);
5897 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5898 NotifyMotionArgs args;
5899
5900 // Orientation 180.
5901 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
5902 processSync(mapper);
5903
5904 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5905 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5906 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5907
5908 processUp(mapper);
5909 processSync(mapper);
5910 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5911}
5912
5913TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation270_RotatesMotions) {
5914 addConfigurationProperty("touch.deviceType", "touchScreen");
5915 prepareButtons();
5916 prepareAxes(POSITION);
5917 addConfigurationProperty("touch.orientationAware", "1");
5918 addConfigurationProperty("touch.orientation", "ORIENTATION_270");
5919 clearViewports();
5920 prepareDisplay(DISPLAY_ORIENTATION_0);
5921 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5922 NotifyMotionArgs args;
5923
5924 // Orientation 270.
5925 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
5926 processSync(mapper);
5927
5928 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5929 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5930 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5931
5932 processUp(mapper);
5933 processSync(mapper);
5934 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5935}
5936
5937TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationSpecified_RotatesMotionWithDisplay) {
5938 addConfigurationProperty("touch.deviceType", "touchScreen");
5939 prepareButtons();
5940 prepareAxes(POSITION);
5941 // Since InputReader works in the un-rotated coordinate space, only devices that are not
5942 // orientation-aware are affected by display rotation.
5943 addConfigurationProperty("touch.orientationAware", "0");
5944 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
5945 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5946
5947 NotifyMotionArgs args;
5948
5949 // Orientation 90, Rotation 0.
5950 clearViewports();
5951 prepareDisplay(DISPLAY_ORIENTATION_0);
5952 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
5953 processSync(mapper);
5954
5955 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5956 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5957 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5958
5959 processUp(mapper);
5960 processSync(mapper);
5961 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5962
5963 // Orientation 90, Rotation 90.
5964 clearViewports();
5965 prepareDisplay(DISPLAY_ORIENTATION_90);
5966 processDown(mapper, toRotatedRawX(50), toRotatedRawY(75));
5967 processSync(mapper);
5968
5969 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5970 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5971 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5972
5973 processUp(mapper);
5974 processSync(mapper);
5975 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5976
5977 // Orientation 90, Rotation 180.
5978 clearViewports();
5979 prepareDisplay(DISPLAY_ORIENTATION_180);
5980 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
5981 processSync(mapper);
5982
5983 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5984 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5985 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5986
5987 processUp(mapper);
5988 processSync(mapper);
5989 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5990
5991 // Orientation 90, Rotation 270.
5992 clearViewports();
5993 prepareDisplay(DISPLAY_ORIENTATION_270);
5994 processDown(mapper, RAW_X_MAX - toRotatedRawX(50) + RAW_X_MIN,
5995 RAW_Y_MAX - toRotatedRawY(75) + RAW_Y_MIN);
5996 processSync(mapper);
5997
5998 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5999 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6000 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6001
6002 processUp(mapper);
6003 processSync(mapper);
6004 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6005}
6006
Michael Wrightd02c5b62014-02-10 15:10:22 -08006007TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006008 addConfigurationProperty("touch.deviceType", "touchScreen");
6009 prepareDisplay(DISPLAY_ORIENTATION_0);
6010 prepareButtons();
6011 prepareAxes(POSITION | PRESSURE | TOOL | DISTANCE | TILT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006012 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006013
6014 // These calculations are based on the input device calibration documentation.
6015 int32_t rawX = 100;
6016 int32_t rawY = 200;
6017 int32_t rawPressure = 10;
6018 int32_t rawToolMajor = 12;
6019 int32_t rawDistance = 2;
6020 int32_t rawTiltX = 30;
6021 int32_t rawTiltY = 110;
6022
6023 float x = toDisplayX(rawX);
6024 float y = toDisplayY(rawY);
6025 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
6026 float size = float(rawToolMajor) / RAW_TOOL_MAX;
6027 float tool = float(rawToolMajor) * GEOMETRIC_SCALE;
6028 float distance = float(rawDistance);
6029
6030 float tiltCenter = (RAW_TILT_MAX + RAW_TILT_MIN) * 0.5f;
6031 float tiltScale = M_PI / 180;
6032 float tiltXAngle = (rawTiltX - tiltCenter) * tiltScale;
6033 float tiltYAngle = (rawTiltY - tiltCenter) * tiltScale;
6034 float orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
6035 float tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
6036
6037 processDown(mapper, rawX, rawY);
6038 processPressure(mapper, rawPressure);
6039 processToolMajor(mapper, rawToolMajor);
6040 processDistance(mapper, rawDistance);
6041 processTilt(mapper, rawTiltX, rawTiltY);
6042 processSync(mapper);
6043
6044 NotifyMotionArgs args;
6045 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6046 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6047 x, y, pressure, size, tool, tool, tool, tool, orientation, distance));
6048 ASSERT_EQ(tilt, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TILT));
6049}
6050
Jason Gerecke489fda82012-09-07 17:19:40 -07006051TEST_F(SingleTouchInputMapperTest, Process_XYAxes_AffineCalibration) {
Jason Gerecke489fda82012-09-07 17:19:40 -07006052 addConfigurationProperty("touch.deviceType", "touchScreen");
6053 prepareDisplay(DISPLAY_ORIENTATION_0);
6054 prepareLocationCalibration();
6055 prepareButtons();
6056 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006057 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Jason Gerecke489fda82012-09-07 17:19:40 -07006058
6059 int32_t rawX = 100;
6060 int32_t rawY = 200;
6061
6062 float x = toDisplayX(toCookedX(rawX, rawY));
6063 float y = toDisplayY(toCookedY(rawX, rawY));
6064
6065 processDown(mapper, rawX, rawY);
6066 processSync(mapper);
6067
6068 NotifyMotionArgs args;
6069 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6070 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6071 x, y, 1, 0, 0, 0, 0, 0, 0, 0));
6072}
6073
Michael Wrightd02c5b62014-02-10 15:10:22 -08006074TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006075 addConfigurationProperty("touch.deviceType", "touchScreen");
6076 prepareDisplay(DISPLAY_ORIENTATION_0);
6077 prepareButtons();
6078 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006079 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006080
6081 NotifyMotionArgs motionArgs;
6082 NotifyKeyArgs keyArgs;
6083
6084 processDown(mapper, 100, 200);
6085 processSync(mapper);
6086 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6087 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6088 ASSERT_EQ(0, motionArgs.buttonState);
6089
6090 // press BTN_LEFT, release BTN_LEFT
6091 processKey(mapper, BTN_LEFT, 1);
6092 processSync(mapper);
6093 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6094 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6095 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6096
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006097 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6098 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6099 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6100
Michael Wrightd02c5b62014-02-10 15:10:22 -08006101 processKey(mapper, BTN_LEFT, 0);
6102 processSync(mapper);
6103 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006104 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006105 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006106
6107 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006108 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006109 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006110
6111 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
6112 processKey(mapper, BTN_RIGHT, 1);
6113 processKey(mapper, BTN_MIDDLE, 1);
6114 processSync(mapper);
6115 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6116 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6117 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6118 motionArgs.buttonState);
6119
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006120 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6121 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6122 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
6123
6124 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6125 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6126 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6127 motionArgs.buttonState);
6128
Michael Wrightd02c5b62014-02-10 15:10:22 -08006129 processKey(mapper, BTN_RIGHT, 0);
6130 processSync(mapper);
6131 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006132 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006133 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006134
6135 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006136 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006137 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006138
6139 processKey(mapper, BTN_MIDDLE, 0);
6140 processSync(mapper);
6141 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006142 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006143 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006144
6145 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006146 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006147 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006148
6149 // press BTN_BACK, release BTN_BACK
6150 processKey(mapper, BTN_BACK, 1);
6151 processSync(mapper);
6152 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6153 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6154 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006155
Michael Wrightd02c5b62014-02-10 15:10:22 -08006156 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006157 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006158 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6159
6160 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6161 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6162 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006163
6164 processKey(mapper, BTN_BACK, 0);
6165 processSync(mapper);
6166 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006167 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006168 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006169
6170 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006171 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006172 ASSERT_EQ(0, motionArgs.buttonState);
6173
Michael Wrightd02c5b62014-02-10 15:10:22 -08006174 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6175 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6176 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6177
6178 // press BTN_SIDE, release BTN_SIDE
6179 processKey(mapper, BTN_SIDE, 1);
6180 processSync(mapper);
6181 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6182 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6183 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006184
Michael Wrightd02c5b62014-02-10 15:10:22 -08006185 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006186 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006187 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6188
6189 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6190 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6191 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006192
6193 processKey(mapper, BTN_SIDE, 0);
6194 processSync(mapper);
6195 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006196 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006197 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006198
6199 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006200 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006201 ASSERT_EQ(0, motionArgs.buttonState);
6202
Michael Wrightd02c5b62014-02-10 15:10:22 -08006203 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6204 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6205 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6206
6207 // press BTN_FORWARD, release BTN_FORWARD
6208 processKey(mapper, BTN_FORWARD, 1);
6209 processSync(mapper);
6210 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6211 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6212 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006213
Michael Wrightd02c5b62014-02-10 15:10:22 -08006214 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006215 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006216 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6217
6218 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6219 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6220 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006221
6222 processKey(mapper, BTN_FORWARD, 0);
6223 processSync(mapper);
6224 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006225 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006226 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006227
6228 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006229 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006230 ASSERT_EQ(0, motionArgs.buttonState);
6231
Michael Wrightd02c5b62014-02-10 15:10:22 -08006232 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6233 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6234 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6235
6236 // press BTN_EXTRA, release BTN_EXTRA
6237 processKey(mapper, BTN_EXTRA, 1);
6238 processSync(mapper);
6239 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6240 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6241 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006242
Michael Wrightd02c5b62014-02-10 15:10:22 -08006243 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006244 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006245 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6246
6247 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6248 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6249 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006250
6251 processKey(mapper, BTN_EXTRA, 0);
6252 processSync(mapper);
6253 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006254 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006255 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006256
6257 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006258 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006259 ASSERT_EQ(0, motionArgs.buttonState);
6260
Michael Wrightd02c5b62014-02-10 15:10:22 -08006261 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6262 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6263 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6264
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006265 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6266
Michael Wrightd02c5b62014-02-10 15:10:22 -08006267 // press BTN_STYLUS, release BTN_STYLUS
6268 processKey(mapper, BTN_STYLUS, 1);
6269 processSync(mapper);
6270 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6271 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006272 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
6273
6274 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6275 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6276 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006277
6278 processKey(mapper, BTN_STYLUS, 0);
6279 processSync(mapper);
6280 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006281 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006282 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006283
6284 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006285 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006286 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006287
6288 // press BTN_STYLUS2, release BTN_STYLUS2
6289 processKey(mapper, BTN_STYLUS2, 1);
6290 processSync(mapper);
6291 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6292 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006293 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
6294
6295 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6296 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6297 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006298
6299 processKey(mapper, BTN_STYLUS2, 0);
6300 processSync(mapper);
6301 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006302 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006303 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006304
6305 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006306 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006307 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006308
6309 // release touch
6310 processUp(mapper);
6311 processSync(mapper);
6312 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6313 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6314 ASSERT_EQ(0, motionArgs.buttonState);
6315}
6316
6317TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006318 addConfigurationProperty("touch.deviceType", "touchScreen");
6319 prepareDisplay(DISPLAY_ORIENTATION_0);
6320 prepareButtons();
6321 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006322 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006323
6324 NotifyMotionArgs motionArgs;
6325
6326 // default tool type is finger
6327 processDown(mapper, 100, 200);
6328 processSync(mapper);
6329 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6330 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6331 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6332
6333 // eraser
6334 processKey(mapper, BTN_TOOL_RUBBER, 1);
6335 processSync(mapper);
6336 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6337 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6338 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6339
6340 // stylus
6341 processKey(mapper, BTN_TOOL_RUBBER, 0);
6342 processKey(mapper, BTN_TOOL_PEN, 1);
6343 processSync(mapper);
6344 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6345 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6346 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6347
6348 // brush
6349 processKey(mapper, BTN_TOOL_PEN, 0);
6350 processKey(mapper, BTN_TOOL_BRUSH, 1);
6351 processSync(mapper);
6352 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6353 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6354 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6355
6356 // pencil
6357 processKey(mapper, BTN_TOOL_BRUSH, 0);
6358 processKey(mapper, BTN_TOOL_PENCIL, 1);
6359 processSync(mapper);
6360 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6361 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6362 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6363
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08006364 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08006365 processKey(mapper, BTN_TOOL_PENCIL, 0);
6366 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
6367 processSync(mapper);
6368 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6369 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6370 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6371
6372 // mouse
6373 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
6374 processKey(mapper, BTN_TOOL_MOUSE, 1);
6375 processSync(mapper);
6376 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6377 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6378 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6379
6380 // lens
6381 processKey(mapper, BTN_TOOL_MOUSE, 0);
6382 processKey(mapper, BTN_TOOL_LENS, 1);
6383 processSync(mapper);
6384 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6385 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6386 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6387
6388 // double-tap
6389 processKey(mapper, BTN_TOOL_LENS, 0);
6390 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
6391 processSync(mapper);
6392 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6393 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6394 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6395
6396 // triple-tap
6397 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
6398 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
6399 processSync(mapper);
6400 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6401 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6402 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6403
6404 // quad-tap
6405 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
6406 processKey(mapper, BTN_TOOL_QUADTAP, 1);
6407 processSync(mapper);
6408 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6409 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6410 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6411
6412 // finger
6413 processKey(mapper, BTN_TOOL_QUADTAP, 0);
6414 processKey(mapper, BTN_TOOL_FINGER, 1);
6415 processSync(mapper);
6416 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6417 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6418 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6419
6420 // stylus trumps finger
6421 processKey(mapper, BTN_TOOL_PEN, 1);
6422 processSync(mapper);
6423 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6424 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6425 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6426
6427 // eraser trumps stylus
6428 processKey(mapper, BTN_TOOL_RUBBER, 1);
6429 processSync(mapper);
6430 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6431 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6432 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6433
6434 // mouse trumps eraser
6435 processKey(mapper, BTN_TOOL_MOUSE, 1);
6436 processSync(mapper);
6437 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6438 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6439 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6440
6441 // back to default tool type
6442 processKey(mapper, BTN_TOOL_MOUSE, 0);
6443 processKey(mapper, BTN_TOOL_RUBBER, 0);
6444 processKey(mapper, BTN_TOOL_PEN, 0);
6445 processKey(mapper, BTN_TOOL_FINGER, 0);
6446 processSync(mapper);
6447 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6448 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6449 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6450}
6451
6452TEST_F(SingleTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006453 addConfigurationProperty("touch.deviceType", "touchScreen");
6454 prepareDisplay(DISPLAY_ORIENTATION_0);
6455 prepareButtons();
6456 prepareAxes(POSITION);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006457 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_FINGER, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006458 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006459
6460 NotifyMotionArgs motionArgs;
6461
6462 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
6463 processKey(mapper, BTN_TOOL_FINGER, 1);
6464 processMove(mapper, 100, 200);
6465 processSync(mapper);
6466 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6467 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6468 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6469 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6470
6471 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6472 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6473 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6474 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6475
6476 // move a little
6477 processMove(mapper, 150, 250);
6478 processSync(mapper);
6479 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6480 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6481 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6482 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6483
6484 // down when BTN_TOUCH is pressed, pressure defaults to 1
6485 processKey(mapper, BTN_TOUCH, 1);
6486 processSync(mapper);
6487 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6488 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6489 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6490 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6491
6492 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6493 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6494 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6495 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6496
6497 // up when BTN_TOUCH is released, hover restored
6498 processKey(mapper, BTN_TOUCH, 0);
6499 processSync(mapper);
6500 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6501 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6502 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6503 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6504
6505 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6506 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6507 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6508 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6509
6510 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6511 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6512 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6513 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6514
6515 // exit hover when pointer goes away
6516 processKey(mapper, BTN_TOOL_FINGER, 0);
6517 processSync(mapper);
6518 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6519 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6520 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6521 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6522}
6523
6524TEST_F(SingleTouchInputMapperTest, Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006525 addConfigurationProperty("touch.deviceType", "touchScreen");
6526 prepareDisplay(DISPLAY_ORIENTATION_0);
6527 prepareButtons();
6528 prepareAxes(POSITION | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006529 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006530
6531 NotifyMotionArgs motionArgs;
6532
6533 // initially hovering because pressure is 0
6534 processDown(mapper, 100, 200);
6535 processPressure(mapper, 0);
6536 processSync(mapper);
6537 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6538 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6539 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6540 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6541
6542 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6543 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6544 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6545 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6546
6547 // move a little
6548 processMove(mapper, 150, 250);
6549 processSync(mapper);
6550 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6551 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6552 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6553 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6554
6555 // down when pressure is non-zero
6556 processPressure(mapper, RAW_PRESSURE_MAX);
6557 processSync(mapper);
6558 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6559 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6560 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6561 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6562
6563 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6564 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6565 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6566 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6567
6568 // up when pressure becomes 0, hover restored
6569 processPressure(mapper, 0);
6570 processSync(mapper);
6571 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6572 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6573 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6574 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6575
6576 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6577 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6578 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6579 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6580
6581 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6582 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6583 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6584 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6585
6586 // exit hover when pointer goes away
6587 processUp(mapper);
6588 processSync(mapper);
6589 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6590 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6591 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6592 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6593}
6594
Prabir Pradhan5632d622021-09-06 07:57:20 -07006595// --- TouchDisplayProjectionTest ---
6596
6597class TouchDisplayProjectionTest : public SingleTouchInputMapperTest {
6598public:
6599 // The values inside DisplayViewport are expected to be pre-rotated. This updates the current
6600 // DisplayViewport to pre-rotate the values. The viewport's physical display will be set to the
6601 // rotated equivalent of the given un-rotated physical display bounds.
6602 void configurePhysicalDisplay(int32_t orientation, Rect naturalPhysicalDisplay) {
6603 uint32_t inverseRotationFlags;
6604 auto width = DISPLAY_WIDTH;
6605 auto height = DISPLAY_HEIGHT;
6606 switch (orientation) {
6607 case DISPLAY_ORIENTATION_90:
6608 inverseRotationFlags = ui::Transform::ROT_270;
6609 std::swap(width, height);
6610 break;
6611 case DISPLAY_ORIENTATION_180:
6612 inverseRotationFlags = ui::Transform::ROT_180;
6613 break;
6614 case DISPLAY_ORIENTATION_270:
6615 inverseRotationFlags = ui::Transform::ROT_90;
6616 std::swap(width, height);
6617 break;
6618 case DISPLAY_ORIENTATION_0:
6619 inverseRotationFlags = ui::Transform::ROT_0;
6620 break;
6621 default:
6622 FAIL() << "Invalid orientation: " << orientation;
6623 }
6624
6625 const ui::Transform rotation(inverseRotationFlags, width, height);
6626 const Rect rotatedPhysicalDisplay = rotation.transform(naturalPhysicalDisplay);
6627
6628 std::optional<DisplayViewport> internalViewport =
6629 *mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6630 DisplayViewport& v = *internalViewport;
6631 v.displayId = DISPLAY_ID;
6632 v.orientation = orientation;
6633
6634 v.logicalLeft = 0;
6635 v.logicalTop = 0;
6636 v.logicalRight = 100;
6637 v.logicalBottom = 100;
6638
6639 v.physicalLeft = rotatedPhysicalDisplay.left;
6640 v.physicalTop = rotatedPhysicalDisplay.top;
6641 v.physicalRight = rotatedPhysicalDisplay.right;
6642 v.physicalBottom = rotatedPhysicalDisplay.bottom;
6643
6644 v.deviceWidth = width;
6645 v.deviceHeight = height;
6646
6647 v.isActive = true;
6648 v.uniqueId = UNIQUE_ID;
6649 v.type = ViewportType::INTERNAL;
6650 mFakePolicy->updateViewport(v);
6651 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
6652 }
6653
6654 void assertReceivedMove(const Point& point) {
6655 NotifyMotionArgs motionArgs;
6656 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6657 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6658 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6659 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], point.x, point.y,
6660 1, 0, 0, 0, 0, 0, 0, 0));
6661 }
6662};
6663
6664TEST_F(TouchDisplayProjectionTest, IgnoresTouchesOutsidePhysicalDisplay) {
6665 addConfigurationProperty("touch.deviceType", "touchScreen");
6666 prepareDisplay(DISPLAY_ORIENTATION_0);
6667
6668 prepareButtons();
6669 prepareAxes(POSITION);
6670 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6671
6672 NotifyMotionArgs motionArgs;
6673
6674 // Configure the DisplayViewport such that the logical display maps to a subsection of
6675 // the display panel called the physical display. Here, the physical display is bounded by the
6676 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
6677 static const Rect kPhysicalDisplay{10, 20, 70, 160};
6678 static const std::array<Point, 6> kPointsOutsidePhysicalDisplay{
6679 {{-10, -10}, {0, 0}, {5, 100}, {50, 15}, {75, 100}, {50, 165}}};
6680
6681 for (auto orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90, DISPLAY_ORIENTATION_180,
6682 DISPLAY_ORIENTATION_270}) {
6683 configurePhysicalDisplay(orientation, kPhysicalDisplay);
6684
6685 // Touches outside the physical display should be ignored, and should not generate any
6686 // events. Ensure touches at the following points that lie outside of the physical display
6687 // area do not generate any events.
6688 for (const auto& point : kPointsOutsidePhysicalDisplay) {
6689 processDown(mapper, toRawX(point.x), toRawY(point.y));
6690 processSync(mapper);
6691 processUp(mapper);
6692 processSync(mapper);
6693 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled())
6694 << "Unexpected event generated for touch outside physical display at point: "
6695 << point.x << ", " << point.y;
6696 }
6697 }
6698}
6699
6700TEST_F(TouchDisplayProjectionTest, EmitsTouchDownAfterEnteringPhysicalDisplay) {
6701 addConfigurationProperty("touch.deviceType", "touchScreen");
6702 prepareDisplay(DISPLAY_ORIENTATION_0);
6703
6704 prepareButtons();
6705 prepareAxes(POSITION);
6706 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6707
6708 NotifyMotionArgs motionArgs;
6709
6710 // Configure the DisplayViewport such that the logical display maps to a subsection of
6711 // the display panel called the physical display. Here, the physical display is bounded by the
6712 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
6713 static const Rect kPhysicalDisplay{10, 20, 70, 160};
6714
6715 for (auto orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90, DISPLAY_ORIENTATION_180,
6716 DISPLAY_ORIENTATION_270}) {
6717 configurePhysicalDisplay(orientation, kPhysicalDisplay);
6718
6719 // Touches that start outside the physical display should be ignored until it enters the
6720 // physical display bounds, at which point it should generate a down event. Start a touch at
6721 // the point (5, 100), which is outside the physical display bounds.
6722 static const Point kOutsidePoint{5, 100};
6723 processDown(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
6724 processSync(mapper);
6725 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6726
6727 // Move the touch into the physical display area. This should generate a pointer down.
6728 processMove(mapper, toRawX(11), toRawY(21));
6729 processSync(mapper);
6730 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6731 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6732 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6733 ASSERT_NO_FATAL_FAILURE(
6734 assertPointerCoords(motionArgs.pointerCoords[0], 11, 21, 1, 0, 0, 0, 0, 0, 0, 0));
6735
6736 // Move the touch inside the physical display area. This should generate a pointer move.
6737 processMove(mapper, toRawX(69), toRawY(159));
6738 processSync(mapper);
6739 assertReceivedMove({69, 159});
6740
6741 // Move outside the physical display area. Since the pointer is already down, this should
6742 // now continue generating events.
6743 processMove(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
6744 processSync(mapper);
6745 assertReceivedMove(kOutsidePoint);
6746
6747 // Release. This should generate a pointer up.
6748 processUp(mapper);
6749 processSync(mapper);
6750 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6751 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6752 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], kOutsidePoint.x,
6753 kOutsidePoint.y, 1, 0, 0, 0, 0, 0, 0, 0));
6754
6755 // Ensure no more events were generated.
6756 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6757 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6758 }
6759}
6760
Michael Wrightd02c5b62014-02-10 15:10:22 -08006761// --- MultiTouchInputMapperTest ---
6762
6763class MultiTouchInputMapperTest : public TouchInputMapperTest {
6764protected:
6765 void prepareAxes(int axes);
6766
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006767 void processPosition(MultiTouchInputMapper& mapper, int32_t x, int32_t y);
6768 void processTouchMajor(MultiTouchInputMapper& mapper, int32_t touchMajor);
6769 void processTouchMinor(MultiTouchInputMapper& mapper, int32_t touchMinor);
6770 void processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor);
6771 void processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor);
6772 void processOrientation(MultiTouchInputMapper& mapper, int32_t orientation);
6773 void processPressure(MultiTouchInputMapper& mapper, int32_t pressure);
6774 void processDistance(MultiTouchInputMapper& mapper, int32_t distance);
6775 void processId(MultiTouchInputMapper& mapper, int32_t id);
6776 void processSlot(MultiTouchInputMapper& mapper, int32_t slot);
6777 void processToolType(MultiTouchInputMapper& mapper, int32_t toolType);
6778 void processKey(MultiTouchInputMapper& mapper, int32_t code, int32_t value);
6779 void processMTSync(MultiTouchInputMapper& mapper);
6780 void processSync(MultiTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006781};
6782
6783void MultiTouchInputMapperTest::prepareAxes(int axes) {
6784 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006785 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
6786 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006787 }
6788 if (axes & TOUCH) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006789 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN,
6790 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006791 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006792 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN,
6793 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006794 }
6795 }
6796 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006797 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
6798 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006799 if (axes & MINOR) {
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08006800 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006801 RAW_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006802 }
6803 }
6804 if (axes & ORIENTATION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006805 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_ORIENTATION, RAW_ORIENTATION_MIN,
6806 RAW_ORIENTATION_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006807 }
6808 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006809 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_PRESSURE, RAW_PRESSURE_MIN,
6810 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006811 }
6812 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006813 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_DISTANCE, RAW_DISTANCE_MIN,
6814 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006815 }
6816 if (axes & ID) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006817 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX, 0,
6818 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006819 }
6820 if (axes & SLOT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006821 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
6822 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_MT_SLOT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006823 }
6824 if (axes & TOOL_TYPE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006825 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOOL_TYPE, 0, MT_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006826 }
6827}
6828
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006829void MultiTouchInputMapperTest::processPosition(MultiTouchInputMapper& mapper, int32_t x,
6830 int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006831 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_X, x);
6832 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006833}
6834
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006835void MultiTouchInputMapperTest::processTouchMajor(MultiTouchInputMapper& mapper,
6836 int32_t touchMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006837 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006838}
6839
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006840void MultiTouchInputMapperTest::processTouchMinor(MultiTouchInputMapper& mapper,
6841 int32_t touchMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006842 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006843}
6844
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006845void MultiTouchInputMapperTest::processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006846 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006847}
6848
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006849void MultiTouchInputMapperTest::processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006850 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006851}
6852
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006853void MultiTouchInputMapperTest::processOrientation(MultiTouchInputMapper& mapper,
6854 int32_t orientation) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006855 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_ORIENTATION, orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006856}
6857
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006858void MultiTouchInputMapperTest::processPressure(MultiTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006859 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006860}
6861
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006862void MultiTouchInputMapperTest::processDistance(MultiTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006863 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006864}
6865
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006866void MultiTouchInputMapperTest::processId(MultiTouchInputMapper& mapper, int32_t id) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006867 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TRACKING_ID, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006868}
6869
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006870void MultiTouchInputMapperTest::processSlot(MultiTouchInputMapper& mapper, int32_t slot) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006871 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_SLOT, slot);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006872}
6873
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006874void MultiTouchInputMapperTest::processToolType(MultiTouchInputMapper& mapper, int32_t toolType) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006875 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006876}
6877
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006878void MultiTouchInputMapperTest::processKey(MultiTouchInputMapper& mapper, int32_t code,
6879 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006880 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006881}
6882
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006883void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006884 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_MT_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006885}
6886
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006887void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006888 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006889}
6890
Michael Wrightd02c5b62014-02-10 15:10:22 -08006891TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006892 addConfigurationProperty("touch.deviceType", "touchScreen");
6893 prepareDisplay(DISPLAY_ORIENTATION_0);
6894 prepareAxes(POSITION);
6895 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006896 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006897
arthurhungdcef2dc2020-08-11 14:47:50 +08006898 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006899
6900 NotifyMotionArgs motionArgs;
6901
6902 // Two fingers down at once.
6903 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
6904 processPosition(mapper, x1, y1);
6905 processMTSync(mapper);
6906 processPosition(mapper, x2, y2);
6907 processMTSync(mapper);
6908 processSync(mapper);
6909
6910 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6911 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6912 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6913 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6914 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6915 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6916 ASSERT_EQ(0, motionArgs.flags);
6917 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6918 ASSERT_EQ(0, motionArgs.buttonState);
6919 ASSERT_EQ(0, motionArgs.edgeFlags);
6920 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6921 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6922 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6923 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6924 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6925 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6926 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6927 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6928
6929 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6930 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6931 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6932 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6933 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08006934 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006935 ASSERT_EQ(0, motionArgs.flags);
6936 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6937 ASSERT_EQ(0, motionArgs.buttonState);
6938 ASSERT_EQ(0, motionArgs.edgeFlags);
6939 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6940 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6941 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6942 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6943 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6944 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6945 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6946 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6947 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6948 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6949 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6950 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6951
6952 // Move.
6953 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
6954 processPosition(mapper, x1, y1);
6955 processMTSync(mapper);
6956 processPosition(mapper, x2, y2);
6957 processMTSync(mapper);
6958 processSync(mapper);
6959
6960 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6961 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6962 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6963 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6964 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6965 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6966 ASSERT_EQ(0, motionArgs.flags);
6967 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6968 ASSERT_EQ(0, motionArgs.buttonState);
6969 ASSERT_EQ(0, motionArgs.edgeFlags);
6970 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6971 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6972 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6973 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6974 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6975 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6976 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6977 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6978 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6979 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6980 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6981 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6982
6983 // First finger up.
6984 x2 += 15; y2 -= 20;
6985 processPosition(mapper, x2, y2);
6986 processMTSync(mapper);
6987 processSync(mapper);
6988
6989 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6990 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6991 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6992 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6993 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08006994 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006995 ASSERT_EQ(0, motionArgs.flags);
6996 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6997 ASSERT_EQ(0, motionArgs.buttonState);
6998 ASSERT_EQ(0, motionArgs.edgeFlags);
6999 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7000 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7001 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7002 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7003 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7004 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7005 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7006 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7007 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7008 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7009 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7010 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7011
7012 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7013 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7014 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7015 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7016 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7017 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7018 ASSERT_EQ(0, motionArgs.flags);
7019 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7020 ASSERT_EQ(0, motionArgs.buttonState);
7021 ASSERT_EQ(0, motionArgs.edgeFlags);
7022 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7023 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7024 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7025 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7026 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7027 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7028 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7029 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7030
7031 // Move.
7032 x2 += 20; y2 -= 25;
7033 processPosition(mapper, x2, y2);
7034 processMTSync(mapper);
7035 processSync(mapper);
7036
7037 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7038 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7039 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7040 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7041 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7042 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7043 ASSERT_EQ(0, motionArgs.flags);
7044 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7045 ASSERT_EQ(0, motionArgs.buttonState);
7046 ASSERT_EQ(0, motionArgs.edgeFlags);
7047 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7048 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7049 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7050 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7051 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7052 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7053 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7054 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7055
7056 // New finger down.
7057 int32_t x3 = 700, y3 = 300;
7058 processPosition(mapper, x2, y2);
7059 processMTSync(mapper);
7060 processPosition(mapper, x3, y3);
7061 processMTSync(mapper);
7062 processSync(mapper);
7063
7064 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7065 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7066 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7067 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7068 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007069 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007070 ASSERT_EQ(0, motionArgs.flags);
7071 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7072 ASSERT_EQ(0, motionArgs.buttonState);
7073 ASSERT_EQ(0, motionArgs.edgeFlags);
7074 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7075 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7076 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7077 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7078 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7079 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7080 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7081 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7082 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7083 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7084 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7085 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7086
7087 // Second finger up.
7088 x3 += 30; y3 -= 20;
7089 processPosition(mapper, x3, y3);
7090 processMTSync(mapper);
7091 processSync(mapper);
7092
7093 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7094 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7095 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7096 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7097 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007098 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007099 ASSERT_EQ(0, motionArgs.flags);
7100 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7101 ASSERT_EQ(0, motionArgs.buttonState);
7102 ASSERT_EQ(0, motionArgs.edgeFlags);
7103 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7104 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7105 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7106 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7107 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7108 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7109 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7110 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7111 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7112 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7113 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7114 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7115
7116 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7117 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7118 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7119 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7120 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7121 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7122 ASSERT_EQ(0, motionArgs.flags);
7123 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7124 ASSERT_EQ(0, motionArgs.buttonState);
7125 ASSERT_EQ(0, motionArgs.edgeFlags);
7126 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7127 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7128 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7129 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7130 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7131 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7132 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7133 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7134
7135 // Last finger up.
7136 processMTSync(mapper);
7137 processSync(mapper);
7138
7139 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7140 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7141 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7142 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7143 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7144 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7145 ASSERT_EQ(0, motionArgs.flags);
7146 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7147 ASSERT_EQ(0, motionArgs.buttonState);
7148 ASSERT_EQ(0, motionArgs.edgeFlags);
7149 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7150 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7151 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7152 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7153 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7154 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7155 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7156 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7157
7158 // Should not have sent any more keys or motions.
7159 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7160 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7161}
7162
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007163TEST_F(MultiTouchInputMapperTest, AxisResolution_IsPopulated) {
7164 addConfigurationProperty("touch.deviceType", "touchScreen");
7165 prepareDisplay(DISPLAY_ORIENTATION_0);
7166
7167 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
7168 /*fuzz*/ 0, /*resolution*/ 10);
7169 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
7170 /*fuzz*/ 0, /*resolution*/ 11);
7171 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
7172 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 12);
7173 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
7174 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 13);
7175 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7176 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 14);
7177 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7178 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 15);
7179
7180 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7181
7182 // X and Y axes
7183 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_X, 10 / X_PRECISION);
7184 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_Y, 11 / Y_PRECISION);
7185 // Touch major and minor
7186 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR, 12 * GEOMETRIC_SCALE);
7187 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR, 13 * GEOMETRIC_SCALE);
7188 // Tool major and minor
7189 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR, 14 * GEOMETRIC_SCALE);
7190 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR, 15 * GEOMETRIC_SCALE);
7191}
7192
7193TEST_F(MultiTouchInputMapperTest, TouchMajorAndMinorAxes_DoNotAppearIfNotSupported) {
7194 addConfigurationProperty("touch.deviceType", "touchScreen");
7195 prepareDisplay(DISPLAY_ORIENTATION_0);
7196
7197 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
7198 /*fuzz*/ 0, /*resolution*/ 10);
7199 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
7200 /*fuzz*/ 0, /*resolution*/ 11);
7201
7202 // We do not add ABS_MT_TOUCH_MAJOR / MINOR or ABS_MT_WIDTH_MAJOR / MINOR axes
7203
7204 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7205
7206 // Touch major and minor
7207 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR);
7208 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR);
7209 // Tool major and minor
7210 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR);
7211 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR);
7212}
7213
Michael Wrightd02c5b62014-02-10 15:10:22 -08007214TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007215 addConfigurationProperty("touch.deviceType", "touchScreen");
7216 prepareDisplay(DISPLAY_ORIENTATION_0);
7217 prepareAxes(POSITION | ID);
7218 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007219 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007220
arthurhungdcef2dc2020-08-11 14:47:50 +08007221 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007222
7223 NotifyMotionArgs motionArgs;
7224
7225 // Two fingers down at once.
7226 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7227 processPosition(mapper, x1, y1);
7228 processId(mapper, 1);
7229 processMTSync(mapper);
7230 processPosition(mapper, x2, y2);
7231 processId(mapper, 2);
7232 processMTSync(mapper);
7233 processSync(mapper);
7234
7235 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7236 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7237 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7238 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7239 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7240 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7241 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7242
7243 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007244 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007245 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7246 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7247 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7248 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7249 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7250 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7251 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7252 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7253 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7254
7255 // Move.
7256 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7257 processPosition(mapper, x1, y1);
7258 processId(mapper, 1);
7259 processMTSync(mapper);
7260 processPosition(mapper, x2, y2);
7261 processId(mapper, 2);
7262 processMTSync(mapper);
7263 processSync(mapper);
7264
7265 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7266 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7267 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7268 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7269 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7270 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7271 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7272 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7273 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7274 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7275 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7276
7277 // First finger up.
7278 x2 += 15; y2 -= 20;
7279 processPosition(mapper, x2, y2);
7280 processId(mapper, 2);
7281 processMTSync(mapper);
7282 processSync(mapper);
7283
7284 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007285 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007286 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7287 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7288 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7289 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7290 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7291 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7292 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7293 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7294 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7295
7296 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7297 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7298 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7299 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7300 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7301 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7302 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7303
7304 // Move.
7305 x2 += 20; y2 -= 25;
7306 processPosition(mapper, x2, y2);
7307 processId(mapper, 2);
7308 processMTSync(mapper);
7309 processSync(mapper);
7310
7311 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7312 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7313 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7314 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7315 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7316 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7317 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7318
7319 // New finger down.
7320 int32_t x3 = 700, y3 = 300;
7321 processPosition(mapper, x2, y2);
7322 processId(mapper, 2);
7323 processMTSync(mapper);
7324 processPosition(mapper, x3, y3);
7325 processId(mapper, 3);
7326 processMTSync(mapper);
7327 processSync(mapper);
7328
7329 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007330 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007331 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7332 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7333 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7334 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7335 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7336 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7337 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7338 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7339 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7340
7341 // Second finger up.
7342 x3 += 30; y3 -= 20;
7343 processPosition(mapper, x3, y3);
7344 processId(mapper, 3);
7345 processMTSync(mapper);
7346 processSync(mapper);
7347
7348 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007349 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007350 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7351 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7352 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7353 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7354 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7355 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7356 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7357 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7358 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7359
7360 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7361 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7362 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7363 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7364 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7365 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7366 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7367
7368 // Last finger up.
7369 processMTSync(mapper);
7370 processSync(mapper);
7371
7372 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7373 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7374 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7375 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7376 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7377 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7378 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7379
7380 // Should not have sent any more keys or motions.
7381 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7382 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7383}
7384
7385TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007386 addConfigurationProperty("touch.deviceType", "touchScreen");
7387 prepareDisplay(DISPLAY_ORIENTATION_0);
7388 prepareAxes(POSITION | ID | SLOT);
7389 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007390 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007391
arthurhungdcef2dc2020-08-11 14:47:50 +08007392 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007393
7394 NotifyMotionArgs motionArgs;
7395
7396 // Two fingers down at once.
7397 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7398 processPosition(mapper, x1, y1);
7399 processId(mapper, 1);
7400 processSlot(mapper, 1);
7401 processPosition(mapper, x2, y2);
7402 processId(mapper, 2);
7403 processSync(mapper);
7404
7405 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7406 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7407 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7408 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7409 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7410 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7411 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7412
7413 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007414 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007415 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7416 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7417 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7418 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7419 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7420 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7421 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7422 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7423 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7424
7425 // Move.
7426 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7427 processSlot(mapper, 0);
7428 processPosition(mapper, x1, y1);
7429 processSlot(mapper, 1);
7430 processPosition(mapper, x2, y2);
7431 processSync(mapper);
7432
7433 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7434 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7435 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7436 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7437 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7438 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7439 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7440 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7441 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7442 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7443 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7444
7445 // First finger up.
7446 x2 += 15; y2 -= 20;
7447 processSlot(mapper, 0);
7448 processId(mapper, -1);
7449 processSlot(mapper, 1);
7450 processPosition(mapper, x2, y2);
7451 processSync(mapper);
7452
7453 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007454 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007455 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7456 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7457 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7458 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7459 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7460 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7461 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7462 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7463 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7464
7465 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7466 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7467 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7468 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7469 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7470 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7471 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7472
7473 // Move.
7474 x2 += 20; y2 -= 25;
7475 processPosition(mapper, x2, y2);
7476 processSync(mapper);
7477
7478 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7479 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7480 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7481 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7482 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7483 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7484 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7485
7486 // New finger down.
7487 int32_t x3 = 700, y3 = 300;
7488 processPosition(mapper, x2, y2);
7489 processSlot(mapper, 0);
7490 processId(mapper, 3);
7491 processPosition(mapper, x3, y3);
7492 processSync(mapper);
7493
7494 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007495 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007496 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7497 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7498 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7499 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7500 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7501 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7502 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7503 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7504 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7505
7506 // Second finger up.
7507 x3 += 30; y3 -= 20;
7508 processSlot(mapper, 1);
7509 processId(mapper, -1);
7510 processSlot(mapper, 0);
7511 processPosition(mapper, x3, y3);
7512 processSync(mapper);
7513
7514 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007515 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007516 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7517 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7518 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7519 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7520 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7521 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7522 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7523 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7524 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7525
7526 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7527 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7528 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7529 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7530 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7531 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7532 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7533
7534 // Last finger up.
7535 processId(mapper, -1);
7536 processSync(mapper);
7537
7538 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7539 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7540 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7541 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7542 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7543 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7544 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7545
7546 // Should not have sent any more keys or motions.
7547 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7548 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7549}
7550
7551TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007552 addConfigurationProperty("touch.deviceType", "touchScreen");
7553 prepareDisplay(DISPLAY_ORIENTATION_0);
7554 prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007555 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007556
7557 // These calculations are based on the input device calibration documentation.
7558 int32_t rawX = 100;
7559 int32_t rawY = 200;
7560 int32_t rawTouchMajor = 7;
7561 int32_t rawTouchMinor = 6;
7562 int32_t rawToolMajor = 9;
7563 int32_t rawToolMinor = 8;
7564 int32_t rawPressure = 11;
7565 int32_t rawDistance = 0;
7566 int32_t rawOrientation = 3;
7567 int32_t id = 5;
7568
7569 float x = toDisplayX(rawX);
7570 float y = toDisplayY(rawY);
7571 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
7572 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
7573 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
7574 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
7575 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
7576 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
7577 float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
7578 float distance = float(rawDistance);
7579
7580 processPosition(mapper, rawX, rawY);
7581 processTouchMajor(mapper, rawTouchMajor);
7582 processTouchMinor(mapper, rawTouchMinor);
7583 processToolMajor(mapper, rawToolMajor);
7584 processToolMinor(mapper, rawToolMinor);
7585 processPressure(mapper, rawPressure);
7586 processOrientation(mapper, rawOrientation);
7587 processDistance(mapper, rawDistance);
7588 processId(mapper, id);
7589 processMTSync(mapper);
7590 processSync(mapper);
7591
7592 NotifyMotionArgs args;
7593 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7594 ASSERT_EQ(0, args.pointerProperties[0].id);
7595 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7596 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
7597 orientation, distance));
7598}
7599
7600TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007601 addConfigurationProperty("touch.deviceType", "touchScreen");
7602 prepareDisplay(DISPLAY_ORIENTATION_0);
7603 prepareAxes(POSITION | TOUCH | TOOL | MINOR);
7604 addConfigurationProperty("touch.size.calibration", "geometric");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007605 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007606
7607 // These calculations are based on the input device calibration documentation.
7608 int32_t rawX = 100;
7609 int32_t rawY = 200;
7610 int32_t rawTouchMajor = 140;
7611 int32_t rawTouchMinor = 120;
7612 int32_t rawToolMajor = 180;
7613 int32_t rawToolMinor = 160;
7614
7615 float x = toDisplayX(rawX);
7616 float y = toDisplayY(rawY);
7617 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
7618 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
7619 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
7620 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
7621 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
7622
7623 processPosition(mapper, rawX, rawY);
7624 processTouchMajor(mapper, rawTouchMajor);
7625 processTouchMinor(mapper, rawTouchMinor);
7626 processToolMajor(mapper, rawToolMajor);
7627 processToolMinor(mapper, rawToolMinor);
7628 processMTSync(mapper);
7629 processSync(mapper);
7630
7631 NotifyMotionArgs args;
7632 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7633 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7634 x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
7635}
7636
7637TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007638 addConfigurationProperty("touch.deviceType", "touchScreen");
7639 prepareDisplay(DISPLAY_ORIENTATION_0);
7640 prepareAxes(POSITION | TOUCH | TOOL);
7641 addConfigurationProperty("touch.size.calibration", "diameter");
7642 addConfigurationProperty("touch.size.scale", "10");
7643 addConfigurationProperty("touch.size.bias", "160");
7644 addConfigurationProperty("touch.size.isSummed", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007645 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007646
7647 // These calculations are based on the input device calibration documentation.
7648 // Note: We only provide a single common touch/tool value because the device is assumed
7649 // not to emit separate values for each pointer (isSummed = 1).
7650 int32_t rawX = 100;
7651 int32_t rawY = 200;
7652 int32_t rawX2 = 150;
7653 int32_t rawY2 = 250;
7654 int32_t rawTouchMajor = 5;
7655 int32_t rawToolMajor = 8;
7656
7657 float x = toDisplayX(rawX);
7658 float y = toDisplayY(rawY);
7659 float x2 = toDisplayX(rawX2);
7660 float y2 = toDisplayY(rawY2);
7661 float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
7662 float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
7663 float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
7664
7665 processPosition(mapper, rawX, rawY);
7666 processTouchMajor(mapper, rawTouchMajor);
7667 processToolMajor(mapper, rawToolMajor);
7668 processMTSync(mapper);
7669 processPosition(mapper, rawX2, rawY2);
7670 processTouchMajor(mapper, rawTouchMajor);
7671 processToolMajor(mapper, rawToolMajor);
7672 processMTSync(mapper);
7673 processSync(mapper);
7674
7675 NotifyMotionArgs args;
7676 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7677 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
7678
7679 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007680 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007681 ASSERT_EQ(size_t(2), args.pointerCount);
7682 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7683 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
7684 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
7685 x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
7686}
7687
7688TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007689 addConfigurationProperty("touch.deviceType", "touchScreen");
7690 prepareDisplay(DISPLAY_ORIENTATION_0);
7691 prepareAxes(POSITION | TOUCH | TOOL);
7692 addConfigurationProperty("touch.size.calibration", "area");
7693 addConfigurationProperty("touch.size.scale", "43");
7694 addConfigurationProperty("touch.size.bias", "3");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007695 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007696
7697 // These calculations are based on the input device calibration documentation.
7698 int32_t rawX = 100;
7699 int32_t rawY = 200;
7700 int32_t rawTouchMajor = 5;
7701 int32_t rawToolMajor = 8;
7702
7703 float x = toDisplayX(rawX);
7704 float y = toDisplayY(rawY);
7705 float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
7706 float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
7707 float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
7708
7709 processPosition(mapper, rawX, rawY);
7710 processTouchMajor(mapper, rawTouchMajor);
7711 processToolMajor(mapper, rawToolMajor);
7712 processMTSync(mapper);
7713 processSync(mapper);
7714
7715 NotifyMotionArgs args;
7716 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7717 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7718 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
7719}
7720
7721TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007722 addConfigurationProperty("touch.deviceType", "touchScreen");
7723 prepareDisplay(DISPLAY_ORIENTATION_0);
7724 prepareAxes(POSITION | PRESSURE);
7725 addConfigurationProperty("touch.pressure.calibration", "amplitude");
7726 addConfigurationProperty("touch.pressure.scale", "0.01");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007727 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007728
Michael Wrightaa449c92017-12-13 21:21:43 +00007729 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007730 mapper.populateDeviceInfo(&info);
Michael Wrightaa449c92017-12-13 21:21:43 +00007731 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
7732 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TOUCHSCREEN,
7733 0.0f, RAW_PRESSURE_MAX * 0.01, 0.0f, 0.0f));
7734
Michael Wrightd02c5b62014-02-10 15:10:22 -08007735 // These calculations are based on the input device calibration documentation.
7736 int32_t rawX = 100;
7737 int32_t rawY = 200;
7738 int32_t rawPressure = 60;
7739
7740 float x = toDisplayX(rawX);
7741 float y = toDisplayY(rawY);
7742 float pressure = float(rawPressure) * 0.01f;
7743
7744 processPosition(mapper, rawX, rawY);
7745 processPressure(mapper, rawPressure);
7746 processMTSync(mapper);
7747 processSync(mapper);
7748
7749 NotifyMotionArgs args;
7750 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7751 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7752 x, y, pressure, 0, 0, 0, 0, 0, 0, 0));
7753}
7754
7755TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007756 addConfigurationProperty("touch.deviceType", "touchScreen");
7757 prepareDisplay(DISPLAY_ORIENTATION_0);
7758 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007759 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007760
7761 NotifyMotionArgs motionArgs;
7762 NotifyKeyArgs keyArgs;
7763
7764 processId(mapper, 1);
7765 processPosition(mapper, 100, 200);
7766 processSync(mapper);
7767 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7768 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7769 ASSERT_EQ(0, motionArgs.buttonState);
7770
7771 // press BTN_LEFT, release BTN_LEFT
7772 processKey(mapper, BTN_LEFT, 1);
7773 processSync(mapper);
7774 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7775 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7776 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
7777
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007778 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7779 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7780 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
7781
Michael Wrightd02c5b62014-02-10 15:10:22 -08007782 processKey(mapper, BTN_LEFT, 0);
7783 processSync(mapper);
7784 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007785 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007786 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007787
7788 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007789 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007790 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007791
7792 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
7793 processKey(mapper, BTN_RIGHT, 1);
7794 processKey(mapper, BTN_MIDDLE, 1);
7795 processSync(mapper);
7796 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7797 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7798 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
7799 motionArgs.buttonState);
7800
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007801 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7802 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7803 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
7804
7805 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7806 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7807 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
7808 motionArgs.buttonState);
7809
Michael Wrightd02c5b62014-02-10 15:10:22 -08007810 processKey(mapper, BTN_RIGHT, 0);
7811 processSync(mapper);
7812 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007813 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007814 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007815
7816 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007817 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007818 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007819
7820 processKey(mapper, BTN_MIDDLE, 0);
7821 processSync(mapper);
7822 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007823 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007824 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007825
7826 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007827 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007828 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007829
7830 // press BTN_BACK, release BTN_BACK
7831 processKey(mapper, BTN_BACK, 1);
7832 processSync(mapper);
7833 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7834 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
7835 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007836
Michael Wrightd02c5b62014-02-10 15:10:22 -08007837 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007838 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007839 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
7840
7841 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7842 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7843 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007844
7845 processKey(mapper, BTN_BACK, 0);
7846 processSync(mapper);
7847 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007848 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007849 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007850
7851 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007852 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007853 ASSERT_EQ(0, motionArgs.buttonState);
7854
Michael Wrightd02c5b62014-02-10 15:10:22 -08007855 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7856 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
7857 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
7858
7859 // press BTN_SIDE, release BTN_SIDE
7860 processKey(mapper, BTN_SIDE, 1);
7861 processSync(mapper);
7862 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7863 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
7864 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007865
Michael Wrightd02c5b62014-02-10 15:10:22 -08007866 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007867 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007868 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
7869
7870 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7871 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7872 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007873
7874 processKey(mapper, BTN_SIDE, 0);
7875 processSync(mapper);
7876 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007877 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007878 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007879
7880 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007881 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007882 ASSERT_EQ(0, motionArgs.buttonState);
7883
Michael Wrightd02c5b62014-02-10 15:10:22 -08007884 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7885 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
7886 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
7887
7888 // press BTN_FORWARD, release BTN_FORWARD
7889 processKey(mapper, BTN_FORWARD, 1);
7890 processSync(mapper);
7891 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7892 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
7893 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007894
Michael Wrightd02c5b62014-02-10 15:10:22 -08007895 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007896 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007897 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
7898
7899 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7900 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7901 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007902
7903 processKey(mapper, BTN_FORWARD, 0);
7904 processSync(mapper);
7905 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007906 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007907 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007908
7909 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007910 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007911 ASSERT_EQ(0, motionArgs.buttonState);
7912
Michael Wrightd02c5b62014-02-10 15:10:22 -08007913 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7914 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
7915 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
7916
7917 // press BTN_EXTRA, release BTN_EXTRA
7918 processKey(mapper, BTN_EXTRA, 1);
7919 processSync(mapper);
7920 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7921 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
7922 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007923
Michael Wrightd02c5b62014-02-10 15:10:22 -08007924 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007925 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007926 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
7927
7928 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7929 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7930 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007931
7932 processKey(mapper, BTN_EXTRA, 0);
7933 processSync(mapper);
7934 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007935 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007936 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007937
7938 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007939 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007940 ASSERT_EQ(0, motionArgs.buttonState);
7941
Michael Wrightd02c5b62014-02-10 15:10:22 -08007942 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7943 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
7944 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
7945
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007946 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7947
Michael Wrightd02c5b62014-02-10 15:10:22 -08007948 // press BTN_STYLUS, release BTN_STYLUS
7949 processKey(mapper, BTN_STYLUS, 1);
7950 processSync(mapper);
7951 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7952 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007953 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
7954
7955 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7956 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7957 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007958
7959 processKey(mapper, BTN_STYLUS, 0);
7960 processSync(mapper);
7961 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007962 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007963 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007964
7965 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007966 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007967 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007968
7969 // press BTN_STYLUS2, release BTN_STYLUS2
7970 processKey(mapper, BTN_STYLUS2, 1);
7971 processSync(mapper);
7972 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7973 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007974 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
7975
7976 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7977 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7978 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007979
7980 processKey(mapper, BTN_STYLUS2, 0);
7981 processSync(mapper);
7982 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007983 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007984 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007985
7986 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007987 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007988 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007989
7990 // release touch
7991 processId(mapper, -1);
7992 processSync(mapper);
7993 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7994 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7995 ASSERT_EQ(0, motionArgs.buttonState);
7996}
7997
7998TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007999 addConfigurationProperty("touch.deviceType", "touchScreen");
8000 prepareDisplay(DISPLAY_ORIENTATION_0);
8001 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008002 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008003
8004 NotifyMotionArgs motionArgs;
8005
8006 // default tool type is finger
8007 processId(mapper, 1);
8008 processPosition(mapper, 100, 200);
8009 processSync(mapper);
8010 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8011 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8012 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8013
8014 // eraser
8015 processKey(mapper, BTN_TOOL_RUBBER, 1);
8016 processSync(mapper);
8017 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8018 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8019 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
8020
8021 // stylus
8022 processKey(mapper, BTN_TOOL_RUBBER, 0);
8023 processKey(mapper, BTN_TOOL_PEN, 1);
8024 processSync(mapper);
8025 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8026 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8027 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8028
8029 // brush
8030 processKey(mapper, BTN_TOOL_PEN, 0);
8031 processKey(mapper, BTN_TOOL_BRUSH, 1);
8032 processSync(mapper);
8033 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8034 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8035 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8036
8037 // pencil
8038 processKey(mapper, BTN_TOOL_BRUSH, 0);
8039 processKey(mapper, BTN_TOOL_PENCIL, 1);
8040 processSync(mapper);
8041 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8042 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8043 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8044
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08008045 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08008046 processKey(mapper, BTN_TOOL_PENCIL, 0);
8047 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
8048 processSync(mapper);
8049 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8050 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8051 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8052
8053 // mouse
8054 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
8055 processKey(mapper, BTN_TOOL_MOUSE, 1);
8056 processSync(mapper);
8057 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8058 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8059 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
8060
8061 // lens
8062 processKey(mapper, BTN_TOOL_MOUSE, 0);
8063 processKey(mapper, BTN_TOOL_LENS, 1);
8064 processSync(mapper);
8065 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8066 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8067 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
8068
8069 // double-tap
8070 processKey(mapper, BTN_TOOL_LENS, 0);
8071 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
8072 processSync(mapper);
8073 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8074 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8075 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8076
8077 // triple-tap
8078 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
8079 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
8080 processSync(mapper);
8081 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8082 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8083 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8084
8085 // quad-tap
8086 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
8087 processKey(mapper, BTN_TOOL_QUADTAP, 1);
8088 processSync(mapper);
8089 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8090 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8091 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8092
8093 // finger
8094 processKey(mapper, BTN_TOOL_QUADTAP, 0);
8095 processKey(mapper, BTN_TOOL_FINGER, 1);
8096 processSync(mapper);
8097 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8098 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8099 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8100
8101 // stylus trumps finger
8102 processKey(mapper, BTN_TOOL_PEN, 1);
8103 processSync(mapper);
8104 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8105 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8106 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8107
8108 // eraser trumps stylus
8109 processKey(mapper, BTN_TOOL_RUBBER, 1);
8110 processSync(mapper);
8111 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8112 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8113 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
8114
8115 // mouse trumps eraser
8116 processKey(mapper, BTN_TOOL_MOUSE, 1);
8117 processSync(mapper);
8118 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8119 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8120 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
8121
8122 // MT tool type trumps BTN tool types: MT_TOOL_FINGER
8123 processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
8124 processSync(mapper);
8125 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8126 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8127 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8128
8129 // MT tool type trumps BTN tool types: MT_TOOL_PEN
8130 processToolType(mapper, MT_TOOL_PEN);
8131 processSync(mapper);
8132 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8133 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8134 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8135
8136 // back to default tool type
8137 processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
8138 processKey(mapper, BTN_TOOL_MOUSE, 0);
8139 processKey(mapper, BTN_TOOL_RUBBER, 0);
8140 processKey(mapper, BTN_TOOL_PEN, 0);
8141 processKey(mapper, BTN_TOOL_FINGER, 0);
8142 processSync(mapper);
8143 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8144 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8145 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8146}
8147
8148TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008149 addConfigurationProperty("touch.deviceType", "touchScreen");
8150 prepareDisplay(DISPLAY_ORIENTATION_0);
8151 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008152 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008153 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008154
8155 NotifyMotionArgs motionArgs;
8156
8157 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
8158 processId(mapper, 1);
8159 processPosition(mapper, 100, 200);
8160 processSync(mapper);
8161 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8162 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8163 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8164 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8165
8166 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8167 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8168 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8169 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8170
8171 // move a little
8172 processPosition(mapper, 150, 250);
8173 processSync(mapper);
8174 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8175 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8176 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8177 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8178
8179 // down when BTN_TOUCH is pressed, pressure defaults to 1
8180 processKey(mapper, BTN_TOUCH, 1);
8181 processSync(mapper);
8182 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8183 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8184 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8185 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8186
8187 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8188 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8189 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8190 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8191
8192 // up when BTN_TOUCH is released, hover restored
8193 processKey(mapper, BTN_TOUCH, 0);
8194 processSync(mapper);
8195 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8196 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8197 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8198 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8199
8200 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8201 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8202 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8203 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8204
8205 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8206 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8207 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8208 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8209
8210 // exit hover when pointer goes away
8211 processId(mapper, -1);
8212 processSync(mapper);
8213 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8214 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8215 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8216 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8217}
8218
8219TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008220 addConfigurationProperty("touch.deviceType", "touchScreen");
8221 prepareDisplay(DISPLAY_ORIENTATION_0);
8222 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008223 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008224
8225 NotifyMotionArgs motionArgs;
8226
8227 // initially hovering because pressure is 0
8228 processId(mapper, 1);
8229 processPosition(mapper, 100, 200);
8230 processPressure(mapper, 0);
8231 processSync(mapper);
8232 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8233 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8234 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8235 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8236
8237 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8238 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8239 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8240 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8241
8242 // move a little
8243 processPosition(mapper, 150, 250);
8244 processSync(mapper);
8245 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8246 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8247 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8248 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8249
8250 // down when pressure becomes non-zero
8251 processPressure(mapper, RAW_PRESSURE_MAX);
8252 processSync(mapper);
8253 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8254 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8255 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8256 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8257
8258 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8259 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8260 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8261 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8262
8263 // up when pressure becomes 0, hover restored
8264 processPressure(mapper, 0);
8265 processSync(mapper);
8266 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8267 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8268 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8269 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8270
8271 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8272 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8273 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8274 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8275
8276 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8277 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8278 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8279 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8280
8281 // exit hover when pointer goes away
8282 processId(mapper, -1);
8283 processSync(mapper);
8284 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8285 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8286 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8287 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8288}
8289
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008290/**
8291 * Set the input device port <--> display port associations, and check that the
8292 * events are routed to the display that matches the display port.
8293 * This can be checked by looking at the displayId of the resulting NotifyMotionArgs.
8294 */
8295TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayPort) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008296 const std::string usb2 = "USB2";
8297 const uint8_t hdmi1 = 0;
8298 const uint8_t hdmi2 = 1;
8299 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008300 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008301
8302 addConfigurationProperty("touch.deviceType", "touchScreen");
8303 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008304 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008305
8306 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
8307 mFakePolicy->addInputPortAssociation(usb2, hdmi2);
8308
8309 // We are intentionally not adding the viewport for display 1 yet. Since the port association
8310 // for this input device is specified, and the matching viewport is not present,
8311 // the input device should be disabled (at the mapper level).
8312
8313 // Add viewport for display 2 on hdmi2
8314 prepareSecondaryDisplay(type, hdmi2);
8315 // Send a touch event
8316 processPosition(mapper, 100, 100);
8317 processSync(mapper);
8318 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8319
8320 // Add viewport for display 1 on hdmi1
8321 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
8322 // Send a touch event again
8323 processPosition(mapper, 100, 100);
8324 processSync(mapper);
8325
8326 NotifyMotionArgs args;
8327 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8328 ASSERT_EQ(DISPLAY_ID, args.displayId);
8329}
Michael Wrightd02c5b62014-02-10 15:10:22 -08008330
Arthur Hung6d5b4b22022-01-21 07:21:10 +00008331TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayUniqueId) {
8332 addConfigurationProperty("touch.deviceType", "touchScreen");
8333 prepareAxes(POSITION);
8334 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8335
8336 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
8337
8338 prepareDisplay(DISPLAY_ORIENTATION_0);
8339 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
8340
8341 // Send a touch event
8342 processPosition(mapper, 100, 100);
8343 processSync(mapper);
8344
8345 NotifyMotionArgs args;
8346 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8347 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
8348}
8349
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008350TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShouldHandleDisplayId) {
Garfield Tan888a6a42020-01-09 11:39:16 -08008351 // Setup for second display.
Michael Wright17db18e2020-06-26 20:51:44 +01008352 std::shared_ptr<FakePointerController> fakePointerController =
8353 std::make_shared<FakePointerController>();
Garfield Tan888a6a42020-01-09 11:39:16 -08008354 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008355 fakePointerController->setPosition(100, 200);
8356 fakePointerController->setButtonState(0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00008357 mFakePolicy->setPointerController(fakePointerController);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008358
Garfield Tan888a6a42020-01-09 11:39:16 -08008359 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008360 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08008361
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008362 prepareDisplay(DISPLAY_ORIENTATION_0);
8363 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008364 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008365
8366 // Check source is mouse that would obtain the PointerController.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008367 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008368
8369 NotifyMotionArgs motionArgs;
8370 processPosition(mapper, 100, 100);
8371 processSync(mapper);
8372
8373 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8374 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8375 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
8376}
8377
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00008378/**
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008379 * Ensure that the readTime is set to the SYN_REPORT value when processing touch events.
8380 */
8381TEST_F(MultiTouchInputMapperTest, Process_SendsReadTime) {
8382 addConfigurationProperty("touch.deviceType", "touchScreen");
8383 prepareAxes(POSITION);
8384 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8385
8386 prepareDisplay(DISPLAY_ORIENTATION_0);
8387 process(mapper, 10, 11 /*readTime*/, EV_ABS, ABS_MT_TRACKING_ID, 1);
8388 process(mapper, 15, 16 /*readTime*/, EV_ABS, ABS_MT_POSITION_X, 100);
8389 process(mapper, 20, 21 /*readTime*/, EV_ABS, ABS_MT_POSITION_Y, 100);
8390 process(mapper, 25, 26 /*readTime*/, EV_SYN, SYN_REPORT, 0);
8391
8392 NotifyMotionArgs args;
8393 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8394 ASSERT_EQ(26, args.readTime);
8395
8396 process(mapper, 30, 31 /*readTime*/, EV_ABS, ABS_MT_POSITION_X, 110);
8397 process(mapper, 30, 32 /*readTime*/, EV_ABS, ABS_MT_POSITION_Y, 220);
8398 process(mapper, 30, 33 /*readTime*/, EV_SYN, SYN_REPORT, 0);
8399
8400 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8401 ASSERT_EQ(33, args.readTime);
8402}
8403
8404/**
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00008405 * When the viewport is not active (isActive=false), the touch mapper should be disabled and the
8406 * events should not be delivered to the listener.
8407 */
8408TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreDropped) {
8409 addConfigurationProperty("touch.deviceType", "touchScreen");
8410 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
8411 DISPLAY_ORIENTATION_0, false /*isActive*/, UNIQUE_ID, NO_PORT,
8412 ViewportType::INTERNAL);
8413 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8414 prepareAxes(POSITION);
8415 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8416
8417 NotifyMotionArgs motionArgs;
8418 processPosition(mapper, 100, 100);
8419 processSync(mapper);
8420
8421 mFakeListener->assertNotifyMotionWasNotCalled();
8422}
8423
Garfield Tanc734e4f2021-01-15 20:01:39 -08008424TEST_F(MultiTouchInputMapperTest, Process_DeactivateViewport_AbortTouches) {
8425 addConfigurationProperty("touch.deviceType", "touchScreen");
8426 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
8427 DISPLAY_ORIENTATION_0, true /*isActive*/, UNIQUE_ID, NO_PORT,
8428 ViewportType::INTERNAL);
8429 std::optional<DisplayViewport> optionalDisplayViewport =
8430 mFakePolicy->getDisplayViewportByUniqueId(UNIQUE_ID);
8431 ASSERT_TRUE(optionalDisplayViewport.has_value());
8432 DisplayViewport displayViewport = *optionalDisplayViewport;
8433
8434 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8435 prepareAxes(POSITION);
8436 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8437
8438 // Finger down
8439 int32_t x = 100, y = 100;
8440 processPosition(mapper, x, y);
8441 processSync(mapper);
8442
8443 NotifyMotionArgs motionArgs;
8444 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8445 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8446
8447 // Deactivate display viewport
8448 displayViewport.isActive = false;
8449 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
8450 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8451
8452 // Finger move
8453 x += 10, y += 10;
8454 processPosition(mapper, x, y);
8455 processSync(mapper);
8456
8457 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8458 EXPECT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
8459
8460 // Reactivate display viewport
8461 displayViewport.isActive = true;
8462 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
8463 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8464
8465 // Finger move again
8466 x += 10, y += 10;
8467 processPosition(mapper, x, y);
8468 processSync(mapper);
8469
8470 // Gesture is aborted, so events after display is activated won't be dispatched until there is
8471 // no pointer on the touch device.
8472 mFakeListener->assertNotifyMotionWasNotCalled();
8473}
8474
Arthur Hung7c645402019-01-25 17:45:42 +08008475TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShowTouches) {
8476 // Setup the first touch screen device.
Arthur Hung7c645402019-01-25 17:45:42 +08008477 prepareAxes(POSITION | ID | SLOT);
8478 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008479 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung7c645402019-01-25 17:45:42 +08008480
8481 // Create the second touch screen device, and enable multi fingers.
8482 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08008483 const std::string DEVICE_NAME2 = "TOUCHSCREEN2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08008484 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008485 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08008486 std::shared_ptr<InputDevice> device2 =
8487 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07008488 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08008489
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008490 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
8491 0 /*flat*/, 0 /*fuzz*/);
8492 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
8493 0 /*flat*/, 0 /*fuzz*/);
8494 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX,
8495 0 /*flat*/, 0 /*fuzz*/);
8496 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX,
8497 0 /*flat*/, 0 /*fuzz*/);
8498 mFakeEventHub->setAbsoluteAxisValue(SECOND_EVENTHUB_ID, ABS_MT_SLOT, 0 /*value*/);
8499 mFakeEventHub->addConfigurationProperty(SECOND_EVENTHUB_ID, String8("touch.deviceType"),
8500 String8("touchScreen"));
Arthur Hung7c645402019-01-25 17:45:42 +08008501
8502 // Setup the second touch screen device.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008503 MultiTouchInputMapper& mapper2 = device2->addMapper<MultiTouchInputMapper>(SECOND_EVENTHUB_ID);
Arthur Hung7c645402019-01-25 17:45:42 +08008504 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
8505 device2->reset(ARBITRARY_TIME);
8506
8507 // Setup PointerController.
Michael Wright17db18e2020-06-26 20:51:44 +01008508 std::shared_ptr<FakePointerController> fakePointerController =
8509 std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00008510 mFakePolicy->setPointerController(fakePointerController);
Arthur Hung7c645402019-01-25 17:45:42 +08008511
8512 // Setup policy for associated displays and show touches.
8513 const uint8_t hdmi1 = 0;
8514 const uint8_t hdmi2 = 1;
8515 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
8516 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
8517 mFakePolicy->setShowTouches(true);
8518
8519 // Create displays.
8520 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008521 prepareSecondaryDisplay(ViewportType::EXTERNAL, hdmi2);
Arthur Hung7c645402019-01-25 17:45:42 +08008522
8523 // Default device will reconfigure above, need additional reconfiguration for another device.
8524 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008525 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung7c645402019-01-25 17:45:42 +08008526
8527 // Two fingers down at default display.
8528 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
8529 processPosition(mapper, x1, y1);
8530 processId(mapper, 1);
8531 processSlot(mapper, 1);
8532 processPosition(mapper, x2, y2);
8533 processId(mapper, 2);
8534 processSync(mapper);
8535
8536 std::map<int32_t, std::vector<int32_t>>::const_iterator iter =
8537 fakePointerController->getSpots().find(DISPLAY_ID);
8538 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
8539 ASSERT_EQ(size_t(2), iter->second.size());
8540
8541 // Two fingers down at second display.
8542 processPosition(mapper2, x1, y1);
8543 processId(mapper2, 1);
8544 processSlot(mapper2, 1);
8545 processPosition(mapper2, x2, y2);
8546 processId(mapper2, 2);
8547 processSync(mapper2);
8548
8549 iter = fakePointerController->getSpots().find(SECONDARY_DISPLAY_ID);
8550 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
8551 ASSERT_EQ(size_t(2), iter->second.size());
8552}
8553
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008554TEST_F(MultiTouchInputMapperTest, VideoFrames_ReceivedByListener) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008555 prepareAxes(POSITION);
8556 addConfigurationProperty("touch.deviceType", "touchScreen");
8557 prepareDisplay(DISPLAY_ORIENTATION_0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008558 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008559
8560 NotifyMotionArgs motionArgs;
8561 // Unrotated video frame
8562 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8563 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008564 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008565 processPosition(mapper, 100, 200);
8566 processSync(mapper);
8567 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8568 ASSERT_EQ(frames, motionArgs.videoFrames);
8569
8570 // Subsequent touch events should not have any videoframes
8571 // This is implemented separately in FakeEventHub,
8572 // but that should match the behaviour of TouchVideoDevice.
8573 processPosition(mapper, 200, 200);
8574 processSync(mapper);
8575 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8576 ASSERT_EQ(std::vector<TouchVideoFrame>(), motionArgs.videoFrames);
8577}
8578
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008579TEST_F(MultiTouchInputMapperTest, VideoFrames_AreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008580 prepareAxes(POSITION);
8581 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008582 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008583 // Unrotated video frame
8584 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8585 NotifyMotionArgs motionArgs;
8586
8587 // Test all 4 orientations
8588 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008589 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
8590 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
8591 clearViewports();
8592 prepareDisplay(orientation);
8593 std::vector<TouchVideoFrame> frames{frame};
8594 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
8595 processPosition(mapper, 100, 200);
8596 processSync(mapper);
8597 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8598 ASSERT_EQ(frames, motionArgs.videoFrames);
8599 }
8600}
8601
8602TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_AreRotated) {
8603 prepareAxes(POSITION);
8604 addConfigurationProperty("touch.deviceType", "touchScreen");
8605 // Since InputReader works in the un-rotated coordinate space, only devices that are not
8606 // orientation-aware are affected by display rotation.
8607 addConfigurationProperty("touch.orientationAware", "0");
8608 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8609 // Unrotated video frame
8610 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8611 NotifyMotionArgs motionArgs;
8612
8613 // Test all 4 orientations
8614 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008615 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
8616 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
8617 clearViewports();
8618 prepareDisplay(orientation);
8619 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008620 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008621 processPosition(mapper, 100, 200);
8622 processSync(mapper);
8623 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008624 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
8625 // compared to the display. This is so that when the window transform (which contains the
8626 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
8627 // window's coordinate space.
8628 frames[0].rotate(getInverseRotation(orientation));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008629 ASSERT_EQ(frames, motionArgs.videoFrames);
8630 }
8631}
8632
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008633TEST_F(MultiTouchInputMapperTest, VideoFrames_MultipleFramesAreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008634 prepareAxes(POSITION);
8635 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008636 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008637 // Unrotated video frames. There's no rule that they must all have the same dimensions,
8638 // so mix these.
8639 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8640 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
8641 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
8642 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
8643 NotifyMotionArgs motionArgs;
8644
8645 prepareDisplay(DISPLAY_ORIENTATION_90);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008646 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008647 processPosition(mapper, 100, 200);
8648 processSync(mapper);
8649 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008650 ASSERT_EQ(frames, motionArgs.videoFrames);
8651}
8652
8653TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_MultipleFramesAreRotated) {
8654 prepareAxes(POSITION);
8655 addConfigurationProperty("touch.deviceType", "touchScreen");
8656 // Since InputReader works in the un-rotated coordinate space, only devices that are not
8657 // orientation-aware are affected by display rotation.
8658 addConfigurationProperty("touch.orientationAware", "0");
8659 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8660 // Unrotated video frames. There's no rule that they must all have the same dimensions,
8661 // so mix these.
8662 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8663 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
8664 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
8665 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
8666 NotifyMotionArgs motionArgs;
8667
8668 prepareDisplay(DISPLAY_ORIENTATION_90);
8669 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
8670 processPosition(mapper, 100, 200);
8671 processSync(mapper);
8672 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8673 std::for_each(frames.begin(), frames.end(), [](TouchVideoFrame& frame) {
8674 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
8675 // compared to the display. This is so that when the window transform (which contains the
8676 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
8677 // window's coordinate space.
8678 frame.rotate(getInverseRotation(DISPLAY_ORIENTATION_90));
8679 });
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008680 ASSERT_EQ(frames, motionArgs.videoFrames);
8681}
8682
Arthur Hung9da14732019-09-02 16:16:58 +08008683/**
8684 * If we had defined port associations, but the viewport is not ready, the touch device would be
8685 * expected to be disabled, and it should be enabled after the viewport has found.
8686 */
8687TEST_F(MultiTouchInputMapperTest, Configure_EnabledForAssociatedDisplay) {
Arthur Hung9da14732019-09-02 16:16:58 +08008688 constexpr uint8_t hdmi2 = 1;
8689 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008690 constexpr ViewportType type = ViewportType::EXTERNAL;
Arthur Hung9da14732019-09-02 16:16:58 +08008691
8692 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi2);
8693
8694 addConfigurationProperty("touch.deviceType", "touchScreen");
8695 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008696 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung9da14732019-09-02 16:16:58 +08008697
8698 ASSERT_EQ(mDevice->isEnabled(), false);
8699
8700 // Add display on hdmi2, the device should be enabled and can receive touch event.
8701 prepareSecondaryDisplay(type, hdmi2);
8702 ASSERT_EQ(mDevice->isEnabled(), true);
8703
8704 // Send a touch event.
8705 processPosition(mapper, 100, 100);
8706 processSync(mapper);
8707
8708 NotifyMotionArgs args;
8709 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8710 ASSERT_EQ(SECONDARY_DISPLAY_ID, args.displayId);
8711}
8712
Arthur Hung421eb1c2020-01-16 00:09:42 +08008713TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleSingleTouch) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08008714 addConfigurationProperty("touch.deviceType", "touchScreen");
8715 prepareDisplay(DISPLAY_ORIENTATION_0);
8716 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008717 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08008718
8719 NotifyMotionArgs motionArgs;
8720
8721 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
8722 // finger down
8723 processId(mapper, 1);
8724 processPosition(mapper, x1, y1);
8725 processSync(mapper);
8726 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8727 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8728 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8729
8730 // finger move
8731 processId(mapper, 1);
8732 processPosition(mapper, x2, y2);
8733 processSync(mapper);
8734 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8735 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8736 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8737
8738 // finger up.
8739 processId(mapper, -1);
8740 processSync(mapper);
8741 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8742 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8743 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8744
8745 // new finger down
8746 processId(mapper, 1);
8747 processPosition(mapper, x3, y3);
8748 processSync(mapper);
8749 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8750 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8751 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8752}
8753
8754/**
arthurhungcc7f9802020-04-30 17:55:40 +08008755 * Test single touch should be canceled when received the MT_TOOL_PALM event, and the following
8756 * MOVE and UP events should be ignored.
Arthur Hung421eb1c2020-01-16 00:09:42 +08008757 */
arthurhungcc7f9802020-04-30 17:55:40 +08008758TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_SinglePointer) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08008759 addConfigurationProperty("touch.deviceType", "touchScreen");
8760 prepareDisplay(DISPLAY_ORIENTATION_0);
8761 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008762 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08008763
8764 NotifyMotionArgs motionArgs;
8765
8766 // default tool type is finger
8767 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
arthurhungcc7f9802020-04-30 17:55:40 +08008768 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008769 processPosition(mapper, x1, y1);
8770 processSync(mapper);
8771 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8772 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8773 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8774
8775 // Tool changed to MT_TOOL_PALM expect sending the cancel event.
8776 processToolType(mapper, MT_TOOL_PALM);
8777 processSync(mapper);
8778 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8779 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
8780
8781 // Ignore the following MOVE and UP events if had detect a palm event.
arthurhungcc7f9802020-04-30 17:55:40 +08008782 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008783 processPosition(mapper, x2, y2);
8784 processSync(mapper);
8785 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8786
8787 // finger up.
arthurhungcc7f9802020-04-30 17:55:40 +08008788 processId(mapper, INVALID_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008789 processSync(mapper);
8790 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8791
8792 // new finger down
arthurhungcc7f9802020-04-30 17:55:40 +08008793 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008794 processToolType(mapper, MT_TOOL_FINGER);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008795 processPosition(mapper, x3, y3);
8796 processSync(mapper);
8797 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8798 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8799 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8800}
8801
arthurhungbf89a482020-04-17 17:37:55 +08008802/**
arthurhungcc7f9802020-04-30 17:55:40 +08008803 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
8804 * and the rest active fingers could still be allowed to receive the events
arthurhungbf89a482020-04-17 17:37:55 +08008805 */
arthurhungcc7f9802020-04-30 17:55:40 +08008806TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_TwoPointers) {
arthurhungbf89a482020-04-17 17:37:55 +08008807 addConfigurationProperty("touch.deviceType", "touchScreen");
8808 prepareDisplay(DISPLAY_ORIENTATION_0);
8809 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
8810 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8811
8812 NotifyMotionArgs motionArgs;
8813
8814 // default tool type is finger
arthurhungcc7f9802020-04-30 17:55:40 +08008815 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
8816 processId(mapper, FIRST_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08008817 processPosition(mapper, x1, y1);
8818 processSync(mapper);
8819 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8820 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8821 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8822
8823 // Second finger down.
arthurhungcc7f9802020-04-30 17:55:40 +08008824 processSlot(mapper, SECOND_SLOT);
8825 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08008826 processPosition(mapper, x2, y2);
arthurhungcc7f9802020-04-30 17:55:40 +08008827 processSync(mapper);
8828 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008829 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08008830 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8831
8832 // If the tool type of the first finger changes to MT_TOOL_PALM,
8833 // we expect to receive ACTION_POINTER_UP with cancel flag.
8834 processSlot(mapper, FIRST_SLOT);
8835 processId(mapper, FIRST_TRACKING_ID);
8836 processToolType(mapper, MT_TOOL_PALM);
8837 processSync(mapper);
8838 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008839 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08008840 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
8841
8842 // The following MOVE events of second finger should be processed.
8843 processSlot(mapper, SECOND_SLOT);
8844 processId(mapper, SECOND_TRACKING_ID);
8845 processPosition(mapper, x2 + 1, y2 + 1);
8846 processSync(mapper);
8847 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8848 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8849 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8850
8851 // First finger up. It used to be in palm mode, and we already generated ACTION_POINTER_UP for
8852 // it. Second finger receive move.
8853 processSlot(mapper, FIRST_SLOT);
8854 processId(mapper, INVALID_TRACKING_ID);
8855 processSync(mapper);
8856 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8857 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8858 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8859
8860 // Second finger keeps moving.
8861 processSlot(mapper, SECOND_SLOT);
8862 processId(mapper, SECOND_TRACKING_ID);
8863 processPosition(mapper, x2 + 2, y2 + 2);
8864 processSync(mapper);
8865 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8866 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8867 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8868
8869 // Second finger up.
8870 processId(mapper, INVALID_TRACKING_ID);
8871 processSync(mapper);
8872 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8873 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8874 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
8875}
8876
8877/**
8878 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event, if only 1 finger
8879 * is active, it should send CANCEL after receiving the MT_TOOL_PALM event.
8880 */
8881TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_ShouldCancelWhenAllTouchIsPalm) {
8882 addConfigurationProperty("touch.deviceType", "touchScreen");
8883 prepareDisplay(DISPLAY_ORIENTATION_0);
8884 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
8885 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8886
8887 NotifyMotionArgs motionArgs;
8888
8889 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
8890 // First finger down.
8891 processId(mapper, FIRST_TRACKING_ID);
8892 processPosition(mapper, x1, y1);
8893 processSync(mapper);
8894 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8895 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8896 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8897
8898 // Second finger down.
8899 processSlot(mapper, SECOND_SLOT);
8900 processId(mapper, SECOND_TRACKING_ID);
8901 processPosition(mapper, x2, y2);
arthurhungbf89a482020-04-17 17:37:55 +08008902 processSync(mapper);
8903 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008904 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungbf89a482020-04-17 17:37:55 +08008905 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8906
arthurhungcc7f9802020-04-30 17:55:40 +08008907 // If the tool type of the first finger changes to MT_TOOL_PALM,
8908 // we expect to receive ACTION_POINTER_UP with cancel flag.
8909 processSlot(mapper, FIRST_SLOT);
8910 processId(mapper, FIRST_TRACKING_ID);
8911 processToolType(mapper, MT_TOOL_PALM);
8912 processSync(mapper);
8913 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008914 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08008915 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
8916
8917 // Second finger keeps moving.
8918 processSlot(mapper, SECOND_SLOT);
8919 processId(mapper, SECOND_TRACKING_ID);
8920 processPosition(mapper, x2 + 1, y2 + 1);
8921 processSync(mapper);
8922 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8923 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8924
8925 // second finger becomes palm, receive cancel due to only 1 finger is active.
8926 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08008927 processToolType(mapper, MT_TOOL_PALM);
8928 processSync(mapper);
8929 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8930 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
8931
arthurhungcc7f9802020-04-30 17:55:40 +08008932 // third finger down.
8933 processSlot(mapper, THIRD_SLOT);
8934 processId(mapper, THIRD_TRACKING_ID);
8935 processToolType(mapper, MT_TOOL_FINGER);
arthurhungbf89a482020-04-17 17:37:55 +08008936 processPosition(mapper, x3, y3);
8937 processSync(mapper);
arthurhungbf89a482020-04-17 17:37:55 +08008938 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8939 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8940 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08008941 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8942
8943 // third finger move
8944 processId(mapper, THIRD_TRACKING_ID);
8945 processPosition(mapper, x3 + 1, y3 + 1);
8946 processSync(mapper);
8947 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8948 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8949
8950 // first finger up, third finger receive move.
8951 processSlot(mapper, FIRST_SLOT);
8952 processId(mapper, INVALID_TRACKING_ID);
8953 processSync(mapper);
8954 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8955 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8956 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8957
8958 // second finger up, third finger receive move.
8959 processSlot(mapper, SECOND_SLOT);
8960 processId(mapper, INVALID_TRACKING_ID);
8961 processSync(mapper);
8962 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8963 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8964 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8965
8966 // third finger up.
8967 processSlot(mapper, THIRD_SLOT);
8968 processId(mapper, INVALID_TRACKING_ID);
8969 processSync(mapper);
8970 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8971 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8972 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
8973}
8974
8975/**
8976 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
8977 * and the active finger could still be allowed to receive the events
8978 */
8979TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_KeepFirstPointer) {
8980 addConfigurationProperty("touch.deviceType", "touchScreen");
8981 prepareDisplay(DISPLAY_ORIENTATION_0);
8982 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
8983 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8984
8985 NotifyMotionArgs motionArgs;
8986
8987 // default tool type is finger
8988 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
8989 processId(mapper, FIRST_TRACKING_ID);
8990 processPosition(mapper, x1, y1);
8991 processSync(mapper);
8992 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8993 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8994 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8995
8996 // Second finger down.
8997 processSlot(mapper, SECOND_SLOT);
8998 processId(mapper, SECOND_TRACKING_ID);
8999 processPosition(mapper, x2, y2);
9000 processSync(mapper);
9001 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009002 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009003 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9004
9005 // If the tool type of the second finger changes to MT_TOOL_PALM,
9006 // we expect to receive ACTION_POINTER_UP with cancel flag.
9007 processId(mapper, SECOND_TRACKING_ID);
9008 processToolType(mapper, MT_TOOL_PALM);
9009 processSync(mapper);
9010 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009011 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009012 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9013
9014 // The following MOVE event should be processed.
9015 processSlot(mapper, FIRST_SLOT);
9016 processId(mapper, FIRST_TRACKING_ID);
9017 processPosition(mapper, x1 + 1, y1 + 1);
9018 processSync(mapper);
9019 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9020 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9021 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9022
9023 // second finger up.
9024 processSlot(mapper, SECOND_SLOT);
9025 processId(mapper, INVALID_TRACKING_ID);
9026 processSync(mapper);
9027 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9028 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9029
9030 // first finger keep moving
9031 processSlot(mapper, FIRST_SLOT);
9032 processId(mapper, FIRST_TRACKING_ID);
9033 processPosition(mapper, x1 + 2, y1 + 2);
9034 processSync(mapper);
9035 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9036 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9037
9038 // first finger up.
9039 processId(mapper, INVALID_TRACKING_ID);
9040 processSync(mapper);
9041 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9042 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9043 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
arthurhungbf89a482020-04-17 17:37:55 +08009044}
9045
Arthur Hung9ad18942021-06-19 02:04:46 +00009046/**
9047 * Test multi-touch should sent ACTION_POINTER_UP/ACTION_UP when received the INVALID_TRACKING_ID,
9048 * to prevent the driver side may send unexpected data after set tracking id as INVALID_TRACKING_ID
9049 * cause slot be valid again.
9050 */
9051TEST_F(MultiTouchInputMapperTest, Process_MultiTouch_WithInvalidTrackingId) {
9052 addConfigurationProperty("touch.deviceType", "touchScreen");
9053 prepareDisplay(DISPLAY_ORIENTATION_0);
9054 prepareAxes(POSITION | ID | SLOT | PRESSURE);
9055 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9056
9057 NotifyMotionArgs motionArgs;
9058
9059 constexpr int32_t x1 = 100, y1 = 200, x2 = 0, y2 = 0;
9060 // First finger down.
9061 processId(mapper, FIRST_TRACKING_ID);
9062 processPosition(mapper, x1, y1);
9063 processPressure(mapper, RAW_PRESSURE_MAX);
9064 processSync(mapper);
9065 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9066 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9067 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9068
9069 // First finger move.
9070 processId(mapper, FIRST_TRACKING_ID);
9071 processPosition(mapper, x1 + 1, y1 + 1);
9072 processPressure(mapper, RAW_PRESSURE_MAX);
9073 processSync(mapper);
9074 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9075 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9076 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9077
9078 // Second finger down.
9079 processSlot(mapper, SECOND_SLOT);
9080 processId(mapper, SECOND_TRACKING_ID);
9081 processPosition(mapper, x2, y2);
9082 processPressure(mapper, RAW_PRESSURE_MAX);
9083 processSync(mapper);
9084 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009085 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Arthur Hung9ad18942021-06-19 02:04:46 +00009086 ASSERT_EQ(uint32_t(2), motionArgs.pointerCount);
9087
9088 // second finger up with some unexpected data.
9089 processSlot(mapper, SECOND_SLOT);
9090 processId(mapper, INVALID_TRACKING_ID);
9091 processPosition(mapper, x2, y2);
9092 processSync(mapper);
9093 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009094 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Arthur Hung9ad18942021-06-19 02:04:46 +00009095 ASSERT_EQ(uint32_t(2), motionArgs.pointerCount);
9096
9097 // first finger up with some unexpected data.
9098 processSlot(mapper, FIRST_SLOT);
9099 processId(mapper, INVALID_TRACKING_ID);
9100 processPosition(mapper, x2, y2);
9101 processPressure(mapper, RAW_PRESSURE_MAX);
9102 processSync(mapper);
9103 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9104 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9105 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9106}
9107
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009108// --- MultiTouchInputMapperTest_ExternalDevice ---
9109
9110class MultiTouchInputMapperTest_ExternalDevice : public MultiTouchInputMapperTest {
9111protected:
Chris Yea52ade12020-08-27 16:49:20 -07009112 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009113};
9114
9115/**
9116 * Expect fallback to internal viewport if device is external and external viewport is not present.
9117 */
9118TEST_F(MultiTouchInputMapperTest_ExternalDevice, Viewports_Fallback) {
9119 prepareAxes(POSITION);
9120 addConfigurationProperty("touch.deviceType", "touchScreen");
9121 prepareDisplay(DISPLAY_ORIENTATION_0);
9122 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9123
9124 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
9125
9126 NotifyMotionArgs motionArgs;
9127
9128 // Expect the event to be sent to the internal viewport,
9129 // because an external viewport is not present.
9130 processPosition(mapper, 100, 100);
9131 processSync(mapper);
9132 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9133 ASSERT_EQ(ADISPLAY_ID_DEFAULT, motionArgs.displayId);
9134
9135 // Expect the event to be sent to the external viewport if it is present.
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009136 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009137 processPosition(mapper, 100, 100);
9138 processSync(mapper);
9139 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9140 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
9141}
Arthur Hung4197f6b2020-03-16 15:39:59 +08009142
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009143TEST_F(MultiTouchInputMapperTest, Process_TouchpadCapture) {
9144 // we need a pointer controller for mouse mode of touchpad (start pointer at 0,0)
9145 std::shared_ptr<FakePointerController> fakePointerController =
9146 std::make_shared<FakePointerController>();
9147 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
9148 fakePointerController->setPosition(0, 0);
9149 fakePointerController->setButtonState(0);
9150
9151 // prepare device and capture
9152 prepareDisplay(DISPLAY_ORIENTATION_0);
9153 prepareAxes(POSITION | ID | SLOT);
9154 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
9155 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
9156 mFakePolicy->setPointerCapture(true);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009157 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009158 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9159
9160 // captured touchpad should be a touchpad source
9161 NotifyDeviceResetArgs resetArgs;
9162 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
9163 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
9164
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009165 InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
Chris Yef74dc422020-09-02 22:41:50 -07009166
9167 const InputDeviceInfo::MotionRange* relRangeX =
9168 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_X, AINPUT_SOURCE_TOUCHPAD);
9169 ASSERT_NE(relRangeX, nullptr);
9170 ASSERT_EQ(relRangeX->min, -(RAW_X_MAX - RAW_X_MIN));
9171 ASSERT_EQ(relRangeX->max, RAW_X_MAX - RAW_X_MIN);
9172 const InputDeviceInfo::MotionRange* relRangeY =
9173 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_Y, AINPUT_SOURCE_TOUCHPAD);
9174 ASSERT_NE(relRangeY, nullptr);
9175 ASSERT_EQ(relRangeY->min, -(RAW_Y_MAX - RAW_Y_MIN));
9176 ASSERT_EQ(relRangeY->max, RAW_Y_MAX - RAW_Y_MIN);
9177
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009178 // run captured pointer tests - note that this is unscaled, so input listener events should be
9179 // identical to what the hardware sends (accounting for any
9180 // calibration).
9181 // FINGER 0 DOWN
Chris Ye364fdb52020-08-05 15:07:56 -07009182 processSlot(mapper, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009183 processId(mapper, 1);
9184 processPosition(mapper, 100 + RAW_X_MIN, 100 + RAW_Y_MIN);
9185 processKey(mapper, BTN_TOUCH, 1);
9186 processSync(mapper);
9187
9188 // expect coord[0] to contain initial location of touch 0
9189 NotifyMotionArgs args;
9190 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9191 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
9192 ASSERT_EQ(1U, args.pointerCount);
9193 ASSERT_EQ(0, args.pointerProperties[0].id);
9194 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, args.source);
9195 ASSERT_NO_FATAL_FAILURE(
9196 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
9197
9198 // FINGER 1 DOWN
9199 processSlot(mapper, 1);
9200 processId(mapper, 2);
9201 processPosition(mapper, 560 + RAW_X_MIN, 154 + RAW_Y_MIN);
9202 processSync(mapper);
9203
9204 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
9205 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009206 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009207 ASSERT_EQ(2U, args.pointerCount);
9208 ASSERT_EQ(0, args.pointerProperties[0].id);
9209 ASSERT_EQ(1, args.pointerProperties[1].id);
9210 ASSERT_NO_FATAL_FAILURE(
9211 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
9212 ASSERT_NO_FATAL_FAILURE(
9213 assertPointerCoords(args.pointerCoords[1], 560, 154, 1, 0, 0, 0, 0, 0, 0, 0));
9214
9215 // FINGER 1 MOVE
9216 processPosition(mapper, 540 + RAW_X_MIN, 690 + RAW_Y_MIN);
9217 processSync(mapper);
9218
9219 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
9220 // from move
9221 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9222 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9223 ASSERT_NO_FATAL_FAILURE(
9224 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
9225 ASSERT_NO_FATAL_FAILURE(
9226 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
9227
9228 // FINGER 0 MOVE
9229 processSlot(mapper, 0);
9230 processPosition(mapper, 50 + RAW_X_MIN, 800 + RAW_Y_MIN);
9231 processSync(mapper);
9232
9233 // expect coord[0] to contain new touch 0 location, coord[1] to contain previous location
9234 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9235 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9236 ASSERT_NO_FATAL_FAILURE(
9237 assertPointerCoords(args.pointerCoords[0], 50, 800, 1, 0, 0, 0, 0, 0, 0, 0));
9238 ASSERT_NO_FATAL_FAILURE(
9239 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
9240
9241 // BUTTON DOWN
9242 processKey(mapper, BTN_LEFT, 1);
9243 processSync(mapper);
9244
9245 // touchinputmapper design sends a move before button press
9246 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9247 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9248 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9249 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
9250
9251 // BUTTON UP
9252 processKey(mapper, BTN_LEFT, 0);
9253 processSync(mapper);
9254
9255 // touchinputmapper design sends a move after button release
9256 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9257 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
9258 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9259 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9260
9261 // FINGER 0 UP
9262 processId(mapper, -1);
9263 processSync(mapper);
9264 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9265 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | 0x0000, args.action);
9266
9267 // FINGER 1 MOVE
9268 processSlot(mapper, 1);
9269 processPosition(mapper, 320 + RAW_X_MIN, 900 + RAW_Y_MIN);
9270 processSync(mapper);
9271
9272 // expect coord[0] to contain new location of touch 1, and properties[0].id to contain 1
9273 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9274 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9275 ASSERT_EQ(1U, args.pointerCount);
9276 ASSERT_EQ(1, args.pointerProperties[0].id);
9277 ASSERT_NO_FATAL_FAILURE(
9278 assertPointerCoords(args.pointerCoords[0], 320, 900, 1, 0, 0, 0, 0, 0, 0, 0));
9279
9280 // FINGER 1 UP
9281 processId(mapper, -1);
9282 processKey(mapper, BTN_TOUCH, 0);
9283 processSync(mapper);
9284 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9285 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
9286
9287 // non captured touchpad should be a mouse source
9288 mFakePolicy->setPointerCapture(false);
9289 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
9290 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
9291 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
9292}
9293
9294TEST_F(MultiTouchInputMapperTest, Process_UnCapturedTouchpadPointer) {
9295 std::shared_ptr<FakePointerController> fakePointerController =
9296 std::make_shared<FakePointerController>();
9297 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
9298 fakePointerController->setPosition(0, 0);
9299 fakePointerController->setButtonState(0);
9300
9301 // prepare device and capture
9302 prepareDisplay(DISPLAY_ORIENTATION_0);
9303 prepareAxes(POSITION | ID | SLOT);
9304 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
9305 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009306 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009307 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9308 // run uncaptured pointer tests - pushes out generic events
9309 // FINGER 0 DOWN
9310 processId(mapper, 3);
9311 processPosition(mapper, 100, 100);
9312 processKey(mapper, BTN_TOUCH, 1);
9313 processSync(mapper);
9314
9315 // start at (100,100), cursor should be at (0,0) * scale
9316 NotifyMotionArgs args;
9317 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9318 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
9319 ASSERT_NO_FATAL_FAILURE(
9320 assertPointerCoords(args.pointerCoords[0], 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
9321
9322 // FINGER 0 MOVE
9323 processPosition(mapper, 200, 200);
9324 processSync(mapper);
9325
9326 // compute scaling to help with touch position checking
9327 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
9328 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
9329 float scale =
9330 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
9331
9332 // translate from (100,100) -> (200,200), cursor should have changed to (100,100) * scale)
9333 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9334 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
9335 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 100 * scale, 100 * scale, 0,
9336 0, 0, 0, 0, 0, 0, 0));
9337}
9338
9339TEST_F(MultiTouchInputMapperTest, WhenCapturedAndNotCaptured_GetSources) {
9340 std::shared_ptr<FakePointerController> fakePointerController =
9341 std::make_shared<FakePointerController>();
9342
9343 prepareDisplay(DISPLAY_ORIENTATION_0);
9344 prepareAxes(POSITION | ID | SLOT);
9345 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009346 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009347 mFakePolicy->setPointerCapture(false);
9348 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9349
9350 // uncaptured touchpad should be a pointer device
9351 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
9352
9353 // captured touchpad should be a touchpad device
9354 mFakePolicy->setPointerCapture(true);
9355 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
9356 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
9357}
9358
Arthur Hung6d5b4b22022-01-21 07:21:10 +00009359// --- JoystickInputMapperTest ---
9360
9361class JoystickInputMapperTest : public InputMapperTest {
9362protected:
9363 static const int32_t RAW_X_MIN;
9364 static const int32_t RAW_X_MAX;
9365 static const int32_t RAW_Y_MIN;
9366 static const int32_t RAW_Y_MAX;
9367
9368 void SetUp() override {
9369 InputMapperTest::SetUp(InputDeviceClass::JOYSTICK | InputDeviceClass::EXTERNAL);
9370 }
9371 void prepareAxes() {
9372 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
9373 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
9374 }
9375
9376 void processAxis(JoystickInputMapper& mapper, int32_t axis, int32_t value) {
9377 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, axis, value);
9378 }
9379
9380 void processSync(JoystickInputMapper& mapper) {
9381 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
9382 }
9383
9384 void prepareVirtualDisplay(int32_t orientation) {
9385 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH,
9386 VIRTUAL_DISPLAY_HEIGHT, orientation, VIRTUAL_DISPLAY_UNIQUE_ID,
9387 NO_PORT, ViewportType::VIRTUAL);
9388 }
9389};
9390
9391const int32_t JoystickInputMapperTest::RAW_X_MIN = -32767;
9392const int32_t JoystickInputMapperTest::RAW_X_MAX = 32767;
9393const int32_t JoystickInputMapperTest::RAW_Y_MIN = -32767;
9394const int32_t JoystickInputMapperTest::RAW_Y_MAX = 32767;
9395
9396TEST_F(JoystickInputMapperTest, Configure_AssignsDisplayUniqueId) {
9397 prepareAxes();
9398 JoystickInputMapper& mapper = addMapperAndConfigure<JoystickInputMapper>();
9399
9400 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
9401
9402 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
9403
9404 // Send an axis event
9405 processAxis(mapper, ABS_X, 100);
9406 processSync(mapper);
9407
9408 NotifyMotionArgs args;
9409 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9410 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
9411
9412 // Send another axis event
9413 processAxis(mapper, ABS_Y, 100);
9414 processSync(mapper);
9415
9416 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9417 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
9418}
9419
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009420// --- PeripheralControllerTest ---
Chris Yee2b1e5c2021-03-10 22:45:12 -08009421
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009422class PeripheralControllerTest : public testing::Test {
Chris Yee2b1e5c2021-03-10 22:45:12 -08009423protected:
9424 static const char* DEVICE_NAME;
9425 static const char* DEVICE_LOCATION;
9426 static const int32_t DEVICE_ID;
9427 static const int32_t DEVICE_GENERATION;
9428 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07009429 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Chris Yee2b1e5c2021-03-10 22:45:12 -08009430 static const int32_t EVENTHUB_ID;
9431
9432 std::shared_ptr<FakeEventHub> mFakeEventHub;
9433 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07009434 std::unique_ptr<TestInputListener> mFakeListener;
Chris Yee2b1e5c2021-03-10 22:45:12 -08009435 std::unique_ptr<InstrumentedInputReader> mReader;
9436 std::shared_ptr<InputDevice> mDevice;
9437
Dominik Laskowski2f01d772022-03-23 16:01:29 -07009438 virtual void SetUp(ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -08009439 mFakeEventHub = std::make_unique<FakeEventHub>();
9440 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07009441 mFakeListener = std::make_unique<TestInputListener>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009442 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07009443 *mFakeListener);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009444 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
9445 }
9446
9447 void SetUp() override { SetUp(DEVICE_CLASSES); }
9448
9449 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07009450 mFakeListener.reset();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009451 mFakePolicy.clear();
9452 }
9453
9454 void configureDevice(uint32_t changes) {
9455 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
9456 mReader->requestRefreshConfiguration(changes);
9457 mReader->loopOnce();
9458 }
9459 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
9460 }
9461
9462 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
9463 const std::string& location, int32_t eventHubId,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07009464 ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -08009465 InputDeviceIdentifier identifier;
9466 identifier.name = name;
9467 identifier.location = location;
9468 std::shared_ptr<InputDevice> device =
9469 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
9470 identifier);
9471 mReader->pushNextDevice(device);
9472 mFakeEventHub->addDevice(eventHubId, name, classes);
9473 mReader->loopOnce();
9474 return device;
9475 }
9476
9477 template <class T, typename... Args>
9478 T& addControllerAndConfigure(Args... args) {
9479 T& controller = mDevice->addController<T>(EVENTHUB_ID, args...);
9480
9481 return controller;
9482 }
9483};
9484
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009485const char* PeripheralControllerTest::DEVICE_NAME = "device";
9486const char* PeripheralControllerTest::DEVICE_LOCATION = "BLUETOOTH";
9487const int32_t PeripheralControllerTest::DEVICE_ID = END_RESERVED_ID + 1000;
9488const int32_t PeripheralControllerTest::DEVICE_GENERATION = 2;
9489const int32_t PeripheralControllerTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07009490const ftl::Flags<InputDeviceClass> PeripheralControllerTest::DEVICE_CLASSES =
9491 ftl::Flags<InputDeviceClass>(0); // not needed for current tests
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009492const int32_t PeripheralControllerTest::EVENTHUB_ID = 1;
Chris Yee2b1e5c2021-03-10 22:45:12 -08009493
9494// --- BatteryControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009495class BatteryControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -08009496protected:
9497 void SetUp() override {
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009498 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::BATTERY);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009499 }
9500};
9501
9502TEST_F(BatteryControllerTest, GetBatteryCapacity) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009503 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009504
9505 ASSERT_TRUE(controller.getBatteryCapacity(DEFAULT_BATTERY));
9506 ASSERT_EQ(controller.getBatteryCapacity(DEFAULT_BATTERY).value_or(-1), BATTERY_CAPACITY);
9507}
9508
9509TEST_F(BatteryControllerTest, GetBatteryStatus) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009510 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009511
9512 ASSERT_TRUE(controller.getBatteryStatus(DEFAULT_BATTERY));
9513 ASSERT_EQ(controller.getBatteryStatus(DEFAULT_BATTERY).value_or(-1), BATTERY_STATUS);
9514}
9515
9516// --- LightControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009517class LightControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -08009518protected:
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009519 void SetUp() override {
9520 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::LIGHT);
9521 }
Chris Yee2b1e5c2021-03-10 22:45:12 -08009522};
9523
Chris Ye85758332021-05-16 23:05:17 -07009524TEST_F(LightControllerTest, MonoLight) {
9525 RawLightInfo infoMono = {.id = 1,
9526 .name = "Mono",
9527 .maxBrightness = 255,
9528 .flags = InputLightClass::BRIGHTNESS,
9529 .path = ""};
9530 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
Chris Yee2b1e5c2021-03-10 22:45:12 -08009531
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009532 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009533 InputDeviceInfo info;
9534 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009535 std::vector<InputDeviceLightInfo> lights = info.getLights();
9536 ASSERT_EQ(1U, lights.size());
9537 ASSERT_EQ(InputDeviceLightType::MONO, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009538
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009539 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
9540 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009541}
9542
9543TEST_F(LightControllerTest, RGBLight) {
9544 RawLightInfo infoRed = {.id = 1,
9545 .name = "red",
9546 .maxBrightness = 255,
9547 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
9548 .path = ""};
9549 RawLightInfo infoGreen = {.id = 2,
9550 .name = "green",
9551 .maxBrightness = 255,
9552 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
9553 .path = ""};
9554 RawLightInfo infoBlue = {.id = 3,
9555 .name = "blue",
9556 .maxBrightness = 255,
9557 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
9558 .path = ""};
9559 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
9560 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
9561 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
9562
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009563 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009564 InputDeviceInfo info;
9565 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009566 std::vector<InputDeviceLightInfo> lights = info.getLights();
9567 ASSERT_EQ(1U, lights.size());
9568 ASSERT_EQ(InputDeviceLightType::RGB, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009569
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009570 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
9571 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009572}
9573
9574TEST_F(LightControllerTest, MultiColorRGBLight) {
9575 RawLightInfo infoColor = {.id = 1,
9576 .name = "red",
9577 .maxBrightness = 255,
9578 .flags = InputLightClass::BRIGHTNESS |
9579 InputLightClass::MULTI_INTENSITY |
9580 InputLightClass::MULTI_INDEX,
9581 .path = ""};
9582
9583 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
9584
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009585 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009586 InputDeviceInfo info;
9587 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009588 std::vector<InputDeviceLightInfo> lights = info.getLights();
9589 ASSERT_EQ(1U, lights.size());
9590 ASSERT_EQ(InputDeviceLightType::MULTI_COLOR, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009591
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009592 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
9593 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009594}
9595
9596TEST_F(LightControllerTest, PlayerIdLight) {
9597 RawLightInfo info1 = {.id = 1,
9598 .name = "player1",
9599 .maxBrightness = 255,
9600 .flags = InputLightClass::BRIGHTNESS,
9601 .path = ""};
9602 RawLightInfo info2 = {.id = 2,
9603 .name = "player2",
9604 .maxBrightness = 255,
9605 .flags = InputLightClass::BRIGHTNESS,
9606 .path = ""};
9607 RawLightInfo info3 = {.id = 3,
9608 .name = "player3",
9609 .maxBrightness = 255,
9610 .flags = InputLightClass::BRIGHTNESS,
9611 .path = ""};
9612 RawLightInfo info4 = {.id = 4,
9613 .name = "player4",
9614 .maxBrightness = 255,
9615 .flags = InputLightClass::BRIGHTNESS,
9616 .path = ""};
9617 mFakeEventHub->addRawLightInfo(info1.id, std::move(info1));
9618 mFakeEventHub->addRawLightInfo(info2.id, std::move(info2));
9619 mFakeEventHub->addRawLightInfo(info3.id, std::move(info3));
9620 mFakeEventHub->addRawLightInfo(info4.id, std::move(info4));
9621
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009622 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009623 InputDeviceInfo info;
9624 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009625 std::vector<InputDeviceLightInfo> lights = info.getLights();
9626 ASSERT_EQ(1U, lights.size());
9627 ASSERT_EQ(InputDeviceLightType::PLAYER_ID, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009628
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009629 ASSERT_FALSE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
9630 ASSERT_TRUE(controller.setLightPlayerId(lights[0].id, LIGHT_PLAYER_ID));
9631 ASSERT_EQ(controller.getLightPlayerId(lights[0].id).value_or(-1), LIGHT_PLAYER_ID);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009632}
9633
Michael Wrightd02c5b62014-02-10 15:10:22 -08009634} // namespace android