blob: 2ccb35e8cba697b608ab9863dbb49129017cae44 [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
990 const std::vector<int32_t> getRawBatteryIds(int32_t deviceId) { return {}; }
991
992 std::optional<RawBatteryInfo> getRawBatteryInfo(int32_t deviceId, int32_t batteryId) {
993 return std::nullopt;
994 }
Kim Low03ea0352020-11-06 12:45:07 -0800995
Chris Ye3fdbfef2021-01-06 18:45:18 -0800996 const std::vector<int32_t> getRawLightIds(int32_t deviceId) override {
997 std::vector<int32_t> ids;
998 for (const auto& [rawId, info] : mRawLightInfos) {
999 ids.push_back(rawId);
1000 }
1001 return ids;
1002 }
1003
1004 std::optional<RawLightInfo> getRawLightInfo(int32_t deviceId, int32_t lightId) override {
1005 auto it = mRawLightInfos.find(lightId);
1006 if (it == mRawLightInfos.end()) {
1007 return std::nullopt;
1008 }
1009 return it->second;
1010 }
1011
1012 void setLightBrightness(int32_t deviceId, int32_t lightId, int32_t brightness) override {
1013 mLightBrightness.emplace(lightId, brightness);
1014 }
1015
1016 void setLightIntensities(int32_t deviceId, int32_t lightId,
1017 std::unordered_map<LightColor, int32_t> intensities) override {
1018 mLightIntensities.emplace(lightId, intensities);
1019 };
1020
1021 std::optional<int32_t> getLightBrightness(int32_t deviceId, int32_t lightId) override {
1022 auto lightIt = mLightBrightness.find(lightId);
1023 if (lightIt == mLightBrightness.end()) {
1024 return std::nullopt;
1025 }
1026 return lightIt->second;
1027 }
1028
1029 std::optional<std::unordered_map<LightColor, int32_t>> getLightIntensities(
1030 int32_t deviceId, int32_t lightId) override {
1031 auto lightIt = mLightIntensities.find(lightId);
1032 if (lightIt == mLightIntensities.end()) {
1033 return std::nullopt;
1034 }
1035 return lightIt->second;
1036 };
1037
Narayan Kamath39efe3e2014-10-17 10:37:08 +01001038 virtual bool isExternal(int32_t) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001039 return false;
1040 }
1041
Chris Yea52ade12020-08-27 16:49:20 -07001042 void dump(std::string&) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001043
Chris Yea52ade12020-08-27 16:49:20 -07001044 void monitor() override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001045
Chris Yea52ade12020-08-27 16:49:20 -07001046 void requestReopenDevices() override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001047
Chris Yea52ade12020-08-27 16:49:20 -07001048 void wake() override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001049};
1050
Michael Wrightd02c5b62014-02-10 15:10:22 -08001051// --- FakeInputMapper ---
1052
1053class FakeInputMapper : public InputMapper {
1054 uint32_t mSources;
1055 int32_t mKeyboardType;
1056 int32_t mMetaState;
1057 KeyedVector<int32_t, int32_t> mKeyCodeStates;
1058 KeyedVector<int32_t, int32_t> mScanCodeStates;
1059 KeyedVector<int32_t, int32_t> mSwitchStates;
Philip Junker4af3b3d2021-12-14 10:36:55 +01001060 // fake mapping which would normally come from keyCharacterMap
1061 std::unordered_map<int32_t, int32_t> mKeyCodeMapping;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001062 std::vector<int32_t> mSupportedKeyCodes;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001063
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001064 std::mutex mLock;
1065 std::condition_variable mStateChangedCondition;
1066 bool mConfigureWasCalled GUARDED_BY(mLock);
1067 bool mResetWasCalled GUARDED_BY(mLock);
1068 bool mProcessWasCalled GUARDED_BY(mLock);
1069 RawEvent mLastEvent GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001070
Arthur Hungc23540e2018-11-29 20:42:11 +08001071 std::optional<DisplayViewport> mViewport;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001072public:
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001073 FakeInputMapper(InputDeviceContext& deviceContext, uint32_t sources)
1074 : InputMapper(deviceContext),
1075 mSources(sources),
1076 mKeyboardType(AINPUT_KEYBOARD_TYPE_NONE),
Michael Wrightd02c5b62014-02-10 15:10:22 -08001077 mMetaState(0),
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001078 mConfigureWasCalled(false),
1079 mResetWasCalled(false),
1080 mProcessWasCalled(false) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001081
Chris Yea52ade12020-08-27 16:49:20 -07001082 virtual ~FakeInputMapper() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001083
1084 void setKeyboardType(int32_t keyboardType) {
1085 mKeyboardType = keyboardType;
1086 }
1087
1088 void setMetaState(int32_t metaState) {
1089 mMetaState = metaState;
1090 }
1091
1092 void assertConfigureWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001093 std::unique_lock<std::mutex> lock(mLock);
1094 base::ScopedLockAssertion assumeLocked(mLock);
1095 const bool configureCalled =
1096 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1097 return mConfigureWasCalled;
1098 });
1099 if (!configureCalled) {
1100 FAIL() << "Expected configure() to have been called.";
1101 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001102 mConfigureWasCalled = false;
1103 }
1104
1105 void assertResetWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001106 std::unique_lock<std::mutex> lock(mLock);
1107 base::ScopedLockAssertion assumeLocked(mLock);
1108 const bool resetCalled =
1109 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1110 return mResetWasCalled;
1111 });
1112 if (!resetCalled) {
1113 FAIL() << "Expected reset() to have been called.";
1114 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001115 mResetWasCalled = false;
1116 }
1117
Yi Kong9b14ac62018-07-17 13:48:38 -07001118 void assertProcessWasCalled(RawEvent* outLastEvent = nullptr) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001119 std::unique_lock<std::mutex> lock(mLock);
1120 base::ScopedLockAssertion assumeLocked(mLock);
1121 const bool processCalled =
1122 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1123 return mProcessWasCalled;
1124 });
1125 if (!processCalled) {
1126 FAIL() << "Expected process() to have been called.";
1127 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001128 if (outLastEvent) {
1129 *outLastEvent = mLastEvent;
1130 }
1131 mProcessWasCalled = false;
1132 }
1133
1134 void setKeyCodeState(int32_t keyCode, int32_t state) {
1135 mKeyCodeStates.replaceValueFor(keyCode, state);
1136 }
1137
1138 void setScanCodeState(int32_t scanCode, int32_t state) {
1139 mScanCodeStates.replaceValueFor(scanCode, state);
1140 }
1141
1142 void setSwitchState(int32_t switchCode, int32_t state) {
1143 mSwitchStates.replaceValueFor(switchCode, state);
1144 }
1145
1146 void addSupportedKeyCode(int32_t keyCode) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001147 mSupportedKeyCodes.push_back(keyCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001148 }
1149
Philip Junker4af3b3d2021-12-14 10:36:55 +01001150 void addKeyCodeMapping(int32_t fromKeyCode, int32_t toKeyCode) {
1151 mKeyCodeMapping.insert_or_assign(fromKeyCode, toKeyCode);
1152 }
1153
Michael Wrightd02c5b62014-02-10 15:10:22 -08001154private:
Philip Junker4af3b3d2021-12-14 10:36:55 +01001155 uint32_t getSources() const override { return mSources; }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001156
Chris Yea52ade12020-08-27 16:49:20 -07001157 void populateDeviceInfo(InputDeviceInfo* deviceInfo) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001158 InputMapper::populateDeviceInfo(deviceInfo);
1159
1160 if (mKeyboardType != AINPUT_KEYBOARD_TYPE_NONE) {
1161 deviceInfo->setKeyboardType(mKeyboardType);
1162 }
1163 }
1164
Chris Yea52ade12020-08-27 16:49:20 -07001165 void configure(nsecs_t, const InputReaderConfiguration* config, uint32_t changes) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001166 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001167 mConfigureWasCalled = true;
Arthur Hungc23540e2018-11-29 20:42:11 +08001168
1169 // Find the associated viewport if exist.
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001170 const std::optional<uint8_t> displayPort = getDeviceContext().getAssociatedDisplayPort();
Arthur Hungc23540e2018-11-29 20:42:11 +08001171 if (displayPort && (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
1172 mViewport = config->getDisplayViewportByPort(*displayPort);
1173 }
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001174
1175 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001176 }
1177
Chris Yea52ade12020-08-27 16:49:20 -07001178 void reset(nsecs_t) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001179 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001180 mResetWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001181 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001182 }
1183
Chris Yea52ade12020-08-27 16:49:20 -07001184 void process(const RawEvent* rawEvent) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001185 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001186 mLastEvent = *rawEvent;
1187 mProcessWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001188 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001189 }
1190
Chris Yea52ade12020-08-27 16:49:20 -07001191 int32_t getKeyCodeState(uint32_t, int32_t keyCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001192 ssize_t index = mKeyCodeStates.indexOfKey(keyCode);
1193 return index >= 0 ? mKeyCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1194 }
1195
Philip Junker4af3b3d2021-12-14 10:36:55 +01001196 int32_t getKeyCodeForKeyLocation(int32_t locationKeyCode) const override {
1197 auto it = mKeyCodeMapping.find(locationKeyCode);
1198 return it != mKeyCodeMapping.end() ? it->second : locationKeyCode;
1199 }
1200
Chris Yea52ade12020-08-27 16:49:20 -07001201 int32_t getScanCodeState(uint32_t, int32_t scanCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001202 ssize_t index = mScanCodeStates.indexOfKey(scanCode);
1203 return index >= 0 ? mScanCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1204 }
1205
Chris Yea52ade12020-08-27 16:49:20 -07001206 int32_t getSwitchState(uint32_t, int32_t switchCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001207 ssize_t index = mSwitchStates.indexOfKey(switchCode);
1208 return index >= 0 ? mSwitchStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1209 }
1210
Chris Yea52ade12020-08-27 16:49:20 -07001211 // Return true if the device has non-empty key layout.
1212 bool markSupportedKeyCodes(uint32_t, size_t numCodes, const int32_t* keyCodes,
1213 uint8_t* outFlags) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001214 for (size_t i = 0; i < numCodes; i++) {
1215 for (size_t j = 0; j < mSupportedKeyCodes.size(); j++) {
1216 if (keyCodes[i] == mSupportedKeyCodes[j]) {
1217 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001218 }
1219 }
1220 }
Chris Yea52ade12020-08-27 16:49:20 -07001221 bool result = mSupportedKeyCodes.size() > 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001222 return result;
1223 }
1224
1225 virtual int32_t getMetaState() {
1226 return mMetaState;
1227 }
1228
1229 virtual void fadePointer() {
1230 }
Arthur Hungc23540e2018-11-29 20:42:11 +08001231
1232 virtual std::optional<int32_t> getAssociatedDisplay() {
1233 if (mViewport) {
1234 return std::make_optional(mViewport->displayId);
1235 }
1236 return std::nullopt;
1237 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001238};
1239
1240
1241// --- InstrumentedInputReader ---
1242
1243class InstrumentedInputReader : public InputReader {
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001244 std::queue<std::shared_ptr<InputDevice>> mNextDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001245
1246public:
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001247 InstrumentedInputReader(std::shared_ptr<EventHubInterface> eventHub,
1248 const sp<InputReaderPolicyInterface>& policy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001249 InputListenerInterface& listener)
arthurhungdcef2dc2020-08-11 14:47:50 +08001250 : InputReader(eventHub, policy, listener), mFakeContext(this) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001251
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001252 virtual ~InstrumentedInputReader() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001253
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001254 void pushNextDevice(std::shared_ptr<InputDevice> device) { mNextDevices.push(device); }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001255
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001256 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001257 const std::string& location = "") {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001258 InputDeviceIdentifier identifier;
1259 identifier.name = name;
Arthur Hungc23540e2018-11-29 20:42:11 +08001260 identifier.location = location;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001261 int32_t generation = deviceId + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08001262 return std::make_shared<InputDevice>(&mFakeContext, deviceId, generation, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001263 }
1264
Prabir Pradhan28efc192019-11-05 01:10:04 +00001265 // Make the protected loopOnce method accessible to tests.
1266 using InputReader::loopOnce;
1267
Michael Wrightd02c5b62014-02-10 15:10:22 -08001268protected:
Chris Ye1c2e0892020-11-30 21:41:44 -08001269 virtual std::shared_ptr<InputDevice> createDeviceLocked(int32_t eventHubId,
1270 const InputDeviceIdentifier& identifier)
1271 REQUIRES(mLock) {
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001272 if (!mNextDevices.empty()) {
1273 std::shared_ptr<InputDevice> device(std::move(mNextDevices.front()));
1274 mNextDevices.pop();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001275 return device;
1276 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001277 return InputReader::createDeviceLocked(eventHubId, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001278 }
1279
arthurhungdcef2dc2020-08-11 14:47:50 +08001280 // --- FakeInputReaderContext ---
1281 class FakeInputReaderContext : public ContextImpl {
1282 int32_t mGlobalMetaState;
1283 bool mUpdateGlobalMetaStateWasCalled;
1284 int32_t mGeneration;
1285
1286 public:
1287 FakeInputReaderContext(InputReader* reader)
1288 : ContextImpl(reader),
1289 mGlobalMetaState(0),
1290 mUpdateGlobalMetaStateWasCalled(false),
1291 mGeneration(1) {}
1292
1293 virtual ~FakeInputReaderContext() {}
1294
1295 void assertUpdateGlobalMetaStateWasCalled() {
1296 ASSERT_TRUE(mUpdateGlobalMetaStateWasCalled)
1297 << "Expected updateGlobalMetaState() to have been called.";
1298 mUpdateGlobalMetaStateWasCalled = false;
1299 }
1300
1301 void setGlobalMetaState(int32_t state) { mGlobalMetaState = state; }
1302
1303 uint32_t getGeneration() { return mGeneration; }
1304
1305 void updateGlobalMetaState() override {
1306 mUpdateGlobalMetaStateWasCalled = true;
1307 ContextImpl::updateGlobalMetaState();
1308 }
1309
1310 int32_t getGlobalMetaState() override {
1311 return mGlobalMetaState | ContextImpl::getGlobalMetaState();
1312 }
1313
1314 int32_t bumpGeneration() override {
1315 mGeneration = ContextImpl::bumpGeneration();
1316 return mGeneration;
1317 }
1318 } mFakeContext;
1319
Michael Wrightd02c5b62014-02-10 15:10:22 -08001320 friend class InputReaderTest;
arthurhungdcef2dc2020-08-11 14:47:50 +08001321
1322public:
1323 FakeInputReaderContext* getContext() { return &mFakeContext; }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001324};
1325
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001326// --- InputReaderPolicyTest ---
1327class InputReaderPolicyTest : public testing::Test {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001328protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001329 sp<FakeInputReaderPolicy> mFakePolicy;
1330
Chris Yea52ade12020-08-27 16:49:20 -07001331 void SetUp() override { mFakePolicy = new FakeInputReaderPolicy(); }
1332 void TearDown() override { mFakePolicy.clear(); }
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001333};
1334
1335/**
1336 * Check that empty set of viewports is an acceptable configuration.
1337 * Also try to get internal viewport two different ways - by type and by uniqueId.
1338 *
1339 * There will be confusion if two viewports with empty uniqueId and identical type are present.
1340 * Such configuration is not currently allowed.
1341 */
1342TEST_F(InputReaderPolicyTest, Viewports_GetCleared) {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001343 static const std::string uniqueId = "local:0";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001344
1345 // We didn't add any viewports yet, so there shouldn't be any.
1346 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001347 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001348 ASSERT_FALSE(internalViewport);
1349
1350 // Add an internal viewport, then clear it
1351 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001352 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId, NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001353 ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001354
1355 // Check matching by uniqueId
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001356 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001357 ASSERT_TRUE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001358 ASSERT_EQ(ViewportType::INTERNAL, internalViewport->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001359
1360 // Check matching by viewport type
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001361 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001362 ASSERT_TRUE(internalViewport);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001363 ASSERT_EQ(uniqueId, internalViewport->uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001364
1365 mFakePolicy->clearViewports();
1366 // Make sure nothing is found after clear
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001367 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001368 ASSERT_FALSE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001369 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001370 ASSERT_FALSE(internalViewport);
1371}
1372
1373TEST_F(InputReaderPolicyTest, Viewports_GetByType) {
1374 const std::string internalUniqueId = "local:0";
1375 const std::string externalUniqueId = "local:1";
1376 const std::string virtualUniqueId1 = "virtual:2";
1377 const std::string virtualUniqueId2 = "virtual:3";
1378 constexpr int32_t virtualDisplayId1 = 2;
1379 constexpr int32_t virtualDisplayId2 = 3;
1380
1381 // Add an internal viewport
1382 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001383 DISPLAY_ORIENTATION_0, true /*isActive*/, internalUniqueId,
1384 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001385 // Add an external viewport
1386 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001387 DISPLAY_ORIENTATION_0, true /*isActive*/, externalUniqueId,
1388 NO_PORT, ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001389 // Add an virtual viewport
1390 mFakePolicy->addDisplayViewport(virtualDisplayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001391 DISPLAY_ORIENTATION_0, true /*isActive*/, virtualUniqueId1,
1392 NO_PORT, ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001393 // Add another virtual viewport
1394 mFakePolicy->addDisplayViewport(virtualDisplayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001395 DISPLAY_ORIENTATION_0, true /*isActive*/, virtualUniqueId2,
1396 NO_PORT, ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001397
1398 // Check matching by type for internal
1399 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001400 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001401 ASSERT_TRUE(internalViewport);
1402 ASSERT_EQ(internalUniqueId, internalViewport->uniqueId);
1403
1404 // Check matching by type for external
1405 std::optional<DisplayViewport> externalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001406 mFakePolicy->getDisplayViewportByType(ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001407 ASSERT_TRUE(externalViewport);
1408 ASSERT_EQ(externalUniqueId, externalViewport->uniqueId);
1409
1410 // Check matching by uniqueId for virtual viewport #1
1411 std::optional<DisplayViewport> virtualViewport1 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001412 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001413 ASSERT_TRUE(virtualViewport1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001414 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport1->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001415 ASSERT_EQ(virtualUniqueId1, virtualViewport1->uniqueId);
1416 ASSERT_EQ(virtualDisplayId1, virtualViewport1->displayId);
1417
1418 // Check matching by uniqueId for virtual viewport #2
1419 std::optional<DisplayViewport> virtualViewport2 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001420 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001421 ASSERT_TRUE(virtualViewport2);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001422 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport2->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001423 ASSERT_EQ(virtualUniqueId2, virtualViewport2->uniqueId);
1424 ASSERT_EQ(virtualDisplayId2, virtualViewport2->displayId);
1425}
1426
1427
1428/**
1429 * We can have 2 viewports of the same kind. We can distinguish them by uniqueId, and confirm
1430 * that lookup works by checking display id.
1431 * Check that 2 viewports of each kind is possible, for all existing viewport types.
1432 */
1433TEST_F(InputReaderPolicyTest, Viewports_TwoOfSameType) {
1434 const std::string uniqueId1 = "uniqueId1";
1435 const std::string uniqueId2 = "uniqueId2";
1436 constexpr int32_t displayId1 = 2;
1437 constexpr int32_t displayId2 = 3;
1438
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001439 std::vector<ViewportType> types = {ViewportType::INTERNAL, ViewportType::EXTERNAL,
1440 ViewportType::VIRTUAL};
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001441 for (const ViewportType& type : types) {
1442 mFakePolicy->clearViewports();
1443 // Add a viewport
1444 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001445 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1,
1446 NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001447 // Add another viewport
1448 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001449 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2,
1450 NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001451
1452 // Check that correct display viewport was returned by comparing the display IDs.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001453 std::optional<DisplayViewport> viewport1 =
1454 mFakePolicy->getDisplayViewportByUniqueId(uniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001455 ASSERT_TRUE(viewport1);
1456 ASSERT_EQ(displayId1, viewport1->displayId);
1457 ASSERT_EQ(type, viewport1->type);
1458
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001459 std::optional<DisplayViewport> viewport2 =
1460 mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001461 ASSERT_TRUE(viewport2);
1462 ASSERT_EQ(displayId2, viewport2->displayId);
1463 ASSERT_EQ(type, viewport2->type);
1464
1465 // When there are multiple viewports of the same kind, and uniqueId is not specified
1466 // in the call to getDisplayViewport, then that situation is not supported.
1467 // The viewports can be stored in any order, so we cannot rely on the order, since that
1468 // is just implementation detail.
1469 // However, we can check that it still returns *a* viewport, we just cannot assert
1470 // which one specifically is returned.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001471 std::optional<DisplayViewport> someViewport = mFakePolicy->getDisplayViewportByType(type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001472 ASSERT_TRUE(someViewport);
1473 }
1474}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001475
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001476/**
Michael Wrightdde67b82020-10-27 16:09:22 +00001477 * When we have multiple internal displays make sure we always return the default display when
1478 * querying by type.
1479 */
1480TEST_F(InputReaderPolicyTest, Viewports_ByTypeReturnsDefaultForInternal) {
1481 const std::string uniqueId1 = "uniqueId1";
1482 const std::string uniqueId2 = "uniqueId2";
1483 constexpr int32_t nonDefaultDisplayId = 2;
1484 static_assert(nonDefaultDisplayId != ADISPLAY_ID_DEFAULT,
1485 "Test display ID should not be ADISPLAY_ID_DEFAULT");
1486
1487 // Add the default display first and ensure it gets returned.
1488 mFakePolicy->clearViewports();
1489 mFakePolicy->addDisplayViewport(ADISPLAY_ID_DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001490 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001491 ViewportType::INTERNAL);
1492 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001493 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001494 ViewportType::INTERNAL);
1495
1496 std::optional<DisplayViewport> viewport =
1497 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
1498 ASSERT_TRUE(viewport);
1499 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
1500 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
1501
1502 // Add the default display second to make sure order doesn't matter.
1503 mFakePolicy->clearViewports();
1504 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001505 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001506 ViewportType::INTERNAL);
1507 mFakePolicy->addDisplayViewport(ADISPLAY_ID_DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001508 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001509 ViewportType::INTERNAL);
1510
1511 viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
1512 ASSERT_TRUE(viewport);
1513 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
1514 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
1515}
1516
1517/**
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001518 * Check getDisplayViewportByPort
1519 */
1520TEST_F(InputReaderPolicyTest, Viewports_GetByPort) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001521 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001522 const std::string uniqueId1 = "uniqueId1";
1523 const std::string uniqueId2 = "uniqueId2";
1524 constexpr int32_t displayId1 = 1;
1525 constexpr int32_t displayId2 = 2;
1526 const uint8_t hdmi1 = 0;
1527 const uint8_t hdmi2 = 1;
1528 const uint8_t hdmi3 = 2;
1529
1530 mFakePolicy->clearViewports();
1531 // Add a viewport that's associated with some display port that's not of interest.
1532 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001533 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, hdmi3,
1534 type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001535 // Add another viewport, connected to HDMI1 port
1536 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001537 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, hdmi1,
1538 type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001539
1540 // Check that correct display viewport was returned by comparing the display ports.
1541 std::optional<DisplayViewport> hdmi1Viewport = mFakePolicy->getDisplayViewportByPort(hdmi1);
1542 ASSERT_TRUE(hdmi1Viewport);
1543 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1544 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1545
1546 // Check that we can still get the same viewport using the uniqueId
1547 hdmi1Viewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
1548 ASSERT_TRUE(hdmi1Viewport);
1549 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1550 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1551 ASSERT_EQ(type, hdmi1Viewport->type);
1552
1553 // Check that we cannot find a port with "HDMI2", because we never added one
1554 std::optional<DisplayViewport> hdmi2Viewport = mFakePolicy->getDisplayViewportByPort(hdmi2);
1555 ASSERT_FALSE(hdmi2Viewport);
1556}
1557
Michael Wrightd02c5b62014-02-10 15:10:22 -08001558// --- InputReaderTest ---
1559
1560class InputReaderTest : public testing::Test {
1561protected:
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001562 std::unique_ptr<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001563 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001564 std::shared_ptr<FakeEventHub> mFakeEventHub;
Prabir Pradhan28efc192019-11-05 01:10:04 +00001565 std::unique_ptr<InstrumentedInputReader> mReader;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001566
Chris Yea52ade12020-08-27 16:49:20 -07001567 void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001568 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001569 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001570 mFakeListener = std::make_unique<TestInputListener>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001571
Prabir Pradhan28efc192019-11-05 01:10:04 +00001572 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001573 *mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001574 }
1575
Chris Yea52ade12020-08-27 16:49:20 -07001576 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001577 mFakeListener.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001578 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001579 }
1580
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001581 void addDevice(int32_t eventHubId, const std::string& name,
1582 ftl::Flags<InputDeviceClass> classes, const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001583 mFakeEventHub->addDevice(eventHubId, name, classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001584
1585 if (configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001586 mFakeEventHub->addConfigurationMap(eventHubId, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001587 }
1588 mFakeEventHub->finishDeviceScan();
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001589 mReader->loopOnce();
1590 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001591 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1592 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001593 }
1594
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001595 void disableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001596 mFakePolicy->addDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001597 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001598 }
1599
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001600 void enableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001601 mFakePolicy->removeDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001602 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001603 }
1604
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001605 FakeInputMapper& addDeviceWithFakeInputMapper(int32_t deviceId, int32_t eventHubId,
Chris Ye1b0c7342020-07-28 21:57:03 -07001606 const std::string& name,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001607 ftl::Flags<InputDeviceClass> classes,
1608 uint32_t sources,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001609 const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001610 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, name);
1611 FakeInputMapper& mapper = device->addMapper<FakeInputMapper>(eventHubId, sources);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001612 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001613 addDevice(eventHubId, name, classes, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001614 return mapper;
1615 }
1616};
1617
Chris Ye98d3f532020-10-01 21:48:59 -07001618TEST_F(InputReaderTest, PolicyGetInputDevices) {
1619 ASSERT_NO_FATAL_FAILURE(addDevice(1, "keyboard", InputDeviceClass::KEYBOARD, nullptr));
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001620 ASSERT_NO_FATAL_FAILURE(addDevice(2, "ignored", ftl::Flags<InputDeviceClass>(0),
Chris Ye98d3f532020-10-01 21:48:59 -07001621 nullptr)); // no classes so device will be ignored
Michael Wrightd02c5b62014-02-10 15:10:22 -08001622
1623 // Should also have received a notification describing the new input devices.
Chris Ye98d3f532020-10-01 21:48:59 -07001624 const std::vector<InputDeviceInfo>& inputDevices = mFakePolicy->getInputDevices();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001625 ASSERT_EQ(1U, inputDevices.size());
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001626 ASSERT_EQ(END_RESERVED_ID + 1, inputDevices[0].getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001627 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001628 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
1629 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00001630 ASSERT_EQ(0U, inputDevices[0].getMotionRanges().size());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001631}
1632
Chris Yee7310032020-09-22 15:36:28 -07001633TEST_F(InputReaderTest, GetMergedInputDevices) {
1634 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1635 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1636 // Add two subdevices to device
1637 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1638 // Must add at least one mapper or the device will be ignored!
1639 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1640 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1641
1642 // Push same device instance for next device to be added, so they'll have same identifier.
1643 mReader->pushNextDevice(device);
1644 mReader->pushNextDevice(device);
1645 ASSERT_NO_FATAL_FAILURE(
1646 addDevice(eventHubIds[0], "fake1", InputDeviceClass::KEYBOARD, nullptr));
1647 ASSERT_NO_FATAL_FAILURE(
1648 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
1649
1650 // Two devices will be merged to one input device as they have same identifier
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00001651 ASSERT_EQ(1U, mFakePolicy->getInputDevices().size());
Chris Yee7310032020-09-22 15:36:28 -07001652}
1653
Chris Yee14523a2020-12-19 13:46:00 -08001654TEST_F(InputReaderTest, GetMergedInputDevicesEnabled) {
1655 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1656 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1657 // Add two subdevices to device
1658 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1659 // Must add at least one mapper or the device will be ignored!
1660 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1661 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1662
1663 // Push same device instance for next device to be added, so they'll have same identifier.
1664 mReader->pushNextDevice(device);
1665 mReader->pushNextDevice(device);
1666 // Sensor device is initially disabled
1667 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1",
1668 InputDeviceClass::KEYBOARD | InputDeviceClass::SENSOR,
1669 nullptr));
1670 // Device is disabled because the only sub device is a sensor device and disabled initially.
1671 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1672 ASSERT_FALSE(device->isEnabled());
1673 ASSERT_NO_FATAL_FAILURE(
1674 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
1675 // The merged device is enabled if any sub device is enabled
1676 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1677 ASSERT_TRUE(device->isEnabled());
1678}
1679
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001680TEST_F(InputReaderTest, WhenEnabledChanges_SendsDeviceResetNotification) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001681 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001682 constexpr ftl::Flags<InputDeviceClass> deviceClass(InputDeviceClass::KEYBOARD);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001683 constexpr int32_t eventHubId = 1;
1684 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001685 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001686 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001687 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001688 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001689
Yi Kong9b14ac62018-07-17 13:48:38 -07001690 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001691
1692 NotifyDeviceResetArgs resetArgs;
1693 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001694 ASSERT_EQ(deviceId, resetArgs.deviceId);
1695
1696 ASSERT_EQ(device->isEnabled(), true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001697 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001698 mReader->loopOnce();
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001699
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001700 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001701 ASSERT_EQ(deviceId, resetArgs.deviceId);
1702 ASSERT_EQ(device->isEnabled(), false);
1703
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001704 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001705 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001706 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
1707 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasNotCalled());
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001708 ASSERT_EQ(device->isEnabled(), false);
1709
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001710 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001711 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001712 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001713 ASSERT_EQ(deviceId, resetArgs.deviceId);
1714 ASSERT_EQ(device->isEnabled(), true);
1715}
1716
Michael Wrightd02c5b62014-02-10 15:10:22 -08001717TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001718 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001719 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001720 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001721 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001722 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001723 AINPUT_SOURCE_KEYBOARD, nullptr);
1724 mapper.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001725
1726 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(0,
1727 AINPUT_SOURCE_ANY, AKEYCODE_A))
1728 << "Should return unknown when the device id is >= 0 but unknown.";
1729
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001730 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1731 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1732 << "Should return unknown when the device id is valid but the sources are not "
1733 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001734
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001735 ASSERT_EQ(AKEY_STATE_DOWN,
1736 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1737 AKEYCODE_A))
1738 << "Should return value provided by mapper when device id is valid and the device "
1739 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001740
1741 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(-1,
1742 AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1743 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1744
1745 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getKeyCodeState(-1,
1746 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1747 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1748}
1749
Philip Junker4af3b3d2021-12-14 10:36:55 +01001750TEST_F(InputReaderTest, GetKeyCodeForKeyLocation_ForwardsRequestsToMappers) {
1751 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1752 constexpr int32_t eventHubId = 1;
1753 FakeInputMapper& mapper = addDeviceWithFakeInputMapper(deviceId, eventHubId, "keyboard",
1754 InputDeviceClass::KEYBOARD,
1755 AINPUT_SOURCE_KEYBOARD, nullptr);
1756 mapper.addKeyCodeMapping(AKEYCODE_Y, AKEYCODE_Z);
1757
1758 ASSERT_EQ(AKEYCODE_UNKNOWN, mReader->getKeyCodeForKeyLocation(0, AKEYCODE_Y))
1759 << "Should return unknown when the device with the specified id is not found.";
1760
1761 ASSERT_EQ(AKEYCODE_Z, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_Y))
1762 << "Should return correct mapping when device id is valid and mapping exists.";
1763
1764 ASSERT_EQ(AKEYCODE_A, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_A))
1765 << "Should return the location key code when device id is valid and there's no "
1766 "mapping.";
1767}
1768
1769TEST_F(InputReaderTest, GetKeyCodeForKeyLocation_NoKeyboardMapper) {
1770 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1771 constexpr int32_t eventHubId = 1;
1772 FakeInputMapper& mapper = addDeviceWithFakeInputMapper(deviceId, eventHubId, "joystick",
1773 InputDeviceClass::JOYSTICK,
1774 AINPUT_SOURCE_GAMEPAD, nullptr);
1775 mapper.addKeyCodeMapping(AKEYCODE_Y, AKEYCODE_Z);
1776
1777 ASSERT_EQ(AKEYCODE_UNKNOWN, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_Y))
1778 << "Should return unknown when the device id is valid but there is no keyboard mapper";
1779}
1780
Michael Wrightd02c5b62014-02-10 15:10:22 -08001781TEST_F(InputReaderTest, GetScanCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001782 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001783 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001784 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001785 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001786 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001787 AINPUT_SOURCE_KEYBOARD, nullptr);
1788 mapper.setScanCodeState(KEY_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001789
1790 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(0,
1791 AINPUT_SOURCE_ANY, KEY_A))
1792 << "Should return unknown when the device id is >= 0 but unknown.";
1793
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001794 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1795 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, KEY_A))
1796 << "Should return unknown when the device id is valid but the sources are not "
1797 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001798
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001799 ASSERT_EQ(AKEY_STATE_DOWN,
1800 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1801 KEY_A))
1802 << "Should return value provided by mapper when device id is valid and the device "
1803 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001804
1805 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(-1,
1806 AINPUT_SOURCE_TRACKBALL, KEY_A))
1807 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1808
1809 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getScanCodeState(-1,
1810 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, KEY_A))
1811 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1812}
1813
1814TEST_F(InputReaderTest, GetSwitchState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001815 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001816 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001817 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001818 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001819 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001820 AINPUT_SOURCE_KEYBOARD, nullptr);
1821 mapper.setSwitchState(SW_LID, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001822
1823 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(0,
1824 AINPUT_SOURCE_ANY, SW_LID))
1825 << "Should return unknown when the device id is >= 0 but unknown.";
1826
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001827 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1828 mReader->getSwitchState(deviceId, AINPUT_SOURCE_TRACKBALL, SW_LID))
1829 << "Should return unknown when the device id is valid but the sources are not "
1830 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001831
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001832 ASSERT_EQ(AKEY_STATE_DOWN,
1833 mReader->getSwitchState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1834 SW_LID))
1835 << "Should return value provided by mapper when device id is valid and the device "
1836 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001837
1838 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(-1,
1839 AINPUT_SOURCE_TRACKBALL, SW_LID))
1840 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1841
1842 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getSwitchState(-1,
1843 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, SW_LID))
1844 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1845}
1846
1847TEST_F(InputReaderTest, MarkSupportedKeyCodes_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001848 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001849 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001850 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001851 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001852 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001853 AINPUT_SOURCE_KEYBOARD, nullptr);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001854
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001855 mapper.addSupportedKeyCode(AKEYCODE_A);
1856 mapper.addSupportedKeyCode(AKEYCODE_B);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001857
1858 const int32_t keyCodes[4] = { AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2 };
1859 uint8_t flags[4] = { 0, 0, 0, 1 };
1860
1861 ASSERT_FALSE(mReader->hasKeys(0, AINPUT_SOURCE_ANY, 4, keyCodes, flags))
1862 << "Should return false when device id is >= 0 but unknown.";
1863 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1864
1865 flags[3] = 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001866 ASSERT_FALSE(mReader->hasKeys(deviceId, AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1867 << "Should return false when device id is valid but the sources are not supported by "
1868 "the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001869 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1870
1871 flags[3] = 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001872 ASSERT_TRUE(mReader->hasKeys(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, 4,
1873 keyCodes, flags))
1874 << "Should return value provided by mapper when device id is valid and the device "
1875 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001876 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1877
1878 flags[3] = 1;
1879 ASSERT_FALSE(mReader->hasKeys(-1, AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1880 << "Should return false when the device id is < 0 but the sources are not supported by any device.";
1881 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1882
1883 flags[3] = 1;
1884 ASSERT_TRUE(mReader->hasKeys(-1, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1885 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1886 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1887}
1888
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001889TEST_F(InputReaderTest, LoopOnce_WhenDeviceScanFinished_SendsConfigurationChanged) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001890 constexpr int32_t eventHubId = 1;
Chris Ye1b0c7342020-07-28 21:57:03 -07001891 addDevice(eventHubId, "ignored", InputDeviceClass::KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001892
1893 NotifyConfigurationChangedArgs args;
1894
1895 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(&args));
1896 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
1897}
1898
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001899TEST_F(InputReaderTest, LoopOnce_ForwardsRawEventsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001900 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001901 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001902 constexpr nsecs_t when = 0;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001903 constexpr int32_t eventHubId = 1;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001904 constexpr nsecs_t readTime = 2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001905 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001906 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001907 AINPUT_SOURCE_KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001908
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001909 mFakeEventHub->enqueueEvent(when, readTime, eventHubId, EV_KEY, KEY_A, 1);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001910 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001911 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
1912
1913 RawEvent event;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001914 ASSERT_NO_FATAL_FAILURE(mapper.assertProcessWasCalled(&event));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001915 ASSERT_EQ(when, event.when);
1916 ASSERT_EQ(readTime, event.readTime);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001917 ASSERT_EQ(eventHubId, event.deviceId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001918 ASSERT_EQ(EV_KEY, event.type);
1919 ASSERT_EQ(KEY_A, event.code);
1920 ASSERT_EQ(1, event.value);
1921}
1922
Garfield Tan1c7bc862020-01-28 13:24:04 -08001923TEST_F(InputReaderTest, DeviceReset_RandomId) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001924 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001925 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001926 constexpr int32_t eventHubId = 1;
1927 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Prabir Pradhan42611e02018-11-27 14:04:02 -08001928 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001929 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001930 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001931 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan42611e02018-11-27 14:04:02 -08001932
1933 NotifyDeviceResetArgs resetArgs;
1934 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001935 int32_t prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001936
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001937 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001938 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001939 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001940 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001941 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001942
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001943 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001944 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001945 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001946 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001947 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001948
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001949 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001950 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001951 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001952 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001953 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001954}
1955
Garfield Tan1c7bc862020-01-28 13:24:04 -08001956TEST_F(InputReaderTest, DeviceReset_GenerateIdWithInputReaderSource) {
1957 constexpr int32_t deviceId = 1;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001958 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Garfield Tan1c7bc862020-01-28 13:24:04 -08001959 constexpr int32_t eventHubId = 1;
1960 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1961 // Must add at least one mapper or the device will be ignored!
1962 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001963 mReader->pushNextDevice(device);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001964 ASSERT_NO_FATAL_FAILURE(addDevice(deviceId, "fake", deviceClass, nullptr));
1965
1966 NotifyDeviceResetArgs resetArgs;
1967 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1968 ASSERT_EQ(IdGenerator::Source::INPUT_READER, IdGenerator::getSource(resetArgs.id));
1969}
1970
Arthur Hungc23540e2018-11-29 20:42:11 +08001971TEST_F(InputReaderTest, Device_CanDispatchToDisplay) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001972 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001973 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001974 constexpr int32_t eventHubId = 1;
Arthur Hungc23540e2018-11-29 20:42:11 +08001975 const char* DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001976 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1977 FakeInputMapper& mapper =
1978 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001979 mReader->pushNextDevice(device);
Arthur Hungc23540e2018-11-29 20:42:11 +08001980
1981 const uint8_t hdmi1 = 1;
1982
1983 // Associated touch screen with second display.
1984 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
1985
1986 // Add default and second display.
Prabir Pradhan28efc192019-11-05 01:10:04 +00001987 mFakePolicy->clearViewports();
Arthur Hungc23540e2018-11-29 20:42:11 +08001988 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001989 DISPLAY_ORIENTATION_0, true /*isActive*/, "local:0", NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001990 ViewportType::INTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08001991 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001992 DISPLAY_ORIENTATION_0, true /*isActive*/, "local:1", hdmi1,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001993 ViewportType::EXTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08001994 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001995 mReader->loopOnce();
Prabir Pradhan28efc192019-11-05 01:10:04 +00001996
1997 // Add the device, and make sure all of the callbacks are triggered.
1998 // The device is added after the input port associations are processed since
1999 // we do not yet support dynamic device-to-display associations.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002000 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07002001 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled());
Prabir Pradhan28efc192019-11-05 01:10:04 +00002002 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002003 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
Arthur Hungc23540e2018-11-29 20:42:11 +08002004
Arthur Hung2c9a3342019-07-23 14:18:59 +08002005 // Device should only dispatch to the specified display.
Arthur Hungc23540e2018-11-29 20:42:11 +08002006 ASSERT_EQ(deviceId, device->getId());
2007 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, DISPLAY_ID));
2008 ASSERT_TRUE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hung2c9a3342019-07-23 14:18:59 +08002009
2010 // Can't dispatch event from a disabled device.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002011 disableDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00002012 mReader->loopOnce();
Arthur Hung2c9a3342019-07-23 14:18:59 +08002013 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hungc23540e2018-11-29 20:42:11 +08002014}
2015
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08002016TEST_F(InputReaderTest, WhenEnabledChanges_AllSubdevicesAreUpdated) {
2017 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002018 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08002019 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
2020 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
2021 // Must add at least one mapper or the device will be ignored!
2022 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
2023 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
2024 mReader->pushNextDevice(device);
2025 mReader->pushNextDevice(device);
2026 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
2027 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
2028
2029 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
2030
2031 NotifyDeviceResetArgs resetArgs;
2032 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2033 ASSERT_EQ(deviceId, resetArgs.deviceId);
2034 ASSERT_TRUE(device->isEnabled());
2035 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
2036 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
2037
2038 disableDevice(deviceId);
2039 mReader->loopOnce();
2040
2041 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2042 ASSERT_EQ(deviceId, resetArgs.deviceId);
2043 ASSERT_FALSE(device->isEnabled());
2044 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
2045 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
2046
2047 enableDevice(deviceId);
2048 mReader->loopOnce();
2049
2050 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2051 ASSERT_EQ(deviceId, resetArgs.deviceId);
2052 ASSERT_TRUE(device->isEnabled());
2053 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
2054 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
2055}
2056
2057TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToSubdeviceMappers) {
2058 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002059 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08002060 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
2061 // Add two subdevices to device
2062 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
2063 FakeInputMapper& mapperDevice1 =
2064 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
2065 FakeInputMapper& mapperDevice2 =
2066 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
2067 mReader->pushNextDevice(device);
2068 mReader->pushNextDevice(device);
2069 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
2070 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
2071
2072 mapperDevice1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
2073 mapperDevice2.setKeyCodeState(AKEYCODE_B, AKEY_STATE_DOWN);
2074
2075 ASSERT_EQ(AKEY_STATE_DOWN,
2076 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_A));
2077 ASSERT_EQ(AKEY_STATE_DOWN,
2078 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_B));
2079 ASSERT_EQ(AKEY_STATE_UNKNOWN,
2080 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_C));
2081}
2082
Prabir Pradhan7e186182020-11-10 13:56:45 -08002083TEST_F(InputReaderTest, ChangingPointerCaptureNotifiesInputListener) {
2084 NotifyPointerCaptureChangedArgs args;
2085
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002086 auto request = mFakePolicy->setPointerCapture(true);
Prabir Pradhan7e186182020-11-10 13:56:45 -08002087 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
2088 mReader->loopOnce();
2089 mFakeListener->assertNotifyCaptureWasCalled(&args);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002090 ASSERT_TRUE(args.request.enable) << "Pointer Capture should be enabled.";
2091 ASSERT_EQ(args.request, request) << "Pointer Capture sequence number should match.";
Prabir Pradhan7e186182020-11-10 13:56:45 -08002092
2093 mFakePolicy->setPointerCapture(false);
2094 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
2095 mReader->loopOnce();
2096 mFakeListener->assertNotifyCaptureWasCalled(&args);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002097 ASSERT_FALSE(args.request.enable) << "Pointer Capture should be disabled.";
Prabir Pradhan7e186182020-11-10 13:56:45 -08002098
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002099 // Verify that the Pointer Capture state is not updated when the configuration value
Prabir Pradhan7e186182020-11-10 13:56:45 -08002100 // does not change.
2101 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
2102 mReader->loopOnce();
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002103 mFakeListener->assertNotifyCaptureWasNotCalled();
Prabir Pradhan7e186182020-11-10 13:56:45 -08002104}
2105
Chris Ye87143712020-11-10 05:05:58 +00002106class FakeVibratorInputMapper : public FakeInputMapper {
2107public:
2108 FakeVibratorInputMapper(InputDeviceContext& deviceContext, uint32_t sources)
2109 : FakeInputMapper(deviceContext, sources) {}
2110
2111 std::vector<int32_t> getVibratorIds() override { return getDeviceContext().getVibratorIds(); }
2112};
2113
2114TEST_F(InputReaderTest, VibratorGetVibratorIds) {
2115 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002116 ftl::Flags<InputDeviceClass> deviceClass =
2117 InputDeviceClass::KEYBOARD | InputDeviceClass::VIBRATOR;
Chris Ye87143712020-11-10 05:05:58 +00002118 constexpr int32_t eventHubId = 1;
2119 const char* DEVICE_LOCATION = "BLUETOOTH";
2120 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
2121 FakeVibratorInputMapper& mapper =
2122 device->addMapper<FakeVibratorInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
2123 mReader->pushNextDevice(device);
2124
2125 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2126 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
2127
2128 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
2129 ASSERT_EQ(mReader->getVibratorIds(deviceId).size(), 2U);
2130}
2131
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002132// --- FakePeripheralController ---
Kim Low03ea0352020-11-06 12:45:07 -08002133
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002134class FakePeripheralController : public PeripheralControllerInterface {
Chris Yee2b1e5c2021-03-10 22:45:12 -08002135public:
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002136 FakePeripheralController(InputDeviceContext& deviceContext) : mDeviceContext(deviceContext) {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08002137
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002138 ~FakePeripheralController() override {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08002139
2140 void populateDeviceInfo(InputDeviceInfo* deviceInfo) override {}
2141
2142 void dump(std::string& dump) override {}
2143
2144 std::optional<int32_t> getBatteryCapacity(int32_t batteryId) override {
2145 return getDeviceContext().getBatteryCapacity(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08002146 }
2147
Chris Yee2b1e5c2021-03-10 22:45:12 -08002148 std::optional<int32_t> getBatteryStatus(int32_t batteryId) override {
2149 return getDeviceContext().getBatteryStatus(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08002150 }
Chris Ye3fdbfef2021-01-06 18:45:18 -08002151
2152 bool setLightColor(int32_t lightId, int32_t color) override {
2153 getDeviceContext().setLightBrightness(lightId, color >> 24);
2154 return true;
2155 }
2156
2157 std::optional<int32_t> getLightColor(int32_t lightId) override {
2158 std::optional<int32_t> result = getDeviceContext().getLightBrightness(lightId);
2159 if (!result.has_value()) {
2160 return std::nullopt;
2161 }
2162 return result.value() << 24;
2163 }
Chris Yee2b1e5c2021-03-10 22:45:12 -08002164
2165 bool setLightPlayerId(int32_t lightId, int32_t playerId) override { return true; }
2166
2167 std::optional<int32_t> getLightPlayerId(int32_t lightId) override { return std::nullopt; }
2168
2169private:
2170 InputDeviceContext& mDeviceContext;
2171 inline int32_t getDeviceId() { return mDeviceContext.getId(); }
2172 inline InputDeviceContext& getDeviceContext() { return mDeviceContext; }
Chris Ye3fdbfef2021-01-06 18:45:18 -08002173};
2174
Chris Yee2b1e5c2021-03-10 22:45:12 -08002175TEST_F(InputReaderTest, BatteryGetCapacity) {
2176 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002177 ftl::Flags<InputDeviceClass> deviceClass =
2178 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
Chris Yee2b1e5c2021-03-10 22:45:12 -08002179 constexpr int32_t eventHubId = 1;
2180 const char* DEVICE_LOCATION = "BLUETOOTH";
2181 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002182 FakePeripheralController& controller =
2183 device->addController<FakePeripheralController>(eventHubId);
Chris Yee2b1e5c2021-03-10 22:45:12 -08002184 mReader->pushNextDevice(device);
2185
2186 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2187
2188 ASSERT_EQ(controller.getBatteryCapacity(DEFAULT_BATTERY), BATTERY_CAPACITY);
2189 ASSERT_EQ(mReader->getBatteryCapacity(deviceId), BATTERY_CAPACITY);
2190}
2191
2192TEST_F(InputReaderTest, BatteryGetStatus) {
2193 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002194 ftl::Flags<InputDeviceClass> deviceClass =
2195 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
Chris Yee2b1e5c2021-03-10 22:45:12 -08002196 constexpr int32_t eventHubId = 1;
2197 const char* DEVICE_LOCATION = "BLUETOOTH";
2198 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002199 FakePeripheralController& controller =
2200 device->addController<FakePeripheralController>(eventHubId);
Chris Yee2b1e5c2021-03-10 22:45:12 -08002201 mReader->pushNextDevice(device);
2202
2203 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2204
2205 ASSERT_EQ(controller.getBatteryStatus(DEFAULT_BATTERY), BATTERY_STATUS);
2206 ASSERT_EQ(mReader->getBatteryStatus(deviceId), BATTERY_STATUS);
2207}
2208
Chris Ye3fdbfef2021-01-06 18:45:18 -08002209TEST_F(InputReaderTest, LightGetColor) {
2210 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002211 ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD | InputDeviceClass::LIGHT;
Chris Ye3fdbfef2021-01-06 18:45:18 -08002212 constexpr int32_t eventHubId = 1;
2213 const char* DEVICE_LOCATION = "BLUETOOTH";
2214 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002215 FakePeripheralController& controller =
2216 device->addController<FakePeripheralController>(eventHubId);
Chris Ye3fdbfef2021-01-06 18:45:18 -08002217 mReader->pushNextDevice(device);
2218 RawLightInfo info = {.id = 1,
2219 .name = "Mono",
2220 .maxBrightness = 255,
2221 .flags = InputLightClass::BRIGHTNESS,
2222 .path = ""};
2223 mFakeEventHub->addRawLightInfo(1 /* rawId */, std::move(info));
2224 mFakeEventHub->fakeLightBrightness(1 /* rawId */, 0x55);
2225
2226 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Chris Ye3fdbfef2021-01-06 18:45:18 -08002227
Chris Yee2b1e5c2021-03-10 22:45:12 -08002228 ASSERT_TRUE(controller.setLightColor(1 /* lightId */, LIGHT_BRIGHTNESS));
2229 ASSERT_EQ(controller.getLightColor(1 /* lightId */), LIGHT_BRIGHTNESS);
Chris Ye3fdbfef2021-01-06 18:45:18 -08002230 ASSERT_TRUE(mReader->setLightColor(deviceId, 1 /* lightId */, LIGHT_BRIGHTNESS));
2231 ASSERT_EQ(mReader->getLightColor(deviceId, 1 /* lightId */), LIGHT_BRIGHTNESS);
2232}
2233
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002234// --- InputReaderIntegrationTest ---
2235
2236// These tests create and interact with the InputReader only through its interface.
2237// The InputReader is started during SetUp(), which starts its processing in its own
2238// thread. The tests use linux uinput to emulate input devices.
2239// NOTE: Interacting with the physical device while these tests are running may cause
2240// the tests to fail.
2241class InputReaderIntegrationTest : public testing::Test {
2242protected:
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002243 std::unique_ptr<TestInputListener> mTestListener;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002244 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002245 std::unique_ptr<InputReaderInterface> mReader;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002246
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00002247 std::shared_ptr<FakePointerController> mFakePointerController;
2248
Chris Yea52ade12020-08-27 16:49:20 -07002249 void SetUp() override {
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002250 mFakePolicy = new FakeInputReaderPolicy();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00002251 mFakePointerController = std::make_shared<FakePointerController>();
2252 mFakePolicy->setPointerController(mFakePointerController);
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002253 mTestListener = std::make_unique<TestInputListener>(2000ms /*eventHappenedTimeout*/,
2254 30ms /*eventDidNotHappenTimeout*/);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002255
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002256 mReader = std::make_unique<InputReader>(std::make_shared<EventHub>(), mFakePolicy,
2257 *mTestListener);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002258 ASSERT_EQ(mReader->start(), OK);
2259
2260 // Since this test is run on a real device, all the input devices connected
2261 // to the test device will show up in mReader. We wait for those input devices to
2262 // show up before beginning the tests.
2263 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2264 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2265 }
2266
Chris Yea52ade12020-08-27 16:49:20 -07002267 void TearDown() override {
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002268 ASSERT_EQ(mReader->stop(), OK);
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002269 mReader.reset();
2270 mTestListener.reset();
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002271 mFakePolicy.clear();
2272 }
2273};
2274
2275TEST_F(InputReaderIntegrationTest, TestInvalidDevice) {
2276 // An invalid input device that is only used for this test.
2277 class InvalidUinputDevice : public UinputDevice {
2278 public:
2279 InvalidUinputDevice() : UinputDevice("Invalid Device") {}
2280
2281 private:
2282 void configureDevice(int fd, uinput_user_dev* device) override {}
2283 };
2284
2285 const size_t numDevices = mFakePolicy->getInputDevices().size();
2286
2287 // UinputDevice does not set any event or key bits, so InputReader should not
2288 // consider it as a valid device.
2289 std::unique_ptr<UinputDevice> invalidDevice = createUinputDevice<InvalidUinputDevice>();
2290 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
2291 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
2292 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
2293
2294 invalidDevice.reset();
2295 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
2296 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
2297 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
2298}
2299
2300TEST_F(InputReaderIntegrationTest, AddNewDevice) {
2301 const size_t initialNumDevices = mFakePolicy->getInputDevices().size();
2302
2303 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
2304 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2305 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2306 ASSERT_EQ(initialNumDevices + 1, mFakePolicy->getInputDevices().size());
2307
2308 // Find the test device by its name.
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002309 const std::vector<InputDeviceInfo> inputDevices = mFakePolicy->getInputDevices();
Chris Ye98d3f532020-10-01 21:48:59 -07002310 const auto& it =
2311 std::find_if(inputDevices.begin(), inputDevices.end(),
2312 [&keyboard](const InputDeviceInfo& info) {
2313 return info.getIdentifier().name == keyboard->getName();
2314 });
2315
2316 ASSERT_NE(it, inputDevices.end());
2317 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, it->getKeyboardType());
2318 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, it->getSources());
2319 ASSERT_EQ(0U, it->getMotionRanges().size());
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002320
2321 keyboard.reset();
2322 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2323 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2324 ASSERT_EQ(initialNumDevices, mFakePolicy->getInputDevices().size());
2325}
2326
2327TEST_F(InputReaderIntegrationTest, SendsEventsToInputListener) {
2328 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
2329 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2330
2331 NotifyConfigurationChangedArgs configChangedArgs;
2332 ASSERT_NO_FATAL_FAILURE(
2333 mTestListener->assertNotifyConfigurationChangedWasCalled(&configChangedArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002334 int32_t prevId = configChangedArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002335 nsecs_t prevTimestamp = configChangedArgs.eventTime;
2336
2337 NotifyKeyArgs keyArgs;
2338 keyboard->pressAndReleaseHomeKey();
2339 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
2340 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002341 ASSERT_NE(prevId, keyArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002342 prevId = keyArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002343 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002344 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002345 prevTimestamp = keyArgs.eventTime;
2346
2347 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
2348 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002349 ASSERT_NE(prevId, keyArgs.id);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002350 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002351 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002352}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002353
Siarhei Vishniakoua0d2b802020-05-13 14:00:31 -07002354/**
2355 * The Steam controller sends BTN_GEAR_DOWN and BTN_GEAR_UP for the two "paddle" buttons
2356 * on the back. In this test, we make sure that BTN_GEAR_DOWN / BTN_WHEEL and BTN_GEAR_UP
2357 * are passed to the listener.
2358 */
2359static_assert(BTN_GEAR_DOWN == BTN_WHEEL);
2360TEST_F(InputReaderIntegrationTest, SendsGearDownAndUpToInputListener) {
2361 std::unique_ptr<UinputSteamController> controller = createUinputDevice<UinputSteamController>();
2362 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2363 NotifyKeyArgs keyArgs;
2364
2365 controller->pressAndReleaseKey(BTN_GEAR_DOWN);
2366 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
2367 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
2368 ASSERT_EQ(BTN_GEAR_DOWN, keyArgs.scanCode);
2369
2370 controller->pressAndReleaseKey(BTN_GEAR_UP);
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_UP, keyArgs.scanCode);
2374}
2375
Arthur Hungaab25622020-01-16 11:22:11 +08002376// --- TouchProcessTest ---
2377class TouchIntegrationTest : public InputReaderIntegrationTest {
2378protected:
Arthur Hungaab25622020-01-16 11:22:11 +08002379 const std::string UNIQUE_ID = "local:0";
2380
Chris Yea52ade12020-08-27 16:49:20 -07002381 void SetUp() override {
Arthur Hungaab25622020-01-16 11:22:11 +08002382 InputReaderIntegrationTest::SetUp();
2383 // At least add an internal display.
2384 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2385 DISPLAY_ORIENTATION_0, UNIQUE_ID, NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002386 ViewportType::INTERNAL);
Arthur Hungaab25622020-01-16 11:22:11 +08002387
2388 mDevice = createUinputDevice<UinputTouchScreen>(Rect(0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT));
2389 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2390 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2391 }
2392
2393 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
2394 int32_t orientation, const std::string& uniqueId,
2395 std::optional<uint8_t> physicalPort,
2396 ViewportType viewportType) {
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002397 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, true /*isActive*/,
2398 uniqueId, physicalPort, viewportType);
Arthur Hungaab25622020-01-16 11:22:11 +08002399 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2400 }
2401
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002402 void assertReceivedMotion(int32_t action, const std::vector<Point>& points) {
2403 NotifyMotionArgs args;
2404 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2405 EXPECT_EQ(action, args.action);
2406 ASSERT_EQ(points.size(), args.pointerCount);
2407 for (size_t i = 0; i < args.pointerCount; i++) {
2408 EXPECT_EQ(points[i].x, args.pointerCoords[i].getX());
2409 EXPECT_EQ(points[i].y, args.pointerCoords[i].getY());
2410 }
2411 }
2412
Arthur Hungaab25622020-01-16 11:22:11 +08002413 std::unique_ptr<UinputTouchScreen> mDevice;
2414};
2415
2416TEST_F(TouchIntegrationTest, InputEvent_ProcessSingleTouch) {
2417 NotifyMotionArgs args;
2418 const Point centerPoint = mDevice->getCenterPoint();
2419
2420 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00002421 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002422 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002423 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002424 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2425 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2426
2427 // ACTION_MOVE
2428 mDevice->sendMove(centerPoint + Point(1, 1));
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002429 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002430 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2431 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2432
2433 // ACTION_UP
2434 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002435 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002436 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2437 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2438}
2439
2440TEST_F(TouchIntegrationTest, InputEvent_ProcessMultiTouch) {
2441 NotifyMotionArgs args;
2442 const Point centerPoint = mDevice->getCenterPoint();
2443
2444 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00002445 mDevice->sendSlot(FIRST_SLOT);
2446 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002447 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002448 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002449 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2450 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2451
2452 // ACTION_POINTER_DOWN (Second slot)
2453 const Point secondPoint = centerPoint + Point(100, 100);
2454 mDevice->sendSlot(SECOND_SLOT);
2455 mDevice->sendTrackingId(SECOND_TRACKING_ID);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002456 mDevice->sendDown(secondPoint);
2457 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002458 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08002459 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002460
2461 // ACTION_MOVE (Second slot)
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002462 mDevice->sendMove(secondPoint + Point(1, 1));
2463 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002464 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2465 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2466
2467 // ACTION_POINTER_UP (Second slot)
arthurhungcc7f9802020-04-30 17:55:40 +08002468 mDevice->sendPointerUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002469 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002470 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08002471 ASSERT_EQ(ACTION_POINTER_1_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002472
2473 // ACTION_UP
2474 mDevice->sendSlot(FIRST_SLOT);
2475 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002476 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002477 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2478 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2479}
2480
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002481/**
2482 * What happens when a pointer goes up while another pointer moves in the same frame? Are POINTER_UP
2483 * events guaranteed to contain the same data as a preceding MOVE, or can they contain different
2484 * data?
2485 * In this test, we try to send a change in coordinates in Pointer 0 in the same frame as the
2486 * liftoff of Pointer 1. We check that POINTER_UP event is generated first, and the MOVE event
2487 * for Pointer 0 only is generated after.
2488 * Suppose we are only interested in learning the movement of Pointer 0. If we only observe MOVE
2489 * events, we will not miss any information.
2490 * Even though the Pointer 1 up event contains updated Pointer 0 coordinates, there is another MOVE
2491 * event generated afterwards that contains the newest movement of pointer 0.
2492 * This is important for palm rejection. If there is a subsequent InputListener stage that detects
2493 * palms, and wants to cancel Pointer 1, then it is safe to simply drop POINTER_1_UP event without
2494 * losing information about non-palm pointers.
2495 */
2496TEST_F(TouchIntegrationTest, MultiTouch_PointerMoveAndSecondPointerUp) {
2497 NotifyMotionArgs args;
2498 const Point centerPoint = mDevice->getCenterPoint();
2499
2500 // ACTION_DOWN
2501 mDevice->sendSlot(FIRST_SLOT);
2502 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2503 mDevice->sendDown(centerPoint);
2504 mDevice->sendSync();
2505 assertReceivedMotion(AMOTION_EVENT_ACTION_DOWN, {centerPoint});
2506
2507 // ACTION_POINTER_DOWN (Second slot)
2508 const Point secondPoint = centerPoint + Point(100, 100);
2509 mDevice->sendSlot(SECOND_SLOT);
2510 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2511 mDevice->sendDown(secondPoint);
2512 mDevice->sendSync();
2513 assertReceivedMotion(ACTION_POINTER_1_DOWN, {centerPoint, secondPoint});
2514
2515 // ACTION_MOVE (First slot)
2516 mDevice->sendSlot(FIRST_SLOT);
2517 mDevice->sendMove(centerPoint + Point(5, 5));
2518 // ACTION_POINTER_UP (Second slot)
2519 mDevice->sendSlot(SECOND_SLOT);
2520 mDevice->sendPointerUp();
2521 // Send a single sync for the above 2 pointer updates
2522 mDevice->sendSync();
2523
2524 // First, we should get POINTER_UP for the second pointer
2525 assertReceivedMotion(ACTION_POINTER_1_UP,
2526 {/*first pointer */ centerPoint + Point(5, 5),
2527 /*second pointer*/ secondPoint});
2528
2529 // Next, the MOVE event for the first pointer
2530 assertReceivedMotion(AMOTION_EVENT_ACTION_MOVE, {centerPoint + Point(5, 5)});
2531}
2532
2533/**
2534 * Similar scenario as above. The difference is that when the second pointer goes up, it will first
2535 * move, and then it will go up, all in the same frame.
2536 * In this scenario, the movement of the second pointer just prior to liftoff is ignored, and never
2537 * gets sent to the listener.
2538 */
2539TEST_F(TouchIntegrationTest, MultiTouch_PointerMoveAndSecondPointerMoveAndUp) {
2540 NotifyMotionArgs args;
2541 const Point centerPoint = mDevice->getCenterPoint();
2542
2543 // ACTION_DOWN
2544 mDevice->sendSlot(FIRST_SLOT);
2545 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2546 mDevice->sendDown(centerPoint);
2547 mDevice->sendSync();
2548 assertReceivedMotion(AMOTION_EVENT_ACTION_DOWN, {centerPoint});
2549
2550 // ACTION_POINTER_DOWN (Second slot)
2551 const Point secondPoint = centerPoint + Point(100, 100);
2552 mDevice->sendSlot(SECOND_SLOT);
2553 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2554 mDevice->sendDown(secondPoint);
2555 mDevice->sendSync();
2556 assertReceivedMotion(ACTION_POINTER_1_DOWN, {centerPoint, secondPoint});
2557
2558 // ACTION_MOVE (First slot)
2559 mDevice->sendSlot(FIRST_SLOT);
2560 mDevice->sendMove(centerPoint + Point(5, 5));
2561 // ACTION_POINTER_UP (Second slot)
2562 mDevice->sendSlot(SECOND_SLOT);
2563 mDevice->sendMove(secondPoint + Point(6, 6));
2564 mDevice->sendPointerUp();
2565 // Send a single sync for the above 2 pointer updates
2566 mDevice->sendSync();
2567
2568 // First, we should get POINTER_UP for the second pointer
2569 // The movement of the second pointer during the liftoff frame is ignored.
2570 // The coordinates 'secondPoint + Point(6, 6)' are never sent to the listener.
2571 assertReceivedMotion(ACTION_POINTER_1_UP,
2572 {/*first pointer */ centerPoint + Point(5, 5),
2573 /*second pointer*/ secondPoint});
2574
2575 // Next, the MOVE event for the first pointer
2576 assertReceivedMotion(AMOTION_EVENT_ACTION_MOVE, {centerPoint + Point(5, 5)});
2577}
2578
Arthur Hungaab25622020-01-16 11:22:11 +08002579TEST_F(TouchIntegrationTest, InputEvent_ProcessPalm) {
2580 NotifyMotionArgs args;
2581 const Point centerPoint = mDevice->getCenterPoint();
2582
2583 // ACTION_DOWN
arthurhungcc7f9802020-04-30 17:55:40 +08002584 mDevice->sendSlot(FIRST_SLOT);
2585 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002586 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002587 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002588 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2589 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2590
arthurhungcc7f9802020-04-30 17:55:40 +08002591 // ACTION_POINTER_DOWN (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002592 const Point secondPoint = centerPoint + Point(100, 100);
2593 mDevice->sendSlot(SECOND_SLOT);
2594 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2595 mDevice->sendDown(secondPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002596 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002597 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08002598 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002599
arthurhungcc7f9802020-04-30 17:55:40 +08002600 // ACTION_MOVE (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002601 mDevice->sendMove(secondPoint + Point(1, 1));
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002602 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002603 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2604 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2605
arthurhungcc7f9802020-04-30 17:55:40 +08002606 // Send MT_TOOL_PALM (second slot), which indicates that the touch IC has determined this to be
2607 // a palm event.
2608 // Expect to receive the ACTION_POINTER_UP with cancel flag.
Arthur Hungaab25622020-01-16 11:22:11 +08002609 mDevice->sendToolType(MT_TOOL_PALM);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002610 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002611 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08002612 ASSERT_EQ(ACTION_POINTER_1_UP, args.action);
arthurhungcc7f9802020-04-30 17:55:40 +08002613 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, args.flags);
Arthur Hungaab25622020-01-16 11:22:11 +08002614
arthurhungcc7f9802020-04-30 17:55:40 +08002615 // Send up to second slot, expect first slot send moving.
2616 mDevice->sendPointerUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002617 mDevice->sendSync();
arthurhungcc7f9802020-04-30 17:55:40 +08002618 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2619 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002620
arthurhungcc7f9802020-04-30 17:55:40 +08002621 // Send ACTION_UP (first slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002622 mDevice->sendSlot(FIRST_SLOT);
2623 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002624 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002625
arthurhungcc7f9802020-04-30 17:55:40 +08002626 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2627 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002628}
2629
Michael Wrightd02c5b62014-02-10 15:10:22 -08002630// --- InputDeviceTest ---
Michael Wrightd02c5b62014-02-10 15:10:22 -08002631class InputDeviceTest : public testing::Test {
2632protected:
2633 static const char* DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002634 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002635 static const int32_t DEVICE_ID;
2636 static const int32_t DEVICE_GENERATION;
2637 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002638 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002639 static const int32_t EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002640
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002641 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002642 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002643 std::unique_ptr<TestInputListener> mFakeListener;
arthurhungdcef2dc2020-08-11 14:47:50 +08002644 std::unique_ptr<InstrumentedInputReader> mReader;
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00002645 std::shared_ptr<InputDevice> mDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002646
Chris Yea52ade12020-08-27 16:49:20 -07002647 void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002648 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002649 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002650 mFakeListener = std::make_unique<TestInputListener>();
arthurhungdcef2dc2020-08-11 14:47:50 +08002651 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002652 *mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002653 InputDeviceIdentifier identifier;
2654 identifier.name = DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002655 identifier.location = DEVICE_LOCATION;
arthurhungdcef2dc2020-08-11 14:47:50 +08002656 mDevice = std::make_shared<InputDevice>(mReader->getContext(), DEVICE_ID, DEVICE_GENERATION,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002657 identifier);
arthurhungdcef2dc2020-08-11 14:47:50 +08002658 mReader->pushNextDevice(mDevice);
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002659 mFakeEventHub->addDevice(EVENTHUB_ID, DEVICE_NAME, ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08002660 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002661 }
2662
Chris Yea52ade12020-08-27 16:49:20 -07002663 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002664 mFakeListener.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002665 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002666 }
2667};
2668
2669const char* InputDeviceTest::DEVICE_NAME = "device";
Arthur Hung2c9a3342019-07-23 14:18:59 +08002670const char* InputDeviceTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002671const int32_t InputDeviceTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002672const int32_t InputDeviceTest::DEVICE_GENERATION = 2;
2673const int32_t InputDeviceTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002674const ftl::Flags<InputDeviceClass> InputDeviceTest::DEVICE_CLASSES =
Chris Ye1b0c7342020-07-28 21:57:03 -07002675 InputDeviceClass::KEYBOARD | InputDeviceClass::TOUCH | InputDeviceClass::JOYSTICK;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002676const int32_t InputDeviceTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002677
2678TEST_F(InputDeviceTest, ImmutableProperties) {
2679 ASSERT_EQ(DEVICE_ID, mDevice->getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002680 ASSERT_STREQ(DEVICE_NAME, mDevice->getName().c_str());
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002681 ASSERT_EQ(ftl::Flags<InputDeviceClass>(0), mDevice->getClasses());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002682}
2683
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002684TEST_F(InputDeviceTest, WhenDeviceCreated_EnabledIsFalse) {
2685 ASSERT_EQ(mDevice->isEnabled(), false);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07002686}
2687
Michael Wrightd02c5b62014-02-10 15:10:22 -08002688TEST_F(InputDeviceTest, WhenNoMappersAreRegistered_DeviceIsIgnored) {
2689 // Configuration.
2690 InputReaderConfiguration config;
2691 mDevice->configure(ARBITRARY_TIME, &config, 0);
2692
2693 // Reset.
2694 mDevice->reset(ARBITRARY_TIME);
2695
2696 NotifyDeviceResetArgs resetArgs;
2697 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2698 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2699 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2700
2701 // Metadata.
2702 ASSERT_TRUE(mDevice->isIgnored());
2703 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mDevice->getSources());
2704
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002705 InputDeviceInfo info = mDevice->getDeviceInfo();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002706 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002707 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002708 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NONE, info.getKeyboardType());
2709 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, info.getSources());
2710
2711 // State queries.
2712 ASSERT_EQ(0, mDevice->getMetaState());
2713
2714 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2715 << "Ignored device should return unknown key code state.";
2716 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2717 << "Ignored device should return unknown scan code state.";
2718 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 0))
2719 << "Ignored device should return unknown switch state.";
2720
2721 const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
2722 uint8_t flags[2] = { 0, 1 };
2723 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, 2, keyCodes, flags))
2724 << "Ignored device should never mark any key codes.";
2725 ASSERT_EQ(0, flags[0]) << "Flag for unsupported key should be unchanged.";
2726 ASSERT_EQ(1, flags[1]) << "Flag for unsupported key should be unchanged.";
2727}
2728
2729TEST_F(InputDeviceTest, WhenMappersAreRegistered_DeviceIsNotIgnoredAndForwardsRequestsToMappers) {
2730 // Configuration.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002731 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, String8("key"), String8("value"));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002732
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002733 FakeInputMapper& mapper1 =
2734 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002735 mapper1.setKeyboardType(AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2736 mapper1.setMetaState(AMETA_ALT_ON);
2737 mapper1.addSupportedKeyCode(AKEYCODE_A);
2738 mapper1.addSupportedKeyCode(AKEYCODE_B);
2739 mapper1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
2740 mapper1.setKeyCodeState(AKEYCODE_B, AKEY_STATE_UP);
2741 mapper1.setScanCodeState(2, AKEY_STATE_DOWN);
2742 mapper1.setScanCodeState(3, AKEY_STATE_UP);
2743 mapper1.setSwitchState(4, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002744
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002745 FakeInputMapper& mapper2 =
2746 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002747 mapper2.setMetaState(AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002748
2749 InputReaderConfiguration config;
2750 mDevice->configure(ARBITRARY_TIME, &config, 0);
2751
2752 String8 propertyValue;
2753 ASSERT_TRUE(mDevice->getConfiguration().tryGetProperty(String8("key"), propertyValue))
2754 << "Device should have read configuration during configuration phase.";
2755 ASSERT_STREQ("value", propertyValue.string());
2756
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002757 ASSERT_NO_FATAL_FAILURE(mapper1.assertConfigureWasCalled());
2758 ASSERT_NO_FATAL_FAILURE(mapper2.assertConfigureWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002759
2760 // Reset
2761 mDevice->reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002762 ASSERT_NO_FATAL_FAILURE(mapper1.assertResetWasCalled());
2763 ASSERT_NO_FATAL_FAILURE(mapper2.assertResetWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002764
2765 NotifyDeviceResetArgs resetArgs;
2766 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2767 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2768 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2769
2770 // Metadata.
2771 ASSERT_FALSE(mDevice->isIgnored());
2772 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), mDevice->getSources());
2773
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002774 InputDeviceInfo info = mDevice->getDeviceInfo();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002775 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002776 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002777 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_ALPHABETIC, info.getKeyboardType());
2778 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), info.getSources());
2779
2780 // State queries.
2781 ASSERT_EQ(AMETA_ALT_ON | AMETA_SHIFT_ON, mDevice->getMetaState())
2782 << "Should query mappers and combine meta states.";
2783
2784 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2785 << "Should return unknown key code state when source not supported.";
2786 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2787 << "Should return unknown scan code state when source not supported.";
2788 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2789 << "Should return unknown switch state when source not supported.";
2790
2791 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, AKEYCODE_A))
2792 << "Should query mapper when source is supported.";
2793 ASSERT_EQ(AKEY_STATE_UP, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 3))
2794 << "Should query mapper when source is supported.";
2795 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 4))
2796 << "Should query mapper when source is supported.";
2797
2798 const int32_t keyCodes[4] = { AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2 };
2799 uint8_t flags[4] = { 0, 0, 0, 1 };
2800 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
2801 << "Should do nothing when source is unsupported.";
2802 ASSERT_EQ(0, flags[0]) << "Flag should be unchanged when source is unsupported.";
2803 ASSERT_EQ(0, flags[1]) << "Flag should be unchanged when source is unsupported.";
2804 ASSERT_EQ(0, flags[2]) << "Flag should be unchanged when source is unsupported.";
2805 ASSERT_EQ(1, flags[3]) << "Flag should be unchanged when source is unsupported.";
2806
2807 ASSERT_TRUE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, 4, keyCodes, flags))
2808 << "Should query mapper when source is supported.";
2809 ASSERT_EQ(1, flags[0]) << "Flag for supported key should be set.";
2810 ASSERT_EQ(1, flags[1]) << "Flag for supported key should be set.";
2811 ASSERT_EQ(0, flags[2]) << "Flag for unsupported key should be unchanged.";
2812 ASSERT_EQ(1, flags[3]) << "Flag for unsupported key should be unchanged.";
2813
2814 // Event handling.
2815 RawEvent event;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002816 event.deviceId = EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002817 mDevice->process(&event, 1);
2818
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002819 ASSERT_NO_FATAL_FAILURE(mapper1.assertProcessWasCalled());
2820 ASSERT_NO_FATAL_FAILURE(mapper2.assertProcessWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002821}
2822
Arthur Hung2c9a3342019-07-23 14:18:59 +08002823// A single input device is associated with a specific display. Check that:
2824// 1. Device is disabled if the viewport corresponding to the associated display is not found
2825// 2. Device is disabled when setEnabled API is called
2826TEST_F(InputDeviceTest, Configure_AssignsDisplayPort) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002827 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002828
2829 // First Configuration.
2830 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
2831
2832 // Device should be enabled by default.
2833 ASSERT_TRUE(mDevice->isEnabled());
2834
2835 // Prepare associated info.
2836 constexpr uint8_t hdmi = 1;
2837 const std::string UNIQUE_ID = "local:1";
2838
2839 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi);
2840 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2841 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2842 // Device should be disabled because it is associated with a specific display via
2843 // input port <-> display port association, but the corresponding display is not found
2844 ASSERT_FALSE(mDevice->isEnabled());
2845
2846 // Prepare displays.
2847 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002848 DISPLAY_ORIENTATION_0, true /*isActive*/, UNIQUE_ID, hdmi,
2849 ViewportType::INTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002850 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2851 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2852 ASSERT_TRUE(mDevice->isEnabled());
2853
2854 // Device should be disabled after set disable.
2855 mFakePolicy->addDisabledDevice(mDevice->getId());
2856 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2857 InputReaderConfiguration::CHANGE_ENABLED_STATE);
2858 ASSERT_FALSE(mDevice->isEnabled());
2859
2860 // Device should still be disabled even found the associated display.
2861 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2862 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2863 ASSERT_FALSE(mDevice->isEnabled());
2864}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002865
Christine Franks1ba71cc2021-04-07 14:37:42 -07002866TEST_F(InputDeviceTest, Configure_AssignsDisplayUniqueId) {
2867 // Device should be enabled by default.
2868 mFakePolicy->clearViewports();
2869 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
2870 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
2871 ASSERT_TRUE(mDevice->isEnabled());
2872
2873 // Device should be disabled because it is associated with a specific display, but the
2874 // corresponding display is not found.
2875 const std::string DISPLAY_UNIQUE_ID = "displayUniqueId";
Christine Franks2a2293c2022-01-18 11:51:16 -08002876 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002877 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2878 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2879 ASSERT_FALSE(mDevice->isEnabled());
2880
2881 // Device should be enabled when a display is found.
2882 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2883 DISPLAY_ORIENTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
2884 NO_PORT, ViewportType::INTERNAL);
2885 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2886 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2887 ASSERT_TRUE(mDevice->isEnabled());
2888
2889 // Device should be disabled after set disable.
2890 mFakePolicy->addDisabledDevice(mDevice->getId());
2891 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2892 InputReaderConfiguration::CHANGE_ENABLED_STATE);
2893 ASSERT_FALSE(mDevice->isEnabled());
2894
2895 // Device should still be disabled even found the associated display.
2896 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2897 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2898 ASSERT_FALSE(mDevice->isEnabled());
2899}
2900
Christine Franks2a2293c2022-01-18 11:51:16 -08002901TEST_F(InputDeviceTest, Configure_UniqueId_CorrectlyMatches) {
2902 mFakePolicy->clearViewports();
2903 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
2904 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
2905
2906 const std::string DISPLAY_UNIQUE_ID = "displayUniqueId";
2907 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
2908 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2909 DISPLAY_ORIENTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
2910 NO_PORT, ViewportType::INTERNAL);
2911 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2912 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2913 ASSERT_EQ(DISPLAY_UNIQUE_ID, mDevice->getAssociatedDisplayUniqueId());
2914}
2915
Michael Wrightd02c5b62014-02-10 15:10:22 -08002916// --- InputMapperTest ---
2917
2918class InputMapperTest : public testing::Test {
2919protected:
2920 static const char* DEVICE_NAME;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002921 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002922 static const int32_t DEVICE_ID;
2923 static const int32_t DEVICE_GENERATION;
2924 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002925 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002926 static const int32_t EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002927
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002928 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002929 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002930 std::unique_ptr<TestInputListener> mFakeListener;
arthurhungdcef2dc2020-08-11 14:47:50 +08002931 std::unique_ptr<InstrumentedInputReader> mReader;
2932 std::shared_ptr<InputDevice> mDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002933
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002934 virtual void SetUp(ftl::Flags<InputDeviceClass> classes) {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002935 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002936 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002937 mFakeListener = std::make_unique<TestInputListener>();
arthurhungdcef2dc2020-08-11 14:47:50 +08002938 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002939 *mFakeListener);
arthurhungdcef2dc2020-08-11 14:47:50 +08002940 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002941 }
2942
Prabir Pradhanc14266f2021-05-12 15:56:24 -07002943 void SetUp() override {
Prabir Pradhanc14266f2021-05-12 15:56:24 -07002944 SetUp(DEVICE_CLASSES);
2945 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002946
Chris Yea52ade12020-08-27 16:49:20 -07002947 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002948 mFakeListener.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002949 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002950 }
2951
2952 void addConfigurationProperty(const char* key, const char* value) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002953 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, String8(key), String8(value));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002954 }
2955
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002956 void configureDevice(uint32_t changes) {
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00002957 if (!changes ||
2958 (changes &
2959 (InputReaderConfiguration::CHANGE_DISPLAY_INFO |
2960 InputReaderConfiguration::CHANGE_POINTER_CAPTURE))) {
arthurhungdcef2dc2020-08-11 14:47:50 +08002961 mReader->requestRefreshConfiguration(changes);
2962 mReader->loopOnce();
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -08002963 }
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002964 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
2965 }
2966
arthurhungdcef2dc2020-08-11 14:47:50 +08002967 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
2968 const std::string& location, int32_t eventHubId,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002969 ftl::Flags<InputDeviceClass> classes) {
arthurhungdcef2dc2020-08-11 14:47:50 +08002970 InputDeviceIdentifier identifier;
2971 identifier.name = name;
2972 identifier.location = location;
2973 std::shared_ptr<InputDevice> device =
2974 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
2975 identifier);
2976 mReader->pushNextDevice(device);
2977 mFakeEventHub->addDevice(eventHubId, name, classes);
2978 mReader->loopOnce();
2979 return device;
2980 }
2981
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002982 template <class T, typename... Args>
2983 T& addMapperAndConfigure(Args... args) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002984 T& mapper = mDevice->addMapper<T>(EVENTHUB_ID, args...);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002985 configureDevice(0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002986 mDevice->reset(ARBITRARY_TIME);
Chris Ye42b06822020-08-07 11:39:33 -07002987 mapper.reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002988 return mapper;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002989 }
2990
2991 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002992 int32_t orientation, const std::string& uniqueId,
2993 std::optional<uint8_t> physicalPort, ViewportType viewportType) {
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002994 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, true /*isActive*/,
2995 uniqueId, physicalPort, viewportType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07002996 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2997 }
2998
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002999 void clearViewports() {
3000 mFakePolicy->clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003001 }
3002
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003003 void process(InputMapper& mapper, nsecs_t when, nsecs_t readTime, int32_t type, int32_t code,
3004 int32_t value) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003005 RawEvent event;
3006 event.when = when;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003007 event.readTime = readTime;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003008 event.deviceId = mapper.getDeviceContext().getEventHubId();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003009 event.type = type;
3010 event.code = code;
3011 event.value = value;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003012 mapper.process(&event);
arthurhungdcef2dc2020-08-11 14:47:50 +08003013 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003014 }
3015
3016 static void assertMotionRange(const InputDeviceInfo& info,
3017 int32_t axis, uint32_t source, float min, float max, float flat, float fuzz) {
3018 const InputDeviceInfo::MotionRange* range = info.getMotionRange(axis, source);
Yi Kong9b14ac62018-07-17 13:48:38 -07003019 ASSERT_TRUE(range != nullptr) << "Axis: " << axis << " Source: " << source;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003020 ASSERT_EQ(axis, range->axis) << "Axis: " << axis << " Source: " << source;
3021 ASSERT_EQ(source, range->source) << "Axis: " << axis << " Source: " << source;
3022 ASSERT_NEAR(min, range->min, EPSILON) << "Axis: " << axis << " Source: " << source;
3023 ASSERT_NEAR(max, range->max, EPSILON) << "Axis: " << axis << " Source: " << source;
3024 ASSERT_NEAR(flat, range->flat, EPSILON) << "Axis: " << axis << " Source: " << source;
3025 ASSERT_NEAR(fuzz, range->fuzz, EPSILON) << "Axis: " << axis << " Source: " << source;
3026 }
3027
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003028 static void assertPointerCoords(const PointerCoords& coords, float x, float y, float pressure,
3029 float size, float touchMajor, float touchMinor, float toolMajor,
3030 float toolMinor, float orientation, float distance,
3031 float scaledAxisEpsilon = 1.f) {
3032 ASSERT_NEAR(x, coords.getAxisValue(AMOTION_EVENT_AXIS_X), scaledAxisEpsilon);
3033 ASSERT_NEAR(y, coords.getAxisValue(AMOTION_EVENT_AXIS_Y), scaledAxisEpsilon);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003034 ASSERT_NEAR(pressure, coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), EPSILON);
3035 ASSERT_NEAR(size, coords.getAxisValue(AMOTION_EVENT_AXIS_SIZE), EPSILON);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003036 ASSERT_NEAR(touchMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR),
3037 scaledAxisEpsilon);
3038 ASSERT_NEAR(touchMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR),
3039 scaledAxisEpsilon);
3040 ASSERT_NEAR(toolMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR),
3041 scaledAxisEpsilon);
3042 ASSERT_NEAR(toolMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR),
3043 scaledAxisEpsilon);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003044 ASSERT_NEAR(orientation, coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION), EPSILON);
3045 ASSERT_NEAR(distance, coords.getAxisValue(AMOTION_EVENT_AXIS_DISTANCE), EPSILON);
3046 }
3047
Michael Wright17db18e2020-06-26 20:51:44 +01003048 static void assertPosition(const FakePointerController& controller, float x, float y) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003049 float actualX, actualY;
Michael Wright17db18e2020-06-26 20:51:44 +01003050 controller.getPosition(&actualX, &actualY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003051 ASSERT_NEAR(x, actualX, 1);
3052 ASSERT_NEAR(y, actualY, 1);
3053 }
3054};
3055
3056const char* InputMapperTest::DEVICE_NAME = "device";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003057const char* InputMapperTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003058const int32_t InputMapperTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003059const int32_t InputMapperTest::DEVICE_GENERATION = 2;
3060const int32_t InputMapperTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003061const ftl::Flags<InputDeviceClass> InputMapperTest::DEVICE_CLASSES =
3062 ftl::Flags<InputDeviceClass>(0); // not needed for current tests
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003063const int32_t InputMapperTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003064
3065// --- SwitchInputMapperTest ---
3066
3067class SwitchInputMapperTest : public InputMapperTest {
3068protected:
3069};
3070
3071TEST_F(SwitchInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003072 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003073
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003074 ASSERT_EQ(uint32_t(AINPUT_SOURCE_SWITCH), mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003075}
3076
3077TEST_F(SwitchInputMapperTest, GetSwitchState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003078 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003079
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003080 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003081 ASSERT_EQ(1, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003082
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003083 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003084 ASSERT_EQ(0, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003085}
3086
3087TEST_F(SwitchInputMapperTest, Process) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003088 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003089
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003090 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_LID, 1);
3091 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_JACK_PHYSICAL_INSERT, 1);
3092 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_HEADPHONE_INSERT, 0);
3093 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003094
3095 NotifySwitchArgs args;
3096 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySwitchWasCalled(&args));
3097 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
Dan Albert1bd2fc02016-02-02 15:11:57 -08003098 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT), args.switchValues);
3099 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT) | (1 << SW_HEADPHONE_INSERT),
Michael Wrightd02c5b62014-02-10 15:10:22 -08003100 args.switchMask);
3101 ASSERT_EQ(uint32_t(0), args.policyFlags);
3102}
3103
Chris Ye87143712020-11-10 05:05:58 +00003104// --- VibratorInputMapperTest ---
3105class VibratorInputMapperTest : public InputMapperTest {
3106protected:
3107 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::VIBRATOR); }
3108};
3109
3110TEST_F(VibratorInputMapperTest, GetSources) {
3111 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
3112
3113 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mapper.getSources());
3114}
3115
3116TEST_F(VibratorInputMapperTest, GetVibratorIds) {
3117 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
3118
3119 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
3120}
3121
3122TEST_F(VibratorInputMapperTest, Vibrate) {
3123 constexpr uint8_t DEFAULT_AMPLITUDE = 192;
Chris Yefb552902021-02-03 17:18:37 -08003124 constexpr int32_t VIBRATION_TOKEN = 100;
Chris Ye87143712020-11-10 05:05:58 +00003125 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
3126
3127 VibrationElement pattern(2);
3128 VibrationSequence sequence(2);
3129 pattern.duration = std::chrono::milliseconds(200);
3130 pattern.channels = {{0 /* vibratorId */, DEFAULT_AMPLITUDE / 2},
3131 {1 /* vibratorId */, DEFAULT_AMPLITUDE}};
3132 sequence.addElement(pattern);
3133 pattern.duration = std::chrono::milliseconds(500);
3134 pattern.channels = {{0 /* vibratorId */, DEFAULT_AMPLITUDE / 4},
3135 {1 /* vibratorId */, DEFAULT_AMPLITUDE}};
3136 sequence.addElement(pattern);
3137
3138 std::vector<int64_t> timings = {0, 1};
3139 std::vector<uint8_t> amplitudes = {DEFAULT_AMPLITUDE, DEFAULT_AMPLITUDE / 2};
3140
3141 ASSERT_FALSE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08003142 // Start vibrating
3143 mapper.vibrate(sequence, -1 /* repeat */, VIBRATION_TOKEN);
Chris Ye87143712020-11-10 05:05:58 +00003144 ASSERT_TRUE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08003145 // Verify vibrator state listener was notified.
3146 mReader->loopOnce();
3147 NotifyVibratorStateArgs args;
3148 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyVibratorStateWasCalled(&args));
3149 ASSERT_EQ(DEVICE_ID, args.deviceId);
3150 ASSERT_TRUE(args.isOn);
3151 // Stop vibrating
3152 mapper.cancelVibrate(VIBRATION_TOKEN);
3153 ASSERT_FALSE(mapper.isVibrating());
3154 // Verify vibrator state listener was notified.
3155 mReader->loopOnce();
3156 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyVibratorStateWasCalled(&args));
3157 ASSERT_EQ(DEVICE_ID, args.deviceId);
3158 ASSERT_FALSE(args.isOn);
Chris Ye87143712020-11-10 05:05:58 +00003159}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003160
Chris Yef59a2f42020-10-16 12:55:26 -07003161// --- SensorInputMapperTest ---
3162
3163class SensorInputMapperTest : public InputMapperTest {
3164protected:
3165 static const int32_t ACCEL_RAW_MIN;
3166 static const int32_t ACCEL_RAW_MAX;
3167 static const int32_t ACCEL_RAW_FUZZ;
3168 static const int32_t ACCEL_RAW_FLAT;
3169 static const int32_t ACCEL_RAW_RESOLUTION;
3170
3171 static const int32_t GYRO_RAW_MIN;
3172 static const int32_t GYRO_RAW_MAX;
3173 static const int32_t GYRO_RAW_FUZZ;
3174 static const int32_t GYRO_RAW_FLAT;
3175 static const int32_t GYRO_RAW_RESOLUTION;
3176
3177 static const float GRAVITY_MS2_UNIT;
3178 static const float DEGREE_RADIAN_UNIT;
3179
3180 void prepareAccelAxes();
3181 void prepareGyroAxes();
3182 void setAccelProperties();
3183 void setGyroProperties();
3184 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::SENSOR); }
3185};
3186
3187const int32_t SensorInputMapperTest::ACCEL_RAW_MIN = -32768;
3188const int32_t SensorInputMapperTest::ACCEL_RAW_MAX = 32768;
3189const int32_t SensorInputMapperTest::ACCEL_RAW_FUZZ = 16;
3190const int32_t SensorInputMapperTest::ACCEL_RAW_FLAT = 0;
3191const int32_t SensorInputMapperTest::ACCEL_RAW_RESOLUTION = 8192;
3192
3193const int32_t SensorInputMapperTest::GYRO_RAW_MIN = -2097152;
3194const int32_t SensorInputMapperTest::GYRO_RAW_MAX = 2097152;
3195const int32_t SensorInputMapperTest::GYRO_RAW_FUZZ = 16;
3196const int32_t SensorInputMapperTest::GYRO_RAW_FLAT = 0;
3197const int32_t SensorInputMapperTest::GYRO_RAW_RESOLUTION = 1024;
3198
3199const float SensorInputMapperTest::GRAVITY_MS2_UNIT = 9.80665f;
3200const float SensorInputMapperTest::DEGREE_RADIAN_UNIT = 0.0174533f;
3201
3202void SensorInputMapperTest::prepareAccelAxes() {
3203 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3204 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3205 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3206 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3207 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Z, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3208 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3209}
3210
3211void SensorInputMapperTest::prepareGyroAxes() {
3212 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RX, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3213 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3214 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RY, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3215 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3216 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RZ, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3217 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3218}
3219
3220void SensorInputMapperTest::setAccelProperties() {
3221 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 0, InputDeviceSensorType::ACCELEROMETER,
3222 /* sensorDataIndex */ 0);
3223 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 1, InputDeviceSensorType::ACCELEROMETER,
3224 /* sensorDataIndex */ 1);
3225 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 2, InputDeviceSensorType::ACCELEROMETER,
3226 /* sensorDataIndex */ 2);
3227 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
3228 addConfigurationProperty("sensor.accelerometer.reportingMode", "0");
3229 addConfigurationProperty("sensor.accelerometer.maxDelay", "100000");
3230 addConfigurationProperty("sensor.accelerometer.minDelay", "5000");
3231 addConfigurationProperty("sensor.accelerometer.power", "1.5");
3232}
3233
3234void SensorInputMapperTest::setGyroProperties() {
3235 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 3, InputDeviceSensorType::GYROSCOPE,
3236 /* sensorDataIndex */ 0);
3237 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 4, InputDeviceSensorType::GYROSCOPE,
3238 /* sensorDataIndex */ 1);
3239 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 5, InputDeviceSensorType::GYROSCOPE,
3240 /* sensorDataIndex */ 2);
3241 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
3242 addConfigurationProperty("sensor.gyroscope.reportingMode", "0");
3243 addConfigurationProperty("sensor.gyroscope.maxDelay", "100000");
3244 addConfigurationProperty("sensor.gyroscope.minDelay", "5000");
3245 addConfigurationProperty("sensor.gyroscope.power", "0.8");
3246}
3247
3248TEST_F(SensorInputMapperTest, GetSources) {
3249 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3250
3251 ASSERT_EQ(static_cast<uint32_t>(AINPUT_SOURCE_SENSOR), mapper.getSources());
3252}
3253
3254TEST_F(SensorInputMapperTest, ProcessAccelerometerSensor) {
3255 setAccelProperties();
3256 prepareAccelAxes();
3257 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3258
3259 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::ACCELEROMETER,
3260 std::chrono::microseconds(10000),
3261 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08003262 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003263 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, 20000);
3264 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, -20000);
3265 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Z, 40000);
3266 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
3267 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07003268
3269 NotifySensorArgs args;
3270 std::vector<float> values = {20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
3271 -20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
3272 40000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT};
3273
3274 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
3275 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
3276 ASSERT_EQ(args.deviceId, DEVICE_ID);
3277 ASSERT_EQ(args.sensorType, InputDeviceSensorType::ACCELEROMETER);
3278 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
3279 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
3280 ASSERT_EQ(args.values, values);
3281 mapper.flushSensor(InputDeviceSensorType::ACCELEROMETER);
3282}
3283
3284TEST_F(SensorInputMapperTest, ProcessGyroscopeSensor) {
3285 setGyroProperties();
3286 prepareGyroAxes();
3287 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3288
3289 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::GYROSCOPE,
3290 std::chrono::microseconds(10000),
3291 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08003292 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003293 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RX, 20000);
3294 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RY, -20000);
3295 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RZ, 40000);
3296 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
3297 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07003298
3299 NotifySensorArgs args;
3300 std::vector<float> values = {20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
3301 -20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
3302 40000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT};
3303
3304 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
3305 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
3306 ASSERT_EQ(args.deviceId, DEVICE_ID);
3307 ASSERT_EQ(args.sensorType, InputDeviceSensorType::GYROSCOPE);
3308 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
3309 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
3310 ASSERT_EQ(args.values, values);
3311 mapper.flushSensor(InputDeviceSensorType::GYROSCOPE);
3312}
3313
Michael Wrightd02c5b62014-02-10 15:10:22 -08003314// --- KeyboardInputMapperTest ---
3315
3316class KeyboardInputMapperTest : public InputMapperTest {
3317protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003318 const std::string UNIQUE_ID = "local:0";
3319
3320 void prepareDisplay(int32_t orientation);
3321
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003322 void testDPadKeyRotation(KeyboardInputMapper& mapper, int32_t originalScanCode,
Arthur Hung2c9a3342019-07-23 14:18:59 +08003323 int32_t originalKeyCode, int32_t rotatedKeyCode,
3324 int32_t displayId = ADISPLAY_ID_NONE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003325};
3326
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003327/* Similar to setDisplayInfoAndReconfigure, but pre-populates all parameters except for the
3328 * orientation.
3329 */
3330void KeyboardInputMapperTest::prepareDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003331 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
3332 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003333}
3334
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003335void KeyboardInputMapperTest::testDPadKeyRotation(KeyboardInputMapper& mapper,
Arthur Hung2c9a3342019-07-23 14:18:59 +08003336 int32_t originalScanCode, int32_t originalKeyCode,
3337 int32_t rotatedKeyCode, int32_t displayId) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003338 NotifyKeyArgs args;
3339
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003340 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003341 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3342 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3343 ASSERT_EQ(originalScanCode, args.scanCode);
3344 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003345 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003346
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003347 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003348 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3349 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3350 ASSERT_EQ(originalScanCode, args.scanCode);
3351 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003352 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003353}
3354
Michael Wrightd02c5b62014-02-10 15:10:22 -08003355TEST_F(KeyboardInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003356 KeyboardInputMapper& mapper =
3357 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3358 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003359
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003360 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003361}
3362
3363TEST_F(KeyboardInputMapperTest, Process_SimpleKeyPress) {
3364 const int32_t USAGE_A = 0x070004;
3365 const int32_t USAGE_UNKNOWN = 0x07ffff;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003366 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3367 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
Chris Yea52ade12020-08-27 16:49:20 -07003368 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, POLICY_FLAG_WAKE);
3369 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, POLICY_FLAG_WAKE);
3370 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003371
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003372 KeyboardInputMapper& mapper =
3373 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3374 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003375 // Initial metastate is AMETA_NONE.
3376 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003377
3378 // Key down by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003379 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003380 NotifyKeyArgs args;
3381 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3382 ASSERT_EQ(DEVICE_ID, args.deviceId);
3383 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3384 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3385 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3386 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3387 ASSERT_EQ(KEY_HOME, args.scanCode);
3388 ASSERT_EQ(AMETA_NONE, args.metaState);
3389 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3390 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3391 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3392
3393 // Key up by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003394 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003395 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3396 ASSERT_EQ(DEVICE_ID, args.deviceId);
3397 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3398 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3399 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3400 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3401 ASSERT_EQ(KEY_HOME, args.scanCode);
3402 ASSERT_EQ(AMETA_NONE, args.metaState);
3403 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3404 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3405 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3406
3407 // Key down by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003408 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3409 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, 0, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003410 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3411 ASSERT_EQ(DEVICE_ID, args.deviceId);
3412 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3413 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3414 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3415 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3416 ASSERT_EQ(0, args.scanCode);
3417 ASSERT_EQ(AMETA_NONE, args.metaState);
3418 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3419 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3420 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3421
3422 // Key up by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003423 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3424 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003425 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3426 ASSERT_EQ(DEVICE_ID, args.deviceId);
3427 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3428 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3429 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3430 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3431 ASSERT_EQ(0, args.scanCode);
3432 ASSERT_EQ(AMETA_NONE, args.metaState);
3433 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3434 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3435 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3436
3437 // Key down with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003438 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3439 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UNKNOWN, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003440 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3441 ASSERT_EQ(DEVICE_ID, args.deviceId);
3442 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3443 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3444 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3445 ASSERT_EQ(0, args.keyCode);
3446 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3447 ASSERT_EQ(AMETA_NONE, args.metaState);
3448 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3449 ASSERT_EQ(0U, args.policyFlags);
3450 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3451
3452 // Key up with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003453 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3454 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003455 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3456 ASSERT_EQ(DEVICE_ID, args.deviceId);
3457 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3458 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3459 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3460 ASSERT_EQ(0, args.keyCode);
3461 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3462 ASSERT_EQ(AMETA_NONE, args.metaState);
3463 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3464 ASSERT_EQ(0U, args.policyFlags);
3465 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3466}
3467
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003468/**
3469 * Ensure that the readTime is set to the time when the EV_KEY is received.
3470 */
3471TEST_F(KeyboardInputMapperTest, Process_SendsReadTime) {
3472 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3473
3474 KeyboardInputMapper& mapper =
3475 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3476 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3477 NotifyKeyArgs args;
3478
3479 // Key down
3480 process(mapper, ARBITRARY_TIME, 12 /*readTime*/, EV_KEY, KEY_HOME, 1);
3481 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3482 ASSERT_EQ(12, args.readTime);
3483
3484 // Key up
3485 process(mapper, ARBITRARY_TIME, 15 /*readTime*/, EV_KEY, KEY_HOME, 1);
3486 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3487 ASSERT_EQ(15, args.readTime);
3488}
3489
Michael Wrightd02c5b62014-02-10 15:10:22 -08003490TEST_F(KeyboardInputMapperTest, Process_ShouldUpdateMetaState) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003491 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFTSHIFT, 0, AKEYCODE_SHIFT_LEFT, 0);
3492 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003493 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, 0);
3494 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, 0);
3495 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003496
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003497 KeyboardInputMapper& mapper =
3498 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3499 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003500
Arthur Hung95f68612022-04-07 14:08:22 +08003501 // Initial metastate is AMETA_NONE.
3502 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003503
3504 // Metakey down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003505 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003506 NotifyKeyArgs args;
3507 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3508 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003509 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003510 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003511
3512 // Key down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003513 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_A, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003514 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3515 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003516 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003517
3518 // Key up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003519 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, KEY_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003520 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3521 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003522 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003523
3524 // Metakey up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003525 process(mapper, ARBITRARY_TIME + 3, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003526 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3527 ASSERT_EQ(AMETA_NONE, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003528 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003529 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003530}
3531
3532TEST_F(KeyboardInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003533 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3534 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3535 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3536 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003537
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003538 KeyboardInputMapper& mapper =
3539 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3540 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003541
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003542 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003543 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3544 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
3545 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3546 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
3547 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3548 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
3549 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3550 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
3551}
3552
3553TEST_F(KeyboardInputMapperTest, Process_WhenOrientationAware_ShouldRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003554 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3555 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3556 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3557 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003558
Michael Wrightd02c5b62014-02-10 15:10:22 -08003559 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003560 KeyboardInputMapper& mapper =
3561 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3562 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003563
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003564 prepareDisplay(DISPLAY_ORIENTATION_0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003565 ASSERT_NO_FATAL_FAILURE(
3566 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3567 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3568 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3569 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3570 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3571 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3572 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003573
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003574 clearViewports();
3575 prepareDisplay(DISPLAY_ORIENTATION_90);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003576 ASSERT_NO_FATAL_FAILURE(
3577 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3578 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3579 AKEYCODE_DPAD_UP, DISPLAY_ID));
3580 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3581 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3582 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3583 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003584
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003585 clearViewports();
3586 prepareDisplay(DISPLAY_ORIENTATION_180);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003587 ASSERT_NO_FATAL_FAILURE(
3588 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3589 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3590 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3591 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3592 AKEYCODE_DPAD_UP, DISPLAY_ID));
3593 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3594 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003595
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003596 clearViewports();
3597 prepareDisplay(DISPLAY_ORIENTATION_270);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003598 ASSERT_NO_FATAL_FAILURE(
3599 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3600 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3601 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3602 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3603 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3604 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3605 AKEYCODE_DPAD_UP, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003606
3607 // Special case: if orientation changes while key is down, we still emit the same keycode
3608 // in the key up as we did in the key down.
3609 NotifyKeyArgs args;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003610 clearViewports();
3611 prepareDisplay(DISPLAY_ORIENTATION_270);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003612 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003613 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3614 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3615 ASSERT_EQ(KEY_UP, args.scanCode);
3616 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3617
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003618 clearViewports();
3619 prepareDisplay(DISPLAY_ORIENTATION_180);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003620 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003621 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3622 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3623 ASSERT_EQ(KEY_UP, args.scanCode);
3624 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3625}
3626
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003627TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_NotOrientationAware) {
3628 // If the keyboard is not orientation aware,
3629 // key events should not be associated with a specific display id
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003630 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003631
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003632 KeyboardInputMapper& mapper =
3633 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3634 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003635 NotifyKeyArgs args;
3636
3637 // Display id should be ADISPLAY_ID_NONE without any display configuration.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003638 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003639 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003640 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003641 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3642 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3643
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003644 prepareDisplay(DISPLAY_ORIENTATION_0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003645 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003646 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003647 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003648 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3649 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3650}
3651
3652TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_OrientationAware) {
3653 // If the keyboard is orientation aware,
3654 // key events should be associated with the internal viewport
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003655 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003656
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003657 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003658 KeyboardInputMapper& mapper =
3659 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3660 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003661 NotifyKeyArgs args;
3662
3663 // Display id should be ADISPLAY_ID_NONE without any display configuration.
3664 // ^--- already checked by the previous test
3665
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003666 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003667 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003668 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003669 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003670 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003671 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3672 ASSERT_EQ(DISPLAY_ID, args.displayId);
3673
3674 constexpr int32_t newDisplayId = 2;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003675 clearViewports();
3676 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003677 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003678 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003679 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003680 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003681 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3682 ASSERT_EQ(newDisplayId, args.displayId);
3683}
3684
Michael Wrightd02c5b62014-02-10 15:10:22 -08003685TEST_F(KeyboardInputMapperTest, GetKeyCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003686 KeyboardInputMapper& mapper =
3687 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3688 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003689
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003690 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003691 ASSERT_EQ(1, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003692
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003693 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003694 ASSERT_EQ(0, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003695}
3696
Philip Junker4af3b3d2021-12-14 10:36:55 +01003697TEST_F(KeyboardInputMapperTest, GetKeyCodeForKeyLocation) {
3698 KeyboardInputMapper& mapper =
3699 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3700 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3701
3702 mFakeEventHub->addKeyCodeMapping(EVENTHUB_ID, AKEYCODE_Y, AKEYCODE_Z);
3703 ASSERT_EQ(AKEYCODE_Z, mapper.getKeyCodeForKeyLocation(AKEYCODE_Y))
3704 << "If a mapping is available, the result is equal to the mapping";
3705
3706 ASSERT_EQ(AKEYCODE_A, mapper.getKeyCodeForKeyLocation(AKEYCODE_A))
3707 << "If no mapping is available, the result is the key location";
3708}
3709
Michael Wrightd02c5b62014-02-10 15:10:22 -08003710TEST_F(KeyboardInputMapperTest, GetScanCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003711 KeyboardInputMapper& mapper =
3712 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3713 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003714
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003715 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003716 ASSERT_EQ(1, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003717
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003718 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003719 ASSERT_EQ(0, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003720}
3721
3722TEST_F(KeyboardInputMapperTest, MarkSupportedKeyCodes) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003723 KeyboardInputMapper& mapper =
3724 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3725 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003726
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003727 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003728
3729 const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
3730 uint8_t flags[2] = { 0, 0 };
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003731 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, 1, keyCodes, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003732 ASSERT_TRUE(flags[0]);
3733 ASSERT_FALSE(flags[1]);
3734}
3735
3736TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleMetaStateAndLeds) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003737 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3738 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3739 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3740 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3741 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3742 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003743
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003744 KeyboardInputMapper& mapper =
3745 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3746 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003747 // Initial metastate is AMETA_NONE.
3748 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003749
3750 // Initialization should have turned all of the lights off.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003751 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3752 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3753 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003754
3755 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003756 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3757 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003758 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3759 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3760 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003761 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003762
3763 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003764 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3765 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003766 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3767 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3768 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003769 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003770
3771 // Toggle caps lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003772 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3773 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003774 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3775 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3776 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003777 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003778
3779 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003780 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3781 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003782 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3783 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3784 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003785 ASSERT_EQ(AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003786
3787 // Toggle num lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003788 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3789 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003790 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3791 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3792 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003793 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003794
3795 // Toggle scroll lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003796 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3797 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003798 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3799 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3800 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003801 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003802}
3803
Chris Yea52ade12020-08-27 16:49:20 -07003804TEST_F(KeyboardInputMapperTest, NoMetaStateWhenMetaKeysNotPresent) {
3805 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_BUTTON_A, 0);
3806 mFakeEventHub->addKey(EVENTHUB_ID, BTN_B, 0, AKEYCODE_BUTTON_B, 0);
3807 mFakeEventHub->addKey(EVENTHUB_ID, BTN_X, 0, AKEYCODE_BUTTON_X, 0);
3808 mFakeEventHub->addKey(EVENTHUB_ID, BTN_Y, 0, AKEYCODE_BUTTON_Y, 0);
3809
3810 KeyboardInputMapper& mapper =
3811 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3812 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
3813
Chris Yea52ade12020-08-27 16:49:20 -07003814 // Meta state should be AMETA_NONE after reset
3815 mapper.reset(ARBITRARY_TIME);
3816 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3817 // Meta state should be AMETA_NONE with update, as device doesn't have the keys.
3818 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
3819 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3820
3821 NotifyKeyArgs args;
3822 // Press button "A"
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003823 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_A, 1);
Chris Yea52ade12020-08-27 16:49:20 -07003824 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3825 ASSERT_EQ(AMETA_NONE, args.metaState);
3826 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3827 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3828 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3829
3830 // Button up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003831 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003832 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3833 ASSERT_EQ(AMETA_NONE, args.metaState);
3834 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3835 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3836 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3837}
3838
Arthur Hung2c9a3342019-07-23 14:18:59 +08003839TEST_F(KeyboardInputMapperTest, Configure_AssignsDisplayPort) {
3840 // keyboard 1.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003841 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3842 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3843 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3844 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003845
3846 // keyboard 2.
3847 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08003848 const std::string DEVICE_NAME2 = "KEYBOARD2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08003849 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003850 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08003851 std::shared_ptr<InputDevice> device2 =
3852 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003853 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08003854
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003855 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3856 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3857 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3858 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003859
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003860 KeyboardInputMapper& mapper =
3861 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3862 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003863
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003864 KeyboardInputMapper& mapper2 =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003865 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003866 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003867 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
3868 device2->reset(ARBITRARY_TIME);
3869
3870 // Prepared displays and associated info.
3871 constexpr uint8_t hdmi1 = 0;
3872 constexpr uint8_t hdmi2 = 1;
3873 const std::string SECONDARY_UNIQUE_ID = "local:1";
3874
3875 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
3876 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
3877
3878 // No associated display viewport found, should disable the device.
3879 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3880 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3881 ASSERT_FALSE(device2->isEnabled());
3882
3883 // Prepare second display.
3884 constexpr int32_t newDisplayId = 2;
3885 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003886 UNIQUE_ID, hdmi1, ViewportType::INTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003887 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003888 SECONDARY_UNIQUE_ID, hdmi2, ViewportType::EXTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003889 // Default device will reconfigure above, need additional reconfiguration for another device.
3890 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3891 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3892
3893 // Device should be enabled after the associated display is found.
3894 ASSERT_TRUE(mDevice->isEnabled());
3895 ASSERT_TRUE(device2->isEnabled());
3896
3897 // Test pad key events
3898 ASSERT_NO_FATAL_FAILURE(
3899 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3900 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3901 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3902 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3903 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3904 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3905 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3906
3907 ASSERT_NO_FATAL_FAILURE(
3908 testDPadKeyRotation(mapper2, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, newDisplayId));
3909 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3910 AKEYCODE_DPAD_RIGHT, newDisplayId));
3911 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3912 AKEYCODE_DPAD_DOWN, newDisplayId));
3913 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3914 AKEYCODE_DPAD_LEFT, newDisplayId));
3915}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003916
arthurhungc903df12020-08-11 15:08:42 +08003917TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleAfterReattach) {
3918 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3919 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3920 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3921 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3922 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3923 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3924
3925 KeyboardInputMapper& mapper =
3926 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3927 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003928 // Initial metastate is AMETA_NONE.
3929 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08003930
3931 // Initialization should have turned all of the lights off.
3932 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3933 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3934 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3935
3936 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003937 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3938 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003939 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3940 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
3941
3942 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003943 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3944 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003945 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3946 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
3947
3948 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003949 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3950 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003951 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3952 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
3953
3954 mFakeEventHub->removeDevice(EVENTHUB_ID);
3955 mReader->loopOnce();
3956
3957 // keyboard 2 should default toggle keys.
3958 const std::string USB2 = "USB2";
3959 const std::string DEVICE_NAME2 = "KEYBOARD2";
3960 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
3961 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
3962 std::shared_ptr<InputDevice> device2 =
3963 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003964 ftl::Flags<InputDeviceClass>(0));
arthurhungc903df12020-08-11 15:08:42 +08003965 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3966 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
3967 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3968 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3969 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3970 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3971
arthurhung6fe95782020-10-05 22:41:16 +08003972 KeyboardInputMapper& mapper2 =
3973 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
3974 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
arthurhungc903df12020-08-11 15:08:42 +08003975 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
3976 device2->reset(ARBITRARY_TIME);
3977
3978 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_CAPSL));
3979 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_NUML));
3980 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_SCROLLL));
arthurhung6fe95782020-10-05 22:41:16 +08003981 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON,
3982 mapper2.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08003983}
3984
Arthur Hungcb40a002021-08-03 14:31:01 +00003985TEST_F(KeyboardInputMapperTest, Process_toggleCapsLockState) {
3986 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3987 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3988 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3989
3990 // Suppose we have two mappers. (DPAD + KEYBOARD)
3991 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_DPAD,
3992 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
3993 KeyboardInputMapper& mapper =
3994 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3995 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003996 // Initial metastate is AMETA_NONE.
3997 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Arthur Hungcb40a002021-08-03 14:31:01 +00003998
3999 mReader->toggleCapsLockState(DEVICE_ID);
4000 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
4001}
4002
Arthur Hungfb3cc112022-04-13 07:39:50 +00004003TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleInMultiDevices) {
4004 // keyboard 1.
4005 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
4006 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
4007 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
4008 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4009 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4010 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
4011
4012 KeyboardInputMapper& mapper1 =
4013 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4014 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
4015
4016 // keyboard 2.
4017 const std::string USB2 = "USB2";
4018 const std::string DEVICE_NAME2 = "KEYBOARD2";
4019 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
4020 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
4021 std::shared_ptr<InputDevice> device2 =
4022 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
4023 ftl::Flags<InputDeviceClass>(0));
4024 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
4025 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
4026 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
4027 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4028 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4029 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
4030
4031 KeyboardInputMapper& mapper2 =
4032 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
4033 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
4034 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
4035 device2->reset(ARBITRARY_TIME);
4036
Arthur Hung95f68612022-04-07 14:08:22 +08004037 // Initial metastate is AMETA_NONE.
4038 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4039 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4040
4041 // Toggle num lock on and off.
4042 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
4043 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Arthur Hungfb3cc112022-04-13 07:39:50 +00004044 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4045 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper1.getMetaState());
4046 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper2.getMetaState());
4047
4048 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
4049 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
4050 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4051 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4052 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4053
4054 // Toggle caps lock on and off.
4055 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
4056 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
4057 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4058 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper1.getMetaState());
4059 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper2.getMetaState());
4060
4061 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
4062 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
4063 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4064 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4065 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4066
4067 // Toggle scroll lock on and off.
4068 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
4069 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
4070 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
4071 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper1.getMetaState());
4072 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper2.getMetaState());
4073
4074 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
4075 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
4076 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
4077 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4078 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4079}
4080
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004081// --- KeyboardInputMapperTest_ExternalDevice ---
4082
4083class KeyboardInputMapperTest_ExternalDevice : public InputMapperTest {
4084protected:
Chris Yea52ade12020-08-27 16:49:20 -07004085 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004086};
4087
4088TEST_F(KeyboardInputMapperTest_ExternalDevice, WakeBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07004089 // For external devices, non-media keys will trigger wake on key down. Media keys need to be
4090 // marked as WAKE in the keylayout file to trigger wake.
Powei Fengd041c5d2019-05-03 17:11:33 -07004091
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004092 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, 0);
4093 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, 0);
4094 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAYPAUSE, 0, AKEYCODE_MEDIA_PLAY_PAUSE,
4095 POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07004096
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004097 KeyboardInputMapper& mapper =
4098 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4099 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07004100
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004101 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004102 NotifyKeyArgs args;
4103 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4104 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4105
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004106 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004107 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4108 ASSERT_EQ(uint32_t(0), args.policyFlags);
4109
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004110 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004111 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4112 ASSERT_EQ(uint32_t(0), args.policyFlags);
4113
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004114 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004115 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4116 ASSERT_EQ(uint32_t(0), args.policyFlags);
4117
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004118 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004119 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4120 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4121
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004122 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004123 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4124 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4125}
4126
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004127TEST_F(KeyboardInputMapperTest_ExternalDevice, DoNotWakeByDefaultBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07004128 // Tv Remote key's wake behavior is prescribed by the keylayout file.
Powei Fengd041c5d2019-05-03 17:11:33 -07004129
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004130 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4131 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
4132 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07004133
Powei Fengd041c5d2019-05-03 17:11:33 -07004134 addConfigurationProperty("keyboard.doNotWakeByDefault", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004135 KeyboardInputMapper& mapper =
4136 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4137 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07004138
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004139 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004140 NotifyKeyArgs args;
4141 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4142 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4143
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004144 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004145 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4146 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4147
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004148 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_DOWN, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004149 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4150 ASSERT_EQ(uint32_t(0), args.policyFlags);
4151
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004152 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_DOWN, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004153 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4154 ASSERT_EQ(uint32_t(0), args.policyFlags);
4155
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004156 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004157 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4158 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4159
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004160 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004161 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4162 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4163}
4164
Michael Wrightd02c5b62014-02-10 15:10:22 -08004165// --- CursorInputMapperTest ---
4166
4167class CursorInputMapperTest : public InputMapperTest {
4168protected:
4169 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD;
4170
Michael Wright17db18e2020-06-26 20:51:44 +01004171 std::shared_ptr<FakePointerController> mFakePointerController;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004172
Chris Yea52ade12020-08-27 16:49:20 -07004173 void SetUp() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004174 InputMapperTest::SetUp();
4175
Michael Wright17db18e2020-06-26 20:51:44 +01004176 mFakePointerController = std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00004177 mFakePolicy->setPointerController(mFakePointerController);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004178 }
4179
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004180 void testMotionRotation(CursorInputMapper& mapper, int32_t originalX, int32_t originalY,
4181 int32_t rotatedX, int32_t rotatedY);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004182
4183 void prepareDisplay(int32_t orientation) {
4184 const std::string uniqueId = "local:0";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004185 const ViewportType viewportType = ViewportType::INTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004186 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
4187 orientation, uniqueId, NO_PORT, viewportType);
4188 }
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004189
4190 static void assertCursorPointerCoords(const PointerCoords& coords, float x, float y,
4191 float pressure) {
4192 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(coords, x, y, pressure, 0.0f, 0.0f, 0.0f, 0.0f,
4193 0.0f, 0.0f, 0.0f, EPSILON));
4194 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08004195};
4196
4197const int32_t CursorInputMapperTest::TRACKBALL_MOVEMENT_THRESHOLD = 6;
4198
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004199void CursorInputMapperTest::testMotionRotation(CursorInputMapper& mapper, int32_t originalX,
4200 int32_t originalY, int32_t rotatedX,
4201 int32_t rotatedY) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004202 NotifyMotionArgs args;
4203
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004204 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, originalX);
4205 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, originalY);
4206 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004207 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4208 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004209 ASSERT_NO_FATAL_FAILURE(
4210 assertCursorPointerCoords(args.pointerCoords[0],
4211 float(rotatedX) / TRACKBALL_MOVEMENT_THRESHOLD,
4212 float(rotatedY) / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004213}
4214
4215TEST_F(CursorInputMapperTest, WhenModeIsPointer_GetSources_ReturnsMouse) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004216 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004217 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004218
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004219 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004220}
4221
4222TEST_F(CursorInputMapperTest, WhenModeIsNavigation_GetSources_ReturnsTrackball) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004223 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004224 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004225
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004226 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004227}
4228
4229TEST_F(CursorInputMapperTest, WhenModeIsPointer_PopulateDeviceInfo_ReturnsRangeFromPointerController) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004230 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004231 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004232
4233 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004234 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004235
4236 // Initially there may not be a valid motion range.
Yi Kong9b14ac62018-07-17 13:48:38 -07004237 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE));
4238 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004239 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4240 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE, 0.0f, 1.0f, 0.0f, 0.0f));
4241
4242 // When the bounds are set, then there should be a valid motion range.
4243 mFakePointerController->setBounds(1, 2, 800 - 1, 480 - 1);
4244
4245 InputDeviceInfo info2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004246 mapper.populateDeviceInfo(&info2);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004247
4248 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
4249 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE,
4250 1, 800 - 1, 0.0f, 0.0f));
4251 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
4252 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE,
4253 2, 480 - 1, 0.0f, 0.0f));
4254 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
4255 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE,
4256 0.0f, 1.0f, 0.0f, 0.0f));
4257}
4258
4259TEST_F(CursorInputMapperTest, WhenModeIsNavigation_PopulateDeviceInfo_ReturnsScaledRange) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004260 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004261 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004262
4263 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004264 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004265
4266 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4267 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_TRACKBALL,
4268 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
4269 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4270 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_TRACKBALL,
4271 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
4272 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4273 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TRACKBALL,
4274 0.0f, 1.0f, 0.0f, 0.0f));
4275}
4276
4277TEST_F(CursorInputMapperTest, Process_ShouldSetAllFieldsAndIncludeGlobalMetaState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004278 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004279 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004280
arthurhungdcef2dc2020-08-11 14:47:50 +08004281 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004282
4283 NotifyMotionArgs args;
4284
4285 // Button press.
4286 // Mostly testing non x/y behavior here so we don't need to check again elsewhere.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004287 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4288 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004289 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4290 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4291 ASSERT_EQ(DEVICE_ID, args.deviceId);
4292 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4293 ASSERT_EQ(uint32_t(0), args.policyFlags);
4294 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
4295 ASSERT_EQ(0, args.flags);
4296 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4297 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
4298 ASSERT_EQ(0, args.edgeFlags);
4299 ASSERT_EQ(uint32_t(1), args.pointerCount);
4300 ASSERT_EQ(0, args.pointerProperties[0].id);
4301 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004302 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004303 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4304 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4305 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4306
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004307 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4308 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4309 ASSERT_EQ(DEVICE_ID, args.deviceId);
4310 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4311 ASSERT_EQ(uint32_t(0), args.policyFlags);
4312 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
4313 ASSERT_EQ(0, args.flags);
4314 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4315 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
4316 ASSERT_EQ(0, args.edgeFlags);
4317 ASSERT_EQ(uint32_t(1), args.pointerCount);
4318 ASSERT_EQ(0, args.pointerProperties[0].id);
4319 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004320 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004321 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4322 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4323 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4324
Michael Wrightd02c5b62014-02-10 15:10:22 -08004325 // Button release. Should have same down time.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004326 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4327 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004328 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4329 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
4330 ASSERT_EQ(DEVICE_ID, args.deviceId);
4331 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4332 ASSERT_EQ(uint32_t(0), args.policyFlags);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004333 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
4334 ASSERT_EQ(0, args.flags);
4335 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4336 ASSERT_EQ(0, args.buttonState);
4337 ASSERT_EQ(0, args.edgeFlags);
4338 ASSERT_EQ(uint32_t(1), args.pointerCount);
4339 ASSERT_EQ(0, args.pointerProperties[0].id);
4340 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004341 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004342 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4343 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4344 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4345
4346 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4347 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
4348 ASSERT_EQ(DEVICE_ID, args.deviceId);
4349 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4350 ASSERT_EQ(uint32_t(0), args.policyFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004351 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
4352 ASSERT_EQ(0, args.flags);
4353 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4354 ASSERT_EQ(0, args.buttonState);
4355 ASSERT_EQ(0, args.edgeFlags);
4356 ASSERT_EQ(uint32_t(1), args.pointerCount);
4357 ASSERT_EQ(0, args.pointerProperties[0].id);
4358 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004359 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004360 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4361 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4362 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4363}
4364
4365TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentXYUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004366 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004367 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004368
4369 NotifyMotionArgs args;
4370
4371 // Motion in X but not Y.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004372 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4373 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004374 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4375 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004376 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4377 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f,
4378 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004379
4380 // Motion in Y but not X.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004381 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
4382 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004383 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4384 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004385 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f,
4386 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004387}
4388
4389TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004390 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004391 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004392
4393 NotifyMotionArgs args;
4394
4395 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004396 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4397 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004398 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4399 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004400 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004401
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004402 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4403 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004404 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004405
Michael Wrightd02c5b62014-02-10 15:10:22 -08004406 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004407 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4408 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004409 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004410 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004411 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004412
4413 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004414 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004415 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004416}
4417
4418TEST_F(CursorInputMapperTest, Process_ShouldHandleCombinedXYAndButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004419 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004420 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004421
4422 NotifyMotionArgs args;
4423
4424 // Combined X, Y and Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004425 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4426 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
4427 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4428 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004429 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4430 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004431 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4432 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4433 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004434
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004435 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4436 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004437 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4438 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4439 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004440
Michael Wrightd02c5b62014-02-10 15:10:22 -08004441 // Move X, Y a bit while pressed.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004442 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 2);
4443 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 1);
4444 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004445 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4446 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004447 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4448 2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4449 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004450
4451 // Release Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004452 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4453 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004454 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004455 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004456 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004457
4458 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004459 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004460 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004461}
4462
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004463TEST_F(CursorInputMapperTest, Process_WhenOrientationAware_ShouldNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004464 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004465 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
4466 // need to be rotated.
4467 addConfigurationProperty("cursor.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004468 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004469
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004470 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004471 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4472 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4473 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4474 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4475 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4476 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4477 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4478 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4479}
4480
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004481TEST_F(CursorInputMapperTest, Process_WhenNotOrientationAware_ShouldRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004482 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004483 // Since InputReader works in the un-rotated coordinate space, only devices that are not
4484 // orientation-aware are affected by display rotation.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004485 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004486
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004487 prepareDisplay(DISPLAY_ORIENTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004488 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4489 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4490 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4491 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4492 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4493 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4494 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4495 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4496
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004497 prepareDisplay(DISPLAY_ORIENTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004498 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, -1, 0));
4499 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, 1));
4500 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, 1));
4501 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, 1));
4502 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 1, 0));
4503 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, -1));
4504 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, -1));
4505 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, -1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004506
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004507 prepareDisplay(DISPLAY_ORIENTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004508 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, -1));
4509 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, -1));
4510 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, -1, 0));
4511 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, 1));
4512 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, 1));
4513 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, 1));
4514 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 1, 0));
4515 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, -1));
4516
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004517 prepareDisplay(DISPLAY_ORIENTATION_270);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004518 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 1, 0));
4519 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, -1));
4520 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, -1));
4521 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, -1));
4522 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, -1, 0));
4523 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, 1));
4524 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, 1));
4525 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, 1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004526}
4527
4528TEST_F(CursorInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004529 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004530 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004531
4532 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4533 mFakePointerController->setPosition(100, 200);
4534 mFakePointerController->setButtonState(0);
4535
4536 NotifyMotionArgs motionArgs;
4537 NotifyKeyArgs keyArgs;
4538
4539 // press BTN_LEFT, release BTN_LEFT
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004540 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 1);
4541 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004542 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4543 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4544 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4545 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004546 ASSERT_NO_FATAL_FAILURE(
4547 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004548
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004549 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4550 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4551 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4552 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004553 ASSERT_NO_FATAL_FAILURE(
4554 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004555
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004556 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 0);
4557 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004558 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004559 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004560 ASSERT_EQ(0, motionArgs.buttonState);
4561 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004562 ASSERT_NO_FATAL_FAILURE(
4563 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004564
4565 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004566 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004567 ASSERT_EQ(0, motionArgs.buttonState);
4568 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004569 ASSERT_NO_FATAL_FAILURE(
4570 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004571
4572 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004573 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004574 ASSERT_EQ(0, motionArgs.buttonState);
4575 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004576 ASSERT_NO_FATAL_FAILURE(
4577 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004578
4579 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004580 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 1);
4581 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 1);
4582 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004583 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4584 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4585 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4586 motionArgs.buttonState);
4587 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4588 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004589 ASSERT_NO_FATAL_FAILURE(
4590 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004591
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004592 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4593 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4594 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4595 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4596 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004597 ASSERT_NO_FATAL_FAILURE(
4598 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004599
4600 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4601 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4602 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4603 motionArgs.buttonState);
4604 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4605 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004606 ASSERT_NO_FATAL_FAILURE(
4607 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004608
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004609 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 0);
4610 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004611 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004612 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004613 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4614 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004615 ASSERT_NO_FATAL_FAILURE(
4616 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004617
4618 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004619 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004620 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4621 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004622 ASSERT_NO_FATAL_FAILURE(
4623 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004624
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004625 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4626 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004627 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004628 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
4629 ASSERT_EQ(0, motionArgs.buttonState);
4630 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004631 ASSERT_NO_FATAL_FAILURE(
4632 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004633 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4634 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004635
4636 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004637 ASSERT_EQ(0, motionArgs.buttonState);
4638 ASSERT_EQ(0, mFakePointerController->getButtonState());
4639 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004640 ASSERT_NO_FATAL_FAILURE(
4641 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004642
Michael Wrightd02c5b62014-02-10 15:10:22 -08004643 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4644 ASSERT_EQ(0, motionArgs.buttonState);
4645 ASSERT_EQ(0, mFakePointerController->getButtonState());
4646 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004647 ASSERT_NO_FATAL_FAILURE(
4648 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004649
4650 // press BTN_BACK, release BTN_BACK
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004651 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 1);
4652 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004653 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4654 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4655 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004656
Michael Wrightd02c5b62014-02-10 15:10:22 -08004657 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004658 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004659 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4660 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004661 ASSERT_NO_FATAL_FAILURE(
4662 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004663
4664 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4665 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4666 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4667 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004668 ASSERT_NO_FATAL_FAILURE(
4669 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004670
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004671 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 0);
4672 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004673 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004674 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004675 ASSERT_EQ(0, motionArgs.buttonState);
4676 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004677 ASSERT_NO_FATAL_FAILURE(
4678 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004679
4680 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004681 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004682 ASSERT_EQ(0, motionArgs.buttonState);
4683 ASSERT_EQ(0, mFakePointerController->getButtonState());
4684
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004685 ASSERT_NO_FATAL_FAILURE(
4686 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004687 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4688 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4689 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4690
4691 // press BTN_SIDE, release BTN_SIDE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004692 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 1);
4693 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004694 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4695 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4696 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004697
Michael Wrightd02c5b62014-02-10 15:10:22 -08004698 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004699 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004700 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4701 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004702 ASSERT_NO_FATAL_FAILURE(
4703 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004704
4705 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4706 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4707 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4708 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004709 ASSERT_NO_FATAL_FAILURE(
4710 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004711
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004712 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 0);
4713 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004714 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004715 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004716 ASSERT_EQ(0, motionArgs.buttonState);
4717 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004718 ASSERT_NO_FATAL_FAILURE(
4719 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004720
4721 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4722 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4723 ASSERT_EQ(0, motionArgs.buttonState);
4724 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004725 ASSERT_NO_FATAL_FAILURE(
4726 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004727
Michael Wrightd02c5b62014-02-10 15:10:22 -08004728 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4729 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4730 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4731
4732 // press BTN_FORWARD, release BTN_FORWARD
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004733 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 1);
4734 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004735 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4736 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4737 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004738
Michael Wrightd02c5b62014-02-10 15:10:22 -08004739 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004740 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004741 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4742 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004743 ASSERT_NO_FATAL_FAILURE(
4744 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004745
4746 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4747 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4748 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4749 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004750 ASSERT_NO_FATAL_FAILURE(
4751 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004752
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004753 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 0);
4754 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004755 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004756 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004757 ASSERT_EQ(0, motionArgs.buttonState);
4758 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004759 ASSERT_NO_FATAL_FAILURE(
4760 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004761
4762 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4763 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4764 ASSERT_EQ(0, motionArgs.buttonState);
4765 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004766 ASSERT_NO_FATAL_FAILURE(
4767 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004768
Michael Wrightd02c5b62014-02-10 15:10:22 -08004769 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4770 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4771 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4772
4773 // press BTN_EXTRA, release BTN_EXTRA
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004774 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 1);
4775 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004776 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4777 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4778 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004779
Michael Wrightd02c5b62014-02-10 15:10:22 -08004780 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004781 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004782 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4783 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004784 ASSERT_NO_FATAL_FAILURE(
4785 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004786
4787 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4788 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4789 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4790 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004791 ASSERT_NO_FATAL_FAILURE(
4792 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004793
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004794 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 0);
4795 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004796 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004797 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004798 ASSERT_EQ(0, motionArgs.buttonState);
4799 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004800 ASSERT_NO_FATAL_FAILURE(
4801 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004802
4803 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4804 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4805 ASSERT_EQ(0, motionArgs.buttonState);
4806 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004807 ASSERT_NO_FATAL_FAILURE(
4808 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004809
Michael Wrightd02c5b62014-02-10 15:10:22 -08004810 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4811 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4812 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4813}
4814
4815TEST_F(CursorInputMapperTest, Process_WhenModeIsPointer_ShouldMoveThePointerAround) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004816 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004817 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004818
4819 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4820 mFakePointerController->setPosition(100, 200);
4821 mFakePointerController->setButtonState(0);
4822
4823 NotifyMotionArgs args;
4824
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004825 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4826 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4827 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004828 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004829 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4830 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4831 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4832 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 +01004833 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004834}
4835
4836TEST_F(CursorInputMapperTest, Process_PointerCapture) {
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004837 addConfigurationProperty("cursor.mode", "pointer");
4838 mFakePolicy->setPointerCapture(true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004839 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004840
4841 NotifyDeviceResetArgs resetArgs;
4842 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4843 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4844 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4845
4846 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4847 mFakePointerController->setPosition(100, 200);
4848 mFakePointerController->setButtonState(0);
4849
4850 NotifyMotionArgs args;
4851
4852 // Move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004853 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4854 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4855 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004856 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4857 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4858 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4859 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4860 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 +01004861 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004862
4863 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004864 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4865 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004866 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4867 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4868 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
4869 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4870 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4871 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4872 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4873 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, 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
4877 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004878 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4879 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004880 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4881 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4882 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
4883 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4884 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4885 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4886 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4887 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, 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
4891 // Another move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004892 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 30);
4893 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 40);
4894 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004895 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4896 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4897 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4898 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4899 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 +01004900 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004901
4902 // Disable pointer capture and check that the device generation got bumped
4903 // and events are generated the usual way.
arthurhungdcef2dc2020-08-11 14:47:50 +08004904 const uint32_t generation = mReader->getContext()->getGeneration();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004905 mFakePolicy->setPointerCapture(false);
4906 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
arthurhungdcef2dc2020-08-11 14:47:50 +08004907 ASSERT_TRUE(mReader->getContext()->getGeneration() != generation);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004908
4909 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4910 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4911 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4912
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004913 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4914 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4915 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004916 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4917 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004918 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4919 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4920 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 +01004921 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004922}
4923
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00004924/**
4925 * When Pointer Capture is enabled, we expect to report unprocessed relative movements, so any
4926 * pointer acceleration or speed processing should not be applied.
4927 */
4928TEST_F(CursorInputMapperTest, PointerCaptureDisablesVelocityProcessing) {
4929 addConfigurationProperty("cursor.mode", "pointer");
4930 const VelocityControlParameters testParams(5.f /*scale*/, 0.f /*low threshold*/,
4931 100.f /*high threshold*/, 10.f /*acceleration*/);
4932 mFakePolicy->setVelocityControlParams(testParams);
4933 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
4934
4935 NotifyDeviceResetArgs resetArgs;
4936 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4937 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4938 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4939
4940 NotifyMotionArgs args;
4941
4942 // Move and verify scale is applied.
4943 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4944 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4945 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4946 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4947 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4948 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4949 const float relX = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X);
4950 const float relY = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y);
4951 ASSERT_GT(relX, 10);
4952 ASSERT_GT(relY, 20);
4953
4954 // Enable Pointer Capture
4955 mFakePolicy->setPointerCapture(true);
4956 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
4957 NotifyPointerCaptureChangedArgs captureArgs;
4958 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyCaptureWasCalled(&captureArgs));
4959 ASSERT_TRUE(captureArgs.request.enable);
4960
4961 // Move and verify scale is not applied.
4962 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4963 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4964 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4965 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4966 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4967 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4968 ASSERT_EQ(10, args.pointerCoords[0].getX());
4969 ASSERT_EQ(20, args.pointerCoords[0].getY());
4970}
4971
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004972TEST_F(CursorInputMapperTest, Process_ShouldHandleDisplayId) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004973 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004974
Garfield Tan888a6a42020-01-09 11:39:16 -08004975 // Setup for second display.
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004976 constexpr int32_t SECOND_DISPLAY_ID = 1;
Garfield Tan888a6a42020-01-09 11:39:16 -08004977 const std::string SECOND_DISPLAY_UNIQUE_ID = "local:1";
4978 mFakePolicy->addDisplayViewport(SECOND_DISPLAY_ID, 800, 480, DISPLAY_ORIENTATION_0,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00004979 true /*isActive*/, SECOND_DISPLAY_UNIQUE_ID, NO_PORT,
4980 ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08004981 mFakePolicy->setDefaultPointerDisplayId(SECOND_DISPLAY_ID);
4982 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
4983
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004984 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4985 mFakePointerController->setPosition(100, 200);
4986 mFakePointerController->setButtonState(0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004987
4988 NotifyMotionArgs args;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004989 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4990 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4991 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004992 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4993 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4994 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4995 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4996 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 +01004997 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004998 ASSERT_EQ(SECOND_DISPLAY_ID, args.displayId);
4999}
5000
Michael Wrightd02c5b62014-02-10 15:10:22 -08005001// --- TouchInputMapperTest ---
5002
5003class TouchInputMapperTest : public InputMapperTest {
5004protected:
5005 static const int32_t RAW_X_MIN;
5006 static const int32_t RAW_X_MAX;
5007 static const int32_t RAW_Y_MIN;
5008 static const int32_t RAW_Y_MAX;
5009 static const int32_t RAW_TOUCH_MIN;
5010 static const int32_t RAW_TOUCH_MAX;
5011 static const int32_t RAW_TOOL_MIN;
5012 static const int32_t RAW_TOOL_MAX;
5013 static const int32_t RAW_PRESSURE_MIN;
5014 static const int32_t RAW_PRESSURE_MAX;
5015 static const int32_t RAW_ORIENTATION_MIN;
5016 static const int32_t RAW_ORIENTATION_MAX;
5017 static const int32_t RAW_DISTANCE_MIN;
5018 static const int32_t RAW_DISTANCE_MAX;
5019 static const int32_t RAW_TILT_MIN;
5020 static const int32_t RAW_TILT_MAX;
5021 static const int32_t RAW_ID_MIN;
5022 static const int32_t RAW_ID_MAX;
5023 static const int32_t RAW_SLOT_MIN;
5024 static const int32_t RAW_SLOT_MAX;
5025 static const float X_PRECISION;
5026 static const float Y_PRECISION;
Santos Cordonfa5cf462017-04-05 10:37:00 -07005027 static const float X_PRECISION_VIRTUAL;
5028 static const float Y_PRECISION_VIRTUAL;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005029
5030 static const float GEOMETRIC_SCALE;
Jason Gerecke489fda82012-09-07 17:19:40 -07005031 static const TouchAffineTransformation AFFINE_TRANSFORM;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005032
5033 static const VirtualKeyDefinition VIRTUAL_KEYS[2];
5034
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005035 const std::string UNIQUE_ID = "local:0";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005036 const std::string SECONDARY_UNIQUE_ID = "local:1";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005037
Michael Wrightd02c5b62014-02-10 15:10:22 -08005038 enum Axes {
5039 POSITION = 1 << 0,
5040 TOUCH = 1 << 1,
5041 TOOL = 1 << 2,
5042 PRESSURE = 1 << 3,
5043 ORIENTATION = 1 << 4,
5044 MINOR = 1 << 5,
5045 ID = 1 << 6,
5046 DISTANCE = 1 << 7,
5047 TILT = 1 << 8,
5048 SLOT = 1 << 9,
5049 TOOL_TYPE = 1 << 10,
5050 };
5051
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005052 void prepareDisplay(int32_t orientation, std::optional<uint8_t> port = NO_PORT);
5053 void prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port = NO_PORT);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005054 void prepareVirtualDisplay(int32_t orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005055 void prepareVirtualKeys();
Jason Gerecke489fda82012-09-07 17:19:40 -07005056 void prepareLocationCalibration();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005057 int32_t toRawX(float displayX);
5058 int32_t toRawY(float displayY);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005059 int32_t toRotatedRawX(float displayX);
5060 int32_t toRotatedRawY(float displayY);
Jason Gerecke489fda82012-09-07 17:19:40 -07005061 float toCookedX(float rawX, float rawY);
5062 float toCookedY(float rawX, float rawY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005063 float toDisplayX(int32_t rawX);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005064 float toDisplayX(int32_t rawX, int32_t displayWidth);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005065 float toDisplayY(int32_t rawY);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005066 float toDisplayY(int32_t rawY, int32_t displayHeight);
5067
Michael Wrightd02c5b62014-02-10 15:10:22 -08005068};
5069
5070const int32_t TouchInputMapperTest::RAW_X_MIN = 25;
5071const int32_t TouchInputMapperTest::RAW_X_MAX = 1019;
5072const int32_t TouchInputMapperTest::RAW_Y_MIN = 30;
5073const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009;
5074const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0;
5075const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31;
5076const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0;
5077const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15;
Michael Wrightaa449c92017-12-13 21:21:43 +00005078const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = 0;
5079const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = 255;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005080const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7;
5081const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
5082const int32_t TouchInputMapperTest::RAW_DISTANCE_MIN = 0;
5083const int32_t TouchInputMapperTest::RAW_DISTANCE_MAX = 7;
5084const int32_t TouchInputMapperTest::RAW_TILT_MIN = 0;
5085const int32_t TouchInputMapperTest::RAW_TILT_MAX = 150;
5086const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
5087const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
5088const int32_t TouchInputMapperTest::RAW_SLOT_MIN = 0;
5089const int32_t TouchInputMapperTest::RAW_SLOT_MAX = 9;
5090const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH;
5091const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT;
Santos Cordonfa5cf462017-04-05 10:37:00 -07005092const float TouchInputMapperTest::X_PRECISION_VIRTUAL =
5093 float(RAW_X_MAX - RAW_X_MIN + 1) / VIRTUAL_DISPLAY_WIDTH;
5094const float TouchInputMapperTest::Y_PRECISION_VIRTUAL =
5095 float(RAW_Y_MAX - RAW_Y_MIN + 1) / VIRTUAL_DISPLAY_HEIGHT;
Jason Gerecke489fda82012-09-07 17:19:40 -07005096const TouchAffineTransformation TouchInputMapperTest::AFFINE_TRANSFORM =
5097 TouchAffineTransformation(1, -2, 3, -4, 5, -6);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005098
5099const float TouchInputMapperTest::GEOMETRIC_SCALE =
5100 avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1),
5101 float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1));
5102
5103const VirtualKeyDefinition TouchInputMapperTest::VIRTUAL_KEYS[2] = {
5104 { KEY_HOME, 60, DISPLAY_HEIGHT + 15, 20, 20 },
5105 { KEY_MENU, DISPLAY_HEIGHT - 60, DISPLAY_WIDTH + 15, 20, 20 },
5106};
5107
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005108void TouchInputMapperTest::prepareDisplay(int32_t orientation, std::optional<uint8_t> port) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01005109 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
5110 port, ViewportType::INTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005111}
5112
5113void TouchInputMapperTest::prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port) {
5114 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
5115 DISPLAY_ORIENTATION_0, SECONDARY_UNIQUE_ID, port, type);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005116}
5117
Santos Cordonfa5cf462017-04-05 10:37:00 -07005118void TouchInputMapperTest::prepareVirtualDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01005119 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH, VIRTUAL_DISPLAY_HEIGHT,
5120 orientation, VIRTUAL_DISPLAY_UNIQUE_ID, NO_PORT,
5121 ViewportType::VIRTUAL);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005122}
5123
Michael Wrightd02c5b62014-02-10 15:10:22 -08005124void TouchInputMapperTest::prepareVirtualKeys() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005125 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[0]);
5126 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[1]);
5127 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
5128 mFakeEventHub->addKey(EVENTHUB_ID, KEY_MENU, 0, AKEYCODE_MENU, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005129}
5130
Jason Gerecke489fda82012-09-07 17:19:40 -07005131void TouchInputMapperTest::prepareLocationCalibration() {
5132 mFakePolicy->setTouchAffineTransformation(AFFINE_TRANSFORM);
5133}
5134
Michael Wrightd02c5b62014-02-10 15:10:22 -08005135int32_t TouchInputMapperTest::toRawX(float displayX) {
5136 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN);
5137}
5138
5139int32_t TouchInputMapperTest::toRawY(float displayY) {
5140 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN);
5141}
5142
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005143int32_t TouchInputMapperTest::toRotatedRawX(float displayX) {
5144 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_HEIGHT + RAW_X_MIN);
5145}
5146
5147int32_t TouchInputMapperTest::toRotatedRawY(float displayY) {
5148 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_WIDTH + RAW_Y_MIN);
5149}
5150
Jason Gerecke489fda82012-09-07 17:19:40 -07005151float TouchInputMapperTest::toCookedX(float rawX, float rawY) {
5152 AFFINE_TRANSFORM.applyTo(rawX, rawY);
5153 return rawX;
5154}
5155
5156float TouchInputMapperTest::toCookedY(float rawX, float rawY) {
5157 AFFINE_TRANSFORM.applyTo(rawX, rawY);
5158 return rawY;
5159}
5160
Michael Wrightd02c5b62014-02-10 15:10:22 -08005161float TouchInputMapperTest::toDisplayX(int32_t rawX) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005162 return toDisplayX(rawX, DISPLAY_WIDTH);
5163}
5164
5165float TouchInputMapperTest::toDisplayX(int32_t rawX, int32_t displayWidth) {
5166 return float(rawX - RAW_X_MIN) * displayWidth / (RAW_X_MAX - RAW_X_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005167}
5168
5169float TouchInputMapperTest::toDisplayY(int32_t rawY) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005170 return toDisplayY(rawY, DISPLAY_HEIGHT);
5171}
5172
5173float TouchInputMapperTest::toDisplayY(int32_t rawY, int32_t displayHeight) {
5174 return float(rawY - RAW_Y_MIN) * displayHeight / (RAW_Y_MAX - RAW_Y_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005175}
5176
5177
5178// --- SingleTouchInputMapperTest ---
5179
5180class SingleTouchInputMapperTest : public TouchInputMapperTest {
5181protected:
5182 void prepareButtons();
5183 void prepareAxes(int axes);
5184
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005185 void processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
5186 void processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
5187 void processUp(SingleTouchInputMapper& mappery);
5188 void processPressure(SingleTouchInputMapper& mapper, int32_t pressure);
5189 void processToolMajor(SingleTouchInputMapper& mapper, int32_t toolMajor);
5190 void processDistance(SingleTouchInputMapper& mapper, int32_t distance);
5191 void processTilt(SingleTouchInputMapper& mapper, int32_t tiltX, int32_t tiltY);
5192 void processKey(SingleTouchInputMapper& mapper, int32_t code, int32_t value);
5193 void processSync(SingleTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005194};
5195
5196void SingleTouchInputMapperTest::prepareButtons() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005197 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005198}
5199
5200void SingleTouchInputMapperTest::prepareAxes(int axes) {
5201 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005202 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
5203 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005204 }
5205 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005206 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MIN,
5207 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005208 }
5209 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005210 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TOOL_WIDTH, RAW_TOOL_MIN, RAW_TOOL_MAX, 0,
5211 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005212 }
5213 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005214 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_DISTANCE, RAW_DISTANCE_MIN,
5215 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005216 }
5217 if (axes & TILT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005218 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_X, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
5219 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_Y, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005220 }
5221}
5222
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005223void SingleTouchInputMapperTest::processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005224 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
5225 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
5226 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005227}
5228
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005229void SingleTouchInputMapperTest::processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005230 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::processUp(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005235 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005236}
5237
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005238void SingleTouchInputMapperTest::processPressure(SingleTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005239 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005240}
5241
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005242void SingleTouchInputMapperTest::processToolMajor(SingleTouchInputMapper& mapper,
5243 int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005244 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TOOL_WIDTH, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005245}
5246
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005247void SingleTouchInputMapperTest::processDistance(SingleTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005248 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005249}
5250
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005251void SingleTouchInputMapperTest::processTilt(SingleTouchInputMapper& mapper, int32_t tiltX,
5252 int32_t tiltY) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005253 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_X, tiltX);
5254 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_Y, tiltY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005255}
5256
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005257void SingleTouchInputMapperTest::processKey(SingleTouchInputMapper& mapper, int32_t code,
5258 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005259 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005260}
5261
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005262void SingleTouchInputMapperTest::processSync(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005263 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005264}
5265
Michael Wrightd02c5b62014-02-10 15:10:22 -08005266TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005267 prepareButtons();
5268 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005269 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005270
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005271 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005272}
5273
5274TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndIsACursor_ReturnsTouchPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005275 mFakeEventHub->addRelativeAxis(EVENTHUB_ID, REL_X);
5276 mFakeEventHub->addRelativeAxis(EVENTHUB_ID, REL_Y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005277 prepareButtons();
5278 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005279 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005280
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005281 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005282}
5283
5284TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchPad_ReturnsTouchPad) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005285 prepareButtons();
5286 prepareAxes(POSITION);
5287 addConfigurationProperty("touch.deviceType", "touchPad");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005288 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005289
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005290 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005291}
5292
5293TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005294 prepareButtons();
5295 prepareAxes(POSITION);
5296 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005297 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005298
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005299 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005300}
5301
5302TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005303 addConfigurationProperty("touch.deviceType", "touchScreen");
5304 prepareDisplay(DISPLAY_ORIENTATION_0);
5305 prepareButtons();
5306 prepareAxes(POSITION);
5307 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005308 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005309
5310 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005311 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005312
5313 // Virtual key is down.
5314 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5315 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5316 processDown(mapper, x, y);
5317 processSync(mapper);
5318 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5319
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005320 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005321
5322 // Virtual key is up.
5323 processUp(mapper);
5324 processSync(mapper);
5325 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5326
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005327 ASSERT_EQ(AKEY_STATE_UP, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005328}
5329
5330TEST_F(SingleTouchInputMapperTest, GetScanCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005331 addConfigurationProperty("touch.deviceType", "touchScreen");
5332 prepareDisplay(DISPLAY_ORIENTATION_0);
5333 prepareButtons();
5334 prepareAxes(POSITION);
5335 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005336 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005337
5338 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005339 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005340
5341 // Virtual key is down.
5342 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5343 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5344 processDown(mapper, x, y);
5345 processSync(mapper);
5346 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5347
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005348 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005349
5350 // Virtual key is up.
5351 processUp(mapper);
5352 processSync(mapper);
5353 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5354
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005355 ASSERT_EQ(AKEY_STATE_UP, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005356}
5357
5358TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005359 addConfigurationProperty("touch.deviceType", "touchScreen");
5360 prepareDisplay(DISPLAY_ORIENTATION_0);
5361 prepareButtons();
5362 prepareAxes(POSITION);
5363 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005364 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005365
5366 const int32_t keys[2] = { AKEYCODE_HOME, AKEYCODE_A };
5367 uint8_t flags[2] = { 0, 0 };
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005368 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, 2, keys, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005369 ASSERT_TRUE(flags[0]);
5370 ASSERT_FALSE(flags[1]);
5371}
5372
5373TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005374 addConfigurationProperty("touch.deviceType", "touchScreen");
5375 prepareDisplay(DISPLAY_ORIENTATION_0);
5376 prepareButtons();
5377 prepareAxes(POSITION);
5378 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005379 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005380
arthurhungdcef2dc2020-08-11 14:47:50 +08005381 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005382
5383 NotifyKeyArgs args;
5384
5385 // Press virtual key.
5386 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5387 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5388 processDown(mapper, x, y);
5389 processSync(mapper);
5390
5391 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
5392 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5393 ASSERT_EQ(DEVICE_ID, args.deviceId);
5394 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
5395 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
5396 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
5397 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5398 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5399 ASSERT_EQ(KEY_HOME, args.scanCode);
5400 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5401 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5402
5403 // Release virtual key.
5404 processUp(mapper);
5405 processSync(mapper);
5406
5407 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
5408 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5409 ASSERT_EQ(DEVICE_ID, args.deviceId);
5410 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
5411 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
5412 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
5413 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5414 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5415 ASSERT_EQ(KEY_HOME, args.scanCode);
5416 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5417 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5418
5419 // Should not have sent any motions.
5420 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5421}
5422
5423TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005424 addConfigurationProperty("touch.deviceType", "touchScreen");
5425 prepareDisplay(DISPLAY_ORIENTATION_0);
5426 prepareButtons();
5427 prepareAxes(POSITION);
5428 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005429 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005430
arthurhungdcef2dc2020-08-11 14:47:50 +08005431 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005432
5433 NotifyKeyArgs keyArgs;
5434
5435 // Press virtual key.
5436 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5437 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5438 processDown(mapper, x, y);
5439 processSync(mapper);
5440
5441 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5442 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5443 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5444 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5445 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5446 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5447 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags);
5448 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5449 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5450 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5451 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5452
5453 // Move out of bounds. This should generate a cancel and a pointer down since we moved
5454 // into the display area.
5455 y -= 100;
5456 processMove(mapper, x, y);
5457 processSync(mapper);
5458
5459 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5460 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5461 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5462 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5463 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5464 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5465 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
5466 | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags);
5467 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5468 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5469 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5470 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5471
5472 NotifyMotionArgs motionArgs;
5473 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5474 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5475 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5476 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5477 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5478 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5479 ASSERT_EQ(0, motionArgs.flags);
5480 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5481 ASSERT_EQ(0, motionArgs.buttonState);
5482 ASSERT_EQ(0, motionArgs.edgeFlags);
5483 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5484 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5485 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5486 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5487 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5488 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5489 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5490 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5491
5492 // Keep moving out of bounds. Should generate a pointer move.
5493 y -= 50;
5494 processMove(mapper, x, y);
5495 processSync(mapper);
5496
5497 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5498 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5499 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5500 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5501 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5502 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5503 ASSERT_EQ(0, motionArgs.flags);
5504 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5505 ASSERT_EQ(0, motionArgs.buttonState);
5506 ASSERT_EQ(0, motionArgs.edgeFlags);
5507 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5508 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5509 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5510 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5511 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5512 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5513 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5514 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5515
5516 // Release out of bounds. Should generate a pointer up.
5517 processUp(mapper);
5518 processSync(mapper);
5519
5520 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5521 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5522 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5523 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5524 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5525 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5526 ASSERT_EQ(0, motionArgs.flags);
5527 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5528 ASSERT_EQ(0, motionArgs.buttonState);
5529 ASSERT_EQ(0, motionArgs.edgeFlags);
5530 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5531 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5532 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5533 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5534 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5535 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5536 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5537 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5538
5539 // Should not have sent any more keys or motions.
5540 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5541 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5542}
5543
5544TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005545 addConfigurationProperty("touch.deviceType", "touchScreen");
5546 prepareDisplay(DISPLAY_ORIENTATION_0);
5547 prepareButtons();
5548 prepareAxes(POSITION);
5549 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005550 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005551
arthurhungdcef2dc2020-08-11 14:47:50 +08005552 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005553
5554 NotifyMotionArgs motionArgs;
5555
5556 // Initially go down out of bounds.
5557 int32_t x = -10;
5558 int32_t y = -10;
5559 processDown(mapper, x, y);
5560 processSync(mapper);
5561
5562 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5563
5564 // Move into the display area. Should generate a pointer down.
5565 x = 50;
5566 y = 75;
5567 processMove(mapper, x, y);
5568 processSync(mapper);
5569
5570 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5571 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5572 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5573 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5574 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5575 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5576 ASSERT_EQ(0, motionArgs.flags);
5577 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5578 ASSERT_EQ(0, motionArgs.buttonState);
5579 ASSERT_EQ(0, motionArgs.edgeFlags);
5580 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5581 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5582 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5583 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5584 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5585 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5586 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5587 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5588
5589 // Release. Should generate a pointer up.
5590 processUp(mapper);
5591 processSync(mapper);
5592
5593 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5594 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5595 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5596 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5597 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5598 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5599 ASSERT_EQ(0, motionArgs.flags);
5600 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5601 ASSERT_EQ(0, motionArgs.buttonState);
5602 ASSERT_EQ(0, motionArgs.edgeFlags);
5603 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5604 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5605 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5606 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5607 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5608 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5609 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5610 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5611
5612 // Should not have sent any more keys or motions.
5613 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5614 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5615}
5616
Santos Cordonfa5cf462017-04-05 10:37:00 -07005617TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture_VirtualDisplay) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005618 addConfigurationProperty("touch.deviceType", "touchScreen");
5619 addConfigurationProperty("touch.displayId", VIRTUAL_DISPLAY_UNIQUE_ID);
5620
5621 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
5622 prepareButtons();
5623 prepareAxes(POSITION);
5624 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005625 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Santos Cordonfa5cf462017-04-05 10:37:00 -07005626
arthurhungdcef2dc2020-08-11 14:47:50 +08005627 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005628
5629 NotifyMotionArgs motionArgs;
5630
5631 // Down.
5632 int32_t x = 100;
5633 int32_t y = 125;
5634 processDown(mapper, x, y);
5635 processSync(mapper);
5636
5637 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5638 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5639 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5640 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5641 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5642 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5643 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5644 ASSERT_EQ(0, motionArgs.flags);
5645 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5646 ASSERT_EQ(0, motionArgs.buttonState);
5647 ASSERT_EQ(0, motionArgs.edgeFlags);
5648 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5649 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5650 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5651 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5652 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5653 1, 0, 0, 0, 0, 0, 0, 0));
5654 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5655 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5656 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5657
5658 // Move.
5659 x += 50;
5660 y += 75;
5661 processMove(mapper, x, y);
5662 processSync(mapper);
5663
5664 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5665 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5666 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5667 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5668 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5669 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5670 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5671 ASSERT_EQ(0, motionArgs.flags);
5672 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5673 ASSERT_EQ(0, motionArgs.buttonState);
5674 ASSERT_EQ(0, motionArgs.edgeFlags);
5675 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5676 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5677 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5678 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5679 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5680 1, 0, 0, 0, 0, 0, 0, 0));
5681 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5682 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5683 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5684
5685 // Up.
5686 processUp(mapper);
5687 processSync(mapper);
5688
5689 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5690 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5691 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5692 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5693 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5694 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5695 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5696 ASSERT_EQ(0, motionArgs.flags);
5697 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5698 ASSERT_EQ(0, motionArgs.buttonState);
5699 ASSERT_EQ(0, motionArgs.edgeFlags);
5700 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5701 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5702 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5703 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5704 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5705 1, 0, 0, 0, 0, 0, 0, 0));
5706 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5707 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5708 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5709
5710 // Should not have sent any more keys or motions.
5711 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5712 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5713}
5714
Michael Wrightd02c5b62014-02-10 15:10:22 -08005715TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005716 addConfigurationProperty("touch.deviceType", "touchScreen");
5717 prepareDisplay(DISPLAY_ORIENTATION_0);
5718 prepareButtons();
5719 prepareAxes(POSITION);
5720 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005721 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005722
arthurhungdcef2dc2020-08-11 14:47:50 +08005723 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005724
5725 NotifyMotionArgs motionArgs;
5726
5727 // Down.
5728 int32_t x = 100;
5729 int32_t y = 125;
5730 processDown(mapper, x, y);
5731 processSync(mapper);
5732
5733 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5734 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5735 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5736 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5737 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5738 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5739 ASSERT_EQ(0, motionArgs.flags);
5740 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5741 ASSERT_EQ(0, motionArgs.buttonState);
5742 ASSERT_EQ(0, motionArgs.edgeFlags);
5743 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5744 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5745 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5746 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5747 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5748 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5749 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5750 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5751
5752 // Move.
5753 x += 50;
5754 y += 75;
5755 processMove(mapper, x, y);
5756 processSync(mapper);
5757
5758 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5759 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5760 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5761 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5762 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5763 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5764 ASSERT_EQ(0, motionArgs.flags);
5765 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5766 ASSERT_EQ(0, motionArgs.buttonState);
5767 ASSERT_EQ(0, motionArgs.edgeFlags);
5768 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5769 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5770 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5771 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5772 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5773 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5774 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5775 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5776
5777 // Up.
5778 processUp(mapper);
5779 processSync(mapper);
5780
5781 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5782 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5783 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5784 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5785 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5786 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5787 ASSERT_EQ(0, motionArgs.flags);
5788 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5789 ASSERT_EQ(0, motionArgs.buttonState);
5790 ASSERT_EQ(0, motionArgs.edgeFlags);
5791 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5792 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5793 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5794 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5795 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5796 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5797 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5798 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5799
5800 // Should not have sent any more keys or motions.
5801 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5802 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5803}
5804
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005805TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_DoesNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005806 addConfigurationProperty("touch.deviceType", "touchScreen");
5807 prepareButtons();
5808 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005809 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
5810 // need to be rotated. Touchscreens are orientation-aware by default.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005811 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005812
5813 NotifyMotionArgs args;
5814
5815 // Rotation 90.
5816 prepareDisplay(DISPLAY_ORIENTATION_90);
5817 processDown(mapper, toRawX(50), toRawY(75));
5818 processSync(mapper);
5819
5820 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5821 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5822 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5823
5824 processUp(mapper);
5825 processSync(mapper);
5826 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5827}
5828
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005829TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_RotatesMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005830 addConfigurationProperty("touch.deviceType", "touchScreen");
5831 prepareButtons();
5832 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005833 // Since InputReader works in the un-rotated coordinate space, only devices that are not
5834 // orientation-aware are affected by display rotation.
5835 addConfigurationProperty("touch.orientationAware", "0");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005836 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005837
5838 NotifyMotionArgs args;
5839
5840 // Rotation 0.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005841 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005842 prepareDisplay(DISPLAY_ORIENTATION_0);
5843 processDown(mapper, toRawX(50), toRawY(75));
5844 processSync(mapper);
5845
5846 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5847 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5848 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5849
5850 processUp(mapper);
5851 processSync(mapper);
5852 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5853
5854 // Rotation 90.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005855 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005856 prepareDisplay(DISPLAY_ORIENTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005857 processDown(mapper, toRawX(75), RAW_Y_MAX - toRawY(50) + RAW_Y_MIN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005858 processSync(mapper);
5859
5860 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5861 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5862 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5863
5864 processUp(mapper);
5865 processSync(mapper);
5866 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5867
5868 // Rotation 180.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005869 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005870 prepareDisplay(DISPLAY_ORIENTATION_180);
5871 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
5872 processSync(mapper);
5873
5874 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5875 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5876 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5877
5878 processUp(mapper);
5879 processSync(mapper);
5880 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5881
5882 // Rotation 270.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005883 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005884 prepareDisplay(DISPLAY_ORIENTATION_270);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005885 processDown(mapper, RAW_X_MAX - toRawX(75) + RAW_X_MIN, toRawY(50));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005886 processSync(mapper);
5887
5888 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5889 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5890 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5891
5892 processUp(mapper);
5893 processSync(mapper);
5894 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5895}
5896
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005897TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation0_RotatesMotions) {
5898 addConfigurationProperty("touch.deviceType", "touchScreen");
5899 prepareButtons();
5900 prepareAxes(POSITION);
5901 addConfigurationProperty("touch.orientationAware", "1");
5902 addConfigurationProperty("touch.orientation", "ORIENTATION_0");
5903 clearViewports();
5904 prepareDisplay(DISPLAY_ORIENTATION_0);
5905 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5906 NotifyMotionArgs args;
5907
5908 // Orientation 0.
5909 processDown(mapper, toRawX(50), toRawY(75));
5910 processSync(mapper);
5911
5912 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5913 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5914 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5915
5916 processUp(mapper);
5917 processSync(mapper);
5918 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5919}
5920
5921TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation90_RotatesMotions) {
5922 addConfigurationProperty("touch.deviceType", "touchScreen");
5923 prepareButtons();
5924 prepareAxes(POSITION);
5925 addConfigurationProperty("touch.orientationAware", "1");
5926 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
5927 clearViewports();
5928 prepareDisplay(DISPLAY_ORIENTATION_0);
5929 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5930 NotifyMotionArgs args;
5931
5932 // Orientation 90.
5933 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
5934 processSync(mapper);
5935
5936 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5937 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5938 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5939
5940 processUp(mapper);
5941 processSync(mapper);
5942 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5943}
5944
5945TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation180_RotatesMotions) {
5946 addConfigurationProperty("touch.deviceType", "touchScreen");
5947 prepareButtons();
5948 prepareAxes(POSITION);
5949 addConfigurationProperty("touch.orientationAware", "1");
5950 addConfigurationProperty("touch.orientation", "ORIENTATION_180");
5951 clearViewports();
5952 prepareDisplay(DISPLAY_ORIENTATION_0);
5953 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5954 NotifyMotionArgs args;
5955
5956 // Orientation 180.
5957 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
5958 processSync(mapper);
5959
5960 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5961 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5962 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5963
5964 processUp(mapper);
5965 processSync(mapper);
5966 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5967}
5968
5969TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation270_RotatesMotions) {
5970 addConfigurationProperty("touch.deviceType", "touchScreen");
5971 prepareButtons();
5972 prepareAxes(POSITION);
5973 addConfigurationProperty("touch.orientationAware", "1");
5974 addConfigurationProperty("touch.orientation", "ORIENTATION_270");
5975 clearViewports();
5976 prepareDisplay(DISPLAY_ORIENTATION_0);
5977 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5978 NotifyMotionArgs args;
5979
5980 // Orientation 270.
5981 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
5982 processSync(mapper);
5983
5984 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5985 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5986 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5987
5988 processUp(mapper);
5989 processSync(mapper);
5990 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5991}
5992
5993TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationSpecified_RotatesMotionWithDisplay) {
5994 addConfigurationProperty("touch.deviceType", "touchScreen");
5995 prepareButtons();
5996 prepareAxes(POSITION);
5997 // Since InputReader works in the un-rotated coordinate space, only devices that are not
5998 // orientation-aware are affected by display rotation.
5999 addConfigurationProperty("touch.orientationAware", "0");
6000 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
6001 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6002
6003 NotifyMotionArgs args;
6004
6005 // Orientation 90, Rotation 0.
6006 clearViewports();
6007 prepareDisplay(DISPLAY_ORIENTATION_0);
6008 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
6009 processSync(mapper);
6010
6011 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6012 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6013 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6014
6015 processUp(mapper);
6016 processSync(mapper);
6017 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6018
6019 // Orientation 90, Rotation 90.
6020 clearViewports();
6021 prepareDisplay(DISPLAY_ORIENTATION_90);
6022 processDown(mapper, toRotatedRawX(50), toRotatedRawY(75));
6023 processSync(mapper);
6024
6025 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6026 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6027 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6028
6029 processUp(mapper);
6030 processSync(mapper);
6031 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6032
6033 // Orientation 90, Rotation 180.
6034 clearViewports();
6035 prepareDisplay(DISPLAY_ORIENTATION_180);
6036 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
6037 processSync(mapper);
6038
6039 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6040 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6041 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6042
6043 processUp(mapper);
6044 processSync(mapper);
6045 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6046
6047 // Orientation 90, Rotation 270.
6048 clearViewports();
6049 prepareDisplay(DISPLAY_ORIENTATION_270);
6050 processDown(mapper, RAW_X_MAX - toRotatedRawX(50) + RAW_X_MIN,
6051 RAW_Y_MAX - toRotatedRawY(75) + RAW_Y_MIN);
6052 processSync(mapper);
6053
6054 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6055 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6056 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6057
6058 processUp(mapper);
6059 processSync(mapper);
6060 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6061}
6062
Michael Wrightd02c5b62014-02-10 15:10:22 -08006063TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006064 addConfigurationProperty("touch.deviceType", "touchScreen");
6065 prepareDisplay(DISPLAY_ORIENTATION_0);
6066 prepareButtons();
6067 prepareAxes(POSITION | PRESSURE | TOOL | DISTANCE | TILT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006068 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006069
6070 // These calculations are based on the input device calibration documentation.
6071 int32_t rawX = 100;
6072 int32_t rawY = 200;
6073 int32_t rawPressure = 10;
6074 int32_t rawToolMajor = 12;
6075 int32_t rawDistance = 2;
6076 int32_t rawTiltX = 30;
6077 int32_t rawTiltY = 110;
6078
6079 float x = toDisplayX(rawX);
6080 float y = toDisplayY(rawY);
6081 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
6082 float size = float(rawToolMajor) / RAW_TOOL_MAX;
6083 float tool = float(rawToolMajor) * GEOMETRIC_SCALE;
6084 float distance = float(rawDistance);
6085
6086 float tiltCenter = (RAW_TILT_MAX + RAW_TILT_MIN) * 0.5f;
6087 float tiltScale = M_PI / 180;
6088 float tiltXAngle = (rawTiltX - tiltCenter) * tiltScale;
6089 float tiltYAngle = (rawTiltY - tiltCenter) * tiltScale;
6090 float orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
6091 float tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
6092
6093 processDown(mapper, rawX, rawY);
6094 processPressure(mapper, rawPressure);
6095 processToolMajor(mapper, rawToolMajor);
6096 processDistance(mapper, rawDistance);
6097 processTilt(mapper, rawTiltX, rawTiltY);
6098 processSync(mapper);
6099
6100 NotifyMotionArgs args;
6101 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6102 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6103 x, y, pressure, size, tool, tool, tool, tool, orientation, distance));
6104 ASSERT_EQ(tilt, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TILT));
6105}
6106
Jason Gerecke489fda82012-09-07 17:19:40 -07006107TEST_F(SingleTouchInputMapperTest, Process_XYAxes_AffineCalibration) {
Jason Gerecke489fda82012-09-07 17:19:40 -07006108 addConfigurationProperty("touch.deviceType", "touchScreen");
6109 prepareDisplay(DISPLAY_ORIENTATION_0);
6110 prepareLocationCalibration();
6111 prepareButtons();
6112 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006113 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Jason Gerecke489fda82012-09-07 17:19:40 -07006114
6115 int32_t rawX = 100;
6116 int32_t rawY = 200;
6117
6118 float x = toDisplayX(toCookedX(rawX, rawY));
6119 float y = toDisplayY(toCookedY(rawX, rawY));
6120
6121 processDown(mapper, rawX, rawY);
6122 processSync(mapper);
6123
6124 NotifyMotionArgs args;
6125 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6126 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6127 x, y, 1, 0, 0, 0, 0, 0, 0, 0));
6128}
6129
Michael Wrightd02c5b62014-02-10 15:10:22 -08006130TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006131 addConfigurationProperty("touch.deviceType", "touchScreen");
6132 prepareDisplay(DISPLAY_ORIENTATION_0);
6133 prepareButtons();
6134 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006135 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006136
6137 NotifyMotionArgs motionArgs;
6138 NotifyKeyArgs keyArgs;
6139
6140 processDown(mapper, 100, 200);
6141 processSync(mapper);
6142 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6143 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6144 ASSERT_EQ(0, motionArgs.buttonState);
6145
6146 // press BTN_LEFT, release BTN_LEFT
6147 processKey(mapper, BTN_LEFT, 1);
6148 processSync(mapper);
6149 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6150 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6151 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6152
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006153 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6154 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6155 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6156
Michael Wrightd02c5b62014-02-10 15:10:22 -08006157 processKey(mapper, BTN_LEFT, 0);
6158 processSync(mapper);
6159 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006160 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006161 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006162
6163 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006164 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006165 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006166
6167 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
6168 processKey(mapper, BTN_RIGHT, 1);
6169 processKey(mapper, BTN_MIDDLE, 1);
6170 processSync(mapper);
6171 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6172 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6173 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6174 motionArgs.buttonState);
6175
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006176 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6177 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6178 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
6179
6180 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6181 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6182 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6183 motionArgs.buttonState);
6184
Michael Wrightd02c5b62014-02-10 15:10:22 -08006185 processKey(mapper, BTN_RIGHT, 0);
6186 processSync(mapper);
6187 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006188 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006189 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006190
6191 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006192 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006193 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006194
6195 processKey(mapper, BTN_MIDDLE, 0);
6196 processSync(mapper);
6197 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006198 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006199 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006200
6201 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006202 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006203 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006204
6205 // press BTN_BACK, release BTN_BACK
6206 processKey(mapper, BTN_BACK, 1);
6207 processSync(mapper);
6208 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6209 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6210 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006211
Michael Wrightd02c5b62014-02-10 15:10:22 -08006212 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006213 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006214 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6215
6216 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6217 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6218 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006219
6220 processKey(mapper, BTN_BACK, 0);
6221 processSync(mapper);
6222 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006223 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006224 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006225
6226 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006227 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006228 ASSERT_EQ(0, motionArgs.buttonState);
6229
Michael Wrightd02c5b62014-02-10 15:10:22 -08006230 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6231 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6232 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6233
6234 // press BTN_SIDE, release BTN_SIDE
6235 processKey(mapper, BTN_SIDE, 1);
6236 processSync(mapper);
6237 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6238 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6239 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006240
Michael Wrightd02c5b62014-02-10 15:10:22 -08006241 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006242 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006243 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6244
6245 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6246 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6247 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006248
6249 processKey(mapper, BTN_SIDE, 0);
6250 processSync(mapper);
6251 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006252 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006253 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006254
6255 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006256 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006257 ASSERT_EQ(0, motionArgs.buttonState);
6258
Michael Wrightd02c5b62014-02-10 15:10:22 -08006259 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6260 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6261 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6262
6263 // press BTN_FORWARD, release BTN_FORWARD
6264 processKey(mapper, BTN_FORWARD, 1);
6265 processSync(mapper);
6266 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6267 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6268 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006269
Michael Wrightd02c5b62014-02-10 15:10:22 -08006270 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006271 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006272 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6273
6274 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6275 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6276 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006277
6278 processKey(mapper, BTN_FORWARD, 0);
6279 processSync(mapper);
6280 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006281 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006282 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006283
6284 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006285 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006286 ASSERT_EQ(0, motionArgs.buttonState);
6287
Michael Wrightd02c5b62014-02-10 15:10:22 -08006288 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6289 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6290 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6291
6292 // press BTN_EXTRA, release BTN_EXTRA
6293 processKey(mapper, BTN_EXTRA, 1);
6294 processSync(mapper);
6295 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6296 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6297 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006298
Michael Wrightd02c5b62014-02-10 15:10:22 -08006299 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006300 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006301 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6302
6303 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6304 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6305 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006306
6307 processKey(mapper, BTN_EXTRA, 0);
6308 processSync(mapper);
6309 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006310 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006311 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006312
6313 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006314 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006315 ASSERT_EQ(0, motionArgs.buttonState);
6316
Michael Wrightd02c5b62014-02-10 15:10:22 -08006317 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6318 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6319 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6320
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006321 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6322
Michael Wrightd02c5b62014-02-10 15:10:22 -08006323 // press BTN_STYLUS, release BTN_STYLUS
6324 processKey(mapper, BTN_STYLUS, 1);
6325 processSync(mapper);
6326 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6327 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006328 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
6329
6330 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6331 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6332 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006333
6334 processKey(mapper, BTN_STYLUS, 0);
6335 processSync(mapper);
6336 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006337 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006338 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006339
6340 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006341 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006342 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006343
6344 // press BTN_STYLUS2, release BTN_STYLUS2
6345 processKey(mapper, BTN_STYLUS2, 1);
6346 processSync(mapper);
6347 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6348 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006349 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
6350
6351 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6352 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6353 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006354
6355 processKey(mapper, BTN_STYLUS2, 0);
6356 processSync(mapper);
6357 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006358 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006359 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006360
6361 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006362 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006363 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006364
6365 // release touch
6366 processUp(mapper);
6367 processSync(mapper);
6368 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6369 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6370 ASSERT_EQ(0, motionArgs.buttonState);
6371}
6372
6373TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006374 addConfigurationProperty("touch.deviceType", "touchScreen");
6375 prepareDisplay(DISPLAY_ORIENTATION_0);
6376 prepareButtons();
6377 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006378 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006379
6380 NotifyMotionArgs motionArgs;
6381
6382 // default tool type is finger
6383 processDown(mapper, 100, 200);
6384 processSync(mapper);
6385 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6386 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6387 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6388
6389 // eraser
6390 processKey(mapper, BTN_TOOL_RUBBER, 1);
6391 processSync(mapper);
6392 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6393 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6394 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6395
6396 // stylus
6397 processKey(mapper, BTN_TOOL_RUBBER, 0);
6398 processKey(mapper, BTN_TOOL_PEN, 1);
6399 processSync(mapper);
6400 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6401 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6402 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6403
6404 // brush
6405 processKey(mapper, BTN_TOOL_PEN, 0);
6406 processKey(mapper, BTN_TOOL_BRUSH, 1);
6407 processSync(mapper);
6408 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6409 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6410 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6411
6412 // pencil
6413 processKey(mapper, BTN_TOOL_BRUSH, 0);
6414 processKey(mapper, BTN_TOOL_PENCIL, 1);
6415 processSync(mapper);
6416 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6417 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6418 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6419
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08006420 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08006421 processKey(mapper, BTN_TOOL_PENCIL, 0);
6422 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
6423 processSync(mapper);
6424 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6425 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6426 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6427
6428 // mouse
6429 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
6430 processKey(mapper, BTN_TOOL_MOUSE, 1);
6431 processSync(mapper);
6432 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6433 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6434 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6435
6436 // lens
6437 processKey(mapper, BTN_TOOL_MOUSE, 0);
6438 processKey(mapper, BTN_TOOL_LENS, 1);
6439 processSync(mapper);
6440 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6441 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6442 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6443
6444 // double-tap
6445 processKey(mapper, BTN_TOOL_LENS, 0);
6446 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
6447 processSync(mapper);
6448 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6449 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6450 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6451
6452 // triple-tap
6453 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
6454 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
6455 processSync(mapper);
6456 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6457 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6458 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6459
6460 // quad-tap
6461 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
6462 processKey(mapper, BTN_TOOL_QUADTAP, 1);
6463 processSync(mapper);
6464 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6465 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6466 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6467
6468 // finger
6469 processKey(mapper, BTN_TOOL_QUADTAP, 0);
6470 processKey(mapper, BTN_TOOL_FINGER, 1);
6471 processSync(mapper);
6472 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6473 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6474 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6475
6476 // stylus trumps finger
6477 processKey(mapper, BTN_TOOL_PEN, 1);
6478 processSync(mapper);
6479 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6480 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6481 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6482
6483 // eraser trumps stylus
6484 processKey(mapper, BTN_TOOL_RUBBER, 1);
6485 processSync(mapper);
6486 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6487 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6488 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6489
6490 // mouse trumps eraser
6491 processKey(mapper, BTN_TOOL_MOUSE, 1);
6492 processSync(mapper);
6493 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6494 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6495 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6496
6497 // back to default tool type
6498 processKey(mapper, BTN_TOOL_MOUSE, 0);
6499 processKey(mapper, BTN_TOOL_RUBBER, 0);
6500 processKey(mapper, BTN_TOOL_PEN, 0);
6501 processKey(mapper, BTN_TOOL_FINGER, 0);
6502 processSync(mapper);
6503 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6504 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6505 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6506}
6507
6508TEST_F(SingleTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006509 addConfigurationProperty("touch.deviceType", "touchScreen");
6510 prepareDisplay(DISPLAY_ORIENTATION_0);
6511 prepareButtons();
6512 prepareAxes(POSITION);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006513 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_FINGER, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006514 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006515
6516 NotifyMotionArgs motionArgs;
6517
6518 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
6519 processKey(mapper, BTN_TOOL_FINGER, 1);
6520 processMove(mapper, 100, 200);
6521 processSync(mapper);
6522 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6523 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6524 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6525 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6526
6527 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6528 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, 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 // move a little
6533 processMove(mapper, 150, 250);
6534 processSync(mapper);
6535 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6536 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6537 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6538 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6539
6540 // down when BTN_TOUCH is pressed, pressure defaults to 1
6541 processKey(mapper, BTN_TOUCH, 1);
6542 processSync(mapper);
6543 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6544 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6545 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6546 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6547
6548 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6549 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6550 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6551 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6552
6553 // up when BTN_TOUCH is released, hover restored
6554 processKey(mapper, BTN_TOUCH, 0);
6555 processSync(mapper);
6556 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6557 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6558 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6559 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6560
6561 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6562 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6563 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6564 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6565
6566 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6567 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, 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 // exit hover when pointer goes away
6572 processKey(mapper, BTN_TOOL_FINGER, 0);
6573 processSync(mapper);
6574 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6575 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6576 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6577 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6578}
6579
6580TEST_F(SingleTouchInputMapperTest, Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006581 addConfigurationProperty("touch.deviceType", "touchScreen");
6582 prepareDisplay(DISPLAY_ORIENTATION_0);
6583 prepareButtons();
6584 prepareAxes(POSITION | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006585 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006586
6587 NotifyMotionArgs motionArgs;
6588
6589 // initially hovering because pressure is 0
6590 processDown(mapper, 100, 200);
6591 processPressure(mapper, 0);
6592 processSync(mapper);
6593 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6594 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6595 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6596 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6597
6598 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6599 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, 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 // move a little
6604 processMove(mapper, 150, 250);
6605 processSync(mapper);
6606 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6607 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6608 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6609 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6610
6611 // down when pressure is non-zero
6612 processPressure(mapper, RAW_PRESSURE_MAX);
6613 processSync(mapper);
6614 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6615 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6616 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6617 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6618
6619 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6620 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6621 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6622 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6623
6624 // up when pressure becomes 0, hover restored
6625 processPressure(mapper, 0);
6626 processSync(mapper);
6627 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6628 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6629 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6630 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6631
6632 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6633 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6634 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6635 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6636
6637 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6638 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, 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 // exit hover when pointer goes away
6643 processUp(mapper);
6644 processSync(mapper);
6645 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6646 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6647 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6648 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6649}
6650
lilinnan687e58f2022-07-19 16:00:50 +08006651TEST_F(SingleTouchInputMapperTest,
6652 Process_WhenViewportDisplayIdChanged_TouchIsCanceledAndDeviceIsReset) {
6653 addConfigurationProperty("touch.deviceType", "touchScreen");
6654 prepareDisplay(DISPLAY_ORIENTATION_0);
6655 prepareButtons();
6656 prepareAxes(POSITION);
6657 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6658 NotifyMotionArgs motionArgs;
6659
6660 // Down.
6661 int32_t x = 100;
6662 int32_t y = 200;
6663 processDown(mapper, x, y);
6664 processSync(mapper);
6665
6666 // We should receive a down event
6667 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6668 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6669
6670 // Change display id
6671 clearViewports();
6672 prepareSecondaryDisplay(ViewportType::INTERNAL);
6673
6674 // We should receive a cancel event
6675 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6676 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
6677 // Then receive reset called
6678 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6679}
6680
Prabir Pradhan5632d622021-09-06 07:57:20 -07006681// --- TouchDisplayProjectionTest ---
6682
6683class TouchDisplayProjectionTest : public SingleTouchInputMapperTest {
6684public:
6685 // The values inside DisplayViewport are expected to be pre-rotated. This updates the current
6686 // DisplayViewport to pre-rotate the values. The viewport's physical display will be set to the
6687 // rotated equivalent of the given un-rotated physical display bounds.
6688 void configurePhysicalDisplay(int32_t orientation, Rect naturalPhysicalDisplay) {
6689 uint32_t inverseRotationFlags;
6690 auto width = DISPLAY_WIDTH;
6691 auto height = DISPLAY_HEIGHT;
6692 switch (orientation) {
6693 case DISPLAY_ORIENTATION_90:
6694 inverseRotationFlags = ui::Transform::ROT_270;
6695 std::swap(width, height);
6696 break;
6697 case DISPLAY_ORIENTATION_180:
6698 inverseRotationFlags = ui::Transform::ROT_180;
6699 break;
6700 case DISPLAY_ORIENTATION_270:
6701 inverseRotationFlags = ui::Transform::ROT_90;
6702 std::swap(width, height);
6703 break;
6704 case DISPLAY_ORIENTATION_0:
6705 inverseRotationFlags = ui::Transform::ROT_0;
6706 break;
6707 default:
6708 FAIL() << "Invalid orientation: " << orientation;
6709 }
6710
6711 const ui::Transform rotation(inverseRotationFlags, width, height);
6712 const Rect rotatedPhysicalDisplay = rotation.transform(naturalPhysicalDisplay);
6713
6714 std::optional<DisplayViewport> internalViewport =
6715 *mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6716 DisplayViewport& v = *internalViewport;
6717 v.displayId = DISPLAY_ID;
6718 v.orientation = orientation;
6719
6720 v.logicalLeft = 0;
6721 v.logicalTop = 0;
6722 v.logicalRight = 100;
6723 v.logicalBottom = 100;
6724
6725 v.physicalLeft = rotatedPhysicalDisplay.left;
6726 v.physicalTop = rotatedPhysicalDisplay.top;
6727 v.physicalRight = rotatedPhysicalDisplay.right;
6728 v.physicalBottom = rotatedPhysicalDisplay.bottom;
6729
6730 v.deviceWidth = width;
6731 v.deviceHeight = height;
6732
6733 v.isActive = true;
6734 v.uniqueId = UNIQUE_ID;
6735 v.type = ViewportType::INTERNAL;
6736 mFakePolicy->updateViewport(v);
6737 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
6738 }
6739
6740 void assertReceivedMove(const Point& point) {
6741 NotifyMotionArgs motionArgs;
6742 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6743 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6744 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6745 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], point.x, point.y,
6746 1, 0, 0, 0, 0, 0, 0, 0));
6747 }
6748};
6749
6750TEST_F(TouchDisplayProjectionTest, IgnoresTouchesOutsidePhysicalDisplay) {
6751 addConfigurationProperty("touch.deviceType", "touchScreen");
6752 prepareDisplay(DISPLAY_ORIENTATION_0);
6753
6754 prepareButtons();
6755 prepareAxes(POSITION);
6756 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6757
6758 NotifyMotionArgs motionArgs;
6759
6760 // Configure the DisplayViewport such that the logical display maps to a subsection of
6761 // the display panel called the physical display. Here, the physical display is bounded by the
6762 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
6763 static const Rect kPhysicalDisplay{10, 20, 70, 160};
6764 static const std::array<Point, 6> kPointsOutsidePhysicalDisplay{
6765 {{-10, -10}, {0, 0}, {5, 100}, {50, 15}, {75, 100}, {50, 165}}};
6766
6767 for (auto orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90, DISPLAY_ORIENTATION_180,
6768 DISPLAY_ORIENTATION_270}) {
6769 configurePhysicalDisplay(orientation, kPhysicalDisplay);
6770
6771 // Touches outside the physical display should be ignored, and should not generate any
6772 // events. Ensure touches at the following points that lie outside of the physical display
6773 // area do not generate any events.
6774 for (const auto& point : kPointsOutsidePhysicalDisplay) {
6775 processDown(mapper, toRawX(point.x), toRawY(point.y));
6776 processSync(mapper);
6777 processUp(mapper);
6778 processSync(mapper);
6779 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled())
6780 << "Unexpected event generated for touch outside physical display at point: "
6781 << point.x << ", " << point.y;
6782 }
6783 }
6784}
6785
6786TEST_F(TouchDisplayProjectionTest, EmitsTouchDownAfterEnteringPhysicalDisplay) {
6787 addConfigurationProperty("touch.deviceType", "touchScreen");
6788 prepareDisplay(DISPLAY_ORIENTATION_0);
6789
6790 prepareButtons();
6791 prepareAxes(POSITION);
6792 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6793
6794 NotifyMotionArgs motionArgs;
6795
6796 // Configure the DisplayViewport such that the logical display maps to a subsection of
6797 // the display panel called the physical display. Here, the physical display is bounded by the
6798 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
6799 static const Rect kPhysicalDisplay{10, 20, 70, 160};
6800
6801 for (auto orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90, DISPLAY_ORIENTATION_180,
6802 DISPLAY_ORIENTATION_270}) {
6803 configurePhysicalDisplay(orientation, kPhysicalDisplay);
6804
6805 // Touches that start outside the physical display should be ignored until it enters the
6806 // physical display bounds, at which point it should generate a down event. Start a touch at
6807 // the point (5, 100), which is outside the physical display bounds.
6808 static const Point kOutsidePoint{5, 100};
6809 processDown(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
6810 processSync(mapper);
6811 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6812
6813 // Move the touch into the physical display area. This should generate a pointer down.
6814 processMove(mapper, toRawX(11), toRawY(21));
6815 processSync(mapper);
6816 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6817 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6818 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6819 ASSERT_NO_FATAL_FAILURE(
6820 assertPointerCoords(motionArgs.pointerCoords[0], 11, 21, 1, 0, 0, 0, 0, 0, 0, 0));
6821
6822 // Move the touch inside the physical display area. This should generate a pointer move.
6823 processMove(mapper, toRawX(69), toRawY(159));
6824 processSync(mapper);
6825 assertReceivedMove({69, 159});
6826
6827 // Move outside the physical display area. Since the pointer is already down, this should
6828 // now continue generating events.
6829 processMove(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
6830 processSync(mapper);
6831 assertReceivedMove(kOutsidePoint);
6832
6833 // Release. This should generate a pointer up.
6834 processUp(mapper);
6835 processSync(mapper);
6836 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6837 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6838 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], kOutsidePoint.x,
6839 kOutsidePoint.y, 1, 0, 0, 0, 0, 0, 0, 0));
6840
6841 // Ensure no more events were generated.
6842 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6843 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6844 }
6845}
6846
Michael Wrightd02c5b62014-02-10 15:10:22 -08006847// --- MultiTouchInputMapperTest ---
6848
6849class MultiTouchInputMapperTest : public TouchInputMapperTest {
6850protected:
6851 void prepareAxes(int axes);
6852
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006853 void processPosition(MultiTouchInputMapper& mapper, int32_t x, int32_t y);
6854 void processTouchMajor(MultiTouchInputMapper& mapper, int32_t touchMajor);
6855 void processTouchMinor(MultiTouchInputMapper& mapper, int32_t touchMinor);
6856 void processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor);
6857 void processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor);
6858 void processOrientation(MultiTouchInputMapper& mapper, int32_t orientation);
6859 void processPressure(MultiTouchInputMapper& mapper, int32_t pressure);
6860 void processDistance(MultiTouchInputMapper& mapper, int32_t distance);
6861 void processId(MultiTouchInputMapper& mapper, int32_t id);
6862 void processSlot(MultiTouchInputMapper& mapper, int32_t slot);
6863 void processToolType(MultiTouchInputMapper& mapper, int32_t toolType);
6864 void processKey(MultiTouchInputMapper& mapper, int32_t code, int32_t value);
6865 void processMTSync(MultiTouchInputMapper& mapper);
6866 void processSync(MultiTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006867};
6868
6869void MultiTouchInputMapperTest::prepareAxes(int axes) {
6870 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006871 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
6872 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006873 }
6874 if (axes & TOUCH) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006875 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN,
6876 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006877 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006878 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN,
6879 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006880 }
6881 }
6882 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006883 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
6884 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006885 if (axes & MINOR) {
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08006886 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006887 RAW_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006888 }
6889 }
6890 if (axes & ORIENTATION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006891 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_ORIENTATION, RAW_ORIENTATION_MIN,
6892 RAW_ORIENTATION_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006893 }
6894 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006895 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_PRESSURE, RAW_PRESSURE_MIN,
6896 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006897 }
6898 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006899 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_DISTANCE, RAW_DISTANCE_MIN,
6900 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006901 }
6902 if (axes & ID) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006903 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX, 0,
6904 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006905 }
6906 if (axes & SLOT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006907 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
6908 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_MT_SLOT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006909 }
6910 if (axes & TOOL_TYPE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006911 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOOL_TYPE, 0, MT_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006912 }
6913}
6914
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006915void MultiTouchInputMapperTest::processPosition(MultiTouchInputMapper& mapper, int32_t x,
6916 int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006917 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_X, x);
6918 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006919}
6920
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006921void MultiTouchInputMapperTest::processTouchMajor(MultiTouchInputMapper& mapper,
6922 int32_t touchMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006923 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006924}
6925
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006926void MultiTouchInputMapperTest::processTouchMinor(MultiTouchInputMapper& mapper,
6927 int32_t touchMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006928 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006929}
6930
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006931void MultiTouchInputMapperTest::processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006932 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006933}
6934
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006935void MultiTouchInputMapperTest::processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006936 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006937}
6938
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006939void MultiTouchInputMapperTest::processOrientation(MultiTouchInputMapper& mapper,
6940 int32_t orientation) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006941 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_ORIENTATION, orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006942}
6943
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006944void MultiTouchInputMapperTest::processPressure(MultiTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006945 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006946}
6947
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006948void MultiTouchInputMapperTest::processDistance(MultiTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006949 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006950}
6951
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006952void MultiTouchInputMapperTest::processId(MultiTouchInputMapper& mapper, int32_t id) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006953 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TRACKING_ID, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006954}
6955
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006956void MultiTouchInputMapperTest::processSlot(MultiTouchInputMapper& mapper, int32_t slot) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006957 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_SLOT, slot);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006958}
6959
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006960void MultiTouchInputMapperTest::processToolType(MultiTouchInputMapper& mapper, int32_t toolType) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006961 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006962}
6963
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006964void MultiTouchInputMapperTest::processKey(MultiTouchInputMapper& mapper, int32_t code,
6965 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006966 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006967}
6968
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006969void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006970 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_MT_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006971}
6972
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006973void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006974 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006975}
6976
Michael Wrightd02c5b62014-02-10 15:10:22 -08006977TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006978 addConfigurationProperty("touch.deviceType", "touchScreen");
6979 prepareDisplay(DISPLAY_ORIENTATION_0);
6980 prepareAxes(POSITION);
6981 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006982 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006983
arthurhungdcef2dc2020-08-11 14:47:50 +08006984 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006985
6986 NotifyMotionArgs motionArgs;
6987
6988 // Two fingers down at once.
6989 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
6990 processPosition(mapper, x1, y1);
6991 processMTSync(mapper);
6992 processPosition(mapper, x2, y2);
6993 processMTSync(mapper);
6994 processSync(mapper);
6995
6996 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6997 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6998 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6999 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7000 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7001 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7002 ASSERT_EQ(0, motionArgs.flags);
7003 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7004 ASSERT_EQ(0, motionArgs.buttonState);
7005 ASSERT_EQ(0, motionArgs.edgeFlags);
7006 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7007 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7008 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7009 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7010 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7011 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7012 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7013 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7014
7015 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7016 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7017 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7018 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7019 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007020 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007021 ASSERT_EQ(0, motionArgs.flags);
7022 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7023 ASSERT_EQ(0, motionArgs.buttonState);
7024 ASSERT_EQ(0, motionArgs.edgeFlags);
7025 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7026 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7027 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7028 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7029 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7030 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7031 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7032 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7033 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7034 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7035 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7036 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7037
7038 // Move.
7039 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7040 processPosition(mapper, x1, y1);
7041 processMTSync(mapper);
7042 processPosition(mapper, x2, y2);
7043 processMTSync(mapper);
7044 processSync(mapper);
7045
7046 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7047 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7048 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7049 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7050 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7051 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7052 ASSERT_EQ(0, motionArgs.flags);
7053 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7054 ASSERT_EQ(0, motionArgs.buttonState);
7055 ASSERT_EQ(0, motionArgs.edgeFlags);
7056 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7057 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7058 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7059 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7060 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7061 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7062 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7063 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7064 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7065 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7066 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7067 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7068
7069 // First finger up.
7070 x2 += 15; y2 -= 20;
7071 processPosition(mapper, x2, y2);
7072 processMTSync(mapper);
7073 processSync(mapper);
7074
7075 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7076 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7077 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7078 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7079 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007080 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007081 ASSERT_EQ(0, motionArgs.flags);
7082 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7083 ASSERT_EQ(0, motionArgs.buttonState);
7084 ASSERT_EQ(0, motionArgs.edgeFlags);
7085 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7086 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7087 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7088 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7089 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7090 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7091 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7092 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7093 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7094 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7095 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7096 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7097
7098 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7099 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7100 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7101 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7102 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7103 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7104 ASSERT_EQ(0, motionArgs.flags);
7105 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7106 ASSERT_EQ(0, motionArgs.buttonState);
7107 ASSERT_EQ(0, motionArgs.edgeFlags);
7108 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7109 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7110 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7111 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7112 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7113 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7114 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7115 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7116
7117 // Move.
7118 x2 += 20; y2 -= 25;
7119 processPosition(mapper, x2, y2);
7120 processMTSync(mapper);
7121 processSync(mapper);
7122
7123 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7124 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7125 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7126 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7127 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7128 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7129 ASSERT_EQ(0, motionArgs.flags);
7130 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7131 ASSERT_EQ(0, motionArgs.buttonState);
7132 ASSERT_EQ(0, motionArgs.edgeFlags);
7133 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7134 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7135 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7136 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7137 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7138 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7139 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7140 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7141
7142 // New finger down.
7143 int32_t x3 = 700, y3 = 300;
7144 processPosition(mapper, x2, y2);
7145 processMTSync(mapper);
7146 processPosition(mapper, x3, y3);
7147 processMTSync(mapper);
7148 processSync(mapper);
7149
7150 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7151 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7152 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7153 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7154 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007155 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007156 ASSERT_EQ(0, motionArgs.flags);
7157 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7158 ASSERT_EQ(0, motionArgs.buttonState);
7159 ASSERT_EQ(0, motionArgs.edgeFlags);
7160 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7161 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7162 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7163 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7164 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7165 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7166 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7167 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7168 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7169 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7170 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7171 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7172
7173 // Second finger up.
7174 x3 += 30; y3 -= 20;
7175 processPosition(mapper, x3, y3);
7176 processMTSync(mapper);
7177 processSync(mapper);
7178
7179 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7180 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7181 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7182 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7183 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007184 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007185 ASSERT_EQ(0, motionArgs.flags);
7186 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7187 ASSERT_EQ(0, motionArgs.buttonState);
7188 ASSERT_EQ(0, motionArgs.edgeFlags);
7189 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7190 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7191 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7192 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7193 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7194 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7195 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7196 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7197 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7198 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7199 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7200 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7201
7202 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7203 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7204 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7205 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7206 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7207 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7208 ASSERT_EQ(0, motionArgs.flags);
7209 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7210 ASSERT_EQ(0, motionArgs.buttonState);
7211 ASSERT_EQ(0, motionArgs.edgeFlags);
7212 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7213 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7214 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7215 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7216 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7217 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7218 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7219 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7220
7221 // Last finger up.
7222 processMTSync(mapper);
7223 processSync(mapper);
7224
7225 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7226 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7227 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7228 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7229 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7230 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7231 ASSERT_EQ(0, motionArgs.flags);
7232 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7233 ASSERT_EQ(0, motionArgs.buttonState);
7234 ASSERT_EQ(0, motionArgs.edgeFlags);
7235 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7236 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7237 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7238 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7239 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7240 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7241 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7242 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7243
7244 // Should not have sent any more keys or motions.
7245 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7246 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7247}
7248
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007249TEST_F(MultiTouchInputMapperTest, AxisResolution_IsPopulated) {
7250 addConfigurationProperty("touch.deviceType", "touchScreen");
7251 prepareDisplay(DISPLAY_ORIENTATION_0);
7252
7253 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
7254 /*fuzz*/ 0, /*resolution*/ 10);
7255 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
7256 /*fuzz*/ 0, /*resolution*/ 11);
7257 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
7258 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 12);
7259 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
7260 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 13);
7261 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7262 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 14);
7263 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7264 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 15);
7265
7266 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7267
7268 // X and Y axes
7269 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_X, 10 / X_PRECISION);
7270 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_Y, 11 / Y_PRECISION);
7271 // Touch major and minor
7272 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR, 12 * GEOMETRIC_SCALE);
7273 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR, 13 * GEOMETRIC_SCALE);
7274 // Tool major and minor
7275 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR, 14 * GEOMETRIC_SCALE);
7276 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR, 15 * GEOMETRIC_SCALE);
7277}
7278
7279TEST_F(MultiTouchInputMapperTest, TouchMajorAndMinorAxes_DoNotAppearIfNotSupported) {
7280 addConfigurationProperty("touch.deviceType", "touchScreen");
7281 prepareDisplay(DISPLAY_ORIENTATION_0);
7282
7283 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
7284 /*fuzz*/ 0, /*resolution*/ 10);
7285 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
7286 /*fuzz*/ 0, /*resolution*/ 11);
7287
7288 // We do not add ABS_MT_TOUCH_MAJOR / MINOR or ABS_MT_WIDTH_MAJOR / MINOR axes
7289
7290 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7291
7292 // Touch major and minor
7293 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR);
7294 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR);
7295 // Tool major and minor
7296 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR);
7297 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR);
7298}
7299
Michael Wrightd02c5b62014-02-10 15:10:22 -08007300TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007301 addConfigurationProperty("touch.deviceType", "touchScreen");
7302 prepareDisplay(DISPLAY_ORIENTATION_0);
7303 prepareAxes(POSITION | ID);
7304 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007305 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007306
arthurhungdcef2dc2020-08-11 14:47:50 +08007307 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007308
7309 NotifyMotionArgs motionArgs;
7310
7311 // Two fingers down at once.
7312 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7313 processPosition(mapper, x1, y1);
7314 processId(mapper, 1);
7315 processMTSync(mapper);
7316 processPosition(mapper, x2, y2);
7317 processId(mapper, 2);
7318 processMTSync(mapper);
7319 processSync(mapper);
7320
7321 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7322 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7323 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7324 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7325 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7326 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7327 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7328
7329 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007330 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007331 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7332 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7333 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7334 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7335 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7336 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7337 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7338 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7339 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7340
7341 // Move.
7342 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7343 processPosition(mapper, x1, y1);
7344 processId(mapper, 1);
7345 processMTSync(mapper);
7346 processPosition(mapper, x2, y2);
7347 processId(mapper, 2);
7348 processMTSync(mapper);
7349 processSync(mapper);
7350
7351 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7352 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7353 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7354 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7355 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7356 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7357 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7358 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7359 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7360 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7361 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7362
7363 // First finger up.
7364 x2 += 15; y2 -= 20;
7365 processPosition(mapper, x2, y2);
7366 processId(mapper, 2);
7367 processMTSync(mapper);
7368 processSync(mapper);
7369
7370 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007371 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007372 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7373 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7374 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7375 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7376 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7377 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7378 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7379 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7380 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7381
7382 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7383 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7384 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7385 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7386 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7387 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7388 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7389
7390 // Move.
7391 x2 += 20; y2 -= 25;
7392 processPosition(mapper, x2, y2);
7393 processId(mapper, 2);
7394 processMTSync(mapper);
7395 processSync(mapper);
7396
7397 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7398 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7399 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7400 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7401 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7402 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7403 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7404
7405 // New finger down.
7406 int32_t x3 = 700, y3 = 300;
7407 processPosition(mapper, x2, y2);
7408 processId(mapper, 2);
7409 processMTSync(mapper);
7410 processPosition(mapper, x3, y3);
7411 processId(mapper, 3);
7412 processMTSync(mapper);
7413 processSync(mapper);
7414
7415 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007416 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007417 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7418 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7419 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7420 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7421 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7422 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7423 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7424 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7425 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7426
7427 // Second finger up.
7428 x3 += 30; y3 -= 20;
7429 processPosition(mapper, x3, y3);
7430 processId(mapper, 3);
7431 processMTSync(mapper);
7432 processSync(mapper);
7433
7434 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007435 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007436 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7437 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7438 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7439 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7440 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7441 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7442 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7443 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7444 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7445
7446 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7447 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7448 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7449 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7450 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7451 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7452 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7453
7454 // Last finger up.
7455 processMTSync(mapper);
7456 processSync(mapper);
7457
7458 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7459 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7460 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7461 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7462 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7463 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7464 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7465
7466 // Should not have sent any more keys or motions.
7467 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7468 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7469}
7470
7471TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007472 addConfigurationProperty("touch.deviceType", "touchScreen");
7473 prepareDisplay(DISPLAY_ORIENTATION_0);
7474 prepareAxes(POSITION | ID | SLOT);
7475 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007476 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007477
arthurhungdcef2dc2020-08-11 14:47:50 +08007478 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007479
7480 NotifyMotionArgs motionArgs;
7481
7482 // Two fingers down at once.
7483 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7484 processPosition(mapper, x1, y1);
7485 processId(mapper, 1);
7486 processSlot(mapper, 1);
7487 processPosition(mapper, x2, y2);
7488 processId(mapper, 2);
7489 processSync(mapper);
7490
7491 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7492 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7493 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7494 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7495 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7496 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7497 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7498
7499 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007500 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007501 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7502 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7503 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7504 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7505 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7506 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7507 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7508 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7509 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7510
7511 // Move.
7512 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7513 processSlot(mapper, 0);
7514 processPosition(mapper, x1, y1);
7515 processSlot(mapper, 1);
7516 processPosition(mapper, x2, y2);
7517 processSync(mapper);
7518
7519 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7520 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7521 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7522 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7523 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7524 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7525 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7526 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7527 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7528 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7529 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7530
7531 // First finger up.
7532 x2 += 15; y2 -= 20;
7533 processSlot(mapper, 0);
7534 processId(mapper, -1);
7535 processSlot(mapper, 1);
7536 processPosition(mapper, x2, y2);
7537 processSync(mapper);
7538
7539 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007540 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007541 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7542 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7543 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7544 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7545 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7546 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7547 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7548 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7549 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7550
7551 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7552 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7553 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7554 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7555 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7556 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7557 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7558
7559 // Move.
7560 x2 += 20; y2 -= 25;
7561 processPosition(mapper, x2, y2);
7562 processSync(mapper);
7563
7564 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7565 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7566 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7567 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7568 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7569 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7570 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7571
7572 // New finger down.
7573 int32_t x3 = 700, y3 = 300;
7574 processPosition(mapper, x2, y2);
7575 processSlot(mapper, 0);
7576 processId(mapper, 3);
7577 processPosition(mapper, x3, y3);
7578 processSync(mapper);
7579
7580 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007581 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007582 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7583 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7584 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7585 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7586 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7587 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7588 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7589 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7590 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7591
7592 // Second finger up.
7593 x3 += 30; y3 -= 20;
7594 processSlot(mapper, 1);
7595 processId(mapper, -1);
7596 processSlot(mapper, 0);
7597 processPosition(mapper, x3, y3);
7598 processSync(mapper);
7599
7600 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007601 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007602 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7603 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7604 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7605 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7606 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7607 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7608 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7609 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7610 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7611
7612 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7613 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7614 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7615 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7616 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7617 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7618 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7619
7620 // Last finger up.
7621 processId(mapper, -1);
7622 processSync(mapper);
7623
7624 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7625 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7626 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7627 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7628 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7629 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7630 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7631
7632 // Should not have sent any more keys or motions.
7633 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7634 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7635}
7636
7637TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007638 addConfigurationProperty("touch.deviceType", "touchScreen");
7639 prepareDisplay(DISPLAY_ORIENTATION_0);
7640 prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007641 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007642
7643 // These calculations are based on the input device calibration documentation.
7644 int32_t rawX = 100;
7645 int32_t rawY = 200;
7646 int32_t rawTouchMajor = 7;
7647 int32_t rawTouchMinor = 6;
7648 int32_t rawToolMajor = 9;
7649 int32_t rawToolMinor = 8;
7650 int32_t rawPressure = 11;
7651 int32_t rawDistance = 0;
7652 int32_t rawOrientation = 3;
7653 int32_t id = 5;
7654
7655 float x = toDisplayX(rawX);
7656 float y = toDisplayY(rawY);
7657 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
7658 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
7659 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
7660 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
7661 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
7662 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
7663 float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
7664 float distance = float(rawDistance);
7665
7666 processPosition(mapper, rawX, rawY);
7667 processTouchMajor(mapper, rawTouchMajor);
7668 processTouchMinor(mapper, rawTouchMinor);
7669 processToolMajor(mapper, rawToolMajor);
7670 processToolMinor(mapper, rawToolMinor);
7671 processPressure(mapper, rawPressure);
7672 processOrientation(mapper, rawOrientation);
7673 processDistance(mapper, rawDistance);
7674 processId(mapper, id);
7675 processMTSync(mapper);
7676 processSync(mapper);
7677
7678 NotifyMotionArgs args;
7679 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7680 ASSERT_EQ(0, args.pointerProperties[0].id);
7681 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7682 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
7683 orientation, distance));
7684}
7685
7686TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007687 addConfigurationProperty("touch.deviceType", "touchScreen");
7688 prepareDisplay(DISPLAY_ORIENTATION_0);
7689 prepareAxes(POSITION | TOUCH | TOOL | MINOR);
7690 addConfigurationProperty("touch.size.calibration", "geometric");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007691 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007692
7693 // These calculations are based on the input device calibration documentation.
7694 int32_t rawX = 100;
7695 int32_t rawY = 200;
7696 int32_t rawTouchMajor = 140;
7697 int32_t rawTouchMinor = 120;
7698 int32_t rawToolMajor = 180;
7699 int32_t rawToolMinor = 160;
7700
7701 float x = toDisplayX(rawX);
7702 float y = toDisplayY(rawY);
7703 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
7704 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
7705 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
7706 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
7707 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
7708
7709 processPosition(mapper, rawX, rawY);
7710 processTouchMajor(mapper, rawTouchMajor);
7711 processTouchMinor(mapper, rawTouchMinor);
7712 processToolMajor(mapper, rawToolMajor);
7713 processToolMinor(mapper, rawToolMinor);
7714 processMTSync(mapper);
7715 processSync(mapper);
7716
7717 NotifyMotionArgs args;
7718 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7719 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7720 x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
7721}
7722
7723TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007724 addConfigurationProperty("touch.deviceType", "touchScreen");
7725 prepareDisplay(DISPLAY_ORIENTATION_0);
7726 prepareAxes(POSITION | TOUCH | TOOL);
7727 addConfigurationProperty("touch.size.calibration", "diameter");
7728 addConfigurationProperty("touch.size.scale", "10");
7729 addConfigurationProperty("touch.size.bias", "160");
7730 addConfigurationProperty("touch.size.isSummed", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007731 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007732
7733 // These calculations are based on the input device calibration documentation.
7734 // Note: We only provide a single common touch/tool value because the device is assumed
7735 // not to emit separate values for each pointer (isSummed = 1).
7736 int32_t rawX = 100;
7737 int32_t rawY = 200;
7738 int32_t rawX2 = 150;
7739 int32_t rawY2 = 250;
7740 int32_t rawTouchMajor = 5;
7741 int32_t rawToolMajor = 8;
7742
7743 float x = toDisplayX(rawX);
7744 float y = toDisplayY(rawY);
7745 float x2 = toDisplayX(rawX2);
7746 float y2 = toDisplayY(rawY2);
7747 float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
7748 float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
7749 float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
7750
7751 processPosition(mapper, rawX, rawY);
7752 processTouchMajor(mapper, rawTouchMajor);
7753 processToolMajor(mapper, rawToolMajor);
7754 processMTSync(mapper);
7755 processPosition(mapper, rawX2, rawY2);
7756 processTouchMajor(mapper, rawTouchMajor);
7757 processToolMajor(mapper, rawToolMajor);
7758 processMTSync(mapper);
7759 processSync(mapper);
7760
7761 NotifyMotionArgs args;
7762 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7763 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
7764
7765 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007766 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007767 ASSERT_EQ(size_t(2), args.pointerCount);
7768 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7769 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
7770 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
7771 x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
7772}
7773
7774TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007775 addConfigurationProperty("touch.deviceType", "touchScreen");
7776 prepareDisplay(DISPLAY_ORIENTATION_0);
7777 prepareAxes(POSITION | TOUCH | TOOL);
7778 addConfigurationProperty("touch.size.calibration", "area");
7779 addConfigurationProperty("touch.size.scale", "43");
7780 addConfigurationProperty("touch.size.bias", "3");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007781 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007782
7783 // These calculations are based on the input device calibration documentation.
7784 int32_t rawX = 100;
7785 int32_t rawY = 200;
7786 int32_t rawTouchMajor = 5;
7787 int32_t rawToolMajor = 8;
7788
7789 float x = toDisplayX(rawX);
7790 float y = toDisplayY(rawY);
7791 float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
7792 float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
7793 float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
7794
7795 processPosition(mapper, rawX, rawY);
7796 processTouchMajor(mapper, rawTouchMajor);
7797 processToolMajor(mapper, rawToolMajor);
7798 processMTSync(mapper);
7799 processSync(mapper);
7800
7801 NotifyMotionArgs args;
7802 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7803 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7804 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
7805}
7806
7807TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007808 addConfigurationProperty("touch.deviceType", "touchScreen");
7809 prepareDisplay(DISPLAY_ORIENTATION_0);
7810 prepareAxes(POSITION | PRESSURE);
7811 addConfigurationProperty("touch.pressure.calibration", "amplitude");
7812 addConfigurationProperty("touch.pressure.scale", "0.01");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007813 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007814
Michael Wrightaa449c92017-12-13 21:21:43 +00007815 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007816 mapper.populateDeviceInfo(&info);
Michael Wrightaa449c92017-12-13 21:21:43 +00007817 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
7818 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TOUCHSCREEN,
7819 0.0f, RAW_PRESSURE_MAX * 0.01, 0.0f, 0.0f));
7820
Michael Wrightd02c5b62014-02-10 15:10:22 -08007821 // These calculations are based on the input device calibration documentation.
7822 int32_t rawX = 100;
7823 int32_t rawY = 200;
7824 int32_t rawPressure = 60;
7825
7826 float x = toDisplayX(rawX);
7827 float y = toDisplayY(rawY);
7828 float pressure = float(rawPressure) * 0.01f;
7829
7830 processPosition(mapper, rawX, rawY);
7831 processPressure(mapper, rawPressure);
7832 processMTSync(mapper);
7833 processSync(mapper);
7834
7835 NotifyMotionArgs args;
7836 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7837 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7838 x, y, pressure, 0, 0, 0, 0, 0, 0, 0));
7839}
7840
7841TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007842 addConfigurationProperty("touch.deviceType", "touchScreen");
7843 prepareDisplay(DISPLAY_ORIENTATION_0);
7844 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007845 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007846
7847 NotifyMotionArgs motionArgs;
7848 NotifyKeyArgs keyArgs;
7849
7850 processId(mapper, 1);
7851 processPosition(mapper, 100, 200);
7852 processSync(mapper);
7853 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7854 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7855 ASSERT_EQ(0, motionArgs.buttonState);
7856
7857 // press BTN_LEFT, release BTN_LEFT
7858 processKey(mapper, BTN_LEFT, 1);
7859 processSync(mapper);
7860 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7861 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7862 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
7863
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007864 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7865 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7866 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
7867
Michael Wrightd02c5b62014-02-10 15:10:22 -08007868 processKey(mapper, BTN_LEFT, 0);
7869 processSync(mapper);
7870 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007871 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007872 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007873
7874 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007875 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007876 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007877
7878 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
7879 processKey(mapper, BTN_RIGHT, 1);
7880 processKey(mapper, BTN_MIDDLE, 1);
7881 processSync(mapper);
7882 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7883 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7884 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
7885 motionArgs.buttonState);
7886
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007887 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7888 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7889 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
7890
7891 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7892 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7893 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
7894 motionArgs.buttonState);
7895
Michael Wrightd02c5b62014-02-10 15:10:22 -08007896 processKey(mapper, BTN_RIGHT, 0);
7897 processSync(mapper);
7898 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007899 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007900 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007901
7902 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007903 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007904 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007905
7906 processKey(mapper, BTN_MIDDLE, 0);
7907 processSync(mapper);
7908 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007909 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007910 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007911
7912 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007913 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007914 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007915
7916 // press BTN_BACK, release BTN_BACK
7917 processKey(mapper, BTN_BACK, 1);
7918 processSync(mapper);
7919 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7920 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
7921 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007922
Michael Wrightd02c5b62014-02-10 15:10:22 -08007923 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007924 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007925 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
7926
7927 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7928 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7929 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007930
7931 processKey(mapper, BTN_BACK, 0);
7932 processSync(mapper);
7933 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007934 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007935 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007936
7937 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007938 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007939 ASSERT_EQ(0, motionArgs.buttonState);
7940
Michael Wrightd02c5b62014-02-10 15:10:22 -08007941 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7942 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
7943 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
7944
7945 // press BTN_SIDE, release BTN_SIDE
7946 processKey(mapper, BTN_SIDE, 1);
7947 processSync(mapper);
7948 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7949 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
7950 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007951
Michael Wrightd02c5b62014-02-10 15:10:22 -08007952 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007953 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007954 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
7955
7956 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7957 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7958 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007959
7960 processKey(mapper, BTN_SIDE, 0);
7961 processSync(mapper);
7962 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007963 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007964 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007965
7966 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007967 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007968 ASSERT_EQ(0, motionArgs.buttonState);
7969
Michael Wrightd02c5b62014-02-10 15:10:22 -08007970 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7971 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
7972 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
7973
7974 // press BTN_FORWARD, release BTN_FORWARD
7975 processKey(mapper, BTN_FORWARD, 1);
7976 processSync(mapper);
7977 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7978 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
7979 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007980
Michael Wrightd02c5b62014-02-10 15:10:22 -08007981 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007982 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007983 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
7984
7985 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7986 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7987 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007988
7989 processKey(mapper, BTN_FORWARD, 0);
7990 processSync(mapper);
7991 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007992 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007993 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007994
7995 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007996 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007997 ASSERT_EQ(0, motionArgs.buttonState);
7998
Michael Wrightd02c5b62014-02-10 15:10:22 -08007999 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8000 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8001 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
8002
8003 // press BTN_EXTRA, release BTN_EXTRA
8004 processKey(mapper, BTN_EXTRA, 1);
8005 processSync(mapper);
8006 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8007 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8008 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008009
Michael Wrightd02c5b62014-02-10 15:10:22 -08008010 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008011 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008012 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
8013
8014 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8015 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8016 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008017
8018 processKey(mapper, BTN_EXTRA, 0);
8019 processSync(mapper);
8020 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008021 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008022 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008023
8024 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008025 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008026 ASSERT_EQ(0, motionArgs.buttonState);
8027
Michael Wrightd02c5b62014-02-10 15:10:22 -08008028 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8029 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8030 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
8031
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008032 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8033
Michael Wrightd02c5b62014-02-10 15:10:22 -08008034 // press BTN_STYLUS, release BTN_STYLUS
8035 processKey(mapper, BTN_STYLUS, 1);
8036 processSync(mapper);
8037 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8038 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008039 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
8040
8041 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8042 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8043 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008044
8045 processKey(mapper, BTN_STYLUS, 0);
8046 processSync(mapper);
8047 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008048 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008049 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008050
8051 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008052 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008053 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008054
8055 // press BTN_STYLUS2, release BTN_STYLUS2
8056 processKey(mapper, BTN_STYLUS2, 1);
8057 processSync(mapper);
8058 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8059 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008060 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
8061
8062 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8063 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8064 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008065
8066 processKey(mapper, BTN_STYLUS2, 0);
8067 processSync(mapper);
8068 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008069 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008070 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008071
8072 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008073 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008074 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008075
8076 // release touch
8077 processId(mapper, -1);
8078 processSync(mapper);
8079 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8080 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8081 ASSERT_EQ(0, motionArgs.buttonState);
8082}
8083
8084TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008085 addConfigurationProperty("touch.deviceType", "touchScreen");
8086 prepareDisplay(DISPLAY_ORIENTATION_0);
8087 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008088 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008089
8090 NotifyMotionArgs motionArgs;
8091
8092 // default tool type is finger
8093 processId(mapper, 1);
8094 processPosition(mapper, 100, 200);
8095 processSync(mapper);
8096 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8097 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8098 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8099
8100 // eraser
8101 processKey(mapper, BTN_TOOL_RUBBER, 1);
8102 processSync(mapper);
8103 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8104 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8105 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
8106
8107 // stylus
8108 processKey(mapper, BTN_TOOL_RUBBER, 0);
8109 processKey(mapper, BTN_TOOL_PEN, 1);
8110 processSync(mapper);
8111 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8112 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8113 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8114
8115 // brush
8116 processKey(mapper, BTN_TOOL_PEN, 0);
8117 processKey(mapper, BTN_TOOL_BRUSH, 1);
8118 processSync(mapper);
8119 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8120 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8121 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8122
8123 // pencil
8124 processKey(mapper, BTN_TOOL_BRUSH, 0);
8125 processKey(mapper, BTN_TOOL_PENCIL, 1);
8126 processSync(mapper);
8127 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8128 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8129 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8130
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08008131 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08008132 processKey(mapper, BTN_TOOL_PENCIL, 0);
8133 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
8134 processSync(mapper);
8135 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8136 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8137 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8138
8139 // mouse
8140 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
8141 processKey(mapper, BTN_TOOL_MOUSE, 1);
8142 processSync(mapper);
8143 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8144 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8145 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
8146
8147 // lens
8148 processKey(mapper, BTN_TOOL_MOUSE, 0);
8149 processKey(mapper, BTN_TOOL_LENS, 1);
8150 processSync(mapper);
8151 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8152 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8153 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
8154
8155 // double-tap
8156 processKey(mapper, BTN_TOOL_LENS, 0);
8157 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
8158 processSync(mapper);
8159 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8160 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8161 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8162
8163 // triple-tap
8164 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
8165 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
8166 processSync(mapper);
8167 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8168 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8169 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8170
8171 // quad-tap
8172 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
8173 processKey(mapper, BTN_TOOL_QUADTAP, 1);
8174 processSync(mapper);
8175 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8176 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8177 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8178
8179 // finger
8180 processKey(mapper, BTN_TOOL_QUADTAP, 0);
8181 processKey(mapper, BTN_TOOL_FINGER, 1);
8182 processSync(mapper);
8183 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8184 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8185 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8186
8187 // stylus trumps finger
8188 processKey(mapper, BTN_TOOL_PEN, 1);
8189 processSync(mapper);
8190 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8191 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8192 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8193
8194 // eraser trumps stylus
8195 processKey(mapper, BTN_TOOL_RUBBER, 1);
8196 processSync(mapper);
8197 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8198 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8199 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
8200
8201 // mouse trumps eraser
8202 processKey(mapper, BTN_TOOL_MOUSE, 1);
8203 processSync(mapper);
8204 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8205 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8206 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
8207
8208 // MT tool type trumps BTN tool types: MT_TOOL_FINGER
8209 processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
8210 processSync(mapper);
8211 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8212 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8213 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8214
8215 // MT tool type trumps BTN tool types: MT_TOOL_PEN
8216 processToolType(mapper, MT_TOOL_PEN);
8217 processSync(mapper);
8218 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8219 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8220 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8221
8222 // back to default tool type
8223 processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
8224 processKey(mapper, BTN_TOOL_MOUSE, 0);
8225 processKey(mapper, BTN_TOOL_RUBBER, 0);
8226 processKey(mapper, BTN_TOOL_PEN, 0);
8227 processKey(mapper, BTN_TOOL_FINGER, 0);
8228 processSync(mapper);
8229 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8230 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8231 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8232}
8233
8234TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008235 addConfigurationProperty("touch.deviceType", "touchScreen");
8236 prepareDisplay(DISPLAY_ORIENTATION_0);
8237 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008238 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008239 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008240
8241 NotifyMotionArgs motionArgs;
8242
8243 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
8244 processId(mapper, 1);
8245 processPosition(mapper, 100, 200);
8246 processSync(mapper);
8247 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8248 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8249 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8250 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8251
8252 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8253 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, 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 // move a little
8258 processPosition(mapper, 150, 250);
8259 processSync(mapper);
8260 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8261 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8262 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8263 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8264
8265 // down when BTN_TOUCH is pressed, pressure defaults to 1
8266 processKey(mapper, BTN_TOUCH, 1);
8267 processSync(mapper);
8268 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8269 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8270 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8271 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8272
8273 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8274 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8275 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8276 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8277
8278 // up when BTN_TOUCH is released, hover restored
8279 processKey(mapper, BTN_TOUCH, 0);
8280 processSync(mapper);
8281 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8282 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8283 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8284 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8285
8286 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8287 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8288 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8289 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8290
8291 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8292 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, 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 // exit hover when pointer goes away
8297 processId(mapper, -1);
8298 processSync(mapper);
8299 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8300 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8301 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8302 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8303}
8304
8305TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008306 addConfigurationProperty("touch.deviceType", "touchScreen");
8307 prepareDisplay(DISPLAY_ORIENTATION_0);
8308 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008309 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008310
8311 NotifyMotionArgs motionArgs;
8312
8313 // initially hovering because pressure is 0
8314 processId(mapper, 1);
8315 processPosition(mapper, 100, 200);
8316 processPressure(mapper, 0);
8317 processSync(mapper);
8318 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8319 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8320 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8321 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8322
8323 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8324 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, 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 // move a little
8329 processPosition(mapper, 150, 250);
8330 processSync(mapper);
8331 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8332 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8333 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8334 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8335
8336 // down when pressure becomes non-zero
8337 processPressure(mapper, RAW_PRESSURE_MAX);
8338 processSync(mapper);
8339 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8340 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8341 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8342 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8343
8344 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8345 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8346 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8347 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8348
8349 // up when pressure becomes 0, hover restored
8350 processPressure(mapper, 0);
8351 processSync(mapper);
8352 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8353 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8354 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8355 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8356
8357 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8358 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8359 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8360 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8361
8362 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8363 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, 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 // exit hover when pointer goes away
8368 processId(mapper, -1);
8369 processSync(mapper);
8370 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8371 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8372 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8373 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8374}
8375
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008376/**
8377 * Set the input device port <--> display port associations, and check that the
8378 * events are routed to the display that matches the display port.
8379 * This can be checked by looking at the displayId of the resulting NotifyMotionArgs.
8380 */
8381TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayPort) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008382 const std::string usb2 = "USB2";
8383 const uint8_t hdmi1 = 0;
8384 const uint8_t hdmi2 = 1;
8385 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008386 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008387
8388 addConfigurationProperty("touch.deviceType", "touchScreen");
8389 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008390 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008391
8392 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
8393 mFakePolicy->addInputPortAssociation(usb2, hdmi2);
8394
8395 // We are intentionally not adding the viewport for display 1 yet. Since the port association
8396 // for this input device is specified, and the matching viewport is not present,
8397 // the input device should be disabled (at the mapper level).
8398
8399 // Add viewport for display 2 on hdmi2
8400 prepareSecondaryDisplay(type, hdmi2);
8401 // Send a touch event
8402 processPosition(mapper, 100, 100);
8403 processSync(mapper);
8404 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8405
8406 // Add viewport for display 1 on hdmi1
8407 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
8408 // Send a touch event again
8409 processPosition(mapper, 100, 100);
8410 processSync(mapper);
8411
8412 NotifyMotionArgs args;
8413 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8414 ASSERT_EQ(DISPLAY_ID, args.displayId);
8415}
Michael Wrightd02c5b62014-02-10 15:10:22 -08008416
Arthur Hung6d5b4b22022-01-21 07:21:10 +00008417TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayUniqueId) {
8418 addConfigurationProperty("touch.deviceType", "touchScreen");
8419 prepareAxes(POSITION);
8420 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8421
8422 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
8423
8424 prepareDisplay(DISPLAY_ORIENTATION_0);
8425 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
8426
8427 // Send a touch event
8428 processPosition(mapper, 100, 100);
8429 processSync(mapper);
8430
8431 NotifyMotionArgs args;
8432 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8433 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
8434}
8435
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008436TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShouldHandleDisplayId) {
Garfield Tan888a6a42020-01-09 11:39:16 -08008437 // Setup for second display.
Michael Wright17db18e2020-06-26 20:51:44 +01008438 std::shared_ptr<FakePointerController> fakePointerController =
8439 std::make_shared<FakePointerController>();
Garfield Tan888a6a42020-01-09 11:39:16 -08008440 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008441 fakePointerController->setPosition(100, 200);
8442 fakePointerController->setButtonState(0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00008443 mFakePolicy->setPointerController(fakePointerController);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008444
Garfield Tan888a6a42020-01-09 11:39:16 -08008445 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008446 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08008447
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008448 prepareDisplay(DISPLAY_ORIENTATION_0);
8449 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008450 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008451
8452 // Check source is mouse that would obtain the PointerController.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008453 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008454
8455 NotifyMotionArgs motionArgs;
8456 processPosition(mapper, 100, 100);
8457 processSync(mapper);
8458
8459 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8460 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8461 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
8462}
8463
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00008464/**
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008465 * Ensure that the readTime is set to the SYN_REPORT value when processing touch events.
8466 */
8467TEST_F(MultiTouchInputMapperTest, Process_SendsReadTime) {
8468 addConfigurationProperty("touch.deviceType", "touchScreen");
8469 prepareAxes(POSITION);
8470 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8471
8472 prepareDisplay(DISPLAY_ORIENTATION_0);
8473 process(mapper, 10, 11 /*readTime*/, EV_ABS, ABS_MT_TRACKING_ID, 1);
8474 process(mapper, 15, 16 /*readTime*/, EV_ABS, ABS_MT_POSITION_X, 100);
8475 process(mapper, 20, 21 /*readTime*/, EV_ABS, ABS_MT_POSITION_Y, 100);
8476 process(mapper, 25, 26 /*readTime*/, EV_SYN, SYN_REPORT, 0);
8477
8478 NotifyMotionArgs args;
8479 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8480 ASSERT_EQ(26, args.readTime);
8481
8482 process(mapper, 30, 31 /*readTime*/, EV_ABS, ABS_MT_POSITION_X, 110);
8483 process(mapper, 30, 32 /*readTime*/, EV_ABS, ABS_MT_POSITION_Y, 220);
8484 process(mapper, 30, 33 /*readTime*/, EV_SYN, SYN_REPORT, 0);
8485
8486 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8487 ASSERT_EQ(33, args.readTime);
8488}
8489
8490/**
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00008491 * When the viewport is not active (isActive=false), the touch mapper should be disabled and the
8492 * events should not be delivered to the listener.
8493 */
8494TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreDropped) {
8495 addConfigurationProperty("touch.deviceType", "touchScreen");
8496 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
8497 DISPLAY_ORIENTATION_0, false /*isActive*/, UNIQUE_ID, NO_PORT,
8498 ViewportType::INTERNAL);
8499 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8500 prepareAxes(POSITION);
8501 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8502
8503 NotifyMotionArgs motionArgs;
8504 processPosition(mapper, 100, 100);
8505 processSync(mapper);
8506
8507 mFakeListener->assertNotifyMotionWasNotCalled();
8508}
8509
Garfield Tanc734e4f2021-01-15 20:01:39 -08008510TEST_F(MultiTouchInputMapperTest, Process_DeactivateViewport_AbortTouches) {
8511 addConfigurationProperty("touch.deviceType", "touchScreen");
8512 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
8513 DISPLAY_ORIENTATION_0, true /*isActive*/, UNIQUE_ID, NO_PORT,
8514 ViewportType::INTERNAL);
8515 std::optional<DisplayViewport> optionalDisplayViewport =
8516 mFakePolicy->getDisplayViewportByUniqueId(UNIQUE_ID);
8517 ASSERT_TRUE(optionalDisplayViewport.has_value());
8518 DisplayViewport displayViewport = *optionalDisplayViewport;
8519
8520 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8521 prepareAxes(POSITION);
8522 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8523
8524 // Finger down
8525 int32_t x = 100, y = 100;
8526 processPosition(mapper, x, y);
8527 processSync(mapper);
8528
8529 NotifyMotionArgs motionArgs;
8530 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8531 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8532
8533 // Deactivate display viewport
8534 displayViewport.isActive = false;
8535 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
8536 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8537
8538 // Finger move
8539 x += 10, y += 10;
8540 processPosition(mapper, x, y);
8541 processSync(mapper);
8542
8543 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8544 EXPECT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
8545
8546 // Reactivate display viewport
8547 displayViewport.isActive = true;
8548 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
8549 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8550
8551 // Finger move again
8552 x += 10, y += 10;
8553 processPosition(mapper, x, y);
8554 processSync(mapper);
8555
8556 // Gesture is aborted, so events after display is activated won't be dispatched until there is
8557 // no pointer on the touch device.
8558 mFakeListener->assertNotifyMotionWasNotCalled();
8559}
8560
Arthur Hung7c645402019-01-25 17:45:42 +08008561TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShowTouches) {
8562 // Setup the first touch screen device.
Arthur Hung7c645402019-01-25 17:45:42 +08008563 prepareAxes(POSITION | ID | SLOT);
8564 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008565 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung7c645402019-01-25 17:45:42 +08008566
8567 // Create the second touch screen device, and enable multi fingers.
8568 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08008569 const std::string DEVICE_NAME2 = "TOUCHSCREEN2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08008570 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008571 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08008572 std::shared_ptr<InputDevice> device2 =
8573 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07008574 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08008575
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008576 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
8577 0 /*flat*/, 0 /*fuzz*/);
8578 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
8579 0 /*flat*/, 0 /*fuzz*/);
8580 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX,
8581 0 /*flat*/, 0 /*fuzz*/);
8582 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX,
8583 0 /*flat*/, 0 /*fuzz*/);
8584 mFakeEventHub->setAbsoluteAxisValue(SECOND_EVENTHUB_ID, ABS_MT_SLOT, 0 /*value*/);
8585 mFakeEventHub->addConfigurationProperty(SECOND_EVENTHUB_ID, String8("touch.deviceType"),
8586 String8("touchScreen"));
Arthur Hung7c645402019-01-25 17:45:42 +08008587
8588 // Setup the second touch screen device.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008589 MultiTouchInputMapper& mapper2 = device2->addMapper<MultiTouchInputMapper>(SECOND_EVENTHUB_ID);
Arthur Hung7c645402019-01-25 17:45:42 +08008590 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
8591 device2->reset(ARBITRARY_TIME);
8592
8593 // Setup PointerController.
Michael Wright17db18e2020-06-26 20:51:44 +01008594 std::shared_ptr<FakePointerController> fakePointerController =
8595 std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00008596 mFakePolicy->setPointerController(fakePointerController);
Arthur Hung7c645402019-01-25 17:45:42 +08008597
8598 // Setup policy for associated displays and show touches.
8599 const uint8_t hdmi1 = 0;
8600 const uint8_t hdmi2 = 1;
8601 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
8602 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
8603 mFakePolicy->setShowTouches(true);
8604
8605 // Create displays.
8606 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008607 prepareSecondaryDisplay(ViewportType::EXTERNAL, hdmi2);
Arthur Hung7c645402019-01-25 17:45:42 +08008608
8609 // Default device will reconfigure above, need additional reconfiguration for another device.
8610 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan197e0862022-07-01 14:28:00 +00008611 InputReaderConfiguration::CHANGE_DISPLAY_INFO |
8612 InputReaderConfiguration::CHANGE_SHOW_TOUCHES);
Arthur Hung7c645402019-01-25 17:45:42 +08008613
8614 // Two fingers down at default display.
8615 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
8616 processPosition(mapper, x1, y1);
8617 processId(mapper, 1);
8618 processSlot(mapper, 1);
8619 processPosition(mapper, x2, y2);
8620 processId(mapper, 2);
8621 processSync(mapper);
8622
8623 std::map<int32_t, std::vector<int32_t>>::const_iterator iter =
8624 fakePointerController->getSpots().find(DISPLAY_ID);
8625 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
8626 ASSERT_EQ(size_t(2), iter->second.size());
8627
8628 // Two fingers down at second display.
8629 processPosition(mapper2, x1, y1);
8630 processId(mapper2, 1);
8631 processSlot(mapper2, 1);
8632 processPosition(mapper2, x2, y2);
8633 processId(mapper2, 2);
8634 processSync(mapper2);
8635
8636 iter = fakePointerController->getSpots().find(SECONDARY_DISPLAY_ID);
8637 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
8638 ASSERT_EQ(size_t(2), iter->second.size());
Prabir Pradhan197e0862022-07-01 14:28:00 +00008639
8640 // Disable the show touches configuration and ensure the spots are cleared.
8641 mFakePolicy->setShowTouches(false);
8642 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
8643 InputReaderConfiguration::CHANGE_SHOW_TOUCHES);
8644
8645 ASSERT_TRUE(fakePointerController->getSpots().empty());
Arthur Hung7c645402019-01-25 17:45:42 +08008646}
8647
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008648TEST_F(MultiTouchInputMapperTest, VideoFrames_ReceivedByListener) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008649 prepareAxes(POSITION);
8650 addConfigurationProperty("touch.deviceType", "touchScreen");
8651 prepareDisplay(DISPLAY_ORIENTATION_0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008652 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008653
8654 NotifyMotionArgs motionArgs;
8655 // Unrotated video frame
8656 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8657 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008658 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008659 processPosition(mapper, 100, 200);
8660 processSync(mapper);
8661 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8662 ASSERT_EQ(frames, motionArgs.videoFrames);
8663
8664 // Subsequent touch events should not have any videoframes
8665 // This is implemented separately in FakeEventHub,
8666 // but that should match the behaviour of TouchVideoDevice.
8667 processPosition(mapper, 200, 200);
8668 processSync(mapper);
8669 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8670 ASSERT_EQ(std::vector<TouchVideoFrame>(), motionArgs.videoFrames);
8671}
8672
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008673TEST_F(MultiTouchInputMapperTest, VideoFrames_AreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008674 prepareAxes(POSITION);
8675 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008676 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008677 // Unrotated video frame
8678 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8679 NotifyMotionArgs motionArgs;
8680
8681 // Test all 4 orientations
8682 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008683 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
8684 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
8685 clearViewports();
8686 prepareDisplay(orientation);
8687 std::vector<TouchVideoFrame> frames{frame};
8688 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
8689 processPosition(mapper, 100, 200);
8690 processSync(mapper);
8691 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8692 ASSERT_EQ(frames, motionArgs.videoFrames);
8693 }
8694}
8695
8696TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_AreRotated) {
8697 prepareAxes(POSITION);
8698 addConfigurationProperty("touch.deviceType", "touchScreen");
8699 // Since InputReader works in the un-rotated coordinate space, only devices that are not
8700 // orientation-aware are affected by display rotation.
8701 addConfigurationProperty("touch.orientationAware", "0");
8702 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8703 // Unrotated video frame
8704 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8705 NotifyMotionArgs motionArgs;
8706
8707 // Test all 4 orientations
8708 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008709 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
8710 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
8711 clearViewports();
8712 prepareDisplay(orientation);
8713 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008714 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008715 processPosition(mapper, 100, 200);
8716 processSync(mapper);
8717 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008718 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
8719 // compared to the display. This is so that when the window transform (which contains the
8720 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
8721 // window's coordinate space.
8722 frames[0].rotate(getInverseRotation(orientation));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008723 ASSERT_EQ(frames, motionArgs.videoFrames);
lilinnan687e58f2022-07-19 16:00:50 +08008724
8725 // Release finger.
8726 processSync(mapper);
8727 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008728 }
8729}
8730
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008731TEST_F(MultiTouchInputMapperTest, VideoFrames_MultipleFramesAreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008732 prepareAxes(POSITION);
8733 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008734 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008735 // Unrotated video frames. There's no rule that they must all have the same dimensions,
8736 // so mix these.
8737 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8738 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
8739 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
8740 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
8741 NotifyMotionArgs motionArgs;
8742
8743 prepareDisplay(DISPLAY_ORIENTATION_90);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008744 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008745 processPosition(mapper, 100, 200);
8746 processSync(mapper);
8747 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008748 ASSERT_EQ(frames, motionArgs.videoFrames);
8749}
8750
8751TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_MultipleFramesAreRotated) {
8752 prepareAxes(POSITION);
8753 addConfigurationProperty("touch.deviceType", "touchScreen");
8754 // Since InputReader works in the un-rotated coordinate space, only devices that are not
8755 // orientation-aware are affected by display rotation.
8756 addConfigurationProperty("touch.orientationAware", "0");
8757 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8758 // Unrotated video frames. There's no rule that they must all have the same dimensions,
8759 // so mix these.
8760 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8761 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
8762 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
8763 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
8764 NotifyMotionArgs motionArgs;
8765
8766 prepareDisplay(DISPLAY_ORIENTATION_90);
8767 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
8768 processPosition(mapper, 100, 200);
8769 processSync(mapper);
8770 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8771 std::for_each(frames.begin(), frames.end(), [](TouchVideoFrame& frame) {
8772 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
8773 // compared to the display. This is so that when the window transform (which contains the
8774 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
8775 // window's coordinate space.
8776 frame.rotate(getInverseRotation(DISPLAY_ORIENTATION_90));
8777 });
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008778 ASSERT_EQ(frames, motionArgs.videoFrames);
8779}
8780
Arthur Hung9da14732019-09-02 16:16:58 +08008781/**
8782 * If we had defined port associations, but the viewport is not ready, the touch device would be
8783 * expected to be disabled, and it should be enabled after the viewport has found.
8784 */
8785TEST_F(MultiTouchInputMapperTest, Configure_EnabledForAssociatedDisplay) {
Arthur Hung9da14732019-09-02 16:16:58 +08008786 constexpr uint8_t hdmi2 = 1;
8787 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008788 constexpr ViewportType type = ViewportType::EXTERNAL;
Arthur Hung9da14732019-09-02 16:16:58 +08008789
8790 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi2);
8791
8792 addConfigurationProperty("touch.deviceType", "touchScreen");
8793 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008794 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung9da14732019-09-02 16:16:58 +08008795
8796 ASSERT_EQ(mDevice->isEnabled(), false);
8797
8798 // Add display on hdmi2, the device should be enabled and can receive touch event.
8799 prepareSecondaryDisplay(type, hdmi2);
8800 ASSERT_EQ(mDevice->isEnabled(), true);
8801
8802 // Send a touch event.
8803 processPosition(mapper, 100, 100);
8804 processSync(mapper);
8805
8806 NotifyMotionArgs args;
8807 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8808 ASSERT_EQ(SECONDARY_DISPLAY_ID, args.displayId);
8809}
8810
Arthur Hung421eb1c2020-01-16 00:09:42 +08008811TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleSingleTouch) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08008812 addConfigurationProperty("touch.deviceType", "touchScreen");
8813 prepareDisplay(DISPLAY_ORIENTATION_0);
8814 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008815 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08008816
8817 NotifyMotionArgs motionArgs;
8818
8819 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
8820 // finger down
8821 processId(mapper, 1);
8822 processPosition(mapper, x1, y1);
8823 processSync(mapper);
8824 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8825 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8826 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8827
8828 // finger move
8829 processId(mapper, 1);
8830 processPosition(mapper, x2, y2);
8831 processSync(mapper);
8832 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8833 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8834 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8835
8836 // finger up.
8837 processId(mapper, -1);
8838 processSync(mapper);
8839 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8840 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8841 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8842
8843 // new finger down
8844 processId(mapper, 1);
8845 processPosition(mapper, x3, y3);
8846 processSync(mapper);
8847 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8848 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8849 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8850}
8851
8852/**
arthurhungcc7f9802020-04-30 17:55:40 +08008853 * Test single touch should be canceled when received the MT_TOOL_PALM event, and the following
8854 * MOVE and UP events should be ignored.
Arthur Hung421eb1c2020-01-16 00:09:42 +08008855 */
arthurhungcc7f9802020-04-30 17:55:40 +08008856TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_SinglePointer) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08008857 addConfigurationProperty("touch.deviceType", "touchScreen");
8858 prepareDisplay(DISPLAY_ORIENTATION_0);
8859 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008860 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08008861
8862 NotifyMotionArgs motionArgs;
8863
8864 // default tool type is finger
8865 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
arthurhungcc7f9802020-04-30 17:55:40 +08008866 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008867 processPosition(mapper, x1, y1);
8868 processSync(mapper);
8869 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8870 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8871 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8872
8873 // Tool changed to MT_TOOL_PALM expect sending the cancel event.
8874 processToolType(mapper, MT_TOOL_PALM);
8875 processSync(mapper);
8876 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8877 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
8878
8879 // Ignore the following MOVE and UP events if had detect a palm event.
arthurhungcc7f9802020-04-30 17:55:40 +08008880 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008881 processPosition(mapper, x2, y2);
8882 processSync(mapper);
8883 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8884
8885 // finger up.
arthurhungcc7f9802020-04-30 17:55:40 +08008886 processId(mapper, INVALID_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008887 processSync(mapper);
8888 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8889
8890 // new finger down
arthurhungcc7f9802020-04-30 17:55:40 +08008891 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008892 processToolType(mapper, MT_TOOL_FINGER);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008893 processPosition(mapper, x3, y3);
8894 processSync(mapper);
8895 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8896 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8897 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8898}
8899
arthurhungbf89a482020-04-17 17:37:55 +08008900/**
arthurhungcc7f9802020-04-30 17:55:40 +08008901 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
8902 * and the rest active fingers could still be allowed to receive the events
arthurhungbf89a482020-04-17 17:37:55 +08008903 */
arthurhungcc7f9802020-04-30 17:55:40 +08008904TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_TwoPointers) {
arthurhungbf89a482020-04-17 17:37:55 +08008905 addConfigurationProperty("touch.deviceType", "touchScreen");
8906 prepareDisplay(DISPLAY_ORIENTATION_0);
8907 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
8908 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8909
8910 NotifyMotionArgs motionArgs;
8911
8912 // default tool type is finger
arthurhungcc7f9802020-04-30 17:55:40 +08008913 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
8914 processId(mapper, FIRST_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08008915 processPosition(mapper, x1, y1);
8916 processSync(mapper);
8917 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8918 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8919 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8920
8921 // Second finger down.
arthurhungcc7f9802020-04-30 17:55:40 +08008922 processSlot(mapper, SECOND_SLOT);
8923 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08008924 processPosition(mapper, x2, y2);
arthurhungcc7f9802020-04-30 17:55:40 +08008925 processSync(mapper);
8926 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008927 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08008928 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8929
8930 // If the tool type of the first finger changes to MT_TOOL_PALM,
8931 // we expect to receive ACTION_POINTER_UP with cancel flag.
8932 processSlot(mapper, FIRST_SLOT);
8933 processId(mapper, FIRST_TRACKING_ID);
8934 processToolType(mapper, MT_TOOL_PALM);
8935 processSync(mapper);
8936 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008937 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08008938 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
8939
8940 // The following MOVE events of second finger should be processed.
8941 processSlot(mapper, SECOND_SLOT);
8942 processId(mapper, SECOND_TRACKING_ID);
8943 processPosition(mapper, x2 + 1, y2 + 1);
8944 processSync(mapper);
8945 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8946 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8947 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8948
8949 // First finger up. It used to be in palm mode, and we already generated ACTION_POINTER_UP for
8950 // it. Second finger receive move.
8951 processSlot(mapper, FIRST_SLOT);
8952 processId(mapper, INVALID_TRACKING_ID);
8953 processSync(mapper);
8954 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8955 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8956 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8957
8958 // Second finger keeps moving.
8959 processSlot(mapper, SECOND_SLOT);
8960 processId(mapper, SECOND_TRACKING_ID);
8961 processPosition(mapper, x2 + 2, y2 + 2);
8962 processSync(mapper);
8963 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8964 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8965 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8966
8967 // Second finger up.
8968 processId(mapper, INVALID_TRACKING_ID);
8969 processSync(mapper);
8970 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8971 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8972 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
8973}
8974
8975/**
8976 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event, if only 1 finger
8977 * is active, it should send CANCEL after receiving the MT_TOOL_PALM event.
8978 */
8979TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_ShouldCancelWhenAllTouchIsPalm) {
8980 addConfigurationProperty("touch.deviceType", "touchScreen");
8981 prepareDisplay(DISPLAY_ORIENTATION_0);
8982 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
8983 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8984
8985 NotifyMotionArgs motionArgs;
8986
8987 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
8988 // First finger down.
8989 processId(mapper, FIRST_TRACKING_ID);
8990 processPosition(mapper, x1, y1);
8991 processSync(mapper);
8992 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8993 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8994 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8995
8996 // Second finger down.
8997 processSlot(mapper, SECOND_SLOT);
8998 processId(mapper, SECOND_TRACKING_ID);
8999 processPosition(mapper, x2, y2);
arthurhungbf89a482020-04-17 17:37:55 +08009000 processSync(mapper);
9001 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009002 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungbf89a482020-04-17 17:37:55 +08009003 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9004
arthurhungcc7f9802020-04-30 17:55:40 +08009005 // If the tool type of the first finger changes to MT_TOOL_PALM,
9006 // we expect to receive ACTION_POINTER_UP with cancel flag.
9007 processSlot(mapper, FIRST_SLOT);
9008 processId(mapper, FIRST_TRACKING_ID);
9009 processToolType(mapper, MT_TOOL_PALM);
9010 processSync(mapper);
9011 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009012 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009013 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9014
9015 // Second finger keeps moving.
9016 processSlot(mapper, SECOND_SLOT);
9017 processId(mapper, SECOND_TRACKING_ID);
9018 processPosition(mapper, x2 + 1, y2 + 1);
9019 processSync(mapper);
9020 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9021 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9022
9023 // second finger becomes palm, receive cancel due to only 1 finger is active.
9024 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009025 processToolType(mapper, MT_TOOL_PALM);
9026 processSync(mapper);
9027 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9028 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9029
arthurhungcc7f9802020-04-30 17:55:40 +08009030 // third finger down.
9031 processSlot(mapper, THIRD_SLOT);
9032 processId(mapper, THIRD_TRACKING_ID);
9033 processToolType(mapper, MT_TOOL_FINGER);
arthurhungbf89a482020-04-17 17:37:55 +08009034 processPosition(mapper, x3, y3);
9035 processSync(mapper);
arthurhungbf89a482020-04-17 17:37:55 +08009036 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9037 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9038 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08009039 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9040
9041 // third finger move
9042 processId(mapper, THIRD_TRACKING_ID);
9043 processPosition(mapper, x3 + 1, y3 + 1);
9044 processSync(mapper);
9045 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9046 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9047
9048 // first finger up, third finger receive move.
9049 processSlot(mapper, FIRST_SLOT);
9050 processId(mapper, INVALID_TRACKING_ID);
9051 processSync(mapper);
9052 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9053 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9054 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9055
9056 // second finger up, third finger receive move.
9057 processSlot(mapper, SECOND_SLOT);
9058 processId(mapper, INVALID_TRACKING_ID);
9059 processSync(mapper);
9060 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9061 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9062 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9063
9064 // third finger up.
9065 processSlot(mapper, THIRD_SLOT);
9066 processId(mapper, INVALID_TRACKING_ID);
9067 processSync(mapper);
9068 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9069 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9070 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9071}
9072
9073/**
9074 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
9075 * and the active finger could still be allowed to receive the events
9076 */
9077TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_KeepFirstPointer) {
9078 addConfigurationProperty("touch.deviceType", "touchScreen");
9079 prepareDisplay(DISPLAY_ORIENTATION_0);
9080 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
9081 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9082
9083 NotifyMotionArgs motionArgs;
9084
9085 // default tool type is finger
9086 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
9087 processId(mapper, FIRST_TRACKING_ID);
9088 processPosition(mapper, x1, y1);
9089 processSync(mapper);
9090 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9091 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9092 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9093
9094 // Second finger down.
9095 processSlot(mapper, SECOND_SLOT);
9096 processId(mapper, SECOND_TRACKING_ID);
9097 processPosition(mapper, x2, y2);
9098 processSync(mapper);
9099 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009100 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009101 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9102
9103 // If the tool type of the second finger changes to MT_TOOL_PALM,
9104 // we expect to receive ACTION_POINTER_UP with cancel flag.
9105 processId(mapper, SECOND_TRACKING_ID);
9106 processToolType(mapper, MT_TOOL_PALM);
9107 processSync(mapper);
9108 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009109 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009110 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9111
9112 // The following MOVE event should be processed.
9113 processSlot(mapper, FIRST_SLOT);
9114 processId(mapper, FIRST_TRACKING_ID);
9115 processPosition(mapper, x1 + 1, y1 + 1);
9116 processSync(mapper);
9117 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9118 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9119 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9120
9121 // second finger up.
9122 processSlot(mapper, SECOND_SLOT);
9123 processId(mapper, INVALID_TRACKING_ID);
9124 processSync(mapper);
9125 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9126 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9127
9128 // first finger keep moving
9129 processSlot(mapper, FIRST_SLOT);
9130 processId(mapper, FIRST_TRACKING_ID);
9131 processPosition(mapper, x1 + 2, y1 + 2);
9132 processSync(mapper);
9133 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9134 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9135
9136 // first finger up.
9137 processId(mapper, INVALID_TRACKING_ID);
9138 processSync(mapper);
9139 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9140 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9141 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
arthurhungbf89a482020-04-17 17:37:55 +08009142}
9143
Arthur Hung9ad18942021-06-19 02:04:46 +00009144/**
9145 * Test multi-touch should sent ACTION_POINTER_UP/ACTION_UP when received the INVALID_TRACKING_ID,
9146 * to prevent the driver side may send unexpected data after set tracking id as INVALID_TRACKING_ID
9147 * cause slot be valid again.
9148 */
9149TEST_F(MultiTouchInputMapperTest, Process_MultiTouch_WithInvalidTrackingId) {
9150 addConfigurationProperty("touch.deviceType", "touchScreen");
9151 prepareDisplay(DISPLAY_ORIENTATION_0);
9152 prepareAxes(POSITION | ID | SLOT | PRESSURE);
9153 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9154
9155 NotifyMotionArgs motionArgs;
9156
9157 constexpr int32_t x1 = 100, y1 = 200, x2 = 0, y2 = 0;
9158 // First finger down.
9159 processId(mapper, FIRST_TRACKING_ID);
9160 processPosition(mapper, x1, y1);
9161 processPressure(mapper, RAW_PRESSURE_MAX);
9162 processSync(mapper);
9163 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9164 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9165 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9166
9167 // First finger move.
9168 processId(mapper, FIRST_TRACKING_ID);
9169 processPosition(mapper, x1 + 1, y1 + 1);
9170 processPressure(mapper, RAW_PRESSURE_MAX);
9171 processSync(mapper);
9172 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9173 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9174 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9175
9176 // Second finger down.
9177 processSlot(mapper, SECOND_SLOT);
9178 processId(mapper, SECOND_TRACKING_ID);
9179 processPosition(mapper, x2, y2);
9180 processPressure(mapper, RAW_PRESSURE_MAX);
9181 processSync(mapper);
9182 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009183 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Arthur Hung9ad18942021-06-19 02:04:46 +00009184 ASSERT_EQ(uint32_t(2), motionArgs.pointerCount);
9185
9186 // second finger up with some unexpected data.
9187 processSlot(mapper, SECOND_SLOT);
9188 processId(mapper, INVALID_TRACKING_ID);
9189 processPosition(mapper, x2, y2);
9190 processSync(mapper);
9191 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009192 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Arthur Hung9ad18942021-06-19 02:04:46 +00009193 ASSERT_EQ(uint32_t(2), motionArgs.pointerCount);
9194
9195 // first finger up with some unexpected data.
9196 processSlot(mapper, FIRST_SLOT);
9197 processId(mapper, INVALID_TRACKING_ID);
9198 processPosition(mapper, x2, y2);
9199 processPressure(mapper, RAW_PRESSURE_MAX);
9200 processSync(mapper);
9201 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9202 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9203 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9204}
9205
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009206// --- MultiTouchInputMapperTest_ExternalDevice ---
9207
9208class MultiTouchInputMapperTest_ExternalDevice : public MultiTouchInputMapperTest {
9209protected:
Chris Yea52ade12020-08-27 16:49:20 -07009210 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009211};
9212
9213/**
9214 * Expect fallback to internal viewport if device is external and external viewport is not present.
9215 */
9216TEST_F(MultiTouchInputMapperTest_ExternalDevice, Viewports_Fallback) {
9217 prepareAxes(POSITION);
9218 addConfigurationProperty("touch.deviceType", "touchScreen");
9219 prepareDisplay(DISPLAY_ORIENTATION_0);
9220 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9221
9222 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
9223
9224 NotifyMotionArgs motionArgs;
9225
9226 // Expect the event to be sent to the internal viewport,
9227 // because an external viewport is not present.
9228 processPosition(mapper, 100, 100);
9229 processSync(mapper);
9230 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9231 ASSERT_EQ(ADISPLAY_ID_DEFAULT, motionArgs.displayId);
9232
9233 // Expect the event to be sent to the external viewport if it is present.
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009234 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009235 processPosition(mapper, 100, 100);
9236 processSync(mapper);
9237 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9238 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
9239}
Arthur Hung4197f6b2020-03-16 15:39:59 +08009240
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009241TEST_F(MultiTouchInputMapperTest, Process_TouchpadCapture) {
9242 // we need a pointer controller for mouse mode of touchpad (start pointer at 0,0)
9243 std::shared_ptr<FakePointerController> fakePointerController =
9244 std::make_shared<FakePointerController>();
9245 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
9246 fakePointerController->setPosition(0, 0);
9247 fakePointerController->setButtonState(0);
9248
9249 // prepare device and capture
9250 prepareDisplay(DISPLAY_ORIENTATION_0);
9251 prepareAxes(POSITION | ID | SLOT);
9252 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
9253 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
9254 mFakePolicy->setPointerCapture(true);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009255 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009256 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9257
9258 // captured touchpad should be a touchpad source
9259 NotifyDeviceResetArgs resetArgs;
9260 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
9261 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
9262
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009263 InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
Chris Yef74dc422020-09-02 22:41:50 -07009264
9265 const InputDeviceInfo::MotionRange* relRangeX =
9266 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_X, AINPUT_SOURCE_TOUCHPAD);
9267 ASSERT_NE(relRangeX, nullptr);
9268 ASSERT_EQ(relRangeX->min, -(RAW_X_MAX - RAW_X_MIN));
9269 ASSERT_EQ(relRangeX->max, RAW_X_MAX - RAW_X_MIN);
9270 const InputDeviceInfo::MotionRange* relRangeY =
9271 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_Y, AINPUT_SOURCE_TOUCHPAD);
9272 ASSERT_NE(relRangeY, nullptr);
9273 ASSERT_EQ(relRangeY->min, -(RAW_Y_MAX - RAW_Y_MIN));
9274 ASSERT_EQ(relRangeY->max, RAW_Y_MAX - RAW_Y_MIN);
9275
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009276 // run captured pointer tests - note that this is unscaled, so input listener events should be
9277 // identical to what the hardware sends (accounting for any
9278 // calibration).
9279 // FINGER 0 DOWN
Chris Ye364fdb52020-08-05 15:07:56 -07009280 processSlot(mapper, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009281 processId(mapper, 1);
9282 processPosition(mapper, 100 + RAW_X_MIN, 100 + RAW_Y_MIN);
9283 processKey(mapper, BTN_TOUCH, 1);
9284 processSync(mapper);
9285
9286 // expect coord[0] to contain initial location of touch 0
9287 NotifyMotionArgs args;
9288 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9289 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
9290 ASSERT_EQ(1U, args.pointerCount);
9291 ASSERT_EQ(0, args.pointerProperties[0].id);
9292 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, args.source);
9293 ASSERT_NO_FATAL_FAILURE(
9294 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
9295
9296 // FINGER 1 DOWN
9297 processSlot(mapper, 1);
9298 processId(mapper, 2);
9299 processPosition(mapper, 560 + RAW_X_MIN, 154 + RAW_Y_MIN);
9300 processSync(mapper);
9301
9302 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
9303 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009304 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009305 ASSERT_EQ(2U, args.pointerCount);
9306 ASSERT_EQ(0, args.pointerProperties[0].id);
9307 ASSERT_EQ(1, args.pointerProperties[1].id);
9308 ASSERT_NO_FATAL_FAILURE(
9309 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
9310 ASSERT_NO_FATAL_FAILURE(
9311 assertPointerCoords(args.pointerCoords[1], 560, 154, 1, 0, 0, 0, 0, 0, 0, 0));
9312
9313 // FINGER 1 MOVE
9314 processPosition(mapper, 540 + RAW_X_MIN, 690 + RAW_Y_MIN);
9315 processSync(mapper);
9316
9317 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
9318 // from move
9319 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9320 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9321 ASSERT_NO_FATAL_FAILURE(
9322 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
9323 ASSERT_NO_FATAL_FAILURE(
9324 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
9325
9326 // FINGER 0 MOVE
9327 processSlot(mapper, 0);
9328 processPosition(mapper, 50 + RAW_X_MIN, 800 + RAW_Y_MIN);
9329 processSync(mapper);
9330
9331 // expect coord[0] to contain new touch 0 location, coord[1] to contain previous location
9332 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9333 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9334 ASSERT_NO_FATAL_FAILURE(
9335 assertPointerCoords(args.pointerCoords[0], 50, 800, 1, 0, 0, 0, 0, 0, 0, 0));
9336 ASSERT_NO_FATAL_FAILURE(
9337 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
9338
9339 // BUTTON DOWN
9340 processKey(mapper, BTN_LEFT, 1);
9341 processSync(mapper);
9342
9343 // touchinputmapper design sends a move before button press
9344 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9345 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9346 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9347 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
9348
9349 // BUTTON UP
9350 processKey(mapper, BTN_LEFT, 0);
9351 processSync(mapper);
9352
9353 // touchinputmapper design sends a move after button release
9354 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9355 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
9356 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9357 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9358
9359 // FINGER 0 UP
9360 processId(mapper, -1);
9361 processSync(mapper);
9362 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9363 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | 0x0000, args.action);
9364
9365 // FINGER 1 MOVE
9366 processSlot(mapper, 1);
9367 processPosition(mapper, 320 + RAW_X_MIN, 900 + RAW_Y_MIN);
9368 processSync(mapper);
9369
9370 // expect coord[0] to contain new location of touch 1, and properties[0].id to contain 1
9371 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9372 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9373 ASSERT_EQ(1U, args.pointerCount);
9374 ASSERT_EQ(1, args.pointerProperties[0].id);
9375 ASSERT_NO_FATAL_FAILURE(
9376 assertPointerCoords(args.pointerCoords[0], 320, 900, 1, 0, 0, 0, 0, 0, 0, 0));
9377
9378 // FINGER 1 UP
9379 processId(mapper, -1);
9380 processKey(mapper, BTN_TOUCH, 0);
9381 processSync(mapper);
9382 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9383 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
9384
9385 // non captured touchpad should be a mouse source
9386 mFakePolicy->setPointerCapture(false);
9387 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
9388 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
9389 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
9390}
9391
9392TEST_F(MultiTouchInputMapperTest, Process_UnCapturedTouchpadPointer) {
9393 std::shared_ptr<FakePointerController> fakePointerController =
9394 std::make_shared<FakePointerController>();
9395 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
9396 fakePointerController->setPosition(0, 0);
9397 fakePointerController->setButtonState(0);
9398
9399 // prepare device and capture
9400 prepareDisplay(DISPLAY_ORIENTATION_0);
9401 prepareAxes(POSITION | ID | SLOT);
9402 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
9403 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009404 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009405 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9406 // run uncaptured pointer tests - pushes out generic events
9407 // FINGER 0 DOWN
9408 processId(mapper, 3);
9409 processPosition(mapper, 100, 100);
9410 processKey(mapper, BTN_TOUCH, 1);
9411 processSync(mapper);
9412
9413 // start at (100,100), cursor should be at (0,0) * scale
9414 NotifyMotionArgs args;
9415 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9416 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
9417 ASSERT_NO_FATAL_FAILURE(
9418 assertPointerCoords(args.pointerCoords[0], 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
9419
9420 // FINGER 0 MOVE
9421 processPosition(mapper, 200, 200);
9422 processSync(mapper);
9423
9424 // compute scaling to help with touch position checking
9425 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
9426 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
9427 float scale =
9428 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
9429
9430 // translate from (100,100) -> (200,200), cursor should have changed to (100,100) * scale)
9431 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9432 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
9433 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 100 * scale, 100 * scale, 0,
9434 0, 0, 0, 0, 0, 0, 0));
9435}
9436
9437TEST_F(MultiTouchInputMapperTest, WhenCapturedAndNotCaptured_GetSources) {
9438 std::shared_ptr<FakePointerController> fakePointerController =
9439 std::make_shared<FakePointerController>();
9440
9441 prepareDisplay(DISPLAY_ORIENTATION_0);
9442 prepareAxes(POSITION | ID | SLOT);
9443 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009444 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009445 mFakePolicy->setPointerCapture(false);
9446 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9447
9448 // uncaptured touchpad should be a pointer device
9449 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
9450
9451 // captured touchpad should be a touchpad device
9452 mFakePolicy->setPointerCapture(true);
9453 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
9454 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
9455}
9456
Arthur Hung6d5b4b22022-01-21 07:21:10 +00009457// --- JoystickInputMapperTest ---
9458
9459class JoystickInputMapperTest : public InputMapperTest {
9460protected:
9461 static const int32_t RAW_X_MIN;
9462 static const int32_t RAW_X_MAX;
9463 static const int32_t RAW_Y_MIN;
9464 static const int32_t RAW_Y_MAX;
9465
9466 void SetUp() override {
9467 InputMapperTest::SetUp(InputDeviceClass::JOYSTICK | InputDeviceClass::EXTERNAL);
9468 }
9469 void prepareAxes() {
9470 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
9471 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
9472 }
9473
9474 void processAxis(JoystickInputMapper& mapper, int32_t axis, int32_t value) {
9475 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, axis, value);
9476 }
9477
9478 void processSync(JoystickInputMapper& mapper) {
9479 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
9480 }
9481
9482 void prepareVirtualDisplay(int32_t orientation) {
9483 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH,
9484 VIRTUAL_DISPLAY_HEIGHT, orientation, VIRTUAL_DISPLAY_UNIQUE_ID,
9485 NO_PORT, ViewportType::VIRTUAL);
9486 }
9487};
9488
9489const int32_t JoystickInputMapperTest::RAW_X_MIN = -32767;
9490const int32_t JoystickInputMapperTest::RAW_X_MAX = 32767;
9491const int32_t JoystickInputMapperTest::RAW_Y_MIN = -32767;
9492const int32_t JoystickInputMapperTest::RAW_Y_MAX = 32767;
9493
9494TEST_F(JoystickInputMapperTest, Configure_AssignsDisplayUniqueId) {
9495 prepareAxes();
9496 JoystickInputMapper& mapper = addMapperAndConfigure<JoystickInputMapper>();
9497
9498 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
9499
9500 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
9501
9502 // Send an axis event
9503 processAxis(mapper, ABS_X, 100);
9504 processSync(mapper);
9505
9506 NotifyMotionArgs args;
9507 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9508 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
9509
9510 // Send another axis event
9511 processAxis(mapper, ABS_Y, 100);
9512 processSync(mapper);
9513
9514 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9515 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
9516}
9517
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009518// --- PeripheralControllerTest ---
Chris Yee2b1e5c2021-03-10 22:45:12 -08009519
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009520class PeripheralControllerTest : public testing::Test {
Chris Yee2b1e5c2021-03-10 22:45:12 -08009521protected:
9522 static const char* DEVICE_NAME;
9523 static const char* DEVICE_LOCATION;
9524 static const int32_t DEVICE_ID;
9525 static const int32_t DEVICE_GENERATION;
9526 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07009527 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Chris Yee2b1e5c2021-03-10 22:45:12 -08009528 static const int32_t EVENTHUB_ID;
9529
9530 std::shared_ptr<FakeEventHub> mFakeEventHub;
9531 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07009532 std::unique_ptr<TestInputListener> mFakeListener;
Chris Yee2b1e5c2021-03-10 22:45:12 -08009533 std::unique_ptr<InstrumentedInputReader> mReader;
9534 std::shared_ptr<InputDevice> mDevice;
9535
Dominik Laskowski2f01d772022-03-23 16:01:29 -07009536 virtual void SetUp(ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -08009537 mFakeEventHub = std::make_unique<FakeEventHub>();
9538 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07009539 mFakeListener = std::make_unique<TestInputListener>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009540 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07009541 *mFakeListener);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009542 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
9543 }
9544
9545 void SetUp() override { SetUp(DEVICE_CLASSES); }
9546
9547 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07009548 mFakeListener.reset();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009549 mFakePolicy.clear();
9550 }
9551
9552 void configureDevice(uint32_t changes) {
9553 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
9554 mReader->requestRefreshConfiguration(changes);
9555 mReader->loopOnce();
9556 }
9557 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
9558 }
9559
9560 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
9561 const std::string& location, int32_t eventHubId,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07009562 ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -08009563 InputDeviceIdentifier identifier;
9564 identifier.name = name;
9565 identifier.location = location;
9566 std::shared_ptr<InputDevice> device =
9567 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
9568 identifier);
9569 mReader->pushNextDevice(device);
9570 mFakeEventHub->addDevice(eventHubId, name, classes);
9571 mReader->loopOnce();
9572 return device;
9573 }
9574
9575 template <class T, typename... Args>
9576 T& addControllerAndConfigure(Args... args) {
9577 T& controller = mDevice->addController<T>(EVENTHUB_ID, args...);
9578
9579 return controller;
9580 }
9581};
9582
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009583const char* PeripheralControllerTest::DEVICE_NAME = "device";
9584const char* PeripheralControllerTest::DEVICE_LOCATION = "BLUETOOTH";
9585const int32_t PeripheralControllerTest::DEVICE_ID = END_RESERVED_ID + 1000;
9586const int32_t PeripheralControllerTest::DEVICE_GENERATION = 2;
9587const int32_t PeripheralControllerTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07009588const ftl::Flags<InputDeviceClass> PeripheralControllerTest::DEVICE_CLASSES =
9589 ftl::Flags<InputDeviceClass>(0); // not needed for current tests
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009590const int32_t PeripheralControllerTest::EVENTHUB_ID = 1;
Chris Yee2b1e5c2021-03-10 22:45:12 -08009591
9592// --- BatteryControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009593class BatteryControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -08009594protected:
9595 void SetUp() override {
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009596 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::BATTERY);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009597 }
9598};
9599
9600TEST_F(BatteryControllerTest, GetBatteryCapacity) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009601 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009602
9603 ASSERT_TRUE(controller.getBatteryCapacity(DEFAULT_BATTERY));
9604 ASSERT_EQ(controller.getBatteryCapacity(DEFAULT_BATTERY).value_or(-1), BATTERY_CAPACITY);
9605}
9606
9607TEST_F(BatteryControllerTest, GetBatteryStatus) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009608 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009609
9610 ASSERT_TRUE(controller.getBatteryStatus(DEFAULT_BATTERY));
9611 ASSERT_EQ(controller.getBatteryStatus(DEFAULT_BATTERY).value_or(-1), BATTERY_STATUS);
9612}
9613
9614// --- LightControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009615class LightControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -08009616protected:
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009617 void SetUp() override {
9618 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::LIGHT);
9619 }
Chris Yee2b1e5c2021-03-10 22:45:12 -08009620};
9621
Chris Ye85758332021-05-16 23:05:17 -07009622TEST_F(LightControllerTest, MonoLight) {
9623 RawLightInfo infoMono = {.id = 1,
9624 .name = "Mono",
9625 .maxBrightness = 255,
9626 .flags = InputLightClass::BRIGHTNESS,
9627 .path = ""};
9628 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
Chris Yee2b1e5c2021-03-10 22:45:12 -08009629
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009630 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009631 InputDeviceInfo info;
9632 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009633 std::vector<InputDeviceLightInfo> lights = info.getLights();
9634 ASSERT_EQ(1U, lights.size());
9635 ASSERT_EQ(InputDeviceLightType::MONO, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009636
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009637 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
9638 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009639}
9640
9641TEST_F(LightControllerTest, RGBLight) {
9642 RawLightInfo infoRed = {.id = 1,
9643 .name = "red",
9644 .maxBrightness = 255,
9645 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
9646 .path = ""};
9647 RawLightInfo infoGreen = {.id = 2,
9648 .name = "green",
9649 .maxBrightness = 255,
9650 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
9651 .path = ""};
9652 RawLightInfo infoBlue = {.id = 3,
9653 .name = "blue",
9654 .maxBrightness = 255,
9655 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
9656 .path = ""};
9657 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
9658 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
9659 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
9660
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009661 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009662 InputDeviceInfo info;
9663 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009664 std::vector<InputDeviceLightInfo> lights = info.getLights();
9665 ASSERT_EQ(1U, lights.size());
9666 ASSERT_EQ(InputDeviceLightType::RGB, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009667
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009668 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
9669 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009670}
9671
9672TEST_F(LightControllerTest, MultiColorRGBLight) {
9673 RawLightInfo infoColor = {.id = 1,
9674 .name = "red",
9675 .maxBrightness = 255,
9676 .flags = InputLightClass::BRIGHTNESS |
9677 InputLightClass::MULTI_INTENSITY |
9678 InputLightClass::MULTI_INDEX,
9679 .path = ""};
9680
9681 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
9682
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009683 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009684 InputDeviceInfo info;
9685 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009686 std::vector<InputDeviceLightInfo> lights = info.getLights();
9687 ASSERT_EQ(1U, lights.size());
9688 ASSERT_EQ(InputDeviceLightType::MULTI_COLOR, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009689
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009690 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
9691 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009692}
9693
9694TEST_F(LightControllerTest, PlayerIdLight) {
9695 RawLightInfo info1 = {.id = 1,
9696 .name = "player1",
9697 .maxBrightness = 255,
9698 .flags = InputLightClass::BRIGHTNESS,
9699 .path = ""};
9700 RawLightInfo info2 = {.id = 2,
9701 .name = "player2",
9702 .maxBrightness = 255,
9703 .flags = InputLightClass::BRIGHTNESS,
9704 .path = ""};
9705 RawLightInfo info3 = {.id = 3,
9706 .name = "player3",
9707 .maxBrightness = 255,
9708 .flags = InputLightClass::BRIGHTNESS,
9709 .path = ""};
9710 RawLightInfo info4 = {.id = 4,
9711 .name = "player4",
9712 .maxBrightness = 255,
9713 .flags = InputLightClass::BRIGHTNESS,
9714 .path = ""};
9715 mFakeEventHub->addRawLightInfo(info1.id, std::move(info1));
9716 mFakeEventHub->addRawLightInfo(info2.id, std::move(info2));
9717 mFakeEventHub->addRawLightInfo(info3.id, std::move(info3));
9718 mFakeEventHub->addRawLightInfo(info4.id, std::move(info4));
9719
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009720 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009721 InputDeviceInfo info;
9722 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009723 std::vector<InputDeviceLightInfo> lights = info.getLights();
9724 ASSERT_EQ(1U, lights.size());
9725 ASSERT_EQ(InputDeviceLightType::PLAYER_ID, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009726
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009727 ASSERT_FALSE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
9728 ASSERT_TRUE(controller.setLightPlayerId(lights[0].id, LIGHT_PLAYER_ID));
9729 ASSERT_EQ(controller.getLightPlayerId(lights[0].id).value_or(-1), LIGHT_PLAYER_ID);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009730}
9731
Michael Wrightd02c5b62014-02-10 15:10:22 -08009732} // namespace android