blob: 8ba501c27549ffd3cff2caa5ef3e63014fad4f16 [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 Pradhanc7ef27e2020-02-03 19:19:15 -08002952 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);
arthurhungc903df12020-08-11 15:08:42 +08003367 // Initial metastate to AMETA_NONE.
3368 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
3369 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003370
3371 // Key down by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003372 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003373 NotifyKeyArgs args;
3374 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3375 ASSERT_EQ(DEVICE_ID, args.deviceId);
3376 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3377 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3378 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3379 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3380 ASSERT_EQ(KEY_HOME, args.scanCode);
3381 ASSERT_EQ(AMETA_NONE, args.metaState);
3382 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3383 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3384 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3385
3386 // Key up by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003387 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003388 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3389 ASSERT_EQ(DEVICE_ID, args.deviceId);
3390 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3391 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3392 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3393 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3394 ASSERT_EQ(KEY_HOME, args.scanCode);
3395 ASSERT_EQ(AMETA_NONE, args.metaState);
3396 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3397 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3398 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3399
3400 // Key down by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003401 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3402 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, 0, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003403 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3404 ASSERT_EQ(DEVICE_ID, args.deviceId);
3405 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3406 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3407 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3408 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3409 ASSERT_EQ(0, args.scanCode);
3410 ASSERT_EQ(AMETA_NONE, args.metaState);
3411 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3412 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3413 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3414
3415 // Key up by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003416 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3417 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003418 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3419 ASSERT_EQ(DEVICE_ID, args.deviceId);
3420 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3421 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3422 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3423 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3424 ASSERT_EQ(0, args.scanCode);
3425 ASSERT_EQ(AMETA_NONE, args.metaState);
3426 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3427 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3428 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3429
3430 // Key down with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003431 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3432 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UNKNOWN, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003433 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3434 ASSERT_EQ(DEVICE_ID, args.deviceId);
3435 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3436 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3437 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3438 ASSERT_EQ(0, args.keyCode);
3439 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3440 ASSERT_EQ(AMETA_NONE, args.metaState);
3441 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3442 ASSERT_EQ(0U, args.policyFlags);
3443 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3444
3445 // Key up with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003446 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3447 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003448 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3449 ASSERT_EQ(DEVICE_ID, args.deviceId);
3450 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3451 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3452 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3453 ASSERT_EQ(0, args.keyCode);
3454 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3455 ASSERT_EQ(AMETA_NONE, args.metaState);
3456 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3457 ASSERT_EQ(0U, args.policyFlags);
3458 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3459}
3460
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003461/**
3462 * Ensure that the readTime is set to the time when the EV_KEY is received.
3463 */
3464TEST_F(KeyboardInputMapperTest, Process_SendsReadTime) {
3465 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3466
3467 KeyboardInputMapper& mapper =
3468 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3469 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3470 NotifyKeyArgs args;
3471
3472 // Key down
3473 process(mapper, ARBITRARY_TIME, 12 /*readTime*/, EV_KEY, KEY_HOME, 1);
3474 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3475 ASSERT_EQ(12, args.readTime);
3476
3477 // Key up
3478 process(mapper, ARBITRARY_TIME, 15 /*readTime*/, EV_KEY, KEY_HOME, 1);
3479 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3480 ASSERT_EQ(15, args.readTime);
3481}
3482
Michael Wrightd02c5b62014-02-10 15:10:22 -08003483TEST_F(KeyboardInputMapperTest, Process_ShouldUpdateMetaState) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003484 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFTSHIFT, 0, AKEYCODE_SHIFT_LEFT, 0);
3485 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003486 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, 0);
3487 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, 0);
3488 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003489
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003490 KeyboardInputMapper& mapper =
3491 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3492 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003493
arthurhungc903df12020-08-11 15:08:42 +08003494 // Initial metastate to AMETA_NONE.
3495 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
3496 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003497
3498 // Metakey down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003499 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003500 NotifyKeyArgs args;
3501 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3502 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003503 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003504 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003505
3506 // Key down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003507 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_A, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003508 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3509 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003510 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003511
3512 // Key up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003513 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, KEY_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003514 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3515 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003516 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003517
3518 // Metakey up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003519 process(mapper, ARBITRARY_TIME + 3, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003520 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3521 ASSERT_EQ(AMETA_NONE, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003522 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003523 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003524}
3525
3526TEST_F(KeyboardInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003527 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3528 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3529 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3530 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003531
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003532 KeyboardInputMapper& mapper =
3533 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3534 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003535
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003536 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003537 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3538 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
3539 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3540 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
3541 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3542 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
3543 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3544 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
3545}
3546
3547TEST_F(KeyboardInputMapperTest, Process_WhenOrientationAware_ShouldRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003548 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3549 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3550 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3551 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003552
Michael Wrightd02c5b62014-02-10 15:10:22 -08003553 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003554 KeyboardInputMapper& mapper =
3555 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3556 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003557
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003558 prepareDisplay(DISPLAY_ORIENTATION_0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003559 ASSERT_NO_FATAL_FAILURE(
3560 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3561 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3562 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3563 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3564 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3565 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3566 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003567
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003568 clearViewports();
3569 prepareDisplay(DISPLAY_ORIENTATION_90);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003570 ASSERT_NO_FATAL_FAILURE(
3571 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3572 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3573 AKEYCODE_DPAD_UP, DISPLAY_ID));
3574 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3575 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3576 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3577 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003578
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003579 clearViewports();
3580 prepareDisplay(DISPLAY_ORIENTATION_180);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003581 ASSERT_NO_FATAL_FAILURE(
3582 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3583 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3584 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3585 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3586 AKEYCODE_DPAD_UP, DISPLAY_ID));
3587 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3588 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003589
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003590 clearViewports();
3591 prepareDisplay(DISPLAY_ORIENTATION_270);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003592 ASSERT_NO_FATAL_FAILURE(
3593 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3594 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3595 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3596 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3597 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3598 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3599 AKEYCODE_DPAD_UP, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003600
3601 // Special case: if orientation changes while key is down, we still emit the same keycode
3602 // in the key up as we did in the key down.
3603 NotifyKeyArgs args;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003604 clearViewports();
3605 prepareDisplay(DISPLAY_ORIENTATION_270);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003606 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003607 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3608 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3609 ASSERT_EQ(KEY_UP, args.scanCode);
3610 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3611
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003612 clearViewports();
3613 prepareDisplay(DISPLAY_ORIENTATION_180);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003614 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003615 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3616 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3617 ASSERT_EQ(KEY_UP, args.scanCode);
3618 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3619}
3620
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003621TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_NotOrientationAware) {
3622 // If the keyboard is not orientation aware,
3623 // key events should not be associated with a specific display id
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003624 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003625
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003626 KeyboardInputMapper& mapper =
3627 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3628 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003629 NotifyKeyArgs args;
3630
3631 // Display id should be ADISPLAY_ID_NONE without any display configuration.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003632 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003633 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003634 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003635 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3636 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3637
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003638 prepareDisplay(DISPLAY_ORIENTATION_0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003639 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003640 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003641 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003642 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3643 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3644}
3645
3646TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_OrientationAware) {
3647 // If the keyboard is orientation aware,
3648 // key events should be associated with the internal viewport
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003649 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003650
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003651 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003652 KeyboardInputMapper& mapper =
3653 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3654 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003655 NotifyKeyArgs args;
3656
3657 // Display id should be ADISPLAY_ID_NONE without any display configuration.
3658 // ^--- already checked by the previous test
3659
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003660 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003661 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003662 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003663 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003664 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003665 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3666 ASSERT_EQ(DISPLAY_ID, args.displayId);
3667
3668 constexpr int32_t newDisplayId = 2;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003669 clearViewports();
3670 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003671 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003672 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003673 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003674 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003675 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3676 ASSERT_EQ(newDisplayId, args.displayId);
3677}
3678
Michael Wrightd02c5b62014-02-10 15:10:22 -08003679TEST_F(KeyboardInputMapperTest, GetKeyCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003680 KeyboardInputMapper& mapper =
3681 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3682 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003683
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003684 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003685 ASSERT_EQ(1, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003686
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003687 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003688 ASSERT_EQ(0, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003689}
3690
Philip Junker4af3b3d2021-12-14 10:36:55 +01003691TEST_F(KeyboardInputMapperTest, GetKeyCodeForKeyLocation) {
3692 KeyboardInputMapper& mapper =
3693 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3694 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3695
3696 mFakeEventHub->addKeyCodeMapping(EVENTHUB_ID, AKEYCODE_Y, AKEYCODE_Z);
3697 ASSERT_EQ(AKEYCODE_Z, mapper.getKeyCodeForKeyLocation(AKEYCODE_Y))
3698 << "If a mapping is available, the result is equal to the mapping";
3699
3700 ASSERT_EQ(AKEYCODE_A, mapper.getKeyCodeForKeyLocation(AKEYCODE_A))
3701 << "If no mapping is available, the result is the key location";
3702}
3703
Michael Wrightd02c5b62014-02-10 15:10:22 -08003704TEST_F(KeyboardInputMapperTest, GetScanCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003705 KeyboardInputMapper& mapper =
3706 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3707 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003708
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003709 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003710 ASSERT_EQ(1, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003711
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003712 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003713 ASSERT_EQ(0, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003714}
3715
3716TEST_F(KeyboardInputMapperTest, MarkSupportedKeyCodes) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003717 KeyboardInputMapper& mapper =
3718 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3719 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003720
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003721 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003722
3723 const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
3724 uint8_t flags[2] = { 0, 0 };
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003725 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, 1, keyCodes, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003726 ASSERT_TRUE(flags[0]);
3727 ASSERT_FALSE(flags[1]);
3728}
3729
3730TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleMetaStateAndLeds) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003731 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3732 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3733 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3734 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3735 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3736 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003737
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003738 KeyboardInputMapper& mapper =
3739 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3740 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Chris Yea52ade12020-08-27 16:49:20 -07003741 // Initialize metastate to AMETA_NUM_LOCK_ON.
arthurhungc903df12020-08-11 15:08:42 +08003742 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
3743 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003744
3745 // Initialization should have turned all of the lights off.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003746 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3747 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3748 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003749
3750 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003751 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3752 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003753 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3754 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3755 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003756 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003757
3758 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003759 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3760 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003761 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3762 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3763 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003764 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003765
3766 // Toggle caps lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003767 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3768 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003769 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3770 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3771 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003772 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003773
3774 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003775 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3776 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003777 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3778 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3779 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003780 ASSERT_EQ(AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003781
3782 // Toggle num lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003783 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3784 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003785 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3786 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3787 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003788 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003789
3790 // Toggle scroll lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003791 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3792 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003793 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3794 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3795 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003796 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003797}
3798
Chris Yea52ade12020-08-27 16:49:20 -07003799TEST_F(KeyboardInputMapperTest, NoMetaStateWhenMetaKeysNotPresent) {
3800 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_BUTTON_A, 0);
3801 mFakeEventHub->addKey(EVENTHUB_ID, BTN_B, 0, AKEYCODE_BUTTON_B, 0);
3802 mFakeEventHub->addKey(EVENTHUB_ID, BTN_X, 0, AKEYCODE_BUTTON_X, 0);
3803 mFakeEventHub->addKey(EVENTHUB_ID, BTN_Y, 0, AKEYCODE_BUTTON_Y, 0);
3804
3805 KeyboardInputMapper& mapper =
3806 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3807 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
3808
3809 // Initial metastate should be AMETA_NONE as no meta keys added.
3810 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3811 // Meta state should be AMETA_NONE after reset
3812 mapper.reset(ARBITRARY_TIME);
3813 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3814 // Meta state should be AMETA_NONE with update, as device doesn't have the keys.
3815 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
3816 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3817
3818 NotifyKeyArgs args;
3819 // Press button "A"
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003820 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_A, 1);
Chris Yea52ade12020-08-27 16:49:20 -07003821 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3822 ASSERT_EQ(AMETA_NONE, args.metaState);
3823 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3824 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3825 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3826
3827 // Button up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003828 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003829 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3830 ASSERT_EQ(AMETA_NONE, args.metaState);
3831 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3832 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3833 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3834}
3835
Arthur Hung2c9a3342019-07-23 14:18:59 +08003836TEST_F(KeyboardInputMapperTest, Configure_AssignsDisplayPort) {
3837 // keyboard 1.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003838 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3839 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3840 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3841 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003842
3843 // keyboard 2.
3844 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08003845 const std::string DEVICE_NAME2 = "KEYBOARD2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08003846 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003847 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08003848 std::shared_ptr<InputDevice> device2 =
3849 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003850 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08003851
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003852 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3853 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3854 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3855 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003856
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003857 KeyboardInputMapper& mapper =
3858 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3859 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003860
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003861 KeyboardInputMapper& mapper2 =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003862 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003863 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003864 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
3865 device2->reset(ARBITRARY_TIME);
3866
3867 // Prepared displays and associated info.
3868 constexpr uint8_t hdmi1 = 0;
3869 constexpr uint8_t hdmi2 = 1;
3870 const std::string SECONDARY_UNIQUE_ID = "local:1";
3871
3872 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
3873 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
3874
3875 // No associated display viewport found, should disable the device.
3876 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3877 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3878 ASSERT_FALSE(device2->isEnabled());
3879
3880 // Prepare second display.
3881 constexpr int32_t newDisplayId = 2;
3882 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003883 UNIQUE_ID, hdmi1, ViewportType::INTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003884 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003885 SECONDARY_UNIQUE_ID, hdmi2, ViewportType::EXTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003886 // Default device will reconfigure above, need additional reconfiguration for another device.
3887 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3888 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3889
3890 // Device should be enabled after the associated display is found.
3891 ASSERT_TRUE(mDevice->isEnabled());
3892 ASSERT_TRUE(device2->isEnabled());
3893
3894 // Test pad key events
3895 ASSERT_NO_FATAL_FAILURE(
3896 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3897 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3898 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3899 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3900 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3901 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3902 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3903
3904 ASSERT_NO_FATAL_FAILURE(
3905 testDPadKeyRotation(mapper2, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, newDisplayId));
3906 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3907 AKEYCODE_DPAD_RIGHT, newDisplayId));
3908 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3909 AKEYCODE_DPAD_DOWN, newDisplayId));
3910 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3911 AKEYCODE_DPAD_LEFT, newDisplayId));
3912}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003913
arthurhungc903df12020-08-11 15:08:42 +08003914TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleAfterReattach) {
3915 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3916 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3917 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3918 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3919 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3920 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3921
3922 KeyboardInputMapper& mapper =
3923 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3924 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3925 // Initial metastate to AMETA_NONE.
3926 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
3927 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
3928
3929 // Initialization should have turned all of the lights off.
3930 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3931 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3932 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3933
3934 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003935 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3936 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003937 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3938 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
3939
3940 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003941 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3942 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003943 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3944 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
3945
3946 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003947 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3948 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003949 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3950 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
3951
3952 mFakeEventHub->removeDevice(EVENTHUB_ID);
3953 mReader->loopOnce();
3954
3955 // keyboard 2 should default toggle keys.
3956 const std::string USB2 = "USB2";
3957 const std::string DEVICE_NAME2 = "KEYBOARD2";
3958 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
3959 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
3960 std::shared_ptr<InputDevice> device2 =
3961 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003962 ftl::Flags<InputDeviceClass>(0));
arthurhungc903df12020-08-11 15:08:42 +08003963 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3964 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
3965 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3966 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3967 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3968 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3969
arthurhung6fe95782020-10-05 22:41:16 +08003970 KeyboardInputMapper& mapper2 =
3971 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
3972 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
arthurhungc903df12020-08-11 15:08:42 +08003973 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
3974 device2->reset(ARBITRARY_TIME);
3975
3976 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_CAPSL));
3977 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_NUML));
3978 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_SCROLLL));
arthurhung6fe95782020-10-05 22:41:16 +08003979 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON,
3980 mapper2.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08003981}
3982
Arthur Hungcb40a002021-08-03 14:31:01 +00003983TEST_F(KeyboardInputMapperTest, Process_toggleCapsLockState) {
3984 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3985 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3986 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3987
3988 // Suppose we have two mappers. (DPAD + KEYBOARD)
3989 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_DPAD,
3990 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
3991 KeyboardInputMapper& mapper =
3992 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3993 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3994 // Initialize metastate to AMETA_NUM_LOCK_ON.
3995 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
3996 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
3997
3998 mReader->toggleCapsLockState(DEVICE_ID);
3999 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
4000}
4001
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004002// --- KeyboardInputMapperTest_ExternalDevice ---
4003
4004class KeyboardInputMapperTest_ExternalDevice : public InputMapperTest {
4005protected:
Chris Yea52ade12020-08-27 16:49:20 -07004006 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004007};
4008
4009TEST_F(KeyboardInputMapperTest_ExternalDevice, WakeBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07004010 // For external devices, non-media keys will trigger wake on key down. Media keys need to be
4011 // marked as WAKE in the keylayout file to trigger wake.
Powei Fengd041c5d2019-05-03 17:11:33 -07004012
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004013 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, 0);
4014 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, 0);
4015 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAYPAUSE, 0, AKEYCODE_MEDIA_PLAY_PAUSE,
4016 POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07004017
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004018 KeyboardInputMapper& mapper =
4019 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4020 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07004021
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004022 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004023 NotifyKeyArgs args;
4024 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4025 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4026
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004027 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004028 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4029 ASSERT_EQ(uint32_t(0), args.policyFlags);
4030
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004031 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004032 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4033 ASSERT_EQ(uint32_t(0), args.policyFlags);
4034
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004035 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004036 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4037 ASSERT_EQ(uint32_t(0), args.policyFlags);
4038
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004039 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004040 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4041 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4042
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004043 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004044 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4045 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4046}
4047
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004048TEST_F(KeyboardInputMapperTest_ExternalDevice, DoNotWakeByDefaultBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07004049 // Tv Remote key's wake behavior is prescribed by the keylayout file.
Powei Fengd041c5d2019-05-03 17:11:33 -07004050
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004051 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4052 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
4053 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07004054
Powei Fengd041c5d2019-05-03 17:11:33 -07004055 addConfigurationProperty("keyboard.doNotWakeByDefault", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004056 KeyboardInputMapper& mapper =
4057 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4058 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07004059
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004060 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004061 NotifyKeyArgs args;
4062 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4063 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4064
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004065 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004066 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4067 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4068
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004069 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_DOWN, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004070 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4071 ASSERT_EQ(uint32_t(0), args.policyFlags);
4072
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004073 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_DOWN, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004074 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4075 ASSERT_EQ(uint32_t(0), args.policyFlags);
4076
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004077 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004078 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4079 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4080
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004081 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004082 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4083 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4084}
4085
Michael Wrightd02c5b62014-02-10 15:10:22 -08004086// --- CursorInputMapperTest ---
4087
4088class CursorInputMapperTest : public InputMapperTest {
4089protected:
4090 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD;
4091
Michael Wright17db18e2020-06-26 20:51:44 +01004092 std::shared_ptr<FakePointerController> mFakePointerController;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004093
Chris Yea52ade12020-08-27 16:49:20 -07004094 void SetUp() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004095 InputMapperTest::SetUp();
4096
Michael Wright17db18e2020-06-26 20:51:44 +01004097 mFakePointerController = std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00004098 mFakePolicy->setPointerController(mFakePointerController);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004099 }
4100
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004101 void testMotionRotation(CursorInputMapper& mapper, int32_t originalX, int32_t originalY,
4102 int32_t rotatedX, int32_t rotatedY);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004103
4104 void prepareDisplay(int32_t orientation) {
4105 const std::string uniqueId = "local:0";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004106 const ViewportType viewportType = ViewportType::INTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004107 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
4108 orientation, uniqueId, NO_PORT, viewportType);
4109 }
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004110
4111 static void assertCursorPointerCoords(const PointerCoords& coords, float x, float y,
4112 float pressure) {
4113 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(coords, x, y, pressure, 0.0f, 0.0f, 0.0f, 0.0f,
4114 0.0f, 0.0f, 0.0f, EPSILON));
4115 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08004116};
4117
4118const int32_t CursorInputMapperTest::TRACKBALL_MOVEMENT_THRESHOLD = 6;
4119
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004120void CursorInputMapperTest::testMotionRotation(CursorInputMapper& mapper, int32_t originalX,
4121 int32_t originalY, int32_t rotatedX,
4122 int32_t rotatedY) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004123 NotifyMotionArgs args;
4124
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004125 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, originalX);
4126 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, originalY);
4127 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004128 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4129 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004130 ASSERT_NO_FATAL_FAILURE(
4131 assertCursorPointerCoords(args.pointerCoords[0],
4132 float(rotatedX) / TRACKBALL_MOVEMENT_THRESHOLD,
4133 float(rotatedY) / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004134}
4135
4136TEST_F(CursorInputMapperTest, WhenModeIsPointer_GetSources_ReturnsMouse) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004137 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004138 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004139
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004140 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004141}
4142
4143TEST_F(CursorInputMapperTest, WhenModeIsNavigation_GetSources_ReturnsTrackball) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004144 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004145 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004146
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004147 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004148}
4149
4150TEST_F(CursorInputMapperTest, WhenModeIsPointer_PopulateDeviceInfo_ReturnsRangeFromPointerController) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004151 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004152 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004153
4154 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004155 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004156
4157 // Initially there may not be a valid motion range.
Yi Kong9b14ac62018-07-17 13:48:38 -07004158 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE));
4159 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004160 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4161 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE, 0.0f, 1.0f, 0.0f, 0.0f));
4162
4163 // When the bounds are set, then there should be a valid motion range.
4164 mFakePointerController->setBounds(1, 2, 800 - 1, 480 - 1);
4165
4166 InputDeviceInfo info2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004167 mapper.populateDeviceInfo(&info2);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004168
4169 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
4170 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE,
4171 1, 800 - 1, 0.0f, 0.0f));
4172 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
4173 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE,
4174 2, 480 - 1, 0.0f, 0.0f));
4175 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
4176 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE,
4177 0.0f, 1.0f, 0.0f, 0.0f));
4178}
4179
4180TEST_F(CursorInputMapperTest, WhenModeIsNavigation_PopulateDeviceInfo_ReturnsScaledRange) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004181 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004182 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004183
4184 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004185 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004186
4187 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4188 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_TRACKBALL,
4189 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
4190 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4191 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_TRACKBALL,
4192 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
4193 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4194 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TRACKBALL,
4195 0.0f, 1.0f, 0.0f, 0.0f));
4196}
4197
4198TEST_F(CursorInputMapperTest, Process_ShouldSetAllFieldsAndIncludeGlobalMetaState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004199 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004200 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004201
arthurhungdcef2dc2020-08-11 14:47:50 +08004202 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004203
4204 NotifyMotionArgs args;
4205
4206 // Button press.
4207 // Mostly testing non x/y behavior here so we don't need to check again elsewhere.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004208 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4209 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004210 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4211 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4212 ASSERT_EQ(DEVICE_ID, args.deviceId);
4213 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4214 ASSERT_EQ(uint32_t(0), args.policyFlags);
4215 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
4216 ASSERT_EQ(0, args.flags);
4217 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4218 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
4219 ASSERT_EQ(0, args.edgeFlags);
4220 ASSERT_EQ(uint32_t(1), args.pointerCount);
4221 ASSERT_EQ(0, args.pointerProperties[0].id);
4222 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004223 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004224 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4225 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4226 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4227
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004228 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4229 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4230 ASSERT_EQ(DEVICE_ID, args.deviceId);
4231 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4232 ASSERT_EQ(uint32_t(0), args.policyFlags);
4233 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
4234 ASSERT_EQ(0, args.flags);
4235 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4236 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
4237 ASSERT_EQ(0, args.edgeFlags);
4238 ASSERT_EQ(uint32_t(1), args.pointerCount);
4239 ASSERT_EQ(0, args.pointerProperties[0].id);
4240 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004241 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004242 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4243 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4244 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4245
Michael Wrightd02c5b62014-02-10 15:10:22 -08004246 // Button release. Should have same down time.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004247 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4248 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004249 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4250 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
4251 ASSERT_EQ(DEVICE_ID, args.deviceId);
4252 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4253 ASSERT_EQ(uint32_t(0), args.policyFlags);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004254 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
4255 ASSERT_EQ(0, args.flags);
4256 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4257 ASSERT_EQ(0, args.buttonState);
4258 ASSERT_EQ(0, args.edgeFlags);
4259 ASSERT_EQ(uint32_t(1), args.pointerCount);
4260 ASSERT_EQ(0, args.pointerProperties[0].id);
4261 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004262 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004263 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4264 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4265 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4266
4267 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4268 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
4269 ASSERT_EQ(DEVICE_ID, args.deviceId);
4270 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4271 ASSERT_EQ(uint32_t(0), args.policyFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004272 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
4273 ASSERT_EQ(0, args.flags);
4274 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4275 ASSERT_EQ(0, args.buttonState);
4276 ASSERT_EQ(0, args.edgeFlags);
4277 ASSERT_EQ(uint32_t(1), args.pointerCount);
4278 ASSERT_EQ(0, args.pointerProperties[0].id);
4279 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004280 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004281 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4282 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4283 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4284}
4285
4286TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentXYUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004287 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004288 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004289
4290 NotifyMotionArgs args;
4291
4292 // Motion in X but not Y.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004293 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4294 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004295 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4296 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004297 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4298 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f,
4299 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004300
4301 // Motion in Y but not X.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004302 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
4303 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004304 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4305 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004306 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f,
4307 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004308}
4309
4310TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004311 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004312 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004313
4314 NotifyMotionArgs args;
4315
4316 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004317 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4318 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004319 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4320 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004321 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004322
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004323 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4324 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004325 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004326
Michael Wrightd02c5b62014-02-10 15:10:22 -08004327 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004328 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4329 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004330 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004331 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004332 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004333
4334 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004335 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004336 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004337}
4338
4339TEST_F(CursorInputMapperTest, Process_ShouldHandleCombinedXYAndButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004340 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004341 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004342
4343 NotifyMotionArgs args;
4344
4345 // Combined X, Y and Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004346 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4347 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
4348 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4349 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004350 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4351 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004352 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4353 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4354 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004355
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004356 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4357 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004358 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4359 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4360 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004361
Michael Wrightd02c5b62014-02-10 15:10:22 -08004362 // Move X, Y a bit while pressed.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004363 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 2);
4364 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 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 2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4370 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004371
4372 // Release Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004373 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
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));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004376 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004377 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004378
4379 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004380 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004381 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004382}
4383
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004384TEST_F(CursorInputMapperTest, Process_WhenOrientationAware_ShouldNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004385 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004386 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
4387 // need to be rotated.
4388 addConfigurationProperty("cursor.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004389 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004390
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004391 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004392 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4393 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4394 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4395 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4396 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4397 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4398 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4399 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4400}
4401
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004402TEST_F(CursorInputMapperTest, Process_WhenNotOrientationAware_ShouldRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004403 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004404 // Since InputReader works in the un-rotated coordinate space, only devices that are not
4405 // orientation-aware are affected by display rotation.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004406 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004407
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004408 prepareDisplay(DISPLAY_ORIENTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004409 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4410 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4411 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4412 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4413 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4414 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4415 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4416 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4417
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004418 prepareDisplay(DISPLAY_ORIENTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004419 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, -1, 0));
4420 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, 1));
4421 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, 1));
4422 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, 1));
4423 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 1, 0));
4424 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, -1));
4425 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, -1));
4426 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, -1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004427
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004428 prepareDisplay(DISPLAY_ORIENTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004429 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, -1));
4430 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, -1));
4431 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, -1, 0));
4432 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, 1));
4433 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, 1));
4434 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, 1));
4435 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 1, 0));
4436 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, -1));
4437
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004438 prepareDisplay(DISPLAY_ORIENTATION_270);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004439 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 1, 0));
4440 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, -1));
4441 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, -1));
4442 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, -1));
4443 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, -1, 0));
4444 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, 1));
4445 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, 1));
4446 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, 1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004447}
4448
4449TEST_F(CursorInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004450 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004451 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004452
4453 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4454 mFakePointerController->setPosition(100, 200);
4455 mFakePointerController->setButtonState(0);
4456
4457 NotifyMotionArgs motionArgs;
4458 NotifyKeyArgs keyArgs;
4459
4460 // press BTN_LEFT, release BTN_LEFT
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004461 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 1);
4462 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004463 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4464 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4465 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4466 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004467 ASSERT_NO_FATAL_FAILURE(
4468 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004469
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004470 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4471 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4472 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4473 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004474 ASSERT_NO_FATAL_FAILURE(
4475 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004476
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004477 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 0);
4478 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004479 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004480 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004481 ASSERT_EQ(0, motionArgs.buttonState);
4482 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004483 ASSERT_NO_FATAL_FAILURE(
4484 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004485
4486 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004487 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004488 ASSERT_EQ(0, motionArgs.buttonState);
4489 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004490 ASSERT_NO_FATAL_FAILURE(
4491 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004492
4493 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004494 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004495 ASSERT_EQ(0, motionArgs.buttonState);
4496 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004497 ASSERT_NO_FATAL_FAILURE(
4498 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004499
4500 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004501 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 1);
4502 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 1);
4503 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004504 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4505 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4506 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4507 motionArgs.buttonState);
4508 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4509 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004510 ASSERT_NO_FATAL_FAILURE(
4511 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004512
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004513 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4514 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4515 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4516 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4517 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004518 ASSERT_NO_FATAL_FAILURE(
4519 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004520
4521 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4522 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4523 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4524 motionArgs.buttonState);
4525 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4526 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004527 ASSERT_NO_FATAL_FAILURE(
4528 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004529
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004530 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 0);
4531 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004532 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004533 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004534 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4535 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004536 ASSERT_NO_FATAL_FAILURE(
4537 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004538
4539 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004540 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004541 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4542 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004543 ASSERT_NO_FATAL_FAILURE(
4544 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004545
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004546 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4547 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004548 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004549 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
4550 ASSERT_EQ(0, motionArgs.buttonState);
4551 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004552 ASSERT_NO_FATAL_FAILURE(
4553 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004554 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4555 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004556
4557 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004558 ASSERT_EQ(0, motionArgs.buttonState);
4559 ASSERT_EQ(0, mFakePointerController->getButtonState());
4560 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
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
Michael Wrightd02c5b62014-02-10 15:10:22 -08004564 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4565 ASSERT_EQ(0, motionArgs.buttonState);
4566 ASSERT_EQ(0, mFakePointerController->getButtonState());
4567 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
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_BACK, release BTN_BACK
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004572 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 1);
4573 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004574 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4575 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4576 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004577
Michael Wrightd02c5b62014-02-10 15:10:22 -08004578 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004579 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004580 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4581 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004582 ASSERT_NO_FATAL_FAILURE(
4583 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004584
4585 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4586 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4587 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4588 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004589 ASSERT_NO_FATAL_FAILURE(
4590 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004591
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004592 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 0);
4593 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004594 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004595 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004596 ASSERT_EQ(0, motionArgs.buttonState);
4597 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004598 ASSERT_NO_FATAL_FAILURE(
4599 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004600
4601 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004602 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004603 ASSERT_EQ(0, motionArgs.buttonState);
4604 ASSERT_EQ(0, mFakePointerController->getButtonState());
4605
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004606 ASSERT_NO_FATAL_FAILURE(
4607 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004608 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4609 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4610 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4611
4612 // press BTN_SIDE, release BTN_SIDE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004613 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 1);
4614 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004615 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4616 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4617 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004618
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_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004621 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4622 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, 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));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004625
4626 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4627 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4628 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4629 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004630 ASSERT_NO_FATAL_FAILURE(
4631 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004632
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004633 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 0);
4634 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004635 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004636 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004637 ASSERT_EQ(0, motionArgs.buttonState);
4638 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004639 ASSERT_NO_FATAL_FAILURE(
4640 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004641
4642 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4643 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4644 ASSERT_EQ(0, motionArgs.buttonState);
4645 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004646 ASSERT_NO_FATAL_FAILURE(
4647 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004648
Michael Wrightd02c5b62014-02-10 15:10:22 -08004649 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4650 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4651 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4652
4653 // press BTN_FORWARD, release BTN_FORWARD
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004654 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 1);
4655 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004656 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4657 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4658 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004659
Michael Wrightd02c5b62014-02-10 15:10:22 -08004660 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004661 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004662 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4663 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004664 ASSERT_NO_FATAL_FAILURE(
4665 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004666
4667 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4668 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4669 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4670 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004671 ASSERT_NO_FATAL_FAILURE(
4672 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004673
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004674 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 0);
4675 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004676 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004677 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004678 ASSERT_EQ(0, motionArgs.buttonState);
4679 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004680 ASSERT_NO_FATAL_FAILURE(
4681 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004682
4683 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4684 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4685 ASSERT_EQ(0, motionArgs.buttonState);
4686 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004687 ASSERT_NO_FATAL_FAILURE(
4688 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004689
Michael Wrightd02c5b62014-02-10 15:10:22 -08004690 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4691 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4692 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4693
4694 // press BTN_EXTRA, release BTN_EXTRA
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004695 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 1);
4696 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004697 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4698 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4699 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004700
Michael Wrightd02c5b62014-02-10 15:10:22 -08004701 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004702 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004703 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4704 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004705 ASSERT_NO_FATAL_FAILURE(
4706 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004707
4708 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4709 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4710 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4711 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004712 ASSERT_NO_FATAL_FAILURE(
4713 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004714
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004715 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 0);
4716 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004717 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004718 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004719 ASSERT_EQ(0, motionArgs.buttonState);
4720 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004721 ASSERT_NO_FATAL_FAILURE(
4722 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004723
4724 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4725 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4726 ASSERT_EQ(0, motionArgs.buttonState);
4727 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004728 ASSERT_NO_FATAL_FAILURE(
4729 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004730
Michael Wrightd02c5b62014-02-10 15:10:22 -08004731 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4732 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4733 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4734}
4735
4736TEST_F(CursorInputMapperTest, Process_WhenModeIsPointer_ShouldMoveThePointerAround) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004737 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004738 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004739
4740 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4741 mFakePointerController->setPosition(100, 200);
4742 mFakePointerController->setButtonState(0);
4743
4744 NotifyMotionArgs args;
4745
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004746 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4747 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4748 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004749 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004750 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4751 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4752 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4753 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 +01004754 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004755}
4756
4757TEST_F(CursorInputMapperTest, Process_PointerCapture) {
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004758 addConfigurationProperty("cursor.mode", "pointer");
4759 mFakePolicy->setPointerCapture(true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004760 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004761
4762 NotifyDeviceResetArgs resetArgs;
4763 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4764 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4765 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4766
4767 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4768 mFakePointerController->setPosition(100, 200);
4769 mFakePointerController->setButtonState(0);
4770
4771 NotifyMotionArgs args;
4772
4773 // Move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004774 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4775 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4776 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004777 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4778 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4779 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4780 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4781 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 +01004782 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004783
4784 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004785 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4786 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004787 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4788 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4789 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
4790 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4791 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4792 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4793 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4794 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
4795 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4796 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4797
4798 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004799 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4800 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004801 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4802 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4803 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
4804 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4805 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4806 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4807 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4808 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
4809 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4810 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4811
4812 // Another move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004813 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 30);
4814 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 40);
4815 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004816 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4817 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4818 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4819 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4820 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 +01004821 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004822
4823 // Disable pointer capture and check that the device generation got bumped
4824 // and events are generated the usual way.
arthurhungdcef2dc2020-08-11 14:47:50 +08004825 const uint32_t generation = mReader->getContext()->getGeneration();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004826 mFakePolicy->setPointerCapture(false);
4827 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
arthurhungdcef2dc2020-08-11 14:47:50 +08004828 ASSERT_TRUE(mReader->getContext()->getGeneration() != generation);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004829
4830 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4831 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4832 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4833
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004834 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4835 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4836 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004837 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4838 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004839 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4840 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4841 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 +01004842 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004843}
4844
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004845TEST_F(CursorInputMapperTest, Process_ShouldHandleDisplayId) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004846 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004847
Garfield Tan888a6a42020-01-09 11:39:16 -08004848 // Setup for second display.
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004849 constexpr int32_t SECOND_DISPLAY_ID = 1;
Garfield Tan888a6a42020-01-09 11:39:16 -08004850 const std::string SECOND_DISPLAY_UNIQUE_ID = "local:1";
4851 mFakePolicy->addDisplayViewport(SECOND_DISPLAY_ID, 800, 480, DISPLAY_ORIENTATION_0,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00004852 true /*isActive*/, SECOND_DISPLAY_UNIQUE_ID, NO_PORT,
4853 ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08004854 mFakePolicy->setDefaultPointerDisplayId(SECOND_DISPLAY_ID);
4855 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
4856
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004857 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4858 mFakePointerController->setPosition(100, 200);
4859 mFakePointerController->setButtonState(0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004860
4861 NotifyMotionArgs args;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004862 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4863 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4864 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004865 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4866 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4867 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4868 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4869 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 +01004870 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004871 ASSERT_EQ(SECOND_DISPLAY_ID, args.displayId);
4872}
4873
Michael Wrightd02c5b62014-02-10 15:10:22 -08004874// --- TouchInputMapperTest ---
4875
4876class TouchInputMapperTest : public InputMapperTest {
4877protected:
4878 static const int32_t RAW_X_MIN;
4879 static const int32_t RAW_X_MAX;
4880 static const int32_t RAW_Y_MIN;
4881 static const int32_t RAW_Y_MAX;
4882 static const int32_t RAW_TOUCH_MIN;
4883 static const int32_t RAW_TOUCH_MAX;
4884 static const int32_t RAW_TOOL_MIN;
4885 static const int32_t RAW_TOOL_MAX;
4886 static const int32_t RAW_PRESSURE_MIN;
4887 static const int32_t RAW_PRESSURE_MAX;
4888 static const int32_t RAW_ORIENTATION_MIN;
4889 static const int32_t RAW_ORIENTATION_MAX;
4890 static const int32_t RAW_DISTANCE_MIN;
4891 static const int32_t RAW_DISTANCE_MAX;
4892 static const int32_t RAW_TILT_MIN;
4893 static const int32_t RAW_TILT_MAX;
4894 static const int32_t RAW_ID_MIN;
4895 static const int32_t RAW_ID_MAX;
4896 static const int32_t RAW_SLOT_MIN;
4897 static const int32_t RAW_SLOT_MAX;
4898 static const float X_PRECISION;
4899 static const float Y_PRECISION;
Santos Cordonfa5cf462017-04-05 10:37:00 -07004900 static const float X_PRECISION_VIRTUAL;
4901 static const float Y_PRECISION_VIRTUAL;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004902
4903 static const float GEOMETRIC_SCALE;
Jason Gerecke489fda82012-09-07 17:19:40 -07004904 static const TouchAffineTransformation AFFINE_TRANSFORM;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004905
4906 static const VirtualKeyDefinition VIRTUAL_KEYS[2];
4907
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004908 const std::string UNIQUE_ID = "local:0";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004909 const std::string SECONDARY_UNIQUE_ID = "local:1";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004910
Michael Wrightd02c5b62014-02-10 15:10:22 -08004911 enum Axes {
4912 POSITION = 1 << 0,
4913 TOUCH = 1 << 1,
4914 TOOL = 1 << 2,
4915 PRESSURE = 1 << 3,
4916 ORIENTATION = 1 << 4,
4917 MINOR = 1 << 5,
4918 ID = 1 << 6,
4919 DISTANCE = 1 << 7,
4920 TILT = 1 << 8,
4921 SLOT = 1 << 9,
4922 TOOL_TYPE = 1 << 10,
4923 };
4924
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004925 void prepareDisplay(int32_t orientation, std::optional<uint8_t> port = NO_PORT);
4926 void prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port = NO_PORT);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004927 void prepareVirtualDisplay(int32_t orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004928 void prepareVirtualKeys();
Jason Gerecke489fda82012-09-07 17:19:40 -07004929 void prepareLocationCalibration();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004930 int32_t toRawX(float displayX);
4931 int32_t toRawY(float displayY);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07004932 int32_t toRotatedRawX(float displayX);
4933 int32_t toRotatedRawY(float displayY);
Jason Gerecke489fda82012-09-07 17:19:40 -07004934 float toCookedX(float rawX, float rawY);
4935 float toCookedY(float rawX, float rawY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004936 float toDisplayX(int32_t rawX);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004937 float toDisplayX(int32_t rawX, int32_t displayWidth);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004938 float toDisplayY(int32_t rawY);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004939 float toDisplayY(int32_t rawY, int32_t displayHeight);
4940
Michael Wrightd02c5b62014-02-10 15:10:22 -08004941};
4942
4943const int32_t TouchInputMapperTest::RAW_X_MIN = 25;
4944const int32_t TouchInputMapperTest::RAW_X_MAX = 1019;
4945const int32_t TouchInputMapperTest::RAW_Y_MIN = 30;
4946const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009;
4947const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0;
4948const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31;
4949const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0;
4950const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15;
Michael Wrightaa449c92017-12-13 21:21:43 +00004951const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = 0;
4952const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = 255;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004953const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7;
4954const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
4955const int32_t TouchInputMapperTest::RAW_DISTANCE_MIN = 0;
4956const int32_t TouchInputMapperTest::RAW_DISTANCE_MAX = 7;
4957const int32_t TouchInputMapperTest::RAW_TILT_MIN = 0;
4958const int32_t TouchInputMapperTest::RAW_TILT_MAX = 150;
4959const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
4960const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
4961const int32_t TouchInputMapperTest::RAW_SLOT_MIN = 0;
4962const int32_t TouchInputMapperTest::RAW_SLOT_MAX = 9;
4963const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH;
4964const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT;
Santos Cordonfa5cf462017-04-05 10:37:00 -07004965const float TouchInputMapperTest::X_PRECISION_VIRTUAL =
4966 float(RAW_X_MAX - RAW_X_MIN + 1) / VIRTUAL_DISPLAY_WIDTH;
4967const float TouchInputMapperTest::Y_PRECISION_VIRTUAL =
4968 float(RAW_Y_MAX - RAW_Y_MIN + 1) / VIRTUAL_DISPLAY_HEIGHT;
Jason Gerecke489fda82012-09-07 17:19:40 -07004969const TouchAffineTransformation TouchInputMapperTest::AFFINE_TRANSFORM =
4970 TouchAffineTransformation(1, -2, 3, -4, 5, -6);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004971
4972const float TouchInputMapperTest::GEOMETRIC_SCALE =
4973 avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1),
4974 float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1));
4975
4976const VirtualKeyDefinition TouchInputMapperTest::VIRTUAL_KEYS[2] = {
4977 { KEY_HOME, 60, DISPLAY_HEIGHT + 15, 20, 20 },
4978 { KEY_MENU, DISPLAY_HEIGHT - 60, DISPLAY_WIDTH + 15, 20, 20 },
4979};
4980
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004981void TouchInputMapperTest::prepareDisplay(int32_t orientation, std::optional<uint8_t> port) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004982 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
4983 port, ViewportType::INTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004984}
4985
4986void TouchInputMapperTest::prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port) {
4987 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
4988 DISPLAY_ORIENTATION_0, SECONDARY_UNIQUE_ID, port, type);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004989}
4990
Santos Cordonfa5cf462017-04-05 10:37:00 -07004991void TouchInputMapperTest::prepareVirtualDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004992 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH, VIRTUAL_DISPLAY_HEIGHT,
4993 orientation, VIRTUAL_DISPLAY_UNIQUE_ID, NO_PORT,
4994 ViewportType::VIRTUAL);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004995}
4996
Michael Wrightd02c5b62014-02-10 15:10:22 -08004997void TouchInputMapperTest::prepareVirtualKeys() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004998 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[0]);
4999 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[1]);
5000 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
5001 mFakeEventHub->addKey(EVENTHUB_ID, KEY_MENU, 0, AKEYCODE_MENU, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005002}
5003
Jason Gerecke489fda82012-09-07 17:19:40 -07005004void TouchInputMapperTest::prepareLocationCalibration() {
5005 mFakePolicy->setTouchAffineTransformation(AFFINE_TRANSFORM);
5006}
5007
Michael Wrightd02c5b62014-02-10 15:10:22 -08005008int32_t TouchInputMapperTest::toRawX(float displayX) {
5009 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN);
5010}
5011
5012int32_t TouchInputMapperTest::toRawY(float displayY) {
5013 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN);
5014}
5015
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005016int32_t TouchInputMapperTest::toRotatedRawX(float displayX) {
5017 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_HEIGHT + RAW_X_MIN);
5018}
5019
5020int32_t TouchInputMapperTest::toRotatedRawY(float displayY) {
5021 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_WIDTH + RAW_Y_MIN);
5022}
5023
Jason Gerecke489fda82012-09-07 17:19:40 -07005024float TouchInputMapperTest::toCookedX(float rawX, float rawY) {
5025 AFFINE_TRANSFORM.applyTo(rawX, rawY);
5026 return rawX;
5027}
5028
5029float TouchInputMapperTest::toCookedY(float rawX, float rawY) {
5030 AFFINE_TRANSFORM.applyTo(rawX, rawY);
5031 return rawY;
5032}
5033
Michael Wrightd02c5b62014-02-10 15:10:22 -08005034float TouchInputMapperTest::toDisplayX(int32_t rawX) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005035 return toDisplayX(rawX, DISPLAY_WIDTH);
5036}
5037
5038float TouchInputMapperTest::toDisplayX(int32_t rawX, int32_t displayWidth) {
5039 return float(rawX - RAW_X_MIN) * displayWidth / (RAW_X_MAX - RAW_X_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005040}
5041
5042float TouchInputMapperTest::toDisplayY(int32_t rawY) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005043 return toDisplayY(rawY, DISPLAY_HEIGHT);
5044}
5045
5046float TouchInputMapperTest::toDisplayY(int32_t rawY, int32_t displayHeight) {
5047 return float(rawY - RAW_Y_MIN) * displayHeight / (RAW_Y_MAX - RAW_Y_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005048}
5049
5050
5051// --- SingleTouchInputMapperTest ---
5052
5053class SingleTouchInputMapperTest : public TouchInputMapperTest {
5054protected:
5055 void prepareButtons();
5056 void prepareAxes(int axes);
5057
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005058 void processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
5059 void processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
5060 void processUp(SingleTouchInputMapper& mappery);
5061 void processPressure(SingleTouchInputMapper& mapper, int32_t pressure);
5062 void processToolMajor(SingleTouchInputMapper& mapper, int32_t toolMajor);
5063 void processDistance(SingleTouchInputMapper& mapper, int32_t distance);
5064 void processTilt(SingleTouchInputMapper& mapper, int32_t tiltX, int32_t tiltY);
5065 void processKey(SingleTouchInputMapper& mapper, int32_t code, int32_t value);
5066 void processSync(SingleTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005067};
5068
5069void SingleTouchInputMapperTest::prepareButtons() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005070 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005071}
5072
5073void SingleTouchInputMapperTest::prepareAxes(int axes) {
5074 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005075 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
5076 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005077 }
5078 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005079 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MIN,
5080 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005081 }
5082 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005083 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TOOL_WIDTH, RAW_TOOL_MIN, RAW_TOOL_MAX, 0,
5084 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005085 }
5086 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005087 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_DISTANCE, RAW_DISTANCE_MIN,
5088 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005089 }
5090 if (axes & TILT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005091 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_X, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
5092 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_Y, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005093 }
5094}
5095
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005096void SingleTouchInputMapperTest::processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005097 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
5098 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
5099 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005100}
5101
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005102void SingleTouchInputMapperTest::processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005103 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
5104 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005105}
5106
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005107void SingleTouchInputMapperTest::processUp(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005108 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005109}
5110
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005111void SingleTouchInputMapperTest::processPressure(SingleTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005112 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005113}
5114
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005115void SingleTouchInputMapperTest::processToolMajor(SingleTouchInputMapper& mapper,
5116 int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005117 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TOOL_WIDTH, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005118}
5119
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005120void SingleTouchInputMapperTest::processDistance(SingleTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005121 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005122}
5123
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005124void SingleTouchInputMapperTest::processTilt(SingleTouchInputMapper& mapper, int32_t tiltX,
5125 int32_t tiltY) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005126 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_X, tiltX);
5127 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_Y, tiltY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005128}
5129
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005130void SingleTouchInputMapperTest::processKey(SingleTouchInputMapper& mapper, int32_t code,
5131 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005132 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005133}
5134
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005135void SingleTouchInputMapperTest::processSync(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005136 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005137}
5138
Michael Wrightd02c5b62014-02-10 15:10:22 -08005139TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005140 prepareButtons();
5141 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005142 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005143
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005144 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005145}
5146
5147TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndIsACursor_ReturnsTouchPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005148 mFakeEventHub->addRelativeAxis(EVENTHUB_ID, REL_X);
5149 mFakeEventHub->addRelativeAxis(EVENTHUB_ID, REL_Y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005150 prepareButtons();
5151 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005152 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005153
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005154 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005155}
5156
5157TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchPad_ReturnsTouchPad) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005158 prepareButtons();
5159 prepareAxes(POSITION);
5160 addConfigurationProperty("touch.deviceType", "touchPad");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005161 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005162
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005163 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005164}
5165
5166TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005167 prepareButtons();
5168 prepareAxes(POSITION);
5169 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005170 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005171
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005172 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005173}
5174
5175TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005176 addConfigurationProperty("touch.deviceType", "touchScreen");
5177 prepareDisplay(DISPLAY_ORIENTATION_0);
5178 prepareButtons();
5179 prepareAxes(POSITION);
5180 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005181 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005182
5183 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005184 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005185
5186 // Virtual key is down.
5187 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5188 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5189 processDown(mapper, x, y);
5190 processSync(mapper);
5191 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5192
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005193 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005194
5195 // Virtual key is up.
5196 processUp(mapper);
5197 processSync(mapper);
5198 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5199
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005200 ASSERT_EQ(AKEY_STATE_UP, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005201}
5202
5203TEST_F(SingleTouchInputMapperTest, GetScanCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005204 addConfigurationProperty("touch.deviceType", "touchScreen");
5205 prepareDisplay(DISPLAY_ORIENTATION_0);
5206 prepareButtons();
5207 prepareAxes(POSITION);
5208 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005209 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005210
5211 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005212 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005213
5214 // Virtual key is down.
5215 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5216 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5217 processDown(mapper, x, y);
5218 processSync(mapper);
5219 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5220
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005221 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005222
5223 // Virtual key is up.
5224 processUp(mapper);
5225 processSync(mapper);
5226 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5227
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005228 ASSERT_EQ(AKEY_STATE_UP, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005229}
5230
5231TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005232 addConfigurationProperty("touch.deviceType", "touchScreen");
5233 prepareDisplay(DISPLAY_ORIENTATION_0);
5234 prepareButtons();
5235 prepareAxes(POSITION);
5236 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005237 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005238
5239 const int32_t keys[2] = { AKEYCODE_HOME, AKEYCODE_A };
5240 uint8_t flags[2] = { 0, 0 };
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005241 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, 2, keys, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005242 ASSERT_TRUE(flags[0]);
5243 ASSERT_FALSE(flags[1]);
5244}
5245
5246TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
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
arthurhungdcef2dc2020-08-11 14:47:50 +08005254 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005255
5256 NotifyKeyArgs args;
5257
5258 // Press virtual key.
5259 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5260 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5261 processDown(mapper, x, y);
5262 processSync(mapper);
5263
5264 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
5265 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5266 ASSERT_EQ(DEVICE_ID, args.deviceId);
5267 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
5268 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
5269 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
5270 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5271 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5272 ASSERT_EQ(KEY_HOME, args.scanCode);
5273 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5274 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5275
5276 // Release virtual key.
5277 processUp(mapper);
5278 processSync(mapper);
5279
5280 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
5281 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5282 ASSERT_EQ(DEVICE_ID, args.deviceId);
5283 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
5284 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
5285 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
5286 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5287 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5288 ASSERT_EQ(KEY_HOME, args.scanCode);
5289 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5290 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5291
5292 // Should not have sent any motions.
5293 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5294}
5295
5296TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005297 addConfigurationProperty("touch.deviceType", "touchScreen");
5298 prepareDisplay(DISPLAY_ORIENTATION_0);
5299 prepareButtons();
5300 prepareAxes(POSITION);
5301 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005302 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005303
arthurhungdcef2dc2020-08-11 14:47:50 +08005304 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005305
5306 NotifyKeyArgs keyArgs;
5307
5308 // Press virtual key.
5309 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5310 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5311 processDown(mapper, x, y);
5312 processSync(mapper);
5313
5314 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5315 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5316 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5317 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5318 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5319 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5320 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags);
5321 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5322 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5323 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5324 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5325
5326 // Move out of bounds. This should generate a cancel and a pointer down since we moved
5327 // into the display area.
5328 y -= 100;
5329 processMove(mapper, x, y);
5330 processSync(mapper);
5331
5332 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5333 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5334 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5335 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5336 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5337 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5338 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
5339 | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags);
5340 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5341 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5342 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5343 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5344
5345 NotifyMotionArgs motionArgs;
5346 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5347 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5348 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5349 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5350 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5351 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5352 ASSERT_EQ(0, motionArgs.flags);
5353 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5354 ASSERT_EQ(0, motionArgs.buttonState);
5355 ASSERT_EQ(0, motionArgs.edgeFlags);
5356 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5357 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5358 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5359 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5360 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5361 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5362 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5363 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5364
5365 // Keep moving out of bounds. Should generate a pointer move.
5366 y -= 50;
5367 processMove(mapper, x, y);
5368 processSync(mapper);
5369
5370 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5371 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5372 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5373 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5374 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5375 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5376 ASSERT_EQ(0, motionArgs.flags);
5377 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5378 ASSERT_EQ(0, motionArgs.buttonState);
5379 ASSERT_EQ(0, motionArgs.edgeFlags);
5380 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5381 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5382 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5383 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5384 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5385 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5386 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5387 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5388
5389 // Release out of bounds. Should generate a pointer up.
5390 processUp(mapper);
5391 processSync(mapper);
5392
5393 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5394 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5395 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5396 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5397 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5398 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5399 ASSERT_EQ(0, motionArgs.flags);
5400 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5401 ASSERT_EQ(0, motionArgs.buttonState);
5402 ASSERT_EQ(0, motionArgs.edgeFlags);
5403 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5404 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5405 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5406 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5407 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5408 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5409 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5410 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5411
5412 // Should not have sent any more keys or motions.
5413 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5414 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5415}
5416
5417TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005418 addConfigurationProperty("touch.deviceType", "touchScreen");
5419 prepareDisplay(DISPLAY_ORIENTATION_0);
5420 prepareButtons();
5421 prepareAxes(POSITION);
5422 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005423 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005424
arthurhungdcef2dc2020-08-11 14:47:50 +08005425 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005426
5427 NotifyMotionArgs motionArgs;
5428
5429 // Initially go down out of bounds.
5430 int32_t x = -10;
5431 int32_t y = -10;
5432 processDown(mapper, x, y);
5433 processSync(mapper);
5434
5435 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5436
5437 // Move into the display area. Should generate a pointer down.
5438 x = 50;
5439 y = 75;
5440 processMove(mapper, x, y);
5441 processSync(mapper);
5442
5443 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5444 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5445 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5446 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5447 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5448 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5449 ASSERT_EQ(0, motionArgs.flags);
5450 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5451 ASSERT_EQ(0, motionArgs.buttonState);
5452 ASSERT_EQ(0, motionArgs.edgeFlags);
5453 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5454 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5455 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5456 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5457 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5458 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5459 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5460 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5461
5462 // Release. Should generate a pointer up.
5463 processUp(mapper);
5464 processSync(mapper);
5465
5466 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5467 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5468 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5469 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5470 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5471 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5472 ASSERT_EQ(0, motionArgs.flags);
5473 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5474 ASSERT_EQ(0, motionArgs.buttonState);
5475 ASSERT_EQ(0, motionArgs.edgeFlags);
5476 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5477 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5478 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5479 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5480 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5481 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5482 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5483 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5484
5485 // Should not have sent any more keys or motions.
5486 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5487 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5488}
5489
Santos Cordonfa5cf462017-04-05 10:37:00 -07005490TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture_VirtualDisplay) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005491 addConfigurationProperty("touch.deviceType", "touchScreen");
5492 addConfigurationProperty("touch.displayId", VIRTUAL_DISPLAY_UNIQUE_ID);
5493
5494 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
5495 prepareButtons();
5496 prepareAxes(POSITION);
5497 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005498 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Santos Cordonfa5cf462017-04-05 10:37:00 -07005499
arthurhungdcef2dc2020-08-11 14:47:50 +08005500 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005501
5502 NotifyMotionArgs motionArgs;
5503
5504 // Down.
5505 int32_t x = 100;
5506 int32_t y = 125;
5507 processDown(mapper, x, y);
5508 processSync(mapper);
5509
5510 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5511 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5512 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5513 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5514 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5515 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5516 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5517 ASSERT_EQ(0, motionArgs.flags);
5518 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5519 ASSERT_EQ(0, motionArgs.buttonState);
5520 ASSERT_EQ(0, motionArgs.edgeFlags);
5521 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5522 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5523 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5524 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5525 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5526 1, 0, 0, 0, 0, 0, 0, 0));
5527 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5528 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5529 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5530
5531 // Move.
5532 x += 50;
5533 y += 75;
5534 processMove(mapper, x, y);
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(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5541 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5542 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5543 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5544 ASSERT_EQ(0, motionArgs.flags);
5545 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5546 ASSERT_EQ(0, motionArgs.buttonState);
5547 ASSERT_EQ(0, motionArgs.edgeFlags);
5548 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5549 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5550 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5551 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5552 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5553 1, 0, 0, 0, 0, 0, 0, 0));
5554 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5555 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5556 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5557
5558 // Up.
5559 processUp(mapper);
5560 processSync(mapper);
5561
5562 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5563 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5564 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5565 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5566 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5567 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5568 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5569 ASSERT_EQ(0, motionArgs.flags);
5570 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5571 ASSERT_EQ(0, motionArgs.buttonState);
5572 ASSERT_EQ(0, motionArgs.edgeFlags);
5573 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5574 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5575 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5576 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5577 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5578 1, 0, 0, 0, 0, 0, 0, 0));
5579 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5580 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5581 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5582
5583 // Should not have sent any more keys or motions.
5584 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5585 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5586}
5587
Michael Wrightd02c5b62014-02-10 15:10:22 -08005588TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005589 addConfigurationProperty("touch.deviceType", "touchScreen");
5590 prepareDisplay(DISPLAY_ORIENTATION_0);
5591 prepareButtons();
5592 prepareAxes(POSITION);
5593 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005594 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005595
arthurhungdcef2dc2020-08-11 14:47:50 +08005596 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005597
5598 NotifyMotionArgs motionArgs;
5599
5600 // Down.
5601 int32_t x = 100;
5602 int32_t y = 125;
5603 processDown(mapper, x, y);
5604 processSync(mapper);
5605
5606 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5607 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5608 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5609 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5610 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5611 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5612 ASSERT_EQ(0, motionArgs.flags);
5613 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5614 ASSERT_EQ(0, motionArgs.buttonState);
5615 ASSERT_EQ(0, motionArgs.edgeFlags);
5616 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5617 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5618 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5619 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5620 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5621 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5622 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5623 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5624
5625 // Move.
5626 x += 50;
5627 y += 75;
5628 processMove(mapper, x, y);
5629 processSync(mapper);
5630
5631 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5632 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5633 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5634 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5635 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5636 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5637 ASSERT_EQ(0, motionArgs.flags);
5638 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5639 ASSERT_EQ(0, motionArgs.buttonState);
5640 ASSERT_EQ(0, motionArgs.edgeFlags);
5641 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5642 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5643 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5644 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5645 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5646 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5647 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5648 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5649
5650 // Up.
5651 processUp(mapper);
5652 processSync(mapper);
5653
5654 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5655 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5656 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5657 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5658 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5659 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5660 ASSERT_EQ(0, motionArgs.flags);
5661 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5662 ASSERT_EQ(0, motionArgs.buttonState);
5663 ASSERT_EQ(0, motionArgs.edgeFlags);
5664 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5665 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5666 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5667 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5668 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5669 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5670 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5671 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5672
5673 // Should not have sent any more keys or motions.
5674 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5675 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5676}
5677
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005678TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_DoesNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005679 addConfigurationProperty("touch.deviceType", "touchScreen");
5680 prepareButtons();
5681 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005682 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
5683 // need to be rotated. Touchscreens are orientation-aware by default.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005684 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005685
5686 NotifyMotionArgs args;
5687
5688 // Rotation 90.
5689 prepareDisplay(DISPLAY_ORIENTATION_90);
5690 processDown(mapper, toRawX(50), toRawY(75));
5691 processSync(mapper);
5692
5693 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5694 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5695 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5696
5697 processUp(mapper);
5698 processSync(mapper);
5699 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5700}
5701
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005702TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_RotatesMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005703 addConfigurationProperty("touch.deviceType", "touchScreen");
5704 prepareButtons();
5705 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005706 // Since InputReader works in the un-rotated coordinate space, only devices that are not
5707 // orientation-aware are affected by display rotation.
5708 addConfigurationProperty("touch.orientationAware", "0");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005709 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005710
5711 NotifyMotionArgs args;
5712
5713 // Rotation 0.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005714 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005715 prepareDisplay(DISPLAY_ORIENTATION_0);
5716 processDown(mapper, toRawX(50), toRawY(75));
5717 processSync(mapper);
5718
5719 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5720 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5721 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5722
5723 processUp(mapper);
5724 processSync(mapper);
5725 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5726
5727 // Rotation 90.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005728 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005729 prepareDisplay(DISPLAY_ORIENTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005730 processDown(mapper, toRawX(75), RAW_Y_MAX - toRawY(50) + RAW_Y_MIN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005731 processSync(mapper);
5732
5733 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5734 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5735 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5736
5737 processUp(mapper);
5738 processSync(mapper);
5739 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5740
5741 // Rotation 180.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005742 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005743 prepareDisplay(DISPLAY_ORIENTATION_180);
5744 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
5745 processSync(mapper);
5746
5747 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5748 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5749 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5750
5751 processUp(mapper);
5752 processSync(mapper);
5753 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5754
5755 // Rotation 270.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005756 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005757 prepareDisplay(DISPLAY_ORIENTATION_270);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005758 processDown(mapper, RAW_X_MAX - toRawX(75) + RAW_X_MIN, toRawY(50));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005759 processSync(mapper);
5760
5761 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5762 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5763 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5764
5765 processUp(mapper);
5766 processSync(mapper);
5767 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5768}
5769
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005770TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation0_RotatesMotions) {
5771 addConfigurationProperty("touch.deviceType", "touchScreen");
5772 prepareButtons();
5773 prepareAxes(POSITION);
5774 addConfigurationProperty("touch.orientationAware", "1");
5775 addConfigurationProperty("touch.orientation", "ORIENTATION_0");
5776 clearViewports();
5777 prepareDisplay(DISPLAY_ORIENTATION_0);
5778 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5779 NotifyMotionArgs args;
5780
5781 // Orientation 0.
5782 processDown(mapper, toRawX(50), toRawY(75));
5783 processSync(mapper);
5784
5785 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5786 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5787 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5788
5789 processUp(mapper);
5790 processSync(mapper);
5791 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5792}
5793
5794TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation90_RotatesMotions) {
5795 addConfigurationProperty("touch.deviceType", "touchScreen");
5796 prepareButtons();
5797 prepareAxes(POSITION);
5798 addConfigurationProperty("touch.orientationAware", "1");
5799 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
5800 clearViewports();
5801 prepareDisplay(DISPLAY_ORIENTATION_0);
5802 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5803 NotifyMotionArgs args;
5804
5805 // Orientation 90.
5806 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
5807 processSync(mapper);
5808
5809 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5810 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5811 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5812
5813 processUp(mapper);
5814 processSync(mapper);
5815 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5816}
5817
5818TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation180_RotatesMotions) {
5819 addConfigurationProperty("touch.deviceType", "touchScreen");
5820 prepareButtons();
5821 prepareAxes(POSITION);
5822 addConfigurationProperty("touch.orientationAware", "1");
5823 addConfigurationProperty("touch.orientation", "ORIENTATION_180");
5824 clearViewports();
5825 prepareDisplay(DISPLAY_ORIENTATION_0);
5826 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5827 NotifyMotionArgs args;
5828
5829 // Orientation 180.
5830 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
5831 processSync(mapper);
5832
5833 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5834 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5835 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5836
5837 processUp(mapper);
5838 processSync(mapper);
5839 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5840}
5841
5842TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation270_RotatesMotions) {
5843 addConfigurationProperty("touch.deviceType", "touchScreen");
5844 prepareButtons();
5845 prepareAxes(POSITION);
5846 addConfigurationProperty("touch.orientationAware", "1");
5847 addConfigurationProperty("touch.orientation", "ORIENTATION_270");
5848 clearViewports();
5849 prepareDisplay(DISPLAY_ORIENTATION_0);
5850 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5851 NotifyMotionArgs args;
5852
5853 // Orientation 270.
5854 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
5855 processSync(mapper);
5856
5857 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5858 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5859 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5860
5861 processUp(mapper);
5862 processSync(mapper);
5863 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5864}
5865
5866TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationSpecified_RotatesMotionWithDisplay) {
5867 addConfigurationProperty("touch.deviceType", "touchScreen");
5868 prepareButtons();
5869 prepareAxes(POSITION);
5870 // Since InputReader works in the un-rotated coordinate space, only devices that are not
5871 // orientation-aware are affected by display rotation.
5872 addConfigurationProperty("touch.orientationAware", "0");
5873 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
5874 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5875
5876 NotifyMotionArgs args;
5877
5878 // Orientation 90, Rotation 0.
5879 clearViewports();
5880 prepareDisplay(DISPLAY_ORIENTATION_0);
5881 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
5882 processSync(mapper);
5883
5884 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5885 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5886 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5887
5888 processUp(mapper);
5889 processSync(mapper);
5890 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5891
5892 // Orientation 90, Rotation 90.
5893 clearViewports();
5894 prepareDisplay(DISPLAY_ORIENTATION_90);
5895 processDown(mapper, toRotatedRawX(50), toRotatedRawY(75));
5896 processSync(mapper);
5897
5898 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5899 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5900 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5901
5902 processUp(mapper);
5903 processSync(mapper);
5904 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5905
5906 // Orientation 90, Rotation 180.
5907 clearViewports();
5908 prepareDisplay(DISPLAY_ORIENTATION_180);
5909 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
5910 processSync(mapper);
5911
5912 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5913 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5914 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5915
5916 processUp(mapper);
5917 processSync(mapper);
5918 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5919
5920 // Orientation 90, Rotation 270.
5921 clearViewports();
5922 prepareDisplay(DISPLAY_ORIENTATION_270);
5923 processDown(mapper, RAW_X_MAX - toRotatedRawX(50) + RAW_X_MIN,
5924 RAW_Y_MAX - toRotatedRawY(75) + RAW_Y_MIN);
5925 processSync(mapper);
5926
5927 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5928 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5929 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5930
5931 processUp(mapper);
5932 processSync(mapper);
5933 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5934}
5935
Michael Wrightd02c5b62014-02-10 15:10:22 -08005936TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005937 addConfigurationProperty("touch.deviceType", "touchScreen");
5938 prepareDisplay(DISPLAY_ORIENTATION_0);
5939 prepareButtons();
5940 prepareAxes(POSITION | PRESSURE | TOOL | DISTANCE | TILT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005941 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005942
5943 // These calculations are based on the input device calibration documentation.
5944 int32_t rawX = 100;
5945 int32_t rawY = 200;
5946 int32_t rawPressure = 10;
5947 int32_t rawToolMajor = 12;
5948 int32_t rawDistance = 2;
5949 int32_t rawTiltX = 30;
5950 int32_t rawTiltY = 110;
5951
5952 float x = toDisplayX(rawX);
5953 float y = toDisplayY(rawY);
5954 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
5955 float size = float(rawToolMajor) / RAW_TOOL_MAX;
5956 float tool = float(rawToolMajor) * GEOMETRIC_SCALE;
5957 float distance = float(rawDistance);
5958
5959 float tiltCenter = (RAW_TILT_MAX + RAW_TILT_MIN) * 0.5f;
5960 float tiltScale = M_PI / 180;
5961 float tiltXAngle = (rawTiltX - tiltCenter) * tiltScale;
5962 float tiltYAngle = (rawTiltY - tiltCenter) * tiltScale;
5963 float orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
5964 float tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
5965
5966 processDown(mapper, rawX, rawY);
5967 processPressure(mapper, rawPressure);
5968 processToolMajor(mapper, rawToolMajor);
5969 processDistance(mapper, rawDistance);
5970 processTilt(mapper, rawTiltX, rawTiltY);
5971 processSync(mapper);
5972
5973 NotifyMotionArgs args;
5974 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5975 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5976 x, y, pressure, size, tool, tool, tool, tool, orientation, distance));
5977 ASSERT_EQ(tilt, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TILT));
5978}
5979
Jason Gerecke489fda82012-09-07 17:19:40 -07005980TEST_F(SingleTouchInputMapperTest, Process_XYAxes_AffineCalibration) {
Jason Gerecke489fda82012-09-07 17:19:40 -07005981 addConfigurationProperty("touch.deviceType", "touchScreen");
5982 prepareDisplay(DISPLAY_ORIENTATION_0);
5983 prepareLocationCalibration();
5984 prepareButtons();
5985 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005986 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Jason Gerecke489fda82012-09-07 17:19:40 -07005987
5988 int32_t rawX = 100;
5989 int32_t rawY = 200;
5990
5991 float x = toDisplayX(toCookedX(rawX, rawY));
5992 float y = toDisplayY(toCookedY(rawX, rawY));
5993
5994 processDown(mapper, rawX, rawY);
5995 processSync(mapper);
5996
5997 NotifyMotionArgs args;
5998 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5999 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6000 x, y, 1, 0, 0, 0, 0, 0, 0, 0));
6001}
6002
Michael Wrightd02c5b62014-02-10 15:10:22 -08006003TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006004 addConfigurationProperty("touch.deviceType", "touchScreen");
6005 prepareDisplay(DISPLAY_ORIENTATION_0);
6006 prepareButtons();
6007 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006008 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006009
6010 NotifyMotionArgs motionArgs;
6011 NotifyKeyArgs keyArgs;
6012
6013 processDown(mapper, 100, 200);
6014 processSync(mapper);
6015 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6016 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6017 ASSERT_EQ(0, motionArgs.buttonState);
6018
6019 // press BTN_LEFT, release BTN_LEFT
6020 processKey(mapper, BTN_LEFT, 1);
6021 processSync(mapper);
6022 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6023 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6024 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6025
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006026 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6027 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6028 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6029
Michael Wrightd02c5b62014-02-10 15:10:22 -08006030 processKey(mapper, BTN_LEFT, 0);
6031 processSync(mapper);
6032 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006033 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006034 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006035
6036 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006037 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006038 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006039
6040 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
6041 processKey(mapper, BTN_RIGHT, 1);
6042 processKey(mapper, BTN_MIDDLE, 1);
6043 processSync(mapper);
6044 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6045 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6046 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6047 motionArgs.buttonState);
6048
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006049 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6050 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6051 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
6052
6053 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6054 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6055 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6056 motionArgs.buttonState);
6057
Michael Wrightd02c5b62014-02-10 15:10:22 -08006058 processKey(mapper, BTN_RIGHT, 0);
6059 processSync(mapper);
6060 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006061 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006062 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006063
6064 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006065 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006066 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006067
6068 processKey(mapper, BTN_MIDDLE, 0);
6069 processSync(mapper);
6070 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006071 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006072 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006073
6074 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006075 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006076 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006077
6078 // press BTN_BACK, release BTN_BACK
6079 processKey(mapper, BTN_BACK, 1);
6080 processSync(mapper);
6081 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6082 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6083 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006084
Michael Wrightd02c5b62014-02-10 15:10:22 -08006085 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006086 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006087 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6088
6089 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6090 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6091 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006092
6093 processKey(mapper, BTN_BACK, 0);
6094 processSync(mapper);
6095 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006096 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006097 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006098
6099 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006100 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006101 ASSERT_EQ(0, motionArgs.buttonState);
6102
Michael Wrightd02c5b62014-02-10 15:10:22 -08006103 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6104 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6105 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6106
6107 // press BTN_SIDE, release BTN_SIDE
6108 processKey(mapper, BTN_SIDE, 1);
6109 processSync(mapper);
6110 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6111 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6112 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006113
Michael Wrightd02c5b62014-02-10 15:10:22 -08006114 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006115 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006116 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6117
6118 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6119 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6120 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006121
6122 processKey(mapper, BTN_SIDE, 0);
6123 processSync(mapper);
6124 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006125 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006126 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006127
6128 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006129 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006130 ASSERT_EQ(0, motionArgs.buttonState);
6131
Michael Wrightd02c5b62014-02-10 15:10:22 -08006132 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6133 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6134 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6135
6136 // press BTN_FORWARD, release BTN_FORWARD
6137 processKey(mapper, BTN_FORWARD, 1);
6138 processSync(mapper);
6139 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6140 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6141 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006142
Michael Wrightd02c5b62014-02-10 15:10:22 -08006143 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006144 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006145 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6146
6147 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6148 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6149 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006150
6151 processKey(mapper, BTN_FORWARD, 0);
6152 processSync(mapper);
6153 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006154 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006155 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006156
6157 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006158 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006159 ASSERT_EQ(0, motionArgs.buttonState);
6160
Michael Wrightd02c5b62014-02-10 15:10:22 -08006161 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6162 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6163 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6164
6165 // press BTN_EXTRA, release BTN_EXTRA
6166 processKey(mapper, BTN_EXTRA, 1);
6167 processSync(mapper);
6168 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6169 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6170 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006171
Michael Wrightd02c5b62014-02-10 15:10:22 -08006172 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006173 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006174 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6175
6176 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6177 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6178 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006179
6180 processKey(mapper, BTN_EXTRA, 0);
6181 processSync(mapper);
6182 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006183 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006184 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006185
6186 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006187 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006188 ASSERT_EQ(0, motionArgs.buttonState);
6189
Michael Wrightd02c5b62014-02-10 15:10:22 -08006190 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6191 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6192 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6193
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006194 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6195
Michael Wrightd02c5b62014-02-10 15:10:22 -08006196 // press BTN_STYLUS, release BTN_STYLUS
6197 processKey(mapper, BTN_STYLUS, 1);
6198 processSync(mapper);
6199 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6200 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006201 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
6202
6203 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6204 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6205 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006206
6207 processKey(mapper, BTN_STYLUS, 0);
6208 processSync(mapper);
6209 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006210 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006211 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006212
6213 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006214 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006215 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006216
6217 // press BTN_STYLUS2, release BTN_STYLUS2
6218 processKey(mapper, BTN_STYLUS2, 1);
6219 processSync(mapper);
6220 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6221 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006222 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
6223
6224 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6225 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6226 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006227
6228 processKey(mapper, BTN_STYLUS2, 0);
6229 processSync(mapper);
6230 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006231 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006232 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006233
6234 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006235 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006236 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006237
6238 // release touch
6239 processUp(mapper);
6240 processSync(mapper);
6241 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6242 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6243 ASSERT_EQ(0, motionArgs.buttonState);
6244}
6245
6246TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006247 addConfigurationProperty("touch.deviceType", "touchScreen");
6248 prepareDisplay(DISPLAY_ORIENTATION_0);
6249 prepareButtons();
6250 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006251 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006252
6253 NotifyMotionArgs motionArgs;
6254
6255 // default tool type is finger
6256 processDown(mapper, 100, 200);
6257 processSync(mapper);
6258 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6259 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6260 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6261
6262 // eraser
6263 processKey(mapper, BTN_TOOL_RUBBER, 1);
6264 processSync(mapper);
6265 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6266 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6267 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6268
6269 // stylus
6270 processKey(mapper, BTN_TOOL_RUBBER, 0);
6271 processKey(mapper, BTN_TOOL_PEN, 1);
6272 processSync(mapper);
6273 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6274 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6275 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6276
6277 // brush
6278 processKey(mapper, BTN_TOOL_PEN, 0);
6279 processKey(mapper, BTN_TOOL_BRUSH, 1);
6280 processSync(mapper);
6281 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6282 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6283 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6284
6285 // pencil
6286 processKey(mapper, BTN_TOOL_BRUSH, 0);
6287 processKey(mapper, BTN_TOOL_PENCIL, 1);
6288 processSync(mapper);
6289 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6290 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6291 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6292
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08006293 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08006294 processKey(mapper, BTN_TOOL_PENCIL, 0);
6295 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
6296 processSync(mapper);
6297 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6298 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6299 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6300
6301 // mouse
6302 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
6303 processKey(mapper, BTN_TOOL_MOUSE, 1);
6304 processSync(mapper);
6305 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6306 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6307 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6308
6309 // lens
6310 processKey(mapper, BTN_TOOL_MOUSE, 0);
6311 processKey(mapper, BTN_TOOL_LENS, 1);
6312 processSync(mapper);
6313 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6314 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6315 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6316
6317 // double-tap
6318 processKey(mapper, BTN_TOOL_LENS, 0);
6319 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
6320 processSync(mapper);
6321 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6322 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6323 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6324
6325 // triple-tap
6326 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
6327 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
6328 processSync(mapper);
6329 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6330 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6331 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6332
6333 // quad-tap
6334 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
6335 processKey(mapper, BTN_TOOL_QUADTAP, 1);
6336 processSync(mapper);
6337 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6338 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6339 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6340
6341 // finger
6342 processKey(mapper, BTN_TOOL_QUADTAP, 0);
6343 processKey(mapper, BTN_TOOL_FINGER, 1);
6344 processSync(mapper);
6345 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6346 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6347 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6348
6349 // stylus trumps finger
6350 processKey(mapper, BTN_TOOL_PEN, 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 // eraser trumps stylus
6357 processKey(mapper, BTN_TOOL_RUBBER, 1);
6358 processSync(mapper);
6359 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6360 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6361 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6362
6363 // mouse trumps eraser
6364 processKey(mapper, BTN_TOOL_MOUSE, 1);
6365 processSync(mapper);
6366 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6367 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6368 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6369
6370 // back to default tool type
6371 processKey(mapper, BTN_TOOL_MOUSE, 0);
6372 processKey(mapper, BTN_TOOL_RUBBER, 0);
6373 processKey(mapper, BTN_TOOL_PEN, 0);
6374 processKey(mapper, BTN_TOOL_FINGER, 0);
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_FINGER, motionArgs.pointerProperties[0].toolType);
6379}
6380
6381TEST_F(SingleTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006382 addConfigurationProperty("touch.deviceType", "touchScreen");
6383 prepareDisplay(DISPLAY_ORIENTATION_0);
6384 prepareButtons();
6385 prepareAxes(POSITION);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006386 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_FINGER, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006387 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006388
6389 NotifyMotionArgs motionArgs;
6390
6391 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
6392 processKey(mapper, BTN_TOOL_FINGER, 1);
6393 processMove(mapper, 100, 200);
6394 processSync(mapper);
6395 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6396 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6397 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6398 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6399
6400 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6401 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6402 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6403 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6404
6405 // move a little
6406 processMove(mapper, 150, 250);
6407 processSync(mapper);
6408 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6409 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6410 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6411 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6412
6413 // down when BTN_TOUCH is pressed, pressure defaults to 1
6414 processKey(mapper, BTN_TOUCH, 1);
6415 processSync(mapper);
6416 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6417 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6418 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6419 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6420
6421 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6422 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6423 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6424 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6425
6426 // up when BTN_TOUCH is released, hover restored
6427 processKey(mapper, BTN_TOUCH, 0);
6428 processSync(mapper);
6429 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6430 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6431 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6432 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6433
6434 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6435 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6436 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6437 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6438
6439 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6440 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6441 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6442 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6443
6444 // exit hover when pointer goes away
6445 processKey(mapper, BTN_TOOL_FINGER, 0);
6446 processSync(mapper);
6447 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6448 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6449 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6450 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6451}
6452
6453TEST_F(SingleTouchInputMapperTest, Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006454 addConfigurationProperty("touch.deviceType", "touchScreen");
6455 prepareDisplay(DISPLAY_ORIENTATION_0);
6456 prepareButtons();
6457 prepareAxes(POSITION | PRESSURE);
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 pressure is 0
6463 processDown(mapper, 100, 200);
6464 processPressure(mapper, 0);
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 pressure is non-zero
6485 processPressure(mapper, RAW_PRESSURE_MAX);
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 pressure becomes 0, hover restored
6498 processPressure(mapper, 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 processUp(mapper);
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
Prabir Pradhan5632d622021-09-06 07:57:20 -07006524// --- TouchDisplayProjectionTest ---
6525
6526class TouchDisplayProjectionTest : public SingleTouchInputMapperTest {
6527public:
6528 // The values inside DisplayViewport are expected to be pre-rotated. This updates the current
6529 // DisplayViewport to pre-rotate the values. The viewport's physical display will be set to the
6530 // rotated equivalent of the given un-rotated physical display bounds.
6531 void configurePhysicalDisplay(int32_t orientation, Rect naturalPhysicalDisplay) {
6532 uint32_t inverseRotationFlags;
6533 auto width = DISPLAY_WIDTH;
6534 auto height = DISPLAY_HEIGHT;
6535 switch (orientation) {
6536 case DISPLAY_ORIENTATION_90:
6537 inverseRotationFlags = ui::Transform::ROT_270;
6538 std::swap(width, height);
6539 break;
6540 case DISPLAY_ORIENTATION_180:
6541 inverseRotationFlags = ui::Transform::ROT_180;
6542 break;
6543 case DISPLAY_ORIENTATION_270:
6544 inverseRotationFlags = ui::Transform::ROT_90;
6545 std::swap(width, height);
6546 break;
6547 case DISPLAY_ORIENTATION_0:
6548 inverseRotationFlags = ui::Transform::ROT_0;
6549 break;
6550 default:
6551 FAIL() << "Invalid orientation: " << orientation;
6552 }
6553
6554 const ui::Transform rotation(inverseRotationFlags, width, height);
6555 const Rect rotatedPhysicalDisplay = rotation.transform(naturalPhysicalDisplay);
6556
6557 std::optional<DisplayViewport> internalViewport =
6558 *mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6559 DisplayViewport& v = *internalViewport;
6560 v.displayId = DISPLAY_ID;
6561 v.orientation = orientation;
6562
6563 v.logicalLeft = 0;
6564 v.logicalTop = 0;
6565 v.logicalRight = 100;
6566 v.logicalBottom = 100;
6567
6568 v.physicalLeft = rotatedPhysicalDisplay.left;
6569 v.physicalTop = rotatedPhysicalDisplay.top;
6570 v.physicalRight = rotatedPhysicalDisplay.right;
6571 v.physicalBottom = rotatedPhysicalDisplay.bottom;
6572
6573 v.deviceWidth = width;
6574 v.deviceHeight = height;
6575
6576 v.isActive = true;
6577 v.uniqueId = UNIQUE_ID;
6578 v.type = ViewportType::INTERNAL;
6579 mFakePolicy->updateViewport(v);
6580 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
6581 }
6582
6583 void assertReceivedMove(const Point& point) {
6584 NotifyMotionArgs motionArgs;
6585 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6586 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6587 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6588 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], point.x, point.y,
6589 1, 0, 0, 0, 0, 0, 0, 0));
6590 }
6591};
6592
6593TEST_F(TouchDisplayProjectionTest, IgnoresTouchesOutsidePhysicalDisplay) {
6594 addConfigurationProperty("touch.deviceType", "touchScreen");
6595 prepareDisplay(DISPLAY_ORIENTATION_0);
6596
6597 prepareButtons();
6598 prepareAxes(POSITION);
6599 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6600
6601 NotifyMotionArgs motionArgs;
6602
6603 // Configure the DisplayViewport such that the logical display maps to a subsection of
6604 // the display panel called the physical display. Here, the physical display is bounded by the
6605 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
6606 static const Rect kPhysicalDisplay{10, 20, 70, 160};
6607 static const std::array<Point, 6> kPointsOutsidePhysicalDisplay{
6608 {{-10, -10}, {0, 0}, {5, 100}, {50, 15}, {75, 100}, {50, 165}}};
6609
6610 for (auto orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90, DISPLAY_ORIENTATION_180,
6611 DISPLAY_ORIENTATION_270}) {
6612 configurePhysicalDisplay(orientation, kPhysicalDisplay);
6613
6614 // Touches outside the physical display should be ignored, and should not generate any
6615 // events. Ensure touches at the following points that lie outside of the physical display
6616 // area do not generate any events.
6617 for (const auto& point : kPointsOutsidePhysicalDisplay) {
6618 processDown(mapper, toRawX(point.x), toRawY(point.y));
6619 processSync(mapper);
6620 processUp(mapper);
6621 processSync(mapper);
6622 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled())
6623 << "Unexpected event generated for touch outside physical display at point: "
6624 << point.x << ", " << point.y;
6625 }
6626 }
6627}
6628
6629TEST_F(TouchDisplayProjectionTest, EmitsTouchDownAfterEnteringPhysicalDisplay) {
6630 addConfigurationProperty("touch.deviceType", "touchScreen");
6631 prepareDisplay(DISPLAY_ORIENTATION_0);
6632
6633 prepareButtons();
6634 prepareAxes(POSITION);
6635 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6636
6637 NotifyMotionArgs motionArgs;
6638
6639 // Configure the DisplayViewport such that the logical display maps to a subsection of
6640 // the display panel called the physical display. Here, the physical display is bounded by the
6641 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
6642 static const Rect kPhysicalDisplay{10, 20, 70, 160};
6643
6644 for (auto orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90, DISPLAY_ORIENTATION_180,
6645 DISPLAY_ORIENTATION_270}) {
6646 configurePhysicalDisplay(orientation, kPhysicalDisplay);
6647
6648 // Touches that start outside the physical display should be ignored until it enters the
6649 // physical display bounds, at which point it should generate a down event. Start a touch at
6650 // the point (5, 100), which is outside the physical display bounds.
6651 static const Point kOutsidePoint{5, 100};
6652 processDown(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
6653 processSync(mapper);
6654 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6655
6656 // Move the touch into the physical display area. This should generate a pointer down.
6657 processMove(mapper, toRawX(11), toRawY(21));
6658 processSync(mapper);
6659 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6660 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6661 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6662 ASSERT_NO_FATAL_FAILURE(
6663 assertPointerCoords(motionArgs.pointerCoords[0], 11, 21, 1, 0, 0, 0, 0, 0, 0, 0));
6664
6665 // Move the touch inside the physical display area. This should generate a pointer move.
6666 processMove(mapper, toRawX(69), toRawY(159));
6667 processSync(mapper);
6668 assertReceivedMove({69, 159});
6669
6670 // Move outside the physical display area. Since the pointer is already down, this should
6671 // now continue generating events.
6672 processMove(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
6673 processSync(mapper);
6674 assertReceivedMove(kOutsidePoint);
6675
6676 // Release. This should generate a pointer up.
6677 processUp(mapper);
6678 processSync(mapper);
6679 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6680 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6681 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], kOutsidePoint.x,
6682 kOutsidePoint.y, 1, 0, 0, 0, 0, 0, 0, 0));
6683
6684 // Ensure no more events were generated.
6685 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6686 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6687 }
6688}
6689
Michael Wrightd02c5b62014-02-10 15:10:22 -08006690// --- MultiTouchInputMapperTest ---
6691
6692class MultiTouchInputMapperTest : public TouchInputMapperTest {
6693protected:
6694 void prepareAxes(int axes);
6695
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006696 void processPosition(MultiTouchInputMapper& mapper, int32_t x, int32_t y);
6697 void processTouchMajor(MultiTouchInputMapper& mapper, int32_t touchMajor);
6698 void processTouchMinor(MultiTouchInputMapper& mapper, int32_t touchMinor);
6699 void processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor);
6700 void processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor);
6701 void processOrientation(MultiTouchInputMapper& mapper, int32_t orientation);
6702 void processPressure(MultiTouchInputMapper& mapper, int32_t pressure);
6703 void processDistance(MultiTouchInputMapper& mapper, int32_t distance);
6704 void processId(MultiTouchInputMapper& mapper, int32_t id);
6705 void processSlot(MultiTouchInputMapper& mapper, int32_t slot);
6706 void processToolType(MultiTouchInputMapper& mapper, int32_t toolType);
6707 void processKey(MultiTouchInputMapper& mapper, int32_t code, int32_t value);
6708 void processMTSync(MultiTouchInputMapper& mapper);
6709 void processSync(MultiTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006710};
6711
6712void MultiTouchInputMapperTest::prepareAxes(int axes) {
6713 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006714 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
6715 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006716 }
6717 if (axes & TOUCH) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006718 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN,
6719 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006720 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006721 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN,
6722 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006723 }
6724 }
6725 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006726 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
6727 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006728 if (axes & MINOR) {
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08006729 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006730 RAW_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006731 }
6732 }
6733 if (axes & ORIENTATION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006734 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_ORIENTATION, RAW_ORIENTATION_MIN,
6735 RAW_ORIENTATION_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006736 }
6737 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006738 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_PRESSURE, RAW_PRESSURE_MIN,
6739 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006740 }
6741 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006742 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_DISTANCE, RAW_DISTANCE_MIN,
6743 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006744 }
6745 if (axes & ID) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006746 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX, 0,
6747 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006748 }
6749 if (axes & SLOT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006750 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
6751 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_MT_SLOT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006752 }
6753 if (axes & TOOL_TYPE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006754 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOOL_TYPE, 0, MT_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006755 }
6756}
6757
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006758void MultiTouchInputMapperTest::processPosition(MultiTouchInputMapper& mapper, int32_t x,
6759 int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006760 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_X, x);
6761 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006762}
6763
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006764void MultiTouchInputMapperTest::processTouchMajor(MultiTouchInputMapper& mapper,
6765 int32_t touchMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006766 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006767}
6768
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006769void MultiTouchInputMapperTest::processTouchMinor(MultiTouchInputMapper& mapper,
6770 int32_t touchMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006771 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006772}
6773
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006774void MultiTouchInputMapperTest::processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006775 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006776}
6777
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006778void MultiTouchInputMapperTest::processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006779 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006780}
6781
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006782void MultiTouchInputMapperTest::processOrientation(MultiTouchInputMapper& mapper,
6783 int32_t orientation) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006784 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_ORIENTATION, orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006785}
6786
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006787void MultiTouchInputMapperTest::processPressure(MultiTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006788 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006789}
6790
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006791void MultiTouchInputMapperTest::processDistance(MultiTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006792 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006793}
6794
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006795void MultiTouchInputMapperTest::processId(MultiTouchInputMapper& mapper, int32_t id) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006796 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TRACKING_ID, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006797}
6798
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006799void MultiTouchInputMapperTest::processSlot(MultiTouchInputMapper& mapper, int32_t slot) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006800 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_SLOT, slot);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006801}
6802
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006803void MultiTouchInputMapperTest::processToolType(MultiTouchInputMapper& mapper, int32_t toolType) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006804 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006805}
6806
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006807void MultiTouchInputMapperTest::processKey(MultiTouchInputMapper& mapper, int32_t code,
6808 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006809 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006810}
6811
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006812void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006813 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_MT_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006814}
6815
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006816void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006817 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006818}
6819
Michael Wrightd02c5b62014-02-10 15:10:22 -08006820TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006821 addConfigurationProperty("touch.deviceType", "touchScreen");
6822 prepareDisplay(DISPLAY_ORIENTATION_0);
6823 prepareAxes(POSITION);
6824 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006825 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006826
arthurhungdcef2dc2020-08-11 14:47:50 +08006827 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006828
6829 NotifyMotionArgs motionArgs;
6830
6831 // Two fingers down at once.
6832 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
6833 processPosition(mapper, x1, y1);
6834 processMTSync(mapper);
6835 processPosition(mapper, x2, y2);
6836 processMTSync(mapper);
6837 processSync(mapper);
6838
6839 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6840 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6841 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6842 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6843 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6844 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6845 ASSERT_EQ(0, motionArgs.flags);
6846 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6847 ASSERT_EQ(0, motionArgs.buttonState);
6848 ASSERT_EQ(0, motionArgs.edgeFlags);
6849 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6850 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6851 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6852 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6853 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6854 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6855 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6856 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6857
6858 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6859 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6860 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6861 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6862 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08006863 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006864 ASSERT_EQ(0, motionArgs.flags);
6865 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6866 ASSERT_EQ(0, motionArgs.buttonState);
6867 ASSERT_EQ(0, motionArgs.edgeFlags);
6868 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6869 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6870 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6871 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6872 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6873 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6874 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6875 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6876 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6877 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6878 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6879 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6880
6881 // Move.
6882 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
6883 processPosition(mapper, x1, y1);
6884 processMTSync(mapper);
6885 processPosition(mapper, x2, y2);
6886 processMTSync(mapper);
6887 processSync(mapper);
6888
6889 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6890 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6891 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6892 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6893 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6894 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6895 ASSERT_EQ(0, motionArgs.flags);
6896 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6897 ASSERT_EQ(0, motionArgs.buttonState);
6898 ASSERT_EQ(0, motionArgs.edgeFlags);
6899 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6900 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6901 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6902 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6903 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6904 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6905 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6906 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6907 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6908 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6909 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6910 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6911
6912 // First finger up.
6913 x2 += 15; y2 -= 20;
6914 processPosition(mapper, x2, y2);
6915 processMTSync(mapper);
6916 processSync(mapper);
6917
6918 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6919 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6920 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6921 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6922 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08006923 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006924 ASSERT_EQ(0, motionArgs.flags);
6925 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6926 ASSERT_EQ(0, motionArgs.buttonState);
6927 ASSERT_EQ(0, motionArgs.edgeFlags);
6928 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6929 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6930 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6931 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6932 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6933 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6934 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6935 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6936 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6937 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6938 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6939 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6940
6941 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6942 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6943 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6944 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6945 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6946 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6947 ASSERT_EQ(0, motionArgs.flags);
6948 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6949 ASSERT_EQ(0, motionArgs.buttonState);
6950 ASSERT_EQ(0, motionArgs.edgeFlags);
6951 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6952 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6953 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6954 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6955 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6956 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6957 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6958 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6959
6960 // Move.
6961 x2 += 20; y2 -= 25;
6962 processPosition(mapper, x2, y2);
6963 processMTSync(mapper);
6964 processSync(mapper);
6965
6966 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6967 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6968 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6969 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6970 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6971 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6972 ASSERT_EQ(0, motionArgs.flags);
6973 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6974 ASSERT_EQ(0, motionArgs.buttonState);
6975 ASSERT_EQ(0, motionArgs.edgeFlags);
6976 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6977 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6978 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6979 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6980 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6981 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6982 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6983 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6984
6985 // New finger down.
6986 int32_t x3 = 700, y3 = 300;
6987 processPosition(mapper, x2, y2);
6988 processMTSync(mapper);
6989 processPosition(mapper, x3, y3);
6990 processMTSync(mapper);
6991 processSync(mapper);
6992
6993 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6994 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6995 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6996 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6997 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08006998 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006999 ASSERT_EQ(0, motionArgs.flags);
7000 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7001 ASSERT_EQ(0, motionArgs.buttonState);
7002 ASSERT_EQ(0, motionArgs.edgeFlags);
7003 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7004 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7005 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7006 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7007 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7008 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7009 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7010 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7011 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7012 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7013 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7014 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7015
7016 // Second finger up.
7017 x3 += 30; y3 -= 20;
7018 processPosition(mapper, x3, y3);
7019 processMTSync(mapper);
7020 processSync(mapper);
7021
7022 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7023 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7024 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7025 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7026 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007027 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007028 ASSERT_EQ(0, motionArgs.flags);
7029 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7030 ASSERT_EQ(0, motionArgs.buttonState);
7031 ASSERT_EQ(0, motionArgs.edgeFlags);
7032 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7033 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7034 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7035 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7036 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7037 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7038 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7039 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7040 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7041 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7042 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7043 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7044
7045 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7046 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7047 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7048 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7049 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7050 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7051 ASSERT_EQ(0, motionArgs.flags);
7052 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7053 ASSERT_EQ(0, motionArgs.buttonState);
7054 ASSERT_EQ(0, motionArgs.edgeFlags);
7055 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7056 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7057 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7058 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7059 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7060 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7061 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7062 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7063
7064 // Last finger up.
7065 processMTSync(mapper);
7066 processSync(mapper);
7067
7068 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7069 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7070 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7071 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7072 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7073 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7074 ASSERT_EQ(0, motionArgs.flags);
7075 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7076 ASSERT_EQ(0, motionArgs.buttonState);
7077 ASSERT_EQ(0, motionArgs.edgeFlags);
7078 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7079 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7080 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7081 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7082 toDisplayX(x3), toDisplayY(y3), 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 // Should not have sent any more keys or motions.
7088 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7089 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7090}
7091
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007092TEST_F(MultiTouchInputMapperTest, AxisResolution_IsPopulated) {
7093 addConfigurationProperty("touch.deviceType", "touchScreen");
7094 prepareDisplay(DISPLAY_ORIENTATION_0);
7095
7096 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
7097 /*fuzz*/ 0, /*resolution*/ 10);
7098 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
7099 /*fuzz*/ 0, /*resolution*/ 11);
7100 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
7101 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 12);
7102 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
7103 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 13);
7104 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7105 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 14);
7106 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7107 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 15);
7108
7109 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7110
7111 // X and Y axes
7112 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_X, 10 / X_PRECISION);
7113 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_Y, 11 / Y_PRECISION);
7114 // Touch major and minor
7115 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR, 12 * GEOMETRIC_SCALE);
7116 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR, 13 * GEOMETRIC_SCALE);
7117 // Tool major and minor
7118 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR, 14 * GEOMETRIC_SCALE);
7119 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR, 15 * GEOMETRIC_SCALE);
7120}
7121
7122TEST_F(MultiTouchInputMapperTest, TouchMajorAndMinorAxes_DoNotAppearIfNotSupported) {
7123 addConfigurationProperty("touch.deviceType", "touchScreen");
7124 prepareDisplay(DISPLAY_ORIENTATION_0);
7125
7126 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
7127 /*fuzz*/ 0, /*resolution*/ 10);
7128 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
7129 /*fuzz*/ 0, /*resolution*/ 11);
7130
7131 // We do not add ABS_MT_TOUCH_MAJOR / MINOR or ABS_MT_WIDTH_MAJOR / MINOR axes
7132
7133 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7134
7135 // Touch major and minor
7136 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR);
7137 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR);
7138 // Tool major and minor
7139 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR);
7140 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR);
7141}
7142
Michael Wrightd02c5b62014-02-10 15:10:22 -08007143TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007144 addConfigurationProperty("touch.deviceType", "touchScreen");
7145 prepareDisplay(DISPLAY_ORIENTATION_0);
7146 prepareAxes(POSITION | ID);
7147 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007148 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007149
arthurhungdcef2dc2020-08-11 14:47:50 +08007150 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007151
7152 NotifyMotionArgs motionArgs;
7153
7154 // Two fingers down at once.
7155 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7156 processPosition(mapper, x1, y1);
7157 processId(mapper, 1);
7158 processMTSync(mapper);
7159 processPosition(mapper, x2, y2);
7160 processId(mapper, 2);
7161 processMTSync(mapper);
7162 processSync(mapper);
7163
7164 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7165 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7166 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7167 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7168 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7169 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7170 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7171
7172 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007173 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007174 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7175 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7176 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7177 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7178 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7179 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7180 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7181 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7182 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7183
7184 // Move.
7185 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7186 processPosition(mapper, x1, y1);
7187 processId(mapper, 1);
7188 processMTSync(mapper);
7189 processPosition(mapper, x2, y2);
7190 processId(mapper, 2);
7191 processMTSync(mapper);
7192 processSync(mapper);
7193
7194 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7195 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7196 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7197 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7198 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7199 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7200 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7201 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7202 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7203 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7204 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7205
7206 // First finger up.
7207 x2 += 15; y2 -= 20;
7208 processPosition(mapper, x2, y2);
7209 processId(mapper, 2);
7210 processMTSync(mapper);
7211 processSync(mapper);
7212
7213 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007214 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007215 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7216 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7217 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7218 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7219 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7220 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7221 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7222 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7223 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7224
7225 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7226 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7227 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7228 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7229 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7230 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7231 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7232
7233 // Move.
7234 x2 += 20; y2 -= 25;
7235 processPosition(mapper, x2, y2);
7236 processId(mapper, 2);
7237 processMTSync(mapper);
7238 processSync(mapper);
7239
7240 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7241 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7242 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7243 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7244 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7245 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7246 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7247
7248 // New finger down.
7249 int32_t x3 = 700, y3 = 300;
7250 processPosition(mapper, x2, y2);
7251 processId(mapper, 2);
7252 processMTSync(mapper);
7253 processPosition(mapper, x3, y3);
7254 processId(mapper, 3);
7255 processMTSync(mapper);
7256 processSync(mapper);
7257
7258 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007259 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007260 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7261 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7262 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7263 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7264 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7265 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7266 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7267 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7268 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7269
7270 // Second finger up.
7271 x3 += 30; y3 -= 20;
7272 processPosition(mapper, x3, y3);
7273 processId(mapper, 3);
7274 processMTSync(mapper);
7275 processSync(mapper);
7276
7277 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007278 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007279 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7280 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7281 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7282 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7283 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7284 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7285 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7286 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7287 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7288
7289 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7290 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7291 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7292 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7293 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7294 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7295 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7296
7297 // Last finger up.
7298 processMTSync(mapper);
7299 processSync(mapper);
7300
7301 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7302 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7303 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7304 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7305 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7306 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7307 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7308
7309 // Should not have sent any more keys or motions.
7310 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7311 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7312}
7313
7314TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007315 addConfigurationProperty("touch.deviceType", "touchScreen");
7316 prepareDisplay(DISPLAY_ORIENTATION_0);
7317 prepareAxes(POSITION | ID | SLOT);
7318 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007319 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007320
arthurhungdcef2dc2020-08-11 14:47:50 +08007321 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007322
7323 NotifyMotionArgs motionArgs;
7324
7325 // Two fingers down at once.
7326 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7327 processPosition(mapper, x1, y1);
7328 processId(mapper, 1);
7329 processSlot(mapper, 1);
7330 processPosition(mapper, x2, y2);
7331 processId(mapper, 2);
7332 processSync(mapper);
7333
7334 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7335 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7336 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7337 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7338 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7339 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7340 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7341
7342 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007343 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007344 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7345 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7346 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7347 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7348 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7349 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7350 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7351 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7352 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7353
7354 // Move.
7355 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7356 processSlot(mapper, 0);
7357 processPosition(mapper, x1, y1);
7358 processSlot(mapper, 1);
7359 processPosition(mapper, x2, y2);
7360 processSync(mapper);
7361
7362 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7363 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7364 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7365 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7366 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7367 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7368 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7369 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7370 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7371 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7372 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7373
7374 // First finger up.
7375 x2 += 15; y2 -= 20;
7376 processSlot(mapper, 0);
7377 processId(mapper, -1);
7378 processSlot(mapper, 1);
7379 processPosition(mapper, x2, y2);
7380 processSync(mapper);
7381
7382 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007383 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007384 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7385 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7386 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7387 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7388 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7389 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7390 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7391 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7392 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7393
7394 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7395 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7396 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7397 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7398 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7399 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7400 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7401
7402 // Move.
7403 x2 += 20; y2 -= 25;
7404 processPosition(mapper, x2, y2);
7405 processSync(mapper);
7406
7407 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7408 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7409 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7410 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7411 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7412 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7413 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7414
7415 // New finger down.
7416 int32_t x3 = 700, y3 = 300;
7417 processPosition(mapper, x2, y2);
7418 processSlot(mapper, 0);
7419 processId(mapper, 3);
7420 processPosition(mapper, x3, y3);
7421 processSync(mapper);
7422
7423 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007424 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007425 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7426 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7427 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7428 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7429 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7430 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7431 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7432 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7433 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7434
7435 // Second finger up.
7436 x3 += 30; y3 -= 20;
7437 processSlot(mapper, 1);
7438 processId(mapper, -1);
7439 processSlot(mapper, 0);
7440 processPosition(mapper, x3, y3);
7441 processSync(mapper);
7442
7443 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007444 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007445 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7446 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7447 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7448 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7449 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7450 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7451 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7452 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7453 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7454
7455 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7456 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7457 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7458 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7459 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7460 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7461 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7462
7463 // Last finger up.
7464 processId(mapper, -1);
7465 processSync(mapper);
7466
7467 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7468 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7469 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7470 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7471 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7472 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7473 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7474
7475 // Should not have sent any more keys or motions.
7476 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7477 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7478}
7479
7480TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007481 addConfigurationProperty("touch.deviceType", "touchScreen");
7482 prepareDisplay(DISPLAY_ORIENTATION_0);
7483 prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007484 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007485
7486 // These calculations are based on the input device calibration documentation.
7487 int32_t rawX = 100;
7488 int32_t rawY = 200;
7489 int32_t rawTouchMajor = 7;
7490 int32_t rawTouchMinor = 6;
7491 int32_t rawToolMajor = 9;
7492 int32_t rawToolMinor = 8;
7493 int32_t rawPressure = 11;
7494 int32_t rawDistance = 0;
7495 int32_t rawOrientation = 3;
7496 int32_t id = 5;
7497
7498 float x = toDisplayX(rawX);
7499 float y = toDisplayY(rawY);
7500 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
7501 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
7502 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
7503 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
7504 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
7505 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
7506 float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
7507 float distance = float(rawDistance);
7508
7509 processPosition(mapper, rawX, rawY);
7510 processTouchMajor(mapper, rawTouchMajor);
7511 processTouchMinor(mapper, rawTouchMinor);
7512 processToolMajor(mapper, rawToolMajor);
7513 processToolMinor(mapper, rawToolMinor);
7514 processPressure(mapper, rawPressure);
7515 processOrientation(mapper, rawOrientation);
7516 processDistance(mapper, rawDistance);
7517 processId(mapper, id);
7518 processMTSync(mapper);
7519 processSync(mapper);
7520
7521 NotifyMotionArgs args;
7522 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7523 ASSERT_EQ(0, args.pointerProperties[0].id);
7524 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7525 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
7526 orientation, distance));
7527}
7528
7529TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007530 addConfigurationProperty("touch.deviceType", "touchScreen");
7531 prepareDisplay(DISPLAY_ORIENTATION_0);
7532 prepareAxes(POSITION | TOUCH | TOOL | MINOR);
7533 addConfigurationProperty("touch.size.calibration", "geometric");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007534 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007535
7536 // These calculations are based on the input device calibration documentation.
7537 int32_t rawX = 100;
7538 int32_t rawY = 200;
7539 int32_t rawTouchMajor = 140;
7540 int32_t rawTouchMinor = 120;
7541 int32_t rawToolMajor = 180;
7542 int32_t rawToolMinor = 160;
7543
7544 float x = toDisplayX(rawX);
7545 float y = toDisplayY(rawY);
7546 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
7547 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
7548 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
7549 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
7550 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
7551
7552 processPosition(mapper, rawX, rawY);
7553 processTouchMajor(mapper, rawTouchMajor);
7554 processTouchMinor(mapper, rawTouchMinor);
7555 processToolMajor(mapper, rawToolMajor);
7556 processToolMinor(mapper, rawToolMinor);
7557 processMTSync(mapper);
7558 processSync(mapper);
7559
7560 NotifyMotionArgs args;
7561 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7562 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7563 x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
7564}
7565
7566TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007567 addConfigurationProperty("touch.deviceType", "touchScreen");
7568 prepareDisplay(DISPLAY_ORIENTATION_0);
7569 prepareAxes(POSITION | TOUCH | TOOL);
7570 addConfigurationProperty("touch.size.calibration", "diameter");
7571 addConfigurationProperty("touch.size.scale", "10");
7572 addConfigurationProperty("touch.size.bias", "160");
7573 addConfigurationProperty("touch.size.isSummed", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007574 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007575
7576 // These calculations are based on the input device calibration documentation.
7577 // Note: We only provide a single common touch/tool value because the device is assumed
7578 // not to emit separate values for each pointer (isSummed = 1).
7579 int32_t rawX = 100;
7580 int32_t rawY = 200;
7581 int32_t rawX2 = 150;
7582 int32_t rawY2 = 250;
7583 int32_t rawTouchMajor = 5;
7584 int32_t rawToolMajor = 8;
7585
7586 float x = toDisplayX(rawX);
7587 float y = toDisplayY(rawY);
7588 float x2 = toDisplayX(rawX2);
7589 float y2 = toDisplayY(rawY2);
7590 float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
7591 float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
7592 float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
7593
7594 processPosition(mapper, rawX, rawY);
7595 processTouchMajor(mapper, rawTouchMajor);
7596 processToolMajor(mapper, rawToolMajor);
7597 processMTSync(mapper);
7598 processPosition(mapper, rawX2, rawY2);
7599 processTouchMajor(mapper, rawTouchMajor);
7600 processToolMajor(mapper, rawToolMajor);
7601 processMTSync(mapper);
7602 processSync(mapper);
7603
7604 NotifyMotionArgs args;
7605 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7606 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
7607
7608 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007609 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007610 ASSERT_EQ(size_t(2), args.pointerCount);
7611 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7612 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
7613 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
7614 x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
7615}
7616
7617TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007618 addConfigurationProperty("touch.deviceType", "touchScreen");
7619 prepareDisplay(DISPLAY_ORIENTATION_0);
7620 prepareAxes(POSITION | TOUCH | TOOL);
7621 addConfigurationProperty("touch.size.calibration", "area");
7622 addConfigurationProperty("touch.size.scale", "43");
7623 addConfigurationProperty("touch.size.bias", "3");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007624 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007625
7626 // These calculations are based on the input device calibration documentation.
7627 int32_t rawX = 100;
7628 int32_t rawY = 200;
7629 int32_t rawTouchMajor = 5;
7630 int32_t rawToolMajor = 8;
7631
7632 float x = toDisplayX(rawX);
7633 float y = toDisplayY(rawY);
7634 float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
7635 float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
7636 float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
7637
7638 processPosition(mapper, rawX, rawY);
7639 processTouchMajor(mapper, rawTouchMajor);
7640 processToolMajor(mapper, rawToolMajor);
7641 processMTSync(mapper);
7642 processSync(mapper);
7643
7644 NotifyMotionArgs args;
7645 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7646 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7647 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
7648}
7649
7650TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007651 addConfigurationProperty("touch.deviceType", "touchScreen");
7652 prepareDisplay(DISPLAY_ORIENTATION_0);
7653 prepareAxes(POSITION | PRESSURE);
7654 addConfigurationProperty("touch.pressure.calibration", "amplitude");
7655 addConfigurationProperty("touch.pressure.scale", "0.01");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007656 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007657
Michael Wrightaa449c92017-12-13 21:21:43 +00007658 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007659 mapper.populateDeviceInfo(&info);
Michael Wrightaa449c92017-12-13 21:21:43 +00007660 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
7661 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TOUCHSCREEN,
7662 0.0f, RAW_PRESSURE_MAX * 0.01, 0.0f, 0.0f));
7663
Michael Wrightd02c5b62014-02-10 15:10:22 -08007664 // These calculations are based on the input device calibration documentation.
7665 int32_t rawX = 100;
7666 int32_t rawY = 200;
7667 int32_t rawPressure = 60;
7668
7669 float x = toDisplayX(rawX);
7670 float y = toDisplayY(rawY);
7671 float pressure = float(rawPressure) * 0.01f;
7672
7673 processPosition(mapper, rawX, rawY);
7674 processPressure(mapper, rawPressure);
7675 processMTSync(mapper);
7676 processSync(mapper);
7677
7678 NotifyMotionArgs args;
7679 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7680 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7681 x, y, pressure, 0, 0, 0, 0, 0, 0, 0));
7682}
7683
7684TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007685 addConfigurationProperty("touch.deviceType", "touchScreen");
7686 prepareDisplay(DISPLAY_ORIENTATION_0);
7687 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007688 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007689
7690 NotifyMotionArgs motionArgs;
7691 NotifyKeyArgs keyArgs;
7692
7693 processId(mapper, 1);
7694 processPosition(mapper, 100, 200);
7695 processSync(mapper);
7696 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7697 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7698 ASSERT_EQ(0, motionArgs.buttonState);
7699
7700 // press BTN_LEFT, release BTN_LEFT
7701 processKey(mapper, BTN_LEFT, 1);
7702 processSync(mapper);
7703 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7704 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7705 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
7706
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007707 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7708 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7709 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
7710
Michael Wrightd02c5b62014-02-10 15:10:22 -08007711 processKey(mapper, BTN_LEFT, 0);
7712 processSync(mapper);
7713 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007714 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007715 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007716
7717 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007718 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007719 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007720
7721 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
7722 processKey(mapper, BTN_RIGHT, 1);
7723 processKey(mapper, BTN_MIDDLE, 1);
7724 processSync(mapper);
7725 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7726 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7727 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
7728 motionArgs.buttonState);
7729
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007730 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7731 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7732 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
7733
7734 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7735 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7736 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
7737 motionArgs.buttonState);
7738
Michael Wrightd02c5b62014-02-10 15:10:22 -08007739 processKey(mapper, BTN_RIGHT, 0);
7740 processSync(mapper);
7741 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007742 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007743 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007744
7745 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007746 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007747 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007748
7749 processKey(mapper, BTN_MIDDLE, 0);
7750 processSync(mapper);
7751 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007752 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007753 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007754
7755 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007756 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007757 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007758
7759 // press BTN_BACK, release BTN_BACK
7760 processKey(mapper, BTN_BACK, 1);
7761 processSync(mapper);
7762 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7763 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
7764 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007765
Michael Wrightd02c5b62014-02-10 15:10:22 -08007766 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007767 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007768 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
7769
7770 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7771 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7772 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007773
7774 processKey(mapper, BTN_BACK, 0);
7775 processSync(mapper);
7776 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007777 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007778 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007779
7780 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007781 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007782 ASSERT_EQ(0, motionArgs.buttonState);
7783
Michael Wrightd02c5b62014-02-10 15:10:22 -08007784 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7785 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
7786 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
7787
7788 // press BTN_SIDE, release BTN_SIDE
7789 processKey(mapper, BTN_SIDE, 1);
7790 processSync(mapper);
7791 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7792 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
7793 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007794
Michael Wrightd02c5b62014-02-10 15:10:22 -08007795 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007796 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007797 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
7798
7799 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7800 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7801 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007802
7803 processKey(mapper, BTN_SIDE, 0);
7804 processSync(mapper);
7805 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007806 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007807 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007808
7809 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007810 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007811 ASSERT_EQ(0, motionArgs.buttonState);
7812
Michael Wrightd02c5b62014-02-10 15:10:22 -08007813 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7814 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
7815 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
7816
7817 // press BTN_FORWARD, release BTN_FORWARD
7818 processKey(mapper, BTN_FORWARD, 1);
7819 processSync(mapper);
7820 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7821 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
7822 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007823
Michael Wrightd02c5b62014-02-10 15:10:22 -08007824 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007825 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007826 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
7827
7828 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7829 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7830 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007831
7832 processKey(mapper, BTN_FORWARD, 0);
7833 processSync(mapper);
7834 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007835 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007836 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007837
7838 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007839 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007840 ASSERT_EQ(0, motionArgs.buttonState);
7841
Michael Wrightd02c5b62014-02-10 15:10:22 -08007842 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7843 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
7844 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
7845
7846 // press BTN_EXTRA, release BTN_EXTRA
7847 processKey(mapper, BTN_EXTRA, 1);
7848 processSync(mapper);
7849 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7850 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
7851 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007852
Michael Wrightd02c5b62014-02-10 15:10:22 -08007853 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007854 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007855 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
7856
7857 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7858 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7859 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007860
7861 processKey(mapper, BTN_EXTRA, 0);
7862 processSync(mapper);
7863 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007864 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007865 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007866
7867 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007868 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007869 ASSERT_EQ(0, motionArgs.buttonState);
7870
Michael Wrightd02c5b62014-02-10 15:10:22 -08007871 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7872 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
7873 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
7874
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007875 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7876
Michael Wrightd02c5b62014-02-10 15:10:22 -08007877 // press BTN_STYLUS, release BTN_STYLUS
7878 processKey(mapper, BTN_STYLUS, 1);
7879 processSync(mapper);
7880 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7881 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007882 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
7883
7884 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7885 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7886 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007887
7888 processKey(mapper, BTN_STYLUS, 0);
7889 processSync(mapper);
7890 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007891 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007892 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007893
7894 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007895 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007896 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007897
7898 // press BTN_STYLUS2, release BTN_STYLUS2
7899 processKey(mapper, BTN_STYLUS2, 1);
7900 processSync(mapper);
7901 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7902 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007903 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
7904
7905 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7906 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7907 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007908
7909 processKey(mapper, BTN_STYLUS2, 0);
7910 processSync(mapper);
7911 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007912 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007913 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007914
7915 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007916 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007917 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007918
7919 // release touch
7920 processId(mapper, -1);
7921 processSync(mapper);
7922 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7923 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7924 ASSERT_EQ(0, motionArgs.buttonState);
7925}
7926
7927TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007928 addConfigurationProperty("touch.deviceType", "touchScreen");
7929 prepareDisplay(DISPLAY_ORIENTATION_0);
7930 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007931 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007932
7933 NotifyMotionArgs motionArgs;
7934
7935 // default tool type is finger
7936 processId(mapper, 1);
7937 processPosition(mapper, 100, 200);
7938 processSync(mapper);
7939 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7940 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7941 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7942
7943 // eraser
7944 processKey(mapper, BTN_TOOL_RUBBER, 1);
7945 processSync(mapper);
7946 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7947 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7948 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
7949
7950 // stylus
7951 processKey(mapper, BTN_TOOL_RUBBER, 0);
7952 processKey(mapper, BTN_TOOL_PEN, 1);
7953 processSync(mapper);
7954 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7955 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7956 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7957
7958 // brush
7959 processKey(mapper, BTN_TOOL_PEN, 0);
7960 processKey(mapper, BTN_TOOL_BRUSH, 1);
7961 processSync(mapper);
7962 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7963 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7964 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7965
7966 // pencil
7967 processKey(mapper, BTN_TOOL_BRUSH, 0);
7968 processKey(mapper, BTN_TOOL_PENCIL, 1);
7969 processSync(mapper);
7970 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7971 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7972 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7973
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08007974 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08007975 processKey(mapper, BTN_TOOL_PENCIL, 0);
7976 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
7977 processSync(mapper);
7978 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7979 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7980 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7981
7982 // mouse
7983 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
7984 processKey(mapper, BTN_TOOL_MOUSE, 1);
7985 processSync(mapper);
7986 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7987 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7988 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
7989
7990 // lens
7991 processKey(mapper, BTN_TOOL_MOUSE, 0);
7992 processKey(mapper, BTN_TOOL_LENS, 1);
7993 processSync(mapper);
7994 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7995 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7996 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
7997
7998 // double-tap
7999 processKey(mapper, BTN_TOOL_LENS, 0);
8000 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
8001 processSync(mapper);
8002 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8003 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8004 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8005
8006 // triple-tap
8007 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
8008 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
8009 processSync(mapper);
8010 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8011 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8012 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8013
8014 // quad-tap
8015 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
8016 processKey(mapper, BTN_TOOL_QUADTAP, 1);
8017 processSync(mapper);
8018 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8019 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8020 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8021
8022 // finger
8023 processKey(mapper, BTN_TOOL_QUADTAP, 0);
8024 processKey(mapper, BTN_TOOL_FINGER, 1);
8025 processSync(mapper);
8026 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8027 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8028 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8029
8030 // stylus trumps finger
8031 processKey(mapper, BTN_TOOL_PEN, 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 // eraser trumps stylus
8038 processKey(mapper, BTN_TOOL_RUBBER, 1);
8039 processSync(mapper);
8040 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8041 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8042 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
8043
8044 // mouse trumps eraser
8045 processKey(mapper, BTN_TOOL_MOUSE, 1);
8046 processSync(mapper);
8047 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8048 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8049 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
8050
8051 // MT tool type trumps BTN tool types: MT_TOOL_FINGER
8052 processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
8053 processSync(mapper);
8054 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8055 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8056 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8057
8058 // MT tool type trumps BTN tool types: MT_TOOL_PEN
8059 processToolType(mapper, MT_TOOL_PEN);
8060 processSync(mapper);
8061 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8062 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8063 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8064
8065 // back to default tool type
8066 processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
8067 processKey(mapper, BTN_TOOL_MOUSE, 0);
8068 processKey(mapper, BTN_TOOL_RUBBER, 0);
8069 processKey(mapper, BTN_TOOL_PEN, 0);
8070 processKey(mapper, BTN_TOOL_FINGER, 0);
8071 processSync(mapper);
8072 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8073 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8074 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8075}
8076
8077TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008078 addConfigurationProperty("touch.deviceType", "touchScreen");
8079 prepareDisplay(DISPLAY_ORIENTATION_0);
8080 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008081 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008082 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008083
8084 NotifyMotionArgs motionArgs;
8085
8086 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
8087 processId(mapper, 1);
8088 processPosition(mapper, 100, 200);
8089 processSync(mapper);
8090 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8091 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8092 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8093 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8094
8095 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8096 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8097 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8098 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8099
8100 // move a little
8101 processPosition(mapper, 150, 250);
8102 processSync(mapper);
8103 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8104 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8105 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8106 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8107
8108 // down when BTN_TOUCH is pressed, pressure defaults to 1
8109 processKey(mapper, BTN_TOUCH, 1);
8110 processSync(mapper);
8111 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8112 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8113 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8114 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8115
8116 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8117 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8118 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8119 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8120
8121 // up when BTN_TOUCH is released, hover restored
8122 processKey(mapper, BTN_TOUCH, 0);
8123 processSync(mapper);
8124 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8125 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8126 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8127 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8128
8129 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8130 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8131 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8132 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8133
8134 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8135 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8136 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8137 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8138
8139 // exit hover when pointer goes away
8140 processId(mapper, -1);
8141 processSync(mapper);
8142 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8143 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8144 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8145 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8146}
8147
8148TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008149 addConfigurationProperty("touch.deviceType", "touchScreen");
8150 prepareDisplay(DISPLAY_ORIENTATION_0);
8151 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008152 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008153
8154 NotifyMotionArgs motionArgs;
8155
8156 // initially hovering because pressure is 0
8157 processId(mapper, 1);
8158 processPosition(mapper, 100, 200);
8159 processPressure(mapper, 0);
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 pressure becomes non-zero
8180 processPressure(mapper, RAW_PRESSURE_MAX);
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 pressure becomes 0, hover restored
8193 processPressure(mapper, 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
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008219/**
8220 * Set the input device port <--> display port associations, and check that the
8221 * events are routed to the display that matches the display port.
8222 * This can be checked by looking at the displayId of the resulting NotifyMotionArgs.
8223 */
8224TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayPort) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008225 const std::string usb2 = "USB2";
8226 const uint8_t hdmi1 = 0;
8227 const uint8_t hdmi2 = 1;
8228 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008229 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008230
8231 addConfigurationProperty("touch.deviceType", "touchScreen");
8232 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008233 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008234
8235 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
8236 mFakePolicy->addInputPortAssociation(usb2, hdmi2);
8237
8238 // We are intentionally not adding the viewport for display 1 yet. Since the port association
8239 // for this input device is specified, and the matching viewport is not present,
8240 // the input device should be disabled (at the mapper level).
8241
8242 // Add viewport for display 2 on hdmi2
8243 prepareSecondaryDisplay(type, hdmi2);
8244 // Send a touch event
8245 processPosition(mapper, 100, 100);
8246 processSync(mapper);
8247 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8248
8249 // Add viewport for display 1 on hdmi1
8250 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
8251 // Send a touch event again
8252 processPosition(mapper, 100, 100);
8253 processSync(mapper);
8254
8255 NotifyMotionArgs args;
8256 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8257 ASSERT_EQ(DISPLAY_ID, args.displayId);
8258}
Michael Wrightd02c5b62014-02-10 15:10:22 -08008259
Arthur Hung6d5b4b22022-01-21 07:21:10 +00008260TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayUniqueId) {
8261 addConfigurationProperty("touch.deviceType", "touchScreen");
8262 prepareAxes(POSITION);
8263 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8264
8265 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
8266
8267 prepareDisplay(DISPLAY_ORIENTATION_0);
8268 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
8269
8270 // Send a touch event
8271 processPosition(mapper, 100, 100);
8272 processSync(mapper);
8273
8274 NotifyMotionArgs args;
8275 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8276 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
8277}
8278
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008279TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShouldHandleDisplayId) {
Garfield Tan888a6a42020-01-09 11:39:16 -08008280 // Setup for second display.
Michael Wright17db18e2020-06-26 20:51:44 +01008281 std::shared_ptr<FakePointerController> fakePointerController =
8282 std::make_shared<FakePointerController>();
Garfield Tan888a6a42020-01-09 11:39:16 -08008283 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008284 fakePointerController->setPosition(100, 200);
8285 fakePointerController->setButtonState(0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00008286 mFakePolicy->setPointerController(fakePointerController);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008287
Garfield Tan888a6a42020-01-09 11:39:16 -08008288 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008289 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08008290
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008291 prepareDisplay(DISPLAY_ORIENTATION_0);
8292 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008293 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008294
8295 // Check source is mouse that would obtain the PointerController.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008296 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008297
8298 NotifyMotionArgs motionArgs;
8299 processPosition(mapper, 100, 100);
8300 processSync(mapper);
8301
8302 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8303 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8304 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
8305}
8306
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00008307/**
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008308 * Ensure that the readTime is set to the SYN_REPORT value when processing touch events.
8309 */
8310TEST_F(MultiTouchInputMapperTest, Process_SendsReadTime) {
8311 addConfigurationProperty("touch.deviceType", "touchScreen");
8312 prepareAxes(POSITION);
8313 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8314
8315 prepareDisplay(DISPLAY_ORIENTATION_0);
8316 process(mapper, 10, 11 /*readTime*/, EV_ABS, ABS_MT_TRACKING_ID, 1);
8317 process(mapper, 15, 16 /*readTime*/, EV_ABS, ABS_MT_POSITION_X, 100);
8318 process(mapper, 20, 21 /*readTime*/, EV_ABS, ABS_MT_POSITION_Y, 100);
8319 process(mapper, 25, 26 /*readTime*/, EV_SYN, SYN_REPORT, 0);
8320
8321 NotifyMotionArgs args;
8322 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8323 ASSERT_EQ(26, args.readTime);
8324
8325 process(mapper, 30, 31 /*readTime*/, EV_ABS, ABS_MT_POSITION_X, 110);
8326 process(mapper, 30, 32 /*readTime*/, EV_ABS, ABS_MT_POSITION_Y, 220);
8327 process(mapper, 30, 33 /*readTime*/, EV_SYN, SYN_REPORT, 0);
8328
8329 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8330 ASSERT_EQ(33, args.readTime);
8331}
8332
8333/**
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00008334 * When the viewport is not active (isActive=false), the touch mapper should be disabled and the
8335 * events should not be delivered to the listener.
8336 */
8337TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreDropped) {
8338 addConfigurationProperty("touch.deviceType", "touchScreen");
8339 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
8340 DISPLAY_ORIENTATION_0, false /*isActive*/, UNIQUE_ID, NO_PORT,
8341 ViewportType::INTERNAL);
8342 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8343 prepareAxes(POSITION);
8344 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8345
8346 NotifyMotionArgs motionArgs;
8347 processPosition(mapper, 100, 100);
8348 processSync(mapper);
8349
8350 mFakeListener->assertNotifyMotionWasNotCalled();
8351}
8352
Garfield Tanc734e4f2021-01-15 20:01:39 -08008353TEST_F(MultiTouchInputMapperTest, Process_DeactivateViewport_AbortTouches) {
8354 addConfigurationProperty("touch.deviceType", "touchScreen");
8355 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
8356 DISPLAY_ORIENTATION_0, true /*isActive*/, UNIQUE_ID, NO_PORT,
8357 ViewportType::INTERNAL);
8358 std::optional<DisplayViewport> optionalDisplayViewport =
8359 mFakePolicy->getDisplayViewportByUniqueId(UNIQUE_ID);
8360 ASSERT_TRUE(optionalDisplayViewport.has_value());
8361 DisplayViewport displayViewport = *optionalDisplayViewport;
8362
8363 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8364 prepareAxes(POSITION);
8365 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8366
8367 // Finger down
8368 int32_t x = 100, y = 100;
8369 processPosition(mapper, x, y);
8370 processSync(mapper);
8371
8372 NotifyMotionArgs motionArgs;
8373 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8374 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8375
8376 // Deactivate display viewport
8377 displayViewport.isActive = false;
8378 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
8379 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8380
8381 // Finger move
8382 x += 10, y += 10;
8383 processPosition(mapper, x, y);
8384 processSync(mapper);
8385
8386 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8387 EXPECT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
8388
8389 // Reactivate display viewport
8390 displayViewport.isActive = true;
8391 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
8392 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8393
8394 // Finger move again
8395 x += 10, y += 10;
8396 processPosition(mapper, x, y);
8397 processSync(mapper);
8398
8399 // Gesture is aborted, so events after display is activated won't be dispatched until there is
8400 // no pointer on the touch device.
8401 mFakeListener->assertNotifyMotionWasNotCalled();
8402}
8403
Arthur Hung7c645402019-01-25 17:45:42 +08008404TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShowTouches) {
8405 // Setup the first touch screen device.
Arthur Hung7c645402019-01-25 17:45:42 +08008406 prepareAxes(POSITION | ID | SLOT);
8407 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008408 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung7c645402019-01-25 17:45:42 +08008409
8410 // Create the second touch screen device, and enable multi fingers.
8411 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08008412 const std::string DEVICE_NAME2 = "TOUCHSCREEN2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08008413 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008414 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08008415 std::shared_ptr<InputDevice> device2 =
8416 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07008417 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08008418
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008419 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
8420 0 /*flat*/, 0 /*fuzz*/);
8421 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
8422 0 /*flat*/, 0 /*fuzz*/);
8423 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX,
8424 0 /*flat*/, 0 /*fuzz*/);
8425 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX,
8426 0 /*flat*/, 0 /*fuzz*/);
8427 mFakeEventHub->setAbsoluteAxisValue(SECOND_EVENTHUB_ID, ABS_MT_SLOT, 0 /*value*/);
8428 mFakeEventHub->addConfigurationProperty(SECOND_EVENTHUB_ID, String8("touch.deviceType"),
8429 String8("touchScreen"));
Arthur Hung7c645402019-01-25 17:45:42 +08008430
8431 // Setup the second touch screen device.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008432 MultiTouchInputMapper& mapper2 = device2->addMapper<MultiTouchInputMapper>(SECOND_EVENTHUB_ID);
Arthur Hung7c645402019-01-25 17:45:42 +08008433 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
8434 device2->reset(ARBITRARY_TIME);
8435
8436 // Setup PointerController.
Michael Wright17db18e2020-06-26 20:51:44 +01008437 std::shared_ptr<FakePointerController> fakePointerController =
8438 std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00008439 mFakePolicy->setPointerController(fakePointerController);
Arthur Hung7c645402019-01-25 17:45:42 +08008440
8441 // Setup policy for associated displays and show touches.
8442 const uint8_t hdmi1 = 0;
8443 const uint8_t hdmi2 = 1;
8444 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
8445 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
8446 mFakePolicy->setShowTouches(true);
8447
8448 // Create displays.
8449 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008450 prepareSecondaryDisplay(ViewportType::EXTERNAL, hdmi2);
Arthur Hung7c645402019-01-25 17:45:42 +08008451
8452 // Default device will reconfigure above, need additional reconfiguration for another device.
8453 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008454 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung7c645402019-01-25 17:45:42 +08008455
8456 // Two fingers down at default display.
8457 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
8458 processPosition(mapper, x1, y1);
8459 processId(mapper, 1);
8460 processSlot(mapper, 1);
8461 processPosition(mapper, x2, y2);
8462 processId(mapper, 2);
8463 processSync(mapper);
8464
8465 std::map<int32_t, std::vector<int32_t>>::const_iterator iter =
8466 fakePointerController->getSpots().find(DISPLAY_ID);
8467 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
8468 ASSERT_EQ(size_t(2), iter->second.size());
8469
8470 // Two fingers down at second display.
8471 processPosition(mapper2, x1, y1);
8472 processId(mapper2, 1);
8473 processSlot(mapper2, 1);
8474 processPosition(mapper2, x2, y2);
8475 processId(mapper2, 2);
8476 processSync(mapper2);
8477
8478 iter = fakePointerController->getSpots().find(SECONDARY_DISPLAY_ID);
8479 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
8480 ASSERT_EQ(size_t(2), iter->second.size());
8481}
8482
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008483TEST_F(MultiTouchInputMapperTest, VideoFrames_ReceivedByListener) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008484 prepareAxes(POSITION);
8485 addConfigurationProperty("touch.deviceType", "touchScreen");
8486 prepareDisplay(DISPLAY_ORIENTATION_0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008487 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008488
8489 NotifyMotionArgs motionArgs;
8490 // Unrotated video frame
8491 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8492 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008493 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008494 processPosition(mapper, 100, 200);
8495 processSync(mapper);
8496 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8497 ASSERT_EQ(frames, motionArgs.videoFrames);
8498
8499 // Subsequent touch events should not have any videoframes
8500 // This is implemented separately in FakeEventHub,
8501 // but that should match the behaviour of TouchVideoDevice.
8502 processPosition(mapper, 200, 200);
8503 processSync(mapper);
8504 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8505 ASSERT_EQ(std::vector<TouchVideoFrame>(), motionArgs.videoFrames);
8506}
8507
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008508TEST_F(MultiTouchInputMapperTest, VideoFrames_AreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008509 prepareAxes(POSITION);
8510 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008511 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008512 // Unrotated video frame
8513 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8514 NotifyMotionArgs motionArgs;
8515
8516 // Test all 4 orientations
8517 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008518 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
8519 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
8520 clearViewports();
8521 prepareDisplay(orientation);
8522 std::vector<TouchVideoFrame> frames{frame};
8523 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
8524 processPosition(mapper, 100, 200);
8525 processSync(mapper);
8526 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8527 ASSERT_EQ(frames, motionArgs.videoFrames);
8528 }
8529}
8530
8531TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_AreRotated) {
8532 prepareAxes(POSITION);
8533 addConfigurationProperty("touch.deviceType", "touchScreen");
8534 // Since InputReader works in the un-rotated coordinate space, only devices that are not
8535 // orientation-aware are affected by display rotation.
8536 addConfigurationProperty("touch.orientationAware", "0");
8537 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8538 // Unrotated video frame
8539 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8540 NotifyMotionArgs motionArgs;
8541
8542 // Test all 4 orientations
8543 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008544 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
8545 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
8546 clearViewports();
8547 prepareDisplay(orientation);
8548 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008549 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008550 processPosition(mapper, 100, 200);
8551 processSync(mapper);
8552 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008553 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
8554 // compared to the display. This is so that when the window transform (which contains the
8555 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
8556 // window's coordinate space.
8557 frames[0].rotate(getInverseRotation(orientation));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008558 ASSERT_EQ(frames, motionArgs.videoFrames);
8559 }
8560}
8561
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008562TEST_F(MultiTouchInputMapperTest, VideoFrames_MultipleFramesAreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008563 prepareAxes(POSITION);
8564 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008565 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008566 // Unrotated video frames. There's no rule that they must all have the same dimensions,
8567 // so mix these.
8568 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8569 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
8570 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
8571 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
8572 NotifyMotionArgs motionArgs;
8573
8574 prepareDisplay(DISPLAY_ORIENTATION_90);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008575 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008576 processPosition(mapper, 100, 200);
8577 processSync(mapper);
8578 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008579 ASSERT_EQ(frames, motionArgs.videoFrames);
8580}
8581
8582TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_MultipleFramesAreRotated) {
8583 prepareAxes(POSITION);
8584 addConfigurationProperty("touch.deviceType", "touchScreen");
8585 // Since InputReader works in the un-rotated coordinate space, only devices that are not
8586 // orientation-aware are affected by display rotation.
8587 addConfigurationProperty("touch.orientationAware", "0");
8588 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8589 // Unrotated video frames. There's no rule that they must all have the same dimensions,
8590 // so mix these.
8591 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8592 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
8593 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
8594 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
8595 NotifyMotionArgs motionArgs;
8596
8597 prepareDisplay(DISPLAY_ORIENTATION_90);
8598 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
8599 processPosition(mapper, 100, 200);
8600 processSync(mapper);
8601 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8602 std::for_each(frames.begin(), frames.end(), [](TouchVideoFrame& frame) {
8603 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
8604 // compared to the display. This is so that when the window transform (which contains the
8605 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
8606 // window's coordinate space.
8607 frame.rotate(getInverseRotation(DISPLAY_ORIENTATION_90));
8608 });
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008609 ASSERT_EQ(frames, motionArgs.videoFrames);
8610}
8611
Arthur Hung9da14732019-09-02 16:16:58 +08008612/**
8613 * If we had defined port associations, but the viewport is not ready, the touch device would be
8614 * expected to be disabled, and it should be enabled after the viewport has found.
8615 */
8616TEST_F(MultiTouchInputMapperTest, Configure_EnabledForAssociatedDisplay) {
Arthur Hung9da14732019-09-02 16:16:58 +08008617 constexpr uint8_t hdmi2 = 1;
8618 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008619 constexpr ViewportType type = ViewportType::EXTERNAL;
Arthur Hung9da14732019-09-02 16:16:58 +08008620
8621 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi2);
8622
8623 addConfigurationProperty("touch.deviceType", "touchScreen");
8624 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008625 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung9da14732019-09-02 16:16:58 +08008626
8627 ASSERT_EQ(mDevice->isEnabled(), false);
8628
8629 // Add display on hdmi2, the device should be enabled and can receive touch event.
8630 prepareSecondaryDisplay(type, hdmi2);
8631 ASSERT_EQ(mDevice->isEnabled(), true);
8632
8633 // Send a touch event.
8634 processPosition(mapper, 100, 100);
8635 processSync(mapper);
8636
8637 NotifyMotionArgs args;
8638 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8639 ASSERT_EQ(SECONDARY_DISPLAY_ID, args.displayId);
8640}
8641
Arthur Hung421eb1c2020-01-16 00:09:42 +08008642TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleSingleTouch) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08008643 addConfigurationProperty("touch.deviceType", "touchScreen");
8644 prepareDisplay(DISPLAY_ORIENTATION_0);
8645 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008646 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08008647
8648 NotifyMotionArgs motionArgs;
8649
8650 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
8651 // finger down
8652 processId(mapper, 1);
8653 processPosition(mapper, x1, y1);
8654 processSync(mapper);
8655 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8656 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8657 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8658
8659 // finger move
8660 processId(mapper, 1);
8661 processPosition(mapper, x2, y2);
8662 processSync(mapper);
8663 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8664 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8665 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8666
8667 // finger up.
8668 processId(mapper, -1);
8669 processSync(mapper);
8670 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8671 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8672 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8673
8674 // new finger down
8675 processId(mapper, 1);
8676 processPosition(mapper, x3, y3);
8677 processSync(mapper);
8678 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8679 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8680 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8681}
8682
8683/**
arthurhungcc7f9802020-04-30 17:55:40 +08008684 * Test single touch should be canceled when received the MT_TOOL_PALM event, and the following
8685 * MOVE and UP events should be ignored.
Arthur Hung421eb1c2020-01-16 00:09:42 +08008686 */
arthurhungcc7f9802020-04-30 17:55:40 +08008687TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_SinglePointer) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08008688 addConfigurationProperty("touch.deviceType", "touchScreen");
8689 prepareDisplay(DISPLAY_ORIENTATION_0);
8690 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008691 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08008692
8693 NotifyMotionArgs motionArgs;
8694
8695 // default tool type is finger
8696 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
arthurhungcc7f9802020-04-30 17:55:40 +08008697 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008698 processPosition(mapper, x1, y1);
8699 processSync(mapper);
8700 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8701 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8702 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8703
8704 // Tool changed to MT_TOOL_PALM expect sending the cancel event.
8705 processToolType(mapper, MT_TOOL_PALM);
8706 processSync(mapper);
8707 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8708 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
8709
8710 // Ignore the following MOVE and UP events if had detect a palm event.
arthurhungcc7f9802020-04-30 17:55:40 +08008711 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008712 processPosition(mapper, x2, y2);
8713 processSync(mapper);
8714 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8715
8716 // finger up.
arthurhungcc7f9802020-04-30 17:55:40 +08008717 processId(mapper, INVALID_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008718 processSync(mapper);
8719 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8720
8721 // new finger down
arthurhungcc7f9802020-04-30 17:55:40 +08008722 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008723 processToolType(mapper, MT_TOOL_FINGER);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008724 processPosition(mapper, x3, y3);
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
arthurhungbf89a482020-04-17 17:37:55 +08008731/**
arthurhungcc7f9802020-04-30 17:55:40 +08008732 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
8733 * and the rest active fingers could still be allowed to receive the events
arthurhungbf89a482020-04-17 17:37:55 +08008734 */
arthurhungcc7f9802020-04-30 17:55:40 +08008735TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_TwoPointers) {
arthurhungbf89a482020-04-17 17:37:55 +08008736 addConfigurationProperty("touch.deviceType", "touchScreen");
8737 prepareDisplay(DISPLAY_ORIENTATION_0);
8738 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
8739 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8740
8741 NotifyMotionArgs motionArgs;
8742
8743 // default tool type is finger
arthurhungcc7f9802020-04-30 17:55:40 +08008744 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
8745 processId(mapper, FIRST_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08008746 processPosition(mapper, x1, y1);
8747 processSync(mapper);
8748 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8749 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8750 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8751
8752 // Second finger down.
arthurhungcc7f9802020-04-30 17:55:40 +08008753 processSlot(mapper, SECOND_SLOT);
8754 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08008755 processPosition(mapper, x2, y2);
arthurhungcc7f9802020-04-30 17:55:40 +08008756 processSync(mapper);
8757 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008758 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08008759 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8760
8761 // If the tool type of the first finger changes to MT_TOOL_PALM,
8762 // we expect to receive ACTION_POINTER_UP with cancel flag.
8763 processSlot(mapper, FIRST_SLOT);
8764 processId(mapper, FIRST_TRACKING_ID);
8765 processToolType(mapper, MT_TOOL_PALM);
8766 processSync(mapper);
8767 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008768 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08008769 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
8770
8771 // The following MOVE events of second finger should be processed.
8772 processSlot(mapper, SECOND_SLOT);
8773 processId(mapper, SECOND_TRACKING_ID);
8774 processPosition(mapper, x2 + 1, y2 + 1);
8775 processSync(mapper);
8776 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8777 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8778 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8779
8780 // First finger up. It used to be in palm mode, and we already generated ACTION_POINTER_UP for
8781 // it. Second finger receive move.
8782 processSlot(mapper, FIRST_SLOT);
8783 processId(mapper, INVALID_TRACKING_ID);
8784 processSync(mapper);
8785 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8786 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8787 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8788
8789 // Second finger keeps moving.
8790 processSlot(mapper, SECOND_SLOT);
8791 processId(mapper, SECOND_TRACKING_ID);
8792 processPosition(mapper, x2 + 2, y2 + 2);
8793 processSync(mapper);
8794 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8795 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8796 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8797
8798 // Second finger up.
8799 processId(mapper, INVALID_TRACKING_ID);
8800 processSync(mapper);
8801 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8802 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8803 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
8804}
8805
8806/**
8807 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event, if only 1 finger
8808 * is active, it should send CANCEL after receiving the MT_TOOL_PALM event.
8809 */
8810TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_ShouldCancelWhenAllTouchIsPalm) {
8811 addConfigurationProperty("touch.deviceType", "touchScreen");
8812 prepareDisplay(DISPLAY_ORIENTATION_0);
8813 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
8814 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8815
8816 NotifyMotionArgs motionArgs;
8817
8818 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
8819 // First finger down.
8820 processId(mapper, FIRST_TRACKING_ID);
8821 processPosition(mapper, x1, y1);
8822 processSync(mapper);
8823 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8824 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8825 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8826
8827 // Second finger down.
8828 processSlot(mapper, SECOND_SLOT);
8829 processId(mapper, SECOND_TRACKING_ID);
8830 processPosition(mapper, x2, y2);
arthurhungbf89a482020-04-17 17:37:55 +08008831 processSync(mapper);
8832 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008833 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungbf89a482020-04-17 17:37:55 +08008834 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8835
arthurhungcc7f9802020-04-30 17:55:40 +08008836 // If the tool type of the first finger changes to MT_TOOL_PALM,
8837 // we expect to receive ACTION_POINTER_UP with cancel flag.
8838 processSlot(mapper, FIRST_SLOT);
8839 processId(mapper, FIRST_TRACKING_ID);
8840 processToolType(mapper, MT_TOOL_PALM);
8841 processSync(mapper);
8842 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008843 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08008844 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
8845
8846 // Second finger keeps moving.
8847 processSlot(mapper, SECOND_SLOT);
8848 processId(mapper, SECOND_TRACKING_ID);
8849 processPosition(mapper, x2 + 1, y2 + 1);
8850 processSync(mapper);
8851 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8852 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8853
8854 // second finger becomes palm, receive cancel due to only 1 finger is active.
8855 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08008856 processToolType(mapper, MT_TOOL_PALM);
8857 processSync(mapper);
8858 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8859 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
8860
arthurhungcc7f9802020-04-30 17:55:40 +08008861 // third finger down.
8862 processSlot(mapper, THIRD_SLOT);
8863 processId(mapper, THIRD_TRACKING_ID);
8864 processToolType(mapper, MT_TOOL_FINGER);
arthurhungbf89a482020-04-17 17:37:55 +08008865 processPosition(mapper, x3, y3);
8866 processSync(mapper);
arthurhungbf89a482020-04-17 17:37:55 +08008867 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8868 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8869 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08008870 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8871
8872 // third finger move
8873 processId(mapper, THIRD_TRACKING_ID);
8874 processPosition(mapper, x3 + 1, y3 + 1);
8875 processSync(mapper);
8876 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8877 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8878
8879 // first finger up, third finger receive move.
8880 processSlot(mapper, FIRST_SLOT);
8881 processId(mapper, INVALID_TRACKING_ID);
8882 processSync(mapper);
8883 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8884 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8885 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8886
8887 // second finger up, third finger receive move.
8888 processSlot(mapper, SECOND_SLOT);
8889 processId(mapper, INVALID_TRACKING_ID);
8890 processSync(mapper);
8891 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8892 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8893 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8894
8895 // third finger up.
8896 processSlot(mapper, THIRD_SLOT);
8897 processId(mapper, INVALID_TRACKING_ID);
8898 processSync(mapper);
8899 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8900 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8901 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
8902}
8903
8904/**
8905 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
8906 * and the active finger could still be allowed to receive the events
8907 */
8908TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_KeepFirstPointer) {
8909 addConfigurationProperty("touch.deviceType", "touchScreen");
8910 prepareDisplay(DISPLAY_ORIENTATION_0);
8911 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
8912 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8913
8914 NotifyMotionArgs motionArgs;
8915
8916 // default tool type is finger
8917 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
8918 processId(mapper, FIRST_TRACKING_ID);
8919 processPosition(mapper, x1, y1);
8920 processSync(mapper);
8921 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8922 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8923 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8924
8925 // Second finger down.
8926 processSlot(mapper, SECOND_SLOT);
8927 processId(mapper, SECOND_TRACKING_ID);
8928 processPosition(mapper, x2, y2);
8929 processSync(mapper);
8930 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008931 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08008932 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8933
8934 // If the tool type of the second finger changes to MT_TOOL_PALM,
8935 // we expect to receive ACTION_POINTER_UP with cancel flag.
8936 processId(mapper, SECOND_TRACKING_ID);
8937 processToolType(mapper, MT_TOOL_PALM);
8938 processSync(mapper);
8939 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008940 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08008941 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
8942
8943 // The following MOVE event should be processed.
8944 processSlot(mapper, FIRST_SLOT);
8945 processId(mapper, FIRST_TRACKING_ID);
8946 processPosition(mapper, x1 + 1, y1 + 1);
8947 processSync(mapper);
8948 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8949 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8950 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8951
8952 // second finger up.
8953 processSlot(mapper, SECOND_SLOT);
8954 processId(mapper, INVALID_TRACKING_ID);
8955 processSync(mapper);
8956 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8957 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8958
8959 // first finger keep moving
8960 processSlot(mapper, FIRST_SLOT);
8961 processId(mapper, FIRST_TRACKING_ID);
8962 processPosition(mapper, x1 + 2, y1 + 2);
8963 processSync(mapper);
8964 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8965 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8966
8967 // first finger up.
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);
arthurhungbf89a482020-04-17 17:37:55 +08008973}
8974
Arthur Hung9ad18942021-06-19 02:04:46 +00008975/**
8976 * Test multi-touch should sent ACTION_POINTER_UP/ACTION_UP when received the INVALID_TRACKING_ID,
8977 * to prevent the driver side may send unexpected data after set tracking id as INVALID_TRACKING_ID
8978 * cause slot be valid again.
8979 */
8980TEST_F(MultiTouchInputMapperTest, Process_MultiTouch_WithInvalidTrackingId) {
8981 addConfigurationProperty("touch.deviceType", "touchScreen");
8982 prepareDisplay(DISPLAY_ORIENTATION_0);
8983 prepareAxes(POSITION | ID | SLOT | PRESSURE);
8984 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8985
8986 NotifyMotionArgs motionArgs;
8987
8988 constexpr int32_t x1 = 100, y1 = 200, x2 = 0, y2 = 0;
8989 // First finger down.
8990 processId(mapper, FIRST_TRACKING_ID);
8991 processPosition(mapper, x1, y1);
8992 processPressure(mapper, RAW_PRESSURE_MAX);
8993 processSync(mapper);
8994 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8995 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8996 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8997
8998 // First finger move.
8999 processId(mapper, FIRST_TRACKING_ID);
9000 processPosition(mapper, x1 + 1, y1 + 1);
9001 processPressure(mapper, RAW_PRESSURE_MAX);
9002 processSync(mapper);
9003 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9004 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9005 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9006
9007 // Second finger down.
9008 processSlot(mapper, SECOND_SLOT);
9009 processId(mapper, SECOND_TRACKING_ID);
9010 processPosition(mapper, x2, y2);
9011 processPressure(mapper, RAW_PRESSURE_MAX);
9012 processSync(mapper);
9013 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009014 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Arthur Hung9ad18942021-06-19 02:04:46 +00009015 ASSERT_EQ(uint32_t(2), motionArgs.pointerCount);
9016
9017 // second finger up with some unexpected data.
9018 processSlot(mapper, SECOND_SLOT);
9019 processId(mapper, INVALID_TRACKING_ID);
9020 processPosition(mapper, x2, y2);
9021 processSync(mapper);
9022 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009023 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Arthur Hung9ad18942021-06-19 02:04:46 +00009024 ASSERT_EQ(uint32_t(2), motionArgs.pointerCount);
9025
9026 // first finger up with some unexpected data.
9027 processSlot(mapper, FIRST_SLOT);
9028 processId(mapper, INVALID_TRACKING_ID);
9029 processPosition(mapper, x2, y2);
9030 processPressure(mapper, RAW_PRESSURE_MAX);
9031 processSync(mapper);
9032 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9033 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9034 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9035}
9036
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009037// --- MultiTouchInputMapperTest_ExternalDevice ---
9038
9039class MultiTouchInputMapperTest_ExternalDevice : public MultiTouchInputMapperTest {
9040protected:
Chris Yea52ade12020-08-27 16:49:20 -07009041 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009042};
9043
9044/**
9045 * Expect fallback to internal viewport if device is external and external viewport is not present.
9046 */
9047TEST_F(MultiTouchInputMapperTest_ExternalDevice, Viewports_Fallback) {
9048 prepareAxes(POSITION);
9049 addConfigurationProperty("touch.deviceType", "touchScreen");
9050 prepareDisplay(DISPLAY_ORIENTATION_0);
9051 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9052
9053 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
9054
9055 NotifyMotionArgs motionArgs;
9056
9057 // Expect the event to be sent to the internal viewport,
9058 // because an external viewport is not present.
9059 processPosition(mapper, 100, 100);
9060 processSync(mapper);
9061 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9062 ASSERT_EQ(ADISPLAY_ID_DEFAULT, motionArgs.displayId);
9063
9064 // Expect the event to be sent to the external viewport if it is present.
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009065 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009066 processPosition(mapper, 100, 100);
9067 processSync(mapper);
9068 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9069 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
9070}
Arthur Hung4197f6b2020-03-16 15:39:59 +08009071
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009072TEST_F(MultiTouchInputMapperTest, Process_TouchpadCapture) {
9073 // we need a pointer controller for mouse mode of touchpad (start pointer at 0,0)
9074 std::shared_ptr<FakePointerController> fakePointerController =
9075 std::make_shared<FakePointerController>();
9076 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
9077 fakePointerController->setPosition(0, 0);
9078 fakePointerController->setButtonState(0);
9079
9080 // prepare device and capture
9081 prepareDisplay(DISPLAY_ORIENTATION_0);
9082 prepareAxes(POSITION | ID | SLOT);
9083 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
9084 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
9085 mFakePolicy->setPointerCapture(true);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009086 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009087 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9088
9089 // captured touchpad should be a touchpad source
9090 NotifyDeviceResetArgs resetArgs;
9091 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
9092 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
9093
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009094 InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
Chris Yef74dc422020-09-02 22:41:50 -07009095
9096 const InputDeviceInfo::MotionRange* relRangeX =
9097 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_X, AINPUT_SOURCE_TOUCHPAD);
9098 ASSERT_NE(relRangeX, nullptr);
9099 ASSERT_EQ(relRangeX->min, -(RAW_X_MAX - RAW_X_MIN));
9100 ASSERT_EQ(relRangeX->max, RAW_X_MAX - RAW_X_MIN);
9101 const InputDeviceInfo::MotionRange* relRangeY =
9102 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_Y, AINPUT_SOURCE_TOUCHPAD);
9103 ASSERT_NE(relRangeY, nullptr);
9104 ASSERT_EQ(relRangeY->min, -(RAW_Y_MAX - RAW_Y_MIN));
9105 ASSERT_EQ(relRangeY->max, RAW_Y_MAX - RAW_Y_MIN);
9106
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009107 // run captured pointer tests - note that this is unscaled, so input listener events should be
9108 // identical to what the hardware sends (accounting for any
9109 // calibration).
9110 // FINGER 0 DOWN
Chris Ye364fdb52020-08-05 15:07:56 -07009111 processSlot(mapper, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009112 processId(mapper, 1);
9113 processPosition(mapper, 100 + RAW_X_MIN, 100 + RAW_Y_MIN);
9114 processKey(mapper, BTN_TOUCH, 1);
9115 processSync(mapper);
9116
9117 // expect coord[0] to contain initial location of touch 0
9118 NotifyMotionArgs args;
9119 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9120 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
9121 ASSERT_EQ(1U, args.pointerCount);
9122 ASSERT_EQ(0, args.pointerProperties[0].id);
9123 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, args.source);
9124 ASSERT_NO_FATAL_FAILURE(
9125 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
9126
9127 // FINGER 1 DOWN
9128 processSlot(mapper, 1);
9129 processId(mapper, 2);
9130 processPosition(mapper, 560 + RAW_X_MIN, 154 + RAW_Y_MIN);
9131 processSync(mapper);
9132
9133 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
9134 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009135 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009136 ASSERT_EQ(2U, args.pointerCount);
9137 ASSERT_EQ(0, args.pointerProperties[0].id);
9138 ASSERT_EQ(1, args.pointerProperties[1].id);
9139 ASSERT_NO_FATAL_FAILURE(
9140 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
9141 ASSERT_NO_FATAL_FAILURE(
9142 assertPointerCoords(args.pointerCoords[1], 560, 154, 1, 0, 0, 0, 0, 0, 0, 0));
9143
9144 // FINGER 1 MOVE
9145 processPosition(mapper, 540 + RAW_X_MIN, 690 + RAW_Y_MIN);
9146 processSync(mapper);
9147
9148 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
9149 // from move
9150 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9151 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9152 ASSERT_NO_FATAL_FAILURE(
9153 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
9154 ASSERT_NO_FATAL_FAILURE(
9155 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
9156
9157 // FINGER 0 MOVE
9158 processSlot(mapper, 0);
9159 processPosition(mapper, 50 + RAW_X_MIN, 800 + RAW_Y_MIN);
9160 processSync(mapper);
9161
9162 // expect coord[0] to contain new touch 0 location, coord[1] to contain previous location
9163 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9164 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9165 ASSERT_NO_FATAL_FAILURE(
9166 assertPointerCoords(args.pointerCoords[0], 50, 800, 1, 0, 0, 0, 0, 0, 0, 0));
9167 ASSERT_NO_FATAL_FAILURE(
9168 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
9169
9170 // BUTTON DOWN
9171 processKey(mapper, BTN_LEFT, 1);
9172 processSync(mapper);
9173
9174 // touchinputmapper design sends a move before button press
9175 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9176 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9177 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9178 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
9179
9180 // BUTTON UP
9181 processKey(mapper, BTN_LEFT, 0);
9182 processSync(mapper);
9183
9184 // touchinputmapper design sends a move after button release
9185 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9186 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
9187 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9188 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9189
9190 // FINGER 0 UP
9191 processId(mapper, -1);
9192 processSync(mapper);
9193 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9194 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | 0x0000, args.action);
9195
9196 // FINGER 1 MOVE
9197 processSlot(mapper, 1);
9198 processPosition(mapper, 320 + RAW_X_MIN, 900 + RAW_Y_MIN);
9199 processSync(mapper);
9200
9201 // expect coord[0] to contain new location of touch 1, and properties[0].id to contain 1
9202 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9203 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9204 ASSERT_EQ(1U, args.pointerCount);
9205 ASSERT_EQ(1, args.pointerProperties[0].id);
9206 ASSERT_NO_FATAL_FAILURE(
9207 assertPointerCoords(args.pointerCoords[0], 320, 900, 1, 0, 0, 0, 0, 0, 0, 0));
9208
9209 // FINGER 1 UP
9210 processId(mapper, -1);
9211 processKey(mapper, BTN_TOUCH, 0);
9212 processSync(mapper);
9213 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9214 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
9215
9216 // non captured touchpad should be a mouse source
9217 mFakePolicy->setPointerCapture(false);
9218 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
9219 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
9220 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
9221}
9222
9223TEST_F(MultiTouchInputMapperTest, Process_UnCapturedTouchpadPointer) {
9224 std::shared_ptr<FakePointerController> fakePointerController =
9225 std::make_shared<FakePointerController>();
9226 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
9227 fakePointerController->setPosition(0, 0);
9228 fakePointerController->setButtonState(0);
9229
9230 // prepare device and capture
9231 prepareDisplay(DISPLAY_ORIENTATION_0);
9232 prepareAxes(POSITION | ID | SLOT);
9233 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
9234 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009235 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009236 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9237 // run uncaptured pointer tests - pushes out generic events
9238 // FINGER 0 DOWN
9239 processId(mapper, 3);
9240 processPosition(mapper, 100, 100);
9241 processKey(mapper, BTN_TOUCH, 1);
9242 processSync(mapper);
9243
9244 // start at (100,100), cursor should be at (0,0) * scale
9245 NotifyMotionArgs args;
9246 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9247 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
9248 ASSERT_NO_FATAL_FAILURE(
9249 assertPointerCoords(args.pointerCoords[0], 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
9250
9251 // FINGER 0 MOVE
9252 processPosition(mapper, 200, 200);
9253 processSync(mapper);
9254
9255 // compute scaling to help with touch position checking
9256 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
9257 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
9258 float scale =
9259 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
9260
9261 // translate from (100,100) -> (200,200), cursor should have changed to (100,100) * scale)
9262 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9263 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
9264 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 100 * scale, 100 * scale, 0,
9265 0, 0, 0, 0, 0, 0, 0));
9266}
9267
9268TEST_F(MultiTouchInputMapperTest, WhenCapturedAndNotCaptured_GetSources) {
9269 std::shared_ptr<FakePointerController> fakePointerController =
9270 std::make_shared<FakePointerController>();
9271
9272 prepareDisplay(DISPLAY_ORIENTATION_0);
9273 prepareAxes(POSITION | ID | SLOT);
9274 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009275 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009276 mFakePolicy->setPointerCapture(false);
9277 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9278
9279 // uncaptured touchpad should be a pointer device
9280 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
9281
9282 // captured touchpad should be a touchpad device
9283 mFakePolicy->setPointerCapture(true);
9284 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
9285 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
9286}
9287
Arthur Hung6d5b4b22022-01-21 07:21:10 +00009288// --- JoystickInputMapperTest ---
9289
9290class JoystickInputMapperTest : public InputMapperTest {
9291protected:
9292 static const int32_t RAW_X_MIN;
9293 static const int32_t RAW_X_MAX;
9294 static const int32_t RAW_Y_MIN;
9295 static const int32_t RAW_Y_MAX;
9296
9297 void SetUp() override {
9298 InputMapperTest::SetUp(InputDeviceClass::JOYSTICK | InputDeviceClass::EXTERNAL);
9299 }
9300 void prepareAxes() {
9301 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
9302 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
9303 }
9304
9305 void processAxis(JoystickInputMapper& mapper, int32_t axis, int32_t value) {
9306 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, axis, value);
9307 }
9308
9309 void processSync(JoystickInputMapper& mapper) {
9310 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
9311 }
9312
9313 void prepareVirtualDisplay(int32_t orientation) {
9314 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH,
9315 VIRTUAL_DISPLAY_HEIGHT, orientation, VIRTUAL_DISPLAY_UNIQUE_ID,
9316 NO_PORT, ViewportType::VIRTUAL);
9317 }
9318};
9319
9320const int32_t JoystickInputMapperTest::RAW_X_MIN = -32767;
9321const int32_t JoystickInputMapperTest::RAW_X_MAX = 32767;
9322const int32_t JoystickInputMapperTest::RAW_Y_MIN = -32767;
9323const int32_t JoystickInputMapperTest::RAW_Y_MAX = 32767;
9324
9325TEST_F(JoystickInputMapperTest, Configure_AssignsDisplayUniqueId) {
9326 prepareAxes();
9327 JoystickInputMapper& mapper = addMapperAndConfigure<JoystickInputMapper>();
9328
9329 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
9330
9331 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
9332
9333 // Send an axis event
9334 processAxis(mapper, ABS_X, 100);
9335 processSync(mapper);
9336
9337 NotifyMotionArgs args;
9338 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9339 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
9340
9341 // Send another axis event
9342 processAxis(mapper, ABS_Y, 100);
9343 processSync(mapper);
9344
9345 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9346 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
9347}
9348
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009349// --- PeripheralControllerTest ---
Chris Yee2b1e5c2021-03-10 22:45:12 -08009350
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009351class PeripheralControllerTest : public testing::Test {
Chris Yee2b1e5c2021-03-10 22:45:12 -08009352protected:
9353 static const char* DEVICE_NAME;
9354 static const char* DEVICE_LOCATION;
9355 static const int32_t DEVICE_ID;
9356 static const int32_t DEVICE_GENERATION;
9357 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07009358 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Chris Yee2b1e5c2021-03-10 22:45:12 -08009359 static const int32_t EVENTHUB_ID;
9360
9361 std::shared_ptr<FakeEventHub> mFakeEventHub;
9362 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07009363 std::unique_ptr<TestInputListener> mFakeListener;
Chris Yee2b1e5c2021-03-10 22:45:12 -08009364 std::unique_ptr<InstrumentedInputReader> mReader;
9365 std::shared_ptr<InputDevice> mDevice;
9366
Dominik Laskowski2f01d772022-03-23 16:01:29 -07009367 virtual void SetUp(ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -08009368 mFakeEventHub = std::make_unique<FakeEventHub>();
9369 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07009370 mFakeListener = std::make_unique<TestInputListener>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009371 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07009372 *mFakeListener);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009373 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
9374 }
9375
9376 void SetUp() override { SetUp(DEVICE_CLASSES); }
9377
9378 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07009379 mFakeListener.reset();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009380 mFakePolicy.clear();
9381 }
9382
9383 void configureDevice(uint32_t changes) {
9384 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
9385 mReader->requestRefreshConfiguration(changes);
9386 mReader->loopOnce();
9387 }
9388 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
9389 }
9390
9391 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
9392 const std::string& location, int32_t eventHubId,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07009393 ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -08009394 InputDeviceIdentifier identifier;
9395 identifier.name = name;
9396 identifier.location = location;
9397 std::shared_ptr<InputDevice> device =
9398 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
9399 identifier);
9400 mReader->pushNextDevice(device);
9401 mFakeEventHub->addDevice(eventHubId, name, classes);
9402 mReader->loopOnce();
9403 return device;
9404 }
9405
9406 template <class T, typename... Args>
9407 T& addControllerAndConfigure(Args... args) {
9408 T& controller = mDevice->addController<T>(EVENTHUB_ID, args...);
9409
9410 return controller;
9411 }
9412};
9413
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009414const char* PeripheralControllerTest::DEVICE_NAME = "device";
9415const char* PeripheralControllerTest::DEVICE_LOCATION = "BLUETOOTH";
9416const int32_t PeripheralControllerTest::DEVICE_ID = END_RESERVED_ID + 1000;
9417const int32_t PeripheralControllerTest::DEVICE_GENERATION = 2;
9418const int32_t PeripheralControllerTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07009419const ftl::Flags<InputDeviceClass> PeripheralControllerTest::DEVICE_CLASSES =
9420 ftl::Flags<InputDeviceClass>(0); // not needed for current tests
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009421const int32_t PeripheralControllerTest::EVENTHUB_ID = 1;
Chris Yee2b1e5c2021-03-10 22:45:12 -08009422
9423// --- BatteryControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009424class BatteryControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -08009425protected:
9426 void SetUp() override {
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009427 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::BATTERY);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009428 }
9429};
9430
9431TEST_F(BatteryControllerTest, GetBatteryCapacity) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009432 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009433
9434 ASSERT_TRUE(controller.getBatteryCapacity(DEFAULT_BATTERY));
9435 ASSERT_EQ(controller.getBatteryCapacity(DEFAULT_BATTERY).value_or(-1), BATTERY_CAPACITY);
9436}
9437
9438TEST_F(BatteryControllerTest, GetBatteryStatus) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009439 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009440
9441 ASSERT_TRUE(controller.getBatteryStatus(DEFAULT_BATTERY));
9442 ASSERT_EQ(controller.getBatteryStatus(DEFAULT_BATTERY).value_or(-1), BATTERY_STATUS);
9443}
9444
9445// --- LightControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009446class LightControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -08009447protected:
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009448 void SetUp() override {
9449 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::LIGHT);
9450 }
Chris Yee2b1e5c2021-03-10 22:45:12 -08009451};
9452
Chris Ye85758332021-05-16 23:05:17 -07009453TEST_F(LightControllerTest, MonoLight) {
9454 RawLightInfo infoMono = {.id = 1,
9455 .name = "Mono",
9456 .maxBrightness = 255,
9457 .flags = InputLightClass::BRIGHTNESS,
9458 .path = ""};
9459 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
Chris Yee2b1e5c2021-03-10 22:45:12 -08009460
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009461 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009462 InputDeviceInfo info;
9463 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009464 std::vector<InputDeviceLightInfo> lights = info.getLights();
9465 ASSERT_EQ(1U, lights.size());
9466 ASSERT_EQ(InputDeviceLightType::MONO, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009467
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009468 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
9469 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009470}
9471
9472TEST_F(LightControllerTest, RGBLight) {
9473 RawLightInfo infoRed = {.id = 1,
9474 .name = "red",
9475 .maxBrightness = 255,
9476 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
9477 .path = ""};
9478 RawLightInfo infoGreen = {.id = 2,
9479 .name = "green",
9480 .maxBrightness = 255,
9481 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
9482 .path = ""};
9483 RawLightInfo infoBlue = {.id = 3,
9484 .name = "blue",
9485 .maxBrightness = 255,
9486 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
9487 .path = ""};
9488 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
9489 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
9490 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
9491
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009492 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009493 InputDeviceInfo info;
9494 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009495 std::vector<InputDeviceLightInfo> lights = info.getLights();
9496 ASSERT_EQ(1U, lights.size());
9497 ASSERT_EQ(InputDeviceLightType::RGB, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009498
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009499 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
9500 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009501}
9502
9503TEST_F(LightControllerTest, MultiColorRGBLight) {
9504 RawLightInfo infoColor = {.id = 1,
9505 .name = "red",
9506 .maxBrightness = 255,
9507 .flags = InputLightClass::BRIGHTNESS |
9508 InputLightClass::MULTI_INTENSITY |
9509 InputLightClass::MULTI_INDEX,
9510 .path = ""};
9511
9512 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
9513
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009514 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009515 InputDeviceInfo info;
9516 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009517 std::vector<InputDeviceLightInfo> lights = info.getLights();
9518 ASSERT_EQ(1U, lights.size());
9519 ASSERT_EQ(InputDeviceLightType::MULTI_COLOR, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009520
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009521 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
9522 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009523}
9524
9525TEST_F(LightControllerTest, PlayerIdLight) {
9526 RawLightInfo info1 = {.id = 1,
9527 .name = "player1",
9528 .maxBrightness = 255,
9529 .flags = InputLightClass::BRIGHTNESS,
9530 .path = ""};
9531 RawLightInfo info2 = {.id = 2,
9532 .name = "player2",
9533 .maxBrightness = 255,
9534 .flags = InputLightClass::BRIGHTNESS,
9535 .path = ""};
9536 RawLightInfo info3 = {.id = 3,
9537 .name = "player3",
9538 .maxBrightness = 255,
9539 .flags = InputLightClass::BRIGHTNESS,
9540 .path = ""};
9541 RawLightInfo info4 = {.id = 4,
9542 .name = "player4",
9543 .maxBrightness = 255,
9544 .flags = InputLightClass::BRIGHTNESS,
9545 .path = ""};
9546 mFakeEventHub->addRawLightInfo(info1.id, std::move(info1));
9547 mFakeEventHub->addRawLightInfo(info2.id, std::move(info2));
9548 mFakeEventHub->addRawLightInfo(info3.id, std::move(info3));
9549 mFakeEventHub->addRawLightInfo(info4.id, std::move(info4));
9550
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009551 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009552 InputDeviceInfo info;
9553 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009554 std::vector<InputDeviceLightInfo> lights = info.getLights();
9555 ASSERT_EQ(1U, lights.size());
9556 ASSERT_EQ(InputDeviceLightType::PLAYER_ID, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009557
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009558 ASSERT_FALSE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
9559 ASSERT_TRUE(controller.setLightPlayerId(lights[0].id, LIGHT_PLAYER_ID));
9560 ASSERT_EQ(controller.getLightPlayerId(lights[0].id).value_or(-1), LIGHT_PLAYER_ID);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009561}
9562
Michael Wrightd02c5b62014-02-10 15:10:22 -08009563} // namespace android