blob: db4669967f9224bf353cbefb728bb274ad5f1c00 [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
Prabir Pradhan197e0862022-07-01 14:28:00 +0000222 void clearSpots() override { mSpotsByDisplay.clear(); }
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
Prabir Pradhanf99d6e72022-04-21 15:28:35 +0000378 void setVelocityControlParams(const VelocityControlParameters& params) {
379 mConfig.pointerVelocityControlParameters = params;
380 mConfig.wheelVelocityControlParameters = params;
381 }
382
Michael Wrightd02c5b62014-02-10 15:10:22 -0800383private:
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000384 uint32_t mNextPointerCaptureSequenceNumber = 0;
385
Chris Yea52ade12020-08-27 16:49:20 -0700386 void getReaderConfiguration(InputReaderConfiguration* outConfig) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800387 *outConfig = mConfig;
388 }
389
Prabir Pradhan2853b7a2021-08-23 14:08:51 +0000390 std::shared_ptr<PointerControllerInterface> obtainPointerController(
391 int32_t /*deviceId*/) override {
392 return mPointerController;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800393 }
394
Chris Yea52ade12020-08-27 16:49:20 -0700395 void notifyInputDevicesChanged(const std::vector<InputDeviceInfo>& inputDevices) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700396 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800397 mInputDevices = inputDevices;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700398 mInputDevicesChanged = true;
399 mDevicesChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800400 }
401
Chris Yea52ade12020-08-27 16:49:20 -0700402 std::shared_ptr<KeyCharacterMap> getKeyboardLayoutOverlay(
403 const InputDeviceIdentifier&) override {
Yi Kong9b14ac62018-07-17 13:48:38 -0700404 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800405 }
406
Chris Yea52ade12020-08-27 16:49:20 -0700407 std::string getDeviceAlias(const InputDeviceIdentifier&) override { return ""; }
Prabir Pradhan1aed8582019-12-30 11:46:51 -0800408
409 void waitForInputDevices(std::function<void(bool)> processDevicesChanged) {
410 std::unique_lock<std::mutex> lock(mLock);
411 base::ScopedLockAssertion assumeLocked(mLock);
412
413 const bool devicesChanged =
414 mDevicesChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
415 return mInputDevicesChanged;
416 });
417 ASSERT_NO_FATAL_FAILURE(processDevicesChanged(devicesChanged));
418 mInputDevicesChanged = false;
419 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800420};
421
Michael Wrightd02c5b62014-02-10 15:10:22 -0800422// --- FakeEventHub ---
423
424class FakeEventHub : public EventHubInterface {
425 struct KeyInfo {
426 int32_t keyCode;
427 uint32_t flags;
428 };
429
Chris Yef59a2f42020-10-16 12:55:26 -0700430 struct SensorInfo {
431 InputDeviceSensorType sensorType;
432 int32_t sensorDataIndex;
433 };
434
Michael Wrightd02c5b62014-02-10 15:10:22 -0800435 struct Device {
436 InputDeviceIdentifier identifier;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700437 ftl::Flags<InputDeviceClass> classes;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800438 PropertyMap configuration;
439 KeyedVector<int, RawAbsoluteAxisInfo> absoluteAxes;
440 KeyedVector<int, bool> relativeAxes;
441 KeyedVector<int32_t, int32_t> keyCodeStates;
442 KeyedVector<int32_t, int32_t> scanCodeStates;
443 KeyedVector<int32_t, int32_t> switchStates;
444 KeyedVector<int32_t, int32_t> absoluteAxisValue;
445 KeyedVector<int32_t, KeyInfo> keysByScanCode;
446 KeyedVector<int32_t, KeyInfo> keysByUsageCode;
447 KeyedVector<int32_t, bool> leds;
Philip Junker4af3b3d2021-12-14 10:36:55 +0100448 // fake mapping which would normally come from keyCharacterMap
449 std::unordered_map<int32_t, int32_t> keyCodeMapping;
Chris Yef59a2f42020-10-16 12:55:26 -0700450 std::unordered_map<int32_t, SensorInfo> sensorsByAbsCode;
451 BitArray<MSC_MAX> mscBitmask;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800452 std::vector<VirtualKeyDefinition> virtualKeys;
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700453 bool enabled;
454
455 status_t enable() {
456 enabled = true;
457 return OK;
458 }
459
460 status_t disable() {
461 enabled = false;
462 return OK;
463 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800464
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700465 explicit Device(ftl::Flags<InputDeviceClass> classes) : classes(classes), enabled(true) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800466 };
467
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700468 std::mutex mLock;
469 std::condition_variable mEventsCondition;
470
Michael Wrightd02c5b62014-02-10 15:10:22 -0800471 KeyedVector<int32_t, Device*> mDevices;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100472 std::vector<std::string> mExcludedDevices;
Siarhei Vishniakou370039c2021-02-04 22:09:01 +0000473 std::vector<RawEvent> mEvents GUARDED_BY(mLock);
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600474 std::unordered_map<int32_t /*deviceId*/, std::vector<TouchVideoFrame>> mVideoFrames;
Chris Ye87143712020-11-10 05:05:58 +0000475 std::vector<int32_t> mVibrators = {0, 1};
Chris Ye3fdbfef2021-01-06 18:45:18 -0800476 std::unordered_map<int32_t, RawLightInfo> mRawLightInfos;
477 // Simulates a device light brightness, from light id to light brightness.
478 std::unordered_map<int32_t /* lightId */, int32_t /* brightness*/> mLightBrightness;
479 // Simulates a device light intensities, from light id to light intensities map.
480 std::unordered_map<int32_t /* lightId */, std::unordered_map<LightColor, int32_t>>
481 mLightIntensities;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800482
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700483public:
Michael Wrightd02c5b62014-02-10 15:10:22 -0800484 virtual ~FakeEventHub() {
485 for (size_t i = 0; i < mDevices.size(); i++) {
486 delete mDevices.valueAt(i);
487 }
488 }
489
Michael Wrightd02c5b62014-02-10 15:10:22 -0800490 FakeEventHub() { }
491
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700492 void addDevice(int32_t deviceId, const std::string& name,
493 ftl::Flags<InputDeviceClass> classes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800494 Device* device = new Device(classes);
495 device->identifier.name = name;
496 mDevices.add(deviceId, device);
497
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000498 enqueueEvent(ARBITRARY_TIME, READ_TIME, deviceId, EventHubInterface::DEVICE_ADDED, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800499 }
500
501 void removeDevice(int32_t deviceId) {
502 delete mDevices.valueFor(deviceId);
503 mDevices.removeItem(deviceId);
504
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000505 enqueueEvent(ARBITRARY_TIME, READ_TIME, deviceId, EventHubInterface::DEVICE_REMOVED, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800506 }
507
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700508 bool isDeviceEnabled(int32_t deviceId) {
509 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700510 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700511 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
512 return false;
513 }
514 return device->enabled;
515 }
516
517 status_t enableDevice(int32_t deviceId) {
518 status_t result;
519 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700520 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700521 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
522 return BAD_VALUE;
523 }
524 if (device->enabled) {
525 ALOGW("Duplicate call to %s, device %" PRId32 " already enabled", __func__, deviceId);
526 return OK;
527 }
528 result = device->enable();
529 return result;
530 }
531
532 status_t disableDevice(int32_t deviceId) {
533 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700534 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700535 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
536 return BAD_VALUE;
537 }
538 if (!device->enabled) {
539 ALOGW("Duplicate call to %s, device %" PRId32 " already disabled", __func__, deviceId);
540 return OK;
541 }
542 return device->disable();
543 }
544
Michael Wrightd02c5b62014-02-10 15:10:22 -0800545 void finishDeviceScan() {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000546 enqueueEvent(ARBITRARY_TIME, READ_TIME, 0, EventHubInterface::FINISHED_DEVICE_SCAN, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800547 }
548
549 void addConfigurationProperty(int32_t deviceId, const String8& key, const String8& value) {
550 Device* device = getDevice(deviceId);
551 device->configuration.addProperty(key, value);
552 }
553
554 void addConfigurationMap(int32_t deviceId, const PropertyMap* configuration) {
555 Device* device = getDevice(deviceId);
556 device->configuration.addAll(configuration);
557 }
558
559 void addAbsoluteAxis(int32_t deviceId, int axis,
560 int32_t minValue, int32_t maxValue, int flat, int fuzz, int resolution = 0) {
561 Device* device = getDevice(deviceId);
562
563 RawAbsoluteAxisInfo info;
564 info.valid = true;
565 info.minValue = minValue;
566 info.maxValue = maxValue;
567 info.flat = flat;
568 info.fuzz = fuzz;
569 info.resolution = resolution;
570 device->absoluteAxes.add(axis, info);
571 }
572
573 void addRelativeAxis(int32_t deviceId, int32_t axis) {
574 Device* device = getDevice(deviceId);
575 device->relativeAxes.add(axis, true);
576 }
577
578 void setKeyCodeState(int32_t deviceId, int32_t keyCode, int32_t state) {
579 Device* device = getDevice(deviceId);
580 device->keyCodeStates.replaceValueFor(keyCode, state);
581 }
582
583 void setScanCodeState(int32_t deviceId, int32_t scanCode, int32_t state) {
584 Device* device = getDevice(deviceId);
585 device->scanCodeStates.replaceValueFor(scanCode, state);
586 }
587
588 void setSwitchState(int32_t deviceId, int32_t switchCode, int32_t state) {
589 Device* device = getDevice(deviceId);
590 device->switchStates.replaceValueFor(switchCode, state);
591 }
592
593 void setAbsoluteAxisValue(int32_t deviceId, int32_t axis, int32_t value) {
594 Device* device = getDevice(deviceId);
595 device->absoluteAxisValue.replaceValueFor(axis, value);
596 }
597
598 void addKey(int32_t deviceId, int32_t scanCode, int32_t usageCode,
599 int32_t keyCode, uint32_t flags) {
600 Device* device = getDevice(deviceId);
601 KeyInfo info;
602 info.keyCode = keyCode;
603 info.flags = flags;
604 if (scanCode) {
605 device->keysByScanCode.add(scanCode, info);
606 }
607 if (usageCode) {
608 device->keysByUsageCode.add(usageCode, info);
609 }
610 }
611
Philip Junker4af3b3d2021-12-14 10:36:55 +0100612 void addKeyCodeMapping(int32_t deviceId, int32_t fromKeyCode, int32_t toKeyCode) {
613 Device* device = getDevice(deviceId);
614 device->keyCodeMapping.insert_or_assign(fromKeyCode, toKeyCode);
615 }
616
Michael Wrightd02c5b62014-02-10 15:10:22 -0800617 void addLed(int32_t deviceId, int32_t led, bool initialState) {
618 Device* device = getDevice(deviceId);
619 device->leds.add(led, initialState);
620 }
621
Chris Yef59a2f42020-10-16 12:55:26 -0700622 void addSensorAxis(int32_t deviceId, int32_t absCode, InputDeviceSensorType sensorType,
623 int32_t sensorDataIndex) {
624 Device* device = getDevice(deviceId);
625 SensorInfo info;
626 info.sensorType = sensorType;
627 info.sensorDataIndex = sensorDataIndex;
628 device->sensorsByAbsCode.emplace(absCode, info);
629 }
630
631 void setMscEvent(int32_t deviceId, int32_t mscEvent) {
632 Device* device = getDevice(deviceId);
633 typename BitArray<MSC_MAX>::Buffer buffer;
634 buffer[mscEvent / 32] = 1 << mscEvent % 32;
635 device->mscBitmask.loadFromBuffer(buffer);
636 }
637
Chris Ye3fdbfef2021-01-06 18:45:18 -0800638 void addRawLightInfo(int32_t rawId, RawLightInfo&& info) {
639 mRawLightInfos.emplace(rawId, std::move(info));
640 }
641
642 void fakeLightBrightness(int32_t rawId, int32_t brightness) {
643 mLightBrightness.emplace(rawId, brightness);
644 }
645
646 void fakeLightIntensities(int32_t rawId,
647 const std::unordered_map<LightColor, int32_t> intensities) {
648 mLightIntensities.emplace(rawId, std::move(intensities));
649 }
650
Michael Wrightd02c5b62014-02-10 15:10:22 -0800651 bool getLedState(int32_t deviceId, int32_t led) {
652 Device* device = getDevice(deviceId);
653 return device->leds.valueFor(led);
654 }
655
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100656 std::vector<std::string>& getExcludedDevices() {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800657 return mExcludedDevices;
658 }
659
660 void addVirtualKeyDefinition(int32_t deviceId, const VirtualKeyDefinition& definition) {
661 Device* device = getDevice(deviceId);
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800662 device->virtualKeys.push_back(definition);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800663 }
664
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000665 void enqueueEvent(nsecs_t when, nsecs_t readTime, int32_t deviceId, int32_t type, int32_t code,
666 int32_t value) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700667 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800668 RawEvent event;
669 event.when = when;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000670 event.readTime = readTime;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800671 event.deviceId = deviceId;
672 event.type = type;
673 event.code = code;
674 event.value = value;
675 mEvents.push_back(event);
676
677 if (type == EV_ABS) {
678 setAbsoluteAxisValue(deviceId, code, value);
679 }
680 }
681
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600682 void setVideoFrames(std::unordered_map<int32_t /*deviceId*/,
683 std::vector<TouchVideoFrame>> videoFrames) {
684 mVideoFrames = std::move(videoFrames);
685 }
686
Michael Wrightd02c5b62014-02-10 15:10:22 -0800687 void assertQueueIsEmpty() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700688 std::unique_lock<std::mutex> lock(mLock);
689 base::ScopedLockAssertion assumeLocked(mLock);
690 const bool queueIsEmpty =
691 mEventsCondition.wait_for(lock, WAIT_TIMEOUT,
692 [this]() REQUIRES(mLock) { return mEvents.size() == 0; });
693 if (!queueIsEmpty) {
694 FAIL() << "Timed out waiting for EventHub queue to be emptied.";
695 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800696 }
697
698private:
699 Device* getDevice(int32_t deviceId) const {
700 ssize_t index = mDevices.indexOfKey(deviceId);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100701 return index >= 0 ? mDevices.valueAt(index) : nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800702 }
703
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700704 ftl::Flags<InputDeviceClass> getDeviceClasses(int32_t deviceId) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800705 Device* device = getDevice(deviceId);
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700706 return device ? device->classes : ftl::Flags<InputDeviceClass>(0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800707 }
708
Chris Yea52ade12020-08-27 16:49:20 -0700709 InputDeviceIdentifier getDeviceIdentifier(int32_t deviceId) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800710 Device* device = getDevice(deviceId);
711 return device ? device->identifier : InputDeviceIdentifier();
712 }
713
Chris Yea52ade12020-08-27 16:49:20 -0700714 int32_t getDeviceControllerNumber(int32_t) const override { return 0; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800715
Chris Yea52ade12020-08-27 16:49:20 -0700716 void getConfiguration(int32_t deviceId, PropertyMap* outConfiguration) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800717 Device* device = getDevice(deviceId);
718 if (device) {
719 *outConfiguration = device->configuration;
720 }
721 }
722
Chris Yea52ade12020-08-27 16:49:20 -0700723 status_t getAbsoluteAxisInfo(int32_t deviceId, int axis,
724 RawAbsoluteAxisInfo* outAxisInfo) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800725 Device* device = getDevice(deviceId);
Arthur Hung9da14732019-09-02 16:16:58 +0800726 if (device && device->enabled) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800727 ssize_t index = device->absoluteAxes.indexOfKey(axis);
728 if (index >= 0) {
729 *outAxisInfo = device->absoluteAxes.valueAt(index);
730 return OK;
731 }
732 }
733 outAxisInfo->clear();
734 return -1;
735 }
736
Chris Yea52ade12020-08-27 16:49:20 -0700737 bool hasRelativeAxis(int32_t deviceId, int axis) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800738 Device* device = getDevice(deviceId);
739 if (device) {
740 return device->relativeAxes.indexOfKey(axis) >= 0;
741 }
742 return false;
743 }
744
Chris Yea52ade12020-08-27 16:49:20 -0700745 bool hasInputProperty(int32_t, int) const override { return false; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800746
Chris Yef59a2f42020-10-16 12:55:26 -0700747 bool hasMscEvent(int32_t deviceId, int mscEvent) const override final {
748 Device* device = getDevice(deviceId);
749 if (device) {
750 return mscEvent >= 0 && mscEvent <= MSC_MAX ? device->mscBitmask.test(mscEvent) : false;
751 }
752 return false;
753 }
754
Chris Yea52ade12020-08-27 16:49:20 -0700755 status_t mapKey(int32_t deviceId, int32_t scanCode, int32_t usageCode, int32_t metaState,
756 int32_t* outKeycode, int32_t* outMetaState, uint32_t* outFlags) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800757 Device* device = getDevice(deviceId);
758 if (device) {
759 const KeyInfo* key = getKey(device, scanCode, usageCode);
760 if (key) {
761 if (outKeycode) {
762 *outKeycode = key->keyCode;
763 }
764 if (outFlags) {
765 *outFlags = key->flags;
766 }
Dmitry Torokhov0faaa0b2015-09-24 13:13:55 -0700767 if (outMetaState) {
768 *outMetaState = metaState;
769 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800770 return OK;
771 }
772 }
773 return NAME_NOT_FOUND;
774 }
775
776 const KeyInfo* getKey(Device* device, int32_t scanCode, int32_t usageCode) const {
777 if (usageCode) {
778 ssize_t index = device->keysByUsageCode.indexOfKey(usageCode);
779 if (index >= 0) {
780 return &device->keysByUsageCode.valueAt(index);
781 }
782 }
783 if (scanCode) {
784 ssize_t index = device->keysByScanCode.indexOfKey(scanCode);
785 if (index >= 0) {
786 return &device->keysByScanCode.valueAt(index);
787 }
788 }
Yi Kong9b14ac62018-07-17 13:48:38 -0700789 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800790 }
791
Chris Yea52ade12020-08-27 16:49:20 -0700792 status_t mapAxis(int32_t, int32_t, AxisInfo*) const override { return NAME_NOT_FOUND; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800793
Chris Yef59a2f42020-10-16 12:55:26 -0700794 base::Result<std::pair<InputDeviceSensorType, int32_t>> mapSensor(int32_t deviceId,
795 int32_t absCode) {
796 Device* device = getDevice(deviceId);
797 if (!device) {
798 return Errorf("Sensor device not found.");
799 }
800 auto it = device->sensorsByAbsCode.find(absCode);
801 if (it == device->sensorsByAbsCode.end()) {
802 return Errorf("Sensor map not found.");
803 }
804 const SensorInfo& info = it->second;
805 return std::make_pair(info.sensorType, info.sensorDataIndex);
806 }
807
Chris Yea52ade12020-08-27 16:49:20 -0700808 void setExcludedDevices(const std::vector<std::string>& devices) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800809 mExcludedDevices = devices;
810 }
811
Siarhei Vishniakou370039c2021-02-04 22:09:01 +0000812 size_t getEvents(int, RawEvent* buffer, size_t bufferSize) override {
813 std::scoped_lock lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800814
Siarhei Vishniakou370039c2021-02-04 22:09:01 +0000815 const size_t filledSize = std::min(mEvents.size(), bufferSize);
816 std::copy(mEvents.begin(), mEvents.begin() + filledSize, buffer);
817
818 mEvents.erase(mEvents.begin(), mEvents.begin() + filledSize);
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700819 mEventsCondition.notify_all();
Siarhei Vishniakou370039c2021-02-04 22:09:01 +0000820 return filledSize;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800821 }
822
Chris Yea52ade12020-08-27 16:49:20 -0700823 std::vector<TouchVideoFrame> getVideoFrames(int32_t deviceId) override {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600824 auto it = mVideoFrames.find(deviceId);
825 if (it != mVideoFrames.end()) {
826 std::vector<TouchVideoFrame> frames = std::move(it->second);
827 mVideoFrames.erase(deviceId);
828 return frames;
829 }
Siarhei Vishniakouadd89292018-12-13 19:23:36 -0800830 return {};
831 }
832
Chris Yea52ade12020-08-27 16:49:20 -0700833 int32_t getScanCodeState(int32_t deviceId, int32_t scanCode) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800834 Device* device = getDevice(deviceId);
835 if (device) {
836 ssize_t index = device->scanCodeStates.indexOfKey(scanCode);
837 if (index >= 0) {
838 return device->scanCodeStates.valueAt(index);
839 }
840 }
841 return AKEY_STATE_UNKNOWN;
842 }
843
Chris Yea52ade12020-08-27 16:49:20 -0700844 int32_t getKeyCodeState(int32_t deviceId, int32_t keyCode) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800845 Device* device = getDevice(deviceId);
846 if (device) {
847 ssize_t index = device->keyCodeStates.indexOfKey(keyCode);
848 if (index >= 0) {
849 return device->keyCodeStates.valueAt(index);
850 }
851 }
852 return AKEY_STATE_UNKNOWN;
853 }
854
Chris Yea52ade12020-08-27 16:49:20 -0700855 int32_t getSwitchState(int32_t deviceId, int32_t sw) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800856 Device* device = getDevice(deviceId);
857 if (device) {
858 ssize_t index = device->switchStates.indexOfKey(sw);
859 if (index >= 0) {
860 return device->switchStates.valueAt(index);
861 }
862 }
863 return AKEY_STATE_UNKNOWN;
864 }
865
Chris Yea52ade12020-08-27 16:49:20 -0700866 status_t getAbsoluteAxisValue(int32_t deviceId, int32_t axis,
867 int32_t* outValue) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800868 Device* device = getDevice(deviceId);
869 if (device) {
870 ssize_t index = device->absoluteAxisValue.indexOfKey(axis);
871 if (index >= 0) {
872 *outValue = device->absoluteAxisValue.valueAt(index);
873 return OK;
874 }
875 }
876 *outValue = 0;
877 return -1;
878 }
879
Philip Junker4af3b3d2021-12-14 10:36:55 +0100880 int32_t getKeyCodeForKeyLocation(int32_t deviceId, int32_t locationKeyCode) const override {
881 Device* device = getDevice(deviceId);
882 if (!device) {
883 return AKEYCODE_UNKNOWN;
884 }
885 auto it = device->keyCodeMapping.find(locationKeyCode);
886 return it != device->keyCodeMapping.end() ? it->second : locationKeyCode;
887 }
888
Chris Yea52ade12020-08-27 16:49:20 -0700889 // Return true if the device has non-empty key layout.
890 bool markSupportedKeyCodes(int32_t deviceId, size_t numCodes, const int32_t* keyCodes,
891 uint8_t* outFlags) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800892 bool result = false;
893 Device* device = getDevice(deviceId);
894 if (device) {
Chris Yea52ade12020-08-27 16:49:20 -0700895 result = device->keysByScanCode.size() > 0 || device->keysByUsageCode.size() > 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800896 for (size_t i = 0; i < numCodes; i++) {
897 for (size_t j = 0; j < device->keysByScanCode.size(); j++) {
898 if (keyCodes[i] == device->keysByScanCode.valueAt(j).keyCode) {
899 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800900 }
901 }
902 for (size_t j = 0; j < device->keysByUsageCode.size(); j++) {
903 if (keyCodes[i] == device->keysByUsageCode.valueAt(j).keyCode) {
904 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800905 }
906 }
907 }
908 }
909 return result;
910 }
911
Chris Yea52ade12020-08-27 16:49:20 -0700912 bool hasScanCode(int32_t deviceId, int32_t scanCode) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800913 Device* device = getDevice(deviceId);
914 if (device) {
915 ssize_t index = device->keysByScanCode.indexOfKey(scanCode);
916 return index >= 0;
917 }
918 return false;
919 }
920
Arthur Hungcb40a002021-08-03 14:31:01 +0000921 bool hasKeyCode(int32_t deviceId, int32_t keyCode) const override {
922 Device* device = getDevice(deviceId);
923 if (!device) {
924 return false;
925 }
926 for (size_t i = 0; i < device->keysByScanCode.size(); i++) {
927 if (keyCode == device->keysByScanCode.valueAt(i).keyCode) {
928 return true;
929 }
930 }
931 for (size_t j = 0; j < device->keysByUsageCode.size(); j++) {
932 if (keyCode == device->keysByUsageCode.valueAt(j).keyCode) {
933 return true;
934 }
935 }
936 return false;
937 }
938
Chris Yea52ade12020-08-27 16:49:20 -0700939 bool hasLed(int32_t deviceId, int32_t led) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800940 Device* device = getDevice(deviceId);
941 return device && device->leds.indexOfKey(led) >= 0;
942 }
943
Chris Yea52ade12020-08-27 16:49:20 -0700944 void setLedState(int32_t deviceId, int32_t led, bool on) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800945 Device* device = getDevice(deviceId);
946 if (device) {
947 ssize_t index = device->leds.indexOfKey(led);
948 if (index >= 0) {
949 device->leds.replaceValueAt(led, on);
950 } else {
951 ADD_FAILURE()
952 << "Attempted to set the state of an LED that the EventHub declared "
953 "was not present. led=" << led;
954 }
955 }
956 }
957
Chris Yea52ade12020-08-27 16:49:20 -0700958 void getVirtualKeyDefinitions(
959 int32_t deviceId, std::vector<VirtualKeyDefinition>& outVirtualKeys) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800960 outVirtualKeys.clear();
961
962 Device* device = getDevice(deviceId);
963 if (device) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800964 outVirtualKeys = device->virtualKeys;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800965 }
966 }
967
Chris Yea52ade12020-08-27 16:49:20 -0700968 const std::shared_ptr<KeyCharacterMap> getKeyCharacterMap(int32_t) const override {
Yi Kong9b14ac62018-07-17 13:48:38 -0700969 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800970 }
971
Chris Yea52ade12020-08-27 16:49:20 -0700972 bool setKeyboardLayoutOverlay(int32_t, std::shared_ptr<KeyCharacterMap>) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800973 return false;
974 }
975
Chris Yea52ade12020-08-27 16:49:20 -0700976 void vibrate(int32_t, const VibrationElement&) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800977
Chris Yea52ade12020-08-27 16:49:20 -0700978 void cancelVibrate(int32_t) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800979
Chris Ye87143712020-11-10 05:05:58 +0000980 std::vector<int32_t> getVibratorIds(int32_t deviceId) override { return mVibrators; };
981
Chris Yee2b1e5c2021-03-10 22:45:12 -0800982 std::optional<int32_t> getBatteryCapacity(int32_t, int32_t) const override {
983 return BATTERY_CAPACITY;
984 }
Kim Low03ea0352020-11-06 12:45:07 -0800985
Chris Yee2b1e5c2021-03-10 22:45:12 -0800986 std::optional<int32_t> getBatteryStatus(int32_t, int32_t) const override {
987 return BATTERY_STATUS;
988 }
989
Andy Chen22c330c2022-08-29 20:07:10 -0400990 const std::vector<int32_t> getRawBatteryIds(int32_t deviceId) override {
991 return {DEFAULT_BATTERY};
992 }
Chris Yee2b1e5c2021-03-10 22:45:12 -0800993
994 std::optional<RawBatteryInfo> getRawBatteryInfo(int32_t deviceId, int32_t batteryId) {
995 return std::nullopt;
996 }
Kim Low03ea0352020-11-06 12:45:07 -0800997
Chris Ye3fdbfef2021-01-06 18:45:18 -0800998 const std::vector<int32_t> getRawLightIds(int32_t deviceId) override {
999 std::vector<int32_t> ids;
1000 for (const auto& [rawId, info] : mRawLightInfos) {
1001 ids.push_back(rawId);
1002 }
1003 return ids;
1004 }
1005
1006 std::optional<RawLightInfo> getRawLightInfo(int32_t deviceId, int32_t lightId) override {
1007 auto it = mRawLightInfos.find(lightId);
1008 if (it == mRawLightInfos.end()) {
1009 return std::nullopt;
1010 }
1011 return it->second;
1012 }
1013
1014 void setLightBrightness(int32_t deviceId, int32_t lightId, int32_t brightness) override {
1015 mLightBrightness.emplace(lightId, brightness);
1016 }
1017
1018 void setLightIntensities(int32_t deviceId, int32_t lightId,
1019 std::unordered_map<LightColor, int32_t> intensities) override {
1020 mLightIntensities.emplace(lightId, intensities);
1021 };
1022
1023 std::optional<int32_t> getLightBrightness(int32_t deviceId, int32_t lightId) override {
1024 auto lightIt = mLightBrightness.find(lightId);
1025 if (lightIt == mLightBrightness.end()) {
1026 return std::nullopt;
1027 }
1028 return lightIt->second;
1029 }
1030
1031 std::optional<std::unordered_map<LightColor, int32_t>> getLightIntensities(
1032 int32_t deviceId, int32_t lightId) override {
1033 auto lightIt = mLightIntensities.find(lightId);
1034 if (lightIt == mLightIntensities.end()) {
1035 return std::nullopt;
1036 }
1037 return lightIt->second;
1038 };
1039
Narayan Kamath39efe3e2014-10-17 10:37:08 +01001040 virtual bool isExternal(int32_t) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001041 return false;
1042 }
1043
Chris Yea52ade12020-08-27 16:49:20 -07001044 void dump(std::string&) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001045
Chris Yea52ade12020-08-27 16:49:20 -07001046 void monitor() override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001047
Chris Yea52ade12020-08-27 16:49:20 -07001048 void requestReopenDevices() override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001049
Chris Yea52ade12020-08-27 16:49:20 -07001050 void wake() override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001051};
1052
Michael Wrightd02c5b62014-02-10 15:10:22 -08001053// --- FakeInputMapper ---
1054
1055class FakeInputMapper : public InputMapper {
1056 uint32_t mSources;
1057 int32_t mKeyboardType;
1058 int32_t mMetaState;
1059 KeyedVector<int32_t, int32_t> mKeyCodeStates;
1060 KeyedVector<int32_t, int32_t> mScanCodeStates;
1061 KeyedVector<int32_t, int32_t> mSwitchStates;
Philip Junker4af3b3d2021-12-14 10:36:55 +01001062 // fake mapping which would normally come from keyCharacterMap
1063 std::unordered_map<int32_t, int32_t> mKeyCodeMapping;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001064 std::vector<int32_t> mSupportedKeyCodes;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001065
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001066 std::mutex mLock;
1067 std::condition_variable mStateChangedCondition;
1068 bool mConfigureWasCalled GUARDED_BY(mLock);
1069 bool mResetWasCalled GUARDED_BY(mLock);
1070 bool mProcessWasCalled GUARDED_BY(mLock);
1071 RawEvent mLastEvent GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001072
Arthur Hungc23540e2018-11-29 20:42:11 +08001073 std::optional<DisplayViewport> mViewport;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001074public:
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001075 FakeInputMapper(InputDeviceContext& deviceContext, uint32_t sources)
1076 : InputMapper(deviceContext),
1077 mSources(sources),
1078 mKeyboardType(AINPUT_KEYBOARD_TYPE_NONE),
Michael Wrightd02c5b62014-02-10 15:10:22 -08001079 mMetaState(0),
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001080 mConfigureWasCalled(false),
1081 mResetWasCalled(false),
1082 mProcessWasCalled(false) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001083
Chris Yea52ade12020-08-27 16:49:20 -07001084 virtual ~FakeInputMapper() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001085
1086 void setKeyboardType(int32_t keyboardType) {
1087 mKeyboardType = keyboardType;
1088 }
1089
1090 void setMetaState(int32_t metaState) {
1091 mMetaState = metaState;
1092 }
1093
1094 void assertConfigureWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001095 std::unique_lock<std::mutex> lock(mLock);
1096 base::ScopedLockAssertion assumeLocked(mLock);
1097 const bool configureCalled =
1098 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1099 return mConfigureWasCalled;
1100 });
1101 if (!configureCalled) {
1102 FAIL() << "Expected configure() to have been called.";
1103 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001104 mConfigureWasCalled = false;
1105 }
1106
1107 void assertResetWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001108 std::unique_lock<std::mutex> lock(mLock);
1109 base::ScopedLockAssertion assumeLocked(mLock);
1110 const bool resetCalled =
1111 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1112 return mResetWasCalled;
1113 });
1114 if (!resetCalled) {
1115 FAIL() << "Expected reset() to have been called.";
1116 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001117 mResetWasCalled = false;
1118 }
1119
Yi Kong9b14ac62018-07-17 13:48:38 -07001120 void assertProcessWasCalled(RawEvent* outLastEvent = nullptr) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001121 std::unique_lock<std::mutex> lock(mLock);
1122 base::ScopedLockAssertion assumeLocked(mLock);
1123 const bool processCalled =
1124 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1125 return mProcessWasCalled;
1126 });
1127 if (!processCalled) {
1128 FAIL() << "Expected process() to have been called.";
1129 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001130 if (outLastEvent) {
1131 *outLastEvent = mLastEvent;
1132 }
1133 mProcessWasCalled = false;
1134 }
1135
1136 void setKeyCodeState(int32_t keyCode, int32_t state) {
1137 mKeyCodeStates.replaceValueFor(keyCode, state);
1138 }
1139
1140 void setScanCodeState(int32_t scanCode, int32_t state) {
1141 mScanCodeStates.replaceValueFor(scanCode, state);
1142 }
1143
1144 void setSwitchState(int32_t switchCode, int32_t state) {
1145 mSwitchStates.replaceValueFor(switchCode, state);
1146 }
1147
1148 void addSupportedKeyCode(int32_t keyCode) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001149 mSupportedKeyCodes.push_back(keyCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001150 }
1151
Philip Junker4af3b3d2021-12-14 10:36:55 +01001152 void addKeyCodeMapping(int32_t fromKeyCode, int32_t toKeyCode) {
1153 mKeyCodeMapping.insert_or_assign(fromKeyCode, toKeyCode);
1154 }
1155
Michael Wrightd02c5b62014-02-10 15:10:22 -08001156private:
Philip Junker4af3b3d2021-12-14 10:36:55 +01001157 uint32_t getSources() const override { return mSources; }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001158
Chris Yea52ade12020-08-27 16:49:20 -07001159 void populateDeviceInfo(InputDeviceInfo* deviceInfo) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001160 InputMapper::populateDeviceInfo(deviceInfo);
1161
1162 if (mKeyboardType != AINPUT_KEYBOARD_TYPE_NONE) {
1163 deviceInfo->setKeyboardType(mKeyboardType);
1164 }
1165 }
1166
Chris Yea52ade12020-08-27 16:49:20 -07001167 void configure(nsecs_t, const InputReaderConfiguration* config, uint32_t changes) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001168 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001169 mConfigureWasCalled = true;
Arthur Hungc23540e2018-11-29 20:42:11 +08001170
1171 // Find the associated viewport if exist.
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001172 const std::optional<uint8_t> displayPort = getDeviceContext().getAssociatedDisplayPort();
Arthur Hungc23540e2018-11-29 20:42:11 +08001173 if (displayPort && (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
1174 mViewport = config->getDisplayViewportByPort(*displayPort);
1175 }
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001176
1177 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001178 }
1179
Chris Yea52ade12020-08-27 16:49:20 -07001180 void reset(nsecs_t) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001181 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001182 mResetWasCalled = 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 void process(const RawEvent* rawEvent) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001187 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001188 mLastEvent = *rawEvent;
1189 mProcessWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001190 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001191 }
1192
Chris Yea52ade12020-08-27 16:49:20 -07001193 int32_t getKeyCodeState(uint32_t, int32_t keyCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001194 ssize_t index = mKeyCodeStates.indexOfKey(keyCode);
1195 return index >= 0 ? mKeyCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1196 }
1197
Philip Junker4af3b3d2021-12-14 10:36:55 +01001198 int32_t getKeyCodeForKeyLocation(int32_t locationKeyCode) const override {
1199 auto it = mKeyCodeMapping.find(locationKeyCode);
1200 return it != mKeyCodeMapping.end() ? it->second : locationKeyCode;
1201 }
1202
Chris Yea52ade12020-08-27 16:49:20 -07001203 int32_t getScanCodeState(uint32_t, int32_t scanCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001204 ssize_t index = mScanCodeStates.indexOfKey(scanCode);
1205 return index >= 0 ? mScanCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1206 }
1207
Chris Yea52ade12020-08-27 16:49:20 -07001208 int32_t getSwitchState(uint32_t, int32_t switchCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001209 ssize_t index = mSwitchStates.indexOfKey(switchCode);
1210 return index >= 0 ? mSwitchStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1211 }
1212
Chris Yea52ade12020-08-27 16:49:20 -07001213 // Return true if the device has non-empty key layout.
1214 bool markSupportedKeyCodes(uint32_t, size_t numCodes, const int32_t* keyCodes,
1215 uint8_t* outFlags) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001216 for (size_t i = 0; i < numCodes; i++) {
1217 for (size_t j = 0; j < mSupportedKeyCodes.size(); j++) {
1218 if (keyCodes[i] == mSupportedKeyCodes[j]) {
1219 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001220 }
1221 }
1222 }
Chris Yea52ade12020-08-27 16:49:20 -07001223 bool result = mSupportedKeyCodes.size() > 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001224 return result;
1225 }
1226
1227 virtual int32_t getMetaState() {
1228 return mMetaState;
1229 }
1230
1231 virtual void fadePointer() {
1232 }
Arthur Hungc23540e2018-11-29 20:42:11 +08001233
1234 virtual std::optional<int32_t> getAssociatedDisplay() {
1235 if (mViewport) {
1236 return std::make_optional(mViewport->displayId);
1237 }
1238 return std::nullopt;
1239 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001240};
1241
1242
1243// --- InstrumentedInputReader ---
1244
1245class InstrumentedInputReader : public InputReader {
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001246 std::queue<std::shared_ptr<InputDevice>> mNextDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001247
1248public:
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001249 InstrumentedInputReader(std::shared_ptr<EventHubInterface> eventHub,
1250 const sp<InputReaderPolicyInterface>& policy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001251 InputListenerInterface& listener)
arthurhungdcef2dc2020-08-11 14:47:50 +08001252 : InputReader(eventHub, policy, listener), mFakeContext(this) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001253
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001254 virtual ~InstrumentedInputReader() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001255
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001256 void pushNextDevice(std::shared_ptr<InputDevice> device) { mNextDevices.push(device); }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001257
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001258 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001259 const std::string& location = "") {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001260 InputDeviceIdentifier identifier;
1261 identifier.name = name;
Arthur Hungc23540e2018-11-29 20:42:11 +08001262 identifier.location = location;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001263 int32_t generation = deviceId + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08001264 return std::make_shared<InputDevice>(&mFakeContext, deviceId, generation, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001265 }
1266
Prabir Pradhan28efc192019-11-05 01:10:04 +00001267 // Make the protected loopOnce method accessible to tests.
1268 using InputReader::loopOnce;
1269
Michael Wrightd02c5b62014-02-10 15:10:22 -08001270protected:
Chris Ye1c2e0892020-11-30 21:41:44 -08001271 virtual std::shared_ptr<InputDevice> createDeviceLocked(int32_t eventHubId,
1272 const InputDeviceIdentifier& identifier)
1273 REQUIRES(mLock) {
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001274 if (!mNextDevices.empty()) {
1275 std::shared_ptr<InputDevice> device(std::move(mNextDevices.front()));
1276 mNextDevices.pop();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001277 return device;
1278 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001279 return InputReader::createDeviceLocked(eventHubId, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001280 }
1281
arthurhungdcef2dc2020-08-11 14:47:50 +08001282 // --- FakeInputReaderContext ---
1283 class FakeInputReaderContext : public ContextImpl {
1284 int32_t mGlobalMetaState;
1285 bool mUpdateGlobalMetaStateWasCalled;
1286 int32_t mGeneration;
1287
1288 public:
1289 FakeInputReaderContext(InputReader* reader)
1290 : ContextImpl(reader),
1291 mGlobalMetaState(0),
1292 mUpdateGlobalMetaStateWasCalled(false),
1293 mGeneration(1) {}
1294
1295 virtual ~FakeInputReaderContext() {}
1296
1297 void assertUpdateGlobalMetaStateWasCalled() {
1298 ASSERT_TRUE(mUpdateGlobalMetaStateWasCalled)
1299 << "Expected updateGlobalMetaState() to have been called.";
1300 mUpdateGlobalMetaStateWasCalled = false;
1301 }
1302
1303 void setGlobalMetaState(int32_t state) { mGlobalMetaState = state; }
1304
1305 uint32_t getGeneration() { return mGeneration; }
1306
1307 void updateGlobalMetaState() override {
1308 mUpdateGlobalMetaStateWasCalled = true;
1309 ContextImpl::updateGlobalMetaState();
1310 }
1311
1312 int32_t getGlobalMetaState() override {
1313 return mGlobalMetaState | ContextImpl::getGlobalMetaState();
1314 }
1315
1316 int32_t bumpGeneration() override {
1317 mGeneration = ContextImpl::bumpGeneration();
1318 return mGeneration;
1319 }
1320 } mFakeContext;
1321
Michael Wrightd02c5b62014-02-10 15:10:22 -08001322 friend class InputReaderTest;
arthurhungdcef2dc2020-08-11 14:47:50 +08001323
1324public:
1325 FakeInputReaderContext* getContext() { return &mFakeContext; }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001326};
1327
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001328// --- InputReaderPolicyTest ---
1329class InputReaderPolicyTest : public testing::Test {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001330protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001331 sp<FakeInputReaderPolicy> mFakePolicy;
1332
Chris Yea52ade12020-08-27 16:49:20 -07001333 void SetUp() override { mFakePolicy = new FakeInputReaderPolicy(); }
1334 void TearDown() override { mFakePolicy.clear(); }
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001335};
1336
1337/**
1338 * Check that empty set of viewports is an acceptable configuration.
1339 * Also try to get internal viewport two different ways - by type and by uniqueId.
1340 *
1341 * There will be confusion if two viewports with empty uniqueId and identical type are present.
1342 * Such configuration is not currently allowed.
1343 */
1344TEST_F(InputReaderPolicyTest, Viewports_GetCleared) {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001345 static const std::string uniqueId = "local:0";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001346
1347 // We didn't add any viewports yet, so there shouldn't be any.
1348 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001349 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001350 ASSERT_FALSE(internalViewport);
1351
1352 // Add an internal viewport, then clear it
1353 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001354 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId, NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001355 ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001356
1357 // Check matching by uniqueId
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001358 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001359 ASSERT_TRUE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001360 ASSERT_EQ(ViewportType::INTERNAL, internalViewport->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001361
1362 // Check matching by viewport type
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001363 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001364 ASSERT_TRUE(internalViewport);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001365 ASSERT_EQ(uniqueId, internalViewport->uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001366
1367 mFakePolicy->clearViewports();
1368 // Make sure nothing is found after clear
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001369 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001370 ASSERT_FALSE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001371 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001372 ASSERT_FALSE(internalViewport);
1373}
1374
1375TEST_F(InputReaderPolicyTest, Viewports_GetByType) {
1376 const std::string internalUniqueId = "local:0";
1377 const std::string externalUniqueId = "local:1";
1378 const std::string virtualUniqueId1 = "virtual:2";
1379 const std::string virtualUniqueId2 = "virtual:3";
1380 constexpr int32_t virtualDisplayId1 = 2;
1381 constexpr int32_t virtualDisplayId2 = 3;
1382
1383 // Add an internal viewport
1384 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001385 DISPLAY_ORIENTATION_0, true /*isActive*/, internalUniqueId,
1386 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001387 // Add an external viewport
1388 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001389 DISPLAY_ORIENTATION_0, true /*isActive*/, externalUniqueId,
1390 NO_PORT, ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001391 // Add an virtual viewport
1392 mFakePolicy->addDisplayViewport(virtualDisplayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001393 DISPLAY_ORIENTATION_0, true /*isActive*/, virtualUniqueId1,
1394 NO_PORT, ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001395 // Add another virtual viewport
1396 mFakePolicy->addDisplayViewport(virtualDisplayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001397 DISPLAY_ORIENTATION_0, true /*isActive*/, virtualUniqueId2,
1398 NO_PORT, ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001399
1400 // Check matching by type for internal
1401 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001402 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001403 ASSERT_TRUE(internalViewport);
1404 ASSERT_EQ(internalUniqueId, internalViewport->uniqueId);
1405
1406 // Check matching by type for external
1407 std::optional<DisplayViewport> externalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001408 mFakePolicy->getDisplayViewportByType(ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001409 ASSERT_TRUE(externalViewport);
1410 ASSERT_EQ(externalUniqueId, externalViewport->uniqueId);
1411
1412 // Check matching by uniqueId for virtual viewport #1
1413 std::optional<DisplayViewport> virtualViewport1 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001414 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001415 ASSERT_TRUE(virtualViewport1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001416 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport1->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001417 ASSERT_EQ(virtualUniqueId1, virtualViewport1->uniqueId);
1418 ASSERT_EQ(virtualDisplayId1, virtualViewport1->displayId);
1419
1420 // Check matching by uniqueId for virtual viewport #2
1421 std::optional<DisplayViewport> virtualViewport2 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001422 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001423 ASSERT_TRUE(virtualViewport2);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001424 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport2->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001425 ASSERT_EQ(virtualUniqueId2, virtualViewport2->uniqueId);
1426 ASSERT_EQ(virtualDisplayId2, virtualViewport2->displayId);
1427}
1428
1429
1430/**
1431 * We can have 2 viewports of the same kind. We can distinguish them by uniqueId, and confirm
1432 * that lookup works by checking display id.
1433 * Check that 2 viewports of each kind is possible, for all existing viewport types.
1434 */
1435TEST_F(InputReaderPolicyTest, Viewports_TwoOfSameType) {
1436 const std::string uniqueId1 = "uniqueId1";
1437 const std::string uniqueId2 = "uniqueId2";
1438 constexpr int32_t displayId1 = 2;
1439 constexpr int32_t displayId2 = 3;
1440
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001441 std::vector<ViewportType> types = {ViewportType::INTERNAL, ViewportType::EXTERNAL,
1442 ViewportType::VIRTUAL};
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001443 for (const ViewportType& type : types) {
1444 mFakePolicy->clearViewports();
1445 // Add a viewport
1446 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001447 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1,
1448 NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001449 // Add another viewport
1450 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001451 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2,
1452 NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001453
1454 // Check that correct display viewport was returned by comparing the display IDs.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001455 std::optional<DisplayViewport> viewport1 =
1456 mFakePolicy->getDisplayViewportByUniqueId(uniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001457 ASSERT_TRUE(viewport1);
1458 ASSERT_EQ(displayId1, viewport1->displayId);
1459 ASSERT_EQ(type, viewport1->type);
1460
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001461 std::optional<DisplayViewport> viewport2 =
1462 mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001463 ASSERT_TRUE(viewport2);
1464 ASSERT_EQ(displayId2, viewport2->displayId);
1465 ASSERT_EQ(type, viewport2->type);
1466
1467 // When there are multiple viewports of the same kind, and uniqueId is not specified
1468 // in the call to getDisplayViewport, then that situation is not supported.
1469 // The viewports can be stored in any order, so we cannot rely on the order, since that
1470 // is just implementation detail.
1471 // However, we can check that it still returns *a* viewport, we just cannot assert
1472 // which one specifically is returned.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001473 std::optional<DisplayViewport> someViewport = mFakePolicy->getDisplayViewportByType(type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001474 ASSERT_TRUE(someViewport);
1475 }
1476}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001477
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001478/**
Michael Wrightdde67b82020-10-27 16:09:22 +00001479 * When we have multiple internal displays make sure we always return the default display when
1480 * querying by type.
1481 */
1482TEST_F(InputReaderPolicyTest, Viewports_ByTypeReturnsDefaultForInternal) {
1483 const std::string uniqueId1 = "uniqueId1";
1484 const std::string uniqueId2 = "uniqueId2";
1485 constexpr int32_t nonDefaultDisplayId = 2;
1486 static_assert(nonDefaultDisplayId != ADISPLAY_ID_DEFAULT,
1487 "Test display ID should not be ADISPLAY_ID_DEFAULT");
1488
1489 // Add the default display first and ensure it gets returned.
1490 mFakePolicy->clearViewports();
1491 mFakePolicy->addDisplayViewport(ADISPLAY_ID_DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001492 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001493 ViewportType::INTERNAL);
1494 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001495 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001496 ViewportType::INTERNAL);
1497
1498 std::optional<DisplayViewport> viewport =
1499 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
1500 ASSERT_TRUE(viewport);
1501 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
1502 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
1503
1504 // Add the default display second to make sure order doesn't matter.
1505 mFakePolicy->clearViewports();
1506 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001507 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001508 ViewportType::INTERNAL);
1509 mFakePolicy->addDisplayViewport(ADISPLAY_ID_DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001510 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001511 ViewportType::INTERNAL);
1512
1513 viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
1514 ASSERT_TRUE(viewport);
1515 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
1516 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
1517}
1518
1519/**
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001520 * Check getDisplayViewportByPort
1521 */
1522TEST_F(InputReaderPolicyTest, Viewports_GetByPort) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001523 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001524 const std::string uniqueId1 = "uniqueId1";
1525 const std::string uniqueId2 = "uniqueId2";
1526 constexpr int32_t displayId1 = 1;
1527 constexpr int32_t displayId2 = 2;
1528 const uint8_t hdmi1 = 0;
1529 const uint8_t hdmi2 = 1;
1530 const uint8_t hdmi3 = 2;
1531
1532 mFakePolicy->clearViewports();
1533 // Add a viewport that's associated with some display port that's not of interest.
1534 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001535 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, hdmi3,
1536 type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001537 // Add another viewport, connected to HDMI1 port
1538 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001539 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, hdmi1,
1540 type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001541
1542 // Check that correct display viewport was returned by comparing the display ports.
1543 std::optional<DisplayViewport> hdmi1Viewport = mFakePolicy->getDisplayViewportByPort(hdmi1);
1544 ASSERT_TRUE(hdmi1Viewport);
1545 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1546 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1547
1548 // Check that we can still get the same viewport using the uniqueId
1549 hdmi1Viewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
1550 ASSERT_TRUE(hdmi1Viewport);
1551 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1552 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1553 ASSERT_EQ(type, hdmi1Viewport->type);
1554
1555 // Check that we cannot find a port with "HDMI2", because we never added one
1556 std::optional<DisplayViewport> hdmi2Viewport = mFakePolicy->getDisplayViewportByPort(hdmi2);
1557 ASSERT_FALSE(hdmi2Viewport);
1558}
1559
Michael Wrightd02c5b62014-02-10 15:10:22 -08001560// --- InputReaderTest ---
1561
1562class InputReaderTest : public testing::Test {
1563protected:
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001564 std::unique_ptr<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001565 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001566 std::shared_ptr<FakeEventHub> mFakeEventHub;
Prabir Pradhan28efc192019-11-05 01:10:04 +00001567 std::unique_ptr<InstrumentedInputReader> mReader;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001568
Chris Yea52ade12020-08-27 16:49:20 -07001569 void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001570 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001571 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001572 mFakeListener = std::make_unique<TestInputListener>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001573
Prabir Pradhan28efc192019-11-05 01:10:04 +00001574 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001575 *mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001576 }
1577
Chris Yea52ade12020-08-27 16:49:20 -07001578 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001579 mFakeListener.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001580 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001581 }
1582
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001583 void addDevice(int32_t eventHubId, const std::string& name,
1584 ftl::Flags<InputDeviceClass> classes, const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001585 mFakeEventHub->addDevice(eventHubId, name, classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001586
1587 if (configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001588 mFakeEventHub->addConfigurationMap(eventHubId, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001589 }
1590 mFakeEventHub->finishDeviceScan();
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001591 mReader->loopOnce();
1592 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001593 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1594 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001595 }
1596
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001597 void disableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001598 mFakePolicy->addDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001599 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001600 }
1601
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001602 void enableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001603 mFakePolicy->removeDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001604 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001605 }
1606
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001607 FakeInputMapper& addDeviceWithFakeInputMapper(int32_t deviceId, int32_t eventHubId,
Chris Ye1b0c7342020-07-28 21:57:03 -07001608 const std::string& name,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001609 ftl::Flags<InputDeviceClass> classes,
1610 uint32_t sources,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001611 const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001612 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, name);
1613 FakeInputMapper& mapper = device->addMapper<FakeInputMapper>(eventHubId, sources);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001614 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001615 addDevice(eventHubId, name, classes, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001616 return mapper;
1617 }
1618};
1619
Chris Ye98d3f532020-10-01 21:48:59 -07001620TEST_F(InputReaderTest, PolicyGetInputDevices) {
1621 ASSERT_NO_FATAL_FAILURE(addDevice(1, "keyboard", InputDeviceClass::KEYBOARD, nullptr));
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001622 ASSERT_NO_FATAL_FAILURE(addDevice(2, "ignored", ftl::Flags<InputDeviceClass>(0),
Chris Ye98d3f532020-10-01 21:48:59 -07001623 nullptr)); // no classes so device will be ignored
Michael Wrightd02c5b62014-02-10 15:10:22 -08001624
1625 // Should also have received a notification describing the new input devices.
Chris Ye98d3f532020-10-01 21:48:59 -07001626 const std::vector<InputDeviceInfo>& inputDevices = mFakePolicy->getInputDevices();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001627 ASSERT_EQ(1U, inputDevices.size());
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001628 ASSERT_EQ(END_RESERVED_ID + 1, inputDevices[0].getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001629 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001630 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
1631 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00001632 ASSERT_EQ(0U, inputDevices[0].getMotionRanges().size());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001633}
1634
Chris Yee7310032020-09-22 15:36:28 -07001635TEST_F(InputReaderTest, GetMergedInputDevices) {
1636 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1637 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1638 // Add two subdevices to device
1639 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1640 // Must add at least one mapper or the device will be ignored!
1641 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1642 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1643
1644 // Push same device instance for next device to be added, so they'll have same identifier.
1645 mReader->pushNextDevice(device);
1646 mReader->pushNextDevice(device);
1647 ASSERT_NO_FATAL_FAILURE(
1648 addDevice(eventHubIds[0], "fake1", InputDeviceClass::KEYBOARD, nullptr));
1649 ASSERT_NO_FATAL_FAILURE(
1650 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
1651
1652 // Two devices will be merged to one input device as they have same identifier
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00001653 ASSERT_EQ(1U, mFakePolicy->getInputDevices().size());
Chris Yee7310032020-09-22 15:36:28 -07001654}
1655
Chris Yee14523a2020-12-19 13:46:00 -08001656TEST_F(InputReaderTest, GetMergedInputDevicesEnabled) {
1657 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1658 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1659 // Add two subdevices to device
1660 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1661 // Must add at least one mapper or the device will be ignored!
1662 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1663 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1664
1665 // Push same device instance for next device to be added, so they'll have same identifier.
1666 mReader->pushNextDevice(device);
1667 mReader->pushNextDevice(device);
1668 // Sensor device is initially disabled
1669 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1",
1670 InputDeviceClass::KEYBOARD | InputDeviceClass::SENSOR,
1671 nullptr));
1672 // Device is disabled because the only sub device is a sensor device and disabled initially.
1673 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1674 ASSERT_FALSE(device->isEnabled());
1675 ASSERT_NO_FATAL_FAILURE(
1676 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
1677 // The merged device is enabled if any sub device is enabled
1678 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1679 ASSERT_TRUE(device->isEnabled());
1680}
1681
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001682TEST_F(InputReaderTest, WhenEnabledChanges_SendsDeviceResetNotification) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001683 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001684 constexpr ftl::Flags<InputDeviceClass> deviceClass(InputDeviceClass::KEYBOARD);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001685 constexpr int32_t eventHubId = 1;
1686 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001687 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001688 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001689 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001690 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001691
Yi Kong9b14ac62018-07-17 13:48:38 -07001692 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001693
1694 NotifyDeviceResetArgs resetArgs;
1695 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001696 ASSERT_EQ(deviceId, resetArgs.deviceId);
1697
1698 ASSERT_EQ(device->isEnabled(), true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001699 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001700 mReader->loopOnce();
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001701
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001702 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001703 ASSERT_EQ(deviceId, resetArgs.deviceId);
1704 ASSERT_EQ(device->isEnabled(), false);
1705
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001706 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001707 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001708 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
1709 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasNotCalled());
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001710 ASSERT_EQ(device->isEnabled(), false);
1711
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001712 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001713 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001714 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001715 ASSERT_EQ(deviceId, resetArgs.deviceId);
1716 ASSERT_EQ(device->isEnabled(), true);
1717}
1718
Michael Wrightd02c5b62014-02-10 15:10:22 -08001719TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001720 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001721 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001722 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001723 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001724 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001725 AINPUT_SOURCE_KEYBOARD, nullptr);
1726 mapper.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001727
1728 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(0,
1729 AINPUT_SOURCE_ANY, AKEYCODE_A))
1730 << "Should return unknown when the device id is >= 0 but unknown.";
1731
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001732 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1733 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1734 << "Should return unknown when the device id is valid but the sources are not "
1735 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001736
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001737 ASSERT_EQ(AKEY_STATE_DOWN,
1738 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1739 AKEYCODE_A))
1740 << "Should return value provided by mapper when device id is valid and the device "
1741 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001742
1743 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(-1,
1744 AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1745 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1746
1747 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getKeyCodeState(-1,
1748 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1749 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1750}
1751
Philip Junker4af3b3d2021-12-14 10:36:55 +01001752TEST_F(InputReaderTest, GetKeyCodeForKeyLocation_ForwardsRequestsToMappers) {
1753 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1754 constexpr int32_t eventHubId = 1;
1755 FakeInputMapper& mapper = addDeviceWithFakeInputMapper(deviceId, eventHubId, "keyboard",
1756 InputDeviceClass::KEYBOARD,
1757 AINPUT_SOURCE_KEYBOARD, nullptr);
1758 mapper.addKeyCodeMapping(AKEYCODE_Y, AKEYCODE_Z);
1759
1760 ASSERT_EQ(AKEYCODE_UNKNOWN, mReader->getKeyCodeForKeyLocation(0, AKEYCODE_Y))
1761 << "Should return unknown when the device with the specified id is not found.";
1762
1763 ASSERT_EQ(AKEYCODE_Z, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_Y))
1764 << "Should return correct mapping when device id is valid and mapping exists.";
1765
1766 ASSERT_EQ(AKEYCODE_A, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_A))
1767 << "Should return the location key code when device id is valid and there's no "
1768 "mapping.";
1769}
1770
1771TEST_F(InputReaderTest, GetKeyCodeForKeyLocation_NoKeyboardMapper) {
1772 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1773 constexpr int32_t eventHubId = 1;
1774 FakeInputMapper& mapper = addDeviceWithFakeInputMapper(deviceId, eventHubId, "joystick",
1775 InputDeviceClass::JOYSTICK,
1776 AINPUT_SOURCE_GAMEPAD, nullptr);
1777 mapper.addKeyCodeMapping(AKEYCODE_Y, AKEYCODE_Z);
1778
1779 ASSERT_EQ(AKEYCODE_UNKNOWN, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_Y))
1780 << "Should return unknown when the device id is valid but there is no keyboard mapper";
1781}
1782
Michael Wrightd02c5b62014-02-10 15:10:22 -08001783TEST_F(InputReaderTest, GetScanCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001784 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001785 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001786 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001787 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001788 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001789 AINPUT_SOURCE_KEYBOARD, nullptr);
1790 mapper.setScanCodeState(KEY_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001791
1792 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(0,
1793 AINPUT_SOURCE_ANY, KEY_A))
1794 << "Should return unknown when the device id is >= 0 but unknown.";
1795
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001796 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1797 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, KEY_A))
1798 << "Should return unknown when the device id is valid but the sources are not "
1799 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001800
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001801 ASSERT_EQ(AKEY_STATE_DOWN,
1802 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1803 KEY_A))
1804 << "Should return value provided by mapper when device id is valid and the device "
1805 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001806
1807 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(-1,
1808 AINPUT_SOURCE_TRACKBALL, KEY_A))
1809 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1810
1811 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getScanCodeState(-1,
1812 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, KEY_A))
1813 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1814}
1815
1816TEST_F(InputReaderTest, GetSwitchState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001817 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001818 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001819 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001820 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001821 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001822 AINPUT_SOURCE_KEYBOARD, nullptr);
1823 mapper.setSwitchState(SW_LID, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001824
1825 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(0,
1826 AINPUT_SOURCE_ANY, SW_LID))
1827 << "Should return unknown when the device id is >= 0 but unknown.";
1828
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001829 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1830 mReader->getSwitchState(deviceId, AINPUT_SOURCE_TRACKBALL, SW_LID))
1831 << "Should return unknown when the device id is valid but the sources are not "
1832 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001833
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001834 ASSERT_EQ(AKEY_STATE_DOWN,
1835 mReader->getSwitchState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1836 SW_LID))
1837 << "Should return value provided by mapper when device id is valid and the device "
1838 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001839
1840 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(-1,
1841 AINPUT_SOURCE_TRACKBALL, SW_LID))
1842 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1843
1844 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getSwitchState(-1,
1845 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, SW_LID))
1846 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1847}
1848
1849TEST_F(InputReaderTest, MarkSupportedKeyCodes_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001850 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001851 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001852 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001853 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001854 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001855 AINPUT_SOURCE_KEYBOARD, nullptr);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001856
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001857 mapper.addSupportedKeyCode(AKEYCODE_A);
1858 mapper.addSupportedKeyCode(AKEYCODE_B);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001859
1860 const int32_t keyCodes[4] = { AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2 };
1861 uint8_t flags[4] = { 0, 0, 0, 1 };
1862
1863 ASSERT_FALSE(mReader->hasKeys(0, AINPUT_SOURCE_ANY, 4, keyCodes, flags))
1864 << "Should return false when device id is >= 0 but unknown.";
1865 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1866
1867 flags[3] = 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001868 ASSERT_FALSE(mReader->hasKeys(deviceId, AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1869 << "Should return false when device id is valid but the sources are not supported by "
1870 "the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001871 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1872
1873 flags[3] = 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001874 ASSERT_TRUE(mReader->hasKeys(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, 4,
1875 keyCodes, flags))
1876 << "Should return value provided by mapper when device id is valid and the device "
1877 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001878 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1879
1880 flags[3] = 1;
1881 ASSERT_FALSE(mReader->hasKeys(-1, AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1882 << "Should return false when the device id is < 0 but the sources are not supported by any device.";
1883 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1884
1885 flags[3] = 1;
1886 ASSERT_TRUE(mReader->hasKeys(-1, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1887 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1888 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1889}
1890
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001891TEST_F(InputReaderTest, LoopOnce_WhenDeviceScanFinished_SendsConfigurationChanged) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001892 constexpr int32_t eventHubId = 1;
Chris Ye1b0c7342020-07-28 21:57:03 -07001893 addDevice(eventHubId, "ignored", InputDeviceClass::KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001894
1895 NotifyConfigurationChangedArgs args;
1896
1897 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(&args));
1898 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
1899}
1900
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001901TEST_F(InputReaderTest, LoopOnce_ForwardsRawEventsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001902 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001903 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001904 constexpr nsecs_t when = 0;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001905 constexpr int32_t eventHubId = 1;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001906 constexpr nsecs_t readTime = 2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001907 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001908 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001909 AINPUT_SOURCE_KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001910
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001911 mFakeEventHub->enqueueEvent(when, readTime, eventHubId, EV_KEY, KEY_A, 1);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001912 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001913 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
1914
1915 RawEvent event;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001916 ASSERT_NO_FATAL_FAILURE(mapper.assertProcessWasCalled(&event));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001917 ASSERT_EQ(when, event.when);
1918 ASSERT_EQ(readTime, event.readTime);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001919 ASSERT_EQ(eventHubId, event.deviceId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001920 ASSERT_EQ(EV_KEY, event.type);
1921 ASSERT_EQ(KEY_A, event.code);
1922 ASSERT_EQ(1, event.value);
1923}
1924
Garfield Tan1c7bc862020-01-28 13:24:04 -08001925TEST_F(InputReaderTest, DeviceReset_RandomId) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001926 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001927 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001928 constexpr int32_t eventHubId = 1;
1929 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Prabir Pradhan42611e02018-11-27 14:04:02 -08001930 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001931 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001932 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001933 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan42611e02018-11-27 14:04:02 -08001934
1935 NotifyDeviceResetArgs resetArgs;
1936 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001937 int32_t prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001938
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001939 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001940 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001941 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001942 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001943 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001944
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001945 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001946 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001947 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001948 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001949 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001950
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001951 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001952 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001953 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001954 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001955 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001956}
1957
Garfield Tan1c7bc862020-01-28 13:24:04 -08001958TEST_F(InputReaderTest, DeviceReset_GenerateIdWithInputReaderSource) {
1959 constexpr int32_t deviceId = 1;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001960 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Garfield Tan1c7bc862020-01-28 13:24:04 -08001961 constexpr int32_t eventHubId = 1;
1962 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1963 // Must add at least one mapper or the device will be ignored!
1964 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001965 mReader->pushNextDevice(device);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001966 ASSERT_NO_FATAL_FAILURE(addDevice(deviceId, "fake", deviceClass, nullptr));
1967
1968 NotifyDeviceResetArgs resetArgs;
1969 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1970 ASSERT_EQ(IdGenerator::Source::INPUT_READER, IdGenerator::getSource(resetArgs.id));
1971}
1972
Arthur Hungc23540e2018-11-29 20:42:11 +08001973TEST_F(InputReaderTest, Device_CanDispatchToDisplay) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001974 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001975 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001976 constexpr int32_t eventHubId = 1;
Arthur Hungc23540e2018-11-29 20:42:11 +08001977 const char* DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001978 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1979 FakeInputMapper& mapper =
1980 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001981 mReader->pushNextDevice(device);
Arthur Hungc23540e2018-11-29 20:42:11 +08001982
1983 const uint8_t hdmi1 = 1;
1984
1985 // Associated touch screen with second display.
1986 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
1987
1988 // Add default and second display.
Prabir Pradhan28efc192019-11-05 01:10:04 +00001989 mFakePolicy->clearViewports();
Arthur Hungc23540e2018-11-29 20:42:11 +08001990 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001991 DISPLAY_ORIENTATION_0, true /*isActive*/, "local:0", NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001992 ViewportType::INTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08001993 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001994 DISPLAY_ORIENTATION_0, true /*isActive*/, "local:1", hdmi1,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001995 ViewportType::EXTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08001996 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001997 mReader->loopOnce();
Prabir Pradhan28efc192019-11-05 01:10:04 +00001998
1999 // Add the device, and make sure all of the callbacks are triggered.
2000 // The device is added after the input port associations are processed since
2001 // we do not yet support dynamic device-to-display associations.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002002 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07002003 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled());
Prabir Pradhan28efc192019-11-05 01:10:04 +00002004 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002005 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
Arthur Hungc23540e2018-11-29 20:42:11 +08002006
Arthur Hung2c9a3342019-07-23 14:18:59 +08002007 // Device should only dispatch to the specified display.
Arthur Hungc23540e2018-11-29 20:42:11 +08002008 ASSERT_EQ(deviceId, device->getId());
2009 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, DISPLAY_ID));
2010 ASSERT_TRUE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hung2c9a3342019-07-23 14:18:59 +08002011
2012 // Can't dispatch event from a disabled device.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002013 disableDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00002014 mReader->loopOnce();
Arthur Hung2c9a3342019-07-23 14:18:59 +08002015 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hungc23540e2018-11-29 20:42:11 +08002016}
2017
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08002018TEST_F(InputReaderTest, WhenEnabledChanges_AllSubdevicesAreUpdated) {
2019 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002020 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08002021 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
2022 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
2023 // Must add at least one mapper or the device will be ignored!
2024 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
2025 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
2026 mReader->pushNextDevice(device);
2027 mReader->pushNextDevice(device);
2028 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
2029 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
2030
2031 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
2032
2033 NotifyDeviceResetArgs resetArgs;
2034 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2035 ASSERT_EQ(deviceId, resetArgs.deviceId);
2036 ASSERT_TRUE(device->isEnabled());
2037 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
2038 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
2039
2040 disableDevice(deviceId);
2041 mReader->loopOnce();
2042
2043 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2044 ASSERT_EQ(deviceId, resetArgs.deviceId);
2045 ASSERT_FALSE(device->isEnabled());
2046 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
2047 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
2048
2049 enableDevice(deviceId);
2050 mReader->loopOnce();
2051
2052 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2053 ASSERT_EQ(deviceId, resetArgs.deviceId);
2054 ASSERT_TRUE(device->isEnabled());
2055 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
2056 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
2057}
2058
2059TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToSubdeviceMappers) {
2060 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002061 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08002062 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
2063 // Add two subdevices to device
2064 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
2065 FakeInputMapper& mapperDevice1 =
2066 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
2067 FakeInputMapper& mapperDevice2 =
2068 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
2069 mReader->pushNextDevice(device);
2070 mReader->pushNextDevice(device);
2071 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
2072 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
2073
2074 mapperDevice1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
2075 mapperDevice2.setKeyCodeState(AKEYCODE_B, AKEY_STATE_DOWN);
2076
2077 ASSERT_EQ(AKEY_STATE_DOWN,
2078 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_A));
2079 ASSERT_EQ(AKEY_STATE_DOWN,
2080 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_B));
2081 ASSERT_EQ(AKEY_STATE_UNKNOWN,
2082 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_C));
2083}
2084
Prabir Pradhan7e186182020-11-10 13:56:45 -08002085TEST_F(InputReaderTest, ChangingPointerCaptureNotifiesInputListener) {
2086 NotifyPointerCaptureChangedArgs args;
2087
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002088 auto request = mFakePolicy->setPointerCapture(true);
Prabir Pradhan7e186182020-11-10 13:56:45 -08002089 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
2090 mReader->loopOnce();
2091 mFakeListener->assertNotifyCaptureWasCalled(&args);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002092 ASSERT_TRUE(args.request.enable) << "Pointer Capture should be enabled.";
2093 ASSERT_EQ(args.request, request) << "Pointer Capture sequence number should match.";
Prabir Pradhan7e186182020-11-10 13:56:45 -08002094
2095 mFakePolicy->setPointerCapture(false);
2096 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
2097 mReader->loopOnce();
2098 mFakeListener->assertNotifyCaptureWasCalled(&args);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002099 ASSERT_FALSE(args.request.enable) << "Pointer Capture should be disabled.";
Prabir Pradhan7e186182020-11-10 13:56:45 -08002100
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002101 // Verify that the Pointer Capture state is not updated when the configuration value
Prabir Pradhan7e186182020-11-10 13:56:45 -08002102 // does not change.
2103 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
2104 mReader->loopOnce();
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002105 mFakeListener->assertNotifyCaptureWasNotCalled();
Prabir Pradhan7e186182020-11-10 13:56:45 -08002106}
2107
Chris Ye87143712020-11-10 05:05:58 +00002108class FakeVibratorInputMapper : public FakeInputMapper {
2109public:
2110 FakeVibratorInputMapper(InputDeviceContext& deviceContext, uint32_t sources)
2111 : FakeInputMapper(deviceContext, sources) {}
2112
2113 std::vector<int32_t> getVibratorIds() override { return getDeviceContext().getVibratorIds(); }
2114};
2115
2116TEST_F(InputReaderTest, VibratorGetVibratorIds) {
2117 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002118 ftl::Flags<InputDeviceClass> deviceClass =
2119 InputDeviceClass::KEYBOARD | InputDeviceClass::VIBRATOR;
Chris Ye87143712020-11-10 05:05:58 +00002120 constexpr int32_t eventHubId = 1;
2121 const char* DEVICE_LOCATION = "BLUETOOTH";
2122 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
2123 FakeVibratorInputMapper& mapper =
2124 device->addMapper<FakeVibratorInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
2125 mReader->pushNextDevice(device);
2126
2127 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2128 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
2129
2130 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
2131 ASSERT_EQ(mReader->getVibratorIds(deviceId).size(), 2U);
2132}
2133
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002134// --- FakePeripheralController ---
Kim Low03ea0352020-11-06 12:45:07 -08002135
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002136class FakePeripheralController : public PeripheralControllerInterface {
Chris Yee2b1e5c2021-03-10 22:45:12 -08002137public:
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002138 FakePeripheralController(InputDeviceContext& deviceContext) : mDeviceContext(deviceContext) {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08002139
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002140 ~FakePeripheralController() override {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08002141
Andy Chen22c330c2022-08-29 20:07:10 -04002142 int32_t getEventHubId() const { return getDeviceContext().getEventHubId(); }
2143
Chris Yee2b1e5c2021-03-10 22:45:12 -08002144 void populateDeviceInfo(InputDeviceInfo* deviceInfo) override {}
2145
2146 void dump(std::string& dump) override {}
2147
2148 std::optional<int32_t> getBatteryCapacity(int32_t batteryId) override {
2149 return getDeviceContext().getBatteryCapacity(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08002150 }
2151
Chris Yee2b1e5c2021-03-10 22:45:12 -08002152 std::optional<int32_t> getBatteryStatus(int32_t batteryId) override {
2153 return getDeviceContext().getBatteryStatus(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08002154 }
Chris Ye3fdbfef2021-01-06 18:45:18 -08002155
2156 bool setLightColor(int32_t lightId, int32_t color) override {
2157 getDeviceContext().setLightBrightness(lightId, color >> 24);
2158 return true;
2159 }
2160
2161 std::optional<int32_t> getLightColor(int32_t lightId) override {
2162 std::optional<int32_t> result = getDeviceContext().getLightBrightness(lightId);
2163 if (!result.has_value()) {
2164 return std::nullopt;
2165 }
2166 return result.value() << 24;
2167 }
Chris Yee2b1e5c2021-03-10 22:45:12 -08002168
2169 bool setLightPlayerId(int32_t lightId, int32_t playerId) override { return true; }
2170
2171 std::optional<int32_t> getLightPlayerId(int32_t lightId) override { return std::nullopt; }
2172
2173private:
2174 InputDeviceContext& mDeviceContext;
2175 inline int32_t getDeviceId() { return mDeviceContext.getId(); }
2176 inline InputDeviceContext& getDeviceContext() { return mDeviceContext; }
Andy Chen22c330c2022-08-29 20:07:10 -04002177 inline InputDeviceContext& getDeviceContext() const { return mDeviceContext; }
Chris Ye3fdbfef2021-01-06 18:45:18 -08002178};
2179
Chris Yee2b1e5c2021-03-10 22:45:12 -08002180TEST_F(InputReaderTest, BatteryGetCapacity) {
2181 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002182 ftl::Flags<InputDeviceClass> deviceClass =
2183 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
Chris Yee2b1e5c2021-03-10 22:45:12 -08002184 constexpr int32_t eventHubId = 1;
2185 const char* DEVICE_LOCATION = "BLUETOOTH";
2186 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002187 FakePeripheralController& controller =
2188 device->addController<FakePeripheralController>(eventHubId);
Chris Yee2b1e5c2021-03-10 22:45:12 -08002189 mReader->pushNextDevice(device);
2190
2191 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2192
2193 ASSERT_EQ(controller.getBatteryCapacity(DEFAULT_BATTERY), BATTERY_CAPACITY);
2194 ASSERT_EQ(mReader->getBatteryCapacity(deviceId), BATTERY_CAPACITY);
2195}
2196
2197TEST_F(InputReaderTest, BatteryGetStatus) {
2198 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002199 ftl::Flags<InputDeviceClass> deviceClass =
2200 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
Chris Yee2b1e5c2021-03-10 22:45:12 -08002201 constexpr int32_t eventHubId = 1;
2202 const char* DEVICE_LOCATION = "BLUETOOTH";
2203 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002204 FakePeripheralController& controller =
2205 device->addController<FakePeripheralController>(eventHubId);
Chris Yee2b1e5c2021-03-10 22:45:12 -08002206 mReader->pushNextDevice(device);
2207
2208 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2209
2210 ASSERT_EQ(controller.getBatteryStatus(DEFAULT_BATTERY), BATTERY_STATUS);
2211 ASSERT_EQ(mReader->getBatteryStatus(deviceId), BATTERY_STATUS);
2212}
2213
Chris Ye3fdbfef2021-01-06 18:45:18 -08002214TEST_F(InputReaderTest, LightGetColor) {
2215 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002216 ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD | InputDeviceClass::LIGHT;
Chris Ye3fdbfef2021-01-06 18:45:18 -08002217 constexpr int32_t eventHubId = 1;
2218 const char* DEVICE_LOCATION = "BLUETOOTH";
2219 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002220 FakePeripheralController& controller =
2221 device->addController<FakePeripheralController>(eventHubId);
Chris Ye3fdbfef2021-01-06 18:45:18 -08002222 mReader->pushNextDevice(device);
2223 RawLightInfo info = {.id = 1,
2224 .name = "Mono",
2225 .maxBrightness = 255,
2226 .flags = InputLightClass::BRIGHTNESS,
2227 .path = ""};
2228 mFakeEventHub->addRawLightInfo(1 /* rawId */, std::move(info));
2229 mFakeEventHub->fakeLightBrightness(1 /* rawId */, 0x55);
2230
2231 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Chris Ye3fdbfef2021-01-06 18:45:18 -08002232
Chris Yee2b1e5c2021-03-10 22:45:12 -08002233 ASSERT_TRUE(controller.setLightColor(1 /* lightId */, LIGHT_BRIGHTNESS));
2234 ASSERT_EQ(controller.getLightColor(1 /* lightId */), LIGHT_BRIGHTNESS);
Chris Ye3fdbfef2021-01-06 18:45:18 -08002235 ASSERT_TRUE(mReader->setLightColor(deviceId, 1 /* lightId */, LIGHT_BRIGHTNESS));
2236 ASSERT_EQ(mReader->getLightColor(deviceId, 1 /* lightId */), LIGHT_BRIGHTNESS);
2237}
2238
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002239// --- InputReaderIntegrationTest ---
2240
2241// These tests create and interact with the InputReader only through its interface.
2242// The InputReader is started during SetUp(), which starts its processing in its own
2243// thread. The tests use linux uinput to emulate input devices.
2244// NOTE: Interacting with the physical device while these tests are running may cause
2245// the tests to fail.
2246class InputReaderIntegrationTest : public testing::Test {
2247protected:
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002248 std::unique_ptr<TestInputListener> mTestListener;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002249 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002250 std::unique_ptr<InputReaderInterface> mReader;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002251
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00002252 std::shared_ptr<FakePointerController> mFakePointerController;
2253
Chris Yea52ade12020-08-27 16:49:20 -07002254 void SetUp() override {
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002255 mFakePolicy = new FakeInputReaderPolicy();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00002256 mFakePointerController = std::make_shared<FakePointerController>();
2257 mFakePolicy->setPointerController(mFakePointerController);
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002258 mTestListener = std::make_unique<TestInputListener>(2000ms /*eventHappenedTimeout*/,
2259 30ms /*eventDidNotHappenTimeout*/);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002260
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002261 mReader = std::make_unique<InputReader>(std::make_shared<EventHub>(), mFakePolicy,
2262 *mTestListener);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002263 ASSERT_EQ(mReader->start(), OK);
2264
2265 // Since this test is run on a real device, all the input devices connected
2266 // to the test device will show up in mReader. We wait for those input devices to
2267 // show up before beginning the tests.
2268 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2269 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2270 }
2271
Chris Yea52ade12020-08-27 16:49:20 -07002272 void TearDown() override {
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002273 ASSERT_EQ(mReader->stop(), OK);
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002274 mReader.reset();
2275 mTestListener.reset();
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002276 mFakePolicy.clear();
2277 }
2278};
2279
2280TEST_F(InputReaderIntegrationTest, TestInvalidDevice) {
2281 // An invalid input device that is only used for this test.
2282 class InvalidUinputDevice : public UinputDevice {
2283 public:
2284 InvalidUinputDevice() : UinputDevice("Invalid Device") {}
2285
2286 private:
2287 void configureDevice(int fd, uinput_user_dev* device) override {}
2288 };
2289
2290 const size_t numDevices = mFakePolicy->getInputDevices().size();
2291
2292 // UinputDevice does not set any event or key bits, so InputReader should not
2293 // consider it as a valid device.
2294 std::unique_ptr<UinputDevice> invalidDevice = createUinputDevice<InvalidUinputDevice>();
2295 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
2296 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
2297 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
2298
2299 invalidDevice.reset();
2300 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
2301 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
2302 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
2303}
2304
2305TEST_F(InputReaderIntegrationTest, AddNewDevice) {
2306 const size_t initialNumDevices = mFakePolicy->getInputDevices().size();
2307
2308 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
2309 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2310 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2311 ASSERT_EQ(initialNumDevices + 1, mFakePolicy->getInputDevices().size());
2312
2313 // Find the test device by its name.
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002314 const std::vector<InputDeviceInfo> inputDevices = mFakePolicy->getInputDevices();
Chris Ye98d3f532020-10-01 21:48:59 -07002315 const auto& it =
2316 std::find_if(inputDevices.begin(), inputDevices.end(),
2317 [&keyboard](const InputDeviceInfo& info) {
2318 return info.getIdentifier().name == keyboard->getName();
2319 });
2320
2321 ASSERT_NE(it, inputDevices.end());
2322 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, it->getKeyboardType());
2323 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, it->getSources());
2324 ASSERT_EQ(0U, it->getMotionRanges().size());
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002325
2326 keyboard.reset();
2327 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2328 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2329 ASSERT_EQ(initialNumDevices, mFakePolicy->getInputDevices().size());
2330}
2331
2332TEST_F(InputReaderIntegrationTest, SendsEventsToInputListener) {
2333 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
2334 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2335
2336 NotifyConfigurationChangedArgs configChangedArgs;
2337 ASSERT_NO_FATAL_FAILURE(
2338 mTestListener->assertNotifyConfigurationChangedWasCalled(&configChangedArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002339 int32_t prevId = configChangedArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002340 nsecs_t prevTimestamp = configChangedArgs.eventTime;
2341
2342 NotifyKeyArgs keyArgs;
2343 keyboard->pressAndReleaseHomeKey();
2344 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
2345 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002346 ASSERT_NE(prevId, keyArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002347 prevId = keyArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002348 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002349 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002350 prevTimestamp = keyArgs.eventTime;
2351
2352 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
2353 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002354 ASSERT_NE(prevId, keyArgs.id);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002355 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002356 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002357}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002358
Siarhei Vishniakoua0d2b802020-05-13 14:00:31 -07002359/**
2360 * The Steam controller sends BTN_GEAR_DOWN and BTN_GEAR_UP for the two "paddle" buttons
2361 * on the back. In this test, we make sure that BTN_GEAR_DOWN / BTN_WHEEL and BTN_GEAR_UP
2362 * are passed to the listener.
2363 */
2364static_assert(BTN_GEAR_DOWN == BTN_WHEEL);
2365TEST_F(InputReaderIntegrationTest, SendsGearDownAndUpToInputListener) {
2366 std::unique_ptr<UinputSteamController> controller = createUinputDevice<UinputSteamController>();
2367 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2368 NotifyKeyArgs keyArgs;
2369
2370 controller->pressAndReleaseKey(BTN_GEAR_DOWN);
2371 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
2372 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
2373 ASSERT_EQ(BTN_GEAR_DOWN, keyArgs.scanCode);
2374
2375 controller->pressAndReleaseKey(BTN_GEAR_UP);
2376 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
2377 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
2378 ASSERT_EQ(BTN_GEAR_UP, keyArgs.scanCode);
2379}
2380
Arthur Hungaab25622020-01-16 11:22:11 +08002381// --- TouchProcessTest ---
2382class TouchIntegrationTest : public InputReaderIntegrationTest {
2383protected:
Arthur Hungaab25622020-01-16 11:22:11 +08002384 const std::string UNIQUE_ID = "local:0";
2385
Chris Yea52ade12020-08-27 16:49:20 -07002386 void SetUp() override {
Arthur Hungaab25622020-01-16 11:22:11 +08002387 InputReaderIntegrationTest::SetUp();
2388 // At least add an internal display.
2389 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2390 DISPLAY_ORIENTATION_0, UNIQUE_ID, NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002391 ViewportType::INTERNAL);
Arthur Hungaab25622020-01-16 11:22:11 +08002392
2393 mDevice = createUinputDevice<UinputTouchScreen>(Rect(0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT));
2394 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2395 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2396 }
2397
2398 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
2399 int32_t orientation, const std::string& uniqueId,
2400 std::optional<uint8_t> physicalPort,
2401 ViewportType viewportType) {
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002402 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, true /*isActive*/,
2403 uniqueId, physicalPort, viewportType);
Arthur Hungaab25622020-01-16 11:22:11 +08002404 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2405 }
2406
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002407 void assertReceivedMotion(int32_t action, const std::vector<Point>& points) {
2408 NotifyMotionArgs args;
2409 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2410 EXPECT_EQ(action, args.action);
2411 ASSERT_EQ(points.size(), args.pointerCount);
2412 for (size_t i = 0; i < args.pointerCount; i++) {
2413 EXPECT_EQ(points[i].x, args.pointerCoords[i].getX());
2414 EXPECT_EQ(points[i].y, args.pointerCoords[i].getY());
2415 }
2416 }
2417
Arthur Hungaab25622020-01-16 11:22:11 +08002418 std::unique_ptr<UinputTouchScreen> mDevice;
2419};
2420
2421TEST_F(TouchIntegrationTest, InputEvent_ProcessSingleTouch) {
2422 NotifyMotionArgs args;
2423 const Point centerPoint = mDevice->getCenterPoint();
2424
2425 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00002426 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002427 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002428 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002429 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2430 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2431
2432 // ACTION_MOVE
2433 mDevice->sendMove(centerPoint + Point(1, 1));
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002434 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002435 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2436 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2437
2438 // ACTION_UP
2439 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002440 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002441 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2442 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2443}
2444
2445TEST_F(TouchIntegrationTest, InputEvent_ProcessMultiTouch) {
2446 NotifyMotionArgs args;
2447 const Point centerPoint = mDevice->getCenterPoint();
2448
2449 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00002450 mDevice->sendSlot(FIRST_SLOT);
2451 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002452 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002453 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002454 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2455 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2456
2457 // ACTION_POINTER_DOWN (Second slot)
2458 const Point secondPoint = centerPoint + Point(100, 100);
2459 mDevice->sendSlot(SECOND_SLOT);
2460 mDevice->sendTrackingId(SECOND_TRACKING_ID);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002461 mDevice->sendDown(secondPoint);
2462 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002463 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08002464 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002465
2466 // ACTION_MOVE (Second slot)
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002467 mDevice->sendMove(secondPoint + Point(1, 1));
2468 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002469 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2470 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2471
2472 // ACTION_POINTER_UP (Second slot)
arthurhungcc7f9802020-04-30 17:55:40 +08002473 mDevice->sendPointerUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002474 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002475 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08002476 ASSERT_EQ(ACTION_POINTER_1_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002477
2478 // ACTION_UP
2479 mDevice->sendSlot(FIRST_SLOT);
2480 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002481 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002482 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2483 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2484}
2485
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002486/**
2487 * What happens when a pointer goes up while another pointer moves in the same frame? Are POINTER_UP
2488 * events guaranteed to contain the same data as a preceding MOVE, or can they contain different
2489 * data?
2490 * In this test, we try to send a change in coordinates in Pointer 0 in the same frame as the
2491 * liftoff of Pointer 1. We check that POINTER_UP event is generated first, and the MOVE event
2492 * for Pointer 0 only is generated after.
2493 * Suppose we are only interested in learning the movement of Pointer 0. If we only observe MOVE
2494 * events, we will not miss any information.
2495 * Even though the Pointer 1 up event contains updated Pointer 0 coordinates, there is another MOVE
2496 * event generated afterwards that contains the newest movement of pointer 0.
2497 * This is important for palm rejection. If there is a subsequent InputListener stage that detects
2498 * palms, and wants to cancel Pointer 1, then it is safe to simply drop POINTER_1_UP event without
2499 * losing information about non-palm pointers.
2500 */
2501TEST_F(TouchIntegrationTest, MultiTouch_PointerMoveAndSecondPointerUp) {
2502 NotifyMotionArgs args;
2503 const Point centerPoint = mDevice->getCenterPoint();
2504
2505 // ACTION_DOWN
2506 mDevice->sendSlot(FIRST_SLOT);
2507 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2508 mDevice->sendDown(centerPoint);
2509 mDevice->sendSync();
2510 assertReceivedMotion(AMOTION_EVENT_ACTION_DOWN, {centerPoint});
2511
2512 // ACTION_POINTER_DOWN (Second slot)
2513 const Point secondPoint = centerPoint + Point(100, 100);
2514 mDevice->sendSlot(SECOND_SLOT);
2515 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2516 mDevice->sendDown(secondPoint);
2517 mDevice->sendSync();
2518 assertReceivedMotion(ACTION_POINTER_1_DOWN, {centerPoint, secondPoint});
2519
2520 // ACTION_MOVE (First slot)
2521 mDevice->sendSlot(FIRST_SLOT);
2522 mDevice->sendMove(centerPoint + Point(5, 5));
2523 // ACTION_POINTER_UP (Second slot)
2524 mDevice->sendSlot(SECOND_SLOT);
2525 mDevice->sendPointerUp();
2526 // Send a single sync for the above 2 pointer updates
2527 mDevice->sendSync();
2528
2529 // First, we should get POINTER_UP for the second pointer
2530 assertReceivedMotion(ACTION_POINTER_1_UP,
2531 {/*first pointer */ centerPoint + Point(5, 5),
2532 /*second pointer*/ secondPoint});
2533
2534 // Next, the MOVE event for the first pointer
2535 assertReceivedMotion(AMOTION_EVENT_ACTION_MOVE, {centerPoint + Point(5, 5)});
2536}
2537
2538/**
2539 * Similar scenario as above. The difference is that when the second pointer goes up, it will first
2540 * move, and then it will go up, all in the same frame.
2541 * In this scenario, the movement of the second pointer just prior to liftoff is ignored, and never
2542 * gets sent to the listener.
2543 */
2544TEST_F(TouchIntegrationTest, MultiTouch_PointerMoveAndSecondPointerMoveAndUp) {
2545 NotifyMotionArgs args;
2546 const Point centerPoint = mDevice->getCenterPoint();
2547
2548 // ACTION_DOWN
2549 mDevice->sendSlot(FIRST_SLOT);
2550 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2551 mDevice->sendDown(centerPoint);
2552 mDevice->sendSync();
2553 assertReceivedMotion(AMOTION_EVENT_ACTION_DOWN, {centerPoint});
2554
2555 // ACTION_POINTER_DOWN (Second slot)
2556 const Point secondPoint = centerPoint + Point(100, 100);
2557 mDevice->sendSlot(SECOND_SLOT);
2558 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2559 mDevice->sendDown(secondPoint);
2560 mDevice->sendSync();
2561 assertReceivedMotion(ACTION_POINTER_1_DOWN, {centerPoint, secondPoint});
2562
2563 // ACTION_MOVE (First slot)
2564 mDevice->sendSlot(FIRST_SLOT);
2565 mDevice->sendMove(centerPoint + Point(5, 5));
2566 // ACTION_POINTER_UP (Second slot)
2567 mDevice->sendSlot(SECOND_SLOT);
2568 mDevice->sendMove(secondPoint + Point(6, 6));
2569 mDevice->sendPointerUp();
2570 // Send a single sync for the above 2 pointer updates
2571 mDevice->sendSync();
2572
2573 // First, we should get POINTER_UP for the second pointer
2574 // The movement of the second pointer during the liftoff frame is ignored.
2575 // The coordinates 'secondPoint + Point(6, 6)' are never sent to the listener.
2576 assertReceivedMotion(ACTION_POINTER_1_UP,
2577 {/*first pointer */ centerPoint + Point(5, 5),
2578 /*second pointer*/ secondPoint});
2579
2580 // Next, the MOVE event for the first pointer
2581 assertReceivedMotion(AMOTION_EVENT_ACTION_MOVE, {centerPoint + Point(5, 5)});
2582}
2583
Arthur Hungaab25622020-01-16 11:22:11 +08002584TEST_F(TouchIntegrationTest, InputEvent_ProcessPalm) {
2585 NotifyMotionArgs args;
2586 const Point centerPoint = mDevice->getCenterPoint();
2587
2588 // ACTION_DOWN
arthurhungcc7f9802020-04-30 17:55:40 +08002589 mDevice->sendSlot(FIRST_SLOT);
2590 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002591 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002592 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002593 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2594 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2595
arthurhungcc7f9802020-04-30 17:55:40 +08002596 // ACTION_POINTER_DOWN (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002597 const Point secondPoint = centerPoint + Point(100, 100);
2598 mDevice->sendSlot(SECOND_SLOT);
2599 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2600 mDevice->sendDown(secondPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002601 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002602 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08002603 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002604
arthurhungcc7f9802020-04-30 17:55:40 +08002605 // ACTION_MOVE (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002606 mDevice->sendMove(secondPoint + Point(1, 1));
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002607 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002608 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2609 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2610
arthurhungcc7f9802020-04-30 17:55:40 +08002611 // Send MT_TOOL_PALM (second slot), which indicates that the touch IC has determined this to be
2612 // a palm event.
2613 // Expect to receive the ACTION_POINTER_UP with cancel flag.
Arthur Hungaab25622020-01-16 11:22:11 +08002614 mDevice->sendToolType(MT_TOOL_PALM);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002615 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002616 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08002617 ASSERT_EQ(ACTION_POINTER_1_UP, args.action);
arthurhungcc7f9802020-04-30 17:55:40 +08002618 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, args.flags);
Arthur Hungaab25622020-01-16 11:22:11 +08002619
arthurhungcc7f9802020-04-30 17:55:40 +08002620 // Send up to second slot, expect first slot send moving.
2621 mDevice->sendPointerUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002622 mDevice->sendSync();
arthurhungcc7f9802020-04-30 17:55:40 +08002623 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2624 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002625
arthurhungcc7f9802020-04-30 17:55:40 +08002626 // Send ACTION_UP (first slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002627 mDevice->sendSlot(FIRST_SLOT);
2628 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002629 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002630
arthurhungcc7f9802020-04-30 17:55:40 +08002631 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2632 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002633}
2634
Michael Wrightd02c5b62014-02-10 15:10:22 -08002635// --- InputDeviceTest ---
Michael Wrightd02c5b62014-02-10 15:10:22 -08002636class InputDeviceTest : public testing::Test {
2637protected:
2638 static const char* DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002639 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002640 static const int32_t DEVICE_ID;
2641 static const int32_t DEVICE_GENERATION;
2642 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002643 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002644 static const int32_t EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002645
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002646 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002647 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002648 std::unique_ptr<TestInputListener> mFakeListener;
arthurhungdcef2dc2020-08-11 14:47:50 +08002649 std::unique_ptr<InstrumentedInputReader> mReader;
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00002650 std::shared_ptr<InputDevice> mDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002651
Chris Yea52ade12020-08-27 16:49:20 -07002652 void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002653 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002654 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002655 mFakeListener = std::make_unique<TestInputListener>();
arthurhungdcef2dc2020-08-11 14:47:50 +08002656 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002657 *mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002658 InputDeviceIdentifier identifier;
2659 identifier.name = DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002660 identifier.location = DEVICE_LOCATION;
arthurhungdcef2dc2020-08-11 14:47:50 +08002661 mDevice = std::make_shared<InputDevice>(mReader->getContext(), DEVICE_ID, DEVICE_GENERATION,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002662 identifier);
arthurhungdcef2dc2020-08-11 14:47:50 +08002663 mReader->pushNextDevice(mDevice);
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002664 mFakeEventHub->addDevice(EVENTHUB_ID, DEVICE_NAME, ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08002665 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002666 }
2667
Chris Yea52ade12020-08-27 16:49:20 -07002668 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002669 mFakeListener.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002670 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002671 }
2672};
2673
2674const char* InputDeviceTest::DEVICE_NAME = "device";
Arthur Hung2c9a3342019-07-23 14:18:59 +08002675const char* InputDeviceTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002676const int32_t InputDeviceTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002677const int32_t InputDeviceTest::DEVICE_GENERATION = 2;
2678const int32_t InputDeviceTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002679const ftl::Flags<InputDeviceClass> InputDeviceTest::DEVICE_CLASSES =
Chris Ye1b0c7342020-07-28 21:57:03 -07002680 InputDeviceClass::KEYBOARD | InputDeviceClass::TOUCH | InputDeviceClass::JOYSTICK;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002681const int32_t InputDeviceTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002682
2683TEST_F(InputDeviceTest, ImmutableProperties) {
2684 ASSERT_EQ(DEVICE_ID, mDevice->getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002685 ASSERT_STREQ(DEVICE_NAME, mDevice->getName().c_str());
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002686 ASSERT_EQ(ftl::Flags<InputDeviceClass>(0), mDevice->getClasses());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002687}
2688
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002689TEST_F(InputDeviceTest, WhenDeviceCreated_EnabledIsFalse) {
2690 ASSERT_EQ(mDevice->isEnabled(), false);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07002691}
2692
Michael Wrightd02c5b62014-02-10 15:10:22 -08002693TEST_F(InputDeviceTest, WhenNoMappersAreRegistered_DeviceIsIgnored) {
2694 // Configuration.
2695 InputReaderConfiguration config;
2696 mDevice->configure(ARBITRARY_TIME, &config, 0);
2697
2698 // Reset.
2699 mDevice->reset(ARBITRARY_TIME);
2700
2701 NotifyDeviceResetArgs resetArgs;
2702 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2703 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2704 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2705
2706 // Metadata.
2707 ASSERT_TRUE(mDevice->isIgnored());
2708 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mDevice->getSources());
2709
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002710 InputDeviceInfo info = mDevice->getDeviceInfo();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002711 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002712 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002713 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NONE, info.getKeyboardType());
2714 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, info.getSources());
2715
2716 // State queries.
2717 ASSERT_EQ(0, mDevice->getMetaState());
2718
2719 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2720 << "Ignored device should return unknown key code state.";
2721 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2722 << "Ignored device should return unknown scan code state.";
2723 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 0))
2724 << "Ignored device should return unknown switch state.";
2725
2726 const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
2727 uint8_t flags[2] = { 0, 1 };
2728 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, 2, keyCodes, flags))
2729 << "Ignored device should never mark any key codes.";
2730 ASSERT_EQ(0, flags[0]) << "Flag for unsupported key should be unchanged.";
2731 ASSERT_EQ(1, flags[1]) << "Flag for unsupported key should be unchanged.";
2732}
2733
2734TEST_F(InputDeviceTest, WhenMappersAreRegistered_DeviceIsNotIgnoredAndForwardsRequestsToMappers) {
2735 // Configuration.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002736 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, String8("key"), String8("value"));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002737
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002738 FakeInputMapper& mapper1 =
2739 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002740 mapper1.setKeyboardType(AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2741 mapper1.setMetaState(AMETA_ALT_ON);
2742 mapper1.addSupportedKeyCode(AKEYCODE_A);
2743 mapper1.addSupportedKeyCode(AKEYCODE_B);
2744 mapper1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
2745 mapper1.setKeyCodeState(AKEYCODE_B, AKEY_STATE_UP);
2746 mapper1.setScanCodeState(2, AKEY_STATE_DOWN);
2747 mapper1.setScanCodeState(3, AKEY_STATE_UP);
2748 mapper1.setSwitchState(4, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002749
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002750 FakeInputMapper& mapper2 =
2751 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002752 mapper2.setMetaState(AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002753
2754 InputReaderConfiguration config;
2755 mDevice->configure(ARBITRARY_TIME, &config, 0);
2756
2757 String8 propertyValue;
2758 ASSERT_TRUE(mDevice->getConfiguration().tryGetProperty(String8("key"), propertyValue))
2759 << "Device should have read configuration during configuration phase.";
2760 ASSERT_STREQ("value", propertyValue.string());
2761
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002762 ASSERT_NO_FATAL_FAILURE(mapper1.assertConfigureWasCalled());
2763 ASSERT_NO_FATAL_FAILURE(mapper2.assertConfigureWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002764
2765 // Reset
2766 mDevice->reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002767 ASSERT_NO_FATAL_FAILURE(mapper1.assertResetWasCalled());
2768 ASSERT_NO_FATAL_FAILURE(mapper2.assertResetWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002769
2770 NotifyDeviceResetArgs resetArgs;
2771 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2772 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2773 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2774
2775 // Metadata.
2776 ASSERT_FALSE(mDevice->isIgnored());
2777 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), mDevice->getSources());
2778
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002779 InputDeviceInfo info = mDevice->getDeviceInfo();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002780 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002781 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002782 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_ALPHABETIC, info.getKeyboardType());
2783 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), info.getSources());
2784
2785 // State queries.
2786 ASSERT_EQ(AMETA_ALT_ON | AMETA_SHIFT_ON, mDevice->getMetaState())
2787 << "Should query mappers and combine meta states.";
2788
2789 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2790 << "Should return unknown key code state when source not supported.";
2791 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2792 << "Should return unknown scan code state when source not supported.";
2793 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2794 << "Should return unknown switch state when source not supported.";
2795
2796 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, AKEYCODE_A))
2797 << "Should query mapper when source is supported.";
2798 ASSERT_EQ(AKEY_STATE_UP, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 3))
2799 << "Should query mapper when source is supported.";
2800 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 4))
2801 << "Should query mapper when source is supported.";
2802
2803 const int32_t keyCodes[4] = { AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2 };
2804 uint8_t flags[4] = { 0, 0, 0, 1 };
2805 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
2806 << "Should do nothing when source is unsupported.";
2807 ASSERT_EQ(0, flags[0]) << "Flag should be unchanged when source is unsupported.";
2808 ASSERT_EQ(0, flags[1]) << "Flag should be unchanged when source is unsupported.";
2809 ASSERT_EQ(0, flags[2]) << "Flag should be unchanged when source is unsupported.";
2810 ASSERT_EQ(1, flags[3]) << "Flag should be unchanged when source is unsupported.";
2811
2812 ASSERT_TRUE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, 4, keyCodes, flags))
2813 << "Should query mapper when source is supported.";
2814 ASSERT_EQ(1, flags[0]) << "Flag for supported key should be set.";
2815 ASSERT_EQ(1, flags[1]) << "Flag for supported key should be set.";
2816 ASSERT_EQ(0, flags[2]) << "Flag for unsupported key should be unchanged.";
2817 ASSERT_EQ(1, flags[3]) << "Flag for unsupported key should be unchanged.";
2818
2819 // Event handling.
2820 RawEvent event;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002821 event.deviceId = EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002822 mDevice->process(&event, 1);
2823
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002824 ASSERT_NO_FATAL_FAILURE(mapper1.assertProcessWasCalled());
2825 ASSERT_NO_FATAL_FAILURE(mapper2.assertProcessWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002826}
2827
Arthur Hung2c9a3342019-07-23 14:18:59 +08002828// A single input device is associated with a specific display. Check that:
2829// 1. Device is disabled if the viewport corresponding to the associated display is not found
2830// 2. Device is disabled when setEnabled API is called
2831TEST_F(InputDeviceTest, Configure_AssignsDisplayPort) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002832 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002833
2834 // First Configuration.
2835 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
2836
2837 // Device should be enabled by default.
2838 ASSERT_TRUE(mDevice->isEnabled());
2839
2840 // Prepare associated info.
2841 constexpr uint8_t hdmi = 1;
2842 const std::string UNIQUE_ID = "local:1";
2843
2844 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi);
2845 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2846 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2847 // Device should be disabled because it is associated with a specific display via
2848 // input port <-> display port association, but the corresponding display is not found
2849 ASSERT_FALSE(mDevice->isEnabled());
2850
2851 // Prepare displays.
2852 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002853 DISPLAY_ORIENTATION_0, true /*isActive*/, UNIQUE_ID, hdmi,
2854 ViewportType::INTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002855 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2856 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2857 ASSERT_TRUE(mDevice->isEnabled());
2858
2859 // Device should be disabled after set disable.
2860 mFakePolicy->addDisabledDevice(mDevice->getId());
2861 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2862 InputReaderConfiguration::CHANGE_ENABLED_STATE);
2863 ASSERT_FALSE(mDevice->isEnabled());
2864
2865 // Device should still be disabled even found the associated display.
2866 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2867 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2868 ASSERT_FALSE(mDevice->isEnabled());
2869}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002870
Christine Franks1ba71cc2021-04-07 14:37:42 -07002871TEST_F(InputDeviceTest, Configure_AssignsDisplayUniqueId) {
2872 // Device should be enabled by default.
2873 mFakePolicy->clearViewports();
2874 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
2875 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
2876 ASSERT_TRUE(mDevice->isEnabled());
2877
2878 // Device should be disabled because it is associated with a specific display, but the
2879 // corresponding display is not found.
2880 const std::string DISPLAY_UNIQUE_ID = "displayUniqueId";
Christine Franks2a2293c2022-01-18 11:51:16 -08002881 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002882 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2883 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2884 ASSERT_FALSE(mDevice->isEnabled());
2885
2886 // Device should be enabled when a display is found.
2887 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2888 DISPLAY_ORIENTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
2889 NO_PORT, ViewportType::INTERNAL);
2890 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2891 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2892 ASSERT_TRUE(mDevice->isEnabled());
2893
2894 // Device should be disabled after set disable.
2895 mFakePolicy->addDisabledDevice(mDevice->getId());
2896 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2897 InputReaderConfiguration::CHANGE_ENABLED_STATE);
2898 ASSERT_FALSE(mDevice->isEnabled());
2899
2900 // Device should still be disabled even found the associated display.
2901 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2902 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2903 ASSERT_FALSE(mDevice->isEnabled());
2904}
2905
Christine Franks2a2293c2022-01-18 11:51:16 -08002906TEST_F(InputDeviceTest, Configure_UniqueId_CorrectlyMatches) {
2907 mFakePolicy->clearViewports();
2908 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
2909 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
2910
2911 const std::string DISPLAY_UNIQUE_ID = "displayUniqueId";
2912 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
2913 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2914 DISPLAY_ORIENTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
2915 NO_PORT, ViewportType::INTERNAL);
2916 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2917 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2918 ASSERT_EQ(DISPLAY_UNIQUE_ID, mDevice->getAssociatedDisplayUniqueId());
2919}
2920
Michael Wrightd02c5b62014-02-10 15:10:22 -08002921// --- InputMapperTest ---
2922
2923class InputMapperTest : public testing::Test {
2924protected:
2925 static const char* DEVICE_NAME;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002926 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002927 static const int32_t DEVICE_ID;
2928 static const int32_t DEVICE_GENERATION;
2929 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002930 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002931 static const int32_t EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002932
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002933 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002934 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002935 std::unique_ptr<TestInputListener> mFakeListener;
arthurhungdcef2dc2020-08-11 14:47:50 +08002936 std::unique_ptr<InstrumentedInputReader> mReader;
2937 std::shared_ptr<InputDevice> mDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002938
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002939 virtual void SetUp(ftl::Flags<InputDeviceClass> classes) {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002940 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002941 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002942 mFakeListener = std::make_unique<TestInputListener>();
arthurhungdcef2dc2020-08-11 14:47:50 +08002943 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002944 *mFakeListener);
arthurhungdcef2dc2020-08-11 14:47:50 +08002945 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002946 }
2947
Prabir Pradhanc14266f2021-05-12 15:56:24 -07002948 void SetUp() override {
Prabir Pradhanc14266f2021-05-12 15:56:24 -07002949 SetUp(DEVICE_CLASSES);
2950 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002951
Chris Yea52ade12020-08-27 16:49:20 -07002952 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002953 mFakeListener.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002954 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002955 }
2956
2957 void addConfigurationProperty(const char* key, const char* value) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002958 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, String8(key), String8(value));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002959 }
2960
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002961 void configureDevice(uint32_t changes) {
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00002962 if (!changes ||
2963 (changes &
2964 (InputReaderConfiguration::CHANGE_DISPLAY_INFO |
2965 InputReaderConfiguration::CHANGE_POINTER_CAPTURE))) {
arthurhungdcef2dc2020-08-11 14:47:50 +08002966 mReader->requestRefreshConfiguration(changes);
2967 mReader->loopOnce();
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -08002968 }
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002969 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
2970 }
2971
arthurhungdcef2dc2020-08-11 14:47:50 +08002972 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
2973 const std::string& location, int32_t eventHubId,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002974 ftl::Flags<InputDeviceClass> classes) {
arthurhungdcef2dc2020-08-11 14:47:50 +08002975 InputDeviceIdentifier identifier;
2976 identifier.name = name;
2977 identifier.location = location;
2978 std::shared_ptr<InputDevice> device =
2979 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
2980 identifier);
2981 mReader->pushNextDevice(device);
2982 mFakeEventHub->addDevice(eventHubId, name, classes);
2983 mReader->loopOnce();
2984 return device;
2985 }
2986
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002987 template <class T, typename... Args>
2988 T& addMapperAndConfigure(Args... args) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002989 T& mapper = mDevice->addMapper<T>(EVENTHUB_ID, args...);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002990 configureDevice(0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002991 mDevice->reset(ARBITRARY_TIME);
Chris Ye42b06822020-08-07 11:39:33 -07002992 mapper.reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002993 return mapper;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002994 }
2995
2996 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002997 int32_t orientation, const std::string& uniqueId,
2998 std::optional<uint8_t> physicalPort, ViewportType viewportType) {
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002999 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, true /*isActive*/,
3000 uniqueId, physicalPort, viewportType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003001 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3002 }
3003
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003004 void clearViewports() {
3005 mFakePolicy->clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003006 }
3007
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003008 void process(InputMapper& mapper, nsecs_t when, nsecs_t readTime, int32_t type, int32_t code,
3009 int32_t value) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003010 RawEvent event;
3011 event.when = when;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003012 event.readTime = readTime;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003013 event.deviceId = mapper.getDeviceContext().getEventHubId();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003014 event.type = type;
3015 event.code = code;
3016 event.value = value;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003017 mapper.process(&event);
arthurhungdcef2dc2020-08-11 14:47:50 +08003018 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003019 }
3020
3021 static void assertMotionRange(const InputDeviceInfo& info,
3022 int32_t axis, uint32_t source, float min, float max, float flat, float fuzz) {
3023 const InputDeviceInfo::MotionRange* range = info.getMotionRange(axis, source);
Yi Kong9b14ac62018-07-17 13:48:38 -07003024 ASSERT_TRUE(range != nullptr) << "Axis: " << axis << " Source: " << source;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003025 ASSERT_EQ(axis, range->axis) << "Axis: " << axis << " Source: " << source;
3026 ASSERT_EQ(source, range->source) << "Axis: " << axis << " Source: " << source;
3027 ASSERT_NEAR(min, range->min, EPSILON) << "Axis: " << axis << " Source: " << source;
3028 ASSERT_NEAR(max, range->max, EPSILON) << "Axis: " << axis << " Source: " << source;
3029 ASSERT_NEAR(flat, range->flat, EPSILON) << "Axis: " << axis << " Source: " << source;
3030 ASSERT_NEAR(fuzz, range->fuzz, EPSILON) << "Axis: " << axis << " Source: " << source;
3031 }
3032
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003033 static void assertPointerCoords(const PointerCoords& coords, float x, float y, float pressure,
3034 float size, float touchMajor, float touchMinor, float toolMajor,
3035 float toolMinor, float orientation, float distance,
3036 float scaledAxisEpsilon = 1.f) {
3037 ASSERT_NEAR(x, coords.getAxisValue(AMOTION_EVENT_AXIS_X), scaledAxisEpsilon);
3038 ASSERT_NEAR(y, coords.getAxisValue(AMOTION_EVENT_AXIS_Y), scaledAxisEpsilon);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003039 ASSERT_NEAR(pressure, coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), EPSILON);
3040 ASSERT_NEAR(size, coords.getAxisValue(AMOTION_EVENT_AXIS_SIZE), EPSILON);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003041 ASSERT_NEAR(touchMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR),
3042 scaledAxisEpsilon);
3043 ASSERT_NEAR(touchMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR),
3044 scaledAxisEpsilon);
3045 ASSERT_NEAR(toolMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR),
3046 scaledAxisEpsilon);
3047 ASSERT_NEAR(toolMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR),
3048 scaledAxisEpsilon);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003049 ASSERT_NEAR(orientation, coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION), EPSILON);
3050 ASSERT_NEAR(distance, coords.getAxisValue(AMOTION_EVENT_AXIS_DISTANCE), EPSILON);
3051 }
3052
Michael Wright17db18e2020-06-26 20:51:44 +01003053 static void assertPosition(const FakePointerController& controller, float x, float y) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003054 float actualX, actualY;
Michael Wright17db18e2020-06-26 20:51:44 +01003055 controller.getPosition(&actualX, &actualY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003056 ASSERT_NEAR(x, actualX, 1);
3057 ASSERT_NEAR(y, actualY, 1);
3058 }
3059};
3060
3061const char* InputMapperTest::DEVICE_NAME = "device";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003062const char* InputMapperTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003063const int32_t InputMapperTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003064const int32_t InputMapperTest::DEVICE_GENERATION = 2;
3065const int32_t InputMapperTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003066const ftl::Flags<InputDeviceClass> InputMapperTest::DEVICE_CLASSES =
3067 ftl::Flags<InputDeviceClass>(0); // not needed for current tests
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003068const int32_t InputMapperTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003069
3070// --- SwitchInputMapperTest ---
3071
3072class SwitchInputMapperTest : public InputMapperTest {
3073protected:
3074};
3075
3076TEST_F(SwitchInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003077 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003078
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003079 ASSERT_EQ(uint32_t(AINPUT_SOURCE_SWITCH), mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003080}
3081
3082TEST_F(SwitchInputMapperTest, GetSwitchState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003083 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003084
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003085 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003086 ASSERT_EQ(1, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003087
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003088 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003089 ASSERT_EQ(0, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003090}
3091
3092TEST_F(SwitchInputMapperTest, Process) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003093 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003094
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003095 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_LID, 1);
3096 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_JACK_PHYSICAL_INSERT, 1);
3097 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_HEADPHONE_INSERT, 0);
3098 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003099
3100 NotifySwitchArgs args;
3101 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySwitchWasCalled(&args));
3102 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
Dan Albert1bd2fc02016-02-02 15:11:57 -08003103 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT), args.switchValues);
3104 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT) | (1 << SW_HEADPHONE_INSERT),
Michael Wrightd02c5b62014-02-10 15:10:22 -08003105 args.switchMask);
3106 ASSERT_EQ(uint32_t(0), args.policyFlags);
3107}
3108
Chris Ye87143712020-11-10 05:05:58 +00003109// --- VibratorInputMapperTest ---
3110class VibratorInputMapperTest : public InputMapperTest {
3111protected:
3112 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::VIBRATOR); }
3113};
3114
3115TEST_F(VibratorInputMapperTest, GetSources) {
3116 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
3117
3118 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mapper.getSources());
3119}
3120
3121TEST_F(VibratorInputMapperTest, GetVibratorIds) {
3122 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
3123
3124 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
3125}
3126
3127TEST_F(VibratorInputMapperTest, Vibrate) {
3128 constexpr uint8_t DEFAULT_AMPLITUDE = 192;
Chris Yefb552902021-02-03 17:18:37 -08003129 constexpr int32_t VIBRATION_TOKEN = 100;
Chris Ye87143712020-11-10 05:05:58 +00003130 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
3131
3132 VibrationElement pattern(2);
3133 VibrationSequence sequence(2);
3134 pattern.duration = std::chrono::milliseconds(200);
3135 pattern.channels = {{0 /* vibratorId */, DEFAULT_AMPLITUDE / 2},
3136 {1 /* vibratorId */, DEFAULT_AMPLITUDE}};
3137 sequence.addElement(pattern);
3138 pattern.duration = std::chrono::milliseconds(500);
3139 pattern.channels = {{0 /* vibratorId */, DEFAULT_AMPLITUDE / 4},
3140 {1 /* vibratorId */, DEFAULT_AMPLITUDE}};
3141 sequence.addElement(pattern);
3142
3143 std::vector<int64_t> timings = {0, 1};
3144 std::vector<uint8_t> amplitudes = {DEFAULT_AMPLITUDE, DEFAULT_AMPLITUDE / 2};
3145
3146 ASSERT_FALSE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08003147 // Start vibrating
3148 mapper.vibrate(sequence, -1 /* repeat */, VIBRATION_TOKEN);
Chris Ye87143712020-11-10 05:05:58 +00003149 ASSERT_TRUE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08003150 // Verify vibrator state listener was notified.
3151 mReader->loopOnce();
3152 NotifyVibratorStateArgs args;
3153 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyVibratorStateWasCalled(&args));
3154 ASSERT_EQ(DEVICE_ID, args.deviceId);
3155 ASSERT_TRUE(args.isOn);
3156 // Stop vibrating
3157 mapper.cancelVibrate(VIBRATION_TOKEN);
3158 ASSERT_FALSE(mapper.isVibrating());
3159 // Verify vibrator state listener was notified.
3160 mReader->loopOnce();
3161 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyVibratorStateWasCalled(&args));
3162 ASSERT_EQ(DEVICE_ID, args.deviceId);
3163 ASSERT_FALSE(args.isOn);
Chris Ye87143712020-11-10 05:05:58 +00003164}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003165
Chris Yef59a2f42020-10-16 12:55:26 -07003166// --- SensorInputMapperTest ---
3167
3168class SensorInputMapperTest : public InputMapperTest {
3169protected:
3170 static const int32_t ACCEL_RAW_MIN;
3171 static const int32_t ACCEL_RAW_MAX;
3172 static const int32_t ACCEL_RAW_FUZZ;
3173 static const int32_t ACCEL_RAW_FLAT;
3174 static const int32_t ACCEL_RAW_RESOLUTION;
3175
3176 static const int32_t GYRO_RAW_MIN;
3177 static const int32_t GYRO_RAW_MAX;
3178 static const int32_t GYRO_RAW_FUZZ;
3179 static const int32_t GYRO_RAW_FLAT;
3180 static const int32_t GYRO_RAW_RESOLUTION;
3181
3182 static const float GRAVITY_MS2_UNIT;
3183 static const float DEGREE_RADIAN_UNIT;
3184
3185 void prepareAccelAxes();
3186 void prepareGyroAxes();
3187 void setAccelProperties();
3188 void setGyroProperties();
3189 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::SENSOR); }
3190};
3191
3192const int32_t SensorInputMapperTest::ACCEL_RAW_MIN = -32768;
3193const int32_t SensorInputMapperTest::ACCEL_RAW_MAX = 32768;
3194const int32_t SensorInputMapperTest::ACCEL_RAW_FUZZ = 16;
3195const int32_t SensorInputMapperTest::ACCEL_RAW_FLAT = 0;
3196const int32_t SensorInputMapperTest::ACCEL_RAW_RESOLUTION = 8192;
3197
3198const int32_t SensorInputMapperTest::GYRO_RAW_MIN = -2097152;
3199const int32_t SensorInputMapperTest::GYRO_RAW_MAX = 2097152;
3200const int32_t SensorInputMapperTest::GYRO_RAW_FUZZ = 16;
3201const int32_t SensorInputMapperTest::GYRO_RAW_FLAT = 0;
3202const int32_t SensorInputMapperTest::GYRO_RAW_RESOLUTION = 1024;
3203
3204const float SensorInputMapperTest::GRAVITY_MS2_UNIT = 9.80665f;
3205const float SensorInputMapperTest::DEGREE_RADIAN_UNIT = 0.0174533f;
3206
3207void SensorInputMapperTest::prepareAccelAxes() {
3208 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3209 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3210 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3211 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3212 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Z, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3213 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3214}
3215
3216void SensorInputMapperTest::prepareGyroAxes() {
3217 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RX, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3218 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3219 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RY, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3220 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3221 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RZ, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3222 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3223}
3224
3225void SensorInputMapperTest::setAccelProperties() {
3226 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 0, InputDeviceSensorType::ACCELEROMETER,
3227 /* sensorDataIndex */ 0);
3228 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 1, InputDeviceSensorType::ACCELEROMETER,
3229 /* sensorDataIndex */ 1);
3230 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 2, InputDeviceSensorType::ACCELEROMETER,
3231 /* sensorDataIndex */ 2);
3232 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
3233 addConfigurationProperty("sensor.accelerometer.reportingMode", "0");
3234 addConfigurationProperty("sensor.accelerometer.maxDelay", "100000");
3235 addConfigurationProperty("sensor.accelerometer.minDelay", "5000");
3236 addConfigurationProperty("sensor.accelerometer.power", "1.5");
3237}
3238
3239void SensorInputMapperTest::setGyroProperties() {
3240 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 3, InputDeviceSensorType::GYROSCOPE,
3241 /* sensorDataIndex */ 0);
3242 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 4, InputDeviceSensorType::GYROSCOPE,
3243 /* sensorDataIndex */ 1);
3244 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 5, InputDeviceSensorType::GYROSCOPE,
3245 /* sensorDataIndex */ 2);
3246 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
3247 addConfigurationProperty("sensor.gyroscope.reportingMode", "0");
3248 addConfigurationProperty("sensor.gyroscope.maxDelay", "100000");
3249 addConfigurationProperty("sensor.gyroscope.minDelay", "5000");
3250 addConfigurationProperty("sensor.gyroscope.power", "0.8");
3251}
3252
3253TEST_F(SensorInputMapperTest, GetSources) {
3254 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3255
3256 ASSERT_EQ(static_cast<uint32_t>(AINPUT_SOURCE_SENSOR), mapper.getSources());
3257}
3258
3259TEST_F(SensorInputMapperTest, ProcessAccelerometerSensor) {
3260 setAccelProperties();
3261 prepareAccelAxes();
3262 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3263
3264 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::ACCELEROMETER,
3265 std::chrono::microseconds(10000),
3266 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08003267 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003268 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, 20000);
3269 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, -20000);
3270 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Z, 40000);
3271 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
3272 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07003273
3274 NotifySensorArgs args;
3275 std::vector<float> values = {20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
3276 -20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
3277 40000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT};
3278
3279 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
3280 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
3281 ASSERT_EQ(args.deviceId, DEVICE_ID);
3282 ASSERT_EQ(args.sensorType, InputDeviceSensorType::ACCELEROMETER);
3283 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
3284 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
3285 ASSERT_EQ(args.values, values);
3286 mapper.flushSensor(InputDeviceSensorType::ACCELEROMETER);
3287}
3288
3289TEST_F(SensorInputMapperTest, ProcessGyroscopeSensor) {
3290 setGyroProperties();
3291 prepareGyroAxes();
3292 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3293
3294 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::GYROSCOPE,
3295 std::chrono::microseconds(10000),
3296 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08003297 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003298 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RX, 20000);
3299 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RY, -20000);
3300 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RZ, 40000);
3301 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
3302 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07003303
3304 NotifySensorArgs args;
3305 std::vector<float> values = {20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
3306 -20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
3307 40000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT};
3308
3309 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
3310 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
3311 ASSERT_EQ(args.deviceId, DEVICE_ID);
3312 ASSERT_EQ(args.sensorType, InputDeviceSensorType::GYROSCOPE);
3313 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
3314 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
3315 ASSERT_EQ(args.values, values);
3316 mapper.flushSensor(InputDeviceSensorType::GYROSCOPE);
3317}
3318
Michael Wrightd02c5b62014-02-10 15:10:22 -08003319// --- KeyboardInputMapperTest ---
3320
3321class KeyboardInputMapperTest : public InputMapperTest {
3322protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003323 const std::string UNIQUE_ID = "local:0";
3324
3325 void prepareDisplay(int32_t orientation);
3326
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003327 void testDPadKeyRotation(KeyboardInputMapper& mapper, int32_t originalScanCode,
Arthur Hung2c9a3342019-07-23 14:18:59 +08003328 int32_t originalKeyCode, int32_t rotatedKeyCode,
3329 int32_t displayId = ADISPLAY_ID_NONE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003330};
3331
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003332/* Similar to setDisplayInfoAndReconfigure, but pre-populates all parameters except for the
3333 * orientation.
3334 */
3335void KeyboardInputMapperTest::prepareDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003336 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
3337 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003338}
3339
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003340void KeyboardInputMapperTest::testDPadKeyRotation(KeyboardInputMapper& mapper,
Arthur Hung2c9a3342019-07-23 14:18:59 +08003341 int32_t originalScanCode, int32_t originalKeyCode,
3342 int32_t rotatedKeyCode, int32_t displayId) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003343 NotifyKeyArgs args;
3344
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003345 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003346 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3347 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3348 ASSERT_EQ(originalScanCode, args.scanCode);
3349 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003350 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003351
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003352 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003353 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3354 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3355 ASSERT_EQ(originalScanCode, args.scanCode);
3356 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003357 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003358}
3359
Michael Wrightd02c5b62014-02-10 15:10:22 -08003360TEST_F(KeyboardInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003361 KeyboardInputMapper& mapper =
3362 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3363 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003364
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003365 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003366}
3367
3368TEST_F(KeyboardInputMapperTest, Process_SimpleKeyPress) {
3369 const int32_t USAGE_A = 0x070004;
3370 const int32_t USAGE_UNKNOWN = 0x07ffff;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003371 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3372 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
Chris Yea52ade12020-08-27 16:49:20 -07003373 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, POLICY_FLAG_WAKE);
3374 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, POLICY_FLAG_WAKE);
3375 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003376
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003377 KeyboardInputMapper& mapper =
3378 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3379 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003380 // Initial metastate is AMETA_NONE.
3381 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003382
3383 // Key down by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003384 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003385 NotifyKeyArgs args;
3386 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3387 ASSERT_EQ(DEVICE_ID, args.deviceId);
3388 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3389 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3390 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3391 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3392 ASSERT_EQ(KEY_HOME, args.scanCode);
3393 ASSERT_EQ(AMETA_NONE, args.metaState);
3394 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3395 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3396 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3397
3398 // Key up by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003399 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003400 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3401 ASSERT_EQ(DEVICE_ID, args.deviceId);
3402 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3403 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3404 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3405 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3406 ASSERT_EQ(KEY_HOME, args.scanCode);
3407 ASSERT_EQ(AMETA_NONE, args.metaState);
3408 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3409 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3410 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3411
3412 // Key down by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003413 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3414 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, 0, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003415 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3416 ASSERT_EQ(DEVICE_ID, args.deviceId);
3417 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3418 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3419 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3420 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3421 ASSERT_EQ(0, args.scanCode);
3422 ASSERT_EQ(AMETA_NONE, args.metaState);
3423 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3424 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3425 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3426
3427 // Key up by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003428 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3429 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003430 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3431 ASSERT_EQ(DEVICE_ID, args.deviceId);
3432 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3433 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3434 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3435 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3436 ASSERT_EQ(0, args.scanCode);
3437 ASSERT_EQ(AMETA_NONE, args.metaState);
3438 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3439 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3440 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3441
3442 // Key down with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003443 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3444 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UNKNOWN, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003445 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3446 ASSERT_EQ(DEVICE_ID, args.deviceId);
3447 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3448 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3449 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3450 ASSERT_EQ(0, args.keyCode);
3451 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3452 ASSERT_EQ(AMETA_NONE, args.metaState);
3453 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3454 ASSERT_EQ(0U, args.policyFlags);
3455 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3456
3457 // Key up with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003458 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3459 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003460 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3461 ASSERT_EQ(DEVICE_ID, args.deviceId);
3462 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3463 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3464 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3465 ASSERT_EQ(0, args.keyCode);
3466 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3467 ASSERT_EQ(AMETA_NONE, args.metaState);
3468 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3469 ASSERT_EQ(0U, args.policyFlags);
3470 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3471}
3472
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003473/**
3474 * Ensure that the readTime is set to the time when the EV_KEY is received.
3475 */
3476TEST_F(KeyboardInputMapperTest, Process_SendsReadTime) {
3477 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3478
3479 KeyboardInputMapper& mapper =
3480 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3481 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3482 NotifyKeyArgs args;
3483
3484 // Key down
3485 process(mapper, ARBITRARY_TIME, 12 /*readTime*/, EV_KEY, KEY_HOME, 1);
3486 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3487 ASSERT_EQ(12, args.readTime);
3488
3489 // Key up
3490 process(mapper, ARBITRARY_TIME, 15 /*readTime*/, EV_KEY, KEY_HOME, 1);
3491 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3492 ASSERT_EQ(15, args.readTime);
3493}
3494
Michael Wrightd02c5b62014-02-10 15:10:22 -08003495TEST_F(KeyboardInputMapperTest, Process_ShouldUpdateMetaState) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003496 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFTSHIFT, 0, AKEYCODE_SHIFT_LEFT, 0);
3497 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003498 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, 0);
3499 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, 0);
3500 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003501
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003502 KeyboardInputMapper& mapper =
3503 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3504 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003505
Arthur Hung95f68612022-04-07 14:08:22 +08003506 // Initial metastate is AMETA_NONE.
3507 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003508
3509 // Metakey down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003510 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003511 NotifyKeyArgs args;
3512 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3513 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003514 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003515 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003516
3517 // Key down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003518 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_A, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003519 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3520 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003521 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003522
3523 // Key up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003524 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, KEY_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003525 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3526 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003527 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003528
3529 // Metakey up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003530 process(mapper, ARBITRARY_TIME + 3, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003531 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3532 ASSERT_EQ(AMETA_NONE, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003533 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003534 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003535}
3536
3537TEST_F(KeyboardInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003538 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3539 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3540 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3541 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003542
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003543 KeyboardInputMapper& mapper =
3544 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3545 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003546
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003547 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003548 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3549 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
3550 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3551 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
3552 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3553 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
3554 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3555 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
3556}
3557
3558TEST_F(KeyboardInputMapperTest, Process_WhenOrientationAware_ShouldRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003559 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3560 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3561 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3562 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003563
Michael Wrightd02c5b62014-02-10 15:10:22 -08003564 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003565 KeyboardInputMapper& mapper =
3566 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3567 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003568
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003569 prepareDisplay(DISPLAY_ORIENTATION_0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003570 ASSERT_NO_FATAL_FAILURE(
3571 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3572 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3573 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3574 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3575 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3576 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3577 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003578
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003579 clearViewports();
3580 prepareDisplay(DISPLAY_ORIENTATION_90);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003581 ASSERT_NO_FATAL_FAILURE(
3582 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3583 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3584 AKEYCODE_DPAD_UP, DISPLAY_ID));
3585 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3586 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3587 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3588 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003589
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003590 clearViewports();
3591 prepareDisplay(DISPLAY_ORIENTATION_180);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003592 ASSERT_NO_FATAL_FAILURE(
3593 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3594 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3595 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3596 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3597 AKEYCODE_DPAD_UP, DISPLAY_ID));
3598 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3599 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003600
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003601 clearViewports();
3602 prepareDisplay(DISPLAY_ORIENTATION_270);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003603 ASSERT_NO_FATAL_FAILURE(
3604 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3605 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3606 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3607 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3608 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3609 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3610 AKEYCODE_DPAD_UP, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003611
3612 // Special case: if orientation changes while key is down, we still emit the same keycode
3613 // in the key up as we did in the key down.
3614 NotifyKeyArgs args;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003615 clearViewports();
3616 prepareDisplay(DISPLAY_ORIENTATION_270);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003617 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003618 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3619 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3620 ASSERT_EQ(KEY_UP, args.scanCode);
3621 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3622
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003623 clearViewports();
3624 prepareDisplay(DISPLAY_ORIENTATION_180);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003625 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003626 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3627 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3628 ASSERT_EQ(KEY_UP, args.scanCode);
3629 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3630}
3631
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003632TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_NotOrientationAware) {
3633 // If the keyboard is not orientation aware,
3634 // key events should not be associated with a specific display id
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003635 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003636
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003637 KeyboardInputMapper& mapper =
3638 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3639 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003640 NotifyKeyArgs args;
3641
3642 // Display id should be ADISPLAY_ID_NONE without any display configuration.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003643 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003644 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003645 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003646 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3647 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3648
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003649 prepareDisplay(DISPLAY_ORIENTATION_0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003650 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003651 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003652 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003653 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3654 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3655}
3656
3657TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_OrientationAware) {
3658 // If the keyboard is orientation aware,
3659 // key events should be associated with the internal viewport
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003660 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003661
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003662 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003663 KeyboardInputMapper& mapper =
3664 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3665 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003666 NotifyKeyArgs args;
3667
3668 // Display id should be ADISPLAY_ID_NONE without any display configuration.
3669 // ^--- already checked by the previous test
3670
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003671 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003672 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003673 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003674 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003675 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003676 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3677 ASSERT_EQ(DISPLAY_ID, args.displayId);
3678
3679 constexpr int32_t newDisplayId = 2;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003680 clearViewports();
3681 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003682 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003683 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003684 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003685 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003686 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3687 ASSERT_EQ(newDisplayId, args.displayId);
3688}
3689
Michael Wrightd02c5b62014-02-10 15:10:22 -08003690TEST_F(KeyboardInputMapperTest, GetKeyCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003691 KeyboardInputMapper& mapper =
3692 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3693 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003694
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003695 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003696 ASSERT_EQ(1, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003697
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003698 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003699 ASSERT_EQ(0, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003700}
3701
Philip Junker4af3b3d2021-12-14 10:36:55 +01003702TEST_F(KeyboardInputMapperTest, GetKeyCodeForKeyLocation) {
3703 KeyboardInputMapper& mapper =
3704 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3705 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3706
3707 mFakeEventHub->addKeyCodeMapping(EVENTHUB_ID, AKEYCODE_Y, AKEYCODE_Z);
3708 ASSERT_EQ(AKEYCODE_Z, mapper.getKeyCodeForKeyLocation(AKEYCODE_Y))
3709 << "If a mapping is available, the result is equal to the mapping";
3710
3711 ASSERT_EQ(AKEYCODE_A, mapper.getKeyCodeForKeyLocation(AKEYCODE_A))
3712 << "If no mapping is available, the result is the key location";
3713}
3714
Michael Wrightd02c5b62014-02-10 15:10:22 -08003715TEST_F(KeyboardInputMapperTest, GetScanCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003716 KeyboardInputMapper& mapper =
3717 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3718 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003719
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003720 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003721 ASSERT_EQ(1, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003722
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003723 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003724 ASSERT_EQ(0, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003725}
3726
3727TEST_F(KeyboardInputMapperTest, MarkSupportedKeyCodes) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003728 KeyboardInputMapper& mapper =
3729 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3730 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003731
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003732 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003733
3734 const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
3735 uint8_t flags[2] = { 0, 0 };
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003736 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, 1, keyCodes, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003737 ASSERT_TRUE(flags[0]);
3738 ASSERT_FALSE(flags[1]);
3739}
3740
3741TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleMetaStateAndLeds) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003742 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3743 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3744 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3745 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3746 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3747 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003748
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003749 KeyboardInputMapper& mapper =
3750 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3751 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003752 // Initial metastate is AMETA_NONE.
3753 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003754
3755 // Initialization should have turned all of the lights off.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003756 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3757 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3758 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003759
3760 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003761 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3762 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003763 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3764 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3765 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003766 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003767
3768 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003769 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3770 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003771 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3772 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3773 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003774 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003775
3776 // Toggle caps lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003777 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3778 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003779 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3780 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3781 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003782 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003783
3784 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003785 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3786 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003787 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3788 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3789 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003790 ASSERT_EQ(AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003791
3792 // Toggle num lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003793 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3794 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003795 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3796 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3797 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003798 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003799
3800 // Toggle scroll lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003801 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3802 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003803 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3804 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3805 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003806 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003807}
3808
Chris Yea52ade12020-08-27 16:49:20 -07003809TEST_F(KeyboardInputMapperTest, NoMetaStateWhenMetaKeysNotPresent) {
3810 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_BUTTON_A, 0);
3811 mFakeEventHub->addKey(EVENTHUB_ID, BTN_B, 0, AKEYCODE_BUTTON_B, 0);
3812 mFakeEventHub->addKey(EVENTHUB_ID, BTN_X, 0, AKEYCODE_BUTTON_X, 0);
3813 mFakeEventHub->addKey(EVENTHUB_ID, BTN_Y, 0, AKEYCODE_BUTTON_Y, 0);
3814
3815 KeyboardInputMapper& mapper =
3816 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3817 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
3818
Chris Yea52ade12020-08-27 16:49:20 -07003819 // Meta state should be AMETA_NONE after reset
3820 mapper.reset(ARBITRARY_TIME);
3821 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3822 // Meta state should be AMETA_NONE with update, as device doesn't have the keys.
3823 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
3824 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3825
3826 NotifyKeyArgs args;
3827 // Press button "A"
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003828 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_A, 1);
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_DOWN, args.action);
3833 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3834
3835 // Button up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003836 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003837 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3838 ASSERT_EQ(AMETA_NONE, args.metaState);
3839 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3840 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3841 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3842}
3843
Arthur Hung2c9a3342019-07-23 14:18:59 +08003844TEST_F(KeyboardInputMapperTest, Configure_AssignsDisplayPort) {
3845 // keyboard 1.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003846 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3847 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3848 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3849 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003850
3851 // keyboard 2.
3852 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08003853 const std::string DEVICE_NAME2 = "KEYBOARD2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08003854 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003855 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08003856 std::shared_ptr<InputDevice> device2 =
3857 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003858 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08003859
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003860 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3861 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3862 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3863 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003864
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003865 KeyboardInputMapper& mapper =
3866 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3867 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003868
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003869 KeyboardInputMapper& mapper2 =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003870 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003871 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003872 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
3873 device2->reset(ARBITRARY_TIME);
3874
3875 // Prepared displays and associated info.
3876 constexpr uint8_t hdmi1 = 0;
3877 constexpr uint8_t hdmi2 = 1;
3878 const std::string SECONDARY_UNIQUE_ID = "local:1";
3879
3880 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
3881 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
3882
3883 // No associated display viewport found, should disable the device.
3884 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3885 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3886 ASSERT_FALSE(device2->isEnabled());
3887
3888 // Prepare second display.
3889 constexpr int32_t newDisplayId = 2;
3890 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003891 UNIQUE_ID, hdmi1, ViewportType::INTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003892 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003893 SECONDARY_UNIQUE_ID, hdmi2, ViewportType::EXTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003894 // Default device will reconfigure above, need additional reconfiguration for another device.
3895 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3896 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3897
3898 // Device should be enabled after the associated display is found.
3899 ASSERT_TRUE(mDevice->isEnabled());
3900 ASSERT_TRUE(device2->isEnabled());
3901
3902 // Test pad key events
3903 ASSERT_NO_FATAL_FAILURE(
3904 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3905 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3906 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3907 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3908 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3909 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3910 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3911
3912 ASSERT_NO_FATAL_FAILURE(
3913 testDPadKeyRotation(mapper2, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, newDisplayId));
3914 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3915 AKEYCODE_DPAD_RIGHT, newDisplayId));
3916 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3917 AKEYCODE_DPAD_DOWN, newDisplayId));
3918 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3919 AKEYCODE_DPAD_LEFT, newDisplayId));
3920}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003921
arthurhungc903df12020-08-11 15:08:42 +08003922TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleAfterReattach) {
3923 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3924 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3925 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3926 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3927 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3928 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3929
3930 KeyboardInputMapper& mapper =
3931 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3932 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003933 // Initial metastate is AMETA_NONE.
3934 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08003935
3936 // Initialization should have turned all of the lights off.
3937 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3938 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3939 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3940
3941 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003942 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3943 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003944 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3945 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
3946
3947 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003948 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3949 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003950 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3951 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
3952
3953 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003954 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3955 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003956 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3957 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
3958
3959 mFakeEventHub->removeDevice(EVENTHUB_ID);
3960 mReader->loopOnce();
3961
3962 // keyboard 2 should default toggle keys.
3963 const std::string USB2 = "USB2";
3964 const std::string DEVICE_NAME2 = "KEYBOARD2";
3965 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
3966 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
3967 std::shared_ptr<InputDevice> device2 =
3968 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003969 ftl::Flags<InputDeviceClass>(0));
arthurhungc903df12020-08-11 15:08:42 +08003970 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3971 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
3972 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3973 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3974 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3975 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3976
arthurhung6fe95782020-10-05 22:41:16 +08003977 KeyboardInputMapper& mapper2 =
3978 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
3979 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
arthurhungc903df12020-08-11 15:08:42 +08003980 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
3981 device2->reset(ARBITRARY_TIME);
3982
3983 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_CAPSL));
3984 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_NUML));
3985 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_SCROLLL));
arthurhung6fe95782020-10-05 22:41:16 +08003986 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON,
3987 mapper2.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08003988}
3989
Arthur Hungcb40a002021-08-03 14:31:01 +00003990TEST_F(KeyboardInputMapperTest, Process_toggleCapsLockState) {
3991 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3992 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3993 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3994
3995 // Suppose we have two mappers. (DPAD + KEYBOARD)
3996 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_DPAD,
3997 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
3998 KeyboardInputMapper& mapper =
3999 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4000 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08004001 // Initial metastate is AMETA_NONE.
4002 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Arthur Hungcb40a002021-08-03 14:31:01 +00004003
4004 mReader->toggleCapsLockState(DEVICE_ID);
4005 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
4006}
4007
Arthur Hungfb3cc112022-04-13 07:39:50 +00004008TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleInMultiDevices) {
4009 // keyboard 1.
4010 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
4011 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
4012 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
4013 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4014 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4015 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
4016
4017 KeyboardInputMapper& mapper1 =
4018 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4019 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
4020
4021 // keyboard 2.
4022 const std::string USB2 = "USB2";
4023 const std::string DEVICE_NAME2 = "KEYBOARD2";
4024 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
4025 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
4026 std::shared_ptr<InputDevice> device2 =
4027 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
4028 ftl::Flags<InputDeviceClass>(0));
4029 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
4030 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
4031 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
4032 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4033 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4034 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
4035
4036 KeyboardInputMapper& mapper2 =
4037 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
4038 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
4039 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
4040 device2->reset(ARBITRARY_TIME);
4041
Arthur Hung95f68612022-04-07 14:08:22 +08004042 // Initial metastate is AMETA_NONE.
4043 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4044 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4045
4046 // Toggle num lock on and off.
4047 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
4048 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Arthur Hungfb3cc112022-04-13 07:39:50 +00004049 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4050 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper1.getMetaState());
4051 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper2.getMetaState());
4052
4053 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
4054 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
4055 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4056 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4057 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4058
4059 // Toggle caps lock on and off.
4060 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
4061 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
4062 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4063 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper1.getMetaState());
4064 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper2.getMetaState());
4065
4066 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
4067 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
4068 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4069 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4070 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4071
4072 // Toggle scroll lock on and off.
4073 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
4074 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
4075 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
4076 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper1.getMetaState());
4077 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper2.getMetaState());
4078
4079 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
4080 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
4081 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
4082 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4083 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4084}
4085
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004086// --- KeyboardInputMapperTest_ExternalDevice ---
4087
4088class KeyboardInputMapperTest_ExternalDevice : public InputMapperTest {
4089protected:
Chris Yea52ade12020-08-27 16:49:20 -07004090 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004091};
4092
4093TEST_F(KeyboardInputMapperTest_ExternalDevice, WakeBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07004094 // For external devices, non-media keys will trigger wake on key down. Media keys need to be
4095 // marked as WAKE in the keylayout file to trigger wake.
Powei Fengd041c5d2019-05-03 17:11:33 -07004096
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004097 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, 0);
4098 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, 0);
4099 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAYPAUSE, 0, AKEYCODE_MEDIA_PLAY_PAUSE,
4100 POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07004101
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004102 KeyboardInputMapper& mapper =
4103 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4104 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07004105
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004106 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004107 NotifyKeyArgs args;
4108 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4109 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4110
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004111 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004112 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4113 ASSERT_EQ(uint32_t(0), args.policyFlags);
4114
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004115 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004116 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4117 ASSERT_EQ(uint32_t(0), args.policyFlags);
4118
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004119 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004120 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4121 ASSERT_EQ(uint32_t(0), args.policyFlags);
4122
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004123 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004124 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4125 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4126
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004127 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004128 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4129 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4130}
4131
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004132TEST_F(KeyboardInputMapperTest_ExternalDevice, DoNotWakeByDefaultBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07004133 // Tv Remote key's wake behavior is prescribed by the keylayout file.
Powei Fengd041c5d2019-05-03 17:11:33 -07004134
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004135 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4136 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
4137 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07004138
Powei Fengd041c5d2019-05-03 17:11:33 -07004139 addConfigurationProperty("keyboard.doNotWakeByDefault", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004140 KeyboardInputMapper& mapper =
4141 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4142 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07004143
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004144 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004145 NotifyKeyArgs args;
4146 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4147 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4148
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004149 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004150 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4151 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4152
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004153 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_DOWN, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004154 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4155 ASSERT_EQ(uint32_t(0), args.policyFlags);
4156
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004157 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_DOWN, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004158 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4159 ASSERT_EQ(uint32_t(0), args.policyFlags);
4160
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004161 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004162 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4163 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4164
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004165 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004166 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4167 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4168}
4169
Michael Wrightd02c5b62014-02-10 15:10:22 -08004170// --- CursorInputMapperTest ---
4171
4172class CursorInputMapperTest : public InputMapperTest {
4173protected:
4174 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD;
4175
Michael Wright17db18e2020-06-26 20:51:44 +01004176 std::shared_ptr<FakePointerController> mFakePointerController;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004177
Chris Yea52ade12020-08-27 16:49:20 -07004178 void SetUp() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004179 InputMapperTest::SetUp();
4180
Michael Wright17db18e2020-06-26 20:51:44 +01004181 mFakePointerController = std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00004182 mFakePolicy->setPointerController(mFakePointerController);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004183 }
4184
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004185 void testMotionRotation(CursorInputMapper& mapper, int32_t originalX, int32_t originalY,
4186 int32_t rotatedX, int32_t rotatedY);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004187
4188 void prepareDisplay(int32_t orientation) {
4189 const std::string uniqueId = "local:0";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004190 const ViewportType viewportType = ViewportType::INTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004191 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
4192 orientation, uniqueId, NO_PORT, viewportType);
4193 }
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004194
4195 static void assertCursorPointerCoords(const PointerCoords& coords, float x, float y,
4196 float pressure) {
4197 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(coords, x, y, pressure, 0.0f, 0.0f, 0.0f, 0.0f,
4198 0.0f, 0.0f, 0.0f, EPSILON));
4199 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08004200};
4201
4202const int32_t CursorInputMapperTest::TRACKBALL_MOVEMENT_THRESHOLD = 6;
4203
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004204void CursorInputMapperTest::testMotionRotation(CursorInputMapper& mapper, int32_t originalX,
4205 int32_t originalY, int32_t rotatedX,
4206 int32_t rotatedY) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004207 NotifyMotionArgs args;
4208
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004209 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, originalX);
4210 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, originalY);
4211 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004212 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4213 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004214 ASSERT_NO_FATAL_FAILURE(
4215 assertCursorPointerCoords(args.pointerCoords[0],
4216 float(rotatedX) / TRACKBALL_MOVEMENT_THRESHOLD,
4217 float(rotatedY) / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004218}
4219
4220TEST_F(CursorInputMapperTest, WhenModeIsPointer_GetSources_ReturnsMouse) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004221 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004222 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004223
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004224 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004225}
4226
4227TEST_F(CursorInputMapperTest, WhenModeIsNavigation_GetSources_ReturnsTrackball) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004228 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004229 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004230
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004231 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004232}
4233
4234TEST_F(CursorInputMapperTest, WhenModeIsPointer_PopulateDeviceInfo_ReturnsRangeFromPointerController) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004235 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004236 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004237
4238 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004239 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004240
4241 // Initially there may not be a valid motion range.
Yi Kong9b14ac62018-07-17 13:48:38 -07004242 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE));
4243 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004244 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4245 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE, 0.0f, 1.0f, 0.0f, 0.0f));
4246
4247 // When the bounds are set, then there should be a valid motion range.
4248 mFakePointerController->setBounds(1, 2, 800 - 1, 480 - 1);
4249
4250 InputDeviceInfo info2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004251 mapper.populateDeviceInfo(&info2);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004252
4253 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
4254 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE,
4255 1, 800 - 1, 0.0f, 0.0f));
4256 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
4257 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE,
4258 2, 480 - 1, 0.0f, 0.0f));
4259 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
4260 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE,
4261 0.0f, 1.0f, 0.0f, 0.0f));
4262}
4263
4264TEST_F(CursorInputMapperTest, WhenModeIsNavigation_PopulateDeviceInfo_ReturnsScaledRange) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004265 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004266 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004267
4268 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004269 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004270
4271 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4272 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_TRACKBALL,
4273 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
4274 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4275 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_TRACKBALL,
4276 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
4277 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4278 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TRACKBALL,
4279 0.0f, 1.0f, 0.0f, 0.0f));
4280}
4281
4282TEST_F(CursorInputMapperTest, Process_ShouldSetAllFieldsAndIncludeGlobalMetaState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004283 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004284 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004285
arthurhungdcef2dc2020-08-11 14:47:50 +08004286 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004287
4288 NotifyMotionArgs args;
4289
4290 // Button press.
4291 // Mostly testing non x/y behavior here so we don't need to check again elsewhere.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004292 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4293 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004294 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4295 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4296 ASSERT_EQ(DEVICE_ID, args.deviceId);
4297 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4298 ASSERT_EQ(uint32_t(0), args.policyFlags);
4299 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
4300 ASSERT_EQ(0, args.flags);
4301 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4302 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
4303 ASSERT_EQ(0, args.edgeFlags);
4304 ASSERT_EQ(uint32_t(1), args.pointerCount);
4305 ASSERT_EQ(0, args.pointerProperties[0].id);
4306 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004307 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004308 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4309 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4310 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4311
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004312 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4313 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4314 ASSERT_EQ(DEVICE_ID, args.deviceId);
4315 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4316 ASSERT_EQ(uint32_t(0), args.policyFlags);
4317 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
4318 ASSERT_EQ(0, args.flags);
4319 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4320 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
4321 ASSERT_EQ(0, args.edgeFlags);
4322 ASSERT_EQ(uint32_t(1), args.pointerCount);
4323 ASSERT_EQ(0, args.pointerProperties[0].id);
4324 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
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 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4327 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4328 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4329
Michael Wrightd02c5b62014-02-10 15:10:22 -08004330 // Button release. Should have same down time.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004331 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4332 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004333 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4334 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
4335 ASSERT_EQ(DEVICE_ID, args.deviceId);
4336 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4337 ASSERT_EQ(uint32_t(0), args.policyFlags);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004338 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
4339 ASSERT_EQ(0, args.flags);
4340 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4341 ASSERT_EQ(0, args.buttonState);
4342 ASSERT_EQ(0, args.edgeFlags);
4343 ASSERT_EQ(uint32_t(1), args.pointerCount);
4344 ASSERT_EQ(0, args.pointerProperties[0].id);
4345 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004346 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004347 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4348 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4349 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4350
4351 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4352 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
4353 ASSERT_EQ(DEVICE_ID, args.deviceId);
4354 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4355 ASSERT_EQ(uint32_t(0), args.policyFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004356 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
4357 ASSERT_EQ(0, args.flags);
4358 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4359 ASSERT_EQ(0, args.buttonState);
4360 ASSERT_EQ(0, args.edgeFlags);
4361 ASSERT_EQ(uint32_t(1), args.pointerCount);
4362 ASSERT_EQ(0, args.pointerProperties[0].id);
4363 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004364 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004365 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4366 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4367 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4368}
4369
4370TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentXYUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004371 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004372 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004373
4374 NotifyMotionArgs args;
4375
4376 // Motion in X but not Y.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004377 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4378 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004379 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4380 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004381 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4382 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f,
4383 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004384
4385 // Motion in Y but not X.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004386 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
4387 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004388 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4389 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004390 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f,
4391 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004392}
4393
4394TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004395 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004396 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004397
4398 NotifyMotionArgs args;
4399
4400 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004401 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4402 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004403 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4404 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004405 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004406
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004407 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4408 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004409 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004410
Michael Wrightd02c5b62014-02-10 15:10:22 -08004411 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004412 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4413 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004414 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004415 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004416 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004417
4418 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004419 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004420 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004421}
4422
4423TEST_F(CursorInputMapperTest, Process_ShouldHandleCombinedXYAndButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004424 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004425 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004426
4427 NotifyMotionArgs args;
4428
4429 // Combined X, Y and Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004430 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4431 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
4432 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4433 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004434 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4435 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004436 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4437 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4438 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004439
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004440 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4441 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004442 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4443 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4444 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004445
Michael Wrightd02c5b62014-02-10 15:10:22 -08004446 // Move X, Y a bit while pressed.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004447 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 2);
4448 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 1);
4449 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004450 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4451 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004452 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4453 2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4454 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004455
4456 // Release Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004457 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4458 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004459 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004460 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004461 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004462
4463 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004464 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004465 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004466}
4467
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004468TEST_F(CursorInputMapperTest, Process_WhenOrientationAware_ShouldNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004469 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004470 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
4471 // need to be rotated.
4472 addConfigurationProperty("cursor.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004473 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004474
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004475 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004476 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4477 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4478 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4479 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4480 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4481 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4482 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4483 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4484}
4485
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004486TEST_F(CursorInputMapperTest, Process_WhenNotOrientationAware_ShouldRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004487 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004488 // Since InputReader works in the un-rotated coordinate space, only devices that are not
4489 // orientation-aware are affected by display rotation.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004490 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004491
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004492 prepareDisplay(DISPLAY_ORIENTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004493 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4494 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4495 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4496 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4497 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4498 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4499 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4500 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4501
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004502 prepareDisplay(DISPLAY_ORIENTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004503 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, -1, 0));
4504 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, 1));
4505 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, 1));
4506 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, 1));
4507 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 1, 0));
4508 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, -1));
4509 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, -1));
4510 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, -1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004511
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004512 prepareDisplay(DISPLAY_ORIENTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004513 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, -1));
4514 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, -1));
4515 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, -1, 0));
4516 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, 1));
4517 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, 1));
4518 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, 1));
4519 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 1, 0));
4520 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, -1));
4521
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004522 prepareDisplay(DISPLAY_ORIENTATION_270);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004523 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 1, 0));
4524 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, -1));
4525 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, -1));
4526 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, -1));
4527 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, -1, 0));
4528 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, 1));
4529 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, 1));
4530 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, 1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004531}
4532
4533TEST_F(CursorInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004534 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004535 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004536
4537 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4538 mFakePointerController->setPosition(100, 200);
4539 mFakePointerController->setButtonState(0);
4540
4541 NotifyMotionArgs motionArgs;
4542 NotifyKeyArgs keyArgs;
4543
4544 // press BTN_LEFT, release BTN_LEFT
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004545 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 1);
4546 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004547 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4548 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4549 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4550 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004551 ASSERT_NO_FATAL_FAILURE(
4552 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004553
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004554 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4555 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4556 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4557 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004558 ASSERT_NO_FATAL_FAILURE(
4559 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004560
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004561 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 0);
4562 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004563 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004564 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004565 ASSERT_EQ(0, motionArgs.buttonState);
4566 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004567 ASSERT_NO_FATAL_FAILURE(
4568 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004569
4570 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004571 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004572 ASSERT_EQ(0, motionArgs.buttonState);
4573 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004574 ASSERT_NO_FATAL_FAILURE(
4575 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004576
4577 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004578 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004579 ASSERT_EQ(0, motionArgs.buttonState);
4580 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004581 ASSERT_NO_FATAL_FAILURE(
4582 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004583
4584 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004585 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 1);
4586 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 1);
4587 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004588 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4589 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4590 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4591 motionArgs.buttonState);
4592 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4593 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004594 ASSERT_NO_FATAL_FAILURE(
4595 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004596
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004597 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4598 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4599 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4600 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4601 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004602 ASSERT_NO_FATAL_FAILURE(
4603 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004604
4605 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4606 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4607 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4608 motionArgs.buttonState);
4609 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4610 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004611 ASSERT_NO_FATAL_FAILURE(
4612 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004613
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004614 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 0);
4615 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004616 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004617 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004618 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4619 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004620 ASSERT_NO_FATAL_FAILURE(
4621 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004622
4623 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004624 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004625 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4626 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004627 ASSERT_NO_FATAL_FAILURE(
4628 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004629
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004630 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4631 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004632 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004633 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
4634 ASSERT_EQ(0, motionArgs.buttonState);
4635 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004636 ASSERT_NO_FATAL_FAILURE(
4637 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004638 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4639 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004640
4641 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004642 ASSERT_EQ(0, motionArgs.buttonState);
4643 ASSERT_EQ(0, mFakePointerController->getButtonState());
4644 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004645 ASSERT_NO_FATAL_FAILURE(
4646 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004647
Michael Wrightd02c5b62014-02-10 15:10:22 -08004648 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4649 ASSERT_EQ(0, motionArgs.buttonState);
4650 ASSERT_EQ(0, mFakePointerController->getButtonState());
4651 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004652 ASSERT_NO_FATAL_FAILURE(
4653 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004654
4655 // press BTN_BACK, release BTN_BACK
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004656 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 1);
4657 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004658 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4659 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4660 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004661
Michael Wrightd02c5b62014-02-10 15:10:22 -08004662 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004663 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004664 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4665 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004666 ASSERT_NO_FATAL_FAILURE(
4667 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004668
4669 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4670 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4671 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4672 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004673 ASSERT_NO_FATAL_FAILURE(
4674 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004675
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004676 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 0);
4677 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004678 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004679 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004680 ASSERT_EQ(0, motionArgs.buttonState);
4681 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004682 ASSERT_NO_FATAL_FAILURE(
4683 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004684
4685 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004686 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004687 ASSERT_EQ(0, motionArgs.buttonState);
4688 ASSERT_EQ(0, mFakePointerController->getButtonState());
4689
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004690 ASSERT_NO_FATAL_FAILURE(
4691 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004692 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4693 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4694 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4695
4696 // press BTN_SIDE, release BTN_SIDE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004697 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 1);
4698 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004699 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4700 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4701 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004702
Michael Wrightd02c5b62014-02-10 15:10:22 -08004703 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004704 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004705 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4706 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004707 ASSERT_NO_FATAL_FAILURE(
4708 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004709
4710 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4711 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4712 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4713 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004714 ASSERT_NO_FATAL_FAILURE(
4715 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004716
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004717 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 0);
4718 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004719 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004720 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004721 ASSERT_EQ(0, motionArgs.buttonState);
4722 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004723 ASSERT_NO_FATAL_FAILURE(
4724 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004725
4726 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4727 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4728 ASSERT_EQ(0, motionArgs.buttonState);
4729 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004730 ASSERT_NO_FATAL_FAILURE(
4731 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004732
Michael Wrightd02c5b62014-02-10 15:10:22 -08004733 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4734 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4735 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4736
4737 // press BTN_FORWARD, release BTN_FORWARD
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004738 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 1);
4739 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004740 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4741 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4742 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004743
Michael Wrightd02c5b62014-02-10 15:10:22 -08004744 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004745 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004746 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4747 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004748 ASSERT_NO_FATAL_FAILURE(
4749 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004750
4751 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4752 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4753 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4754 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004755 ASSERT_NO_FATAL_FAILURE(
4756 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004757
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004758 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 0);
4759 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004760 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004761 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004762 ASSERT_EQ(0, motionArgs.buttonState);
4763 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004764 ASSERT_NO_FATAL_FAILURE(
4765 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004766
4767 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4768 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4769 ASSERT_EQ(0, motionArgs.buttonState);
4770 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004771 ASSERT_NO_FATAL_FAILURE(
4772 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004773
Michael Wrightd02c5b62014-02-10 15:10:22 -08004774 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4775 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4776 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4777
4778 // press BTN_EXTRA, release BTN_EXTRA
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004779 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 1);
4780 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004781 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4782 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4783 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004784
Michael Wrightd02c5b62014-02-10 15:10:22 -08004785 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004786 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004787 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4788 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004789 ASSERT_NO_FATAL_FAILURE(
4790 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004791
4792 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4793 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4794 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4795 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004796 ASSERT_NO_FATAL_FAILURE(
4797 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004798
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004799 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 0);
4800 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004801 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004802 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004803 ASSERT_EQ(0, motionArgs.buttonState);
4804 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004805 ASSERT_NO_FATAL_FAILURE(
4806 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004807
4808 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4809 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4810 ASSERT_EQ(0, motionArgs.buttonState);
4811 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004812 ASSERT_NO_FATAL_FAILURE(
4813 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004814
Michael Wrightd02c5b62014-02-10 15:10:22 -08004815 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4816 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4817 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4818}
4819
4820TEST_F(CursorInputMapperTest, Process_WhenModeIsPointer_ShouldMoveThePointerAround) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004821 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004822 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004823
4824 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4825 mFakePointerController->setPosition(100, 200);
4826 mFakePointerController->setButtonState(0);
4827
4828 NotifyMotionArgs args;
4829
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004830 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4831 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4832 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004833 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004834 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4835 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4836 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4837 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 +01004838 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004839}
4840
4841TEST_F(CursorInputMapperTest, Process_PointerCapture) {
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004842 addConfigurationProperty("cursor.mode", "pointer");
4843 mFakePolicy->setPointerCapture(true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004844 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004845
4846 NotifyDeviceResetArgs resetArgs;
4847 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4848 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4849 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4850
4851 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4852 mFakePointerController->setPosition(100, 200);
4853 mFakePointerController->setButtonState(0);
4854
4855 NotifyMotionArgs args;
4856
4857 // Move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004858 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4859 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4860 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004861 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4862 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4863 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4864 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4865 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 +01004866 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004867
4868 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004869 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4870 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004871 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4872 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4873 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
4874 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4875 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4876 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4877 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4878 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
4879 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4880 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4881
4882 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004883 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4884 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004885 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4886 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4887 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
4888 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4889 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4890 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4891 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4892 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
4893 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4894 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4895
4896 // Another move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004897 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 30);
4898 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 40);
4899 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004900 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4901 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4902 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4903 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4904 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 +01004905 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004906
4907 // Disable pointer capture and check that the device generation got bumped
4908 // and events are generated the usual way.
arthurhungdcef2dc2020-08-11 14:47:50 +08004909 const uint32_t generation = mReader->getContext()->getGeneration();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004910 mFakePolicy->setPointerCapture(false);
4911 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
arthurhungdcef2dc2020-08-11 14:47:50 +08004912 ASSERT_TRUE(mReader->getContext()->getGeneration() != generation);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004913
4914 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4915 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4916 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4917
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004918 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4919 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4920 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004921 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4922 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004923 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4924 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4925 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 +01004926 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004927}
4928
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00004929/**
4930 * When Pointer Capture is enabled, we expect to report unprocessed relative movements, so any
4931 * pointer acceleration or speed processing should not be applied.
4932 */
4933TEST_F(CursorInputMapperTest, PointerCaptureDisablesVelocityProcessing) {
4934 addConfigurationProperty("cursor.mode", "pointer");
4935 const VelocityControlParameters testParams(5.f /*scale*/, 0.f /*low threshold*/,
4936 100.f /*high threshold*/, 10.f /*acceleration*/);
4937 mFakePolicy->setVelocityControlParams(testParams);
4938 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
4939
4940 NotifyDeviceResetArgs resetArgs;
4941 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4942 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4943 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4944
4945 NotifyMotionArgs args;
4946
4947 // Move and verify scale is applied.
4948 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4949 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4950 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4951 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4952 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4953 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4954 const float relX = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X);
4955 const float relY = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y);
4956 ASSERT_GT(relX, 10);
4957 ASSERT_GT(relY, 20);
4958
4959 // Enable Pointer Capture
4960 mFakePolicy->setPointerCapture(true);
4961 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
4962 NotifyPointerCaptureChangedArgs captureArgs;
4963 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyCaptureWasCalled(&captureArgs));
4964 ASSERT_TRUE(captureArgs.request.enable);
4965
4966 // Move and verify scale is not applied.
4967 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4968 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4969 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4970 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4971 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4972 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4973 ASSERT_EQ(10, args.pointerCoords[0].getX());
4974 ASSERT_EQ(20, args.pointerCoords[0].getY());
4975}
4976
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004977TEST_F(CursorInputMapperTest, Process_ShouldHandleDisplayId) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004978 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004979
Garfield Tan888a6a42020-01-09 11:39:16 -08004980 // Setup for second display.
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004981 constexpr int32_t SECOND_DISPLAY_ID = 1;
Garfield Tan888a6a42020-01-09 11:39:16 -08004982 const std::string SECOND_DISPLAY_UNIQUE_ID = "local:1";
4983 mFakePolicy->addDisplayViewport(SECOND_DISPLAY_ID, 800, 480, DISPLAY_ORIENTATION_0,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00004984 true /*isActive*/, SECOND_DISPLAY_UNIQUE_ID, NO_PORT,
4985 ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08004986 mFakePolicy->setDefaultPointerDisplayId(SECOND_DISPLAY_ID);
4987 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
4988
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004989 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4990 mFakePointerController->setPosition(100, 200);
4991 mFakePointerController->setButtonState(0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004992
4993 NotifyMotionArgs args;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004994 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4995 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4996 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004997 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4998 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4999 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
5000 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5001 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 +01005002 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005003 ASSERT_EQ(SECOND_DISPLAY_ID, args.displayId);
5004}
5005
Michael Wrightd02c5b62014-02-10 15:10:22 -08005006// --- TouchInputMapperTest ---
5007
5008class TouchInputMapperTest : public InputMapperTest {
5009protected:
5010 static const int32_t RAW_X_MIN;
5011 static const int32_t RAW_X_MAX;
5012 static const int32_t RAW_Y_MIN;
5013 static const int32_t RAW_Y_MAX;
5014 static const int32_t RAW_TOUCH_MIN;
5015 static const int32_t RAW_TOUCH_MAX;
5016 static const int32_t RAW_TOOL_MIN;
5017 static const int32_t RAW_TOOL_MAX;
5018 static const int32_t RAW_PRESSURE_MIN;
5019 static const int32_t RAW_PRESSURE_MAX;
5020 static const int32_t RAW_ORIENTATION_MIN;
5021 static const int32_t RAW_ORIENTATION_MAX;
5022 static const int32_t RAW_DISTANCE_MIN;
5023 static const int32_t RAW_DISTANCE_MAX;
5024 static const int32_t RAW_TILT_MIN;
5025 static const int32_t RAW_TILT_MAX;
5026 static const int32_t RAW_ID_MIN;
5027 static const int32_t RAW_ID_MAX;
5028 static const int32_t RAW_SLOT_MIN;
5029 static const int32_t RAW_SLOT_MAX;
5030 static const float X_PRECISION;
5031 static const float Y_PRECISION;
Santos Cordonfa5cf462017-04-05 10:37:00 -07005032 static const float X_PRECISION_VIRTUAL;
5033 static const float Y_PRECISION_VIRTUAL;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005034
5035 static const float GEOMETRIC_SCALE;
Jason Gerecke489fda82012-09-07 17:19:40 -07005036 static const TouchAffineTransformation AFFINE_TRANSFORM;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005037
5038 static const VirtualKeyDefinition VIRTUAL_KEYS[2];
5039
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005040 const std::string UNIQUE_ID = "local:0";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005041 const std::string SECONDARY_UNIQUE_ID = "local:1";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005042
Michael Wrightd02c5b62014-02-10 15:10:22 -08005043 enum Axes {
5044 POSITION = 1 << 0,
5045 TOUCH = 1 << 1,
5046 TOOL = 1 << 2,
5047 PRESSURE = 1 << 3,
5048 ORIENTATION = 1 << 4,
5049 MINOR = 1 << 5,
5050 ID = 1 << 6,
5051 DISTANCE = 1 << 7,
5052 TILT = 1 << 8,
5053 SLOT = 1 << 9,
5054 TOOL_TYPE = 1 << 10,
5055 };
5056
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005057 void prepareDisplay(int32_t orientation, std::optional<uint8_t> port = NO_PORT);
5058 void prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port = NO_PORT);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005059 void prepareVirtualDisplay(int32_t orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005060 void prepareVirtualKeys();
Jason Gerecke489fda82012-09-07 17:19:40 -07005061 void prepareLocationCalibration();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005062 int32_t toRawX(float displayX);
5063 int32_t toRawY(float displayY);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005064 int32_t toRotatedRawX(float displayX);
5065 int32_t toRotatedRawY(float displayY);
Jason Gerecke489fda82012-09-07 17:19:40 -07005066 float toCookedX(float rawX, float rawY);
5067 float toCookedY(float rawX, float rawY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005068 float toDisplayX(int32_t rawX);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005069 float toDisplayX(int32_t rawX, int32_t displayWidth);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005070 float toDisplayY(int32_t rawY);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005071 float toDisplayY(int32_t rawY, int32_t displayHeight);
5072
Michael Wrightd02c5b62014-02-10 15:10:22 -08005073};
5074
5075const int32_t TouchInputMapperTest::RAW_X_MIN = 25;
5076const int32_t TouchInputMapperTest::RAW_X_MAX = 1019;
5077const int32_t TouchInputMapperTest::RAW_Y_MIN = 30;
5078const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009;
5079const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0;
5080const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31;
5081const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0;
5082const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15;
Michael Wrightaa449c92017-12-13 21:21:43 +00005083const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = 0;
5084const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = 255;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005085const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7;
5086const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
5087const int32_t TouchInputMapperTest::RAW_DISTANCE_MIN = 0;
5088const int32_t TouchInputMapperTest::RAW_DISTANCE_MAX = 7;
5089const int32_t TouchInputMapperTest::RAW_TILT_MIN = 0;
5090const int32_t TouchInputMapperTest::RAW_TILT_MAX = 150;
5091const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
5092const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
5093const int32_t TouchInputMapperTest::RAW_SLOT_MIN = 0;
5094const int32_t TouchInputMapperTest::RAW_SLOT_MAX = 9;
5095const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH;
5096const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT;
Santos Cordonfa5cf462017-04-05 10:37:00 -07005097const float TouchInputMapperTest::X_PRECISION_VIRTUAL =
5098 float(RAW_X_MAX - RAW_X_MIN + 1) / VIRTUAL_DISPLAY_WIDTH;
5099const float TouchInputMapperTest::Y_PRECISION_VIRTUAL =
5100 float(RAW_Y_MAX - RAW_Y_MIN + 1) / VIRTUAL_DISPLAY_HEIGHT;
Jason Gerecke489fda82012-09-07 17:19:40 -07005101const TouchAffineTransformation TouchInputMapperTest::AFFINE_TRANSFORM =
5102 TouchAffineTransformation(1, -2, 3, -4, 5, -6);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005103
5104const float TouchInputMapperTest::GEOMETRIC_SCALE =
5105 avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1),
5106 float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1));
5107
5108const VirtualKeyDefinition TouchInputMapperTest::VIRTUAL_KEYS[2] = {
5109 { KEY_HOME, 60, DISPLAY_HEIGHT + 15, 20, 20 },
5110 { KEY_MENU, DISPLAY_HEIGHT - 60, DISPLAY_WIDTH + 15, 20, 20 },
5111};
5112
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005113void TouchInputMapperTest::prepareDisplay(int32_t orientation, std::optional<uint8_t> port) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01005114 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
5115 port, ViewportType::INTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005116}
5117
5118void TouchInputMapperTest::prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port) {
5119 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
5120 DISPLAY_ORIENTATION_0, SECONDARY_UNIQUE_ID, port, type);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005121}
5122
Santos Cordonfa5cf462017-04-05 10:37:00 -07005123void TouchInputMapperTest::prepareVirtualDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01005124 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH, VIRTUAL_DISPLAY_HEIGHT,
5125 orientation, VIRTUAL_DISPLAY_UNIQUE_ID, NO_PORT,
5126 ViewportType::VIRTUAL);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005127}
5128
Michael Wrightd02c5b62014-02-10 15:10:22 -08005129void TouchInputMapperTest::prepareVirtualKeys() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005130 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[0]);
5131 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[1]);
5132 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
5133 mFakeEventHub->addKey(EVENTHUB_ID, KEY_MENU, 0, AKEYCODE_MENU, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005134}
5135
Jason Gerecke489fda82012-09-07 17:19:40 -07005136void TouchInputMapperTest::prepareLocationCalibration() {
5137 mFakePolicy->setTouchAffineTransformation(AFFINE_TRANSFORM);
5138}
5139
Michael Wrightd02c5b62014-02-10 15:10:22 -08005140int32_t TouchInputMapperTest::toRawX(float displayX) {
5141 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN);
5142}
5143
5144int32_t TouchInputMapperTest::toRawY(float displayY) {
5145 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN);
5146}
5147
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005148int32_t TouchInputMapperTest::toRotatedRawX(float displayX) {
5149 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_HEIGHT + RAW_X_MIN);
5150}
5151
5152int32_t TouchInputMapperTest::toRotatedRawY(float displayY) {
5153 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_WIDTH + RAW_Y_MIN);
5154}
5155
Jason Gerecke489fda82012-09-07 17:19:40 -07005156float TouchInputMapperTest::toCookedX(float rawX, float rawY) {
5157 AFFINE_TRANSFORM.applyTo(rawX, rawY);
5158 return rawX;
5159}
5160
5161float TouchInputMapperTest::toCookedY(float rawX, float rawY) {
5162 AFFINE_TRANSFORM.applyTo(rawX, rawY);
5163 return rawY;
5164}
5165
Michael Wrightd02c5b62014-02-10 15:10:22 -08005166float TouchInputMapperTest::toDisplayX(int32_t rawX) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005167 return toDisplayX(rawX, DISPLAY_WIDTH);
5168}
5169
5170float TouchInputMapperTest::toDisplayX(int32_t rawX, int32_t displayWidth) {
5171 return float(rawX - RAW_X_MIN) * displayWidth / (RAW_X_MAX - RAW_X_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005172}
5173
5174float TouchInputMapperTest::toDisplayY(int32_t rawY) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005175 return toDisplayY(rawY, DISPLAY_HEIGHT);
5176}
5177
5178float TouchInputMapperTest::toDisplayY(int32_t rawY, int32_t displayHeight) {
5179 return float(rawY - RAW_Y_MIN) * displayHeight / (RAW_Y_MAX - RAW_Y_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005180}
5181
5182
5183// --- SingleTouchInputMapperTest ---
5184
5185class SingleTouchInputMapperTest : public TouchInputMapperTest {
5186protected:
5187 void prepareButtons();
5188 void prepareAxes(int axes);
5189
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005190 void processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
5191 void processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
5192 void processUp(SingleTouchInputMapper& mappery);
5193 void processPressure(SingleTouchInputMapper& mapper, int32_t pressure);
5194 void processToolMajor(SingleTouchInputMapper& mapper, int32_t toolMajor);
5195 void processDistance(SingleTouchInputMapper& mapper, int32_t distance);
5196 void processTilt(SingleTouchInputMapper& mapper, int32_t tiltX, int32_t tiltY);
5197 void processKey(SingleTouchInputMapper& mapper, int32_t code, int32_t value);
5198 void processSync(SingleTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005199};
5200
5201void SingleTouchInputMapperTest::prepareButtons() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005202 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005203}
5204
5205void SingleTouchInputMapperTest::prepareAxes(int axes) {
5206 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005207 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
5208 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005209 }
5210 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005211 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MIN,
5212 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005213 }
5214 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005215 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TOOL_WIDTH, RAW_TOOL_MIN, RAW_TOOL_MAX, 0,
5216 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005217 }
5218 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005219 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_DISTANCE, RAW_DISTANCE_MIN,
5220 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005221 }
5222 if (axes & TILT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005223 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_X, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
5224 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_Y, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005225 }
5226}
5227
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005228void SingleTouchInputMapperTest::processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005229 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
5230 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
5231 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005232}
5233
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005234void SingleTouchInputMapperTest::processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005235 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
5236 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005237}
5238
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005239void SingleTouchInputMapperTest::processUp(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005240 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005241}
5242
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005243void SingleTouchInputMapperTest::processPressure(SingleTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005244 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005245}
5246
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005247void SingleTouchInputMapperTest::processToolMajor(SingleTouchInputMapper& mapper,
5248 int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005249 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TOOL_WIDTH, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005250}
5251
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005252void SingleTouchInputMapperTest::processDistance(SingleTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005253 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005254}
5255
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005256void SingleTouchInputMapperTest::processTilt(SingleTouchInputMapper& mapper, int32_t tiltX,
5257 int32_t tiltY) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005258 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_X, tiltX);
5259 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_Y, tiltY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005260}
5261
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005262void SingleTouchInputMapperTest::processKey(SingleTouchInputMapper& mapper, int32_t code,
5263 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005264 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005265}
5266
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005267void SingleTouchInputMapperTest::processSync(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005268 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005269}
5270
Michael Wrightd02c5b62014-02-10 15:10:22 -08005271TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005272 prepareButtons();
5273 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005274 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005275
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005276 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005277}
5278
5279TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndIsACursor_ReturnsTouchPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005280 mFakeEventHub->addRelativeAxis(EVENTHUB_ID, REL_X);
5281 mFakeEventHub->addRelativeAxis(EVENTHUB_ID, REL_Y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005282 prepareButtons();
5283 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005284 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005285
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005286 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005287}
5288
5289TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchPad_ReturnsTouchPad) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005290 prepareButtons();
5291 prepareAxes(POSITION);
5292 addConfigurationProperty("touch.deviceType", "touchPad");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005293 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005294
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005295 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005296}
5297
5298TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005299 prepareButtons();
5300 prepareAxes(POSITION);
5301 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005302 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005303
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005304 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005305}
5306
5307TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005308 addConfigurationProperty("touch.deviceType", "touchScreen");
5309 prepareDisplay(DISPLAY_ORIENTATION_0);
5310 prepareButtons();
5311 prepareAxes(POSITION);
5312 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005313 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005314
5315 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005316 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005317
5318 // Virtual key is down.
5319 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5320 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5321 processDown(mapper, x, y);
5322 processSync(mapper);
5323 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5324
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005325 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005326
5327 // Virtual key is up.
5328 processUp(mapper);
5329 processSync(mapper);
5330 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5331
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005332 ASSERT_EQ(AKEY_STATE_UP, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005333}
5334
5335TEST_F(SingleTouchInputMapperTest, GetScanCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005336 addConfigurationProperty("touch.deviceType", "touchScreen");
5337 prepareDisplay(DISPLAY_ORIENTATION_0);
5338 prepareButtons();
5339 prepareAxes(POSITION);
5340 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005341 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005342
5343 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005344 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005345
5346 // Virtual key is down.
5347 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5348 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5349 processDown(mapper, x, y);
5350 processSync(mapper);
5351 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5352
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005353 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005354
5355 // Virtual key is up.
5356 processUp(mapper);
5357 processSync(mapper);
5358 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5359
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005360 ASSERT_EQ(AKEY_STATE_UP, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005361}
5362
5363TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005364 addConfigurationProperty("touch.deviceType", "touchScreen");
5365 prepareDisplay(DISPLAY_ORIENTATION_0);
5366 prepareButtons();
5367 prepareAxes(POSITION);
5368 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005369 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005370
5371 const int32_t keys[2] = { AKEYCODE_HOME, AKEYCODE_A };
5372 uint8_t flags[2] = { 0, 0 };
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005373 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, 2, keys, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005374 ASSERT_TRUE(flags[0]);
5375 ASSERT_FALSE(flags[1]);
5376}
5377
5378TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005379 addConfigurationProperty("touch.deviceType", "touchScreen");
5380 prepareDisplay(DISPLAY_ORIENTATION_0);
5381 prepareButtons();
5382 prepareAxes(POSITION);
5383 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005384 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005385
arthurhungdcef2dc2020-08-11 14:47:50 +08005386 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005387
5388 NotifyKeyArgs args;
5389
5390 // Press virtual key.
5391 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5392 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5393 processDown(mapper, x, y);
5394 processSync(mapper);
5395
5396 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
5397 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5398 ASSERT_EQ(DEVICE_ID, args.deviceId);
5399 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
5400 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
5401 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
5402 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5403 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5404 ASSERT_EQ(KEY_HOME, args.scanCode);
5405 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5406 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5407
5408 // Release virtual key.
5409 processUp(mapper);
5410 processSync(mapper);
5411
5412 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
5413 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5414 ASSERT_EQ(DEVICE_ID, args.deviceId);
5415 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
5416 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
5417 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
5418 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5419 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5420 ASSERT_EQ(KEY_HOME, args.scanCode);
5421 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5422 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5423
5424 // Should not have sent any motions.
5425 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5426}
5427
5428TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005429 addConfigurationProperty("touch.deviceType", "touchScreen");
5430 prepareDisplay(DISPLAY_ORIENTATION_0);
5431 prepareButtons();
5432 prepareAxes(POSITION);
5433 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005434 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005435
arthurhungdcef2dc2020-08-11 14:47:50 +08005436 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005437
5438 NotifyKeyArgs keyArgs;
5439
5440 // Press virtual key.
5441 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5442 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5443 processDown(mapper, x, y);
5444 processSync(mapper);
5445
5446 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5447 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5448 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5449 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5450 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5451 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5452 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags);
5453 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5454 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5455 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5456 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5457
5458 // Move out of bounds. This should generate a cancel and a pointer down since we moved
5459 // into the display area.
5460 y -= 100;
5461 processMove(mapper, x, y);
5462 processSync(mapper);
5463
5464 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5465 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5466 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5467 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5468 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5469 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5470 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
5471 | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags);
5472 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5473 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5474 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5475 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5476
5477 NotifyMotionArgs motionArgs;
5478 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5479 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5480 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5481 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5482 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5483 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5484 ASSERT_EQ(0, motionArgs.flags);
5485 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5486 ASSERT_EQ(0, motionArgs.buttonState);
5487 ASSERT_EQ(0, motionArgs.edgeFlags);
5488 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5489 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5490 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5491 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5492 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5493 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5494 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5495 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5496
5497 // Keep moving out of bounds. Should generate a pointer move.
5498 y -= 50;
5499 processMove(mapper, x, y);
5500 processSync(mapper);
5501
5502 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5503 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5504 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5505 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5506 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5507 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5508 ASSERT_EQ(0, motionArgs.flags);
5509 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5510 ASSERT_EQ(0, motionArgs.buttonState);
5511 ASSERT_EQ(0, motionArgs.edgeFlags);
5512 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5513 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5514 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5515 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5516 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5517 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5518 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5519 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5520
5521 // Release out of bounds. Should generate a pointer up.
5522 processUp(mapper);
5523 processSync(mapper);
5524
5525 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5526 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5527 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5528 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5529 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5530 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5531 ASSERT_EQ(0, motionArgs.flags);
5532 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5533 ASSERT_EQ(0, motionArgs.buttonState);
5534 ASSERT_EQ(0, motionArgs.edgeFlags);
5535 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5536 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5537 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5538 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5539 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5540 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5541 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5542 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5543
5544 // Should not have sent any more keys or motions.
5545 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5546 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5547}
5548
5549TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005550 addConfigurationProperty("touch.deviceType", "touchScreen");
5551 prepareDisplay(DISPLAY_ORIENTATION_0);
5552 prepareButtons();
5553 prepareAxes(POSITION);
5554 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005555 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005556
arthurhungdcef2dc2020-08-11 14:47:50 +08005557 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005558
5559 NotifyMotionArgs motionArgs;
5560
5561 // Initially go down out of bounds.
5562 int32_t x = -10;
5563 int32_t y = -10;
5564 processDown(mapper, x, y);
5565 processSync(mapper);
5566
5567 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5568
5569 // Move into the display area. Should generate a pointer down.
5570 x = 50;
5571 y = 75;
5572 processMove(mapper, x, y);
5573 processSync(mapper);
5574
5575 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5576 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5577 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5578 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5579 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5580 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5581 ASSERT_EQ(0, motionArgs.flags);
5582 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5583 ASSERT_EQ(0, motionArgs.buttonState);
5584 ASSERT_EQ(0, motionArgs.edgeFlags);
5585 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5586 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5587 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5588 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5589 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5590 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5591 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5592 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5593
5594 // Release. Should generate a pointer up.
5595 processUp(mapper);
5596 processSync(mapper);
5597
5598 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5599 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5600 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5601 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5602 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5603 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5604 ASSERT_EQ(0, motionArgs.flags);
5605 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5606 ASSERT_EQ(0, motionArgs.buttonState);
5607 ASSERT_EQ(0, motionArgs.edgeFlags);
5608 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5609 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5610 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5611 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5612 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5613 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5614 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5615 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5616
5617 // Should not have sent any more keys or motions.
5618 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5619 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5620}
5621
Santos Cordonfa5cf462017-04-05 10:37:00 -07005622TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture_VirtualDisplay) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005623 addConfigurationProperty("touch.deviceType", "touchScreen");
5624 addConfigurationProperty("touch.displayId", VIRTUAL_DISPLAY_UNIQUE_ID);
5625
5626 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
5627 prepareButtons();
5628 prepareAxes(POSITION);
5629 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005630 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Santos Cordonfa5cf462017-04-05 10:37:00 -07005631
arthurhungdcef2dc2020-08-11 14:47:50 +08005632 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005633
5634 NotifyMotionArgs motionArgs;
5635
5636 // Down.
5637 int32_t x = 100;
5638 int32_t y = 125;
5639 processDown(mapper, x, y);
5640 processSync(mapper);
5641
5642 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5643 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5644 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5645 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5646 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5647 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5648 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5649 ASSERT_EQ(0, motionArgs.flags);
5650 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5651 ASSERT_EQ(0, motionArgs.buttonState);
5652 ASSERT_EQ(0, motionArgs.edgeFlags);
5653 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5654 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5655 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5656 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5657 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5658 1, 0, 0, 0, 0, 0, 0, 0));
5659 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5660 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5661 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5662
5663 // Move.
5664 x += 50;
5665 y += 75;
5666 processMove(mapper, x, y);
5667 processSync(mapper);
5668
5669 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5670 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5671 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5672 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5673 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5674 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5675 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5676 ASSERT_EQ(0, motionArgs.flags);
5677 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5678 ASSERT_EQ(0, motionArgs.buttonState);
5679 ASSERT_EQ(0, motionArgs.edgeFlags);
5680 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5681 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5682 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5683 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5684 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5685 1, 0, 0, 0, 0, 0, 0, 0));
5686 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5687 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5688 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5689
5690 // Up.
5691 processUp(mapper);
5692 processSync(mapper);
5693
5694 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5695 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5696 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5697 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5698 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5699 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5700 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5701 ASSERT_EQ(0, motionArgs.flags);
5702 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5703 ASSERT_EQ(0, motionArgs.buttonState);
5704 ASSERT_EQ(0, motionArgs.edgeFlags);
5705 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5706 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5707 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5708 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5709 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5710 1, 0, 0, 0, 0, 0, 0, 0));
5711 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5712 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5713 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5714
5715 // Should not have sent any more keys or motions.
5716 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5717 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5718}
5719
Michael Wrightd02c5b62014-02-10 15:10:22 -08005720TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005721 addConfigurationProperty("touch.deviceType", "touchScreen");
5722 prepareDisplay(DISPLAY_ORIENTATION_0);
5723 prepareButtons();
5724 prepareAxes(POSITION);
5725 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005726 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005727
arthurhungdcef2dc2020-08-11 14:47:50 +08005728 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005729
5730 NotifyMotionArgs motionArgs;
5731
5732 // Down.
5733 int32_t x = 100;
5734 int32_t y = 125;
5735 processDown(mapper, x, y);
5736 processSync(mapper);
5737
5738 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5739 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5740 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5741 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5742 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5743 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5744 ASSERT_EQ(0, motionArgs.flags);
5745 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5746 ASSERT_EQ(0, motionArgs.buttonState);
5747 ASSERT_EQ(0, motionArgs.edgeFlags);
5748 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5749 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5750 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5751 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5752 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5753 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5754 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5755 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5756
5757 // Move.
5758 x += 50;
5759 y += 75;
5760 processMove(mapper, x, y);
5761 processSync(mapper);
5762
5763 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5764 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5765 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5766 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5767 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5768 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5769 ASSERT_EQ(0, motionArgs.flags);
5770 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5771 ASSERT_EQ(0, motionArgs.buttonState);
5772 ASSERT_EQ(0, motionArgs.edgeFlags);
5773 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5774 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5775 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5776 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5777 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5778 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5779 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5780 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5781
5782 // Up.
5783 processUp(mapper);
5784 processSync(mapper);
5785
5786 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5787 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5788 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5789 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5790 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5791 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5792 ASSERT_EQ(0, motionArgs.flags);
5793 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5794 ASSERT_EQ(0, motionArgs.buttonState);
5795 ASSERT_EQ(0, motionArgs.edgeFlags);
5796 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5797 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5798 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5799 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5800 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5801 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5802 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5803 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5804
5805 // Should not have sent any more keys or motions.
5806 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5807 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5808}
5809
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005810TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_DoesNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005811 addConfigurationProperty("touch.deviceType", "touchScreen");
5812 prepareButtons();
5813 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005814 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
5815 // need to be rotated. Touchscreens are orientation-aware by default.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005816 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005817
5818 NotifyMotionArgs args;
5819
5820 // Rotation 90.
5821 prepareDisplay(DISPLAY_ORIENTATION_90);
5822 processDown(mapper, toRawX(50), toRawY(75));
5823 processSync(mapper);
5824
5825 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5826 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5827 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5828
5829 processUp(mapper);
5830 processSync(mapper);
5831 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5832}
5833
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005834TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_RotatesMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005835 addConfigurationProperty("touch.deviceType", "touchScreen");
5836 prepareButtons();
5837 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005838 // Since InputReader works in the un-rotated coordinate space, only devices that are not
5839 // orientation-aware are affected by display rotation.
5840 addConfigurationProperty("touch.orientationAware", "0");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005841 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005842
5843 NotifyMotionArgs args;
5844
5845 // Rotation 0.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005846 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005847 prepareDisplay(DISPLAY_ORIENTATION_0);
5848 processDown(mapper, toRawX(50), toRawY(75));
5849 processSync(mapper);
5850
5851 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5852 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5853 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5854
5855 processUp(mapper);
5856 processSync(mapper);
5857 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5858
5859 // Rotation 90.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005860 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005861 prepareDisplay(DISPLAY_ORIENTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005862 processDown(mapper, toRawX(75), RAW_Y_MAX - toRawY(50) + RAW_Y_MIN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005863 processSync(mapper);
5864
5865 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5866 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5867 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5868
5869 processUp(mapper);
5870 processSync(mapper);
5871 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5872
5873 // Rotation 180.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005874 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005875 prepareDisplay(DISPLAY_ORIENTATION_180);
5876 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
5877 processSync(mapper);
5878
5879 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5880 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5881 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5882
5883 processUp(mapper);
5884 processSync(mapper);
5885 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5886
5887 // Rotation 270.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005888 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005889 prepareDisplay(DISPLAY_ORIENTATION_270);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005890 processDown(mapper, RAW_X_MAX - toRawX(75) + RAW_X_MIN, toRawY(50));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005891 processSync(mapper);
5892
5893 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5894 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5895 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5896
5897 processUp(mapper);
5898 processSync(mapper);
5899 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5900}
5901
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005902TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation0_RotatesMotions) {
5903 addConfigurationProperty("touch.deviceType", "touchScreen");
5904 prepareButtons();
5905 prepareAxes(POSITION);
5906 addConfigurationProperty("touch.orientationAware", "1");
5907 addConfigurationProperty("touch.orientation", "ORIENTATION_0");
5908 clearViewports();
5909 prepareDisplay(DISPLAY_ORIENTATION_0);
5910 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5911 NotifyMotionArgs args;
5912
5913 // Orientation 0.
5914 processDown(mapper, toRawX(50), toRawY(75));
5915 processSync(mapper);
5916
5917 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5918 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5919 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5920
5921 processUp(mapper);
5922 processSync(mapper);
5923 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5924}
5925
5926TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation90_RotatesMotions) {
5927 addConfigurationProperty("touch.deviceType", "touchScreen");
5928 prepareButtons();
5929 prepareAxes(POSITION);
5930 addConfigurationProperty("touch.orientationAware", "1");
5931 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
5932 clearViewports();
5933 prepareDisplay(DISPLAY_ORIENTATION_0);
5934 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5935 NotifyMotionArgs args;
5936
5937 // Orientation 90.
5938 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
5939 processSync(mapper);
5940
5941 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5942 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5943 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5944
5945 processUp(mapper);
5946 processSync(mapper);
5947 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5948}
5949
5950TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation180_RotatesMotions) {
5951 addConfigurationProperty("touch.deviceType", "touchScreen");
5952 prepareButtons();
5953 prepareAxes(POSITION);
5954 addConfigurationProperty("touch.orientationAware", "1");
5955 addConfigurationProperty("touch.orientation", "ORIENTATION_180");
5956 clearViewports();
5957 prepareDisplay(DISPLAY_ORIENTATION_0);
5958 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5959 NotifyMotionArgs args;
5960
5961 // Orientation 180.
5962 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
5963 processSync(mapper);
5964
5965 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5966 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5967 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5968
5969 processUp(mapper);
5970 processSync(mapper);
5971 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5972}
5973
5974TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation270_RotatesMotions) {
5975 addConfigurationProperty("touch.deviceType", "touchScreen");
5976 prepareButtons();
5977 prepareAxes(POSITION);
5978 addConfigurationProperty("touch.orientationAware", "1");
5979 addConfigurationProperty("touch.orientation", "ORIENTATION_270");
5980 clearViewports();
5981 prepareDisplay(DISPLAY_ORIENTATION_0);
5982 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5983 NotifyMotionArgs args;
5984
5985 // Orientation 270.
5986 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
5987 processSync(mapper);
5988
5989 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5990 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5991 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5992
5993 processUp(mapper);
5994 processSync(mapper);
5995 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5996}
5997
5998TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationSpecified_RotatesMotionWithDisplay) {
5999 addConfigurationProperty("touch.deviceType", "touchScreen");
6000 prepareButtons();
6001 prepareAxes(POSITION);
6002 // Since InputReader works in the un-rotated coordinate space, only devices that are not
6003 // orientation-aware are affected by display rotation.
6004 addConfigurationProperty("touch.orientationAware", "0");
6005 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
6006 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6007
6008 NotifyMotionArgs args;
6009
6010 // Orientation 90, Rotation 0.
6011 clearViewports();
6012 prepareDisplay(DISPLAY_ORIENTATION_0);
6013 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
6014 processSync(mapper);
6015
6016 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6017 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6018 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6019
6020 processUp(mapper);
6021 processSync(mapper);
6022 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6023
6024 // Orientation 90, Rotation 90.
6025 clearViewports();
6026 prepareDisplay(DISPLAY_ORIENTATION_90);
6027 processDown(mapper, toRotatedRawX(50), toRotatedRawY(75));
6028 processSync(mapper);
6029
6030 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6031 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6032 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6033
6034 processUp(mapper);
6035 processSync(mapper);
6036 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6037
6038 // Orientation 90, Rotation 180.
6039 clearViewports();
6040 prepareDisplay(DISPLAY_ORIENTATION_180);
6041 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
6042 processSync(mapper);
6043
6044 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6045 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6046 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6047
6048 processUp(mapper);
6049 processSync(mapper);
6050 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6051
6052 // Orientation 90, Rotation 270.
6053 clearViewports();
6054 prepareDisplay(DISPLAY_ORIENTATION_270);
6055 processDown(mapper, RAW_X_MAX - toRotatedRawX(50) + RAW_X_MIN,
6056 RAW_Y_MAX - toRotatedRawY(75) + RAW_Y_MIN);
6057 processSync(mapper);
6058
6059 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6060 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6061 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6062
6063 processUp(mapper);
6064 processSync(mapper);
6065 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6066}
6067
Michael Wrightd02c5b62014-02-10 15:10:22 -08006068TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006069 addConfigurationProperty("touch.deviceType", "touchScreen");
6070 prepareDisplay(DISPLAY_ORIENTATION_0);
6071 prepareButtons();
6072 prepareAxes(POSITION | PRESSURE | TOOL | DISTANCE | TILT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006073 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006074
6075 // These calculations are based on the input device calibration documentation.
6076 int32_t rawX = 100;
6077 int32_t rawY = 200;
6078 int32_t rawPressure = 10;
6079 int32_t rawToolMajor = 12;
6080 int32_t rawDistance = 2;
6081 int32_t rawTiltX = 30;
6082 int32_t rawTiltY = 110;
6083
6084 float x = toDisplayX(rawX);
6085 float y = toDisplayY(rawY);
6086 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
6087 float size = float(rawToolMajor) / RAW_TOOL_MAX;
6088 float tool = float(rawToolMajor) * GEOMETRIC_SCALE;
6089 float distance = float(rawDistance);
6090
6091 float tiltCenter = (RAW_TILT_MAX + RAW_TILT_MIN) * 0.5f;
6092 float tiltScale = M_PI / 180;
6093 float tiltXAngle = (rawTiltX - tiltCenter) * tiltScale;
6094 float tiltYAngle = (rawTiltY - tiltCenter) * tiltScale;
6095 float orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
6096 float tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
6097
6098 processDown(mapper, rawX, rawY);
6099 processPressure(mapper, rawPressure);
6100 processToolMajor(mapper, rawToolMajor);
6101 processDistance(mapper, rawDistance);
6102 processTilt(mapper, rawTiltX, rawTiltY);
6103 processSync(mapper);
6104
6105 NotifyMotionArgs args;
6106 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6107 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6108 x, y, pressure, size, tool, tool, tool, tool, orientation, distance));
6109 ASSERT_EQ(tilt, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TILT));
6110}
6111
Jason Gerecke489fda82012-09-07 17:19:40 -07006112TEST_F(SingleTouchInputMapperTest, Process_XYAxes_AffineCalibration) {
Jason Gerecke489fda82012-09-07 17:19:40 -07006113 addConfigurationProperty("touch.deviceType", "touchScreen");
6114 prepareDisplay(DISPLAY_ORIENTATION_0);
6115 prepareLocationCalibration();
6116 prepareButtons();
6117 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006118 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Jason Gerecke489fda82012-09-07 17:19:40 -07006119
6120 int32_t rawX = 100;
6121 int32_t rawY = 200;
6122
6123 float x = toDisplayX(toCookedX(rawX, rawY));
6124 float y = toDisplayY(toCookedY(rawX, rawY));
6125
6126 processDown(mapper, rawX, rawY);
6127 processSync(mapper);
6128
6129 NotifyMotionArgs args;
6130 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6131 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6132 x, y, 1, 0, 0, 0, 0, 0, 0, 0));
6133}
6134
Michael Wrightd02c5b62014-02-10 15:10:22 -08006135TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006136 addConfigurationProperty("touch.deviceType", "touchScreen");
6137 prepareDisplay(DISPLAY_ORIENTATION_0);
6138 prepareButtons();
6139 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006140 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006141
6142 NotifyMotionArgs motionArgs;
6143 NotifyKeyArgs keyArgs;
6144
6145 processDown(mapper, 100, 200);
6146 processSync(mapper);
6147 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6148 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6149 ASSERT_EQ(0, motionArgs.buttonState);
6150
6151 // press BTN_LEFT, release BTN_LEFT
6152 processKey(mapper, BTN_LEFT, 1);
6153 processSync(mapper);
6154 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6155 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6156 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6157
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006158 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6159 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6160 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6161
Michael Wrightd02c5b62014-02-10 15:10:22 -08006162 processKey(mapper, BTN_LEFT, 0);
6163 processSync(mapper);
6164 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006165 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006166 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006167
6168 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006169 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006170 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006171
6172 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
6173 processKey(mapper, BTN_RIGHT, 1);
6174 processKey(mapper, BTN_MIDDLE, 1);
6175 processSync(mapper);
6176 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6177 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6178 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6179 motionArgs.buttonState);
6180
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006181 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6182 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6183 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
6184
6185 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6186 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6187 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6188 motionArgs.buttonState);
6189
Michael Wrightd02c5b62014-02-10 15:10:22 -08006190 processKey(mapper, BTN_RIGHT, 0);
6191 processSync(mapper);
6192 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006193 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006194 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006195
6196 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006197 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006198 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006199
6200 processKey(mapper, BTN_MIDDLE, 0);
6201 processSync(mapper);
6202 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006203 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006204 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006205
6206 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006207 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006208 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006209
6210 // press BTN_BACK, release BTN_BACK
6211 processKey(mapper, BTN_BACK, 1);
6212 processSync(mapper);
6213 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6214 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6215 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006216
Michael Wrightd02c5b62014-02-10 15:10:22 -08006217 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006218 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006219 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6220
6221 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6222 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6223 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006224
6225 processKey(mapper, BTN_BACK, 0);
6226 processSync(mapper);
6227 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006228 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006229 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006230
6231 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006232 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006233 ASSERT_EQ(0, motionArgs.buttonState);
6234
Michael Wrightd02c5b62014-02-10 15:10:22 -08006235 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6236 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6237 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6238
6239 // press BTN_SIDE, release BTN_SIDE
6240 processKey(mapper, BTN_SIDE, 1);
6241 processSync(mapper);
6242 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6243 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6244 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006245
Michael Wrightd02c5b62014-02-10 15:10:22 -08006246 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006247 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006248 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6249
6250 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6251 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6252 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006253
6254 processKey(mapper, BTN_SIDE, 0);
6255 processSync(mapper);
6256 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006257 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006258 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006259
6260 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006261 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006262 ASSERT_EQ(0, motionArgs.buttonState);
6263
Michael Wrightd02c5b62014-02-10 15:10:22 -08006264 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6265 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6266 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6267
6268 // press BTN_FORWARD, release BTN_FORWARD
6269 processKey(mapper, BTN_FORWARD, 1);
6270 processSync(mapper);
6271 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6272 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6273 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006274
Michael Wrightd02c5b62014-02-10 15:10:22 -08006275 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006276 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006277 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6278
6279 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6280 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6281 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006282
6283 processKey(mapper, BTN_FORWARD, 0);
6284 processSync(mapper);
6285 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006286 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006287 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006288
6289 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006290 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006291 ASSERT_EQ(0, motionArgs.buttonState);
6292
Michael Wrightd02c5b62014-02-10 15:10:22 -08006293 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6294 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6295 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6296
6297 // press BTN_EXTRA, release BTN_EXTRA
6298 processKey(mapper, BTN_EXTRA, 1);
6299 processSync(mapper);
6300 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6301 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6302 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006303
Michael Wrightd02c5b62014-02-10 15:10:22 -08006304 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006305 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006306 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6307
6308 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6309 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6310 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006311
6312 processKey(mapper, BTN_EXTRA, 0);
6313 processSync(mapper);
6314 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006315 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006316 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006317
6318 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006319 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006320 ASSERT_EQ(0, motionArgs.buttonState);
6321
Michael Wrightd02c5b62014-02-10 15:10:22 -08006322 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6323 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6324 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6325
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006326 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6327
Michael Wrightd02c5b62014-02-10 15:10:22 -08006328 // press BTN_STYLUS, release BTN_STYLUS
6329 processKey(mapper, BTN_STYLUS, 1);
6330 processSync(mapper);
6331 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6332 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006333 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
6334
6335 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6336 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6337 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006338
6339 processKey(mapper, BTN_STYLUS, 0);
6340 processSync(mapper);
6341 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006342 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006343 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006344
6345 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006346 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006347 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006348
6349 // press BTN_STYLUS2, release BTN_STYLUS2
6350 processKey(mapper, BTN_STYLUS2, 1);
6351 processSync(mapper);
6352 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6353 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006354 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
6355
6356 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6357 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6358 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006359
6360 processKey(mapper, BTN_STYLUS2, 0);
6361 processSync(mapper);
6362 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006363 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006364 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006365
6366 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006367 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006368 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006369
6370 // release touch
6371 processUp(mapper);
6372 processSync(mapper);
6373 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6374 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6375 ASSERT_EQ(0, motionArgs.buttonState);
6376}
6377
6378TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006379 addConfigurationProperty("touch.deviceType", "touchScreen");
6380 prepareDisplay(DISPLAY_ORIENTATION_0);
6381 prepareButtons();
6382 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006383 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006384
6385 NotifyMotionArgs motionArgs;
6386
6387 // default tool type is finger
6388 processDown(mapper, 100, 200);
6389 processSync(mapper);
6390 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6391 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6392 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6393
6394 // eraser
6395 processKey(mapper, BTN_TOOL_RUBBER, 1);
6396 processSync(mapper);
6397 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6398 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6399 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6400
6401 // stylus
6402 processKey(mapper, BTN_TOOL_RUBBER, 0);
6403 processKey(mapper, BTN_TOOL_PEN, 1);
6404 processSync(mapper);
6405 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6406 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6407 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6408
6409 // brush
6410 processKey(mapper, BTN_TOOL_PEN, 0);
6411 processKey(mapper, BTN_TOOL_BRUSH, 1);
6412 processSync(mapper);
6413 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6414 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6415 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6416
6417 // pencil
6418 processKey(mapper, BTN_TOOL_BRUSH, 0);
6419 processKey(mapper, BTN_TOOL_PENCIL, 1);
6420 processSync(mapper);
6421 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6422 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6423 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6424
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08006425 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08006426 processKey(mapper, BTN_TOOL_PENCIL, 0);
6427 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
6428 processSync(mapper);
6429 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6430 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6431 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6432
6433 // mouse
6434 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
6435 processKey(mapper, BTN_TOOL_MOUSE, 1);
6436 processSync(mapper);
6437 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6438 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6439 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6440
6441 // lens
6442 processKey(mapper, BTN_TOOL_MOUSE, 0);
6443 processKey(mapper, BTN_TOOL_LENS, 1);
6444 processSync(mapper);
6445 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6446 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6447 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6448
6449 // double-tap
6450 processKey(mapper, BTN_TOOL_LENS, 0);
6451 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
6452 processSync(mapper);
6453 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6454 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6455 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6456
6457 // triple-tap
6458 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
6459 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
6460 processSync(mapper);
6461 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6462 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6463 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6464
6465 // quad-tap
6466 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
6467 processKey(mapper, BTN_TOOL_QUADTAP, 1);
6468 processSync(mapper);
6469 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6470 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6471 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6472
6473 // finger
6474 processKey(mapper, BTN_TOOL_QUADTAP, 0);
6475 processKey(mapper, BTN_TOOL_FINGER, 1);
6476 processSync(mapper);
6477 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6478 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6479 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6480
6481 // stylus trumps finger
6482 processKey(mapper, BTN_TOOL_PEN, 1);
6483 processSync(mapper);
6484 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6485 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6486 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6487
6488 // eraser trumps stylus
6489 processKey(mapper, BTN_TOOL_RUBBER, 1);
6490 processSync(mapper);
6491 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6492 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6493 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6494
6495 // mouse trumps eraser
6496 processKey(mapper, BTN_TOOL_MOUSE, 1);
6497 processSync(mapper);
6498 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6499 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6500 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6501
6502 // back to default tool type
6503 processKey(mapper, BTN_TOOL_MOUSE, 0);
6504 processKey(mapper, BTN_TOOL_RUBBER, 0);
6505 processKey(mapper, BTN_TOOL_PEN, 0);
6506 processKey(mapper, BTN_TOOL_FINGER, 0);
6507 processSync(mapper);
6508 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6509 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6510 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6511}
6512
6513TEST_F(SingleTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006514 addConfigurationProperty("touch.deviceType", "touchScreen");
6515 prepareDisplay(DISPLAY_ORIENTATION_0);
6516 prepareButtons();
6517 prepareAxes(POSITION);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006518 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_FINGER, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006519 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006520
6521 NotifyMotionArgs motionArgs;
6522
6523 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
6524 processKey(mapper, BTN_TOOL_FINGER, 1);
6525 processMove(mapper, 100, 200);
6526 processSync(mapper);
6527 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6528 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6529 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6530 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6531
6532 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6533 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6534 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6535 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6536
6537 // move a little
6538 processMove(mapper, 150, 250);
6539 processSync(mapper);
6540 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6541 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6542 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6543 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6544
6545 // down when BTN_TOUCH is pressed, pressure defaults to 1
6546 processKey(mapper, BTN_TOUCH, 1);
6547 processSync(mapper);
6548 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6549 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6550 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6551 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6552
6553 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6554 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6555 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6556 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6557
6558 // up when BTN_TOUCH is released, hover restored
6559 processKey(mapper, BTN_TOUCH, 0);
6560 processSync(mapper);
6561 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6562 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6563 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6564 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6565
6566 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6567 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6568 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6569 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6570
6571 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6572 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6573 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6574 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6575
6576 // exit hover when pointer goes away
6577 processKey(mapper, BTN_TOOL_FINGER, 0);
6578 processSync(mapper);
6579 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6580 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6581 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6582 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6583}
6584
6585TEST_F(SingleTouchInputMapperTest, Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006586 addConfigurationProperty("touch.deviceType", "touchScreen");
6587 prepareDisplay(DISPLAY_ORIENTATION_0);
6588 prepareButtons();
6589 prepareAxes(POSITION | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006590 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006591
6592 NotifyMotionArgs motionArgs;
6593
6594 // initially hovering because pressure is 0
6595 processDown(mapper, 100, 200);
6596 processPressure(mapper, 0);
6597 processSync(mapper);
6598 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6599 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6600 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6601 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6602
6603 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6604 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6605 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6606 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6607
6608 // move a little
6609 processMove(mapper, 150, 250);
6610 processSync(mapper);
6611 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6612 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6613 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6614 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6615
6616 // down when pressure is non-zero
6617 processPressure(mapper, RAW_PRESSURE_MAX);
6618 processSync(mapper);
6619 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6620 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6621 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6622 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6623
6624 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6625 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6626 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6627 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6628
6629 // up when pressure becomes 0, hover restored
6630 processPressure(mapper, 0);
6631 processSync(mapper);
6632 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6633 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6634 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6635 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6636
6637 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6638 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6639 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6640 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6641
6642 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6643 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6644 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6645 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6646
6647 // exit hover when pointer goes away
6648 processUp(mapper);
6649 processSync(mapper);
6650 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6651 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6652 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6653 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6654}
6655
lilinnane34bc532022-07-19 16:00:50 +08006656TEST_F(SingleTouchInputMapperTest,
6657 Process_WhenViewportDisplayIdChanged_TouchIsCanceledAndDeviceIsReset) {
6658 addConfigurationProperty("touch.deviceType", "touchScreen");
6659 prepareDisplay(DISPLAY_ORIENTATION_0);
6660 prepareButtons();
6661 prepareAxes(POSITION);
6662 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6663 NotifyMotionArgs motionArgs;
6664
6665 // Down.
6666 int32_t x = 100;
6667 int32_t y = 200;
6668 processDown(mapper, x, y);
6669 processSync(mapper);
6670
6671 // We should receive a down event
6672 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6673 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6674
6675 // Change display id
6676 clearViewports();
6677 prepareSecondaryDisplay(ViewportType::INTERNAL);
6678
6679 // We should receive a cancel event
6680 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6681 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
6682 // Then receive reset called
6683 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6684}
6685
Prabir Pradhan5632d622021-09-06 07:57:20 -07006686// --- TouchDisplayProjectionTest ---
6687
6688class TouchDisplayProjectionTest : public SingleTouchInputMapperTest {
6689public:
6690 // The values inside DisplayViewport are expected to be pre-rotated. This updates the current
6691 // DisplayViewport to pre-rotate the values. The viewport's physical display will be set to the
6692 // rotated equivalent of the given un-rotated physical display bounds.
6693 void configurePhysicalDisplay(int32_t orientation, Rect naturalPhysicalDisplay) {
6694 uint32_t inverseRotationFlags;
6695 auto width = DISPLAY_WIDTH;
6696 auto height = DISPLAY_HEIGHT;
6697 switch (orientation) {
6698 case DISPLAY_ORIENTATION_90:
6699 inverseRotationFlags = ui::Transform::ROT_270;
6700 std::swap(width, height);
6701 break;
6702 case DISPLAY_ORIENTATION_180:
6703 inverseRotationFlags = ui::Transform::ROT_180;
6704 break;
6705 case DISPLAY_ORIENTATION_270:
6706 inverseRotationFlags = ui::Transform::ROT_90;
6707 std::swap(width, height);
6708 break;
6709 case DISPLAY_ORIENTATION_0:
6710 inverseRotationFlags = ui::Transform::ROT_0;
6711 break;
6712 default:
6713 FAIL() << "Invalid orientation: " << orientation;
6714 }
6715
6716 const ui::Transform rotation(inverseRotationFlags, width, height);
6717 const Rect rotatedPhysicalDisplay = rotation.transform(naturalPhysicalDisplay);
6718
6719 std::optional<DisplayViewport> internalViewport =
6720 *mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6721 DisplayViewport& v = *internalViewport;
6722 v.displayId = DISPLAY_ID;
6723 v.orientation = orientation;
6724
6725 v.logicalLeft = 0;
6726 v.logicalTop = 0;
6727 v.logicalRight = 100;
6728 v.logicalBottom = 100;
6729
6730 v.physicalLeft = rotatedPhysicalDisplay.left;
6731 v.physicalTop = rotatedPhysicalDisplay.top;
6732 v.physicalRight = rotatedPhysicalDisplay.right;
6733 v.physicalBottom = rotatedPhysicalDisplay.bottom;
6734
6735 v.deviceWidth = width;
6736 v.deviceHeight = height;
6737
6738 v.isActive = true;
6739 v.uniqueId = UNIQUE_ID;
6740 v.type = ViewportType::INTERNAL;
6741 mFakePolicy->updateViewport(v);
6742 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
6743 }
6744
6745 void assertReceivedMove(const Point& point) {
6746 NotifyMotionArgs motionArgs;
6747 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6748 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6749 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6750 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], point.x, point.y,
6751 1, 0, 0, 0, 0, 0, 0, 0));
6752 }
6753};
6754
6755TEST_F(TouchDisplayProjectionTest, IgnoresTouchesOutsidePhysicalDisplay) {
6756 addConfigurationProperty("touch.deviceType", "touchScreen");
6757 prepareDisplay(DISPLAY_ORIENTATION_0);
6758
6759 prepareButtons();
6760 prepareAxes(POSITION);
6761 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6762
6763 NotifyMotionArgs motionArgs;
6764
6765 // Configure the DisplayViewport such that the logical display maps to a subsection of
6766 // the display panel called the physical display. Here, the physical display is bounded by the
6767 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
6768 static const Rect kPhysicalDisplay{10, 20, 70, 160};
6769 static const std::array<Point, 6> kPointsOutsidePhysicalDisplay{
6770 {{-10, -10}, {0, 0}, {5, 100}, {50, 15}, {75, 100}, {50, 165}}};
6771
6772 for (auto orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90, DISPLAY_ORIENTATION_180,
6773 DISPLAY_ORIENTATION_270}) {
6774 configurePhysicalDisplay(orientation, kPhysicalDisplay);
6775
6776 // Touches outside the physical display should be ignored, and should not generate any
6777 // events. Ensure touches at the following points that lie outside of the physical display
6778 // area do not generate any events.
6779 for (const auto& point : kPointsOutsidePhysicalDisplay) {
6780 processDown(mapper, toRawX(point.x), toRawY(point.y));
6781 processSync(mapper);
6782 processUp(mapper);
6783 processSync(mapper);
6784 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled())
6785 << "Unexpected event generated for touch outside physical display at point: "
6786 << point.x << ", " << point.y;
6787 }
6788 }
6789}
6790
6791TEST_F(TouchDisplayProjectionTest, EmitsTouchDownAfterEnteringPhysicalDisplay) {
6792 addConfigurationProperty("touch.deviceType", "touchScreen");
6793 prepareDisplay(DISPLAY_ORIENTATION_0);
6794
6795 prepareButtons();
6796 prepareAxes(POSITION);
6797 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6798
6799 NotifyMotionArgs motionArgs;
6800
6801 // Configure the DisplayViewport such that the logical display maps to a subsection of
6802 // the display panel called the physical display. Here, the physical display is bounded by the
6803 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
6804 static const Rect kPhysicalDisplay{10, 20, 70, 160};
6805
6806 for (auto orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90, DISPLAY_ORIENTATION_180,
6807 DISPLAY_ORIENTATION_270}) {
6808 configurePhysicalDisplay(orientation, kPhysicalDisplay);
6809
6810 // Touches that start outside the physical display should be ignored until it enters the
6811 // physical display bounds, at which point it should generate a down event. Start a touch at
6812 // the point (5, 100), which is outside the physical display bounds.
6813 static const Point kOutsidePoint{5, 100};
6814 processDown(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
6815 processSync(mapper);
6816 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6817
6818 // Move the touch into the physical display area. This should generate a pointer down.
6819 processMove(mapper, toRawX(11), toRawY(21));
6820 processSync(mapper);
6821 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6822 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6823 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6824 ASSERT_NO_FATAL_FAILURE(
6825 assertPointerCoords(motionArgs.pointerCoords[0], 11, 21, 1, 0, 0, 0, 0, 0, 0, 0));
6826
6827 // Move the touch inside the physical display area. This should generate a pointer move.
6828 processMove(mapper, toRawX(69), toRawY(159));
6829 processSync(mapper);
6830 assertReceivedMove({69, 159});
6831
6832 // Move outside the physical display area. Since the pointer is already down, this should
6833 // now continue generating events.
6834 processMove(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
6835 processSync(mapper);
6836 assertReceivedMove(kOutsidePoint);
6837
6838 // Release. This should generate a pointer up.
6839 processUp(mapper);
6840 processSync(mapper);
6841 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6842 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6843 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], kOutsidePoint.x,
6844 kOutsidePoint.y, 1, 0, 0, 0, 0, 0, 0, 0));
6845
6846 // Ensure no more events were generated.
6847 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6848 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6849 }
6850}
6851
Michael Wrightd02c5b62014-02-10 15:10:22 -08006852// --- MultiTouchInputMapperTest ---
6853
6854class MultiTouchInputMapperTest : public TouchInputMapperTest {
6855protected:
6856 void prepareAxes(int axes);
6857
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006858 void processPosition(MultiTouchInputMapper& mapper, int32_t x, int32_t y);
6859 void processTouchMajor(MultiTouchInputMapper& mapper, int32_t touchMajor);
6860 void processTouchMinor(MultiTouchInputMapper& mapper, int32_t touchMinor);
6861 void processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor);
6862 void processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor);
6863 void processOrientation(MultiTouchInputMapper& mapper, int32_t orientation);
6864 void processPressure(MultiTouchInputMapper& mapper, int32_t pressure);
6865 void processDistance(MultiTouchInputMapper& mapper, int32_t distance);
6866 void processId(MultiTouchInputMapper& mapper, int32_t id);
6867 void processSlot(MultiTouchInputMapper& mapper, int32_t slot);
6868 void processToolType(MultiTouchInputMapper& mapper, int32_t toolType);
6869 void processKey(MultiTouchInputMapper& mapper, int32_t code, int32_t value);
6870 void processMTSync(MultiTouchInputMapper& mapper);
6871 void processSync(MultiTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006872};
6873
6874void MultiTouchInputMapperTest::prepareAxes(int axes) {
6875 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006876 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
6877 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006878 }
6879 if (axes & TOUCH) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006880 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN,
6881 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006882 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006883 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN,
6884 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006885 }
6886 }
6887 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006888 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
6889 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006890 if (axes & MINOR) {
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08006891 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006892 RAW_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006893 }
6894 }
6895 if (axes & ORIENTATION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006896 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_ORIENTATION, RAW_ORIENTATION_MIN,
6897 RAW_ORIENTATION_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006898 }
6899 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006900 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_PRESSURE, RAW_PRESSURE_MIN,
6901 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006902 }
6903 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006904 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_DISTANCE, RAW_DISTANCE_MIN,
6905 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006906 }
6907 if (axes & ID) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006908 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX, 0,
6909 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006910 }
6911 if (axes & SLOT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006912 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
6913 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_MT_SLOT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006914 }
6915 if (axes & TOOL_TYPE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006916 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOOL_TYPE, 0, MT_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006917 }
6918}
6919
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006920void MultiTouchInputMapperTest::processPosition(MultiTouchInputMapper& mapper, int32_t x,
6921 int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006922 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_X, x);
6923 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006924}
6925
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006926void MultiTouchInputMapperTest::processTouchMajor(MultiTouchInputMapper& mapper,
6927 int32_t touchMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006928 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006929}
6930
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006931void MultiTouchInputMapperTest::processTouchMinor(MultiTouchInputMapper& mapper,
6932 int32_t touchMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006933 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006934}
6935
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006936void MultiTouchInputMapperTest::processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006937 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006938}
6939
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006940void MultiTouchInputMapperTest::processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006941 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006942}
6943
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006944void MultiTouchInputMapperTest::processOrientation(MultiTouchInputMapper& mapper,
6945 int32_t orientation) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006946 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_ORIENTATION, orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006947}
6948
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006949void MultiTouchInputMapperTest::processPressure(MultiTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006950 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006951}
6952
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006953void MultiTouchInputMapperTest::processDistance(MultiTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006954 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006955}
6956
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006957void MultiTouchInputMapperTest::processId(MultiTouchInputMapper& mapper, int32_t id) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006958 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TRACKING_ID, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006959}
6960
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006961void MultiTouchInputMapperTest::processSlot(MultiTouchInputMapper& mapper, int32_t slot) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006962 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_SLOT, slot);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006963}
6964
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006965void MultiTouchInputMapperTest::processToolType(MultiTouchInputMapper& mapper, int32_t toolType) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006966 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006967}
6968
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006969void MultiTouchInputMapperTest::processKey(MultiTouchInputMapper& mapper, int32_t code,
6970 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006971 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006972}
6973
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006974void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006975 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_MT_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006976}
6977
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006978void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006979 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006980}
6981
Michael Wrightd02c5b62014-02-10 15:10:22 -08006982TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006983 addConfigurationProperty("touch.deviceType", "touchScreen");
6984 prepareDisplay(DISPLAY_ORIENTATION_0);
6985 prepareAxes(POSITION);
6986 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006987 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006988
arthurhungdcef2dc2020-08-11 14:47:50 +08006989 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006990
6991 NotifyMotionArgs motionArgs;
6992
6993 // Two fingers down at once.
6994 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
6995 processPosition(mapper, x1, y1);
6996 processMTSync(mapper);
6997 processPosition(mapper, x2, y2);
6998 processMTSync(mapper);
6999 processSync(mapper);
7000
7001 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7002 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7003 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7004 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7005 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7006 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7007 ASSERT_EQ(0, motionArgs.flags);
7008 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7009 ASSERT_EQ(0, motionArgs.buttonState);
7010 ASSERT_EQ(0, motionArgs.edgeFlags);
7011 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7012 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7013 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7014 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7015 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7016 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7017 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7018 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7019
7020 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7021 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7022 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7023 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7024 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007025 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007026 ASSERT_EQ(0, motionArgs.flags);
7027 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7028 ASSERT_EQ(0, motionArgs.buttonState);
7029 ASSERT_EQ(0, motionArgs.edgeFlags);
7030 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7031 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7032 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7033 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7034 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7035 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7036 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7037 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7038 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7039 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7040 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7041 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7042
7043 // Move.
7044 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7045 processPosition(mapper, x1, y1);
7046 processMTSync(mapper);
7047 processPosition(mapper, x2, y2);
7048 processMTSync(mapper);
7049 processSync(mapper);
7050
7051 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7052 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7053 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7054 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7055 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7056 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7057 ASSERT_EQ(0, motionArgs.flags);
7058 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7059 ASSERT_EQ(0, motionArgs.buttonState);
7060 ASSERT_EQ(0, motionArgs.edgeFlags);
7061 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7062 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7063 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7064 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7065 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7066 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7067 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7068 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7069 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7070 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7071 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7072 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7073
7074 // First finger up.
7075 x2 += 15; y2 -= 20;
7076 processPosition(mapper, x2, y2);
7077 processMTSync(mapper);
7078 processSync(mapper);
7079
7080 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7081 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7082 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7083 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7084 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007085 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007086 ASSERT_EQ(0, motionArgs.flags);
7087 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7088 ASSERT_EQ(0, motionArgs.buttonState);
7089 ASSERT_EQ(0, motionArgs.edgeFlags);
7090 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7091 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7092 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7093 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7094 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7095 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7096 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7097 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7098 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7099 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7100 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7101 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7102
7103 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7104 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7105 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7106 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7107 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7108 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7109 ASSERT_EQ(0, motionArgs.flags);
7110 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7111 ASSERT_EQ(0, motionArgs.buttonState);
7112 ASSERT_EQ(0, motionArgs.edgeFlags);
7113 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7114 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7115 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7116 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7117 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7118 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7119 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7120 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7121
7122 // Move.
7123 x2 += 20; y2 -= 25;
7124 processPosition(mapper, x2, y2);
7125 processMTSync(mapper);
7126 processSync(mapper);
7127
7128 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7129 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7130 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7131 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7132 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7133 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7134 ASSERT_EQ(0, motionArgs.flags);
7135 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7136 ASSERT_EQ(0, motionArgs.buttonState);
7137 ASSERT_EQ(0, motionArgs.edgeFlags);
7138 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7139 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7140 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7141 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7142 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7143 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7144 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7145 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7146
7147 // New finger down.
7148 int32_t x3 = 700, y3 = 300;
7149 processPosition(mapper, x2, y2);
7150 processMTSync(mapper);
7151 processPosition(mapper, x3, y3);
7152 processMTSync(mapper);
7153 processSync(mapper);
7154
7155 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7156 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7157 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7158 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7159 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007160 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007161 ASSERT_EQ(0, motionArgs.flags);
7162 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7163 ASSERT_EQ(0, motionArgs.buttonState);
7164 ASSERT_EQ(0, motionArgs.edgeFlags);
7165 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7166 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7167 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7168 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7169 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7170 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7171 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7172 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7173 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7174 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7175 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7176 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7177
7178 // Second finger up.
7179 x3 += 30; y3 -= 20;
7180 processPosition(mapper, x3, y3);
7181 processMTSync(mapper);
7182 processSync(mapper);
7183
7184 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7185 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7186 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7187 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7188 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007189 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007190 ASSERT_EQ(0, motionArgs.flags);
7191 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7192 ASSERT_EQ(0, motionArgs.buttonState);
7193 ASSERT_EQ(0, motionArgs.edgeFlags);
7194 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7195 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7196 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7197 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7198 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7199 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7200 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7201 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7202 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7203 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7204 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7205 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7206
7207 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7208 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7209 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7210 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7211 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7212 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7213 ASSERT_EQ(0, motionArgs.flags);
7214 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7215 ASSERT_EQ(0, motionArgs.buttonState);
7216 ASSERT_EQ(0, motionArgs.edgeFlags);
7217 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7218 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7219 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7220 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7221 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7222 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7223 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7224 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7225
7226 // Last finger up.
7227 processMTSync(mapper);
7228 processSync(mapper);
7229
7230 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7231 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7232 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7233 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7234 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7235 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7236 ASSERT_EQ(0, motionArgs.flags);
7237 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7238 ASSERT_EQ(0, motionArgs.buttonState);
7239 ASSERT_EQ(0, motionArgs.edgeFlags);
7240 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7241 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7242 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7243 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7244 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7245 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7246 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7247 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7248
7249 // Should not have sent any more keys or motions.
7250 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7251 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7252}
7253
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007254TEST_F(MultiTouchInputMapperTest, AxisResolution_IsPopulated) {
7255 addConfigurationProperty("touch.deviceType", "touchScreen");
7256 prepareDisplay(DISPLAY_ORIENTATION_0);
7257
7258 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
7259 /*fuzz*/ 0, /*resolution*/ 10);
7260 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
7261 /*fuzz*/ 0, /*resolution*/ 11);
7262 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
7263 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 12);
7264 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
7265 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 13);
7266 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7267 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 14);
7268 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7269 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 15);
7270
7271 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7272
7273 // X and Y axes
7274 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_X, 10 / X_PRECISION);
7275 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_Y, 11 / Y_PRECISION);
7276 // Touch major and minor
7277 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR, 12 * GEOMETRIC_SCALE);
7278 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR, 13 * GEOMETRIC_SCALE);
7279 // Tool major and minor
7280 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR, 14 * GEOMETRIC_SCALE);
7281 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR, 15 * GEOMETRIC_SCALE);
7282}
7283
7284TEST_F(MultiTouchInputMapperTest, TouchMajorAndMinorAxes_DoNotAppearIfNotSupported) {
7285 addConfigurationProperty("touch.deviceType", "touchScreen");
7286 prepareDisplay(DISPLAY_ORIENTATION_0);
7287
7288 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
7289 /*fuzz*/ 0, /*resolution*/ 10);
7290 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
7291 /*fuzz*/ 0, /*resolution*/ 11);
7292
7293 // We do not add ABS_MT_TOUCH_MAJOR / MINOR or ABS_MT_WIDTH_MAJOR / MINOR axes
7294
7295 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7296
7297 // Touch major and minor
7298 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR);
7299 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR);
7300 // Tool major and minor
7301 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR);
7302 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR);
7303}
7304
Michael Wrightd02c5b62014-02-10 15:10:22 -08007305TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007306 addConfigurationProperty("touch.deviceType", "touchScreen");
7307 prepareDisplay(DISPLAY_ORIENTATION_0);
7308 prepareAxes(POSITION | ID);
7309 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007310 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007311
arthurhungdcef2dc2020-08-11 14:47:50 +08007312 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007313
7314 NotifyMotionArgs motionArgs;
7315
7316 // Two fingers down at once.
7317 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7318 processPosition(mapper, x1, y1);
7319 processId(mapper, 1);
7320 processMTSync(mapper);
7321 processPosition(mapper, x2, y2);
7322 processId(mapper, 2);
7323 processMTSync(mapper);
7324 processSync(mapper);
7325
7326 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7327 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7328 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7329 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7330 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7331 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7332 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7333
7334 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007335 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007336 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7337 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7338 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7339 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7340 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7341 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7342 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7343 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7344 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7345
7346 // Move.
7347 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7348 processPosition(mapper, x1, y1);
7349 processId(mapper, 1);
7350 processMTSync(mapper);
7351 processPosition(mapper, x2, y2);
7352 processId(mapper, 2);
7353 processMTSync(mapper);
7354 processSync(mapper);
7355
7356 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7357 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7358 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7359 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7360 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7361 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7362 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7363 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7364 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7365 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7366 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7367
7368 // First finger up.
7369 x2 += 15; y2 -= 20;
7370 processPosition(mapper, x2, y2);
7371 processId(mapper, 2);
7372 processMTSync(mapper);
7373 processSync(mapper);
7374
7375 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007376 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007377 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7378 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7379 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7380 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7381 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7382 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7383 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7384 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7385 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7386
7387 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7388 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7389 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7390 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7391 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7392 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7393 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7394
7395 // Move.
7396 x2 += 20; y2 -= 25;
7397 processPosition(mapper, x2, y2);
7398 processId(mapper, 2);
7399 processMTSync(mapper);
7400 processSync(mapper);
7401
7402 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7403 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7404 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7405 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7406 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7407 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7408 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7409
7410 // New finger down.
7411 int32_t x3 = 700, y3 = 300;
7412 processPosition(mapper, x2, y2);
7413 processId(mapper, 2);
7414 processMTSync(mapper);
7415 processPosition(mapper, x3, y3);
7416 processId(mapper, 3);
7417 processMTSync(mapper);
7418 processSync(mapper);
7419
7420 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007421 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007422 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7423 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7424 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7425 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7426 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7427 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7428 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7429 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7430 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7431
7432 // Second finger up.
7433 x3 += 30; y3 -= 20;
7434 processPosition(mapper, x3, y3);
7435 processId(mapper, 3);
7436 processMTSync(mapper);
7437 processSync(mapper);
7438
7439 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007440 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007441 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7442 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7443 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7444 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7445 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7446 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7447 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7448 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7449 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7450
7451 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7452 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7453 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7454 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7455 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7456 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7457 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7458
7459 // Last finger up.
7460 processMTSync(mapper);
7461 processSync(mapper);
7462
7463 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7464 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7465 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7466 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7467 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7468 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7469 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7470
7471 // Should not have sent any more keys or motions.
7472 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7473 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7474}
7475
7476TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007477 addConfigurationProperty("touch.deviceType", "touchScreen");
7478 prepareDisplay(DISPLAY_ORIENTATION_0);
7479 prepareAxes(POSITION | ID | SLOT);
7480 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007481 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007482
arthurhungdcef2dc2020-08-11 14:47:50 +08007483 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007484
7485 NotifyMotionArgs motionArgs;
7486
7487 // Two fingers down at once.
7488 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7489 processPosition(mapper, x1, y1);
7490 processId(mapper, 1);
7491 processSlot(mapper, 1);
7492 processPosition(mapper, x2, y2);
7493 processId(mapper, 2);
7494 processSync(mapper);
7495
7496 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7497 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7498 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7499 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7500 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7501 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7502 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7503
7504 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007505 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007506 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7507 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7508 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7509 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7510 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7511 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7512 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7513 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7514 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7515
7516 // Move.
7517 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7518 processSlot(mapper, 0);
7519 processPosition(mapper, x1, y1);
7520 processSlot(mapper, 1);
7521 processPosition(mapper, x2, y2);
7522 processSync(mapper);
7523
7524 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7525 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7526 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7527 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7528 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7529 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7530 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7531 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7532 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7533 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7534 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7535
7536 // First finger up.
7537 x2 += 15; y2 -= 20;
7538 processSlot(mapper, 0);
7539 processId(mapper, -1);
7540 processSlot(mapper, 1);
7541 processPosition(mapper, x2, y2);
7542 processSync(mapper);
7543
7544 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007545 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007546 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7547 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7548 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7549 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7550 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7551 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7552 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7553 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7554 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7555
7556 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7557 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7558 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7559 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7560 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7561 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7562 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7563
7564 // Move.
7565 x2 += 20; y2 -= 25;
7566 processPosition(mapper, x2, y2);
7567 processSync(mapper);
7568
7569 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7570 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7571 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7572 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7573 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7574 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7575 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7576
7577 // New finger down.
7578 int32_t x3 = 700, y3 = 300;
7579 processPosition(mapper, x2, y2);
7580 processSlot(mapper, 0);
7581 processId(mapper, 3);
7582 processPosition(mapper, x3, y3);
7583 processSync(mapper);
7584
7585 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007586 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007587 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7588 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7589 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7590 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7591 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7592 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7593 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7594 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7595 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7596
7597 // Second finger up.
7598 x3 += 30; y3 -= 20;
7599 processSlot(mapper, 1);
7600 processId(mapper, -1);
7601 processSlot(mapper, 0);
7602 processPosition(mapper, x3, y3);
7603 processSync(mapper);
7604
7605 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007606 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007607 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7608 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7609 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7610 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7611 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7612 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7613 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7614 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7615 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7616
7617 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7618 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7619 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7620 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7621 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7622 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7623 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7624
7625 // Last finger up.
7626 processId(mapper, -1);
7627 processSync(mapper);
7628
7629 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7630 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7631 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7632 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7633 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7634 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7635 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7636
7637 // Should not have sent any more keys or motions.
7638 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7639 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7640}
7641
7642TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007643 addConfigurationProperty("touch.deviceType", "touchScreen");
7644 prepareDisplay(DISPLAY_ORIENTATION_0);
7645 prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007646 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007647
7648 // These calculations are based on the input device calibration documentation.
7649 int32_t rawX = 100;
7650 int32_t rawY = 200;
7651 int32_t rawTouchMajor = 7;
7652 int32_t rawTouchMinor = 6;
7653 int32_t rawToolMajor = 9;
7654 int32_t rawToolMinor = 8;
7655 int32_t rawPressure = 11;
7656 int32_t rawDistance = 0;
7657 int32_t rawOrientation = 3;
7658 int32_t id = 5;
7659
7660 float x = toDisplayX(rawX);
7661 float y = toDisplayY(rawY);
7662 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
7663 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
7664 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
7665 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
7666 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
7667 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
7668 float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
7669 float distance = float(rawDistance);
7670
7671 processPosition(mapper, rawX, rawY);
7672 processTouchMajor(mapper, rawTouchMajor);
7673 processTouchMinor(mapper, rawTouchMinor);
7674 processToolMajor(mapper, rawToolMajor);
7675 processToolMinor(mapper, rawToolMinor);
7676 processPressure(mapper, rawPressure);
7677 processOrientation(mapper, rawOrientation);
7678 processDistance(mapper, rawDistance);
7679 processId(mapper, id);
7680 processMTSync(mapper);
7681 processSync(mapper);
7682
7683 NotifyMotionArgs args;
7684 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7685 ASSERT_EQ(0, args.pointerProperties[0].id);
7686 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7687 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
7688 orientation, distance));
7689}
7690
7691TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007692 addConfigurationProperty("touch.deviceType", "touchScreen");
7693 prepareDisplay(DISPLAY_ORIENTATION_0);
7694 prepareAxes(POSITION | TOUCH | TOOL | MINOR);
7695 addConfigurationProperty("touch.size.calibration", "geometric");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007696 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007697
7698 // These calculations are based on the input device calibration documentation.
7699 int32_t rawX = 100;
7700 int32_t rawY = 200;
7701 int32_t rawTouchMajor = 140;
7702 int32_t rawTouchMinor = 120;
7703 int32_t rawToolMajor = 180;
7704 int32_t rawToolMinor = 160;
7705
7706 float x = toDisplayX(rawX);
7707 float y = toDisplayY(rawY);
7708 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
7709 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
7710 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
7711 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
7712 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
7713
7714 processPosition(mapper, rawX, rawY);
7715 processTouchMajor(mapper, rawTouchMajor);
7716 processTouchMinor(mapper, rawTouchMinor);
7717 processToolMajor(mapper, rawToolMajor);
7718 processToolMinor(mapper, rawToolMinor);
7719 processMTSync(mapper);
7720 processSync(mapper);
7721
7722 NotifyMotionArgs args;
7723 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7724 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7725 x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
7726}
7727
7728TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007729 addConfigurationProperty("touch.deviceType", "touchScreen");
7730 prepareDisplay(DISPLAY_ORIENTATION_0);
7731 prepareAxes(POSITION | TOUCH | TOOL);
7732 addConfigurationProperty("touch.size.calibration", "diameter");
7733 addConfigurationProperty("touch.size.scale", "10");
7734 addConfigurationProperty("touch.size.bias", "160");
7735 addConfigurationProperty("touch.size.isSummed", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007736 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007737
7738 // These calculations are based on the input device calibration documentation.
7739 // Note: We only provide a single common touch/tool value because the device is assumed
7740 // not to emit separate values for each pointer (isSummed = 1).
7741 int32_t rawX = 100;
7742 int32_t rawY = 200;
7743 int32_t rawX2 = 150;
7744 int32_t rawY2 = 250;
7745 int32_t rawTouchMajor = 5;
7746 int32_t rawToolMajor = 8;
7747
7748 float x = toDisplayX(rawX);
7749 float y = toDisplayY(rawY);
7750 float x2 = toDisplayX(rawX2);
7751 float y2 = toDisplayY(rawY2);
7752 float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
7753 float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
7754 float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
7755
7756 processPosition(mapper, rawX, rawY);
7757 processTouchMajor(mapper, rawTouchMajor);
7758 processToolMajor(mapper, rawToolMajor);
7759 processMTSync(mapper);
7760 processPosition(mapper, rawX2, rawY2);
7761 processTouchMajor(mapper, rawTouchMajor);
7762 processToolMajor(mapper, rawToolMajor);
7763 processMTSync(mapper);
7764 processSync(mapper);
7765
7766 NotifyMotionArgs args;
7767 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7768 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
7769
7770 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007771 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007772 ASSERT_EQ(size_t(2), args.pointerCount);
7773 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7774 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
7775 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
7776 x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
7777}
7778
7779TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007780 addConfigurationProperty("touch.deviceType", "touchScreen");
7781 prepareDisplay(DISPLAY_ORIENTATION_0);
7782 prepareAxes(POSITION | TOUCH | TOOL);
7783 addConfigurationProperty("touch.size.calibration", "area");
7784 addConfigurationProperty("touch.size.scale", "43");
7785 addConfigurationProperty("touch.size.bias", "3");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007786 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007787
7788 // These calculations are based on the input device calibration documentation.
7789 int32_t rawX = 100;
7790 int32_t rawY = 200;
7791 int32_t rawTouchMajor = 5;
7792 int32_t rawToolMajor = 8;
7793
7794 float x = toDisplayX(rawX);
7795 float y = toDisplayY(rawY);
7796 float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
7797 float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
7798 float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
7799
7800 processPosition(mapper, rawX, rawY);
7801 processTouchMajor(mapper, rawTouchMajor);
7802 processToolMajor(mapper, rawToolMajor);
7803 processMTSync(mapper);
7804 processSync(mapper);
7805
7806 NotifyMotionArgs args;
7807 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7808 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7809 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
7810}
7811
7812TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007813 addConfigurationProperty("touch.deviceType", "touchScreen");
7814 prepareDisplay(DISPLAY_ORIENTATION_0);
7815 prepareAxes(POSITION | PRESSURE);
7816 addConfigurationProperty("touch.pressure.calibration", "amplitude");
7817 addConfigurationProperty("touch.pressure.scale", "0.01");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007818 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007819
Michael Wrightaa449c92017-12-13 21:21:43 +00007820 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007821 mapper.populateDeviceInfo(&info);
Michael Wrightaa449c92017-12-13 21:21:43 +00007822 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
7823 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TOUCHSCREEN,
7824 0.0f, RAW_PRESSURE_MAX * 0.01, 0.0f, 0.0f));
7825
Michael Wrightd02c5b62014-02-10 15:10:22 -08007826 // These calculations are based on the input device calibration documentation.
7827 int32_t rawX = 100;
7828 int32_t rawY = 200;
7829 int32_t rawPressure = 60;
7830
7831 float x = toDisplayX(rawX);
7832 float y = toDisplayY(rawY);
7833 float pressure = float(rawPressure) * 0.01f;
7834
7835 processPosition(mapper, rawX, rawY);
7836 processPressure(mapper, rawPressure);
7837 processMTSync(mapper);
7838 processSync(mapper);
7839
7840 NotifyMotionArgs args;
7841 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7842 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7843 x, y, pressure, 0, 0, 0, 0, 0, 0, 0));
7844}
7845
7846TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007847 addConfigurationProperty("touch.deviceType", "touchScreen");
7848 prepareDisplay(DISPLAY_ORIENTATION_0);
7849 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007850 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007851
7852 NotifyMotionArgs motionArgs;
7853 NotifyKeyArgs keyArgs;
7854
7855 processId(mapper, 1);
7856 processPosition(mapper, 100, 200);
7857 processSync(mapper);
7858 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7859 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7860 ASSERT_EQ(0, motionArgs.buttonState);
7861
7862 // press BTN_LEFT, release BTN_LEFT
7863 processKey(mapper, BTN_LEFT, 1);
7864 processSync(mapper);
7865 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7866 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7867 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
7868
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007869 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7870 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7871 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
7872
Michael Wrightd02c5b62014-02-10 15:10:22 -08007873 processKey(mapper, BTN_LEFT, 0);
7874 processSync(mapper);
7875 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007876 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007877 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007878
7879 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007880 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007881 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007882
7883 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
7884 processKey(mapper, BTN_RIGHT, 1);
7885 processKey(mapper, BTN_MIDDLE, 1);
7886 processSync(mapper);
7887 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7888 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7889 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
7890 motionArgs.buttonState);
7891
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007892 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7893 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7894 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
7895
7896 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7897 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7898 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
7899 motionArgs.buttonState);
7900
Michael Wrightd02c5b62014-02-10 15:10:22 -08007901 processKey(mapper, BTN_RIGHT, 0);
7902 processSync(mapper);
7903 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007904 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007905 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007906
7907 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007908 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007909 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007910
7911 processKey(mapper, BTN_MIDDLE, 0);
7912 processSync(mapper);
7913 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007914 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007915 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007916
7917 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007918 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007919 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007920
7921 // press BTN_BACK, release BTN_BACK
7922 processKey(mapper, BTN_BACK, 1);
7923 processSync(mapper);
7924 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7925 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
7926 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007927
Michael Wrightd02c5b62014-02-10 15:10:22 -08007928 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007929 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007930 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
7931
7932 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7933 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7934 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007935
7936 processKey(mapper, BTN_BACK, 0);
7937 processSync(mapper);
7938 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007939 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007940 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007941
7942 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007943 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007944 ASSERT_EQ(0, motionArgs.buttonState);
7945
Michael Wrightd02c5b62014-02-10 15:10:22 -08007946 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7947 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
7948 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
7949
7950 // press BTN_SIDE, release BTN_SIDE
7951 processKey(mapper, BTN_SIDE, 1);
7952 processSync(mapper);
7953 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7954 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
7955 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007956
Michael Wrightd02c5b62014-02-10 15:10:22 -08007957 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007958 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007959 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
7960
7961 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7962 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7963 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007964
7965 processKey(mapper, BTN_SIDE, 0);
7966 processSync(mapper);
7967 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007968 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007969 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007970
7971 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007972 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007973 ASSERT_EQ(0, motionArgs.buttonState);
7974
Michael Wrightd02c5b62014-02-10 15:10:22 -08007975 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7976 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
7977 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
7978
7979 // press BTN_FORWARD, release BTN_FORWARD
7980 processKey(mapper, BTN_FORWARD, 1);
7981 processSync(mapper);
7982 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7983 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
7984 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007985
Michael Wrightd02c5b62014-02-10 15:10:22 -08007986 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007987 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007988 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
7989
7990 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7991 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7992 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007993
7994 processKey(mapper, BTN_FORWARD, 0);
7995 processSync(mapper);
7996 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007997 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007998 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007999
8000 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008001 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008002 ASSERT_EQ(0, motionArgs.buttonState);
8003
Michael Wrightd02c5b62014-02-10 15:10:22 -08008004 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8005 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8006 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
8007
8008 // press BTN_EXTRA, release BTN_EXTRA
8009 processKey(mapper, BTN_EXTRA, 1);
8010 processSync(mapper);
8011 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8012 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8013 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008014
Michael Wrightd02c5b62014-02-10 15:10:22 -08008015 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008016 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008017 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
8018
8019 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8020 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8021 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008022
8023 processKey(mapper, BTN_EXTRA, 0);
8024 processSync(mapper);
8025 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008026 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008027 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008028
8029 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008030 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008031 ASSERT_EQ(0, motionArgs.buttonState);
8032
Michael Wrightd02c5b62014-02-10 15:10:22 -08008033 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8034 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8035 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
8036
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008037 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8038
Michael Wrightd02c5b62014-02-10 15:10:22 -08008039 // press BTN_STYLUS, release BTN_STYLUS
8040 processKey(mapper, BTN_STYLUS, 1);
8041 processSync(mapper);
8042 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8043 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008044 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
8045
8046 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8047 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8048 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008049
8050 processKey(mapper, BTN_STYLUS, 0);
8051 processSync(mapper);
8052 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008053 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008054 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008055
8056 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008057 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008058 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008059
8060 // press BTN_STYLUS2, release BTN_STYLUS2
8061 processKey(mapper, BTN_STYLUS2, 1);
8062 processSync(mapper);
8063 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8064 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008065 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
8066
8067 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8068 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8069 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008070
8071 processKey(mapper, BTN_STYLUS2, 0);
8072 processSync(mapper);
8073 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008074 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008075 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008076
8077 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008078 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008079 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008080
8081 // release touch
8082 processId(mapper, -1);
8083 processSync(mapper);
8084 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8085 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8086 ASSERT_EQ(0, motionArgs.buttonState);
8087}
8088
8089TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008090 addConfigurationProperty("touch.deviceType", "touchScreen");
8091 prepareDisplay(DISPLAY_ORIENTATION_0);
8092 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008093 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008094
8095 NotifyMotionArgs motionArgs;
8096
8097 // default tool type is finger
8098 processId(mapper, 1);
8099 processPosition(mapper, 100, 200);
8100 processSync(mapper);
8101 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8102 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8103 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8104
8105 // eraser
8106 processKey(mapper, BTN_TOOL_RUBBER, 1);
8107 processSync(mapper);
8108 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8109 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8110 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
8111
8112 // stylus
8113 processKey(mapper, BTN_TOOL_RUBBER, 0);
8114 processKey(mapper, BTN_TOOL_PEN, 1);
8115 processSync(mapper);
8116 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8117 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8118 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8119
8120 // brush
8121 processKey(mapper, BTN_TOOL_PEN, 0);
8122 processKey(mapper, BTN_TOOL_BRUSH, 1);
8123 processSync(mapper);
8124 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8125 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8126 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8127
8128 // pencil
8129 processKey(mapper, BTN_TOOL_BRUSH, 0);
8130 processKey(mapper, BTN_TOOL_PENCIL, 1);
8131 processSync(mapper);
8132 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8133 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8134 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8135
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08008136 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08008137 processKey(mapper, BTN_TOOL_PENCIL, 0);
8138 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
8139 processSync(mapper);
8140 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8141 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8142 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8143
8144 // mouse
8145 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
8146 processKey(mapper, BTN_TOOL_MOUSE, 1);
8147 processSync(mapper);
8148 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8149 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8150 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
8151
8152 // lens
8153 processKey(mapper, BTN_TOOL_MOUSE, 0);
8154 processKey(mapper, BTN_TOOL_LENS, 1);
8155 processSync(mapper);
8156 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8157 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8158 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
8159
8160 // double-tap
8161 processKey(mapper, BTN_TOOL_LENS, 0);
8162 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
8163 processSync(mapper);
8164 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8165 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8166 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8167
8168 // triple-tap
8169 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
8170 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
8171 processSync(mapper);
8172 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8173 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8174 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8175
8176 // quad-tap
8177 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
8178 processKey(mapper, BTN_TOOL_QUADTAP, 1);
8179 processSync(mapper);
8180 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8181 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8182 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8183
8184 // finger
8185 processKey(mapper, BTN_TOOL_QUADTAP, 0);
8186 processKey(mapper, BTN_TOOL_FINGER, 1);
8187 processSync(mapper);
8188 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8189 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8190 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8191
8192 // stylus trumps finger
8193 processKey(mapper, BTN_TOOL_PEN, 1);
8194 processSync(mapper);
8195 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8196 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8197 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8198
8199 // eraser trumps stylus
8200 processKey(mapper, BTN_TOOL_RUBBER, 1);
8201 processSync(mapper);
8202 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8203 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8204 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
8205
8206 // mouse trumps eraser
8207 processKey(mapper, BTN_TOOL_MOUSE, 1);
8208 processSync(mapper);
8209 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8210 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8211 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
8212
8213 // MT tool type trumps BTN tool types: MT_TOOL_FINGER
8214 processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
8215 processSync(mapper);
8216 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8217 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8218 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8219
8220 // MT tool type trumps BTN tool types: MT_TOOL_PEN
8221 processToolType(mapper, MT_TOOL_PEN);
8222 processSync(mapper);
8223 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8224 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8225 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8226
8227 // back to default tool type
8228 processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
8229 processKey(mapper, BTN_TOOL_MOUSE, 0);
8230 processKey(mapper, BTN_TOOL_RUBBER, 0);
8231 processKey(mapper, BTN_TOOL_PEN, 0);
8232 processKey(mapper, BTN_TOOL_FINGER, 0);
8233 processSync(mapper);
8234 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8235 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8236 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8237}
8238
8239TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008240 addConfigurationProperty("touch.deviceType", "touchScreen");
8241 prepareDisplay(DISPLAY_ORIENTATION_0);
8242 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008243 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008244 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008245
8246 NotifyMotionArgs motionArgs;
8247
8248 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
8249 processId(mapper, 1);
8250 processPosition(mapper, 100, 200);
8251 processSync(mapper);
8252 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8253 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8254 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8255 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8256
8257 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8258 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8259 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8260 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8261
8262 // move a little
8263 processPosition(mapper, 150, 250);
8264 processSync(mapper);
8265 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8266 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8267 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8268 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8269
8270 // down when BTN_TOUCH is pressed, pressure defaults to 1
8271 processKey(mapper, BTN_TOUCH, 1);
8272 processSync(mapper);
8273 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8274 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8275 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8276 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8277
8278 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8279 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8280 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8281 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8282
8283 // up when BTN_TOUCH is released, hover restored
8284 processKey(mapper, BTN_TOUCH, 0);
8285 processSync(mapper);
8286 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8287 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8288 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8289 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8290
8291 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8292 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8293 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8294 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8295
8296 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8297 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8298 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8299 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8300
8301 // exit hover when pointer goes away
8302 processId(mapper, -1);
8303 processSync(mapper);
8304 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8305 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8306 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8307 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8308}
8309
8310TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008311 addConfigurationProperty("touch.deviceType", "touchScreen");
8312 prepareDisplay(DISPLAY_ORIENTATION_0);
8313 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008314 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008315
8316 NotifyMotionArgs motionArgs;
8317
8318 // initially hovering because pressure is 0
8319 processId(mapper, 1);
8320 processPosition(mapper, 100, 200);
8321 processPressure(mapper, 0);
8322 processSync(mapper);
8323 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8324 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8325 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8326 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8327
8328 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8329 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8330 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8331 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8332
8333 // move a little
8334 processPosition(mapper, 150, 250);
8335 processSync(mapper);
8336 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8337 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8338 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8339 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8340
8341 // down when pressure becomes non-zero
8342 processPressure(mapper, RAW_PRESSURE_MAX);
8343 processSync(mapper);
8344 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8345 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8346 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8347 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8348
8349 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8350 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8351 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8352 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8353
8354 // up when pressure becomes 0, hover restored
8355 processPressure(mapper, 0);
8356 processSync(mapper);
8357 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8358 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8359 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8360 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8361
8362 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8363 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8364 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8365 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8366
8367 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8368 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8369 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8370 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8371
8372 // exit hover when pointer goes away
8373 processId(mapper, -1);
8374 processSync(mapper);
8375 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8376 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8377 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8378 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8379}
8380
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008381/**
8382 * Set the input device port <--> display port associations, and check that the
8383 * events are routed to the display that matches the display port.
8384 * This can be checked by looking at the displayId of the resulting NotifyMotionArgs.
8385 */
8386TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayPort) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008387 const std::string usb2 = "USB2";
8388 const uint8_t hdmi1 = 0;
8389 const uint8_t hdmi2 = 1;
8390 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008391 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008392
8393 addConfigurationProperty("touch.deviceType", "touchScreen");
8394 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008395 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008396
8397 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
8398 mFakePolicy->addInputPortAssociation(usb2, hdmi2);
8399
8400 // We are intentionally not adding the viewport for display 1 yet. Since the port association
8401 // for this input device is specified, and the matching viewport is not present,
8402 // the input device should be disabled (at the mapper level).
8403
8404 // Add viewport for display 2 on hdmi2
8405 prepareSecondaryDisplay(type, hdmi2);
8406 // Send a touch event
8407 processPosition(mapper, 100, 100);
8408 processSync(mapper);
8409 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8410
8411 // Add viewport for display 1 on hdmi1
8412 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
8413 // Send a touch event again
8414 processPosition(mapper, 100, 100);
8415 processSync(mapper);
8416
8417 NotifyMotionArgs args;
8418 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8419 ASSERT_EQ(DISPLAY_ID, args.displayId);
8420}
Michael Wrightd02c5b62014-02-10 15:10:22 -08008421
Arthur Hung6d5b4b22022-01-21 07:21:10 +00008422TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayUniqueId) {
8423 addConfigurationProperty("touch.deviceType", "touchScreen");
8424 prepareAxes(POSITION);
8425 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8426
8427 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
8428
8429 prepareDisplay(DISPLAY_ORIENTATION_0);
8430 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
8431
8432 // Send a touch event
8433 processPosition(mapper, 100, 100);
8434 processSync(mapper);
8435
8436 NotifyMotionArgs args;
8437 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8438 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
8439}
8440
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008441TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShouldHandleDisplayId) {
Garfield Tan888a6a42020-01-09 11:39:16 -08008442 // Setup for second display.
Michael Wright17db18e2020-06-26 20:51:44 +01008443 std::shared_ptr<FakePointerController> fakePointerController =
8444 std::make_shared<FakePointerController>();
Garfield Tan888a6a42020-01-09 11:39:16 -08008445 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008446 fakePointerController->setPosition(100, 200);
8447 fakePointerController->setButtonState(0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00008448 mFakePolicy->setPointerController(fakePointerController);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008449
Garfield Tan888a6a42020-01-09 11:39:16 -08008450 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008451 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08008452
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008453 prepareDisplay(DISPLAY_ORIENTATION_0);
8454 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008455 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008456
8457 // Check source is mouse that would obtain the PointerController.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008458 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008459
8460 NotifyMotionArgs motionArgs;
8461 processPosition(mapper, 100, 100);
8462 processSync(mapper);
8463
8464 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8465 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8466 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
8467}
8468
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00008469/**
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008470 * Ensure that the readTime is set to the SYN_REPORT value when processing touch events.
8471 */
8472TEST_F(MultiTouchInputMapperTest, Process_SendsReadTime) {
8473 addConfigurationProperty("touch.deviceType", "touchScreen");
8474 prepareAxes(POSITION);
8475 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8476
8477 prepareDisplay(DISPLAY_ORIENTATION_0);
8478 process(mapper, 10, 11 /*readTime*/, EV_ABS, ABS_MT_TRACKING_ID, 1);
8479 process(mapper, 15, 16 /*readTime*/, EV_ABS, ABS_MT_POSITION_X, 100);
8480 process(mapper, 20, 21 /*readTime*/, EV_ABS, ABS_MT_POSITION_Y, 100);
8481 process(mapper, 25, 26 /*readTime*/, EV_SYN, SYN_REPORT, 0);
8482
8483 NotifyMotionArgs args;
8484 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8485 ASSERT_EQ(26, args.readTime);
8486
8487 process(mapper, 30, 31 /*readTime*/, EV_ABS, ABS_MT_POSITION_X, 110);
8488 process(mapper, 30, 32 /*readTime*/, EV_ABS, ABS_MT_POSITION_Y, 220);
8489 process(mapper, 30, 33 /*readTime*/, EV_SYN, SYN_REPORT, 0);
8490
8491 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8492 ASSERT_EQ(33, args.readTime);
8493}
8494
8495/**
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00008496 * When the viewport is not active (isActive=false), the touch mapper should be disabled and the
8497 * events should not be delivered to the listener.
8498 */
8499TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreDropped) {
8500 addConfigurationProperty("touch.deviceType", "touchScreen");
8501 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
8502 DISPLAY_ORIENTATION_0, false /*isActive*/, UNIQUE_ID, NO_PORT,
8503 ViewportType::INTERNAL);
8504 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8505 prepareAxes(POSITION);
8506 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8507
8508 NotifyMotionArgs motionArgs;
8509 processPosition(mapper, 100, 100);
8510 processSync(mapper);
8511
8512 mFakeListener->assertNotifyMotionWasNotCalled();
8513}
8514
Garfield Tanc734e4f2021-01-15 20:01:39 -08008515TEST_F(MultiTouchInputMapperTest, Process_DeactivateViewport_AbortTouches) {
8516 addConfigurationProperty("touch.deviceType", "touchScreen");
8517 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
8518 DISPLAY_ORIENTATION_0, true /*isActive*/, UNIQUE_ID, NO_PORT,
8519 ViewportType::INTERNAL);
8520 std::optional<DisplayViewport> optionalDisplayViewport =
8521 mFakePolicy->getDisplayViewportByUniqueId(UNIQUE_ID);
8522 ASSERT_TRUE(optionalDisplayViewport.has_value());
8523 DisplayViewport displayViewport = *optionalDisplayViewport;
8524
8525 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8526 prepareAxes(POSITION);
8527 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8528
8529 // Finger down
8530 int32_t x = 100, y = 100;
8531 processPosition(mapper, x, y);
8532 processSync(mapper);
8533
8534 NotifyMotionArgs motionArgs;
8535 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8536 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8537
8538 // Deactivate display viewport
8539 displayViewport.isActive = false;
8540 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
8541 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8542
8543 // Finger move
8544 x += 10, y += 10;
8545 processPosition(mapper, x, y);
8546 processSync(mapper);
8547
8548 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8549 EXPECT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
8550
8551 // Reactivate display viewport
8552 displayViewport.isActive = true;
8553 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
8554 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8555
8556 // Finger move again
8557 x += 10, y += 10;
8558 processPosition(mapper, x, y);
8559 processSync(mapper);
8560
8561 // Gesture is aborted, so events after display is activated won't be dispatched until there is
8562 // no pointer on the touch device.
8563 mFakeListener->assertNotifyMotionWasNotCalled();
8564}
8565
Arthur Hung7c645402019-01-25 17:45:42 +08008566TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShowTouches) {
8567 // Setup the first touch screen device.
Arthur Hung7c645402019-01-25 17:45:42 +08008568 prepareAxes(POSITION | ID | SLOT);
8569 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008570 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung7c645402019-01-25 17:45:42 +08008571
8572 // Create the second touch screen device, and enable multi fingers.
8573 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08008574 const std::string DEVICE_NAME2 = "TOUCHSCREEN2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08008575 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008576 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08008577 std::shared_ptr<InputDevice> device2 =
8578 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07008579 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08008580
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008581 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
8582 0 /*flat*/, 0 /*fuzz*/);
8583 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
8584 0 /*flat*/, 0 /*fuzz*/);
8585 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX,
8586 0 /*flat*/, 0 /*fuzz*/);
8587 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX,
8588 0 /*flat*/, 0 /*fuzz*/);
8589 mFakeEventHub->setAbsoluteAxisValue(SECOND_EVENTHUB_ID, ABS_MT_SLOT, 0 /*value*/);
8590 mFakeEventHub->addConfigurationProperty(SECOND_EVENTHUB_ID, String8("touch.deviceType"),
8591 String8("touchScreen"));
Arthur Hung7c645402019-01-25 17:45:42 +08008592
8593 // Setup the second touch screen device.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008594 MultiTouchInputMapper& mapper2 = device2->addMapper<MultiTouchInputMapper>(SECOND_EVENTHUB_ID);
Arthur Hung7c645402019-01-25 17:45:42 +08008595 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
8596 device2->reset(ARBITRARY_TIME);
8597
8598 // Setup PointerController.
Michael Wright17db18e2020-06-26 20:51:44 +01008599 std::shared_ptr<FakePointerController> fakePointerController =
8600 std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00008601 mFakePolicy->setPointerController(fakePointerController);
Arthur Hung7c645402019-01-25 17:45:42 +08008602
8603 // Setup policy for associated displays and show touches.
8604 const uint8_t hdmi1 = 0;
8605 const uint8_t hdmi2 = 1;
8606 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
8607 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
8608 mFakePolicy->setShowTouches(true);
8609
8610 // Create displays.
8611 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008612 prepareSecondaryDisplay(ViewportType::EXTERNAL, hdmi2);
Arthur Hung7c645402019-01-25 17:45:42 +08008613
8614 // Default device will reconfigure above, need additional reconfiguration for another device.
8615 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan197e0862022-07-01 14:28:00 +00008616 InputReaderConfiguration::CHANGE_DISPLAY_INFO |
8617 InputReaderConfiguration::CHANGE_SHOW_TOUCHES);
Arthur Hung7c645402019-01-25 17:45:42 +08008618
8619 // Two fingers down at default display.
8620 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
8621 processPosition(mapper, x1, y1);
8622 processId(mapper, 1);
8623 processSlot(mapper, 1);
8624 processPosition(mapper, x2, y2);
8625 processId(mapper, 2);
8626 processSync(mapper);
8627
8628 std::map<int32_t, std::vector<int32_t>>::const_iterator iter =
8629 fakePointerController->getSpots().find(DISPLAY_ID);
8630 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
8631 ASSERT_EQ(size_t(2), iter->second.size());
8632
8633 // Two fingers down at second display.
8634 processPosition(mapper2, x1, y1);
8635 processId(mapper2, 1);
8636 processSlot(mapper2, 1);
8637 processPosition(mapper2, x2, y2);
8638 processId(mapper2, 2);
8639 processSync(mapper2);
8640
8641 iter = fakePointerController->getSpots().find(SECONDARY_DISPLAY_ID);
8642 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
8643 ASSERT_EQ(size_t(2), iter->second.size());
Prabir Pradhan197e0862022-07-01 14:28:00 +00008644
8645 // Disable the show touches configuration and ensure the spots are cleared.
8646 mFakePolicy->setShowTouches(false);
8647 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
8648 InputReaderConfiguration::CHANGE_SHOW_TOUCHES);
8649
8650 ASSERT_TRUE(fakePointerController->getSpots().empty());
Arthur Hung7c645402019-01-25 17:45:42 +08008651}
8652
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008653TEST_F(MultiTouchInputMapperTest, VideoFrames_ReceivedByListener) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008654 prepareAxes(POSITION);
8655 addConfigurationProperty("touch.deviceType", "touchScreen");
8656 prepareDisplay(DISPLAY_ORIENTATION_0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008657 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008658
8659 NotifyMotionArgs motionArgs;
8660 // Unrotated video frame
8661 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8662 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008663 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008664 processPosition(mapper, 100, 200);
8665 processSync(mapper);
8666 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8667 ASSERT_EQ(frames, motionArgs.videoFrames);
8668
8669 // Subsequent touch events should not have any videoframes
8670 // This is implemented separately in FakeEventHub,
8671 // but that should match the behaviour of TouchVideoDevice.
8672 processPosition(mapper, 200, 200);
8673 processSync(mapper);
8674 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8675 ASSERT_EQ(std::vector<TouchVideoFrame>(), motionArgs.videoFrames);
8676}
8677
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008678TEST_F(MultiTouchInputMapperTest, VideoFrames_AreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008679 prepareAxes(POSITION);
8680 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008681 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008682 // Unrotated video frame
8683 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8684 NotifyMotionArgs motionArgs;
8685
8686 // Test all 4 orientations
8687 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008688 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
8689 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
8690 clearViewports();
8691 prepareDisplay(orientation);
8692 std::vector<TouchVideoFrame> frames{frame};
8693 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
8694 processPosition(mapper, 100, 200);
8695 processSync(mapper);
8696 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8697 ASSERT_EQ(frames, motionArgs.videoFrames);
8698 }
8699}
8700
8701TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_AreRotated) {
8702 prepareAxes(POSITION);
8703 addConfigurationProperty("touch.deviceType", "touchScreen");
8704 // Since InputReader works in the un-rotated coordinate space, only devices that are not
8705 // orientation-aware are affected by display rotation.
8706 addConfigurationProperty("touch.orientationAware", "0");
8707 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8708 // Unrotated video frame
8709 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8710 NotifyMotionArgs motionArgs;
8711
8712 // Test all 4 orientations
8713 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008714 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
8715 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
8716 clearViewports();
8717 prepareDisplay(orientation);
8718 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008719 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008720 processPosition(mapper, 100, 200);
8721 processSync(mapper);
8722 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008723 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
8724 // compared to the display. This is so that when the window transform (which contains the
8725 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
8726 // window's coordinate space.
8727 frames[0].rotate(getInverseRotation(orientation));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008728 ASSERT_EQ(frames, motionArgs.videoFrames);
lilinnane34bc532022-07-19 16:00:50 +08008729
8730 // Release finger.
8731 processSync(mapper);
8732 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008733 }
8734}
8735
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008736TEST_F(MultiTouchInputMapperTest, VideoFrames_MultipleFramesAreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008737 prepareAxes(POSITION);
8738 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008739 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008740 // Unrotated video frames. There's no rule that they must all have the same dimensions,
8741 // so mix these.
8742 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8743 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
8744 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
8745 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
8746 NotifyMotionArgs motionArgs;
8747
8748 prepareDisplay(DISPLAY_ORIENTATION_90);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008749 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008750 processPosition(mapper, 100, 200);
8751 processSync(mapper);
8752 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008753 ASSERT_EQ(frames, motionArgs.videoFrames);
8754}
8755
8756TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_MultipleFramesAreRotated) {
8757 prepareAxes(POSITION);
8758 addConfigurationProperty("touch.deviceType", "touchScreen");
8759 // Since InputReader works in the un-rotated coordinate space, only devices that are not
8760 // orientation-aware are affected by display rotation.
8761 addConfigurationProperty("touch.orientationAware", "0");
8762 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8763 // Unrotated video frames. There's no rule that they must all have the same dimensions,
8764 // so mix these.
8765 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8766 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
8767 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
8768 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
8769 NotifyMotionArgs motionArgs;
8770
8771 prepareDisplay(DISPLAY_ORIENTATION_90);
8772 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
8773 processPosition(mapper, 100, 200);
8774 processSync(mapper);
8775 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8776 std::for_each(frames.begin(), frames.end(), [](TouchVideoFrame& frame) {
8777 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
8778 // compared to the display. This is so that when the window transform (which contains the
8779 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
8780 // window's coordinate space.
8781 frame.rotate(getInverseRotation(DISPLAY_ORIENTATION_90));
8782 });
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008783 ASSERT_EQ(frames, motionArgs.videoFrames);
8784}
8785
Arthur Hung9da14732019-09-02 16:16:58 +08008786/**
8787 * If we had defined port associations, but the viewport is not ready, the touch device would be
8788 * expected to be disabled, and it should be enabled after the viewport has found.
8789 */
8790TEST_F(MultiTouchInputMapperTest, Configure_EnabledForAssociatedDisplay) {
Arthur Hung9da14732019-09-02 16:16:58 +08008791 constexpr uint8_t hdmi2 = 1;
8792 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008793 constexpr ViewportType type = ViewportType::EXTERNAL;
Arthur Hung9da14732019-09-02 16:16:58 +08008794
8795 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi2);
8796
8797 addConfigurationProperty("touch.deviceType", "touchScreen");
8798 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008799 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung9da14732019-09-02 16:16:58 +08008800
8801 ASSERT_EQ(mDevice->isEnabled(), false);
8802
8803 // Add display on hdmi2, the device should be enabled and can receive touch event.
8804 prepareSecondaryDisplay(type, hdmi2);
8805 ASSERT_EQ(mDevice->isEnabled(), true);
8806
8807 // Send a touch event.
8808 processPosition(mapper, 100, 100);
8809 processSync(mapper);
8810
8811 NotifyMotionArgs args;
8812 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8813 ASSERT_EQ(SECONDARY_DISPLAY_ID, args.displayId);
8814}
8815
Arthur Hung421eb1c2020-01-16 00:09:42 +08008816TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleSingleTouch) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08008817 addConfigurationProperty("touch.deviceType", "touchScreen");
8818 prepareDisplay(DISPLAY_ORIENTATION_0);
8819 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008820 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08008821
8822 NotifyMotionArgs motionArgs;
8823
8824 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
8825 // finger down
8826 processId(mapper, 1);
8827 processPosition(mapper, x1, y1);
8828 processSync(mapper);
8829 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8830 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8831 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8832
8833 // finger move
8834 processId(mapper, 1);
8835 processPosition(mapper, x2, y2);
8836 processSync(mapper);
8837 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8838 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8839 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8840
8841 // finger up.
8842 processId(mapper, -1);
8843 processSync(mapper);
8844 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8845 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8846 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8847
8848 // new finger down
8849 processId(mapper, 1);
8850 processPosition(mapper, x3, y3);
8851 processSync(mapper);
8852 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8853 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8854 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8855}
8856
8857/**
arthurhungcc7f9802020-04-30 17:55:40 +08008858 * Test single touch should be canceled when received the MT_TOOL_PALM event, and the following
8859 * MOVE and UP events should be ignored.
Arthur Hung421eb1c2020-01-16 00:09:42 +08008860 */
arthurhungcc7f9802020-04-30 17:55:40 +08008861TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_SinglePointer) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08008862 addConfigurationProperty("touch.deviceType", "touchScreen");
8863 prepareDisplay(DISPLAY_ORIENTATION_0);
8864 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008865 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08008866
8867 NotifyMotionArgs motionArgs;
8868
8869 // default tool type is finger
8870 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
arthurhungcc7f9802020-04-30 17:55:40 +08008871 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008872 processPosition(mapper, x1, y1);
8873 processSync(mapper);
8874 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8875 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8876 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8877
8878 // Tool changed to MT_TOOL_PALM expect sending the cancel event.
8879 processToolType(mapper, MT_TOOL_PALM);
8880 processSync(mapper);
8881 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8882 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
8883
8884 // Ignore the following MOVE and UP events if had detect a palm event.
arthurhungcc7f9802020-04-30 17:55:40 +08008885 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008886 processPosition(mapper, x2, y2);
8887 processSync(mapper);
8888 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8889
8890 // finger up.
arthurhungcc7f9802020-04-30 17:55:40 +08008891 processId(mapper, INVALID_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008892 processSync(mapper);
8893 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8894
8895 // new finger down
arthurhungcc7f9802020-04-30 17:55:40 +08008896 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008897 processToolType(mapper, MT_TOOL_FINGER);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008898 processPosition(mapper, x3, y3);
8899 processSync(mapper);
8900 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8901 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8902 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8903}
8904
arthurhungbf89a482020-04-17 17:37:55 +08008905/**
arthurhungcc7f9802020-04-30 17:55:40 +08008906 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
8907 * and the rest active fingers could still be allowed to receive the events
arthurhungbf89a482020-04-17 17:37:55 +08008908 */
arthurhungcc7f9802020-04-30 17:55:40 +08008909TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_TwoPointers) {
arthurhungbf89a482020-04-17 17:37:55 +08008910 addConfigurationProperty("touch.deviceType", "touchScreen");
8911 prepareDisplay(DISPLAY_ORIENTATION_0);
8912 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
8913 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8914
8915 NotifyMotionArgs motionArgs;
8916
8917 // default tool type is finger
arthurhungcc7f9802020-04-30 17:55:40 +08008918 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
8919 processId(mapper, FIRST_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08008920 processPosition(mapper, x1, y1);
8921 processSync(mapper);
8922 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8923 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8924 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8925
8926 // Second finger down.
arthurhungcc7f9802020-04-30 17:55:40 +08008927 processSlot(mapper, SECOND_SLOT);
8928 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08008929 processPosition(mapper, x2, y2);
arthurhungcc7f9802020-04-30 17:55:40 +08008930 processSync(mapper);
8931 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008932 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08008933 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8934
8935 // If the tool type of the first finger changes to MT_TOOL_PALM,
8936 // we expect to receive ACTION_POINTER_UP with cancel flag.
8937 processSlot(mapper, FIRST_SLOT);
8938 processId(mapper, FIRST_TRACKING_ID);
8939 processToolType(mapper, MT_TOOL_PALM);
8940 processSync(mapper);
8941 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008942 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08008943 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
8944
8945 // The following MOVE events of second finger should be processed.
8946 processSlot(mapper, SECOND_SLOT);
8947 processId(mapper, SECOND_TRACKING_ID);
8948 processPosition(mapper, x2 + 1, y2 + 1);
8949 processSync(mapper);
8950 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8951 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8952 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8953
8954 // First finger up. It used to be in palm mode, and we already generated ACTION_POINTER_UP for
8955 // it. Second finger receive move.
8956 processSlot(mapper, FIRST_SLOT);
8957 processId(mapper, INVALID_TRACKING_ID);
8958 processSync(mapper);
8959 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8960 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8961 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8962
8963 // Second finger keeps moving.
8964 processSlot(mapper, SECOND_SLOT);
8965 processId(mapper, SECOND_TRACKING_ID);
8966 processPosition(mapper, x2 + 2, y2 + 2);
8967 processSync(mapper);
8968 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8969 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8970 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8971
8972 // Second finger up.
8973 processId(mapper, INVALID_TRACKING_ID);
8974 processSync(mapper);
8975 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8976 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8977 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
8978}
8979
8980/**
8981 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event, if only 1 finger
8982 * is active, it should send CANCEL after receiving the MT_TOOL_PALM event.
8983 */
8984TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_ShouldCancelWhenAllTouchIsPalm) {
8985 addConfigurationProperty("touch.deviceType", "touchScreen");
8986 prepareDisplay(DISPLAY_ORIENTATION_0);
8987 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
8988 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8989
8990 NotifyMotionArgs motionArgs;
8991
8992 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
8993 // First finger down.
8994 processId(mapper, FIRST_TRACKING_ID);
8995 processPosition(mapper, x1, y1);
8996 processSync(mapper);
8997 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8998 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8999 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9000
9001 // Second finger down.
9002 processSlot(mapper, SECOND_SLOT);
9003 processId(mapper, SECOND_TRACKING_ID);
9004 processPosition(mapper, x2, y2);
arthurhungbf89a482020-04-17 17:37:55 +08009005 processSync(mapper);
9006 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009007 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungbf89a482020-04-17 17:37:55 +08009008 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9009
arthurhungcc7f9802020-04-30 17:55:40 +08009010 // If the tool type of the first finger changes to MT_TOOL_PALM,
9011 // we expect to receive ACTION_POINTER_UP with cancel flag.
9012 processSlot(mapper, FIRST_SLOT);
9013 processId(mapper, FIRST_TRACKING_ID);
9014 processToolType(mapper, MT_TOOL_PALM);
9015 processSync(mapper);
9016 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009017 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009018 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9019
9020 // Second finger keeps moving.
9021 processSlot(mapper, SECOND_SLOT);
9022 processId(mapper, SECOND_TRACKING_ID);
9023 processPosition(mapper, x2 + 1, y2 + 1);
9024 processSync(mapper);
9025 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9026 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9027
9028 // second finger becomes palm, receive cancel due to only 1 finger is active.
9029 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009030 processToolType(mapper, MT_TOOL_PALM);
9031 processSync(mapper);
9032 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9033 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9034
arthurhungcc7f9802020-04-30 17:55:40 +08009035 // third finger down.
9036 processSlot(mapper, THIRD_SLOT);
9037 processId(mapper, THIRD_TRACKING_ID);
9038 processToolType(mapper, MT_TOOL_FINGER);
arthurhungbf89a482020-04-17 17:37:55 +08009039 processPosition(mapper, x3, y3);
9040 processSync(mapper);
arthurhungbf89a482020-04-17 17:37:55 +08009041 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9042 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9043 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08009044 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9045
9046 // third finger move
9047 processId(mapper, THIRD_TRACKING_ID);
9048 processPosition(mapper, x3 + 1, y3 + 1);
9049 processSync(mapper);
9050 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9051 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9052
9053 // first finger up, third finger receive move.
9054 processSlot(mapper, FIRST_SLOT);
9055 processId(mapper, INVALID_TRACKING_ID);
9056 processSync(mapper);
9057 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9058 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9059 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9060
9061 // second finger up, third finger receive move.
9062 processSlot(mapper, SECOND_SLOT);
9063 processId(mapper, INVALID_TRACKING_ID);
9064 processSync(mapper);
9065 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9066 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9067 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9068
9069 // third finger up.
9070 processSlot(mapper, THIRD_SLOT);
9071 processId(mapper, INVALID_TRACKING_ID);
9072 processSync(mapper);
9073 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9074 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9075 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9076}
9077
9078/**
9079 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
9080 * and the active finger could still be allowed to receive the events
9081 */
9082TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_KeepFirstPointer) {
9083 addConfigurationProperty("touch.deviceType", "touchScreen");
9084 prepareDisplay(DISPLAY_ORIENTATION_0);
9085 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
9086 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9087
9088 NotifyMotionArgs motionArgs;
9089
9090 // default tool type is finger
9091 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
9092 processId(mapper, FIRST_TRACKING_ID);
9093 processPosition(mapper, x1, y1);
9094 processSync(mapper);
9095 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9096 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9097 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9098
9099 // Second finger down.
9100 processSlot(mapper, SECOND_SLOT);
9101 processId(mapper, SECOND_TRACKING_ID);
9102 processPosition(mapper, x2, y2);
9103 processSync(mapper);
9104 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009105 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009106 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9107
9108 // If the tool type of the second finger changes to MT_TOOL_PALM,
9109 // we expect to receive ACTION_POINTER_UP with cancel flag.
9110 processId(mapper, SECOND_TRACKING_ID);
9111 processToolType(mapper, MT_TOOL_PALM);
9112 processSync(mapper);
9113 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009114 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009115 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9116
9117 // The following MOVE event should be processed.
9118 processSlot(mapper, FIRST_SLOT);
9119 processId(mapper, FIRST_TRACKING_ID);
9120 processPosition(mapper, x1 + 1, y1 + 1);
9121 processSync(mapper);
9122 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9123 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9124 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9125
9126 // second finger up.
9127 processSlot(mapper, SECOND_SLOT);
9128 processId(mapper, INVALID_TRACKING_ID);
9129 processSync(mapper);
9130 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9131 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9132
9133 // first finger keep moving
9134 processSlot(mapper, FIRST_SLOT);
9135 processId(mapper, FIRST_TRACKING_ID);
9136 processPosition(mapper, x1 + 2, y1 + 2);
9137 processSync(mapper);
9138 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9139 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9140
9141 // first finger up.
9142 processId(mapper, INVALID_TRACKING_ID);
9143 processSync(mapper);
9144 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9145 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9146 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
arthurhungbf89a482020-04-17 17:37:55 +08009147}
9148
Arthur Hung9ad18942021-06-19 02:04:46 +00009149/**
9150 * Test multi-touch should sent ACTION_POINTER_UP/ACTION_UP when received the INVALID_TRACKING_ID,
9151 * to prevent the driver side may send unexpected data after set tracking id as INVALID_TRACKING_ID
9152 * cause slot be valid again.
9153 */
9154TEST_F(MultiTouchInputMapperTest, Process_MultiTouch_WithInvalidTrackingId) {
9155 addConfigurationProperty("touch.deviceType", "touchScreen");
9156 prepareDisplay(DISPLAY_ORIENTATION_0);
9157 prepareAxes(POSITION | ID | SLOT | PRESSURE);
9158 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9159
9160 NotifyMotionArgs motionArgs;
9161
9162 constexpr int32_t x1 = 100, y1 = 200, x2 = 0, y2 = 0;
9163 // First finger down.
9164 processId(mapper, FIRST_TRACKING_ID);
9165 processPosition(mapper, x1, y1);
9166 processPressure(mapper, RAW_PRESSURE_MAX);
9167 processSync(mapper);
9168 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9169 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9170 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9171
9172 // First finger move.
9173 processId(mapper, FIRST_TRACKING_ID);
9174 processPosition(mapper, x1 + 1, y1 + 1);
9175 processPressure(mapper, RAW_PRESSURE_MAX);
9176 processSync(mapper);
9177 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9178 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9179 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9180
9181 // Second finger down.
9182 processSlot(mapper, SECOND_SLOT);
9183 processId(mapper, SECOND_TRACKING_ID);
9184 processPosition(mapper, x2, y2);
9185 processPressure(mapper, RAW_PRESSURE_MAX);
9186 processSync(mapper);
9187 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009188 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Arthur Hung9ad18942021-06-19 02:04:46 +00009189 ASSERT_EQ(uint32_t(2), motionArgs.pointerCount);
9190
9191 // second finger up with some unexpected data.
9192 processSlot(mapper, SECOND_SLOT);
9193 processId(mapper, INVALID_TRACKING_ID);
9194 processPosition(mapper, x2, y2);
9195 processSync(mapper);
9196 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009197 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Arthur Hung9ad18942021-06-19 02:04:46 +00009198 ASSERT_EQ(uint32_t(2), motionArgs.pointerCount);
9199
9200 // first finger up with some unexpected data.
9201 processSlot(mapper, FIRST_SLOT);
9202 processId(mapper, INVALID_TRACKING_ID);
9203 processPosition(mapper, x2, y2);
9204 processPressure(mapper, RAW_PRESSURE_MAX);
9205 processSync(mapper);
9206 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9207 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9208 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9209}
9210
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009211// --- MultiTouchInputMapperTest_ExternalDevice ---
9212
9213class MultiTouchInputMapperTest_ExternalDevice : public MultiTouchInputMapperTest {
9214protected:
Chris Yea52ade12020-08-27 16:49:20 -07009215 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009216};
9217
9218/**
9219 * Expect fallback to internal viewport if device is external and external viewport is not present.
9220 */
9221TEST_F(MultiTouchInputMapperTest_ExternalDevice, Viewports_Fallback) {
9222 prepareAxes(POSITION);
9223 addConfigurationProperty("touch.deviceType", "touchScreen");
9224 prepareDisplay(DISPLAY_ORIENTATION_0);
9225 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9226
9227 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
9228
9229 NotifyMotionArgs motionArgs;
9230
9231 // Expect the event to be sent to the internal viewport,
9232 // because an external viewport is not present.
9233 processPosition(mapper, 100, 100);
9234 processSync(mapper);
9235 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9236 ASSERT_EQ(ADISPLAY_ID_DEFAULT, motionArgs.displayId);
9237
9238 // Expect the event to be sent to the external viewport if it is present.
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009239 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009240 processPosition(mapper, 100, 100);
9241 processSync(mapper);
9242 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9243 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
9244}
Arthur Hung4197f6b2020-03-16 15:39:59 +08009245
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009246TEST_F(MultiTouchInputMapperTest, Process_TouchpadCapture) {
9247 // we need a pointer controller for mouse mode of touchpad (start pointer at 0,0)
9248 std::shared_ptr<FakePointerController> fakePointerController =
9249 std::make_shared<FakePointerController>();
9250 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
9251 fakePointerController->setPosition(0, 0);
9252 fakePointerController->setButtonState(0);
9253
9254 // prepare device and capture
9255 prepareDisplay(DISPLAY_ORIENTATION_0);
9256 prepareAxes(POSITION | ID | SLOT);
9257 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
9258 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
9259 mFakePolicy->setPointerCapture(true);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009260 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009261 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9262
9263 // captured touchpad should be a touchpad source
9264 NotifyDeviceResetArgs resetArgs;
9265 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
9266 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
9267
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009268 InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
Chris Yef74dc422020-09-02 22:41:50 -07009269
9270 const InputDeviceInfo::MotionRange* relRangeX =
9271 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_X, AINPUT_SOURCE_TOUCHPAD);
9272 ASSERT_NE(relRangeX, nullptr);
9273 ASSERT_EQ(relRangeX->min, -(RAW_X_MAX - RAW_X_MIN));
9274 ASSERT_EQ(relRangeX->max, RAW_X_MAX - RAW_X_MIN);
9275 const InputDeviceInfo::MotionRange* relRangeY =
9276 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_Y, AINPUT_SOURCE_TOUCHPAD);
9277 ASSERT_NE(relRangeY, nullptr);
9278 ASSERT_EQ(relRangeY->min, -(RAW_Y_MAX - RAW_Y_MIN));
9279 ASSERT_EQ(relRangeY->max, RAW_Y_MAX - RAW_Y_MIN);
9280
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009281 // run captured pointer tests - note that this is unscaled, so input listener events should be
9282 // identical to what the hardware sends (accounting for any
9283 // calibration).
9284 // FINGER 0 DOWN
Chris Ye364fdb52020-08-05 15:07:56 -07009285 processSlot(mapper, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009286 processId(mapper, 1);
9287 processPosition(mapper, 100 + RAW_X_MIN, 100 + RAW_Y_MIN);
9288 processKey(mapper, BTN_TOUCH, 1);
9289 processSync(mapper);
9290
9291 // expect coord[0] to contain initial location of touch 0
9292 NotifyMotionArgs args;
9293 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9294 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
9295 ASSERT_EQ(1U, args.pointerCount);
9296 ASSERT_EQ(0, args.pointerProperties[0].id);
9297 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, args.source);
9298 ASSERT_NO_FATAL_FAILURE(
9299 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
9300
9301 // FINGER 1 DOWN
9302 processSlot(mapper, 1);
9303 processId(mapper, 2);
9304 processPosition(mapper, 560 + RAW_X_MIN, 154 + RAW_Y_MIN);
9305 processSync(mapper);
9306
9307 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
9308 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009309 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009310 ASSERT_EQ(2U, args.pointerCount);
9311 ASSERT_EQ(0, args.pointerProperties[0].id);
9312 ASSERT_EQ(1, args.pointerProperties[1].id);
9313 ASSERT_NO_FATAL_FAILURE(
9314 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
9315 ASSERT_NO_FATAL_FAILURE(
9316 assertPointerCoords(args.pointerCoords[1], 560, 154, 1, 0, 0, 0, 0, 0, 0, 0));
9317
9318 // FINGER 1 MOVE
9319 processPosition(mapper, 540 + RAW_X_MIN, 690 + RAW_Y_MIN);
9320 processSync(mapper);
9321
9322 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
9323 // from move
9324 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9325 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9326 ASSERT_NO_FATAL_FAILURE(
9327 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
9328 ASSERT_NO_FATAL_FAILURE(
9329 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
9330
9331 // FINGER 0 MOVE
9332 processSlot(mapper, 0);
9333 processPosition(mapper, 50 + RAW_X_MIN, 800 + RAW_Y_MIN);
9334 processSync(mapper);
9335
9336 // expect coord[0] to contain new touch 0 location, coord[1] to contain previous location
9337 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9338 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9339 ASSERT_NO_FATAL_FAILURE(
9340 assertPointerCoords(args.pointerCoords[0], 50, 800, 1, 0, 0, 0, 0, 0, 0, 0));
9341 ASSERT_NO_FATAL_FAILURE(
9342 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
9343
9344 // BUTTON DOWN
9345 processKey(mapper, BTN_LEFT, 1);
9346 processSync(mapper);
9347
9348 // touchinputmapper design sends a move before button press
9349 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9350 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9351 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9352 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
9353
9354 // BUTTON UP
9355 processKey(mapper, BTN_LEFT, 0);
9356 processSync(mapper);
9357
9358 // touchinputmapper design sends a move after button release
9359 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9360 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
9361 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9362 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9363
9364 // FINGER 0 UP
9365 processId(mapper, -1);
9366 processSync(mapper);
9367 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9368 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | 0x0000, args.action);
9369
9370 // FINGER 1 MOVE
9371 processSlot(mapper, 1);
9372 processPosition(mapper, 320 + RAW_X_MIN, 900 + RAW_Y_MIN);
9373 processSync(mapper);
9374
9375 // expect coord[0] to contain new location of touch 1, and properties[0].id to contain 1
9376 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9377 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9378 ASSERT_EQ(1U, args.pointerCount);
9379 ASSERT_EQ(1, args.pointerProperties[0].id);
9380 ASSERT_NO_FATAL_FAILURE(
9381 assertPointerCoords(args.pointerCoords[0], 320, 900, 1, 0, 0, 0, 0, 0, 0, 0));
9382
9383 // FINGER 1 UP
9384 processId(mapper, -1);
9385 processKey(mapper, BTN_TOUCH, 0);
9386 processSync(mapper);
9387 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9388 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
9389
9390 // non captured touchpad should be a mouse source
9391 mFakePolicy->setPointerCapture(false);
9392 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
9393 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
9394 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
9395}
9396
9397TEST_F(MultiTouchInputMapperTest, Process_UnCapturedTouchpadPointer) {
9398 std::shared_ptr<FakePointerController> fakePointerController =
9399 std::make_shared<FakePointerController>();
9400 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
9401 fakePointerController->setPosition(0, 0);
9402 fakePointerController->setButtonState(0);
9403
9404 // prepare device and capture
9405 prepareDisplay(DISPLAY_ORIENTATION_0);
9406 prepareAxes(POSITION | ID | SLOT);
9407 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
9408 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009409 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009410 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9411 // run uncaptured pointer tests - pushes out generic events
9412 // FINGER 0 DOWN
9413 processId(mapper, 3);
9414 processPosition(mapper, 100, 100);
9415 processKey(mapper, BTN_TOUCH, 1);
9416 processSync(mapper);
9417
9418 // start at (100,100), cursor should be at (0,0) * scale
9419 NotifyMotionArgs args;
9420 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9421 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
9422 ASSERT_NO_FATAL_FAILURE(
9423 assertPointerCoords(args.pointerCoords[0], 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
9424
9425 // FINGER 0 MOVE
9426 processPosition(mapper, 200, 200);
9427 processSync(mapper);
9428
9429 // compute scaling to help with touch position checking
9430 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
9431 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
9432 float scale =
9433 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
9434
9435 // translate from (100,100) -> (200,200), cursor should have changed to (100,100) * scale)
9436 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9437 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
9438 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 100 * scale, 100 * scale, 0,
9439 0, 0, 0, 0, 0, 0, 0));
9440}
9441
9442TEST_F(MultiTouchInputMapperTest, WhenCapturedAndNotCaptured_GetSources) {
9443 std::shared_ptr<FakePointerController> fakePointerController =
9444 std::make_shared<FakePointerController>();
9445
9446 prepareDisplay(DISPLAY_ORIENTATION_0);
9447 prepareAxes(POSITION | ID | SLOT);
9448 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009449 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009450 mFakePolicy->setPointerCapture(false);
9451 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9452
9453 // uncaptured touchpad should be a pointer device
9454 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
9455
9456 // captured touchpad should be a touchpad device
9457 mFakePolicy->setPointerCapture(true);
9458 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
9459 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
9460}
9461
Arthur Hung6d5b4b22022-01-21 07:21:10 +00009462// --- JoystickInputMapperTest ---
9463
9464class JoystickInputMapperTest : public InputMapperTest {
9465protected:
9466 static const int32_t RAW_X_MIN;
9467 static const int32_t RAW_X_MAX;
9468 static const int32_t RAW_Y_MIN;
9469 static const int32_t RAW_Y_MAX;
9470
9471 void SetUp() override {
9472 InputMapperTest::SetUp(InputDeviceClass::JOYSTICK | InputDeviceClass::EXTERNAL);
9473 }
9474 void prepareAxes() {
9475 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
9476 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
9477 }
9478
9479 void processAxis(JoystickInputMapper& mapper, int32_t axis, int32_t value) {
9480 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, axis, value);
9481 }
9482
9483 void processSync(JoystickInputMapper& mapper) {
9484 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
9485 }
9486
9487 void prepareVirtualDisplay(int32_t orientation) {
9488 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH,
9489 VIRTUAL_DISPLAY_HEIGHT, orientation, VIRTUAL_DISPLAY_UNIQUE_ID,
9490 NO_PORT, ViewportType::VIRTUAL);
9491 }
9492};
9493
9494const int32_t JoystickInputMapperTest::RAW_X_MIN = -32767;
9495const int32_t JoystickInputMapperTest::RAW_X_MAX = 32767;
9496const int32_t JoystickInputMapperTest::RAW_Y_MIN = -32767;
9497const int32_t JoystickInputMapperTest::RAW_Y_MAX = 32767;
9498
9499TEST_F(JoystickInputMapperTest, Configure_AssignsDisplayUniqueId) {
9500 prepareAxes();
9501 JoystickInputMapper& mapper = addMapperAndConfigure<JoystickInputMapper>();
9502
9503 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
9504
9505 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
9506
9507 // Send an axis event
9508 processAxis(mapper, ABS_X, 100);
9509 processSync(mapper);
9510
9511 NotifyMotionArgs args;
9512 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9513 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
9514
9515 // Send another axis event
9516 processAxis(mapper, ABS_Y, 100);
9517 processSync(mapper);
9518
9519 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9520 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
9521}
9522
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009523// --- PeripheralControllerTest ---
Chris Yee2b1e5c2021-03-10 22:45:12 -08009524
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009525class PeripheralControllerTest : public testing::Test {
Chris Yee2b1e5c2021-03-10 22:45:12 -08009526protected:
9527 static const char* DEVICE_NAME;
9528 static const char* DEVICE_LOCATION;
9529 static const int32_t DEVICE_ID;
9530 static const int32_t DEVICE_GENERATION;
9531 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07009532 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Chris Yee2b1e5c2021-03-10 22:45:12 -08009533 static const int32_t EVENTHUB_ID;
9534
9535 std::shared_ptr<FakeEventHub> mFakeEventHub;
9536 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07009537 std::unique_ptr<TestInputListener> mFakeListener;
Chris Yee2b1e5c2021-03-10 22:45:12 -08009538 std::unique_ptr<InstrumentedInputReader> mReader;
9539 std::shared_ptr<InputDevice> mDevice;
9540
Dominik Laskowski2f01d772022-03-23 16:01:29 -07009541 virtual void SetUp(ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -08009542 mFakeEventHub = std::make_unique<FakeEventHub>();
9543 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07009544 mFakeListener = std::make_unique<TestInputListener>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009545 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07009546 *mFakeListener);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009547 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
9548 }
9549
9550 void SetUp() override { SetUp(DEVICE_CLASSES); }
9551
9552 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07009553 mFakeListener.reset();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009554 mFakePolicy.clear();
9555 }
9556
9557 void configureDevice(uint32_t changes) {
9558 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
9559 mReader->requestRefreshConfiguration(changes);
9560 mReader->loopOnce();
9561 }
9562 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
9563 }
9564
9565 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
9566 const std::string& location, int32_t eventHubId,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07009567 ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -08009568 InputDeviceIdentifier identifier;
9569 identifier.name = name;
9570 identifier.location = location;
9571 std::shared_ptr<InputDevice> device =
9572 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
9573 identifier);
9574 mReader->pushNextDevice(device);
9575 mFakeEventHub->addDevice(eventHubId, name, classes);
9576 mReader->loopOnce();
9577 return device;
9578 }
9579
9580 template <class T, typename... Args>
9581 T& addControllerAndConfigure(Args... args) {
9582 T& controller = mDevice->addController<T>(EVENTHUB_ID, args...);
9583
9584 return controller;
9585 }
9586};
9587
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009588const char* PeripheralControllerTest::DEVICE_NAME = "device";
9589const char* PeripheralControllerTest::DEVICE_LOCATION = "BLUETOOTH";
9590const int32_t PeripheralControllerTest::DEVICE_ID = END_RESERVED_ID + 1000;
9591const int32_t PeripheralControllerTest::DEVICE_GENERATION = 2;
9592const int32_t PeripheralControllerTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07009593const ftl::Flags<InputDeviceClass> PeripheralControllerTest::DEVICE_CLASSES =
9594 ftl::Flags<InputDeviceClass>(0); // not needed for current tests
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009595const int32_t PeripheralControllerTest::EVENTHUB_ID = 1;
Chris Yee2b1e5c2021-03-10 22:45:12 -08009596
9597// --- BatteryControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009598class BatteryControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -08009599protected:
9600 void SetUp() override {
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009601 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::BATTERY);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009602 }
9603};
9604
9605TEST_F(BatteryControllerTest, GetBatteryCapacity) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009606 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009607
9608 ASSERT_TRUE(controller.getBatteryCapacity(DEFAULT_BATTERY));
9609 ASSERT_EQ(controller.getBatteryCapacity(DEFAULT_BATTERY).value_or(-1), BATTERY_CAPACITY);
9610}
9611
9612TEST_F(BatteryControllerTest, GetBatteryStatus) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009613 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009614
9615 ASSERT_TRUE(controller.getBatteryStatus(DEFAULT_BATTERY));
9616 ASSERT_EQ(controller.getBatteryStatus(DEFAULT_BATTERY).value_or(-1), BATTERY_STATUS);
9617}
9618
9619// --- LightControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009620class LightControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -08009621protected:
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009622 void SetUp() override {
9623 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::LIGHT);
9624 }
Chris Yee2b1e5c2021-03-10 22:45:12 -08009625};
9626
Chris Ye85758332021-05-16 23:05:17 -07009627TEST_F(LightControllerTest, MonoLight) {
9628 RawLightInfo infoMono = {.id = 1,
9629 .name = "Mono",
9630 .maxBrightness = 255,
9631 .flags = InputLightClass::BRIGHTNESS,
9632 .path = ""};
9633 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
Chris Yee2b1e5c2021-03-10 22:45:12 -08009634
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009635 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009636 InputDeviceInfo info;
9637 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009638 std::vector<InputDeviceLightInfo> lights = info.getLights();
9639 ASSERT_EQ(1U, lights.size());
9640 ASSERT_EQ(InputDeviceLightType::MONO, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009641
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009642 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
9643 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009644}
9645
9646TEST_F(LightControllerTest, RGBLight) {
9647 RawLightInfo infoRed = {.id = 1,
9648 .name = "red",
9649 .maxBrightness = 255,
9650 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
9651 .path = ""};
9652 RawLightInfo infoGreen = {.id = 2,
9653 .name = "green",
9654 .maxBrightness = 255,
9655 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
9656 .path = ""};
9657 RawLightInfo infoBlue = {.id = 3,
9658 .name = "blue",
9659 .maxBrightness = 255,
9660 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
9661 .path = ""};
9662 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
9663 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
9664 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
9665
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009666 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009667 InputDeviceInfo info;
9668 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009669 std::vector<InputDeviceLightInfo> lights = info.getLights();
9670 ASSERT_EQ(1U, lights.size());
9671 ASSERT_EQ(InputDeviceLightType::RGB, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009672
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009673 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
9674 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009675}
9676
9677TEST_F(LightControllerTest, MultiColorRGBLight) {
9678 RawLightInfo infoColor = {.id = 1,
9679 .name = "red",
9680 .maxBrightness = 255,
9681 .flags = InputLightClass::BRIGHTNESS |
9682 InputLightClass::MULTI_INTENSITY |
9683 InputLightClass::MULTI_INDEX,
9684 .path = ""};
9685
9686 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
9687
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009688 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009689 InputDeviceInfo info;
9690 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009691 std::vector<InputDeviceLightInfo> lights = info.getLights();
9692 ASSERT_EQ(1U, lights.size());
9693 ASSERT_EQ(InputDeviceLightType::MULTI_COLOR, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009694
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009695 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
9696 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009697}
9698
9699TEST_F(LightControllerTest, PlayerIdLight) {
9700 RawLightInfo info1 = {.id = 1,
9701 .name = "player1",
9702 .maxBrightness = 255,
9703 .flags = InputLightClass::BRIGHTNESS,
9704 .path = ""};
9705 RawLightInfo info2 = {.id = 2,
9706 .name = "player2",
9707 .maxBrightness = 255,
9708 .flags = InputLightClass::BRIGHTNESS,
9709 .path = ""};
9710 RawLightInfo info3 = {.id = 3,
9711 .name = "player3",
9712 .maxBrightness = 255,
9713 .flags = InputLightClass::BRIGHTNESS,
9714 .path = ""};
9715 RawLightInfo info4 = {.id = 4,
9716 .name = "player4",
9717 .maxBrightness = 255,
9718 .flags = InputLightClass::BRIGHTNESS,
9719 .path = ""};
9720 mFakeEventHub->addRawLightInfo(info1.id, std::move(info1));
9721 mFakeEventHub->addRawLightInfo(info2.id, std::move(info2));
9722 mFakeEventHub->addRawLightInfo(info3.id, std::move(info3));
9723 mFakeEventHub->addRawLightInfo(info4.id, std::move(info4));
9724
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009725 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009726 InputDeviceInfo info;
9727 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009728 std::vector<InputDeviceLightInfo> lights = info.getLights();
9729 ASSERT_EQ(1U, lights.size());
9730 ASSERT_EQ(InputDeviceLightType::PLAYER_ID, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009731
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009732 ASSERT_FALSE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
9733 ASSERT_TRUE(controller.setLightPlayerId(lights[0].id, LIGHT_PLAYER_ID));
9734 ASSERT_EQ(controller.getLightPlayerId(lights[0].id).value_or(-1), LIGHT_PLAYER_ID);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009735}
9736
Michael Wrightd02c5b62014-02-10 15:10:22 -08009737} // namespace android