blob: 5d6ec7487afb2606844e0102f7d320bb0fc9a1a0 [file] [log] [blame]
Michael Wrightd02c5b62014-02-10 15:10:22 -08001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Dominik Laskowski2f01d772022-03-23 16:01:29 -070017#include <cinttypes>
18#include <memory>
19
Prabir Pradhan2770d242019-09-02 18:07:11 -070020#include <CursorInputMapper.h>
21#include <InputDevice.h>
22#include <InputMapper.h>
23#include <InputReader.h>
Prabir Pradhan1aed8582019-12-30 11:46:51 -080024#include <InputReaderBase.h>
25#include <InputReaderFactory.h>
Arthur Hung6d5b4b22022-01-21 07:21:10 +000026#include <JoystickInputMapper.h>
Prabir Pradhan2770d242019-09-02 18:07:11 -070027#include <KeyboardInputMapper.h>
28#include <MultiTouchInputMapper.h>
Chris Ye1dd2e5c2021-04-04 23:12:41 -070029#include <PeripheralController.h>
Chris Yef59a2f42020-10-16 12:55:26 -070030#include <SensorInputMapper.h>
Prabir Pradhan2770d242019-09-02 18:07:11 -070031#include <SingleTouchInputMapper.h>
32#include <SwitchInputMapper.h>
33#include <TestInputListener.h>
34#include <TouchInputMapper.h>
Prabir Pradhan1aed8582019-12-30 11:46:51 -080035#include <UinputDevice.h>
Chris Ye87143712020-11-10 05:05:58 +000036#include <VibratorInputMapper.h>
Prabir Pradhan2574dfa2019-10-16 16:35:07 -070037#include <android-base/thread_annotations.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080038#include <gtest/gtest.h>
chaviw3277faf2021-05-19 16:45:23 -050039#include <gui/constants.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080040
Michael Wrightdde67b82020-10-27 16:09:22 +000041#include "input/DisplayViewport.h"
42#include "input/Input.h"
Michael Wright17db18e2020-06-26 20:51:44 +010043
Michael Wrightd02c5b62014-02-10 15:10:22 -080044namespace android {
45
Dominik Laskowski2f01d772022-03-23 16:01:29 -070046using namespace ftl::flag_operators;
47
Prabir Pradhan2574dfa2019-10-16 16:35:07 -070048using std::chrono_literals::operator""ms;
49
50// Timeout for waiting for an expected event
51static constexpr std::chrono::duration WAIT_TIMEOUT = 100ms;
52
Michael Wrightd02c5b62014-02-10 15:10:22 -080053// An arbitrary time value.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +000054static constexpr nsecs_t ARBITRARY_TIME = 1234;
55static constexpr nsecs_t READ_TIME = 4321;
Michael Wrightd02c5b62014-02-10 15:10:22 -080056
57// Arbitrary display properties.
arthurhungcc7f9802020-04-30 17:55:40 +080058static constexpr int32_t DISPLAY_ID = 0;
59static constexpr int32_t SECONDARY_DISPLAY_ID = DISPLAY_ID + 1;
60static constexpr int32_t DISPLAY_WIDTH = 480;
61static constexpr int32_t DISPLAY_HEIGHT = 800;
62static constexpr int32_t VIRTUAL_DISPLAY_ID = 1;
63static constexpr int32_t VIRTUAL_DISPLAY_WIDTH = 400;
64static constexpr int32_t VIRTUAL_DISPLAY_HEIGHT = 500;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -070065static const char* VIRTUAL_DISPLAY_UNIQUE_ID = "virtual:1";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -070066static constexpr std::optional<uint8_t> NO_PORT = std::nullopt; // no physical port is specified
Michael Wrightd02c5b62014-02-10 15:10:22 -080067
arthurhungcc7f9802020-04-30 17:55:40 +080068static constexpr int32_t FIRST_SLOT = 0;
69static constexpr int32_t SECOND_SLOT = 1;
70static constexpr int32_t THIRD_SLOT = 2;
71static constexpr int32_t INVALID_TRACKING_ID = -1;
72static constexpr int32_t FIRST_TRACKING_ID = 0;
73static constexpr int32_t SECOND_TRACKING_ID = 1;
74static constexpr int32_t THIRD_TRACKING_ID = 2;
Chris Yee2b1e5c2021-03-10 22:45:12 -080075static constexpr int32_t DEFAULT_BATTERY = 1;
Kim Low03ea0352020-11-06 12:45:07 -080076static constexpr int32_t BATTERY_STATUS = 4;
77static constexpr int32_t BATTERY_CAPACITY = 66;
Chris Ye3fdbfef2021-01-06 18:45:18 -080078static constexpr int32_t LIGHT_BRIGHTNESS = 0x55000000;
79static constexpr int32_t LIGHT_COLOR = 0x7F448866;
80static constexpr int32_t LIGHT_PLAYER_ID = 2;
arthurhungcc7f9802020-04-30 17:55:40 +080081
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080082static constexpr int32_t ACTION_POINTER_0_DOWN =
83 AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
84static constexpr int32_t ACTION_POINTER_0_UP =
85 AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
86static constexpr int32_t ACTION_POINTER_1_DOWN =
87 AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
88static constexpr int32_t ACTION_POINTER_1_UP =
89 AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
90
Michael Wrightd02c5b62014-02-10 15:10:22 -080091// Error tolerance for floating point assertions.
92static const float EPSILON = 0.001f;
93
94template<typename T>
95static inline T min(T a, T b) {
96 return a < b ? a : b;
97}
98
99static inline float avg(float x, float y) {
100 return (x + y) / 2;
101}
102
Chris Ye3fdbfef2021-01-06 18:45:18 -0800103// Mapping for light color name and the light color
104const std::unordered_map<std::string, LightColor> LIGHT_COLORS = {{"red", LightColor::RED},
105 {"green", LightColor::GREEN},
106 {"blue", LightColor::BLUE}};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800107
Prabir Pradhanc14266f2021-05-12 15:56:24 -0700108static int32_t getInverseRotation(int32_t orientation) {
109 switch (orientation) {
110 case DISPLAY_ORIENTATION_90:
111 return DISPLAY_ORIENTATION_270;
112 case DISPLAY_ORIENTATION_270:
113 return DISPLAY_ORIENTATION_90;
114 default:
115 return orientation;
116 }
117}
118
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800119static void assertAxisResolution(MultiTouchInputMapper& mapper, int axis, float resolution) {
120 InputDeviceInfo info;
121 mapper.populateDeviceInfo(&info);
122
123 const InputDeviceInfo::MotionRange* motionRange =
124 info.getMotionRange(axis, AINPUT_SOURCE_TOUCHSCREEN);
125 ASSERT_NEAR(motionRange->resolution, resolution, EPSILON);
126}
127
128static void assertAxisNotPresent(MultiTouchInputMapper& mapper, int axis) {
129 InputDeviceInfo info;
130 mapper.populateDeviceInfo(&info);
131
132 const InputDeviceInfo::MotionRange* motionRange =
133 info.getMotionRange(axis, AINPUT_SOURCE_TOUCHSCREEN);
134 ASSERT_EQ(nullptr, motionRange);
135}
136
Michael Wrightd02c5b62014-02-10 15:10:22 -0800137// --- FakePointerController ---
138
139class FakePointerController : public PointerControllerInterface {
140 bool mHaveBounds;
141 float mMinX, mMinY, mMaxX, mMaxY;
142 float mX, mY;
143 int32_t mButtonState;
Arthur Hungc7ad2d02018-12-18 17:41:29 +0800144 int32_t mDisplayId;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800145
Michael Wrightd02c5b62014-02-10 15:10:22 -0800146public:
147 FakePointerController() :
148 mHaveBounds(false), mMinX(0), mMinY(0), mMaxX(0), mMaxY(0), mX(0), mY(0),
Arthur Hungc7ad2d02018-12-18 17:41:29 +0800149 mButtonState(0), mDisplayId(ADISPLAY_ID_DEFAULT) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800150 }
151
Michael Wright17db18e2020-06-26 20:51:44 +0100152 virtual ~FakePointerController() {}
153
Michael Wrightd02c5b62014-02-10 15:10:22 -0800154 void setBounds(float minX, float minY, float maxX, float maxY) {
155 mHaveBounds = true;
156 mMinX = minX;
157 mMinY = minY;
158 mMaxX = maxX;
159 mMaxY = maxY;
160 }
161
Chris Yea52ade12020-08-27 16:49:20 -0700162 void setPosition(float x, float y) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800163 mX = x;
164 mY = y;
165 }
166
Chris Yea52ade12020-08-27 16:49:20 -0700167 void setButtonState(int32_t buttonState) override { mButtonState = buttonState; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800168
Chris Yea52ade12020-08-27 16:49:20 -0700169 int32_t getButtonState() const override { return mButtonState; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800170
Chris Yea52ade12020-08-27 16:49:20 -0700171 void getPosition(float* outX, float* outY) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800172 *outX = mX;
173 *outY = mY;
174 }
175
Chris Yea52ade12020-08-27 16:49:20 -0700176 int32_t getDisplayId() const override { return mDisplayId; }
Arthur Hungc7ad2d02018-12-18 17:41:29 +0800177
Chris Yea52ade12020-08-27 16:49:20 -0700178 void setDisplayViewport(const DisplayViewport& viewport) override {
Garfield Tan888a6a42020-01-09 11:39:16 -0800179 mDisplayId = viewport.displayId;
180 }
181
Arthur Hung7c645402019-01-25 17:45:42 +0800182 const std::map<int32_t, std::vector<int32_t>>& getSpots() {
183 return mSpotsByDisplay;
184 }
185
Michael Wrightd02c5b62014-02-10 15:10:22 -0800186private:
Chris Yea52ade12020-08-27 16:49:20 -0700187 bool getBounds(float* outMinX, float* outMinY, float* outMaxX, float* outMaxY) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800188 *outMinX = mMinX;
189 *outMinY = mMinY;
190 *outMaxX = mMaxX;
191 *outMaxY = mMaxY;
192 return mHaveBounds;
193 }
194
Chris Yea52ade12020-08-27 16:49:20 -0700195 void move(float deltaX, float deltaY) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800196 mX += deltaX;
197 if (mX < mMinX) mX = mMinX;
198 if (mX > mMaxX) mX = mMaxX;
199 mY += deltaY;
200 if (mY < mMinY) mY = mMinY;
201 if (mY > mMaxY) mY = mMaxY;
202 }
203
Chris Yea52ade12020-08-27 16:49:20 -0700204 void fade(Transition) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800205
Chris Yea52ade12020-08-27 16:49:20 -0700206 void unfade(Transition) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800207
Chris Yea52ade12020-08-27 16:49:20 -0700208 void setPresentation(Presentation) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800209
Chris Yea52ade12020-08-27 16:49:20 -0700210 void setSpots(const PointerCoords*, const uint32_t*, BitSet32 spotIdBits,
211 int32_t displayId) override {
Arthur Hung7c645402019-01-25 17:45:42 +0800212 std::vector<int32_t> newSpots;
213 // Add spots for fingers that are down.
214 for (BitSet32 idBits(spotIdBits); !idBits.isEmpty(); ) {
215 uint32_t id = idBits.clearFirstMarkedBit();
216 newSpots.push_back(id);
217 }
218
219 mSpotsByDisplay[displayId] = newSpots;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800220 }
221
Prabir Pradhan197e0862022-07-01 14:28:00 +0000222 void clearSpots() override { mSpotsByDisplay.clear(); }
Arthur Hung7c645402019-01-25 17:45:42 +0800223
224 std::map<int32_t, std::vector<int32_t>> mSpotsByDisplay;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800225};
226
227
228// --- FakeInputReaderPolicy ---
229
230class FakeInputReaderPolicy : public InputReaderPolicyInterface {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700231 std::mutex mLock;
232 std::condition_variable mDevicesChangedCondition;
233
Michael Wrightd02c5b62014-02-10 15:10:22 -0800234 InputReaderConfiguration mConfig;
Prabir Pradhan2853b7a2021-08-23 14:08:51 +0000235 std::shared_ptr<FakePointerController> mPointerController;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700236 std::vector<InputDeviceInfo> mInputDevices GUARDED_BY(mLock);
237 bool mInputDevicesChanged GUARDED_BY(mLock){false};
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100238 std::vector<DisplayViewport> mViewports;
Jason Gerecke489fda82012-09-07 17:19:40 -0700239 TouchAffineTransformation transform;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800240
241protected:
Chris Yea52ade12020-08-27 16:49:20 -0700242 virtual ~FakeInputReaderPolicy() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800243
244public:
245 FakeInputReaderPolicy() {
246 }
247
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700248 void assertInputDevicesChanged() {
Prabir Pradhan1aed8582019-12-30 11:46:51 -0800249 waitForInputDevices([](bool devicesChanged) {
250 if (!devicesChanged) {
251 FAIL() << "Timed out waiting for notifyInputDevicesChanged() to be called.";
252 }
253 });
254 }
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700255
Prabir Pradhan1aed8582019-12-30 11:46:51 -0800256 void assertInputDevicesNotChanged() {
257 waitForInputDevices([](bool devicesChanged) {
258 if (devicesChanged) {
259 FAIL() << "Expected notifyInputDevicesChanged() to not be called.";
260 }
261 });
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700262 }
263
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700264 virtual void clearViewports() {
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100265 mViewports.clear();
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100266 mConfig.setDisplayViewports(mViewports);
Santos Cordonfa5cf462017-04-05 10:37:00 -0700267 }
268
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700269 std::optional<DisplayViewport> getDisplayViewportByUniqueId(const std::string& uniqueId) const {
270 return mConfig.getDisplayViewportByUniqueId(uniqueId);
271 }
272 std::optional<DisplayViewport> getDisplayViewportByType(ViewportType type) const {
273 return mConfig.getDisplayViewportByType(type);
274 }
275
276 std::optional<DisplayViewport> getDisplayViewportByPort(uint8_t displayPort) const {
277 return mConfig.getDisplayViewportByPort(displayPort);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700278 }
279
Prabir Pradhan5632d622021-09-06 07:57:20 -0700280 void addDisplayViewport(DisplayViewport viewport) {
281 mViewports.push_back(std::move(viewport));
282 mConfig.setDisplayViewports(mViewports);
283 }
284
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700285 void addDisplayViewport(int32_t displayId, int32_t width, int32_t height, int32_t orientation,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +0000286 bool isActive, const std::string& uniqueId,
Prabir Pradhan5632d622021-09-06 07:57:20 -0700287 std::optional<uint8_t> physicalPort, ViewportType type) {
288 const bool isRotated =
289 (orientation == DISPLAY_ORIENTATION_90 || orientation == DISPLAY_ORIENTATION_270);
290 DisplayViewport v;
291 v.displayId = displayId;
292 v.orientation = orientation;
293 v.logicalLeft = 0;
294 v.logicalTop = 0;
295 v.logicalRight = isRotated ? height : width;
296 v.logicalBottom = isRotated ? width : height;
297 v.physicalLeft = 0;
298 v.physicalTop = 0;
299 v.physicalRight = isRotated ? height : width;
300 v.physicalBottom = isRotated ? width : height;
301 v.deviceWidth = isRotated ? height : width;
302 v.deviceHeight = isRotated ? width : height;
303 v.isActive = isActive;
304 v.uniqueId = uniqueId;
305 v.physicalPort = physicalPort;
306 v.type = type;
307
308 addDisplayViewport(v);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800309 }
310
Arthur Hung6cd19a42019-08-30 19:04:12 +0800311 bool updateViewport(const DisplayViewport& viewport) {
312 size_t count = mViewports.size();
313 for (size_t i = 0; i < count; i++) {
314 const DisplayViewport& currentViewport = mViewports[i];
315 if (currentViewport.displayId == viewport.displayId) {
316 mViewports[i] = viewport;
317 mConfig.setDisplayViewports(mViewports);
318 return true;
319 }
320 }
321 // no viewport found.
322 return false;
323 }
324
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100325 void addExcludedDeviceName(const std::string& deviceName) {
326 mConfig.excludedDeviceNames.push_back(deviceName);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800327 }
328
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700329 void addInputPortAssociation(const std::string& inputPort, uint8_t displayPort) {
330 mConfig.portAssociations.insert({inputPort, displayPort});
331 }
332
Christine Franks1ba71cc2021-04-07 14:37:42 -0700333 void addInputUniqueIdAssociation(const std::string& inputUniqueId,
334 const std::string& displayUniqueId) {
335 mConfig.uniqueIdAssociations.insert({inputUniqueId, displayUniqueId});
336 }
337
Siarhei Vishniakouc6f61192019-07-23 18:12:31 +0000338 void addDisabledDevice(int32_t deviceId) { mConfig.disabledDevices.insert(deviceId); }
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700339
Siarhei Vishniakouc6f61192019-07-23 18:12:31 +0000340 void removeDisabledDevice(int32_t deviceId) { mConfig.disabledDevices.erase(deviceId); }
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700341
Prabir Pradhan2853b7a2021-08-23 14:08:51 +0000342 void setPointerController(std::shared_ptr<FakePointerController> controller) {
343 mPointerController = std::move(controller);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800344 }
345
346 const InputReaderConfiguration* getReaderConfiguration() const {
347 return &mConfig;
348 }
349
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800350 const std::vector<InputDeviceInfo>& getInputDevices() const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800351 return mInputDevices;
352 }
353
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100354 TouchAffineTransformation getTouchAffineTransformation(const std::string& inputDeviceDescriptor,
Jason Gerecke71b16e82014-03-10 09:47:59 -0700355 int32_t surfaceRotation) {
Jason Gerecke489fda82012-09-07 17:19:40 -0700356 return transform;
357 }
358
359 void setTouchAffineTransformation(const TouchAffineTransformation t) {
360 transform = t;
Jason Gerecke12d6baa2014-01-27 18:34:20 -0800361 }
362
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000363 PointerCaptureRequest setPointerCapture(bool enabled) {
364 mConfig.pointerCaptureRequest = {enabled, mNextPointerCaptureSequenceNumber++};
365 return mConfig.pointerCaptureRequest;
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -0800366 }
367
Arthur Hung7c645402019-01-25 17:45:42 +0800368 void setShowTouches(bool enabled) {
369 mConfig.showTouches = enabled;
370 }
371
Garfield Tan888a6a42020-01-09 11:39:16 -0800372 void setDefaultPointerDisplayId(int32_t pointerDisplayId) {
373 mConfig.defaultPointerDisplayId = pointerDisplayId;
374 }
375
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -0800376 float getPointerGestureMovementSpeedRatio() { return mConfig.pointerGestureMovementSpeedRatio; }
377
Prabir Pradhanf99d6e72022-04-21 15:28:35 +0000378 void setVelocityControlParams(const VelocityControlParameters& params) {
379 mConfig.pointerVelocityControlParameters = params;
380 mConfig.wheelVelocityControlParameters = params;
381 }
382
Michael Wrightd02c5b62014-02-10 15:10:22 -0800383private:
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000384 uint32_t mNextPointerCaptureSequenceNumber = 0;
385
Chris Yea52ade12020-08-27 16:49:20 -0700386 void getReaderConfiguration(InputReaderConfiguration* outConfig) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800387 *outConfig = mConfig;
388 }
389
Prabir Pradhan2853b7a2021-08-23 14:08:51 +0000390 std::shared_ptr<PointerControllerInterface> obtainPointerController(
391 int32_t /*deviceId*/) override {
392 return mPointerController;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800393 }
394
Chris Yea52ade12020-08-27 16:49:20 -0700395 void notifyInputDevicesChanged(const std::vector<InputDeviceInfo>& inputDevices) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700396 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800397 mInputDevices = inputDevices;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700398 mInputDevicesChanged = true;
399 mDevicesChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800400 }
401
Chris Yea52ade12020-08-27 16:49:20 -0700402 std::shared_ptr<KeyCharacterMap> getKeyboardLayoutOverlay(
403 const InputDeviceIdentifier&) override {
Yi Kong9b14ac62018-07-17 13:48:38 -0700404 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800405 }
406
Chris Yea52ade12020-08-27 16:49:20 -0700407 std::string getDeviceAlias(const InputDeviceIdentifier&) override { return ""; }
Prabir Pradhan1aed8582019-12-30 11:46:51 -0800408
409 void waitForInputDevices(std::function<void(bool)> processDevicesChanged) {
410 std::unique_lock<std::mutex> lock(mLock);
411 base::ScopedLockAssertion assumeLocked(mLock);
412
413 const bool devicesChanged =
414 mDevicesChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
415 return mInputDevicesChanged;
416 });
417 ASSERT_NO_FATAL_FAILURE(processDevicesChanged(devicesChanged));
418 mInputDevicesChanged = false;
419 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800420};
421
Michael Wrightd02c5b62014-02-10 15:10:22 -0800422// --- FakeEventHub ---
423
424class FakeEventHub : public EventHubInterface {
425 struct KeyInfo {
426 int32_t keyCode;
427 uint32_t flags;
428 };
429
Chris Yef59a2f42020-10-16 12:55:26 -0700430 struct SensorInfo {
431 InputDeviceSensorType sensorType;
432 int32_t sensorDataIndex;
433 };
434
Michael Wrightd02c5b62014-02-10 15:10:22 -0800435 struct Device {
436 InputDeviceIdentifier identifier;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700437 ftl::Flags<InputDeviceClass> classes;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800438 PropertyMap configuration;
439 KeyedVector<int, RawAbsoluteAxisInfo> absoluteAxes;
440 KeyedVector<int, bool> relativeAxes;
441 KeyedVector<int32_t, int32_t> keyCodeStates;
442 KeyedVector<int32_t, int32_t> scanCodeStates;
443 KeyedVector<int32_t, int32_t> switchStates;
444 KeyedVector<int32_t, int32_t> absoluteAxisValue;
445 KeyedVector<int32_t, KeyInfo> keysByScanCode;
446 KeyedVector<int32_t, KeyInfo> keysByUsageCode;
447 KeyedVector<int32_t, bool> leds;
Philip Junker4af3b3d2021-12-14 10:36:55 +0100448 // fake mapping which would normally come from keyCharacterMap
449 std::unordered_map<int32_t, int32_t> keyCodeMapping;
Chris Yef59a2f42020-10-16 12:55:26 -0700450 std::unordered_map<int32_t, SensorInfo> sensorsByAbsCode;
451 BitArray<MSC_MAX> mscBitmask;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800452 std::vector<VirtualKeyDefinition> virtualKeys;
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700453 bool enabled;
454
455 status_t enable() {
456 enabled = true;
457 return OK;
458 }
459
460 status_t disable() {
461 enabled = false;
462 return OK;
463 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800464
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700465 explicit Device(ftl::Flags<InputDeviceClass> classes) : classes(classes), enabled(true) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800466 };
467
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700468 std::mutex mLock;
469 std::condition_variable mEventsCondition;
470
Michael Wrightd02c5b62014-02-10 15:10:22 -0800471 KeyedVector<int32_t, Device*> mDevices;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100472 std::vector<std::string> mExcludedDevices;
Siarhei Vishniakou370039c2021-02-04 22:09:01 +0000473 std::vector<RawEvent> mEvents GUARDED_BY(mLock);
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600474 std::unordered_map<int32_t /*deviceId*/, std::vector<TouchVideoFrame>> mVideoFrames;
Chris Ye87143712020-11-10 05:05:58 +0000475 std::vector<int32_t> mVibrators = {0, 1};
Chris Ye3fdbfef2021-01-06 18:45:18 -0800476 std::unordered_map<int32_t, RawLightInfo> mRawLightInfos;
477 // Simulates a device light brightness, from light id to light brightness.
478 std::unordered_map<int32_t /* lightId */, int32_t /* brightness*/> mLightBrightness;
479 // Simulates a device light intensities, from light id to light intensities map.
480 std::unordered_map<int32_t /* lightId */, std::unordered_map<LightColor, int32_t>>
481 mLightIntensities;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800482
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700483public:
Michael Wrightd02c5b62014-02-10 15:10:22 -0800484 virtual ~FakeEventHub() {
485 for (size_t i = 0; i < mDevices.size(); i++) {
486 delete mDevices.valueAt(i);
487 }
488 }
489
Michael Wrightd02c5b62014-02-10 15:10:22 -0800490 FakeEventHub() { }
491
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700492 void addDevice(int32_t deviceId, const std::string& name,
493 ftl::Flags<InputDeviceClass> classes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800494 Device* device = new Device(classes);
495 device->identifier.name = name;
496 mDevices.add(deviceId, device);
497
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000498 enqueueEvent(ARBITRARY_TIME, READ_TIME, deviceId, EventHubInterface::DEVICE_ADDED, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800499 }
500
501 void removeDevice(int32_t deviceId) {
502 delete mDevices.valueFor(deviceId);
503 mDevices.removeItem(deviceId);
504
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000505 enqueueEvent(ARBITRARY_TIME, READ_TIME, deviceId, EventHubInterface::DEVICE_REMOVED, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800506 }
507
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700508 bool isDeviceEnabled(int32_t deviceId) {
509 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700510 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700511 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
512 return false;
513 }
514 return device->enabled;
515 }
516
517 status_t enableDevice(int32_t deviceId) {
518 status_t result;
519 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700520 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700521 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
522 return BAD_VALUE;
523 }
524 if (device->enabled) {
525 ALOGW("Duplicate call to %s, device %" PRId32 " already enabled", __func__, deviceId);
526 return OK;
527 }
528 result = device->enable();
529 return result;
530 }
531
532 status_t disableDevice(int32_t deviceId) {
533 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700534 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700535 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
536 return BAD_VALUE;
537 }
538 if (!device->enabled) {
539 ALOGW("Duplicate call to %s, device %" PRId32 " already disabled", __func__, deviceId);
540 return OK;
541 }
542 return device->disable();
543 }
544
Michael Wrightd02c5b62014-02-10 15:10:22 -0800545 void finishDeviceScan() {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000546 enqueueEvent(ARBITRARY_TIME, READ_TIME, 0, EventHubInterface::FINISHED_DEVICE_SCAN, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800547 }
548
549 void addConfigurationProperty(int32_t deviceId, const String8& key, const String8& value) {
550 Device* device = getDevice(deviceId);
551 device->configuration.addProperty(key, value);
552 }
553
554 void addConfigurationMap(int32_t deviceId, const PropertyMap* configuration) {
555 Device* device = getDevice(deviceId);
556 device->configuration.addAll(configuration);
557 }
558
559 void addAbsoluteAxis(int32_t deviceId, int axis,
560 int32_t minValue, int32_t maxValue, int flat, int fuzz, int resolution = 0) {
561 Device* device = getDevice(deviceId);
562
563 RawAbsoluteAxisInfo info;
564 info.valid = true;
565 info.minValue = minValue;
566 info.maxValue = maxValue;
567 info.flat = flat;
568 info.fuzz = fuzz;
569 info.resolution = resolution;
570 device->absoluteAxes.add(axis, info);
571 }
572
573 void addRelativeAxis(int32_t deviceId, int32_t axis) {
574 Device* device = getDevice(deviceId);
575 device->relativeAxes.add(axis, true);
576 }
577
578 void setKeyCodeState(int32_t deviceId, int32_t keyCode, int32_t state) {
579 Device* device = getDevice(deviceId);
580 device->keyCodeStates.replaceValueFor(keyCode, state);
581 }
582
583 void setScanCodeState(int32_t deviceId, int32_t scanCode, int32_t state) {
584 Device* device = getDevice(deviceId);
585 device->scanCodeStates.replaceValueFor(scanCode, state);
586 }
587
588 void setSwitchState(int32_t deviceId, int32_t switchCode, int32_t state) {
589 Device* device = getDevice(deviceId);
590 device->switchStates.replaceValueFor(switchCode, state);
591 }
592
593 void setAbsoluteAxisValue(int32_t deviceId, int32_t axis, int32_t value) {
594 Device* device = getDevice(deviceId);
595 device->absoluteAxisValue.replaceValueFor(axis, value);
596 }
597
598 void addKey(int32_t deviceId, int32_t scanCode, int32_t usageCode,
599 int32_t keyCode, uint32_t flags) {
600 Device* device = getDevice(deviceId);
601 KeyInfo info;
602 info.keyCode = keyCode;
603 info.flags = flags;
604 if (scanCode) {
605 device->keysByScanCode.add(scanCode, info);
606 }
607 if (usageCode) {
608 device->keysByUsageCode.add(usageCode, info);
609 }
610 }
611
Philip Junker4af3b3d2021-12-14 10:36:55 +0100612 void addKeyCodeMapping(int32_t deviceId, int32_t fromKeyCode, int32_t toKeyCode) {
613 Device* device = getDevice(deviceId);
614 device->keyCodeMapping.insert_or_assign(fromKeyCode, toKeyCode);
615 }
616
Michael Wrightd02c5b62014-02-10 15:10:22 -0800617 void addLed(int32_t deviceId, int32_t led, bool initialState) {
618 Device* device = getDevice(deviceId);
619 device->leds.add(led, initialState);
620 }
621
Chris Yef59a2f42020-10-16 12:55:26 -0700622 void addSensorAxis(int32_t deviceId, int32_t absCode, InputDeviceSensorType sensorType,
623 int32_t sensorDataIndex) {
624 Device* device = getDevice(deviceId);
625 SensorInfo info;
626 info.sensorType = sensorType;
627 info.sensorDataIndex = sensorDataIndex;
628 device->sensorsByAbsCode.emplace(absCode, info);
629 }
630
631 void setMscEvent(int32_t deviceId, int32_t mscEvent) {
632 Device* device = getDevice(deviceId);
633 typename BitArray<MSC_MAX>::Buffer buffer;
634 buffer[mscEvent / 32] = 1 << mscEvent % 32;
635 device->mscBitmask.loadFromBuffer(buffer);
636 }
637
Chris Ye3fdbfef2021-01-06 18:45:18 -0800638 void addRawLightInfo(int32_t rawId, RawLightInfo&& info) {
639 mRawLightInfos.emplace(rawId, std::move(info));
640 }
641
642 void fakeLightBrightness(int32_t rawId, int32_t brightness) {
643 mLightBrightness.emplace(rawId, brightness);
644 }
645
646 void fakeLightIntensities(int32_t rawId,
647 const std::unordered_map<LightColor, int32_t> intensities) {
648 mLightIntensities.emplace(rawId, std::move(intensities));
649 }
650
Michael Wrightd02c5b62014-02-10 15:10:22 -0800651 bool getLedState(int32_t deviceId, int32_t led) {
652 Device* device = getDevice(deviceId);
653 return device->leds.valueFor(led);
654 }
655
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100656 std::vector<std::string>& getExcludedDevices() {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800657 return mExcludedDevices;
658 }
659
660 void addVirtualKeyDefinition(int32_t deviceId, const VirtualKeyDefinition& definition) {
661 Device* device = getDevice(deviceId);
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800662 device->virtualKeys.push_back(definition);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800663 }
664
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000665 void enqueueEvent(nsecs_t when, nsecs_t readTime, int32_t deviceId, int32_t type, int32_t code,
666 int32_t value) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700667 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800668 RawEvent event;
669 event.when = when;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000670 event.readTime = readTime;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800671 event.deviceId = deviceId;
672 event.type = type;
673 event.code = code;
674 event.value = value;
675 mEvents.push_back(event);
676
677 if (type == EV_ABS) {
678 setAbsoluteAxisValue(deviceId, code, value);
679 }
680 }
681
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600682 void setVideoFrames(std::unordered_map<int32_t /*deviceId*/,
683 std::vector<TouchVideoFrame>> videoFrames) {
684 mVideoFrames = std::move(videoFrames);
685 }
686
Michael Wrightd02c5b62014-02-10 15:10:22 -0800687 void assertQueueIsEmpty() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700688 std::unique_lock<std::mutex> lock(mLock);
689 base::ScopedLockAssertion assumeLocked(mLock);
690 const bool queueIsEmpty =
691 mEventsCondition.wait_for(lock, WAIT_TIMEOUT,
692 [this]() REQUIRES(mLock) { return mEvents.size() == 0; });
693 if (!queueIsEmpty) {
694 FAIL() << "Timed out waiting for EventHub queue to be emptied.";
695 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800696 }
697
698private:
699 Device* getDevice(int32_t deviceId) const {
700 ssize_t index = mDevices.indexOfKey(deviceId);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100701 return index >= 0 ? mDevices.valueAt(index) : nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800702 }
703
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700704 ftl::Flags<InputDeviceClass> getDeviceClasses(int32_t deviceId) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800705 Device* device = getDevice(deviceId);
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700706 return device ? device->classes : ftl::Flags<InputDeviceClass>(0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800707 }
708
Chris Yea52ade12020-08-27 16:49:20 -0700709 InputDeviceIdentifier getDeviceIdentifier(int32_t deviceId) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800710 Device* device = getDevice(deviceId);
711 return device ? device->identifier : InputDeviceIdentifier();
712 }
713
Chris Yea52ade12020-08-27 16:49:20 -0700714 int32_t getDeviceControllerNumber(int32_t) const override { return 0; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800715
Chris Yea52ade12020-08-27 16:49:20 -0700716 void getConfiguration(int32_t deviceId, PropertyMap* outConfiguration) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800717 Device* device = getDevice(deviceId);
718 if (device) {
719 *outConfiguration = device->configuration;
720 }
721 }
722
Chris Yea52ade12020-08-27 16:49:20 -0700723 status_t getAbsoluteAxisInfo(int32_t deviceId, int axis,
724 RawAbsoluteAxisInfo* outAxisInfo) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800725 Device* device = getDevice(deviceId);
Arthur Hung9da14732019-09-02 16:16:58 +0800726 if (device && device->enabled) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800727 ssize_t index = device->absoluteAxes.indexOfKey(axis);
728 if (index >= 0) {
729 *outAxisInfo = device->absoluteAxes.valueAt(index);
730 return OK;
731 }
732 }
733 outAxisInfo->clear();
734 return -1;
735 }
736
Chris Yea52ade12020-08-27 16:49:20 -0700737 bool hasRelativeAxis(int32_t deviceId, int axis) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800738 Device* device = getDevice(deviceId);
739 if (device) {
740 return device->relativeAxes.indexOfKey(axis) >= 0;
741 }
742 return false;
743 }
744
Chris Yea52ade12020-08-27 16:49:20 -0700745 bool hasInputProperty(int32_t, int) const override { return false; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800746
Chris Yef59a2f42020-10-16 12:55:26 -0700747 bool hasMscEvent(int32_t deviceId, int mscEvent) const override final {
748 Device* device = getDevice(deviceId);
749 if (device) {
750 return mscEvent >= 0 && mscEvent <= MSC_MAX ? device->mscBitmask.test(mscEvent) : false;
751 }
752 return false;
753 }
754
Chris Yea52ade12020-08-27 16:49:20 -0700755 status_t mapKey(int32_t deviceId, int32_t scanCode, int32_t usageCode, int32_t metaState,
756 int32_t* outKeycode, int32_t* outMetaState, uint32_t* outFlags) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800757 Device* device = getDevice(deviceId);
758 if (device) {
759 const KeyInfo* key = getKey(device, scanCode, usageCode);
760 if (key) {
761 if (outKeycode) {
762 *outKeycode = key->keyCode;
763 }
764 if (outFlags) {
765 *outFlags = key->flags;
766 }
Dmitry Torokhov0faaa0b2015-09-24 13:13:55 -0700767 if (outMetaState) {
768 *outMetaState = metaState;
769 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800770 return OK;
771 }
772 }
773 return NAME_NOT_FOUND;
774 }
775
776 const KeyInfo* getKey(Device* device, int32_t scanCode, int32_t usageCode) const {
777 if (usageCode) {
778 ssize_t index = device->keysByUsageCode.indexOfKey(usageCode);
779 if (index >= 0) {
780 return &device->keysByUsageCode.valueAt(index);
781 }
782 }
783 if (scanCode) {
784 ssize_t index = device->keysByScanCode.indexOfKey(scanCode);
785 if (index >= 0) {
786 return &device->keysByScanCode.valueAt(index);
787 }
788 }
Yi Kong9b14ac62018-07-17 13:48:38 -0700789 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800790 }
791
Chris Yea52ade12020-08-27 16:49:20 -0700792 status_t mapAxis(int32_t, int32_t, AxisInfo*) const override { return NAME_NOT_FOUND; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800793
Chris Yef59a2f42020-10-16 12:55:26 -0700794 base::Result<std::pair<InputDeviceSensorType, int32_t>> mapSensor(int32_t deviceId,
795 int32_t absCode) {
796 Device* device = getDevice(deviceId);
797 if (!device) {
798 return Errorf("Sensor device not found.");
799 }
800 auto it = device->sensorsByAbsCode.find(absCode);
801 if (it == device->sensorsByAbsCode.end()) {
802 return Errorf("Sensor map not found.");
803 }
804 const SensorInfo& info = it->second;
805 return std::make_pair(info.sensorType, info.sensorDataIndex);
806 }
807
Chris Yea52ade12020-08-27 16:49:20 -0700808 void setExcludedDevices(const std::vector<std::string>& devices) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800809 mExcludedDevices = devices;
810 }
811
Siarhei Vishniakou370039c2021-02-04 22:09:01 +0000812 size_t getEvents(int, RawEvent* buffer, size_t bufferSize) override {
813 std::scoped_lock lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800814
Siarhei Vishniakou370039c2021-02-04 22:09:01 +0000815 const size_t filledSize = std::min(mEvents.size(), bufferSize);
816 std::copy(mEvents.begin(), mEvents.begin() + filledSize, buffer);
817
818 mEvents.erase(mEvents.begin(), mEvents.begin() + filledSize);
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700819 mEventsCondition.notify_all();
Siarhei Vishniakou370039c2021-02-04 22:09:01 +0000820 return filledSize;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800821 }
822
Chris Yea52ade12020-08-27 16:49:20 -0700823 std::vector<TouchVideoFrame> getVideoFrames(int32_t deviceId) override {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600824 auto it = mVideoFrames.find(deviceId);
825 if (it != mVideoFrames.end()) {
826 std::vector<TouchVideoFrame> frames = std::move(it->second);
827 mVideoFrames.erase(deviceId);
828 return frames;
829 }
Siarhei Vishniakouadd89292018-12-13 19:23:36 -0800830 return {};
831 }
832
Chris Yea52ade12020-08-27 16:49:20 -0700833 int32_t getScanCodeState(int32_t deviceId, int32_t scanCode) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800834 Device* device = getDevice(deviceId);
835 if (device) {
836 ssize_t index = device->scanCodeStates.indexOfKey(scanCode);
837 if (index >= 0) {
838 return device->scanCodeStates.valueAt(index);
839 }
840 }
841 return AKEY_STATE_UNKNOWN;
842 }
843
Chris Yea52ade12020-08-27 16:49:20 -0700844 int32_t getKeyCodeState(int32_t deviceId, int32_t keyCode) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800845 Device* device = getDevice(deviceId);
846 if (device) {
847 ssize_t index = device->keyCodeStates.indexOfKey(keyCode);
848 if (index >= 0) {
849 return device->keyCodeStates.valueAt(index);
850 }
851 }
852 return AKEY_STATE_UNKNOWN;
853 }
854
Chris Yea52ade12020-08-27 16:49:20 -0700855 int32_t getSwitchState(int32_t deviceId, int32_t sw) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800856 Device* device = getDevice(deviceId);
857 if (device) {
858 ssize_t index = device->switchStates.indexOfKey(sw);
859 if (index >= 0) {
860 return device->switchStates.valueAt(index);
861 }
862 }
863 return AKEY_STATE_UNKNOWN;
864 }
865
Chris Yea52ade12020-08-27 16:49:20 -0700866 status_t getAbsoluteAxisValue(int32_t deviceId, int32_t axis,
867 int32_t* outValue) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800868 Device* device = getDevice(deviceId);
869 if (device) {
870 ssize_t index = device->absoluteAxisValue.indexOfKey(axis);
871 if (index >= 0) {
872 *outValue = device->absoluteAxisValue.valueAt(index);
873 return OK;
874 }
875 }
876 *outValue = 0;
877 return -1;
878 }
879
Philip Junker4af3b3d2021-12-14 10:36:55 +0100880 int32_t getKeyCodeForKeyLocation(int32_t deviceId, int32_t locationKeyCode) const override {
881 Device* device = getDevice(deviceId);
882 if (!device) {
883 return AKEYCODE_UNKNOWN;
884 }
885 auto it = device->keyCodeMapping.find(locationKeyCode);
886 return it != device->keyCodeMapping.end() ? it->second : locationKeyCode;
887 }
888
Chris Yea52ade12020-08-27 16:49:20 -0700889 // Return true if the device has non-empty key layout.
890 bool markSupportedKeyCodes(int32_t deviceId, size_t numCodes, const int32_t* keyCodes,
891 uint8_t* outFlags) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800892 bool result = false;
893 Device* device = getDevice(deviceId);
894 if (device) {
Chris Yea52ade12020-08-27 16:49:20 -0700895 result = device->keysByScanCode.size() > 0 || device->keysByUsageCode.size() > 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800896 for (size_t i = 0; i < numCodes; i++) {
897 for (size_t j = 0; j < device->keysByScanCode.size(); j++) {
898 if (keyCodes[i] == device->keysByScanCode.valueAt(j).keyCode) {
899 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800900 }
901 }
902 for (size_t j = 0; j < device->keysByUsageCode.size(); j++) {
903 if (keyCodes[i] == device->keysByUsageCode.valueAt(j).keyCode) {
904 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800905 }
906 }
907 }
908 }
909 return result;
910 }
911
Chris Yea52ade12020-08-27 16:49:20 -0700912 bool hasScanCode(int32_t deviceId, int32_t scanCode) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800913 Device* device = getDevice(deviceId);
914 if (device) {
915 ssize_t index = device->keysByScanCode.indexOfKey(scanCode);
916 return index >= 0;
917 }
918 return false;
919 }
920
Arthur Hungcb40a002021-08-03 14:31:01 +0000921 bool hasKeyCode(int32_t deviceId, int32_t keyCode) const override {
922 Device* device = getDevice(deviceId);
923 if (!device) {
924 return false;
925 }
926 for (size_t i = 0; i < device->keysByScanCode.size(); i++) {
927 if (keyCode == device->keysByScanCode.valueAt(i).keyCode) {
928 return true;
929 }
930 }
931 for (size_t j = 0; j < device->keysByUsageCode.size(); j++) {
932 if (keyCode == device->keysByUsageCode.valueAt(j).keyCode) {
933 return true;
934 }
935 }
936 return false;
937 }
938
Chris Yea52ade12020-08-27 16:49:20 -0700939 bool hasLed(int32_t deviceId, int32_t led) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800940 Device* device = getDevice(deviceId);
941 return device && device->leds.indexOfKey(led) >= 0;
942 }
943
Chris Yea52ade12020-08-27 16:49:20 -0700944 void setLedState(int32_t deviceId, int32_t led, bool on) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800945 Device* device = getDevice(deviceId);
946 if (device) {
947 ssize_t index = device->leds.indexOfKey(led);
948 if (index >= 0) {
949 device->leds.replaceValueAt(led, on);
950 } else {
951 ADD_FAILURE()
952 << "Attempted to set the state of an LED that the EventHub declared "
953 "was not present. led=" << led;
954 }
955 }
956 }
957
Chris Yea52ade12020-08-27 16:49:20 -0700958 void getVirtualKeyDefinitions(
959 int32_t deviceId, std::vector<VirtualKeyDefinition>& outVirtualKeys) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800960 outVirtualKeys.clear();
961
962 Device* device = getDevice(deviceId);
963 if (device) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800964 outVirtualKeys = device->virtualKeys;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800965 }
966 }
967
Chris Yea52ade12020-08-27 16:49:20 -0700968 const std::shared_ptr<KeyCharacterMap> getKeyCharacterMap(int32_t) const override {
Yi Kong9b14ac62018-07-17 13:48:38 -0700969 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800970 }
971
Chris Yea52ade12020-08-27 16:49:20 -0700972 bool setKeyboardLayoutOverlay(int32_t, std::shared_ptr<KeyCharacterMap>) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800973 return false;
974 }
975
Chris Yea52ade12020-08-27 16:49:20 -0700976 void vibrate(int32_t, const VibrationElement&) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800977
Chris Yea52ade12020-08-27 16:49:20 -0700978 void cancelVibrate(int32_t) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800979
Chris Ye87143712020-11-10 05:05:58 +0000980 std::vector<int32_t> getVibratorIds(int32_t deviceId) override { return mVibrators; };
981
Chris Yee2b1e5c2021-03-10 22:45:12 -0800982 std::optional<int32_t> getBatteryCapacity(int32_t, int32_t) const override {
983 return BATTERY_CAPACITY;
984 }
Kim Low03ea0352020-11-06 12:45:07 -0800985
Chris Yee2b1e5c2021-03-10 22:45:12 -0800986 std::optional<int32_t> getBatteryStatus(int32_t, int32_t) const override {
987 return BATTERY_STATUS;
988 }
989
Andy Chen22c330c2022-08-29 20:07:10 -0400990 const std::vector<int32_t> getRawBatteryIds(int32_t deviceId) override {
991 return {DEFAULT_BATTERY};
992 }
Chris Yee2b1e5c2021-03-10 22:45:12 -0800993
994 std::optional<RawBatteryInfo> getRawBatteryInfo(int32_t deviceId, int32_t batteryId) {
995 return std::nullopt;
996 }
Kim Low03ea0352020-11-06 12:45:07 -0800997
Chris Ye3fdbfef2021-01-06 18:45:18 -0800998 const std::vector<int32_t> getRawLightIds(int32_t deviceId) override {
999 std::vector<int32_t> ids;
1000 for (const auto& [rawId, info] : mRawLightInfos) {
1001 ids.push_back(rawId);
1002 }
1003 return ids;
1004 }
1005
1006 std::optional<RawLightInfo> getRawLightInfo(int32_t deviceId, int32_t lightId) override {
1007 auto it = mRawLightInfos.find(lightId);
1008 if (it == mRawLightInfos.end()) {
1009 return std::nullopt;
1010 }
1011 return it->second;
1012 }
1013
1014 void setLightBrightness(int32_t deviceId, int32_t lightId, int32_t brightness) override {
1015 mLightBrightness.emplace(lightId, brightness);
1016 }
1017
1018 void setLightIntensities(int32_t deviceId, int32_t lightId,
1019 std::unordered_map<LightColor, int32_t> intensities) override {
1020 mLightIntensities.emplace(lightId, intensities);
1021 };
1022
1023 std::optional<int32_t> getLightBrightness(int32_t deviceId, int32_t lightId) override {
1024 auto lightIt = mLightBrightness.find(lightId);
1025 if (lightIt == mLightBrightness.end()) {
1026 return std::nullopt;
1027 }
1028 return lightIt->second;
1029 }
1030
1031 std::optional<std::unordered_map<LightColor, int32_t>> getLightIntensities(
1032 int32_t deviceId, int32_t lightId) override {
1033 auto lightIt = mLightIntensities.find(lightId);
1034 if (lightIt == mLightIntensities.end()) {
1035 return std::nullopt;
1036 }
1037 return lightIt->second;
1038 };
1039
Narayan Kamath39efe3e2014-10-17 10:37:08 +01001040 virtual bool isExternal(int32_t) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001041 return false;
1042 }
1043
Chris Yea52ade12020-08-27 16:49:20 -07001044 void dump(std::string&) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001045
Chris Yea52ade12020-08-27 16:49:20 -07001046 void monitor() override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001047
Chris Yea52ade12020-08-27 16:49:20 -07001048 void requestReopenDevices() override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001049
Chris Yea52ade12020-08-27 16:49:20 -07001050 void wake() override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001051};
1052
Michael Wrightd02c5b62014-02-10 15:10:22 -08001053// --- FakeInputMapper ---
1054
1055class FakeInputMapper : public InputMapper {
1056 uint32_t mSources;
1057 int32_t mKeyboardType;
1058 int32_t mMetaState;
1059 KeyedVector<int32_t, int32_t> mKeyCodeStates;
1060 KeyedVector<int32_t, int32_t> mScanCodeStates;
1061 KeyedVector<int32_t, int32_t> mSwitchStates;
Philip Junker4af3b3d2021-12-14 10:36:55 +01001062 // fake mapping which would normally come from keyCharacterMap
1063 std::unordered_map<int32_t, int32_t> mKeyCodeMapping;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001064 std::vector<int32_t> mSupportedKeyCodes;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001065
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001066 std::mutex mLock;
1067 std::condition_variable mStateChangedCondition;
1068 bool mConfigureWasCalled GUARDED_BY(mLock);
1069 bool mResetWasCalled GUARDED_BY(mLock);
1070 bool mProcessWasCalled GUARDED_BY(mLock);
1071 RawEvent mLastEvent GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001072
Arthur Hungc23540e2018-11-29 20:42:11 +08001073 std::optional<DisplayViewport> mViewport;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001074public:
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001075 FakeInputMapper(InputDeviceContext& deviceContext, uint32_t sources)
1076 : InputMapper(deviceContext),
1077 mSources(sources),
1078 mKeyboardType(AINPUT_KEYBOARD_TYPE_NONE),
Michael Wrightd02c5b62014-02-10 15:10:22 -08001079 mMetaState(0),
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001080 mConfigureWasCalled(false),
1081 mResetWasCalled(false),
1082 mProcessWasCalled(false) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001083
Chris Yea52ade12020-08-27 16:49:20 -07001084 virtual ~FakeInputMapper() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001085
1086 void setKeyboardType(int32_t keyboardType) {
1087 mKeyboardType = keyboardType;
1088 }
1089
1090 void setMetaState(int32_t metaState) {
1091 mMetaState = metaState;
1092 }
1093
1094 void assertConfigureWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001095 std::unique_lock<std::mutex> lock(mLock);
1096 base::ScopedLockAssertion assumeLocked(mLock);
1097 const bool configureCalled =
1098 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1099 return mConfigureWasCalled;
1100 });
1101 if (!configureCalled) {
1102 FAIL() << "Expected configure() to have been called.";
1103 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001104 mConfigureWasCalled = false;
1105 }
1106
1107 void assertResetWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001108 std::unique_lock<std::mutex> lock(mLock);
1109 base::ScopedLockAssertion assumeLocked(mLock);
1110 const bool resetCalled =
1111 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1112 return mResetWasCalled;
1113 });
1114 if (!resetCalled) {
1115 FAIL() << "Expected reset() to have been called.";
1116 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001117 mResetWasCalled = false;
1118 }
1119
Yi Kong9b14ac62018-07-17 13:48:38 -07001120 void assertProcessWasCalled(RawEvent* outLastEvent = nullptr) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001121 std::unique_lock<std::mutex> lock(mLock);
1122 base::ScopedLockAssertion assumeLocked(mLock);
1123 const bool processCalled =
1124 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1125 return mProcessWasCalled;
1126 });
1127 if (!processCalled) {
1128 FAIL() << "Expected process() to have been called.";
1129 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001130 if (outLastEvent) {
1131 *outLastEvent = mLastEvent;
1132 }
1133 mProcessWasCalled = false;
1134 }
1135
1136 void setKeyCodeState(int32_t keyCode, int32_t state) {
1137 mKeyCodeStates.replaceValueFor(keyCode, state);
1138 }
1139
1140 void setScanCodeState(int32_t scanCode, int32_t state) {
1141 mScanCodeStates.replaceValueFor(scanCode, state);
1142 }
1143
1144 void setSwitchState(int32_t switchCode, int32_t state) {
1145 mSwitchStates.replaceValueFor(switchCode, state);
1146 }
1147
1148 void addSupportedKeyCode(int32_t keyCode) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001149 mSupportedKeyCodes.push_back(keyCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001150 }
1151
Philip Junker4af3b3d2021-12-14 10:36:55 +01001152 void addKeyCodeMapping(int32_t fromKeyCode, int32_t toKeyCode) {
1153 mKeyCodeMapping.insert_or_assign(fromKeyCode, toKeyCode);
1154 }
1155
Michael Wrightd02c5b62014-02-10 15:10:22 -08001156private:
Philip Junker4af3b3d2021-12-14 10:36:55 +01001157 uint32_t getSources() const override { return mSources; }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001158
Chris Yea52ade12020-08-27 16:49:20 -07001159 void populateDeviceInfo(InputDeviceInfo* deviceInfo) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001160 InputMapper::populateDeviceInfo(deviceInfo);
1161
1162 if (mKeyboardType != AINPUT_KEYBOARD_TYPE_NONE) {
1163 deviceInfo->setKeyboardType(mKeyboardType);
1164 }
1165 }
1166
Chris Yea52ade12020-08-27 16:49:20 -07001167 void configure(nsecs_t, const InputReaderConfiguration* config, uint32_t changes) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001168 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001169 mConfigureWasCalled = true;
Arthur Hungc23540e2018-11-29 20:42:11 +08001170
1171 // Find the associated viewport if exist.
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001172 const std::optional<uint8_t> displayPort = getDeviceContext().getAssociatedDisplayPort();
Arthur Hungc23540e2018-11-29 20:42:11 +08001173 if (displayPort && (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
1174 mViewport = config->getDisplayViewportByPort(*displayPort);
1175 }
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001176
1177 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001178 }
1179
Chris Yea52ade12020-08-27 16:49:20 -07001180 void reset(nsecs_t) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001181 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001182 mResetWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001183 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001184 }
1185
Chris Yea52ade12020-08-27 16:49:20 -07001186 void process(const RawEvent* rawEvent) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001187 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001188 mLastEvent = *rawEvent;
1189 mProcessWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001190 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001191 }
1192
Chris Yea52ade12020-08-27 16:49:20 -07001193 int32_t getKeyCodeState(uint32_t, int32_t keyCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001194 ssize_t index = mKeyCodeStates.indexOfKey(keyCode);
1195 return index >= 0 ? mKeyCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1196 }
1197
Philip Junker4af3b3d2021-12-14 10:36:55 +01001198 int32_t getKeyCodeForKeyLocation(int32_t locationKeyCode) const override {
1199 auto it = mKeyCodeMapping.find(locationKeyCode);
1200 return it != mKeyCodeMapping.end() ? it->second : locationKeyCode;
1201 }
1202
Chris Yea52ade12020-08-27 16:49:20 -07001203 int32_t getScanCodeState(uint32_t, int32_t scanCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001204 ssize_t index = mScanCodeStates.indexOfKey(scanCode);
1205 return index >= 0 ? mScanCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1206 }
1207
Chris Yea52ade12020-08-27 16:49:20 -07001208 int32_t getSwitchState(uint32_t, int32_t switchCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001209 ssize_t index = mSwitchStates.indexOfKey(switchCode);
1210 return index >= 0 ? mSwitchStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1211 }
1212
Chris Yea52ade12020-08-27 16:49:20 -07001213 // Return true if the device has non-empty key layout.
1214 bool markSupportedKeyCodes(uint32_t, size_t numCodes, const int32_t* keyCodes,
1215 uint8_t* outFlags) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001216 for (size_t i = 0; i < numCodes; i++) {
1217 for (size_t j = 0; j < mSupportedKeyCodes.size(); j++) {
1218 if (keyCodes[i] == mSupportedKeyCodes[j]) {
1219 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001220 }
1221 }
1222 }
Chris Yea52ade12020-08-27 16:49:20 -07001223 bool result = mSupportedKeyCodes.size() > 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001224 return result;
1225 }
1226
1227 virtual int32_t getMetaState() {
1228 return mMetaState;
1229 }
1230
1231 virtual void fadePointer() {
1232 }
Arthur Hungc23540e2018-11-29 20:42:11 +08001233
1234 virtual std::optional<int32_t> getAssociatedDisplay() {
1235 if (mViewport) {
1236 return std::make_optional(mViewport->displayId);
1237 }
1238 return std::nullopt;
1239 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001240};
1241
1242
1243// --- InstrumentedInputReader ---
1244
1245class InstrumentedInputReader : public InputReader {
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001246 std::queue<std::shared_ptr<InputDevice>> mNextDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001247
1248public:
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001249 InstrumentedInputReader(std::shared_ptr<EventHubInterface> eventHub,
1250 const sp<InputReaderPolicyInterface>& policy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001251 InputListenerInterface& listener)
arthurhungdcef2dc2020-08-11 14:47:50 +08001252 : InputReader(eventHub, policy, listener), mFakeContext(this) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001253
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001254 virtual ~InstrumentedInputReader() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001255
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001256 void pushNextDevice(std::shared_ptr<InputDevice> device) { mNextDevices.push(device); }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001257
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001258 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001259 const std::string& location = "") {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001260 InputDeviceIdentifier identifier;
1261 identifier.name = name;
Arthur Hungc23540e2018-11-29 20:42:11 +08001262 identifier.location = location;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001263 int32_t generation = deviceId + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08001264 return std::make_shared<InputDevice>(&mFakeContext, deviceId, generation, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001265 }
1266
Prabir Pradhan28efc192019-11-05 01:10:04 +00001267 // Make the protected loopOnce method accessible to tests.
1268 using InputReader::loopOnce;
1269
Michael Wrightd02c5b62014-02-10 15:10:22 -08001270protected:
Chris Ye1c2e0892020-11-30 21:41:44 -08001271 virtual std::shared_ptr<InputDevice> createDeviceLocked(int32_t eventHubId,
1272 const InputDeviceIdentifier& identifier)
1273 REQUIRES(mLock) {
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001274 if (!mNextDevices.empty()) {
1275 std::shared_ptr<InputDevice> device(std::move(mNextDevices.front()));
1276 mNextDevices.pop();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001277 return device;
1278 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001279 return InputReader::createDeviceLocked(eventHubId, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001280 }
1281
arthurhungdcef2dc2020-08-11 14:47:50 +08001282 // --- FakeInputReaderContext ---
1283 class FakeInputReaderContext : public ContextImpl {
1284 int32_t mGlobalMetaState;
1285 bool mUpdateGlobalMetaStateWasCalled;
1286 int32_t mGeneration;
1287
1288 public:
1289 FakeInputReaderContext(InputReader* reader)
1290 : ContextImpl(reader),
1291 mGlobalMetaState(0),
1292 mUpdateGlobalMetaStateWasCalled(false),
1293 mGeneration(1) {}
1294
1295 virtual ~FakeInputReaderContext() {}
1296
1297 void assertUpdateGlobalMetaStateWasCalled() {
1298 ASSERT_TRUE(mUpdateGlobalMetaStateWasCalled)
1299 << "Expected updateGlobalMetaState() to have been called.";
1300 mUpdateGlobalMetaStateWasCalled = false;
1301 }
1302
1303 void setGlobalMetaState(int32_t state) { mGlobalMetaState = state; }
1304
1305 uint32_t getGeneration() { return mGeneration; }
1306
1307 void updateGlobalMetaState() override {
1308 mUpdateGlobalMetaStateWasCalled = true;
1309 ContextImpl::updateGlobalMetaState();
1310 }
1311
1312 int32_t getGlobalMetaState() override {
1313 return mGlobalMetaState | ContextImpl::getGlobalMetaState();
1314 }
1315
1316 int32_t bumpGeneration() override {
1317 mGeneration = ContextImpl::bumpGeneration();
1318 return mGeneration;
1319 }
1320 } mFakeContext;
1321
Michael Wrightd02c5b62014-02-10 15:10:22 -08001322 friend class InputReaderTest;
arthurhungdcef2dc2020-08-11 14:47:50 +08001323
1324public:
1325 FakeInputReaderContext* getContext() { return &mFakeContext; }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001326};
1327
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001328// --- InputReaderPolicyTest ---
1329class InputReaderPolicyTest : public testing::Test {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001330protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001331 sp<FakeInputReaderPolicy> mFakePolicy;
1332
Chris Yea52ade12020-08-27 16:49:20 -07001333 void SetUp() override { mFakePolicy = new FakeInputReaderPolicy(); }
1334 void TearDown() override { mFakePolicy.clear(); }
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001335};
1336
1337/**
1338 * Check that empty set of viewports is an acceptable configuration.
1339 * Also try to get internal viewport two different ways - by type and by uniqueId.
1340 *
1341 * There will be confusion if two viewports with empty uniqueId and identical type are present.
1342 * Such configuration is not currently allowed.
1343 */
1344TEST_F(InputReaderPolicyTest, Viewports_GetCleared) {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001345 static const std::string uniqueId = "local:0";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001346
1347 // We didn't add any viewports yet, so there shouldn't be any.
1348 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001349 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001350 ASSERT_FALSE(internalViewport);
1351
1352 // Add an internal viewport, then clear it
1353 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001354 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId, NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001355 ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001356
1357 // Check matching by uniqueId
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001358 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001359 ASSERT_TRUE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001360 ASSERT_EQ(ViewportType::INTERNAL, internalViewport->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001361
1362 // Check matching by viewport type
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001363 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001364 ASSERT_TRUE(internalViewport);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001365 ASSERT_EQ(uniqueId, internalViewport->uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001366
1367 mFakePolicy->clearViewports();
1368 // Make sure nothing is found after clear
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001369 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001370 ASSERT_FALSE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001371 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001372 ASSERT_FALSE(internalViewport);
1373}
1374
1375TEST_F(InputReaderPolicyTest, Viewports_GetByType) {
1376 const std::string internalUniqueId = "local:0";
1377 const std::string externalUniqueId = "local:1";
1378 const std::string virtualUniqueId1 = "virtual:2";
1379 const std::string virtualUniqueId2 = "virtual:3";
1380 constexpr int32_t virtualDisplayId1 = 2;
1381 constexpr int32_t virtualDisplayId2 = 3;
1382
1383 // Add an internal viewport
1384 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001385 DISPLAY_ORIENTATION_0, true /*isActive*/, internalUniqueId,
1386 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001387 // Add an external viewport
1388 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001389 DISPLAY_ORIENTATION_0, true /*isActive*/, externalUniqueId,
1390 NO_PORT, ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001391 // Add an virtual viewport
1392 mFakePolicy->addDisplayViewport(virtualDisplayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001393 DISPLAY_ORIENTATION_0, true /*isActive*/, virtualUniqueId1,
1394 NO_PORT, ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001395 // Add another virtual viewport
1396 mFakePolicy->addDisplayViewport(virtualDisplayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001397 DISPLAY_ORIENTATION_0, true /*isActive*/, virtualUniqueId2,
1398 NO_PORT, ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001399
1400 // Check matching by type for internal
1401 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001402 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001403 ASSERT_TRUE(internalViewport);
1404 ASSERT_EQ(internalUniqueId, internalViewport->uniqueId);
1405
1406 // Check matching by type for external
1407 std::optional<DisplayViewport> externalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001408 mFakePolicy->getDisplayViewportByType(ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001409 ASSERT_TRUE(externalViewport);
1410 ASSERT_EQ(externalUniqueId, externalViewport->uniqueId);
1411
1412 // Check matching by uniqueId for virtual viewport #1
1413 std::optional<DisplayViewport> virtualViewport1 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001414 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001415 ASSERT_TRUE(virtualViewport1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001416 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport1->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001417 ASSERT_EQ(virtualUniqueId1, virtualViewport1->uniqueId);
1418 ASSERT_EQ(virtualDisplayId1, virtualViewport1->displayId);
1419
1420 // Check matching by uniqueId for virtual viewport #2
1421 std::optional<DisplayViewport> virtualViewport2 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001422 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001423 ASSERT_TRUE(virtualViewport2);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001424 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport2->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001425 ASSERT_EQ(virtualUniqueId2, virtualViewport2->uniqueId);
1426 ASSERT_EQ(virtualDisplayId2, virtualViewport2->displayId);
1427}
1428
1429
1430/**
1431 * We can have 2 viewports of the same kind. We can distinguish them by uniqueId, and confirm
1432 * that lookup works by checking display id.
1433 * Check that 2 viewports of each kind is possible, for all existing viewport types.
1434 */
1435TEST_F(InputReaderPolicyTest, Viewports_TwoOfSameType) {
1436 const std::string uniqueId1 = "uniqueId1";
1437 const std::string uniqueId2 = "uniqueId2";
1438 constexpr int32_t displayId1 = 2;
1439 constexpr int32_t displayId2 = 3;
1440
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001441 std::vector<ViewportType> types = {ViewportType::INTERNAL, ViewportType::EXTERNAL,
1442 ViewportType::VIRTUAL};
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001443 for (const ViewportType& type : types) {
1444 mFakePolicy->clearViewports();
1445 // Add a viewport
1446 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001447 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1,
1448 NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001449 // Add another viewport
1450 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001451 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2,
1452 NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001453
1454 // Check that correct display viewport was returned by comparing the display IDs.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001455 std::optional<DisplayViewport> viewport1 =
1456 mFakePolicy->getDisplayViewportByUniqueId(uniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001457 ASSERT_TRUE(viewport1);
1458 ASSERT_EQ(displayId1, viewport1->displayId);
1459 ASSERT_EQ(type, viewport1->type);
1460
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001461 std::optional<DisplayViewport> viewport2 =
1462 mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001463 ASSERT_TRUE(viewport2);
1464 ASSERT_EQ(displayId2, viewport2->displayId);
1465 ASSERT_EQ(type, viewport2->type);
1466
1467 // When there are multiple viewports of the same kind, and uniqueId is not specified
1468 // in the call to getDisplayViewport, then that situation is not supported.
1469 // The viewports can be stored in any order, so we cannot rely on the order, since that
1470 // is just implementation detail.
1471 // However, we can check that it still returns *a* viewport, we just cannot assert
1472 // which one specifically is returned.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001473 std::optional<DisplayViewport> someViewport = mFakePolicy->getDisplayViewportByType(type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001474 ASSERT_TRUE(someViewport);
1475 }
1476}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001477
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001478/**
Michael Wrightdde67b82020-10-27 16:09:22 +00001479 * When we have multiple internal displays make sure we always return the default display when
1480 * querying by type.
1481 */
1482TEST_F(InputReaderPolicyTest, Viewports_ByTypeReturnsDefaultForInternal) {
1483 const std::string uniqueId1 = "uniqueId1";
1484 const std::string uniqueId2 = "uniqueId2";
1485 constexpr int32_t nonDefaultDisplayId = 2;
1486 static_assert(nonDefaultDisplayId != ADISPLAY_ID_DEFAULT,
1487 "Test display ID should not be ADISPLAY_ID_DEFAULT");
1488
1489 // Add the default display first and ensure it gets returned.
1490 mFakePolicy->clearViewports();
1491 mFakePolicy->addDisplayViewport(ADISPLAY_ID_DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001492 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001493 ViewportType::INTERNAL);
1494 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001495 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001496 ViewportType::INTERNAL);
1497
1498 std::optional<DisplayViewport> viewport =
1499 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
1500 ASSERT_TRUE(viewport);
1501 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
1502 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
1503
1504 // Add the default display second to make sure order doesn't matter.
1505 mFakePolicy->clearViewports();
1506 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001507 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001508 ViewportType::INTERNAL);
1509 mFakePolicy->addDisplayViewport(ADISPLAY_ID_DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001510 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001511 ViewportType::INTERNAL);
1512
1513 viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
1514 ASSERT_TRUE(viewport);
1515 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
1516 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
1517}
1518
1519/**
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001520 * Check getDisplayViewportByPort
1521 */
1522TEST_F(InputReaderPolicyTest, Viewports_GetByPort) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001523 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001524 const std::string uniqueId1 = "uniqueId1";
1525 const std::string uniqueId2 = "uniqueId2";
1526 constexpr int32_t displayId1 = 1;
1527 constexpr int32_t displayId2 = 2;
1528 const uint8_t hdmi1 = 0;
1529 const uint8_t hdmi2 = 1;
1530 const uint8_t hdmi3 = 2;
1531
1532 mFakePolicy->clearViewports();
1533 // Add a viewport that's associated with some display port that's not of interest.
1534 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001535 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, hdmi3,
1536 type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001537 // Add another viewport, connected to HDMI1 port
1538 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001539 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, hdmi1,
1540 type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001541
1542 // Check that correct display viewport was returned by comparing the display ports.
1543 std::optional<DisplayViewport> hdmi1Viewport = mFakePolicy->getDisplayViewportByPort(hdmi1);
1544 ASSERT_TRUE(hdmi1Viewport);
1545 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1546 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1547
1548 // Check that we can still get the same viewport using the uniqueId
1549 hdmi1Viewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
1550 ASSERT_TRUE(hdmi1Viewport);
1551 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1552 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1553 ASSERT_EQ(type, hdmi1Viewport->type);
1554
1555 // Check that we cannot find a port with "HDMI2", because we never added one
1556 std::optional<DisplayViewport> hdmi2Viewport = mFakePolicy->getDisplayViewportByPort(hdmi2);
1557 ASSERT_FALSE(hdmi2Viewport);
1558}
1559
Michael Wrightd02c5b62014-02-10 15:10:22 -08001560// --- InputReaderTest ---
1561
1562class InputReaderTest : public testing::Test {
1563protected:
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001564 std::unique_ptr<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001565 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001566 std::shared_ptr<FakeEventHub> mFakeEventHub;
Prabir Pradhan28efc192019-11-05 01:10:04 +00001567 std::unique_ptr<InstrumentedInputReader> mReader;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001568
Chris Yea52ade12020-08-27 16:49:20 -07001569 void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001570 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001571 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001572 mFakeListener = std::make_unique<TestInputListener>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001573
Prabir Pradhan28efc192019-11-05 01:10:04 +00001574 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001575 *mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001576 }
1577
Chris Yea52ade12020-08-27 16:49:20 -07001578 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001579 mFakeListener.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001580 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001581 }
1582
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001583 void addDevice(int32_t eventHubId, const std::string& name,
1584 ftl::Flags<InputDeviceClass> classes, const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001585 mFakeEventHub->addDevice(eventHubId, name, classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001586
1587 if (configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001588 mFakeEventHub->addConfigurationMap(eventHubId, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001589 }
1590 mFakeEventHub->finishDeviceScan();
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001591 mReader->loopOnce();
1592 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001593 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1594 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001595 }
1596
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001597 void disableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001598 mFakePolicy->addDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001599 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001600 }
1601
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001602 void enableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001603 mFakePolicy->removeDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001604 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001605 }
1606
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001607 FakeInputMapper& addDeviceWithFakeInputMapper(int32_t deviceId, int32_t eventHubId,
Chris Ye1b0c7342020-07-28 21:57:03 -07001608 const std::string& name,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001609 ftl::Flags<InputDeviceClass> classes,
1610 uint32_t sources,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001611 const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001612 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, name);
1613 FakeInputMapper& mapper = device->addMapper<FakeInputMapper>(eventHubId, sources);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001614 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001615 addDevice(eventHubId, name, classes, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001616 return mapper;
1617 }
1618};
1619
Chris Ye98d3f532020-10-01 21:48:59 -07001620TEST_F(InputReaderTest, PolicyGetInputDevices) {
1621 ASSERT_NO_FATAL_FAILURE(addDevice(1, "keyboard", InputDeviceClass::KEYBOARD, nullptr));
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001622 ASSERT_NO_FATAL_FAILURE(addDevice(2, "ignored", ftl::Flags<InputDeviceClass>(0),
Chris Ye98d3f532020-10-01 21:48:59 -07001623 nullptr)); // no classes so device will be ignored
Michael Wrightd02c5b62014-02-10 15:10:22 -08001624
1625 // Should also have received a notification describing the new input devices.
Chris Ye98d3f532020-10-01 21:48:59 -07001626 const std::vector<InputDeviceInfo>& inputDevices = mFakePolicy->getInputDevices();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001627 ASSERT_EQ(1U, inputDevices.size());
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001628 ASSERT_EQ(END_RESERVED_ID + 1, inputDevices[0].getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001629 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001630 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
1631 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00001632 ASSERT_EQ(0U, inputDevices[0].getMotionRanges().size());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001633}
1634
Chris Yee7310032020-09-22 15:36:28 -07001635TEST_F(InputReaderTest, GetMergedInputDevices) {
1636 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1637 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1638 // Add two subdevices to device
1639 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1640 // Must add at least one mapper or the device will be ignored!
1641 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1642 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1643
1644 // Push same device instance for next device to be added, so they'll have same identifier.
1645 mReader->pushNextDevice(device);
1646 mReader->pushNextDevice(device);
1647 ASSERT_NO_FATAL_FAILURE(
1648 addDevice(eventHubIds[0], "fake1", InputDeviceClass::KEYBOARD, nullptr));
1649 ASSERT_NO_FATAL_FAILURE(
1650 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
1651
1652 // Two devices will be merged to one input device as they have same identifier
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00001653 ASSERT_EQ(1U, mFakePolicy->getInputDevices().size());
Chris Yee7310032020-09-22 15:36:28 -07001654}
1655
Chris Yee14523a2020-12-19 13:46:00 -08001656TEST_F(InputReaderTest, GetMergedInputDevicesEnabled) {
1657 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1658 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1659 // Add two subdevices to device
1660 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1661 // Must add at least one mapper or the device will be ignored!
1662 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1663 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1664
1665 // Push same device instance for next device to be added, so they'll have same identifier.
1666 mReader->pushNextDevice(device);
1667 mReader->pushNextDevice(device);
1668 // Sensor device is initially disabled
1669 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1",
1670 InputDeviceClass::KEYBOARD | InputDeviceClass::SENSOR,
1671 nullptr));
1672 // Device is disabled because the only sub device is a sensor device and disabled initially.
1673 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1674 ASSERT_FALSE(device->isEnabled());
1675 ASSERT_NO_FATAL_FAILURE(
1676 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
1677 // The merged device is enabled if any sub device is enabled
1678 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1679 ASSERT_TRUE(device->isEnabled());
1680}
1681
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001682TEST_F(InputReaderTest, WhenEnabledChanges_SendsDeviceResetNotification) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001683 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001684 constexpr ftl::Flags<InputDeviceClass> deviceClass(InputDeviceClass::KEYBOARD);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001685 constexpr int32_t eventHubId = 1;
1686 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001687 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001688 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001689 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001690 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001691
Yi Kong9b14ac62018-07-17 13:48:38 -07001692 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001693
1694 NotifyDeviceResetArgs resetArgs;
1695 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001696 ASSERT_EQ(deviceId, resetArgs.deviceId);
1697
1698 ASSERT_EQ(device->isEnabled(), true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001699 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001700 mReader->loopOnce();
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001701
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001702 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001703 ASSERT_EQ(deviceId, resetArgs.deviceId);
1704 ASSERT_EQ(device->isEnabled(), false);
1705
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001706 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001707 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001708 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
1709 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasNotCalled());
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001710 ASSERT_EQ(device->isEnabled(), false);
1711
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001712 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001713 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001714 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001715 ASSERT_EQ(deviceId, resetArgs.deviceId);
1716 ASSERT_EQ(device->isEnabled(), true);
1717}
1718
Michael Wrightd02c5b62014-02-10 15:10:22 -08001719TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001720 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001721 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001722 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001723 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001724 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001725 AINPUT_SOURCE_KEYBOARD, nullptr);
1726 mapper.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001727
1728 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(0,
1729 AINPUT_SOURCE_ANY, AKEYCODE_A))
1730 << "Should return unknown when the device id is >= 0 but unknown.";
1731
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001732 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1733 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1734 << "Should return unknown when the device id is valid but the sources are not "
1735 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001736
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001737 ASSERT_EQ(AKEY_STATE_DOWN,
1738 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1739 AKEYCODE_A))
1740 << "Should return value provided by mapper when device id is valid and the device "
1741 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001742
1743 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(-1,
1744 AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1745 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1746
1747 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getKeyCodeState(-1,
1748 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1749 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1750}
1751
Philip Junker4af3b3d2021-12-14 10:36:55 +01001752TEST_F(InputReaderTest, GetKeyCodeForKeyLocation_ForwardsRequestsToMappers) {
1753 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1754 constexpr int32_t eventHubId = 1;
1755 FakeInputMapper& mapper = addDeviceWithFakeInputMapper(deviceId, eventHubId, "keyboard",
1756 InputDeviceClass::KEYBOARD,
1757 AINPUT_SOURCE_KEYBOARD, nullptr);
1758 mapper.addKeyCodeMapping(AKEYCODE_Y, AKEYCODE_Z);
1759
1760 ASSERT_EQ(AKEYCODE_UNKNOWN, mReader->getKeyCodeForKeyLocation(0, AKEYCODE_Y))
1761 << "Should return unknown when the device with the specified id is not found.";
1762
1763 ASSERT_EQ(AKEYCODE_Z, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_Y))
1764 << "Should return correct mapping when device id is valid and mapping exists.";
1765
1766 ASSERT_EQ(AKEYCODE_A, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_A))
1767 << "Should return the location key code when device id is valid and there's no "
1768 "mapping.";
1769}
1770
1771TEST_F(InputReaderTest, GetKeyCodeForKeyLocation_NoKeyboardMapper) {
1772 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1773 constexpr int32_t eventHubId = 1;
1774 FakeInputMapper& mapper = addDeviceWithFakeInputMapper(deviceId, eventHubId, "joystick",
1775 InputDeviceClass::JOYSTICK,
1776 AINPUT_SOURCE_GAMEPAD, nullptr);
1777 mapper.addKeyCodeMapping(AKEYCODE_Y, AKEYCODE_Z);
1778
1779 ASSERT_EQ(AKEYCODE_UNKNOWN, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_Y))
1780 << "Should return unknown when the device id is valid but there is no keyboard mapper";
1781}
1782
Michael Wrightd02c5b62014-02-10 15:10:22 -08001783TEST_F(InputReaderTest, GetScanCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001784 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001785 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001786 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001787 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001788 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001789 AINPUT_SOURCE_KEYBOARD, nullptr);
1790 mapper.setScanCodeState(KEY_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001791
1792 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(0,
1793 AINPUT_SOURCE_ANY, KEY_A))
1794 << "Should return unknown when the device id is >= 0 but unknown.";
1795
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001796 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1797 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, KEY_A))
1798 << "Should return unknown when the device id is valid but the sources are not "
1799 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001800
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001801 ASSERT_EQ(AKEY_STATE_DOWN,
1802 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1803 KEY_A))
1804 << "Should return value provided by mapper when device id is valid and the device "
1805 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001806
1807 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(-1,
1808 AINPUT_SOURCE_TRACKBALL, KEY_A))
1809 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1810
1811 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getScanCodeState(-1,
1812 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, KEY_A))
1813 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1814}
1815
1816TEST_F(InputReaderTest, GetSwitchState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001817 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001818 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001819 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001820 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001821 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001822 AINPUT_SOURCE_KEYBOARD, nullptr);
1823 mapper.setSwitchState(SW_LID, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001824
1825 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(0,
1826 AINPUT_SOURCE_ANY, SW_LID))
1827 << "Should return unknown when the device id is >= 0 but unknown.";
1828
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001829 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1830 mReader->getSwitchState(deviceId, AINPUT_SOURCE_TRACKBALL, SW_LID))
1831 << "Should return unknown when the device id is valid but the sources are not "
1832 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001833
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001834 ASSERT_EQ(AKEY_STATE_DOWN,
1835 mReader->getSwitchState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1836 SW_LID))
1837 << "Should return value provided by mapper when device id is valid and the device "
1838 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001839
1840 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(-1,
1841 AINPUT_SOURCE_TRACKBALL, SW_LID))
1842 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1843
1844 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getSwitchState(-1,
1845 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, SW_LID))
1846 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1847}
1848
1849TEST_F(InputReaderTest, MarkSupportedKeyCodes_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001850 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001851 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001852 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001853 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001854 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001855 AINPUT_SOURCE_KEYBOARD, nullptr);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001856
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001857 mapper.addSupportedKeyCode(AKEYCODE_A);
1858 mapper.addSupportedKeyCode(AKEYCODE_B);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001859
1860 const int32_t keyCodes[4] = { AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2 };
1861 uint8_t flags[4] = { 0, 0, 0, 1 };
1862
1863 ASSERT_FALSE(mReader->hasKeys(0, AINPUT_SOURCE_ANY, 4, keyCodes, flags))
1864 << "Should return false when device id is >= 0 but unknown.";
1865 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1866
1867 flags[3] = 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001868 ASSERT_FALSE(mReader->hasKeys(deviceId, AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1869 << "Should return false when device id is valid but the sources are not supported by "
1870 "the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001871 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1872
1873 flags[3] = 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001874 ASSERT_TRUE(mReader->hasKeys(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, 4,
1875 keyCodes, flags))
1876 << "Should return value provided by mapper when device id is valid and the device "
1877 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001878 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1879
1880 flags[3] = 1;
1881 ASSERT_FALSE(mReader->hasKeys(-1, AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1882 << "Should return false when the device id is < 0 but the sources are not supported by any device.";
1883 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1884
1885 flags[3] = 1;
1886 ASSERT_TRUE(mReader->hasKeys(-1, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1887 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1888 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1889}
1890
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001891TEST_F(InputReaderTest, LoopOnce_WhenDeviceScanFinished_SendsConfigurationChanged) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001892 constexpr int32_t eventHubId = 1;
Chris Ye1b0c7342020-07-28 21:57:03 -07001893 addDevice(eventHubId, "ignored", InputDeviceClass::KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001894
1895 NotifyConfigurationChangedArgs args;
1896
1897 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(&args));
1898 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
1899}
1900
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001901TEST_F(InputReaderTest, LoopOnce_ForwardsRawEventsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001902 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001903 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001904 constexpr nsecs_t when = 0;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001905 constexpr int32_t eventHubId = 1;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001906 constexpr nsecs_t readTime = 2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001907 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001908 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001909 AINPUT_SOURCE_KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001910
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001911 mFakeEventHub->enqueueEvent(when, readTime, eventHubId, EV_KEY, KEY_A, 1);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001912 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001913 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
1914
1915 RawEvent event;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001916 ASSERT_NO_FATAL_FAILURE(mapper.assertProcessWasCalled(&event));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001917 ASSERT_EQ(when, event.when);
1918 ASSERT_EQ(readTime, event.readTime);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001919 ASSERT_EQ(eventHubId, event.deviceId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001920 ASSERT_EQ(EV_KEY, event.type);
1921 ASSERT_EQ(KEY_A, event.code);
1922 ASSERT_EQ(1, event.value);
1923}
1924
Garfield Tan1c7bc862020-01-28 13:24:04 -08001925TEST_F(InputReaderTest, DeviceReset_RandomId) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001926 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001927 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001928 constexpr int32_t eventHubId = 1;
1929 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Prabir Pradhan42611e02018-11-27 14:04:02 -08001930 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001931 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001932 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001933 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan42611e02018-11-27 14:04:02 -08001934
1935 NotifyDeviceResetArgs resetArgs;
1936 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001937 int32_t prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001938
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001939 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001940 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001941 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001942 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001943 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001944
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001945 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001946 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001947 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001948 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001949 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001950
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001951 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001952 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001953 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001954 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001955 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001956}
1957
Garfield Tan1c7bc862020-01-28 13:24:04 -08001958TEST_F(InputReaderTest, DeviceReset_GenerateIdWithInputReaderSource) {
1959 constexpr int32_t deviceId = 1;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001960 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Garfield Tan1c7bc862020-01-28 13:24:04 -08001961 constexpr int32_t eventHubId = 1;
1962 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1963 // Must add at least one mapper or the device will be ignored!
1964 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001965 mReader->pushNextDevice(device);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001966 ASSERT_NO_FATAL_FAILURE(addDevice(deviceId, "fake", deviceClass, nullptr));
1967
1968 NotifyDeviceResetArgs resetArgs;
1969 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1970 ASSERT_EQ(IdGenerator::Source::INPUT_READER, IdGenerator::getSource(resetArgs.id));
1971}
1972
Arthur Hungc23540e2018-11-29 20:42:11 +08001973TEST_F(InputReaderTest, Device_CanDispatchToDisplay) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001974 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001975 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001976 constexpr int32_t eventHubId = 1;
Arthur Hungc23540e2018-11-29 20:42:11 +08001977 const char* DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001978 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1979 FakeInputMapper& mapper =
1980 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001981 mReader->pushNextDevice(device);
Arthur Hungc23540e2018-11-29 20:42:11 +08001982
1983 const uint8_t hdmi1 = 1;
1984
1985 // Associated touch screen with second display.
1986 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
1987
1988 // Add default and second display.
Prabir Pradhan28efc192019-11-05 01:10:04 +00001989 mFakePolicy->clearViewports();
Arthur Hungc23540e2018-11-29 20:42:11 +08001990 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001991 DISPLAY_ORIENTATION_0, true /*isActive*/, "local:0", NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001992 ViewportType::INTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08001993 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001994 DISPLAY_ORIENTATION_0, true /*isActive*/, "local:1", hdmi1,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001995 ViewportType::EXTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08001996 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001997 mReader->loopOnce();
Prabir Pradhan28efc192019-11-05 01:10:04 +00001998
1999 // Add the device, and make sure all of the callbacks are triggered.
2000 // The device is added after the input port associations are processed since
2001 // we do not yet support dynamic device-to-display associations.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002002 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07002003 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled());
Prabir Pradhan28efc192019-11-05 01:10:04 +00002004 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002005 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
Arthur Hungc23540e2018-11-29 20:42:11 +08002006
Arthur Hung2c9a3342019-07-23 14:18:59 +08002007 // Device should only dispatch to the specified display.
Arthur Hungc23540e2018-11-29 20:42:11 +08002008 ASSERT_EQ(deviceId, device->getId());
2009 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, DISPLAY_ID));
2010 ASSERT_TRUE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hung2c9a3342019-07-23 14:18:59 +08002011
2012 // Can't dispatch event from a disabled device.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002013 disableDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00002014 mReader->loopOnce();
Arthur Hung2c9a3342019-07-23 14:18:59 +08002015 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hungc23540e2018-11-29 20:42:11 +08002016}
2017
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08002018TEST_F(InputReaderTest, WhenEnabledChanges_AllSubdevicesAreUpdated) {
2019 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002020 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08002021 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
2022 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
2023 // Must add at least one mapper or the device will be ignored!
2024 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
2025 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
2026 mReader->pushNextDevice(device);
2027 mReader->pushNextDevice(device);
2028 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
2029 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
2030
2031 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
2032
2033 NotifyDeviceResetArgs resetArgs;
2034 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2035 ASSERT_EQ(deviceId, resetArgs.deviceId);
2036 ASSERT_TRUE(device->isEnabled());
2037 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
2038 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
2039
2040 disableDevice(deviceId);
2041 mReader->loopOnce();
2042
2043 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2044 ASSERT_EQ(deviceId, resetArgs.deviceId);
2045 ASSERT_FALSE(device->isEnabled());
2046 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
2047 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
2048
2049 enableDevice(deviceId);
2050 mReader->loopOnce();
2051
2052 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2053 ASSERT_EQ(deviceId, resetArgs.deviceId);
2054 ASSERT_TRUE(device->isEnabled());
2055 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
2056 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
2057}
2058
2059TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToSubdeviceMappers) {
2060 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002061 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08002062 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
2063 // Add two subdevices to device
2064 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
2065 FakeInputMapper& mapperDevice1 =
2066 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
2067 FakeInputMapper& mapperDevice2 =
2068 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
2069 mReader->pushNextDevice(device);
2070 mReader->pushNextDevice(device);
2071 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
2072 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
2073
2074 mapperDevice1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
2075 mapperDevice2.setKeyCodeState(AKEYCODE_B, AKEY_STATE_DOWN);
2076
2077 ASSERT_EQ(AKEY_STATE_DOWN,
2078 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_A));
2079 ASSERT_EQ(AKEY_STATE_DOWN,
2080 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_B));
2081 ASSERT_EQ(AKEY_STATE_UNKNOWN,
2082 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_C));
2083}
2084
Prabir Pradhan7e186182020-11-10 13:56:45 -08002085TEST_F(InputReaderTest, ChangingPointerCaptureNotifiesInputListener) {
2086 NotifyPointerCaptureChangedArgs args;
2087
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002088 auto request = mFakePolicy->setPointerCapture(true);
Prabir Pradhan7e186182020-11-10 13:56:45 -08002089 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
2090 mReader->loopOnce();
2091 mFakeListener->assertNotifyCaptureWasCalled(&args);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002092 ASSERT_TRUE(args.request.enable) << "Pointer Capture should be enabled.";
2093 ASSERT_EQ(args.request, request) << "Pointer Capture sequence number should match.";
Prabir Pradhan7e186182020-11-10 13:56:45 -08002094
2095 mFakePolicy->setPointerCapture(false);
2096 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
2097 mReader->loopOnce();
2098 mFakeListener->assertNotifyCaptureWasCalled(&args);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002099 ASSERT_FALSE(args.request.enable) << "Pointer Capture should be disabled.";
Prabir Pradhan7e186182020-11-10 13:56:45 -08002100
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002101 // Verify that the Pointer Capture state is not updated when the configuration value
Prabir Pradhan7e186182020-11-10 13:56:45 -08002102 // does not change.
2103 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
2104 mReader->loopOnce();
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002105 mFakeListener->assertNotifyCaptureWasNotCalled();
Prabir Pradhan7e186182020-11-10 13:56:45 -08002106}
2107
Chris Ye87143712020-11-10 05:05:58 +00002108class FakeVibratorInputMapper : public FakeInputMapper {
2109public:
2110 FakeVibratorInputMapper(InputDeviceContext& deviceContext, uint32_t sources)
2111 : FakeInputMapper(deviceContext, sources) {}
2112
2113 std::vector<int32_t> getVibratorIds() override { return getDeviceContext().getVibratorIds(); }
2114};
2115
2116TEST_F(InputReaderTest, VibratorGetVibratorIds) {
2117 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002118 ftl::Flags<InputDeviceClass> deviceClass =
2119 InputDeviceClass::KEYBOARD | InputDeviceClass::VIBRATOR;
Chris Ye87143712020-11-10 05:05:58 +00002120 constexpr int32_t eventHubId = 1;
2121 const char* DEVICE_LOCATION = "BLUETOOTH";
2122 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
2123 FakeVibratorInputMapper& mapper =
2124 device->addMapper<FakeVibratorInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
2125 mReader->pushNextDevice(device);
2126
2127 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2128 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
2129
2130 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
2131 ASSERT_EQ(mReader->getVibratorIds(deviceId).size(), 2U);
2132}
2133
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002134// --- FakePeripheralController ---
Kim Low03ea0352020-11-06 12:45:07 -08002135
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002136class FakePeripheralController : public PeripheralControllerInterface {
Chris Yee2b1e5c2021-03-10 22:45:12 -08002137public:
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002138 FakePeripheralController(InputDeviceContext& deviceContext) : mDeviceContext(deviceContext) {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08002139
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002140 ~FakePeripheralController() override {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08002141
Andy Chen22c330c2022-08-29 20:07:10 -04002142 int32_t getEventHubId() const { return getDeviceContext().getEventHubId(); }
2143
Chris Yee2b1e5c2021-03-10 22:45:12 -08002144 void populateDeviceInfo(InputDeviceInfo* deviceInfo) override {}
2145
2146 void dump(std::string& dump) override {}
2147
2148 std::optional<int32_t> getBatteryCapacity(int32_t batteryId) override {
2149 return getDeviceContext().getBatteryCapacity(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08002150 }
2151
Chris Yee2b1e5c2021-03-10 22:45:12 -08002152 std::optional<int32_t> getBatteryStatus(int32_t batteryId) override {
2153 return getDeviceContext().getBatteryStatus(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08002154 }
Chris Ye3fdbfef2021-01-06 18:45:18 -08002155
2156 bool setLightColor(int32_t lightId, int32_t color) override {
2157 getDeviceContext().setLightBrightness(lightId, color >> 24);
2158 return true;
2159 }
2160
2161 std::optional<int32_t> getLightColor(int32_t lightId) override {
2162 std::optional<int32_t> result = getDeviceContext().getLightBrightness(lightId);
2163 if (!result.has_value()) {
2164 return std::nullopt;
2165 }
2166 return result.value() << 24;
2167 }
Chris Yee2b1e5c2021-03-10 22:45:12 -08002168
2169 bool setLightPlayerId(int32_t lightId, int32_t playerId) override { return true; }
2170
2171 std::optional<int32_t> getLightPlayerId(int32_t lightId) override { return std::nullopt; }
2172
2173private:
2174 InputDeviceContext& mDeviceContext;
2175 inline int32_t getDeviceId() { return mDeviceContext.getId(); }
2176 inline InputDeviceContext& getDeviceContext() { return mDeviceContext; }
Andy Chen22c330c2022-08-29 20:07:10 -04002177 inline InputDeviceContext& getDeviceContext() const { return mDeviceContext; }
Chris Ye3fdbfef2021-01-06 18:45:18 -08002178};
2179
Chris Yee2b1e5c2021-03-10 22:45:12 -08002180TEST_F(InputReaderTest, BatteryGetCapacity) {
2181 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002182 ftl::Flags<InputDeviceClass> deviceClass =
2183 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
Chris Yee2b1e5c2021-03-10 22:45:12 -08002184 constexpr int32_t eventHubId = 1;
2185 const char* DEVICE_LOCATION = "BLUETOOTH";
2186 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002187 FakePeripheralController& controller =
2188 device->addController<FakePeripheralController>(eventHubId);
Chris Yee2b1e5c2021-03-10 22:45:12 -08002189 mReader->pushNextDevice(device);
2190
2191 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2192
2193 ASSERT_EQ(controller.getBatteryCapacity(DEFAULT_BATTERY), BATTERY_CAPACITY);
2194 ASSERT_EQ(mReader->getBatteryCapacity(deviceId), BATTERY_CAPACITY);
2195}
2196
2197TEST_F(InputReaderTest, BatteryGetStatus) {
2198 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002199 ftl::Flags<InputDeviceClass> deviceClass =
2200 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
Chris Yee2b1e5c2021-03-10 22:45:12 -08002201 constexpr int32_t eventHubId = 1;
2202 const char* DEVICE_LOCATION = "BLUETOOTH";
2203 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002204 FakePeripheralController& controller =
2205 device->addController<FakePeripheralController>(eventHubId);
Chris Yee2b1e5c2021-03-10 22:45:12 -08002206 mReader->pushNextDevice(device);
2207
2208 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2209
2210 ASSERT_EQ(controller.getBatteryStatus(DEFAULT_BATTERY), BATTERY_STATUS);
2211 ASSERT_EQ(mReader->getBatteryStatus(deviceId), BATTERY_STATUS);
2212}
2213
Chris Ye3fdbfef2021-01-06 18:45:18 -08002214TEST_F(InputReaderTest, LightGetColor) {
2215 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002216 ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD | InputDeviceClass::LIGHT;
Chris Ye3fdbfef2021-01-06 18:45:18 -08002217 constexpr int32_t eventHubId = 1;
2218 const char* DEVICE_LOCATION = "BLUETOOTH";
2219 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002220 FakePeripheralController& controller =
2221 device->addController<FakePeripheralController>(eventHubId);
Chris Ye3fdbfef2021-01-06 18:45:18 -08002222 mReader->pushNextDevice(device);
2223 RawLightInfo info = {.id = 1,
2224 .name = "Mono",
2225 .maxBrightness = 255,
2226 .flags = InputLightClass::BRIGHTNESS,
2227 .path = ""};
2228 mFakeEventHub->addRawLightInfo(1 /* rawId */, std::move(info));
2229 mFakeEventHub->fakeLightBrightness(1 /* rawId */, 0x55);
2230
2231 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Chris Ye3fdbfef2021-01-06 18:45:18 -08002232
Chris Yee2b1e5c2021-03-10 22:45:12 -08002233 ASSERT_TRUE(controller.setLightColor(1 /* lightId */, LIGHT_BRIGHTNESS));
2234 ASSERT_EQ(controller.getLightColor(1 /* lightId */), LIGHT_BRIGHTNESS);
Chris Ye3fdbfef2021-01-06 18:45:18 -08002235 ASSERT_TRUE(mReader->setLightColor(deviceId, 1 /* lightId */, LIGHT_BRIGHTNESS));
2236 ASSERT_EQ(mReader->getLightColor(deviceId, 1 /* lightId */), LIGHT_BRIGHTNESS);
2237}
2238
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002239// --- InputReaderIntegrationTest ---
2240
2241// These tests create and interact with the InputReader only through its interface.
2242// The InputReader is started during SetUp(), which starts its processing in its own
2243// thread. The tests use linux uinput to emulate input devices.
2244// NOTE: Interacting with the physical device while these tests are running may cause
2245// the tests to fail.
2246class InputReaderIntegrationTest : public testing::Test {
2247protected:
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002248 std::unique_ptr<TestInputListener> mTestListener;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002249 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002250 std::unique_ptr<InputReaderInterface> mReader;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002251
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00002252 std::shared_ptr<FakePointerController> mFakePointerController;
2253
Chris Yea52ade12020-08-27 16:49:20 -07002254 void SetUp() override {
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002255 mFakePolicy = new FakeInputReaderPolicy();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00002256 mFakePointerController = std::make_shared<FakePointerController>();
2257 mFakePolicy->setPointerController(mFakePointerController);
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002258 mTestListener = std::make_unique<TestInputListener>(2000ms /*eventHappenedTimeout*/,
2259 30ms /*eventDidNotHappenTimeout*/);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002260
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002261 mReader = std::make_unique<InputReader>(std::make_shared<EventHub>(), mFakePolicy,
2262 *mTestListener);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002263 ASSERT_EQ(mReader->start(), OK);
2264
2265 // Since this test is run on a real device, all the input devices connected
2266 // to the test device will show up in mReader. We wait for those input devices to
2267 // show up before beginning the tests.
2268 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2269 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2270 }
2271
Chris Yea52ade12020-08-27 16:49:20 -07002272 void TearDown() override {
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002273 ASSERT_EQ(mReader->stop(), OK);
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002274 mReader.reset();
2275 mTestListener.reset();
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002276 mFakePolicy.clear();
2277 }
2278};
2279
2280TEST_F(InputReaderIntegrationTest, TestInvalidDevice) {
2281 // An invalid input device that is only used for this test.
2282 class InvalidUinputDevice : public UinputDevice {
2283 public:
2284 InvalidUinputDevice() : UinputDevice("Invalid Device") {}
2285
2286 private:
2287 void configureDevice(int fd, uinput_user_dev* device) override {}
2288 };
2289
2290 const size_t numDevices = mFakePolicy->getInputDevices().size();
2291
2292 // UinputDevice does not set any event or key bits, so InputReader should not
2293 // consider it as a valid device.
2294 std::unique_ptr<UinputDevice> invalidDevice = createUinputDevice<InvalidUinputDevice>();
2295 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
2296 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
2297 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
2298
2299 invalidDevice.reset();
2300 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
2301 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
2302 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
2303}
2304
2305TEST_F(InputReaderIntegrationTest, AddNewDevice) {
2306 const size_t initialNumDevices = mFakePolicy->getInputDevices().size();
2307
2308 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
2309 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2310 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2311 ASSERT_EQ(initialNumDevices + 1, mFakePolicy->getInputDevices().size());
2312
2313 // Find the test device by its name.
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002314 const std::vector<InputDeviceInfo> inputDevices = mFakePolicy->getInputDevices();
Chris Ye98d3f532020-10-01 21:48:59 -07002315 const auto& it =
2316 std::find_if(inputDevices.begin(), inputDevices.end(),
2317 [&keyboard](const InputDeviceInfo& info) {
2318 return info.getIdentifier().name == keyboard->getName();
2319 });
2320
2321 ASSERT_NE(it, inputDevices.end());
2322 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, it->getKeyboardType());
2323 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, it->getSources());
2324 ASSERT_EQ(0U, it->getMotionRanges().size());
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002325
2326 keyboard.reset();
2327 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2328 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2329 ASSERT_EQ(initialNumDevices, mFakePolicy->getInputDevices().size());
2330}
2331
2332TEST_F(InputReaderIntegrationTest, SendsEventsToInputListener) {
2333 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
2334 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2335
2336 NotifyConfigurationChangedArgs configChangedArgs;
2337 ASSERT_NO_FATAL_FAILURE(
2338 mTestListener->assertNotifyConfigurationChangedWasCalled(&configChangedArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002339 int32_t prevId = configChangedArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002340 nsecs_t prevTimestamp = configChangedArgs.eventTime;
2341
2342 NotifyKeyArgs keyArgs;
2343 keyboard->pressAndReleaseHomeKey();
2344 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
2345 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002346 ASSERT_NE(prevId, keyArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002347 prevId = keyArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002348 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002349 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002350 prevTimestamp = keyArgs.eventTime;
2351
2352 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
2353 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002354 ASSERT_NE(prevId, keyArgs.id);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002355 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002356 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002357}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002358
Siarhei Vishniakoua0d2b802020-05-13 14:00:31 -07002359/**
2360 * The Steam controller sends BTN_GEAR_DOWN and BTN_GEAR_UP for the two "paddle" buttons
2361 * on the back. In this test, we make sure that BTN_GEAR_DOWN / BTN_WHEEL and BTN_GEAR_UP
2362 * are passed to the listener.
2363 */
2364static_assert(BTN_GEAR_DOWN == BTN_WHEEL);
2365TEST_F(InputReaderIntegrationTest, SendsGearDownAndUpToInputListener) {
2366 std::unique_ptr<UinputSteamController> controller = createUinputDevice<UinputSteamController>();
2367 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2368 NotifyKeyArgs keyArgs;
2369
2370 controller->pressAndReleaseKey(BTN_GEAR_DOWN);
2371 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
2372 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
2373 ASSERT_EQ(BTN_GEAR_DOWN, keyArgs.scanCode);
2374
2375 controller->pressAndReleaseKey(BTN_GEAR_UP);
2376 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
2377 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
2378 ASSERT_EQ(BTN_GEAR_UP, keyArgs.scanCode);
2379}
2380
Arthur Hungaab25622020-01-16 11:22:11 +08002381// --- TouchProcessTest ---
2382class TouchIntegrationTest : public InputReaderIntegrationTest {
2383protected:
Arthur Hungaab25622020-01-16 11:22:11 +08002384 const std::string UNIQUE_ID = "local:0";
2385
Chris Yea52ade12020-08-27 16:49:20 -07002386 void SetUp() override {
Arthur Hungaab25622020-01-16 11:22:11 +08002387 InputReaderIntegrationTest::SetUp();
2388 // At least add an internal display.
2389 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2390 DISPLAY_ORIENTATION_0, UNIQUE_ID, NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002391 ViewportType::INTERNAL);
Arthur Hungaab25622020-01-16 11:22:11 +08002392
2393 mDevice = createUinputDevice<UinputTouchScreen>(Rect(0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT));
2394 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2395 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2396 }
2397
2398 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
2399 int32_t orientation, const std::string& uniqueId,
2400 std::optional<uint8_t> physicalPort,
2401 ViewportType viewportType) {
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002402 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, true /*isActive*/,
2403 uniqueId, physicalPort, viewportType);
Arthur Hungaab25622020-01-16 11:22:11 +08002404 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2405 }
2406
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002407 void assertReceivedMotion(int32_t action, const std::vector<Point>& points) {
2408 NotifyMotionArgs args;
2409 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2410 EXPECT_EQ(action, args.action);
2411 ASSERT_EQ(points.size(), args.pointerCount);
2412 for (size_t i = 0; i < args.pointerCount; i++) {
2413 EXPECT_EQ(points[i].x, args.pointerCoords[i].getX());
2414 EXPECT_EQ(points[i].y, args.pointerCoords[i].getY());
2415 }
2416 }
2417
Arthur Hungaab25622020-01-16 11:22:11 +08002418 std::unique_ptr<UinputTouchScreen> mDevice;
2419};
2420
2421TEST_F(TouchIntegrationTest, InputEvent_ProcessSingleTouch) {
2422 NotifyMotionArgs args;
2423 const Point centerPoint = mDevice->getCenterPoint();
2424
2425 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00002426 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002427 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002428 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002429 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2430 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2431
2432 // ACTION_MOVE
2433 mDevice->sendMove(centerPoint + Point(1, 1));
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002434 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002435 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2436 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2437
2438 // ACTION_UP
2439 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002440 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002441 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2442 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2443}
2444
2445TEST_F(TouchIntegrationTest, InputEvent_ProcessMultiTouch) {
2446 NotifyMotionArgs args;
2447 const Point centerPoint = mDevice->getCenterPoint();
2448
2449 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00002450 mDevice->sendSlot(FIRST_SLOT);
2451 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002452 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002453 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002454 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2455 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2456
2457 // ACTION_POINTER_DOWN (Second slot)
2458 const Point secondPoint = centerPoint + Point(100, 100);
2459 mDevice->sendSlot(SECOND_SLOT);
2460 mDevice->sendTrackingId(SECOND_TRACKING_ID);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002461 mDevice->sendDown(secondPoint);
2462 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002463 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08002464 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002465
2466 // ACTION_MOVE (Second slot)
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002467 mDevice->sendMove(secondPoint + Point(1, 1));
2468 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002469 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2470 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2471
2472 // ACTION_POINTER_UP (Second slot)
arthurhungcc7f9802020-04-30 17:55:40 +08002473 mDevice->sendPointerUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002474 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002475 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08002476 ASSERT_EQ(ACTION_POINTER_1_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002477
2478 // ACTION_UP
2479 mDevice->sendSlot(FIRST_SLOT);
2480 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002481 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002482 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2483 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2484}
2485
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002486/**
2487 * What happens when a pointer goes up while another pointer moves in the same frame? Are POINTER_UP
2488 * events guaranteed to contain the same data as a preceding MOVE, or can they contain different
2489 * data?
2490 * In this test, we try to send a change in coordinates in Pointer 0 in the same frame as the
2491 * liftoff of Pointer 1. We check that POINTER_UP event is generated first, and the MOVE event
2492 * for Pointer 0 only is generated after.
2493 * Suppose we are only interested in learning the movement of Pointer 0. If we only observe MOVE
2494 * events, we will not miss any information.
2495 * Even though the Pointer 1 up event contains updated Pointer 0 coordinates, there is another MOVE
2496 * event generated afterwards that contains the newest movement of pointer 0.
2497 * This is important for palm rejection. If there is a subsequent InputListener stage that detects
2498 * palms, and wants to cancel Pointer 1, then it is safe to simply drop POINTER_1_UP event without
2499 * losing information about non-palm pointers.
2500 */
2501TEST_F(TouchIntegrationTest, MultiTouch_PointerMoveAndSecondPointerUp) {
2502 NotifyMotionArgs args;
2503 const Point centerPoint = mDevice->getCenterPoint();
2504
2505 // ACTION_DOWN
2506 mDevice->sendSlot(FIRST_SLOT);
2507 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2508 mDevice->sendDown(centerPoint);
2509 mDevice->sendSync();
2510 assertReceivedMotion(AMOTION_EVENT_ACTION_DOWN, {centerPoint});
2511
2512 // ACTION_POINTER_DOWN (Second slot)
2513 const Point secondPoint = centerPoint + Point(100, 100);
2514 mDevice->sendSlot(SECOND_SLOT);
2515 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2516 mDevice->sendDown(secondPoint);
2517 mDevice->sendSync();
2518 assertReceivedMotion(ACTION_POINTER_1_DOWN, {centerPoint, secondPoint});
2519
2520 // ACTION_MOVE (First slot)
2521 mDevice->sendSlot(FIRST_SLOT);
2522 mDevice->sendMove(centerPoint + Point(5, 5));
2523 // ACTION_POINTER_UP (Second slot)
2524 mDevice->sendSlot(SECOND_SLOT);
2525 mDevice->sendPointerUp();
2526 // Send a single sync for the above 2 pointer updates
2527 mDevice->sendSync();
2528
2529 // First, we should get POINTER_UP for the second pointer
2530 assertReceivedMotion(ACTION_POINTER_1_UP,
2531 {/*first pointer */ centerPoint + Point(5, 5),
2532 /*second pointer*/ secondPoint});
2533
2534 // Next, the MOVE event for the first pointer
2535 assertReceivedMotion(AMOTION_EVENT_ACTION_MOVE, {centerPoint + Point(5, 5)});
2536}
2537
2538/**
2539 * Similar scenario as above. The difference is that when the second pointer goes up, it will first
2540 * move, and then it will go up, all in the same frame.
2541 * In this scenario, the movement of the second pointer just prior to liftoff is ignored, and never
2542 * gets sent to the listener.
2543 */
2544TEST_F(TouchIntegrationTest, MultiTouch_PointerMoveAndSecondPointerMoveAndUp) {
2545 NotifyMotionArgs args;
2546 const Point centerPoint = mDevice->getCenterPoint();
2547
2548 // ACTION_DOWN
2549 mDevice->sendSlot(FIRST_SLOT);
2550 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2551 mDevice->sendDown(centerPoint);
2552 mDevice->sendSync();
2553 assertReceivedMotion(AMOTION_EVENT_ACTION_DOWN, {centerPoint});
2554
2555 // ACTION_POINTER_DOWN (Second slot)
2556 const Point secondPoint = centerPoint + Point(100, 100);
2557 mDevice->sendSlot(SECOND_SLOT);
2558 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2559 mDevice->sendDown(secondPoint);
2560 mDevice->sendSync();
2561 assertReceivedMotion(ACTION_POINTER_1_DOWN, {centerPoint, secondPoint});
2562
2563 // ACTION_MOVE (First slot)
2564 mDevice->sendSlot(FIRST_SLOT);
2565 mDevice->sendMove(centerPoint + Point(5, 5));
2566 // ACTION_POINTER_UP (Second slot)
2567 mDevice->sendSlot(SECOND_SLOT);
2568 mDevice->sendMove(secondPoint + Point(6, 6));
2569 mDevice->sendPointerUp();
2570 // Send a single sync for the above 2 pointer updates
2571 mDevice->sendSync();
2572
2573 // First, we should get POINTER_UP for the second pointer
2574 // The movement of the second pointer during the liftoff frame is ignored.
2575 // The coordinates 'secondPoint + Point(6, 6)' are never sent to the listener.
2576 assertReceivedMotion(ACTION_POINTER_1_UP,
2577 {/*first pointer */ centerPoint + Point(5, 5),
2578 /*second pointer*/ secondPoint});
2579
2580 // Next, the MOVE event for the first pointer
2581 assertReceivedMotion(AMOTION_EVENT_ACTION_MOVE, {centerPoint + Point(5, 5)});
2582}
2583
Arthur Hungaab25622020-01-16 11:22:11 +08002584TEST_F(TouchIntegrationTest, InputEvent_ProcessPalm) {
2585 NotifyMotionArgs args;
2586 const Point centerPoint = mDevice->getCenterPoint();
2587
2588 // ACTION_DOWN
arthurhungcc7f9802020-04-30 17:55:40 +08002589 mDevice->sendSlot(FIRST_SLOT);
2590 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002591 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002592 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002593 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2594 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2595
arthurhungcc7f9802020-04-30 17:55:40 +08002596 // ACTION_POINTER_DOWN (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002597 const Point secondPoint = centerPoint + Point(100, 100);
2598 mDevice->sendSlot(SECOND_SLOT);
2599 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2600 mDevice->sendDown(secondPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002601 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002602 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08002603 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002604
arthurhungcc7f9802020-04-30 17:55:40 +08002605 // ACTION_MOVE (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002606 mDevice->sendMove(secondPoint + Point(1, 1));
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002607 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002608 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2609 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2610
arthurhungcc7f9802020-04-30 17:55:40 +08002611 // Send MT_TOOL_PALM (second slot), which indicates that the touch IC has determined this to be
2612 // a palm event.
2613 // Expect to receive the ACTION_POINTER_UP with cancel flag.
Arthur Hungaab25622020-01-16 11:22:11 +08002614 mDevice->sendToolType(MT_TOOL_PALM);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002615 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002616 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08002617 ASSERT_EQ(ACTION_POINTER_1_UP, args.action);
arthurhungcc7f9802020-04-30 17:55:40 +08002618 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, args.flags);
Arthur Hungaab25622020-01-16 11:22:11 +08002619
arthurhungcc7f9802020-04-30 17:55:40 +08002620 // Send up to second slot, expect first slot send moving.
2621 mDevice->sendPointerUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002622 mDevice->sendSync();
arthurhungcc7f9802020-04-30 17:55:40 +08002623 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2624 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002625
arthurhungcc7f9802020-04-30 17:55:40 +08002626 // Send ACTION_UP (first slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002627 mDevice->sendSlot(FIRST_SLOT);
2628 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002629 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002630
arthurhungcc7f9802020-04-30 17:55:40 +08002631 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2632 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002633}
2634
Michael Wrightd02c5b62014-02-10 15:10:22 -08002635// --- InputDeviceTest ---
Michael Wrightd02c5b62014-02-10 15:10:22 -08002636class InputDeviceTest : public testing::Test {
2637protected:
2638 static const char* DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002639 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002640 static const int32_t DEVICE_ID;
2641 static const int32_t DEVICE_GENERATION;
2642 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002643 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002644 static const int32_t EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002645
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002646 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002647 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002648 std::unique_ptr<TestInputListener> mFakeListener;
arthurhungdcef2dc2020-08-11 14:47:50 +08002649 std::unique_ptr<InstrumentedInputReader> mReader;
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00002650 std::shared_ptr<InputDevice> mDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002651
Chris Yea52ade12020-08-27 16:49:20 -07002652 void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002653 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002654 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002655 mFakeListener = std::make_unique<TestInputListener>();
arthurhungdcef2dc2020-08-11 14:47:50 +08002656 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002657 *mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002658 InputDeviceIdentifier identifier;
2659 identifier.name = DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002660 identifier.location = DEVICE_LOCATION;
arthurhungdcef2dc2020-08-11 14:47:50 +08002661 mDevice = std::make_shared<InputDevice>(mReader->getContext(), DEVICE_ID, DEVICE_GENERATION,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002662 identifier);
arthurhungdcef2dc2020-08-11 14:47:50 +08002663 mReader->pushNextDevice(mDevice);
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002664 mFakeEventHub->addDevice(EVENTHUB_ID, DEVICE_NAME, ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08002665 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002666 }
2667
Chris Yea52ade12020-08-27 16:49:20 -07002668 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002669 mFakeListener.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002670 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002671 }
2672};
2673
2674const char* InputDeviceTest::DEVICE_NAME = "device";
Arthur Hung2c9a3342019-07-23 14:18:59 +08002675const char* InputDeviceTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002676const int32_t InputDeviceTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002677const int32_t InputDeviceTest::DEVICE_GENERATION = 2;
2678const int32_t InputDeviceTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002679const ftl::Flags<InputDeviceClass> InputDeviceTest::DEVICE_CLASSES =
Chris Ye1b0c7342020-07-28 21:57:03 -07002680 InputDeviceClass::KEYBOARD | InputDeviceClass::TOUCH | InputDeviceClass::JOYSTICK;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002681const int32_t InputDeviceTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002682
2683TEST_F(InputDeviceTest, ImmutableProperties) {
2684 ASSERT_EQ(DEVICE_ID, mDevice->getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002685 ASSERT_STREQ(DEVICE_NAME, mDevice->getName().c_str());
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002686 ASSERT_EQ(ftl::Flags<InputDeviceClass>(0), mDevice->getClasses());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002687}
2688
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002689TEST_F(InputDeviceTest, WhenDeviceCreated_EnabledIsFalse) {
2690 ASSERT_EQ(mDevice->isEnabled(), false);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07002691}
2692
Michael Wrightd02c5b62014-02-10 15:10:22 -08002693TEST_F(InputDeviceTest, WhenNoMappersAreRegistered_DeviceIsIgnored) {
2694 // Configuration.
2695 InputReaderConfiguration config;
2696 mDevice->configure(ARBITRARY_TIME, &config, 0);
2697
2698 // Reset.
2699 mDevice->reset(ARBITRARY_TIME);
2700
2701 NotifyDeviceResetArgs resetArgs;
2702 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2703 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2704 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2705
2706 // Metadata.
2707 ASSERT_TRUE(mDevice->isIgnored());
2708 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mDevice->getSources());
2709
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002710 InputDeviceInfo info = mDevice->getDeviceInfo();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002711 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002712 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002713 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NONE, info.getKeyboardType());
2714 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, info.getSources());
2715
2716 // State queries.
2717 ASSERT_EQ(0, mDevice->getMetaState());
2718
2719 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2720 << "Ignored device should return unknown key code state.";
2721 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2722 << "Ignored device should return unknown scan code state.";
2723 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 0))
2724 << "Ignored device should return unknown switch state.";
2725
2726 const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
2727 uint8_t flags[2] = { 0, 1 };
2728 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, 2, keyCodes, flags))
2729 << "Ignored device should never mark any key codes.";
2730 ASSERT_EQ(0, flags[0]) << "Flag for unsupported key should be unchanged.";
2731 ASSERT_EQ(1, flags[1]) << "Flag for unsupported key should be unchanged.";
2732}
2733
2734TEST_F(InputDeviceTest, WhenMappersAreRegistered_DeviceIsNotIgnoredAndForwardsRequestsToMappers) {
2735 // Configuration.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002736 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, String8("key"), String8("value"));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002737
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002738 FakeInputMapper& mapper1 =
2739 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002740 mapper1.setKeyboardType(AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2741 mapper1.setMetaState(AMETA_ALT_ON);
2742 mapper1.addSupportedKeyCode(AKEYCODE_A);
2743 mapper1.addSupportedKeyCode(AKEYCODE_B);
2744 mapper1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
2745 mapper1.setKeyCodeState(AKEYCODE_B, AKEY_STATE_UP);
2746 mapper1.setScanCodeState(2, AKEY_STATE_DOWN);
2747 mapper1.setScanCodeState(3, AKEY_STATE_UP);
2748 mapper1.setSwitchState(4, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002749
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002750 FakeInputMapper& mapper2 =
2751 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002752 mapper2.setMetaState(AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002753
2754 InputReaderConfiguration config;
2755 mDevice->configure(ARBITRARY_TIME, &config, 0);
2756
2757 String8 propertyValue;
2758 ASSERT_TRUE(mDevice->getConfiguration().tryGetProperty(String8("key"), propertyValue))
2759 << "Device should have read configuration during configuration phase.";
2760 ASSERT_STREQ("value", propertyValue.string());
2761
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002762 ASSERT_NO_FATAL_FAILURE(mapper1.assertConfigureWasCalled());
2763 ASSERT_NO_FATAL_FAILURE(mapper2.assertConfigureWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002764
2765 // Reset
2766 mDevice->reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002767 ASSERT_NO_FATAL_FAILURE(mapper1.assertResetWasCalled());
2768 ASSERT_NO_FATAL_FAILURE(mapper2.assertResetWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002769
2770 NotifyDeviceResetArgs resetArgs;
2771 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2772 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2773 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2774
2775 // Metadata.
2776 ASSERT_FALSE(mDevice->isIgnored());
2777 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), mDevice->getSources());
2778
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002779 InputDeviceInfo info = mDevice->getDeviceInfo();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002780 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002781 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002782 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_ALPHABETIC, info.getKeyboardType());
2783 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), info.getSources());
2784
2785 // State queries.
2786 ASSERT_EQ(AMETA_ALT_ON | AMETA_SHIFT_ON, mDevice->getMetaState())
2787 << "Should query mappers and combine meta states.";
2788
2789 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2790 << "Should return unknown key code state when source not supported.";
2791 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2792 << "Should return unknown scan code state when source not supported.";
2793 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2794 << "Should return unknown switch state when source not supported.";
2795
2796 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, AKEYCODE_A))
2797 << "Should query mapper when source is supported.";
2798 ASSERT_EQ(AKEY_STATE_UP, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 3))
2799 << "Should query mapper when source is supported.";
2800 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 4))
2801 << "Should query mapper when source is supported.";
2802
2803 const int32_t keyCodes[4] = { AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2 };
2804 uint8_t flags[4] = { 0, 0, 0, 1 };
2805 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
2806 << "Should do nothing when source is unsupported.";
2807 ASSERT_EQ(0, flags[0]) << "Flag should be unchanged when source is unsupported.";
2808 ASSERT_EQ(0, flags[1]) << "Flag should be unchanged when source is unsupported.";
2809 ASSERT_EQ(0, flags[2]) << "Flag should be unchanged when source is unsupported.";
2810 ASSERT_EQ(1, flags[3]) << "Flag should be unchanged when source is unsupported.";
2811
2812 ASSERT_TRUE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, 4, keyCodes, flags))
2813 << "Should query mapper when source is supported.";
2814 ASSERT_EQ(1, flags[0]) << "Flag for supported key should be set.";
2815 ASSERT_EQ(1, flags[1]) << "Flag for supported key should be set.";
2816 ASSERT_EQ(0, flags[2]) << "Flag for unsupported key should be unchanged.";
2817 ASSERT_EQ(1, flags[3]) << "Flag for unsupported key should be unchanged.";
2818
2819 // Event handling.
2820 RawEvent event;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002821 event.deviceId = EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002822 mDevice->process(&event, 1);
2823
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002824 ASSERT_NO_FATAL_FAILURE(mapper1.assertProcessWasCalled());
2825 ASSERT_NO_FATAL_FAILURE(mapper2.assertProcessWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002826}
2827
Arthur Hung2c9a3342019-07-23 14:18:59 +08002828// A single input device is associated with a specific display. Check that:
2829// 1. Device is disabled if the viewport corresponding to the associated display is not found
2830// 2. Device is disabled when setEnabled API is called
2831TEST_F(InputDeviceTest, Configure_AssignsDisplayPort) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002832 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002833
2834 // First Configuration.
2835 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
2836
2837 // Device should be enabled by default.
2838 ASSERT_TRUE(mDevice->isEnabled());
2839
2840 // Prepare associated info.
2841 constexpr uint8_t hdmi = 1;
2842 const std::string UNIQUE_ID = "local:1";
2843
2844 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi);
2845 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2846 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2847 // Device should be disabled because it is associated with a specific display via
2848 // input port <-> display port association, but the corresponding display is not found
2849 ASSERT_FALSE(mDevice->isEnabled());
2850
2851 // Prepare displays.
2852 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002853 DISPLAY_ORIENTATION_0, true /*isActive*/, UNIQUE_ID, hdmi,
2854 ViewportType::INTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002855 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2856 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2857 ASSERT_TRUE(mDevice->isEnabled());
2858
2859 // Device should be disabled after set disable.
2860 mFakePolicy->addDisabledDevice(mDevice->getId());
2861 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2862 InputReaderConfiguration::CHANGE_ENABLED_STATE);
2863 ASSERT_FALSE(mDevice->isEnabled());
2864
2865 // Device should still be disabled even found the associated display.
2866 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2867 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2868 ASSERT_FALSE(mDevice->isEnabled());
2869}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002870
Christine Franks1ba71cc2021-04-07 14:37:42 -07002871TEST_F(InputDeviceTest, Configure_AssignsDisplayUniqueId) {
2872 // Device should be enabled by default.
2873 mFakePolicy->clearViewports();
2874 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
2875 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
2876 ASSERT_TRUE(mDevice->isEnabled());
2877
2878 // Device should be disabled because it is associated with a specific display, but the
2879 // corresponding display is not found.
2880 const std::string DISPLAY_UNIQUE_ID = "displayUniqueId";
Christine Franks2a2293c2022-01-18 11:51:16 -08002881 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002882 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2883 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2884 ASSERT_FALSE(mDevice->isEnabled());
2885
2886 // Device should be enabled when a display is found.
2887 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2888 DISPLAY_ORIENTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
2889 NO_PORT, ViewportType::INTERNAL);
2890 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2891 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2892 ASSERT_TRUE(mDevice->isEnabled());
2893
2894 // Device should be disabled after set disable.
2895 mFakePolicy->addDisabledDevice(mDevice->getId());
2896 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2897 InputReaderConfiguration::CHANGE_ENABLED_STATE);
2898 ASSERT_FALSE(mDevice->isEnabled());
2899
2900 // Device should still be disabled even found the associated display.
2901 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2902 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2903 ASSERT_FALSE(mDevice->isEnabled());
2904}
2905
Christine Franks2a2293c2022-01-18 11:51:16 -08002906TEST_F(InputDeviceTest, Configure_UniqueId_CorrectlyMatches) {
2907 mFakePolicy->clearViewports();
2908 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
2909 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
2910
2911 const std::string DISPLAY_UNIQUE_ID = "displayUniqueId";
2912 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
2913 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2914 DISPLAY_ORIENTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
2915 NO_PORT, ViewportType::INTERNAL);
2916 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2917 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2918 ASSERT_EQ(DISPLAY_UNIQUE_ID, mDevice->getAssociatedDisplayUniqueId());
2919}
2920
Michael Wrightd02c5b62014-02-10 15:10:22 -08002921// --- InputMapperTest ---
2922
2923class InputMapperTest : public testing::Test {
2924protected:
2925 static const char* DEVICE_NAME;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002926 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002927 static const int32_t DEVICE_ID;
2928 static const int32_t DEVICE_GENERATION;
2929 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002930 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002931 static const int32_t EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002932
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002933 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002934 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002935 std::unique_ptr<TestInputListener> mFakeListener;
arthurhungdcef2dc2020-08-11 14:47:50 +08002936 std::unique_ptr<InstrumentedInputReader> mReader;
2937 std::shared_ptr<InputDevice> mDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002938
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002939 virtual void SetUp(ftl::Flags<InputDeviceClass> classes) {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002940 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002941 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002942 mFakeListener = std::make_unique<TestInputListener>();
arthurhungdcef2dc2020-08-11 14:47:50 +08002943 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002944 *mFakeListener);
arthurhungdcef2dc2020-08-11 14:47:50 +08002945 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002946 }
2947
Prabir Pradhanc14266f2021-05-12 15:56:24 -07002948 void SetUp() override {
Prabir Pradhanc14266f2021-05-12 15:56:24 -07002949 SetUp(DEVICE_CLASSES);
2950 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002951
Chris Yea52ade12020-08-27 16:49:20 -07002952 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002953 mFakeListener.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002954 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002955 }
2956
2957 void addConfigurationProperty(const char* key, const char* value) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002958 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, String8(key), String8(value));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002959 }
2960
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002961 void configureDevice(uint32_t changes) {
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00002962 if (!changes ||
2963 (changes &
2964 (InputReaderConfiguration::CHANGE_DISPLAY_INFO |
2965 InputReaderConfiguration::CHANGE_POINTER_CAPTURE))) {
arthurhungdcef2dc2020-08-11 14:47:50 +08002966 mReader->requestRefreshConfiguration(changes);
2967 mReader->loopOnce();
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -08002968 }
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002969 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
2970 }
2971
arthurhungdcef2dc2020-08-11 14:47:50 +08002972 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
2973 const std::string& location, int32_t eventHubId,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002974 ftl::Flags<InputDeviceClass> classes) {
arthurhungdcef2dc2020-08-11 14:47:50 +08002975 InputDeviceIdentifier identifier;
2976 identifier.name = name;
2977 identifier.location = location;
2978 std::shared_ptr<InputDevice> device =
2979 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
2980 identifier);
2981 mReader->pushNextDevice(device);
2982 mFakeEventHub->addDevice(eventHubId, name, classes);
2983 mReader->loopOnce();
2984 return device;
2985 }
2986
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002987 template <class T, typename... Args>
2988 T& addMapperAndConfigure(Args... args) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002989 T& mapper = mDevice->addMapper<T>(EVENTHUB_ID, args...);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002990 configureDevice(0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002991 mDevice->reset(ARBITRARY_TIME);
Chris Ye42b06822020-08-07 11:39:33 -07002992 mapper.reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002993 return mapper;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002994 }
2995
2996 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002997 int32_t orientation, const std::string& uniqueId,
2998 std::optional<uint8_t> physicalPort, ViewportType viewportType) {
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002999 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, true /*isActive*/,
3000 uniqueId, physicalPort, viewportType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003001 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3002 }
3003
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003004 void clearViewports() {
3005 mFakePolicy->clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003006 }
3007
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003008 void process(InputMapper& mapper, nsecs_t when, nsecs_t readTime, int32_t type, int32_t code,
3009 int32_t value) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003010 RawEvent event;
3011 event.when = when;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003012 event.readTime = readTime;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003013 event.deviceId = mapper.getDeviceContext().getEventHubId();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003014 event.type = type;
3015 event.code = code;
3016 event.value = value;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003017 mapper.process(&event);
arthurhungdcef2dc2020-08-11 14:47:50 +08003018 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003019 }
3020
3021 static void assertMotionRange(const InputDeviceInfo& info,
3022 int32_t axis, uint32_t source, float min, float max, float flat, float fuzz) {
3023 const InputDeviceInfo::MotionRange* range = info.getMotionRange(axis, source);
Yi Kong9b14ac62018-07-17 13:48:38 -07003024 ASSERT_TRUE(range != nullptr) << "Axis: " << axis << " Source: " << source;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003025 ASSERT_EQ(axis, range->axis) << "Axis: " << axis << " Source: " << source;
3026 ASSERT_EQ(source, range->source) << "Axis: " << axis << " Source: " << source;
3027 ASSERT_NEAR(min, range->min, EPSILON) << "Axis: " << axis << " Source: " << source;
3028 ASSERT_NEAR(max, range->max, EPSILON) << "Axis: " << axis << " Source: " << source;
3029 ASSERT_NEAR(flat, range->flat, EPSILON) << "Axis: " << axis << " Source: " << source;
3030 ASSERT_NEAR(fuzz, range->fuzz, EPSILON) << "Axis: " << axis << " Source: " << source;
3031 }
3032
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003033 static void assertPointerCoords(const PointerCoords& coords, float x, float y, float pressure,
3034 float size, float touchMajor, float touchMinor, float toolMajor,
3035 float toolMinor, float orientation, float distance,
3036 float scaledAxisEpsilon = 1.f) {
3037 ASSERT_NEAR(x, coords.getAxisValue(AMOTION_EVENT_AXIS_X), scaledAxisEpsilon);
3038 ASSERT_NEAR(y, coords.getAxisValue(AMOTION_EVENT_AXIS_Y), scaledAxisEpsilon);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003039 ASSERT_NEAR(pressure, coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), EPSILON);
3040 ASSERT_NEAR(size, coords.getAxisValue(AMOTION_EVENT_AXIS_SIZE), EPSILON);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003041 ASSERT_NEAR(touchMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR),
3042 scaledAxisEpsilon);
3043 ASSERT_NEAR(touchMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR),
3044 scaledAxisEpsilon);
3045 ASSERT_NEAR(toolMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR),
3046 scaledAxisEpsilon);
3047 ASSERT_NEAR(toolMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR),
3048 scaledAxisEpsilon);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003049 ASSERT_NEAR(orientation, coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION), EPSILON);
3050 ASSERT_NEAR(distance, coords.getAxisValue(AMOTION_EVENT_AXIS_DISTANCE), EPSILON);
3051 }
3052
Michael Wright17db18e2020-06-26 20:51:44 +01003053 static void assertPosition(const FakePointerController& controller, float x, float y) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003054 float actualX, actualY;
Michael Wright17db18e2020-06-26 20:51:44 +01003055 controller.getPosition(&actualX, &actualY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003056 ASSERT_NEAR(x, actualX, 1);
3057 ASSERT_NEAR(y, actualY, 1);
3058 }
3059};
3060
3061const char* InputMapperTest::DEVICE_NAME = "device";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003062const char* InputMapperTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003063const int32_t InputMapperTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003064const int32_t InputMapperTest::DEVICE_GENERATION = 2;
3065const int32_t InputMapperTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003066const ftl::Flags<InputDeviceClass> InputMapperTest::DEVICE_CLASSES =
3067 ftl::Flags<InputDeviceClass>(0); // not needed for current tests
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003068const int32_t InputMapperTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003069
3070// --- SwitchInputMapperTest ---
3071
3072class SwitchInputMapperTest : public InputMapperTest {
3073protected:
3074};
3075
3076TEST_F(SwitchInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003077 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003078
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003079 ASSERT_EQ(uint32_t(AINPUT_SOURCE_SWITCH), mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003080}
3081
3082TEST_F(SwitchInputMapperTest, GetSwitchState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003083 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003084
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003085 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003086 ASSERT_EQ(1, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003087
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003088 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003089 ASSERT_EQ(0, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003090}
3091
3092TEST_F(SwitchInputMapperTest, Process) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003093 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003094
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003095 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_LID, 1);
3096 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_JACK_PHYSICAL_INSERT, 1);
3097 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_HEADPHONE_INSERT, 0);
3098 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003099
3100 NotifySwitchArgs args;
3101 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySwitchWasCalled(&args));
3102 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
Dan Albert1bd2fc02016-02-02 15:11:57 -08003103 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT), args.switchValues);
3104 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT) | (1 << SW_HEADPHONE_INSERT),
Michael Wrightd02c5b62014-02-10 15:10:22 -08003105 args.switchMask);
3106 ASSERT_EQ(uint32_t(0), args.policyFlags);
3107}
3108
Chris Ye87143712020-11-10 05:05:58 +00003109// --- VibratorInputMapperTest ---
3110class VibratorInputMapperTest : public InputMapperTest {
3111protected:
3112 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::VIBRATOR); }
3113};
3114
3115TEST_F(VibratorInputMapperTest, GetSources) {
3116 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
3117
3118 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mapper.getSources());
3119}
3120
3121TEST_F(VibratorInputMapperTest, GetVibratorIds) {
3122 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
3123
3124 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
3125}
3126
3127TEST_F(VibratorInputMapperTest, Vibrate) {
3128 constexpr uint8_t DEFAULT_AMPLITUDE = 192;
Chris Yefb552902021-02-03 17:18:37 -08003129 constexpr int32_t VIBRATION_TOKEN = 100;
Chris Ye87143712020-11-10 05:05:58 +00003130 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
3131
3132 VibrationElement pattern(2);
3133 VibrationSequence sequence(2);
3134 pattern.duration = std::chrono::milliseconds(200);
3135 pattern.channels = {{0 /* vibratorId */, DEFAULT_AMPLITUDE / 2},
3136 {1 /* vibratorId */, DEFAULT_AMPLITUDE}};
3137 sequence.addElement(pattern);
3138 pattern.duration = std::chrono::milliseconds(500);
3139 pattern.channels = {{0 /* vibratorId */, DEFAULT_AMPLITUDE / 4},
3140 {1 /* vibratorId */, DEFAULT_AMPLITUDE}};
3141 sequence.addElement(pattern);
3142
3143 std::vector<int64_t> timings = {0, 1};
3144 std::vector<uint8_t> amplitudes = {DEFAULT_AMPLITUDE, DEFAULT_AMPLITUDE / 2};
3145
3146 ASSERT_FALSE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08003147 // Start vibrating
3148 mapper.vibrate(sequence, -1 /* repeat */, VIBRATION_TOKEN);
Chris Ye87143712020-11-10 05:05:58 +00003149 ASSERT_TRUE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08003150 // Verify vibrator state listener was notified.
3151 mReader->loopOnce();
3152 NotifyVibratorStateArgs args;
3153 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyVibratorStateWasCalled(&args));
3154 ASSERT_EQ(DEVICE_ID, args.deviceId);
3155 ASSERT_TRUE(args.isOn);
3156 // Stop vibrating
3157 mapper.cancelVibrate(VIBRATION_TOKEN);
3158 ASSERT_FALSE(mapper.isVibrating());
3159 // Verify vibrator state listener was notified.
3160 mReader->loopOnce();
3161 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyVibratorStateWasCalled(&args));
3162 ASSERT_EQ(DEVICE_ID, args.deviceId);
3163 ASSERT_FALSE(args.isOn);
Chris Ye87143712020-11-10 05:05:58 +00003164}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003165
Chris Yef59a2f42020-10-16 12:55:26 -07003166// --- SensorInputMapperTest ---
3167
3168class SensorInputMapperTest : public InputMapperTest {
3169protected:
3170 static const int32_t ACCEL_RAW_MIN;
3171 static const int32_t ACCEL_RAW_MAX;
3172 static const int32_t ACCEL_RAW_FUZZ;
3173 static const int32_t ACCEL_RAW_FLAT;
3174 static const int32_t ACCEL_RAW_RESOLUTION;
3175
3176 static const int32_t GYRO_RAW_MIN;
3177 static const int32_t GYRO_RAW_MAX;
3178 static const int32_t GYRO_RAW_FUZZ;
3179 static const int32_t GYRO_RAW_FLAT;
3180 static const int32_t GYRO_RAW_RESOLUTION;
3181
3182 static const float GRAVITY_MS2_UNIT;
3183 static const float DEGREE_RADIAN_UNIT;
3184
3185 void prepareAccelAxes();
3186 void prepareGyroAxes();
3187 void setAccelProperties();
3188 void setGyroProperties();
3189 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::SENSOR); }
3190};
3191
3192const int32_t SensorInputMapperTest::ACCEL_RAW_MIN = -32768;
3193const int32_t SensorInputMapperTest::ACCEL_RAW_MAX = 32768;
3194const int32_t SensorInputMapperTest::ACCEL_RAW_FUZZ = 16;
3195const int32_t SensorInputMapperTest::ACCEL_RAW_FLAT = 0;
3196const int32_t SensorInputMapperTest::ACCEL_RAW_RESOLUTION = 8192;
3197
3198const int32_t SensorInputMapperTest::GYRO_RAW_MIN = -2097152;
3199const int32_t SensorInputMapperTest::GYRO_RAW_MAX = 2097152;
3200const int32_t SensorInputMapperTest::GYRO_RAW_FUZZ = 16;
3201const int32_t SensorInputMapperTest::GYRO_RAW_FLAT = 0;
3202const int32_t SensorInputMapperTest::GYRO_RAW_RESOLUTION = 1024;
3203
3204const float SensorInputMapperTest::GRAVITY_MS2_UNIT = 9.80665f;
3205const float SensorInputMapperTest::DEGREE_RADIAN_UNIT = 0.0174533f;
3206
3207void SensorInputMapperTest::prepareAccelAxes() {
3208 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3209 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3210 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3211 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3212 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Z, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3213 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3214}
3215
3216void SensorInputMapperTest::prepareGyroAxes() {
3217 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RX, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3218 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3219 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RY, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3220 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3221 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RZ, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3222 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3223}
3224
3225void SensorInputMapperTest::setAccelProperties() {
3226 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 0, InputDeviceSensorType::ACCELEROMETER,
3227 /* sensorDataIndex */ 0);
3228 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 1, InputDeviceSensorType::ACCELEROMETER,
3229 /* sensorDataIndex */ 1);
3230 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 2, InputDeviceSensorType::ACCELEROMETER,
3231 /* sensorDataIndex */ 2);
3232 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
3233 addConfigurationProperty("sensor.accelerometer.reportingMode", "0");
3234 addConfigurationProperty("sensor.accelerometer.maxDelay", "100000");
3235 addConfigurationProperty("sensor.accelerometer.minDelay", "5000");
3236 addConfigurationProperty("sensor.accelerometer.power", "1.5");
3237}
3238
3239void SensorInputMapperTest::setGyroProperties() {
3240 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 3, InputDeviceSensorType::GYROSCOPE,
3241 /* sensorDataIndex */ 0);
3242 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 4, InputDeviceSensorType::GYROSCOPE,
3243 /* sensorDataIndex */ 1);
3244 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 5, InputDeviceSensorType::GYROSCOPE,
3245 /* sensorDataIndex */ 2);
3246 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
3247 addConfigurationProperty("sensor.gyroscope.reportingMode", "0");
3248 addConfigurationProperty("sensor.gyroscope.maxDelay", "100000");
3249 addConfigurationProperty("sensor.gyroscope.minDelay", "5000");
3250 addConfigurationProperty("sensor.gyroscope.power", "0.8");
3251}
3252
3253TEST_F(SensorInputMapperTest, GetSources) {
3254 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3255
3256 ASSERT_EQ(static_cast<uint32_t>(AINPUT_SOURCE_SENSOR), mapper.getSources());
3257}
3258
3259TEST_F(SensorInputMapperTest, ProcessAccelerometerSensor) {
3260 setAccelProperties();
3261 prepareAccelAxes();
3262 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3263
3264 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::ACCELEROMETER,
3265 std::chrono::microseconds(10000),
3266 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08003267 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003268 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, 20000);
3269 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, -20000);
3270 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Z, 40000);
3271 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
3272 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07003273
3274 NotifySensorArgs args;
3275 std::vector<float> values = {20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
3276 -20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
3277 40000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT};
3278
3279 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
3280 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
3281 ASSERT_EQ(args.deviceId, DEVICE_ID);
3282 ASSERT_EQ(args.sensorType, InputDeviceSensorType::ACCELEROMETER);
3283 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
3284 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
3285 ASSERT_EQ(args.values, values);
3286 mapper.flushSensor(InputDeviceSensorType::ACCELEROMETER);
3287}
3288
3289TEST_F(SensorInputMapperTest, ProcessGyroscopeSensor) {
3290 setGyroProperties();
3291 prepareGyroAxes();
3292 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3293
3294 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::GYROSCOPE,
3295 std::chrono::microseconds(10000),
3296 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08003297 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003298 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RX, 20000);
3299 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RY, -20000);
3300 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RZ, 40000);
3301 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
3302 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07003303
3304 NotifySensorArgs args;
3305 std::vector<float> values = {20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
3306 -20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
3307 40000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT};
3308
3309 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
3310 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
3311 ASSERT_EQ(args.deviceId, DEVICE_ID);
3312 ASSERT_EQ(args.sensorType, InputDeviceSensorType::GYROSCOPE);
3313 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
3314 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
3315 ASSERT_EQ(args.values, values);
3316 mapper.flushSensor(InputDeviceSensorType::GYROSCOPE);
3317}
3318
Michael Wrightd02c5b62014-02-10 15:10:22 -08003319// --- KeyboardInputMapperTest ---
3320
3321class KeyboardInputMapperTest : public InputMapperTest {
3322protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003323 const std::string UNIQUE_ID = "local:0";
3324
3325 void prepareDisplay(int32_t orientation);
3326
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003327 void testDPadKeyRotation(KeyboardInputMapper& mapper, int32_t originalScanCode,
Arthur Hung2c9a3342019-07-23 14:18:59 +08003328 int32_t originalKeyCode, int32_t rotatedKeyCode,
3329 int32_t displayId = ADISPLAY_ID_NONE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003330};
3331
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003332/* Similar to setDisplayInfoAndReconfigure, but pre-populates all parameters except for the
3333 * orientation.
3334 */
3335void KeyboardInputMapperTest::prepareDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003336 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
3337 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003338}
3339
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003340void KeyboardInputMapperTest::testDPadKeyRotation(KeyboardInputMapper& mapper,
Arthur Hung2c9a3342019-07-23 14:18:59 +08003341 int32_t originalScanCode, int32_t originalKeyCode,
3342 int32_t rotatedKeyCode, int32_t displayId) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003343 NotifyKeyArgs args;
3344
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003345 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003346 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3347 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3348 ASSERT_EQ(originalScanCode, args.scanCode);
3349 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003350 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003351
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003352 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003353 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3354 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3355 ASSERT_EQ(originalScanCode, args.scanCode);
3356 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003357 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003358}
3359
Michael Wrightd02c5b62014-02-10 15:10:22 -08003360TEST_F(KeyboardInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003361 KeyboardInputMapper& mapper =
3362 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3363 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003364
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003365 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003366}
3367
3368TEST_F(KeyboardInputMapperTest, Process_SimpleKeyPress) {
3369 const int32_t USAGE_A = 0x070004;
3370 const int32_t USAGE_UNKNOWN = 0x07ffff;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003371 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3372 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
Chris Yea52ade12020-08-27 16:49:20 -07003373 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, POLICY_FLAG_WAKE);
3374 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, POLICY_FLAG_WAKE);
3375 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003376
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003377 KeyboardInputMapper& mapper =
3378 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3379 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003380 // Initial metastate is AMETA_NONE.
3381 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003382
3383 // Key down by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003384 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003385 NotifyKeyArgs args;
3386 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3387 ASSERT_EQ(DEVICE_ID, args.deviceId);
3388 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3389 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3390 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3391 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3392 ASSERT_EQ(KEY_HOME, args.scanCode);
3393 ASSERT_EQ(AMETA_NONE, args.metaState);
3394 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3395 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3396 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3397
3398 // Key up by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003399 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003400 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3401 ASSERT_EQ(DEVICE_ID, args.deviceId);
3402 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3403 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3404 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3405 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3406 ASSERT_EQ(KEY_HOME, args.scanCode);
3407 ASSERT_EQ(AMETA_NONE, args.metaState);
3408 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3409 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3410 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3411
3412 // Key down by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003413 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3414 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, 0, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003415 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3416 ASSERT_EQ(DEVICE_ID, args.deviceId);
3417 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3418 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3419 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3420 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3421 ASSERT_EQ(0, args.scanCode);
3422 ASSERT_EQ(AMETA_NONE, args.metaState);
3423 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3424 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3425 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3426
3427 // Key up by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003428 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3429 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003430 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3431 ASSERT_EQ(DEVICE_ID, args.deviceId);
3432 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3433 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3434 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3435 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3436 ASSERT_EQ(0, args.scanCode);
3437 ASSERT_EQ(AMETA_NONE, args.metaState);
3438 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3439 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3440 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3441
3442 // Key down with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003443 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3444 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UNKNOWN, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003445 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3446 ASSERT_EQ(DEVICE_ID, args.deviceId);
3447 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3448 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3449 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3450 ASSERT_EQ(0, args.keyCode);
3451 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3452 ASSERT_EQ(AMETA_NONE, args.metaState);
3453 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3454 ASSERT_EQ(0U, args.policyFlags);
3455 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3456
3457 // Key up with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003458 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3459 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003460 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3461 ASSERT_EQ(DEVICE_ID, args.deviceId);
3462 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3463 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3464 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3465 ASSERT_EQ(0, args.keyCode);
3466 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3467 ASSERT_EQ(AMETA_NONE, args.metaState);
3468 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3469 ASSERT_EQ(0U, args.policyFlags);
3470 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3471}
3472
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003473/**
3474 * Ensure that the readTime is set to the time when the EV_KEY is received.
3475 */
3476TEST_F(KeyboardInputMapperTest, Process_SendsReadTime) {
3477 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3478
3479 KeyboardInputMapper& mapper =
3480 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3481 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3482 NotifyKeyArgs args;
3483
3484 // Key down
3485 process(mapper, ARBITRARY_TIME, 12 /*readTime*/, EV_KEY, KEY_HOME, 1);
3486 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3487 ASSERT_EQ(12, args.readTime);
3488
3489 // Key up
3490 process(mapper, ARBITRARY_TIME, 15 /*readTime*/, EV_KEY, KEY_HOME, 1);
3491 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3492 ASSERT_EQ(15, args.readTime);
3493}
3494
Michael Wrightd02c5b62014-02-10 15:10:22 -08003495TEST_F(KeyboardInputMapperTest, Process_ShouldUpdateMetaState) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003496 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFTSHIFT, 0, AKEYCODE_SHIFT_LEFT, 0);
3497 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003498 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, 0);
3499 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, 0);
3500 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003501
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003502 KeyboardInputMapper& mapper =
3503 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3504 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003505
Arthur Hung95f68612022-04-07 14:08:22 +08003506 // Initial metastate is AMETA_NONE.
3507 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003508
3509 // Metakey down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003510 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003511 NotifyKeyArgs args;
3512 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3513 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003514 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003515 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003516
3517 // Key down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003518 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_A, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003519 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3520 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003521 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003522
3523 // Key up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003524 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, KEY_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003525 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3526 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003527 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003528
3529 // Metakey up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003530 process(mapper, ARBITRARY_TIME + 3, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003531 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3532 ASSERT_EQ(AMETA_NONE, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003533 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003534 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003535}
3536
3537TEST_F(KeyboardInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003538 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3539 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3540 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3541 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003542
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003543 KeyboardInputMapper& mapper =
3544 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3545 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003546
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003547 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003548 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3549 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
3550 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3551 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
3552 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3553 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
3554 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3555 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
3556}
3557
3558TEST_F(KeyboardInputMapperTest, Process_WhenOrientationAware_ShouldRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003559 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3560 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3561 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3562 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003563
Michael Wrightd02c5b62014-02-10 15:10:22 -08003564 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003565 KeyboardInputMapper& mapper =
3566 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3567 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003568
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003569 prepareDisplay(DISPLAY_ORIENTATION_0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003570 ASSERT_NO_FATAL_FAILURE(
3571 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3572 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3573 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3574 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3575 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3576 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3577 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003578
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003579 clearViewports();
3580 prepareDisplay(DISPLAY_ORIENTATION_90);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003581 ASSERT_NO_FATAL_FAILURE(
3582 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3583 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3584 AKEYCODE_DPAD_UP, DISPLAY_ID));
3585 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3586 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3587 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3588 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003589
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003590 clearViewports();
3591 prepareDisplay(DISPLAY_ORIENTATION_180);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003592 ASSERT_NO_FATAL_FAILURE(
3593 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3594 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3595 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3596 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3597 AKEYCODE_DPAD_UP, DISPLAY_ID));
3598 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3599 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003600
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003601 clearViewports();
3602 prepareDisplay(DISPLAY_ORIENTATION_270);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003603 ASSERT_NO_FATAL_FAILURE(
3604 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3605 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3606 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3607 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3608 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3609 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3610 AKEYCODE_DPAD_UP, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003611
3612 // Special case: if orientation changes while key is down, we still emit the same keycode
3613 // in the key up as we did in the key down.
3614 NotifyKeyArgs args;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003615 clearViewports();
3616 prepareDisplay(DISPLAY_ORIENTATION_270);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003617 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003618 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3619 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3620 ASSERT_EQ(KEY_UP, args.scanCode);
3621 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3622
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003623 clearViewports();
3624 prepareDisplay(DISPLAY_ORIENTATION_180);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003625 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003626 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3627 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3628 ASSERT_EQ(KEY_UP, args.scanCode);
3629 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3630}
3631
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003632TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_NotOrientationAware) {
3633 // If the keyboard is not orientation aware,
3634 // key events should not be associated with a specific display id
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003635 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003636
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003637 KeyboardInputMapper& mapper =
3638 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3639 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003640 NotifyKeyArgs args;
3641
3642 // Display id should be ADISPLAY_ID_NONE without any display configuration.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003643 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003644 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003645 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003646 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3647 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3648
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003649 prepareDisplay(DISPLAY_ORIENTATION_0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003650 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003651 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003652 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003653 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3654 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3655}
3656
3657TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_OrientationAware) {
3658 // If the keyboard is orientation aware,
3659 // key events should be associated with the internal viewport
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003660 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003661
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003662 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003663 KeyboardInputMapper& mapper =
3664 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3665 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003666 NotifyKeyArgs args;
3667
3668 // Display id should be ADISPLAY_ID_NONE without any display configuration.
3669 // ^--- already checked by the previous test
3670
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003671 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003672 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003673 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003674 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003675 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003676 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3677 ASSERT_EQ(DISPLAY_ID, args.displayId);
3678
3679 constexpr int32_t newDisplayId = 2;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003680 clearViewports();
3681 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003682 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003683 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003684 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003685 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003686 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3687 ASSERT_EQ(newDisplayId, args.displayId);
3688}
3689
Michael Wrightd02c5b62014-02-10 15:10:22 -08003690TEST_F(KeyboardInputMapperTest, GetKeyCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003691 KeyboardInputMapper& mapper =
3692 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3693 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003694
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003695 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003696 ASSERT_EQ(1, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003697
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003698 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003699 ASSERT_EQ(0, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003700}
3701
Philip Junker4af3b3d2021-12-14 10:36:55 +01003702TEST_F(KeyboardInputMapperTest, GetKeyCodeForKeyLocation) {
3703 KeyboardInputMapper& mapper =
3704 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3705 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3706
3707 mFakeEventHub->addKeyCodeMapping(EVENTHUB_ID, AKEYCODE_Y, AKEYCODE_Z);
3708 ASSERT_EQ(AKEYCODE_Z, mapper.getKeyCodeForKeyLocation(AKEYCODE_Y))
3709 << "If a mapping is available, the result is equal to the mapping";
3710
3711 ASSERT_EQ(AKEYCODE_A, mapper.getKeyCodeForKeyLocation(AKEYCODE_A))
3712 << "If no mapping is available, the result is the key location";
3713}
3714
Michael Wrightd02c5b62014-02-10 15:10:22 -08003715TEST_F(KeyboardInputMapperTest, GetScanCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003716 KeyboardInputMapper& mapper =
3717 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3718 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003719
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003720 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003721 ASSERT_EQ(1, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003722
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003723 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003724 ASSERT_EQ(0, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003725}
3726
3727TEST_F(KeyboardInputMapperTest, MarkSupportedKeyCodes) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003728 KeyboardInputMapper& mapper =
3729 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3730 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003731
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003732 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003733
3734 const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
3735 uint8_t flags[2] = { 0, 0 };
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003736 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, 1, keyCodes, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003737 ASSERT_TRUE(flags[0]);
3738 ASSERT_FALSE(flags[1]);
3739}
3740
3741TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleMetaStateAndLeds) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003742 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3743 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3744 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3745 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3746 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3747 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003748
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003749 KeyboardInputMapper& mapper =
3750 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3751 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003752 // Initial metastate is AMETA_NONE.
3753 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003754
3755 // Initialization should have turned all of the lights off.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003756 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3757 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3758 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003759
3760 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003761 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3762 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003763 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3764 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3765 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003766 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003767
3768 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003769 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3770 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003771 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3772 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3773 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003774 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003775
3776 // Toggle caps lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003777 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3778 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003779 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3780 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3781 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003782 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003783
3784 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003785 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3786 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003787 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3788 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3789 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003790 ASSERT_EQ(AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003791
3792 // Toggle num lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003793 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3794 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003795 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3796 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3797 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003798 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003799
3800 // Toggle scroll lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003801 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3802 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003803 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3804 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3805 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003806 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003807}
3808
Chris Yea52ade12020-08-27 16:49:20 -07003809TEST_F(KeyboardInputMapperTest, NoMetaStateWhenMetaKeysNotPresent) {
3810 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_BUTTON_A, 0);
3811 mFakeEventHub->addKey(EVENTHUB_ID, BTN_B, 0, AKEYCODE_BUTTON_B, 0);
3812 mFakeEventHub->addKey(EVENTHUB_ID, BTN_X, 0, AKEYCODE_BUTTON_X, 0);
3813 mFakeEventHub->addKey(EVENTHUB_ID, BTN_Y, 0, AKEYCODE_BUTTON_Y, 0);
3814
3815 KeyboardInputMapper& mapper =
3816 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3817 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
3818
Chris Yea52ade12020-08-27 16:49:20 -07003819 // Meta state should be AMETA_NONE after reset
3820 mapper.reset(ARBITRARY_TIME);
3821 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3822 // Meta state should be AMETA_NONE with update, as device doesn't have the keys.
3823 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
3824 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3825
3826 NotifyKeyArgs args;
3827 // Press button "A"
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003828 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_A, 1);
Chris Yea52ade12020-08-27 16:49:20 -07003829 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3830 ASSERT_EQ(AMETA_NONE, args.metaState);
3831 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3832 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3833 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3834
3835 // Button up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003836 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003837 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3838 ASSERT_EQ(AMETA_NONE, args.metaState);
3839 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3840 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3841 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3842}
3843
Arthur Hung2c9a3342019-07-23 14:18:59 +08003844TEST_F(KeyboardInputMapperTest, Configure_AssignsDisplayPort) {
3845 // keyboard 1.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003846 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3847 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3848 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3849 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003850
3851 // keyboard 2.
3852 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08003853 const std::string DEVICE_NAME2 = "KEYBOARD2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08003854 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003855 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08003856 std::shared_ptr<InputDevice> device2 =
3857 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003858 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08003859
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003860 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3861 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3862 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3863 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003864
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003865 KeyboardInputMapper& mapper =
3866 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3867 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003868
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003869 KeyboardInputMapper& mapper2 =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003870 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003871 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003872 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
3873 device2->reset(ARBITRARY_TIME);
3874
3875 // Prepared displays and associated info.
3876 constexpr uint8_t hdmi1 = 0;
3877 constexpr uint8_t hdmi2 = 1;
3878 const std::string SECONDARY_UNIQUE_ID = "local:1";
3879
3880 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
3881 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
3882
3883 // No associated display viewport found, should disable the device.
3884 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3885 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3886 ASSERT_FALSE(device2->isEnabled());
3887
3888 // Prepare second display.
3889 constexpr int32_t newDisplayId = 2;
3890 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003891 UNIQUE_ID, hdmi1, ViewportType::INTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003892 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003893 SECONDARY_UNIQUE_ID, hdmi2, ViewportType::EXTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003894 // Default device will reconfigure above, need additional reconfiguration for another device.
3895 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3896 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3897
3898 // Device should be enabled after the associated display is found.
3899 ASSERT_TRUE(mDevice->isEnabled());
3900 ASSERT_TRUE(device2->isEnabled());
3901
3902 // Test pad key events
3903 ASSERT_NO_FATAL_FAILURE(
3904 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3905 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3906 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3907 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3908 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3909 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3910 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3911
3912 ASSERT_NO_FATAL_FAILURE(
3913 testDPadKeyRotation(mapper2, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, newDisplayId));
3914 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3915 AKEYCODE_DPAD_RIGHT, newDisplayId));
3916 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3917 AKEYCODE_DPAD_DOWN, newDisplayId));
3918 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3919 AKEYCODE_DPAD_LEFT, newDisplayId));
3920}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003921
arthurhungc903df12020-08-11 15:08:42 +08003922TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleAfterReattach) {
3923 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3924 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3925 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3926 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3927 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3928 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3929
3930 KeyboardInputMapper& mapper =
3931 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3932 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003933 // Initial metastate is AMETA_NONE.
3934 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08003935
3936 // Initialization should have turned all of the lights off.
3937 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3938 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3939 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3940
3941 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003942 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3943 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003944 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3945 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
3946
3947 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003948 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3949 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003950 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3951 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
3952
3953 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003954 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3955 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003956 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3957 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
3958
3959 mFakeEventHub->removeDevice(EVENTHUB_ID);
3960 mReader->loopOnce();
3961
3962 // keyboard 2 should default toggle keys.
3963 const std::string USB2 = "USB2";
3964 const std::string DEVICE_NAME2 = "KEYBOARD2";
3965 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
3966 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
3967 std::shared_ptr<InputDevice> device2 =
3968 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003969 ftl::Flags<InputDeviceClass>(0));
arthurhungc903df12020-08-11 15:08:42 +08003970 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3971 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
3972 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3973 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3974 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3975 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3976
arthurhung6fe95782020-10-05 22:41:16 +08003977 KeyboardInputMapper& mapper2 =
3978 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
3979 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
arthurhungc903df12020-08-11 15:08:42 +08003980 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
3981 device2->reset(ARBITRARY_TIME);
3982
3983 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_CAPSL));
3984 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_NUML));
3985 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_SCROLLL));
arthurhung6fe95782020-10-05 22:41:16 +08003986 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON,
3987 mapper2.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08003988}
3989
Arthur Hungcb40a002021-08-03 14:31:01 +00003990TEST_F(KeyboardInputMapperTest, Process_toggleCapsLockState) {
3991 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3992 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3993 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3994
3995 // Suppose we have two mappers. (DPAD + KEYBOARD)
3996 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_DPAD,
3997 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
3998 KeyboardInputMapper& mapper =
3999 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4000 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08004001 // Initial metastate is AMETA_NONE.
4002 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Arthur Hungcb40a002021-08-03 14:31:01 +00004003
4004 mReader->toggleCapsLockState(DEVICE_ID);
4005 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
4006}
4007
Arthur Hungfb3cc112022-04-13 07:39:50 +00004008TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleInMultiDevices) {
4009 // keyboard 1.
4010 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
4011 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
4012 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
4013 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4014 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4015 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
4016
4017 KeyboardInputMapper& mapper1 =
4018 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4019 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
4020
4021 // keyboard 2.
4022 const std::string USB2 = "USB2";
4023 const std::string DEVICE_NAME2 = "KEYBOARD2";
4024 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
4025 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
4026 std::shared_ptr<InputDevice> device2 =
4027 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
4028 ftl::Flags<InputDeviceClass>(0));
4029 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
4030 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
4031 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
4032 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4033 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4034 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
4035
4036 KeyboardInputMapper& mapper2 =
4037 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
4038 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
4039 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
4040 device2->reset(ARBITRARY_TIME);
4041
Arthur Hung95f68612022-04-07 14:08:22 +08004042 // Initial metastate is AMETA_NONE.
4043 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4044 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4045
4046 // Toggle num lock on and off.
4047 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
4048 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Arthur Hungfb3cc112022-04-13 07:39:50 +00004049 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4050 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper1.getMetaState());
4051 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper2.getMetaState());
4052
4053 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
4054 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
4055 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4056 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4057 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4058
4059 // Toggle caps lock on and off.
4060 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
4061 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
4062 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4063 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper1.getMetaState());
4064 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper2.getMetaState());
4065
4066 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
4067 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
4068 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4069 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4070 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4071
4072 // Toggle scroll lock on and off.
4073 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
4074 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
4075 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
4076 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper1.getMetaState());
4077 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper2.getMetaState());
4078
4079 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
4080 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
4081 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
4082 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4083 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4084}
4085
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004086// --- KeyboardInputMapperTest_ExternalDevice ---
4087
4088class KeyboardInputMapperTest_ExternalDevice : public InputMapperTest {
4089protected:
Chris Yea52ade12020-08-27 16:49:20 -07004090 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004091};
4092
4093TEST_F(KeyboardInputMapperTest_ExternalDevice, WakeBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07004094 // For external devices, non-media keys will trigger wake on key down. Media keys need to be
4095 // marked as WAKE in the keylayout file to trigger wake.
Powei Fengd041c5d2019-05-03 17:11:33 -07004096
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004097 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, 0);
4098 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, 0);
4099 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAYPAUSE, 0, AKEYCODE_MEDIA_PLAY_PAUSE,
4100 POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07004101
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004102 KeyboardInputMapper& mapper =
4103 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4104 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07004105
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004106 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004107 NotifyKeyArgs args;
4108 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4109 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4110
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004111 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004112 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4113 ASSERT_EQ(uint32_t(0), args.policyFlags);
4114
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004115 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004116 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4117 ASSERT_EQ(uint32_t(0), args.policyFlags);
4118
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004119 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004120 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4121 ASSERT_EQ(uint32_t(0), args.policyFlags);
4122
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004123 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004124 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4125 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4126
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004127 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004128 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4129 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4130}
4131
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004132TEST_F(KeyboardInputMapperTest_ExternalDevice, DoNotWakeByDefaultBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07004133 // Tv Remote key's wake behavior is prescribed by the keylayout file.
Powei Fengd041c5d2019-05-03 17:11:33 -07004134
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004135 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4136 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
4137 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07004138
Powei Fengd041c5d2019-05-03 17:11:33 -07004139 addConfigurationProperty("keyboard.doNotWakeByDefault", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004140 KeyboardInputMapper& mapper =
4141 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4142 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07004143
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004144 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004145 NotifyKeyArgs args;
4146 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4147 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4148
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004149 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004150 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4151 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4152
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004153 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_DOWN, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004154 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4155 ASSERT_EQ(uint32_t(0), args.policyFlags);
4156
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004157 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_DOWN, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004158 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4159 ASSERT_EQ(uint32_t(0), args.policyFlags);
4160
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004161 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004162 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4163 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4164
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004165 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004166 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4167 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4168}
4169
Michael Wrightd02c5b62014-02-10 15:10:22 -08004170// --- CursorInputMapperTest ---
4171
4172class CursorInputMapperTest : public InputMapperTest {
4173protected:
4174 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD;
4175
Michael Wright17db18e2020-06-26 20:51:44 +01004176 std::shared_ptr<FakePointerController> mFakePointerController;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004177
Chris Yea52ade12020-08-27 16:49:20 -07004178 void SetUp() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004179 InputMapperTest::SetUp();
4180
Michael Wright17db18e2020-06-26 20:51:44 +01004181 mFakePointerController = std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00004182 mFakePolicy->setPointerController(mFakePointerController);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004183 }
4184
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004185 void testMotionRotation(CursorInputMapper& mapper, int32_t originalX, int32_t originalY,
4186 int32_t rotatedX, int32_t rotatedY);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004187
4188 void prepareDisplay(int32_t orientation) {
4189 const std::string uniqueId = "local:0";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004190 const ViewportType viewportType = ViewportType::INTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004191 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
4192 orientation, uniqueId, NO_PORT, viewportType);
4193 }
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004194
4195 static void assertCursorPointerCoords(const PointerCoords& coords, float x, float y,
4196 float pressure) {
4197 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(coords, x, y, pressure, 0.0f, 0.0f, 0.0f, 0.0f,
4198 0.0f, 0.0f, 0.0f, EPSILON));
4199 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08004200};
4201
4202const int32_t CursorInputMapperTest::TRACKBALL_MOVEMENT_THRESHOLD = 6;
4203
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004204void CursorInputMapperTest::testMotionRotation(CursorInputMapper& mapper, int32_t originalX,
4205 int32_t originalY, int32_t rotatedX,
4206 int32_t rotatedY) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004207 NotifyMotionArgs args;
4208
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004209 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, originalX);
4210 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, originalY);
4211 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004212 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4213 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004214 ASSERT_NO_FATAL_FAILURE(
4215 assertCursorPointerCoords(args.pointerCoords[0],
4216 float(rotatedX) / TRACKBALL_MOVEMENT_THRESHOLD,
4217 float(rotatedY) / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004218}
4219
4220TEST_F(CursorInputMapperTest, WhenModeIsPointer_GetSources_ReturnsMouse) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004221 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004222 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004223
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004224 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004225}
4226
4227TEST_F(CursorInputMapperTest, WhenModeIsNavigation_GetSources_ReturnsTrackball) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004228 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004229 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004230
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004231 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004232}
4233
4234TEST_F(CursorInputMapperTest, WhenModeIsPointer_PopulateDeviceInfo_ReturnsRangeFromPointerController) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004235 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004236 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004237
4238 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004239 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004240
4241 // Initially there may not be a valid motion range.
Yi Kong9b14ac62018-07-17 13:48:38 -07004242 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE));
4243 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004244 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4245 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE, 0.0f, 1.0f, 0.0f, 0.0f));
4246
4247 // When the bounds are set, then there should be a valid motion range.
4248 mFakePointerController->setBounds(1, 2, 800 - 1, 480 - 1);
4249
4250 InputDeviceInfo info2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004251 mapper.populateDeviceInfo(&info2);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004252
4253 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
4254 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE,
4255 1, 800 - 1, 0.0f, 0.0f));
4256 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
4257 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE,
4258 2, 480 - 1, 0.0f, 0.0f));
4259 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
4260 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE,
4261 0.0f, 1.0f, 0.0f, 0.0f));
4262}
4263
4264TEST_F(CursorInputMapperTest, WhenModeIsNavigation_PopulateDeviceInfo_ReturnsScaledRange) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004265 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004266 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004267
4268 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004269 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004270
4271 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4272 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_TRACKBALL,
4273 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
4274 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4275 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_TRACKBALL,
4276 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
4277 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4278 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TRACKBALL,
4279 0.0f, 1.0f, 0.0f, 0.0f));
4280}
4281
4282TEST_F(CursorInputMapperTest, Process_ShouldSetAllFieldsAndIncludeGlobalMetaState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004283 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004284 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004285
arthurhungdcef2dc2020-08-11 14:47:50 +08004286 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004287
4288 NotifyMotionArgs args;
4289
4290 // Button press.
4291 // Mostly testing non x/y behavior here so we don't need to check again elsewhere.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004292 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4293 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004294 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4295 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4296 ASSERT_EQ(DEVICE_ID, args.deviceId);
4297 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4298 ASSERT_EQ(uint32_t(0), args.policyFlags);
4299 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
4300 ASSERT_EQ(0, args.flags);
4301 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4302 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
4303 ASSERT_EQ(0, args.edgeFlags);
4304 ASSERT_EQ(uint32_t(1), args.pointerCount);
4305 ASSERT_EQ(0, args.pointerProperties[0].id);
4306 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004307 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004308 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4309 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4310 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4311
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004312 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4313 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4314 ASSERT_EQ(DEVICE_ID, args.deviceId);
4315 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4316 ASSERT_EQ(uint32_t(0), args.policyFlags);
4317 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
4318 ASSERT_EQ(0, args.flags);
4319 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4320 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
4321 ASSERT_EQ(0, args.edgeFlags);
4322 ASSERT_EQ(uint32_t(1), args.pointerCount);
4323 ASSERT_EQ(0, args.pointerProperties[0].id);
4324 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004325 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004326 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4327 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4328 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4329
Michael Wrightd02c5b62014-02-10 15:10:22 -08004330 // Button release. Should have same down time.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004331 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4332 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004333 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4334 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
4335 ASSERT_EQ(DEVICE_ID, args.deviceId);
4336 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4337 ASSERT_EQ(uint32_t(0), args.policyFlags);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004338 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
4339 ASSERT_EQ(0, args.flags);
4340 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4341 ASSERT_EQ(0, args.buttonState);
4342 ASSERT_EQ(0, args.edgeFlags);
4343 ASSERT_EQ(uint32_t(1), args.pointerCount);
4344 ASSERT_EQ(0, args.pointerProperties[0].id);
4345 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004346 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004347 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4348 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4349 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4350
4351 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4352 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
4353 ASSERT_EQ(DEVICE_ID, args.deviceId);
4354 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4355 ASSERT_EQ(uint32_t(0), args.policyFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004356 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
4357 ASSERT_EQ(0, args.flags);
4358 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4359 ASSERT_EQ(0, args.buttonState);
4360 ASSERT_EQ(0, args.edgeFlags);
4361 ASSERT_EQ(uint32_t(1), args.pointerCount);
4362 ASSERT_EQ(0, args.pointerProperties[0].id);
4363 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004364 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004365 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4366 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4367 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4368}
4369
4370TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentXYUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004371 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004372 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004373
4374 NotifyMotionArgs args;
4375
4376 // Motion in X but not Y.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004377 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4378 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004379 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4380 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004381 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4382 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f,
4383 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004384
4385 // Motion in Y but not X.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004386 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
4387 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004388 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4389 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004390 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f,
4391 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004392}
4393
4394TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004395 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004396 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004397
4398 NotifyMotionArgs args;
4399
4400 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004401 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4402 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004403 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4404 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004405 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004406
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004407 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4408 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004409 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004410
Michael Wrightd02c5b62014-02-10 15:10:22 -08004411 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004412 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4413 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004414 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004415 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004416 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004417
4418 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004419 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004420 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004421}
4422
4423TEST_F(CursorInputMapperTest, Process_ShouldHandleCombinedXYAndButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004424 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004425 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004426
4427 NotifyMotionArgs args;
4428
4429 // Combined X, Y and Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004430 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4431 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
4432 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4433 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004434 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4435 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004436 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4437 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4438 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004439
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004440 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4441 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004442 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4443 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4444 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004445
Michael Wrightd02c5b62014-02-10 15:10:22 -08004446 // Move X, Y a bit while pressed.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004447 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 2);
4448 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 1);
4449 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004450 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4451 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004452 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4453 2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4454 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004455
4456 // Release Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004457 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4458 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004459 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004460 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004461 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004462
4463 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004464 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004465 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004466}
4467
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004468TEST_F(CursorInputMapperTest, Process_WhenOrientationAware_ShouldNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004469 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004470 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
4471 // need to be rotated.
4472 addConfigurationProperty("cursor.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004473 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004474
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004475 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004476 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4477 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4478 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4479 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4480 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4481 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4482 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4483 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4484}
4485
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004486TEST_F(CursorInputMapperTest, Process_WhenNotOrientationAware_ShouldRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004487 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004488 // Since InputReader works in the un-rotated coordinate space, only devices that are not
4489 // orientation-aware are affected by display rotation.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004490 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004491
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004492 prepareDisplay(DISPLAY_ORIENTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004493 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4494 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4495 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4496 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4497 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4498 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4499 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4500 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4501
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004502 prepareDisplay(DISPLAY_ORIENTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004503 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, -1, 0));
4504 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, 1));
4505 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, 1));
4506 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, 1));
4507 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 1, 0));
4508 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, -1));
4509 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, -1));
4510 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, -1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004511
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004512 prepareDisplay(DISPLAY_ORIENTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004513 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, -1));
4514 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, -1));
4515 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, -1, 0));
4516 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, 1));
4517 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, 1));
4518 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, 1));
4519 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 1, 0));
4520 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, -1));
4521
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004522 prepareDisplay(DISPLAY_ORIENTATION_270);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004523 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 1, 0));
4524 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, -1));
4525 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, -1));
4526 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, -1));
4527 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, -1, 0));
4528 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, 1));
4529 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, 1));
4530 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, 1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004531}
4532
4533TEST_F(CursorInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004534 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004535 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004536
4537 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4538 mFakePointerController->setPosition(100, 200);
4539 mFakePointerController->setButtonState(0);
4540
4541 NotifyMotionArgs motionArgs;
4542 NotifyKeyArgs keyArgs;
4543
4544 // press BTN_LEFT, release BTN_LEFT
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004545 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 1);
4546 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004547 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4548 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4549 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4550 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004551 ASSERT_NO_FATAL_FAILURE(
4552 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004553
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004554 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4555 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4556 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4557 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004558 ASSERT_NO_FATAL_FAILURE(
4559 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004560
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004561 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 0);
4562 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004563 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004564 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004565 ASSERT_EQ(0, motionArgs.buttonState);
4566 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004567 ASSERT_NO_FATAL_FAILURE(
4568 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004569
4570 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004571 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004572 ASSERT_EQ(0, motionArgs.buttonState);
4573 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004574 ASSERT_NO_FATAL_FAILURE(
4575 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004576
4577 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004578 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004579 ASSERT_EQ(0, motionArgs.buttonState);
4580 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004581 ASSERT_NO_FATAL_FAILURE(
4582 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004583
4584 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004585 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 1);
4586 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 1);
4587 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004588 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4589 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4590 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4591 motionArgs.buttonState);
4592 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4593 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004594 ASSERT_NO_FATAL_FAILURE(
4595 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004596
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004597 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4598 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4599 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4600 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4601 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004602 ASSERT_NO_FATAL_FAILURE(
4603 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004604
4605 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4606 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4607 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4608 motionArgs.buttonState);
4609 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4610 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004611 ASSERT_NO_FATAL_FAILURE(
4612 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004613
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004614 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 0);
4615 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004616 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004617 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004618 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4619 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004620 ASSERT_NO_FATAL_FAILURE(
4621 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004622
4623 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004624 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004625 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4626 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004627 ASSERT_NO_FATAL_FAILURE(
4628 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004629
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004630 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4631 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004632 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004633 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
4634 ASSERT_EQ(0, motionArgs.buttonState);
4635 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004636 ASSERT_NO_FATAL_FAILURE(
4637 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004638 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4639 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004640
4641 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004642 ASSERT_EQ(0, motionArgs.buttonState);
4643 ASSERT_EQ(0, mFakePointerController->getButtonState());
4644 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004645 ASSERT_NO_FATAL_FAILURE(
4646 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004647
Michael Wrightd02c5b62014-02-10 15:10:22 -08004648 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4649 ASSERT_EQ(0, motionArgs.buttonState);
4650 ASSERT_EQ(0, mFakePointerController->getButtonState());
4651 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004652 ASSERT_NO_FATAL_FAILURE(
4653 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004654
4655 // press BTN_BACK, release BTN_BACK
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004656 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 1);
4657 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004658 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4659 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4660 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004661
Michael Wrightd02c5b62014-02-10 15:10:22 -08004662 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004663 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004664 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4665 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004666 ASSERT_NO_FATAL_FAILURE(
4667 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004668
4669 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4670 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4671 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4672 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004673 ASSERT_NO_FATAL_FAILURE(
4674 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004675
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004676 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 0);
4677 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004678 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004679 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004680 ASSERT_EQ(0, motionArgs.buttonState);
4681 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004682 ASSERT_NO_FATAL_FAILURE(
4683 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004684
4685 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004686 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004687 ASSERT_EQ(0, motionArgs.buttonState);
4688 ASSERT_EQ(0, mFakePointerController->getButtonState());
4689
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004690 ASSERT_NO_FATAL_FAILURE(
4691 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004692 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4693 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4694 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4695
4696 // press BTN_SIDE, release BTN_SIDE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004697 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 1);
4698 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004699 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4700 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4701 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004702
Michael Wrightd02c5b62014-02-10 15:10:22 -08004703 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004704 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004705 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4706 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004707 ASSERT_NO_FATAL_FAILURE(
4708 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004709
4710 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4711 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4712 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4713 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004714 ASSERT_NO_FATAL_FAILURE(
4715 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004716
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004717 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 0);
4718 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004719 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004720 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004721 ASSERT_EQ(0, motionArgs.buttonState);
4722 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004723 ASSERT_NO_FATAL_FAILURE(
4724 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004725
4726 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4727 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4728 ASSERT_EQ(0, motionArgs.buttonState);
4729 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004730 ASSERT_NO_FATAL_FAILURE(
4731 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004732
Michael Wrightd02c5b62014-02-10 15:10:22 -08004733 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4734 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4735 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4736
4737 // press BTN_FORWARD, release BTN_FORWARD
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004738 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 1);
4739 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004740 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4741 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4742 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004743
Michael Wrightd02c5b62014-02-10 15:10:22 -08004744 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004745 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004746 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4747 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004748 ASSERT_NO_FATAL_FAILURE(
4749 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004750
4751 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4752 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4753 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4754 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004755 ASSERT_NO_FATAL_FAILURE(
4756 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004757
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004758 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 0);
4759 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004760 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004761 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004762 ASSERT_EQ(0, motionArgs.buttonState);
4763 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004764 ASSERT_NO_FATAL_FAILURE(
4765 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004766
4767 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4768 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4769 ASSERT_EQ(0, motionArgs.buttonState);
4770 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004771 ASSERT_NO_FATAL_FAILURE(
4772 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004773
Michael Wrightd02c5b62014-02-10 15:10:22 -08004774 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4775 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4776 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4777
4778 // press BTN_EXTRA, release BTN_EXTRA
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004779 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 1);
4780 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004781 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4782 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4783 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004784
Michael Wrightd02c5b62014-02-10 15:10:22 -08004785 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004786 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004787 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4788 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004789 ASSERT_NO_FATAL_FAILURE(
4790 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004791
4792 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4793 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4794 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4795 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004796 ASSERT_NO_FATAL_FAILURE(
4797 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004798
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004799 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 0);
4800 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004801 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004802 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004803 ASSERT_EQ(0, motionArgs.buttonState);
4804 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004805 ASSERT_NO_FATAL_FAILURE(
4806 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004807
4808 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4809 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4810 ASSERT_EQ(0, motionArgs.buttonState);
4811 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004812 ASSERT_NO_FATAL_FAILURE(
4813 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004814
Michael Wrightd02c5b62014-02-10 15:10:22 -08004815 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4816 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4817 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4818}
4819
4820TEST_F(CursorInputMapperTest, Process_WhenModeIsPointer_ShouldMoveThePointerAround) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004821 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004822 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004823
4824 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4825 mFakePointerController->setPosition(100, 200);
4826 mFakePointerController->setButtonState(0);
4827
4828 NotifyMotionArgs args;
4829
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004830 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4831 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4832 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004833 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004834 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4835 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4836 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4837 110.0f, 220.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
Michael Wright17db18e2020-06-26 20:51:44 +01004838 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004839}
4840
4841TEST_F(CursorInputMapperTest, Process_PointerCapture) {
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004842 addConfigurationProperty("cursor.mode", "pointer");
4843 mFakePolicy->setPointerCapture(true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004844 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004845
4846 NotifyDeviceResetArgs resetArgs;
4847 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4848 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4849 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4850
4851 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4852 mFakePointerController->setPosition(100, 200);
4853 mFakePointerController->setButtonState(0);
4854
4855 NotifyMotionArgs args;
4856
4857 // Move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004858 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4859 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4860 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004861 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4862 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4863 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4864 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4865 10.0f, 20.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
Michael Wright17db18e2020-06-26 20:51:44 +01004866 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004867
4868 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004869 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4870 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004871 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4872 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4873 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
4874 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4875 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4876 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4877 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4878 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
4879 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4880 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4881
4882 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004883 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4884 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004885 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4886 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4887 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
4888 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4889 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4890 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4891 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4892 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
4893 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4894 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4895
4896 // Another move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004897 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 30);
4898 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 40);
4899 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004900 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4901 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4902 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4903 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4904 30.0f, 40.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
Michael Wright17db18e2020-06-26 20:51:44 +01004905 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004906
4907 // Disable pointer capture and check that the device generation got bumped
4908 // and events are generated the usual way.
arthurhungdcef2dc2020-08-11 14:47:50 +08004909 const uint32_t generation = mReader->getContext()->getGeneration();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004910 mFakePolicy->setPointerCapture(false);
4911 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
arthurhungdcef2dc2020-08-11 14:47:50 +08004912 ASSERT_TRUE(mReader->getContext()->getGeneration() != generation);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004913
4914 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4915 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4916 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4917
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004918 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4919 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4920 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004921 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4922 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004923 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4924 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4925 110.0f, 220.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
Michael Wright17db18e2020-06-26 20:51:44 +01004926 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004927}
4928
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00004929/**
4930 * When Pointer Capture is enabled, we expect to report unprocessed relative movements, so any
4931 * pointer acceleration or speed processing should not be applied.
4932 */
4933TEST_F(CursorInputMapperTest, PointerCaptureDisablesVelocityProcessing) {
4934 addConfigurationProperty("cursor.mode", "pointer");
4935 const VelocityControlParameters testParams(5.f /*scale*/, 0.f /*low threshold*/,
4936 100.f /*high threshold*/, 10.f /*acceleration*/);
4937 mFakePolicy->setVelocityControlParams(testParams);
4938 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
4939
4940 NotifyDeviceResetArgs resetArgs;
4941 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4942 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4943 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4944
4945 NotifyMotionArgs args;
4946
4947 // Move and verify scale is applied.
4948 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4949 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4950 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4951 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4952 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4953 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4954 const float relX = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X);
4955 const float relY = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y);
4956 ASSERT_GT(relX, 10);
4957 ASSERT_GT(relY, 20);
4958
4959 // Enable Pointer Capture
4960 mFakePolicy->setPointerCapture(true);
4961 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
4962 NotifyPointerCaptureChangedArgs captureArgs;
4963 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyCaptureWasCalled(&captureArgs));
4964 ASSERT_TRUE(captureArgs.request.enable);
4965
4966 // Move and verify scale is not applied.
4967 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4968 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4969 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4970 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4971 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4972 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4973 ASSERT_EQ(10, args.pointerCoords[0].getX());
4974 ASSERT_EQ(20, args.pointerCoords[0].getY());
4975}
4976
Prabir Pradhan5d9d8e92022-06-24 18:37:04 +00004977TEST_F(CursorInputMapperTest, PointerCaptureDisablesOrientationChanges) {
4978 addConfigurationProperty("cursor.mode", "pointer");
4979 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
4980
4981 NotifyDeviceResetArgs resetArgs;
4982 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4983 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4984 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4985
4986 // Ensure the display is rotated.
4987 prepareDisplay(DISPLAY_ORIENTATION_90);
4988
4989 NotifyMotionArgs args;
4990
4991 // Verify that the coordinates are rotated.
4992 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4993 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4994 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4995 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4996 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4997 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4998 ASSERT_EQ(-20, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X));
4999 ASSERT_EQ(10, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y));
5000
5001 // Enable Pointer Capture.
5002 mFakePolicy->setPointerCapture(true);
5003 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
5004 NotifyPointerCaptureChangedArgs captureArgs;
5005 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyCaptureWasCalled(&captureArgs));
5006 ASSERT_TRUE(captureArgs.request.enable);
5007
5008 // Move and verify rotation is not applied.
5009 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5010 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5011 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5012 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5013 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
5014 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
5015 ASSERT_EQ(10, args.pointerCoords[0].getX());
5016 ASSERT_EQ(20, args.pointerCoords[0].getY());
5017}
5018
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005019TEST_F(CursorInputMapperTest, Process_ShouldHandleDisplayId) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005020 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005021
Garfield Tan888a6a42020-01-09 11:39:16 -08005022 // Setup for second display.
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005023 constexpr int32_t SECOND_DISPLAY_ID = 1;
Garfield Tan888a6a42020-01-09 11:39:16 -08005024 const std::string SECOND_DISPLAY_UNIQUE_ID = "local:1";
5025 mFakePolicy->addDisplayViewport(SECOND_DISPLAY_ID, 800, 480, DISPLAY_ORIENTATION_0,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00005026 true /*isActive*/, SECOND_DISPLAY_UNIQUE_ID, NO_PORT,
5027 ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08005028 mFakePolicy->setDefaultPointerDisplayId(SECOND_DISPLAY_ID);
5029 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
5030
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005031 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
5032 mFakePointerController->setPosition(100, 200);
5033 mFakePointerController->setButtonState(0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005034
5035 NotifyMotionArgs args;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005036 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5037 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5038 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005039 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5040 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
5041 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
5042 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5043 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 +01005044 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005045 ASSERT_EQ(SECOND_DISPLAY_ID, args.displayId);
5046}
5047
Michael Wrightd02c5b62014-02-10 15:10:22 -08005048// --- TouchInputMapperTest ---
5049
5050class TouchInputMapperTest : public InputMapperTest {
5051protected:
5052 static const int32_t RAW_X_MIN;
5053 static const int32_t RAW_X_MAX;
5054 static const int32_t RAW_Y_MIN;
5055 static const int32_t RAW_Y_MAX;
5056 static const int32_t RAW_TOUCH_MIN;
5057 static const int32_t RAW_TOUCH_MAX;
5058 static const int32_t RAW_TOOL_MIN;
5059 static const int32_t RAW_TOOL_MAX;
5060 static const int32_t RAW_PRESSURE_MIN;
5061 static const int32_t RAW_PRESSURE_MAX;
5062 static const int32_t RAW_ORIENTATION_MIN;
5063 static const int32_t RAW_ORIENTATION_MAX;
5064 static const int32_t RAW_DISTANCE_MIN;
5065 static const int32_t RAW_DISTANCE_MAX;
5066 static const int32_t RAW_TILT_MIN;
5067 static const int32_t RAW_TILT_MAX;
5068 static const int32_t RAW_ID_MIN;
5069 static const int32_t RAW_ID_MAX;
5070 static const int32_t RAW_SLOT_MIN;
5071 static const int32_t RAW_SLOT_MAX;
5072 static const float X_PRECISION;
5073 static const float Y_PRECISION;
Santos Cordonfa5cf462017-04-05 10:37:00 -07005074 static const float X_PRECISION_VIRTUAL;
5075 static const float Y_PRECISION_VIRTUAL;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005076
5077 static const float GEOMETRIC_SCALE;
Jason Gerecke489fda82012-09-07 17:19:40 -07005078 static const TouchAffineTransformation AFFINE_TRANSFORM;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005079
5080 static const VirtualKeyDefinition VIRTUAL_KEYS[2];
5081
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005082 const std::string UNIQUE_ID = "local:0";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005083 const std::string SECONDARY_UNIQUE_ID = "local:1";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005084
Michael Wrightd02c5b62014-02-10 15:10:22 -08005085 enum Axes {
5086 POSITION = 1 << 0,
5087 TOUCH = 1 << 1,
5088 TOOL = 1 << 2,
5089 PRESSURE = 1 << 3,
5090 ORIENTATION = 1 << 4,
5091 MINOR = 1 << 5,
5092 ID = 1 << 6,
5093 DISTANCE = 1 << 7,
5094 TILT = 1 << 8,
5095 SLOT = 1 << 9,
5096 TOOL_TYPE = 1 << 10,
5097 };
5098
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005099 void prepareDisplay(int32_t orientation, std::optional<uint8_t> port = NO_PORT);
5100 void prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port = NO_PORT);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005101 void prepareVirtualDisplay(int32_t orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005102 void prepareVirtualKeys();
Jason Gerecke489fda82012-09-07 17:19:40 -07005103 void prepareLocationCalibration();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005104 int32_t toRawX(float displayX);
5105 int32_t toRawY(float displayY);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005106 int32_t toRotatedRawX(float displayX);
5107 int32_t toRotatedRawY(float displayY);
Jason Gerecke489fda82012-09-07 17:19:40 -07005108 float toCookedX(float rawX, float rawY);
5109 float toCookedY(float rawX, float rawY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005110 float toDisplayX(int32_t rawX);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005111 float toDisplayX(int32_t rawX, int32_t displayWidth);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005112 float toDisplayY(int32_t rawY);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005113 float toDisplayY(int32_t rawY, int32_t displayHeight);
5114
Michael Wrightd02c5b62014-02-10 15:10:22 -08005115};
5116
5117const int32_t TouchInputMapperTest::RAW_X_MIN = 25;
5118const int32_t TouchInputMapperTest::RAW_X_MAX = 1019;
5119const int32_t TouchInputMapperTest::RAW_Y_MIN = 30;
5120const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009;
5121const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0;
5122const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31;
5123const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0;
5124const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15;
Michael Wrightaa449c92017-12-13 21:21:43 +00005125const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = 0;
5126const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = 255;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005127const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7;
5128const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
5129const int32_t TouchInputMapperTest::RAW_DISTANCE_MIN = 0;
5130const int32_t TouchInputMapperTest::RAW_DISTANCE_MAX = 7;
5131const int32_t TouchInputMapperTest::RAW_TILT_MIN = 0;
5132const int32_t TouchInputMapperTest::RAW_TILT_MAX = 150;
5133const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
5134const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
5135const int32_t TouchInputMapperTest::RAW_SLOT_MIN = 0;
5136const int32_t TouchInputMapperTest::RAW_SLOT_MAX = 9;
5137const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH;
5138const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT;
Santos Cordonfa5cf462017-04-05 10:37:00 -07005139const float TouchInputMapperTest::X_PRECISION_VIRTUAL =
5140 float(RAW_X_MAX - RAW_X_MIN + 1) / VIRTUAL_DISPLAY_WIDTH;
5141const float TouchInputMapperTest::Y_PRECISION_VIRTUAL =
5142 float(RAW_Y_MAX - RAW_Y_MIN + 1) / VIRTUAL_DISPLAY_HEIGHT;
Jason Gerecke489fda82012-09-07 17:19:40 -07005143const TouchAffineTransformation TouchInputMapperTest::AFFINE_TRANSFORM =
5144 TouchAffineTransformation(1, -2, 3, -4, 5, -6);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005145
5146const float TouchInputMapperTest::GEOMETRIC_SCALE =
5147 avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1),
5148 float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1));
5149
5150const VirtualKeyDefinition TouchInputMapperTest::VIRTUAL_KEYS[2] = {
5151 { KEY_HOME, 60, DISPLAY_HEIGHT + 15, 20, 20 },
5152 { KEY_MENU, DISPLAY_HEIGHT - 60, DISPLAY_WIDTH + 15, 20, 20 },
5153};
5154
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005155void TouchInputMapperTest::prepareDisplay(int32_t orientation, std::optional<uint8_t> port) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01005156 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
5157 port, ViewportType::INTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005158}
5159
5160void TouchInputMapperTest::prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port) {
5161 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
5162 DISPLAY_ORIENTATION_0, SECONDARY_UNIQUE_ID, port, type);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005163}
5164
Santos Cordonfa5cf462017-04-05 10:37:00 -07005165void TouchInputMapperTest::prepareVirtualDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01005166 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH, VIRTUAL_DISPLAY_HEIGHT,
5167 orientation, VIRTUAL_DISPLAY_UNIQUE_ID, NO_PORT,
5168 ViewportType::VIRTUAL);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005169}
5170
Michael Wrightd02c5b62014-02-10 15:10:22 -08005171void TouchInputMapperTest::prepareVirtualKeys() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005172 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[0]);
5173 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[1]);
5174 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
5175 mFakeEventHub->addKey(EVENTHUB_ID, KEY_MENU, 0, AKEYCODE_MENU, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005176}
5177
Jason Gerecke489fda82012-09-07 17:19:40 -07005178void TouchInputMapperTest::prepareLocationCalibration() {
5179 mFakePolicy->setTouchAffineTransformation(AFFINE_TRANSFORM);
5180}
5181
Michael Wrightd02c5b62014-02-10 15:10:22 -08005182int32_t TouchInputMapperTest::toRawX(float displayX) {
5183 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN);
5184}
5185
5186int32_t TouchInputMapperTest::toRawY(float displayY) {
5187 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN);
5188}
5189
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005190int32_t TouchInputMapperTest::toRotatedRawX(float displayX) {
5191 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_HEIGHT + RAW_X_MIN);
5192}
5193
5194int32_t TouchInputMapperTest::toRotatedRawY(float displayY) {
5195 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_WIDTH + RAW_Y_MIN);
5196}
5197
Jason Gerecke489fda82012-09-07 17:19:40 -07005198float TouchInputMapperTest::toCookedX(float rawX, float rawY) {
5199 AFFINE_TRANSFORM.applyTo(rawX, rawY);
5200 return rawX;
5201}
5202
5203float TouchInputMapperTest::toCookedY(float rawX, float rawY) {
5204 AFFINE_TRANSFORM.applyTo(rawX, rawY);
5205 return rawY;
5206}
5207
Michael Wrightd02c5b62014-02-10 15:10:22 -08005208float TouchInputMapperTest::toDisplayX(int32_t rawX) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005209 return toDisplayX(rawX, DISPLAY_WIDTH);
5210}
5211
5212float TouchInputMapperTest::toDisplayX(int32_t rawX, int32_t displayWidth) {
5213 return float(rawX - RAW_X_MIN) * displayWidth / (RAW_X_MAX - RAW_X_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005214}
5215
5216float TouchInputMapperTest::toDisplayY(int32_t rawY) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005217 return toDisplayY(rawY, DISPLAY_HEIGHT);
5218}
5219
5220float TouchInputMapperTest::toDisplayY(int32_t rawY, int32_t displayHeight) {
5221 return float(rawY - RAW_Y_MIN) * displayHeight / (RAW_Y_MAX - RAW_Y_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005222}
5223
5224
5225// --- SingleTouchInputMapperTest ---
5226
5227class SingleTouchInputMapperTest : public TouchInputMapperTest {
5228protected:
5229 void prepareButtons();
5230 void prepareAxes(int axes);
5231
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005232 void processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
5233 void processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
5234 void processUp(SingleTouchInputMapper& mappery);
5235 void processPressure(SingleTouchInputMapper& mapper, int32_t pressure);
5236 void processToolMajor(SingleTouchInputMapper& mapper, int32_t toolMajor);
5237 void processDistance(SingleTouchInputMapper& mapper, int32_t distance);
5238 void processTilt(SingleTouchInputMapper& mapper, int32_t tiltX, int32_t tiltY);
5239 void processKey(SingleTouchInputMapper& mapper, int32_t code, int32_t value);
5240 void processSync(SingleTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005241};
5242
5243void SingleTouchInputMapperTest::prepareButtons() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005244 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005245}
5246
5247void SingleTouchInputMapperTest::prepareAxes(int axes) {
5248 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005249 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
5250 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005251 }
5252 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005253 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MIN,
5254 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005255 }
5256 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005257 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TOOL_WIDTH, RAW_TOOL_MIN, RAW_TOOL_MAX, 0,
5258 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005259 }
5260 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005261 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_DISTANCE, RAW_DISTANCE_MIN,
5262 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005263 }
5264 if (axes & TILT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005265 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_X, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
5266 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_Y, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005267 }
5268}
5269
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005270void SingleTouchInputMapperTest::processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005271 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
5272 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
5273 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005274}
5275
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005276void SingleTouchInputMapperTest::processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005277 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
5278 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005279}
5280
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005281void SingleTouchInputMapperTest::processUp(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005282 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005283}
5284
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005285void SingleTouchInputMapperTest::processPressure(SingleTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005286 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005287}
5288
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005289void SingleTouchInputMapperTest::processToolMajor(SingleTouchInputMapper& mapper,
5290 int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005291 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TOOL_WIDTH, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005292}
5293
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005294void SingleTouchInputMapperTest::processDistance(SingleTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005295 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005296}
5297
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005298void SingleTouchInputMapperTest::processTilt(SingleTouchInputMapper& mapper, int32_t tiltX,
5299 int32_t tiltY) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005300 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_X, tiltX);
5301 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_Y, tiltY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005302}
5303
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005304void SingleTouchInputMapperTest::processKey(SingleTouchInputMapper& mapper, int32_t code,
5305 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005306 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005307}
5308
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005309void SingleTouchInputMapperTest::processSync(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005310 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005311}
5312
Michael Wrightd02c5b62014-02-10 15:10:22 -08005313TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005314 prepareButtons();
5315 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005316 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005317
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005318 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005319}
5320
5321TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndIsACursor_ReturnsTouchPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005322 mFakeEventHub->addRelativeAxis(EVENTHUB_ID, REL_X);
5323 mFakeEventHub->addRelativeAxis(EVENTHUB_ID, REL_Y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005324 prepareButtons();
5325 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005326 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005327
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005328 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005329}
5330
5331TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchPad_ReturnsTouchPad) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005332 prepareButtons();
5333 prepareAxes(POSITION);
5334 addConfigurationProperty("touch.deviceType", "touchPad");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005335 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005336
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005337 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005338}
5339
5340TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005341 prepareButtons();
5342 prepareAxes(POSITION);
5343 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005344 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005345
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005346 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005347}
5348
5349TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005350 addConfigurationProperty("touch.deviceType", "touchScreen");
5351 prepareDisplay(DISPLAY_ORIENTATION_0);
5352 prepareButtons();
5353 prepareAxes(POSITION);
5354 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005355 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005356
5357 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005358 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005359
5360 // Virtual key is down.
5361 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5362 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5363 processDown(mapper, x, y);
5364 processSync(mapper);
5365 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5366
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005367 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005368
5369 // Virtual key is up.
5370 processUp(mapper);
5371 processSync(mapper);
5372 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5373
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005374 ASSERT_EQ(AKEY_STATE_UP, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005375}
5376
5377TEST_F(SingleTouchInputMapperTest, GetScanCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005378 addConfigurationProperty("touch.deviceType", "touchScreen");
5379 prepareDisplay(DISPLAY_ORIENTATION_0);
5380 prepareButtons();
5381 prepareAxes(POSITION);
5382 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005383 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005384
5385 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005386 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005387
5388 // Virtual key is down.
5389 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5390 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5391 processDown(mapper, x, y);
5392 processSync(mapper);
5393 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5394
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005395 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005396
5397 // Virtual key is up.
5398 processUp(mapper);
5399 processSync(mapper);
5400 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5401
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005402 ASSERT_EQ(AKEY_STATE_UP, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005403}
5404
5405TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005406 addConfigurationProperty("touch.deviceType", "touchScreen");
5407 prepareDisplay(DISPLAY_ORIENTATION_0);
5408 prepareButtons();
5409 prepareAxes(POSITION);
5410 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005411 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005412
5413 const int32_t keys[2] = { AKEYCODE_HOME, AKEYCODE_A };
5414 uint8_t flags[2] = { 0, 0 };
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005415 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, 2, keys, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005416 ASSERT_TRUE(flags[0]);
5417 ASSERT_FALSE(flags[1]);
5418}
5419
5420TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005421 addConfigurationProperty("touch.deviceType", "touchScreen");
5422 prepareDisplay(DISPLAY_ORIENTATION_0);
5423 prepareButtons();
5424 prepareAxes(POSITION);
5425 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005426 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005427
arthurhungdcef2dc2020-08-11 14:47:50 +08005428 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005429
5430 NotifyKeyArgs args;
5431
5432 // Press virtual key.
5433 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5434 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5435 processDown(mapper, x, y);
5436 processSync(mapper);
5437
5438 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
5439 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5440 ASSERT_EQ(DEVICE_ID, args.deviceId);
5441 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
5442 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
5443 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
5444 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5445 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5446 ASSERT_EQ(KEY_HOME, args.scanCode);
5447 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5448 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5449
5450 // Release virtual key.
5451 processUp(mapper);
5452 processSync(mapper);
5453
5454 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
5455 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5456 ASSERT_EQ(DEVICE_ID, args.deviceId);
5457 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
5458 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
5459 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
5460 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5461 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5462 ASSERT_EQ(KEY_HOME, args.scanCode);
5463 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5464 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5465
5466 // Should not have sent any motions.
5467 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5468}
5469
5470TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005471 addConfigurationProperty("touch.deviceType", "touchScreen");
5472 prepareDisplay(DISPLAY_ORIENTATION_0);
5473 prepareButtons();
5474 prepareAxes(POSITION);
5475 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005476 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005477
arthurhungdcef2dc2020-08-11 14:47:50 +08005478 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005479
5480 NotifyKeyArgs keyArgs;
5481
5482 // Press virtual key.
5483 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5484 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5485 processDown(mapper, x, y);
5486 processSync(mapper);
5487
5488 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5489 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5490 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5491 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5492 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5493 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5494 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags);
5495 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5496 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5497 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5498 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5499
5500 // Move out of bounds. This should generate a cancel and a pointer down since we moved
5501 // into the display area.
5502 y -= 100;
5503 processMove(mapper, x, y);
5504 processSync(mapper);
5505
5506 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5507 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5508 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5509 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5510 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5511 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5512 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
5513 | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags);
5514 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5515 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5516 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5517 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5518
5519 NotifyMotionArgs motionArgs;
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_DOWN, 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 // Keep moving out of bounds. Should generate a pointer move.
5540 y -= 50;
5541 processMove(mapper, x, y);
5542 processSync(mapper);
5543
5544 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5545 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5546 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5547 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5548 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5549 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5550 ASSERT_EQ(0, motionArgs.flags);
5551 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5552 ASSERT_EQ(0, motionArgs.buttonState);
5553 ASSERT_EQ(0, motionArgs.edgeFlags);
5554 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5555 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5556 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5557 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5558 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5559 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5560 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5561 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5562
5563 // Release out of bounds. Should generate a pointer up.
5564 processUp(mapper);
5565 processSync(mapper);
5566
5567 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5568 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5569 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5570 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5571 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5572 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5573 ASSERT_EQ(0, motionArgs.flags);
5574 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5575 ASSERT_EQ(0, motionArgs.buttonState);
5576 ASSERT_EQ(0, motionArgs.edgeFlags);
5577 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5578 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5579 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5580 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5581 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5582 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5583 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5584 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5585
5586 // Should not have sent any more keys or motions.
5587 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5588 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5589}
5590
5591TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005592 addConfigurationProperty("touch.deviceType", "touchScreen");
5593 prepareDisplay(DISPLAY_ORIENTATION_0);
5594 prepareButtons();
5595 prepareAxes(POSITION);
5596 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005597 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005598
arthurhungdcef2dc2020-08-11 14:47:50 +08005599 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005600
5601 NotifyMotionArgs motionArgs;
5602
5603 // Initially go down out of bounds.
5604 int32_t x = -10;
5605 int32_t y = -10;
5606 processDown(mapper, x, y);
5607 processSync(mapper);
5608
5609 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5610
5611 // Move into the display area. Should generate a pointer down.
5612 x = 50;
5613 y = 75;
5614 processMove(mapper, x, y);
5615 processSync(mapper);
5616
5617 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5618 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5619 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5620 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5621 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5622 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5623 ASSERT_EQ(0, motionArgs.flags);
5624 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5625 ASSERT_EQ(0, motionArgs.buttonState);
5626 ASSERT_EQ(0, motionArgs.edgeFlags);
5627 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5628 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5629 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5630 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5631 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5632 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5633 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5634 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5635
5636 // Release. Should generate a pointer up.
5637 processUp(mapper);
5638 processSync(mapper);
5639
5640 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5641 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5642 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5643 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5644 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5645 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5646 ASSERT_EQ(0, motionArgs.flags);
5647 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5648 ASSERT_EQ(0, motionArgs.buttonState);
5649 ASSERT_EQ(0, motionArgs.edgeFlags);
5650 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5651 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5652 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5653 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5654 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5655 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5656 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5657 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5658
5659 // Should not have sent any more keys or motions.
5660 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5661 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5662}
5663
Santos Cordonfa5cf462017-04-05 10:37:00 -07005664TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture_VirtualDisplay) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005665 addConfigurationProperty("touch.deviceType", "touchScreen");
5666 addConfigurationProperty("touch.displayId", VIRTUAL_DISPLAY_UNIQUE_ID);
5667
5668 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
5669 prepareButtons();
5670 prepareAxes(POSITION);
5671 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005672 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Santos Cordonfa5cf462017-04-05 10:37:00 -07005673
arthurhungdcef2dc2020-08-11 14:47:50 +08005674 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005675
5676 NotifyMotionArgs motionArgs;
5677
5678 // Down.
5679 int32_t x = 100;
5680 int32_t y = 125;
5681 processDown(mapper, x, y);
5682 processSync(mapper);
5683
5684 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5685 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5686 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5687 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5688 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5689 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5690 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5691 ASSERT_EQ(0, motionArgs.flags);
5692 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5693 ASSERT_EQ(0, motionArgs.buttonState);
5694 ASSERT_EQ(0, motionArgs.edgeFlags);
5695 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5696 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5697 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5698 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5699 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5700 1, 0, 0, 0, 0, 0, 0, 0));
5701 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5702 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5703 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5704
5705 // Move.
5706 x += 50;
5707 y += 75;
5708 processMove(mapper, x, y);
5709 processSync(mapper);
5710
5711 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5712 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5713 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5714 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5715 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5716 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5717 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5718 ASSERT_EQ(0, motionArgs.flags);
5719 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5720 ASSERT_EQ(0, motionArgs.buttonState);
5721 ASSERT_EQ(0, motionArgs.edgeFlags);
5722 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5723 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5724 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5725 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5726 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5727 1, 0, 0, 0, 0, 0, 0, 0));
5728 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5729 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5730 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5731
5732 // Up.
5733 processUp(mapper);
5734 processSync(mapper);
5735
5736 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5737 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5738 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5739 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5740 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5741 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5742 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5743 ASSERT_EQ(0, motionArgs.flags);
5744 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5745 ASSERT_EQ(0, motionArgs.buttonState);
5746 ASSERT_EQ(0, motionArgs.edgeFlags);
5747 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5748 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5749 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5750 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5751 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5752 1, 0, 0, 0, 0, 0, 0, 0));
5753 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5754 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5755 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5756
5757 // Should not have sent any more keys or motions.
5758 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5759 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5760}
5761
Michael Wrightd02c5b62014-02-10 15:10:22 -08005762TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005763 addConfigurationProperty("touch.deviceType", "touchScreen");
5764 prepareDisplay(DISPLAY_ORIENTATION_0);
5765 prepareButtons();
5766 prepareAxes(POSITION);
5767 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005768 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005769
arthurhungdcef2dc2020-08-11 14:47:50 +08005770 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005771
5772 NotifyMotionArgs motionArgs;
5773
5774 // Down.
5775 int32_t x = 100;
5776 int32_t y = 125;
5777 processDown(mapper, x, y);
5778 processSync(mapper);
5779
5780 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5781 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5782 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5783 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5784 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5785 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5786 ASSERT_EQ(0, motionArgs.flags);
5787 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5788 ASSERT_EQ(0, motionArgs.buttonState);
5789 ASSERT_EQ(0, motionArgs.edgeFlags);
5790 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5791 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5792 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5793 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5794 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5795 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5796 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5797 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5798
5799 // Move.
5800 x += 50;
5801 y += 75;
5802 processMove(mapper, x, y);
5803 processSync(mapper);
5804
5805 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5806 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5807 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5808 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5809 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5810 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5811 ASSERT_EQ(0, motionArgs.flags);
5812 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5813 ASSERT_EQ(0, motionArgs.buttonState);
5814 ASSERT_EQ(0, motionArgs.edgeFlags);
5815 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5816 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5817 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5818 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5819 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5820 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5821 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5822 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5823
5824 // Up.
5825 processUp(mapper);
5826 processSync(mapper);
5827
5828 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5829 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5830 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5831 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5832 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5833 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5834 ASSERT_EQ(0, motionArgs.flags);
5835 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5836 ASSERT_EQ(0, motionArgs.buttonState);
5837 ASSERT_EQ(0, motionArgs.edgeFlags);
5838 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5839 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5840 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5841 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5842 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5843 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5844 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5845 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5846
5847 // Should not have sent any more keys or motions.
5848 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5849 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5850}
5851
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005852TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_DoesNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005853 addConfigurationProperty("touch.deviceType", "touchScreen");
5854 prepareButtons();
5855 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005856 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
5857 // need to be rotated. Touchscreens are orientation-aware by default.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005858 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005859
5860 NotifyMotionArgs args;
5861
5862 // Rotation 90.
5863 prepareDisplay(DISPLAY_ORIENTATION_90);
5864 processDown(mapper, toRawX(50), toRawY(75));
5865 processSync(mapper);
5866
5867 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5868 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5869 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5870
5871 processUp(mapper);
5872 processSync(mapper);
5873 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5874}
5875
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005876TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_RotatesMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005877 addConfigurationProperty("touch.deviceType", "touchScreen");
5878 prepareButtons();
5879 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005880 // Since InputReader works in the un-rotated coordinate space, only devices that are not
5881 // orientation-aware are affected by display rotation.
5882 addConfigurationProperty("touch.orientationAware", "0");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005883 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005884
5885 NotifyMotionArgs args;
5886
5887 // Rotation 0.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005888 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005889 prepareDisplay(DISPLAY_ORIENTATION_0);
5890 processDown(mapper, toRawX(50), toRawY(75));
5891 processSync(mapper);
5892
5893 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5894 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5895 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5896
5897 processUp(mapper);
5898 processSync(mapper);
5899 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5900
5901 // Rotation 90.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005902 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005903 prepareDisplay(DISPLAY_ORIENTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005904 processDown(mapper, toRawX(75), RAW_Y_MAX - toRawY(50) + RAW_Y_MIN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005905 processSync(mapper);
5906
5907 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5908 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5909 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5910
5911 processUp(mapper);
5912 processSync(mapper);
5913 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5914
5915 // Rotation 180.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005916 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005917 prepareDisplay(DISPLAY_ORIENTATION_180);
5918 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
5919 processSync(mapper);
5920
5921 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5922 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5923 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5924
5925 processUp(mapper);
5926 processSync(mapper);
5927 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5928
5929 // Rotation 270.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005930 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005931 prepareDisplay(DISPLAY_ORIENTATION_270);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005932 processDown(mapper, RAW_X_MAX - toRawX(75) + RAW_X_MIN, toRawY(50));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005933 processSync(mapper);
5934
5935 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5936 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5937 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5938
5939 processUp(mapper);
5940 processSync(mapper);
5941 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5942}
5943
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005944TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation0_RotatesMotions) {
5945 addConfigurationProperty("touch.deviceType", "touchScreen");
5946 prepareButtons();
5947 prepareAxes(POSITION);
5948 addConfigurationProperty("touch.orientationAware", "1");
5949 addConfigurationProperty("touch.orientation", "ORIENTATION_0");
5950 clearViewports();
5951 prepareDisplay(DISPLAY_ORIENTATION_0);
5952 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5953 NotifyMotionArgs args;
5954
5955 // Orientation 0.
5956 processDown(mapper, toRawX(50), toRawY(75));
5957 processSync(mapper);
5958
5959 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5960 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5961 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5962
5963 processUp(mapper);
5964 processSync(mapper);
5965 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5966}
5967
5968TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation90_RotatesMotions) {
5969 addConfigurationProperty("touch.deviceType", "touchScreen");
5970 prepareButtons();
5971 prepareAxes(POSITION);
5972 addConfigurationProperty("touch.orientationAware", "1");
5973 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
5974 clearViewports();
5975 prepareDisplay(DISPLAY_ORIENTATION_0);
5976 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5977 NotifyMotionArgs args;
5978
5979 // Orientation 90.
5980 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
5981 processSync(mapper);
5982
5983 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5984 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5985 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5986
5987 processUp(mapper);
5988 processSync(mapper);
5989 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5990}
5991
5992TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation180_RotatesMotions) {
5993 addConfigurationProperty("touch.deviceType", "touchScreen");
5994 prepareButtons();
5995 prepareAxes(POSITION);
5996 addConfigurationProperty("touch.orientationAware", "1");
5997 addConfigurationProperty("touch.orientation", "ORIENTATION_180");
5998 clearViewports();
5999 prepareDisplay(DISPLAY_ORIENTATION_0);
6000 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6001 NotifyMotionArgs args;
6002
6003 // Orientation 180.
6004 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
6005 processSync(mapper);
6006
6007 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6008 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6009 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6010
6011 processUp(mapper);
6012 processSync(mapper);
6013 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6014}
6015
6016TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation270_RotatesMotions) {
6017 addConfigurationProperty("touch.deviceType", "touchScreen");
6018 prepareButtons();
6019 prepareAxes(POSITION);
6020 addConfigurationProperty("touch.orientationAware", "1");
6021 addConfigurationProperty("touch.orientation", "ORIENTATION_270");
6022 clearViewports();
6023 prepareDisplay(DISPLAY_ORIENTATION_0);
6024 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6025 NotifyMotionArgs args;
6026
6027 // Orientation 270.
6028 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
6029 processSync(mapper);
6030
6031 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6032 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6033 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6034
6035 processUp(mapper);
6036 processSync(mapper);
6037 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6038}
6039
6040TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationSpecified_RotatesMotionWithDisplay) {
6041 addConfigurationProperty("touch.deviceType", "touchScreen");
6042 prepareButtons();
6043 prepareAxes(POSITION);
6044 // Since InputReader works in the un-rotated coordinate space, only devices that are not
6045 // orientation-aware are affected by display rotation.
6046 addConfigurationProperty("touch.orientationAware", "0");
6047 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
6048 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6049
6050 NotifyMotionArgs args;
6051
6052 // Orientation 90, Rotation 0.
6053 clearViewports();
6054 prepareDisplay(DISPLAY_ORIENTATION_0);
6055 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
6056 processSync(mapper);
6057
6058 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6059 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6060 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6061
6062 processUp(mapper);
6063 processSync(mapper);
6064 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6065
6066 // Orientation 90, Rotation 90.
6067 clearViewports();
6068 prepareDisplay(DISPLAY_ORIENTATION_90);
6069 processDown(mapper, toRotatedRawX(50), toRotatedRawY(75));
6070 processSync(mapper);
6071
6072 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6073 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6074 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6075
6076 processUp(mapper);
6077 processSync(mapper);
6078 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6079
6080 // Orientation 90, Rotation 180.
6081 clearViewports();
6082 prepareDisplay(DISPLAY_ORIENTATION_180);
6083 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
6084 processSync(mapper);
6085
6086 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6087 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6088 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6089
6090 processUp(mapper);
6091 processSync(mapper);
6092 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6093
6094 // Orientation 90, Rotation 270.
6095 clearViewports();
6096 prepareDisplay(DISPLAY_ORIENTATION_270);
6097 processDown(mapper, RAW_X_MAX - toRotatedRawX(50) + RAW_X_MIN,
6098 RAW_Y_MAX - toRotatedRawY(75) + RAW_Y_MIN);
6099 processSync(mapper);
6100
6101 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6102 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6103 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6104
6105 processUp(mapper);
6106 processSync(mapper);
6107 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6108}
6109
Michael Wrightd02c5b62014-02-10 15:10:22 -08006110TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006111 addConfigurationProperty("touch.deviceType", "touchScreen");
6112 prepareDisplay(DISPLAY_ORIENTATION_0);
6113 prepareButtons();
6114 prepareAxes(POSITION | PRESSURE | TOOL | DISTANCE | TILT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006115 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006116
6117 // These calculations are based on the input device calibration documentation.
6118 int32_t rawX = 100;
6119 int32_t rawY = 200;
6120 int32_t rawPressure = 10;
6121 int32_t rawToolMajor = 12;
6122 int32_t rawDistance = 2;
6123 int32_t rawTiltX = 30;
6124 int32_t rawTiltY = 110;
6125
6126 float x = toDisplayX(rawX);
6127 float y = toDisplayY(rawY);
6128 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
6129 float size = float(rawToolMajor) / RAW_TOOL_MAX;
6130 float tool = float(rawToolMajor) * GEOMETRIC_SCALE;
6131 float distance = float(rawDistance);
6132
6133 float tiltCenter = (RAW_TILT_MAX + RAW_TILT_MIN) * 0.5f;
6134 float tiltScale = M_PI / 180;
6135 float tiltXAngle = (rawTiltX - tiltCenter) * tiltScale;
6136 float tiltYAngle = (rawTiltY - tiltCenter) * tiltScale;
6137 float orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
6138 float tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
6139
6140 processDown(mapper, rawX, rawY);
6141 processPressure(mapper, rawPressure);
6142 processToolMajor(mapper, rawToolMajor);
6143 processDistance(mapper, rawDistance);
6144 processTilt(mapper, rawTiltX, rawTiltY);
6145 processSync(mapper);
6146
6147 NotifyMotionArgs args;
6148 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6149 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6150 x, y, pressure, size, tool, tool, tool, tool, orientation, distance));
6151 ASSERT_EQ(tilt, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TILT));
6152}
6153
Jason Gerecke489fda82012-09-07 17:19:40 -07006154TEST_F(SingleTouchInputMapperTest, Process_XYAxes_AffineCalibration) {
Jason Gerecke489fda82012-09-07 17:19:40 -07006155 addConfigurationProperty("touch.deviceType", "touchScreen");
6156 prepareDisplay(DISPLAY_ORIENTATION_0);
6157 prepareLocationCalibration();
6158 prepareButtons();
6159 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006160 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Jason Gerecke489fda82012-09-07 17:19:40 -07006161
6162 int32_t rawX = 100;
6163 int32_t rawY = 200;
6164
6165 float x = toDisplayX(toCookedX(rawX, rawY));
6166 float y = toDisplayY(toCookedY(rawX, rawY));
6167
6168 processDown(mapper, rawX, rawY);
6169 processSync(mapper);
6170
6171 NotifyMotionArgs args;
6172 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6173 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6174 x, y, 1, 0, 0, 0, 0, 0, 0, 0));
6175}
6176
Michael Wrightd02c5b62014-02-10 15:10:22 -08006177TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006178 addConfigurationProperty("touch.deviceType", "touchScreen");
6179 prepareDisplay(DISPLAY_ORIENTATION_0);
6180 prepareButtons();
6181 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006182 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006183
6184 NotifyMotionArgs motionArgs;
6185 NotifyKeyArgs keyArgs;
6186
6187 processDown(mapper, 100, 200);
6188 processSync(mapper);
6189 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6190 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6191 ASSERT_EQ(0, motionArgs.buttonState);
6192
6193 // press BTN_LEFT, release BTN_LEFT
6194 processKey(mapper, BTN_LEFT, 1);
6195 processSync(mapper);
6196 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6197 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6198 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6199
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006200 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6201 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6202 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6203
Michael Wrightd02c5b62014-02-10 15:10:22 -08006204 processKey(mapper, BTN_LEFT, 0);
6205 processSync(mapper);
6206 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006207 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006208 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006209
6210 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006211 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006212 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006213
6214 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
6215 processKey(mapper, BTN_RIGHT, 1);
6216 processKey(mapper, BTN_MIDDLE, 1);
6217 processSync(mapper);
6218 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6219 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6220 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6221 motionArgs.buttonState);
6222
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006223 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6224 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6225 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
6226
6227 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6228 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6229 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6230 motionArgs.buttonState);
6231
Michael Wrightd02c5b62014-02-10 15:10:22 -08006232 processKey(mapper, BTN_RIGHT, 0);
6233 processSync(mapper);
6234 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006235 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006236 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006237
6238 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006239 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006240 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006241
6242 processKey(mapper, BTN_MIDDLE, 0);
6243 processSync(mapper);
6244 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006245 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006246 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006247
6248 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006249 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006250 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006251
6252 // press BTN_BACK, release BTN_BACK
6253 processKey(mapper, BTN_BACK, 1);
6254 processSync(mapper);
6255 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6256 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6257 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006258
Michael Wrightd02c5b62014-02-10 15:10:22 -08006259 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006260 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006261 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6262
6263 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6264 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6265 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006266
6267 processKey(mapper, BTN_BACK, 0);
6268 processSync(mapper);
6269 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006270 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006271 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006272
6273 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006274 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006275 ASSERT_EQ(0, motionArgs.buttonState);
6276
Michael Wrightd02c5b62014-02-10 15:10:22 -08006277 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6278 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6279 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6280
6281 // press BTN_SIDE, release BTN_SIDE
6282 processKey(mapper, BTN_SIDE, 1);
6283 processSync(mapper);
6284 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6285 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6286 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006287
Michael Wrightd02c5b62014-02-10 15:10:22 -08006288 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006289 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006290 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6291
6292 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6293 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6294 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006295
6296 processKey(mapper, BTN_SIDE, 0);
6297 processSync(mapper);
6298 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006299 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006300 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006301
6302 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006303 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006304 ASSERT_EQ(0, motionArgs.buttonState);
6305
Michael Wrightd02c5b62014-02-10 15:10:22 -08006306 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6307 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6308 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6309
6310 // press BTN_FORWARD, release BTN_FORWARD
6311 processKey(mapper, BTN_FORWARD, 1);
6312 processSync(mapper);
6313 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6314 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6315 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006316
Michael Wrightd02c5b62014-02-10 15:10:22 -08006317 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006318 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006319 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6320
6321 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6322 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6323 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006324
6325 processKey(mapper, BTN_FORWARD, 0);
6326 processSync(mapper);
6327 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006328 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006329 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006330
6331 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006332 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006333 ASSERT_EQ(0, motionArgs.buttonState);
6334
Michael Wrightd02c5b62014-02-10 15:10:22 -08006335 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6336 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6337 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6338
6339 // press BTN_EXTRA, release BTN_EXTRA
6340 processKey(mapper, BTN_EXTRA, 1);
6341 processSync(mapper);
6342 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6343 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6344 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006345
Michael Wrightd02c5b62014-02-10 15:10:22 -08006346 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006347 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006348 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6349
6350 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6351 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6352 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006353
6354 processKey(mapper, BTN_EXTRA, 0);
6355 processSync(mapper);
6356 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006357 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006358 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006359
6360 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006361 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006362 ASSERT_EQ(0, motionArgs.buttonState);
6363
Michael Wrightd02c5b62014-02-10 15:10:22 -08006364 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6365 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6366 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6367
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006368 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6369
Michael Wrightd02c5b62014-02-10 15:10:22 -08006370 // press BTN_STYLUS, release BTN_STYLUS
6371 processKey(mapper, BTN_STYLUS, 1);
6372 processSync(mapper);
6373 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6374 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006375 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
6376
6377 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6378 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6379 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006380
6381 processKey(mapper, BTN_STYLUS, 0);
6382 processSync(mapper);
6383 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006384 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006385 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006386
6387 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006388 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006389 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006390
6391 // press BTN_STYLUS2, release BTN_STYLUS2
6392 processKey(mapper, BTN_STYLUS2, 1);
6393 processSync(mapper);
6394 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6395 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006396 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
6397
6398 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6399 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6400 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006401
6402 processKey(mapper, BTN_STYLUS2, 0);
6403 processSync(mapper);
6404 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006405 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006406 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006407
6408 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006409 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006410 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006411
6412 // release touch
6413 processUp(mapper);
6414 processSync(mapper);
6415 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6416 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6417 ASSERT_EQ(0, motionArgs.buttonState);
6418}
6419
6420TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006421 addConfigurationProperty("touch.deviceType", "touchScreen");
6422 prepareDisplay(DISPLAY_ORIENTATION_0);
6423 prepareButtons();
6424 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006425 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006426
6427 NotifyMotionArgs motionArgs;
6428
6429 // default tool type is finger
6430 processDown(mapper, 100, 200);
6431 processSync(mapper);
6432 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6433 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6434 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6435
6436 // eraser
6437 processKey(mapper, BTN_TOOL_RUBBER, 1);
6438 processSync(mapper);
6439 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6440 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6441 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6442
6443 // stylus
6444 processKey(mapper, BTN_TOOL_RUBBER, 0);
6445 processKey(mapper, BTN_TOOL_PEN, 1);
6446 processSync(mapper);
6447 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6448 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6449 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6450
6451 // brush
6452 processKey(mapper, BTN_TOOL_PEN, 0);
6453 processKey(mapper, BTN_TOOL_BRUSH, 1);
6454 processSync(mapper);
6455 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6456 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6457 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6458
6459 // pencil
6460 processKey(mapper, BTN_TOOL_BRUSH, 0);
6461 processKey(mapper, BTN_TOOL_PENCIL, 1);
6462 processSync(mapper);
6463 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6464 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6465 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6466
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08006467 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08006468 processKey(mapper, BTN_TOOL_PENCIL, 0);
6469 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
6470 processSync(mapper);
6471 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6472 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6473 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6474
6475 // mouse
6476 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
6477 processKey(mapper, BTN_TOOL_MOUSE, 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_MOUSE, motionArgs.pointerProperties[0].toolType);
6482
6483 // lens
6484 processKey(mapper, BTN_TOOL_MOUSE, 0);
6485 processKey(mapper, BTN_TOOL_LENS, 1);
6486 processSync(mapper);
6487 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6488 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6489 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6490
6491 // double-tap
6492 processKey(mapper, BTN_TOOL_LENS, 0);
6493 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
6494 processSync(mapper);
6495 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6496 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6497 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6498
6499 // triple-tap
6500 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
6501 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
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 // quad-tap
6508 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
6509 processKey(mapper, BTN_TOOL_QUADTAP, 1);
6510 processSync(mapper);
6511 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6512 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6513 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6514
6515 // finger
6516 processKey(mapper, BTN_TOOL_QUADTAP, 0);
6517 processKey(mapper, BTN_TOOL_FINGER, 1);
6518 processSync(mapper);
6519 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6520 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6521 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6522
6523 // stylus trumps finger
6524 processKey(mapper, BTN_TOOL_PEN, 1);
6525 processSync(mapper);
6526 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6527 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6528 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6529
6530 // eraser trumps stylus
6531 processKey(mapper, BTN_TOOL_RUBBER, 1);
6532 processSync(mapper);
6533 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6534 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6535 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6536
6537 // mouse trumps eraser
6538 processKey(mapper, BTN_TOOL_MOUSE, 1);
6539 processSync(mapper);
6540 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6541 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6542 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6543
6544 // back to default tool type
6545 processKey(mapper, BTN_TOOL_MOUSE, 0);
6546 processKey(mapper, BTN_TOOL_RUBBER, 0);
6547 processKey(mapper, BTN_TOOL_PEN, 0);
6548 processKey(mapper, BTN_TOOL_FINGER, 0);
6549 processSync(mapper);
6550 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6551 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6552 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6553}
6554
6555TEST_F(SingleTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006556 addConfigurationProperty("touch.deviceType", "touchScreen");
6557 prepareDisplay(DISPLAY_ORIENTATION_0);
6558 prepareButtons();
6559 prepareAxes(POSITION);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006560 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_FINGER, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006561 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006562
6563 NotifyMotionArgs motionArgs;
6564
6565 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
6566 processKey(mapper, BTN_TOOL_FINGER, 1);
6567 processMove(mapper, 100, 200);
6568 processSync(mapper);
6569 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6570 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6571 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6572 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6573
6574 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6575 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6576 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6577 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6578
6579 // move a little
6580 processMove(mapper, 150, 250);
6581 processSync(mapper);
6582 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6583 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6584 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6585 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6586
6587 // down when BTN_TOUCH is pressed, pressure defaults to 1
6588 processKey(mapper, BTN_TOUCH, 1);
6589 processSync(mapper);
6590 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6591 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6592 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6593 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6594
6595 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6596 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6597 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6598 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6599
6600 // up when BTN_TOUCH is released, hover restored
6601 processKey(mapper, BTN_TOUCH, 0);
6602 processSync(mapper);
6603 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6604 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6605 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6606 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6607
6608 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6609 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6610 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6611 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6612
6613 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6614 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6615 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6616 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6617
6618 // exit hover when pointer goes away
6619 processKey(mapper, BTN_TOOL_FINGER, 0);
6620 processSync(mapper);
6621 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6622 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6623 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6624 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6625}
6626
6627TEST_F(SingleTouchInputMapperTest, Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006628 addConfigurationProperty("touch.deviceType", "touchScreen");
6629 prepareDisplay(DISPLAY_ORIENTATION_0);
6630 prepareButtons();
6631 prepareAxes(POSITION | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006632 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006633
6634 NotifyMotionArgs motionArgs;
6635
6636 // initially hovering because pressure is 0
6637 processDown(mapper, 100, 200);
6638 processPressure(mapper, 0);
6639 processSync(mapper);
6640 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6641 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6642 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6643 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6644
6645 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6646 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6647 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6648 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6649
6650 // move a little
6651 processMove(mapper, 150, 250);
6652 processSync(mapper);
6653 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6654 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6655 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6656 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6657
6658 // down when pressure is non-zero
6659 processPressure(mapper, RAW_PRESSURE_MAX);
6660 processSync(mapper);
6661 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6662 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6663 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6664 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6665
6666 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6667 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6668 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6669 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6670
6671 // up when pressure becomes 0, hover restored
6672 processPressure(mapper, 0);
6673 processSync(mapper);
6674 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6675 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6676 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6677 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6678
6679 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6680 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6681 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6682 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6683
6684 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6685 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6686 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6687 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6688
6689 // exit hover when pointer goes away
6690 processUp(mapper);
6691 processSync(mapper);
6692 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6693 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6694 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6695 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6696}
6697
lilinnane34bc532022-07-19 16:00:50 +08006698TEST_F(SingleTouchInputMapperTest,
6699 Process_WhenViewportDisplayIdChanged_TouchIsCanceledAndDeviceIsReset) {
6700 addConfigurationProperty("touch.deviceType", "touchScreen");
6701 prepareDisplay(DISPLAY_ORIENTATION_0);
6702 prepareButtons();
6703 prepareAxes(POSITION);
6704 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6705 NotifyMotionArgs motionArgs;
6706
6707 // Down.
6708 int32_t x = 100;
6709 int32_t y = 200;
6710 processDown(mapper, x, y);
6711 processSync(mapper);
6712
6713 // We should receive a down event
6714 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6715 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6716
6717 // Change display id
6718 clearViewports();
6719 prepareSecondaryDisplay(ViewportType::INTERNAL);
6720
6721 // We should receive a cancel event
6722 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6723 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
6724 // Then receive reset called
6725 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6726}
6727
Prabir Pradhan5632d622021-09-06 07:57:20 -07006728// --- TouchDisplayProjectionTest ---
6729
6730class TouchDisplayProjectionTest : public SingleTouchInputMapperTest {
6731public:
6732 // The values inside DisplayViewport are expected to be pre-rotated. This updates the current
6733 // DisplayViewport to pre-rotate the values. The viewport's physical display will be set to the
6734 // rotated equivalent of the given un-rotated physical display bounds.
6735 void configurePhysicalDisplay(int32_t orientation, Rect naturalPhysicalDisplay) {
6736 uint32_t inverseRotationFlags;
6737 auto width = DISPLAY_WIDTH;
6738 auto height = DISPLAY_HEIGHT;
6739 switch (orientation) {
6740 case DISPLAY_ORIENTATION_90:
6741 inverseRotationFlags = ui::Transform::ROT_270;
6742 std::swap(width, height);
6743 break;
6744 case DISPLAY_ORIENTATION_180:
6745 inverseRotationFlags = ui::Transform::ROT_180;
6746 break;
6747 case DISPLAY_ORIENTATION_270:
6748 inverseRotationFlags = ui::Transform::ROT_90;
6749 std::swap(width, height);
6750 break;
6751 case DISPLAY_ORIENTATION_0:
6752 inverseRotationFlags = ui::Transform::ROT_0;
6753 break;
6754 default:
6755 FAIL() << "Invalid orientation: " << orientation;
6756 }
6757
6758 const ui::Transform rotation(inverseRotationFlags, width, height);
6759 const Rect rotatedPhysicalDisplay = rotation.transform(naturalPhysicalDisplay);
6760
6761 std::optional<DisplayViewport> internalViewport =
6762 *mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6763 DisplayViewport& v = *internalViewport;
6764 v.displayId = DISPLAY_ID;
6765 v.orientation = orientation;
6766
6767 v.logicalLeft = 0;
6768 v.logicalTop = 0;
6769 v.logicalRight = 100;
6770 v.logicalBottom = 100;
6771
6772 v.physicalLeft = rotatedPhysicalDisplay.left;
6773 v.physicalTop = rotatedPhysicalDisplay.top;
6774 v.physicalRight = rotatedPhysicalDisplay.right;
6775 v.physicalBottom = rotatedPhysicalDisplay.bottom;
6776
6777 v.deviceWidth = width;
6778 v.deviceHeight = height;
6779
6780 v.isActive = true;
6781 v.uniqueId = UNIQUE_ID;
6782 v.type = ViewportType::INTERNAL;
6783 mFakePolicy->updateViewport(v);
6784 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
6785 }
6786
6787 void assertReceivedMove(const Point& point) {
6788 NotifyMotionArgs motionArgs;
6789 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6790 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6791 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6792 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], point.x, point.y,
6793 1, 0, 0, 0, 0, 0, 0, 0));
6794 }
6795};
6796
6797TEST_F(TouchDisplayProjectionTest, IgnoresTouchesOutsidePhysicalDisplay) {
6798 addConfigurationProperty("touch.deviceType", "touchScreen");
6799 prepareDisplay(DISPLAY_ORIENTATION_0);
6800
6801 prepareButtons();
6802 prepareAxes(POSITION);
6803 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6804
6805 NotifyMotionArgs motionArgs;
6806
6807 // Configure the DisplayViewport such that the logical display maps to a subsection of
6808 // the display panel called the physical display. Here, the physical display is bounded by the
6809 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
6810 static const Rect kPhysicalDisplay{10, 20, 70, 160};
6811 static const std::array<Point, 6> kPointsOutsidePhysicalDisplay{
6812 {{-10, -10}, {0, 0}, {5, 100}, {50, 15}, {75, 100}, {50, 165}}};
6813
6814 for (auto orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90, DISPLAY_ORIENTATION_180,
6815 DISPLAY_ORIENTATION_270}) {
6816 configurePhysicalDisplay(orientation, kPhysicalDisplay);
6817
6818 // Touches outside the physical display should be ignored, and should not generate any
6819 // events. Ensure touches at the following points that lie outside of the physical display
6820 // area do not generate any events.
6821 for (const auto& point : kPointsOutsidePhysicalDisplay) {
6822 processDown(mapper, toRawX(point.x), toRawY(point.y));
6823 processSync(mapper);
6824 processUp(mapper);
6825 processSync(mapper);
6826 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled())
6827 << "Unexpected event generated for touch outside physical display at point: "
6828 << point.x << ", " << point.y;
6829 }
6830 }
6831}
6832
6833TEST_F(TouchDisplayProjectionTest, EmitsTouchDownAfterEnteringPhysicalDisplay) {
6834 addConfigurationProperty("touch.deviceType", "touchScreen");
6835 prepareDisplay(DISPLAY_ORIENTATION_0);
6836
6837 prepareButtons();
6838 prepareAxes(POSITION);
6839 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6840
6841 NotifyMotionArgs motionArgs;
6842
6843 // Configure the DisplayViewport such that the logical display maps to a subsection of
6844 // the display panel called the physical display. Here, the physical display is bounded by the
6845 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
6846 static const Rect kPhysicalDisplay{10, 20, 70, 160};
6847
6848 for (auto orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90, DISPLAY_ORIENTATION_180,
6849 DISPLAY_ORIENTATION_270}) {
6850 configurePhysicalDisplay(orientation, kPhysicalDisplay);
6851
6852 // Touches that start outside the physical display should be ignored until it enters the
6853 // physical display bounds, at which point it should generate a down event. Start a touch at
6854 // the point (5, 100), which is outside the physical display bounds.
6855 static const Point kOutsidePoint{5, 100};
6856 processDown(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
6857 processSync(mapper);
6858 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6859
6860 // Move the touch into the physical display area. This should generate a pointer down.
6861 processMove(mapper, toRawX(11), toRawY(21));
6862 processSync(mapper);
6863 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6864 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6865 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6866 ASSERT_NO_FATAL_FAILURE(
6867 assertPointerCoords(motionArgs.pointerCoords[0], 11, 21, 1, 0, 0, 0, 0, 0, 0, 0));
6868
6869 // Move the touch inside the physical display area. This should generate a pointer move.
6870 processMove(mapper, toRawX(69), toRawY(159));
6871 processSync(mapper);
6872 assertReceivedMove({69, 159});
6873
6874 // Move outside the physical display area. Since the pointer is already down, this should
6875 // now continue generating events.
6876 processMove(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
6877 processSync(mapper);
6878 assertReceivedMove(kOutsidePoint);
6879
6880 // Release. This should generate a pointer up.
6881 processUp(mapper);
6882 processSync(mapper);
6883 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6884 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6885 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], kOutsidePoint.x,
6886 kOutsidePoint.y, 1, 0, 0, 0, 0, 0, 0, 0));
6887
6888 // Ensure no more events were generated.
6889 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6890 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6891 }
6892}
6893
Michael Wrightd02c5b62014-02-10 15:10:22 -08006894// --- MultiTouchInputMapperTest ---
6895
6896class MultiTouchInputMapperTest : public TouchInputMapperTest {
6897protected:
6898 void prepareAxes(int axes);
6899
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006900 void processPosition(MultiTouchInputMapper& mapper, int32_t x, int32_t y);
6901 void processTouchMajor(MultiTouchInputMapper& mapper, int32_t touchMajor);
6902 void processTouchMinor(MultiTouchInputMapper& mapper, int32_t touchMinor);
6903 void processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor);
6904 void processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor);
6905 void processOrientation(MultiTouchInputMapper& mapper, int32_t orientation);
6906 void processPressure(MultiTouchInputMapper& mapper, int32_t pressure);
6907 void processDistance(MultiTouchInputMapper& mapper, int32_t distance);
6908 void processId(MultiTouchInputMapper& mapper, int32_t id);
6909 void processSlot(MultiTouchInputMapper& mapper, int32_t slot);
6910 void processToolType(MultiTouchInputMapper& mapper, int32_t toolType);
6911 void processKey(MultiTouchInputMapper& mapper, int32_t code, int32_t value);
6912 void processMTSync(MultiTouchInputMapper& mapper);
6913 void processSync(MultiTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006914};
6915
6916void MultiTouchInputMapperTest::prepareAxes(int axes) {
6917 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006918 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
6919 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006920 }
6921 if (axes & TOUCH) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006922 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN,
6923 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006924 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006925 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN,
6926 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006927 }
6928 }
6929 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006930 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
6931 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006932 if (axes & MINOR) {
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08006933 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006934 RAW_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006935 }
6936 }
6937 if (axes & ORIENTATION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006938 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_ORIENTATION, RAW_ORIENTATION_MIN,
6939 RAW_ORIENTATION_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006940 }
6941 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006942 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_PRESSURE, RAW_PRESSURE_MIN,
6943 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006944 }
6945 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006946 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_DISTANCE, RAW_DISTANCE_MIN,
6947 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006948 }
6949 if (axes & ID) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006950 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX, 0,
6951 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006952 }
6953 if (axes & SLOT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006954 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
6955 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_MT_SLOT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006956 }
6957 if (axes & TOOL_TYPE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006958 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOOL_TYPE, 0, MT_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006959 }
6960}
6961
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006962void MultiTouchInputMapperTest::processPosition(MultiTouchInputMapper& mapper, int32_t x,
6963 int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006964 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_X, x);
6965 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006966}
6967
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006968void MultiTouchInputMapperTest::processTouchMajor(MultiTouchInputMapper& mapper,
6969 int32_t touchMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006970 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006971}
6972
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006973void MultiTouchInputMapperTest::processTouchMinor(MultiTouchInputMapper& mapper,
6974 int32_t touchMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006975 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006976}
6977
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006978void MultiTouchInputMapperTest::processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006979 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006980}
6981
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006982void MultiTouchInputMapperTest::processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006983 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006984}
6985
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006986void MultiTouchInputMapperTest::processOrientation(MultiTouchInputMapper& mapper,
6987 int32_t orientation) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006988 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_ORIENTATION, orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006989}
6990
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006991void MultiTouchInputMapperTest::processPressure(MultiTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006992 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006993}
6994
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006995void MultiTouchInputMapperTest::processDistance(MultiTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006996 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006997}
6998
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006999void MultiTouchInputMapperTest::processId(MultiTouchInputMapper& mapper, int32_t id) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007000 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TRACKING_ID, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007001}
7002
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007003void MultiTouchInputMapperTest::processSlot(MultiTouchInputMapper& mapper, int32_t slot) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007004 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_SLOT, slot);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007005}
7006
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007007void MultiTouchInputMapperTest::processToolType(MultiTouchInputMapper& mapper, int32_t toolType) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007008 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007009}
7010
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007011void MultiTouchInputMapperTest::processKey(MultiTouchInputMapper& mapper, int32_t code,
7012 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007013 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007014}
7015
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007016void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007017 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_MT_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007018}
7019
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007020void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007021 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007022}
7023
Michael Wrightd02c5b62014-02-10 15:10:22 -08007024TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007025 addConfigurationProperty("touch.deviceType", "touchScreen");
7026 prepareDisplay(DISPLAY_ORIENTATION_0);
7027 prepareAxes(POSITION);
7028 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007029 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007030
arthurhungdcef2dc2020-08-11 14:47:50 +08007031 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007032
7033 NotifyMotionArgs motionArgs;
7034
7035 // Two fingers down at once.
7036 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7037 processPosition(mapper, x1, y1);
7038 processMTSync(mapper);
7039 processPosition(mapper, x2, y2);
7040 processMTSync(mapper);
7041 processSync(mapper);
7042
7043 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7044 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7045 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7046 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7047 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7048 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7049 ASSERT_EQ(0, motionArgs.flags);
7050 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7051 ASSERT_EQ(0, motionArgs.buttonState);
7052 ASSERT_EQ(0, motionArgs.edgeFlags);
7053 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7054 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7055 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7056 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7057 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7058 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7059 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7060 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7061
7062 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7063 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7064 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7065 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7066 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007067 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007068 ASSERT_EQ(0, motionArgs.flags);
7069 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7070 ASSERT_EQ(0, motionArgs.buttonState);
7071 ASSERT_EQ(0, motionArgs.edgeFlags);
7072 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7073 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7074 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7075 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7076 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7077 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7078 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7079 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7080 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7081 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7082 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7083 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7084
7085 // Move.
7086 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7087 processPosition(mapper, x1, y1);
7088 processMTSync(mapper);
7089 processPosition(mapper, x2, y2);
7090 processMTSync(mapper);
7091 processSync(mapper);
7092
7093 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7094 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7095 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7096 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7097 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7098 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7099 ASSERT_EQ(0, motionArgs.flags);
7100 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7101 ASSERT_EQ(0, motionArgs.buttonState);
7102 ASSERT_EQ(0, motionArgs.edgeFlags);
7103 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7104 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7105 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7106 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7107 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7108 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7109 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7110 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7111 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7112 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7113 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7114 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7115
7116 // First finger up.
7117 x2 += 15; y2 -= 20;
7118 processPosition(mapper, x2, y2);
7119 processMTSync(mapper);
7120 processSync(mapper);
7121
7122 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7123 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7124 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7125 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7126 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007127 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007128 ASSERT_EQ(0, motionArgs.flags);
7129 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7130 ASSERT_EQ(0, motionArgs.buttonState);
7131 ASSERT_EQ(0, motionArgs.edgeFlags);
7132 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7133 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7134 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7135 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7136 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7137 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7138 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7139 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7140 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7141 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7142 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7143 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7144
7145 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7146 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7147 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7148 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7149 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7150 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7151 ASSERT_EQ(0, motionArgs.flags);
7152 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7153 ASSERT_EQ(0, motionArgs.buttonState);
7154 ASSERT_EQ(0, motionArgs.edgeFlags);
7155 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7156 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7157 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7158 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7159 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7160 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7161 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7162 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7163
7164 // Move.
7165 x2 += 20; y2 -= 25;
7166 processPosition(mapper, x2, y2);
7167 processMTSync(mapper);
7168 processSync(mapper);
7169
7170 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7171 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7172 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7173 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7174 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7175 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7176 ASSERT_EQ(0, motionArgs.flags);
7177 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7178 ASSERT_EQ(0, motionArgs.buttonState);
7179 ASSERT_EQ(0, motionArgs.edgeFlags);
7180 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7181 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7182 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7183 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7184 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7185 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7186 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7187 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7188
7189 // New finger down.
7190 int32_t x3 = 700, y3 = 300;
7191 processPosition(mapper, x2, y2);
7192 processMTSync(mapper);
7193 processPosition(mapper, x3, y3);
7194 processMTSync(mapper);
7195 processSync(mapper);
7196
7197 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7198 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7199 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7200 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7201 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007202 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007203 ASSERT_EQ(0, motionArgs.flags);
7204 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7205 ASSERT_EQ(0, motionArgs.buttonState);
7206 ASSERT_EQ(0, motionArgs.edgeFlags);
7207 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7208 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7209 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7210 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7211 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7212 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7213 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7214 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7215 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7216 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7217 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7218 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7219
7220 // Second finger up.
7221 x3 += 30; y3 -= 20;
7222 processPosition(mapper, x3, y3);
7223 processMTSync(mapper);
7224 processSync(mapper);
7225
7226 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7227 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7228 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7229 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7230 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007231 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007232 ASSERT_EQ(0, motionArgs.flags);
7233 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7234 ASSERT_EQ(0, motionArgs.buttonState);
7235 ASSERT_EQ(0, motionArgs.edgeFlags);
7236 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7237 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7238 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7239 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7240 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7241 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7242 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7243 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7244 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7245 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7246 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7247 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7248
7249 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7250 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7251 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7252 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7253 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7254 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7255 ASSERT_EQ(0, motionArgs.flags);
7256 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7257 ASSERT_EQ(0, motionArgs.buttonState);
7258 ASSERT_EQ(0, motionArgs.edgeFlags);
7259 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7260 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7261 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7262 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7263 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7264 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7265 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7266 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7267
7268 // Last finger up.
7269 processMTSync(mapper);
7270 processSync(mapper);
7271
7272 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7273 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7274 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7275 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7276 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7277 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7278 ASSERT_EQ(0, motionArgs.flags);
7279 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7280 ASSERT_EQ(0, motionArgs.buttonState);
7281 ASSERT_EQ(0, motionArgs.edgeFlags);
7282 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7283 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7284 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7285 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7286 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7287 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7288 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7289 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7290
7291 // Should not have sent any more keys or motions.
7292 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7293 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7294}
7295
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007296TEST_F(MultiTouchInputMapperTest, AxisResolution_IsPopulated) {
7297 addConfigurationProperty("touch.deviceType", "touchScreen");
7298 prepareDisplay(DISPLAY_ORIENTATION_0);
7299
7300 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
7301 /*fuzz*/ 0, /*resolution*/ 10);
7302 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
7303 /*fuzz*/ 0, /*resolution*/ 11);
7304 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
7305 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 12);
7306 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
7307 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 13);
7308 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7309 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 14);
7310 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7311 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 15);
7312
7313 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7314
7315 // X and Y axes
7316 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_X, 10 / X_PRECISION);
7317 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_Y, 11 / Y_PRECISION);
7318 // Touch major and minor
7319 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR, 12 * GEOMETRIC_SCALE);
7320 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR, 13 * GEOMETRIC_SCALE);
7321 // Tool major and minor
7322 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR, 14 * GEOMETRIC_SCALE);
7323 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR, 15 * GEOMETRIC_SCALE);
7324}
7325
7326TEST_F(MultiTouchInputMapperTest, TouchMajorAndMinorAxes_DoNotAppearIfNotSupported) {
7327 addConfigurationProperty("touch.deviceType", "touchScreen");
7328 prepareDisplay(DISPLAY_ORIENTATION_0);
7329
7330 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
7331 /*fuzz*/ 0, /*resolution*/ 10);
7332 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
7333 /*fuzz*/ 0, /*resolution*/ 11);
7334
7335 // We do not add ABS_MT_TOUCH_MAJOR / MINOR or ABS_MT_WIDTH_MAJOR / MINOR axes
7336
7337 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7338
7339 // Touch major and minor
7340 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR);
7341 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR);
7342 // Tool major and minor
7343 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR);
7344 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR);
7345}
7346
Michael Wrightd02c5b62014-02-10 15:10:22 -08007347TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007348 addConfigurationProperty("touch.deviceType", "touchScreen");
7349 prepareDisplay(DISPLAY_ORIENTATION_0);
7350 prepareAxes(POSITION | ID);
7351 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007352 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007353
arthurhungdcef2dc2020-08-11 14:47:50 +08007354 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007355
7356 NotifyMotionArgs motionArgs;
7357
7358 // Two fingers down at once.
7359 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7360 processPosition(mapper, x1, y1);
7361 processId(mapper, 1);
7362 processMTSync(mapper);
7363 processPosition(mapper, x2, y2);
7364 processId(mapper, 2);
7365 processMTSync(mapper);
7366 processSync(mapper);
7367
7368 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7369 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7370 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7371 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7372 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7373 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7374 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7375
7376 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007377 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007378 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7379 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7380 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7381 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7382 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7383 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7384 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7385 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7386 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7387
7388 // Move.
7389 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7390 processPosition(mapper, x1, y1);
7391 processId(mapper, 1);
7392 processMTSync(mapper);
7393 processPosition(mapper, x2, y2);
7394 processId(mapper, 2);
7395 processMTSync(mapper);
7396 processSync(mapper);
7397
7398 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7399 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7400 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7401 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7402 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7403 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7404 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7405 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7406 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7407 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7408 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7409
7410 // First finger up.
7411 x2 += 15; y2 -= 20;
7412 processPosition(mapper, x2, y2);
7413 processId(mapper, 2);
7414 processMTSync(mapper);
7415 processSync(mapper);
7416
7417 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007418 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007419 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7420 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7421 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7422 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7423 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7424 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7425 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7426 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7427 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7428
7429 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7430 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7431 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7432 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7433 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7434 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7435 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7436
7437 // Move.
7438 x2 += 20; y2 -= 25;
7439 processPosition(mapper, x2, y2);
7440 processId(mapper, 2);
7441 processMTSync(mapper);
7442 processSync(mapper);
7443
7444 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7445 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7446 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7447 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7448 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7449 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7450 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7451
7452 // New finger down.
7453 int32_t x3 = 700, y3 = 300;
7454 processPosition(mapper, x2, y2);
7455 processId(mapper, 2);
7456 processMTSync(mapper);
7457 processPosition(mapper, x3, y3);
7458 processId(mapper, 3);
7459 processMTSync(mapper);
7460 processSync(mapper);
7461
7462 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007463 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007464 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7465 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7466 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7467 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7468 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7469 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7470 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7471 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7472 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7473
7474 // Second finger up.
7475 x3 += 30; y3 -= 20;
7476 processPosition(mapper, x3, y3);
7477 processId(mapper, 3);
7478 processMTSync(mapper);
7479 processSync(mapper);
7480
7481 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007482 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007483 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7484 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7485 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7486 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7487 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7488 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7489 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7490 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7491 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7492
7493 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7494 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7495 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7496 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7497 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7498 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7499 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7500
7501 // Last finger up.
7502 processMTSync(mapper);
7503 processSync(mapper);
7504
7505 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7506 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7507 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7508 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7509 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7510 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7511 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7512
7513 // Should not have sent any more keys or motions.
7514 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7515 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7516}
7517
7518TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007519 addConfigurationProperty("touch.deviceType", "touchScreen");
7520 prepareDisplay(DISPLAY_ORIENTATION_0);
7521 prepareAxes(POSITION | ID | SLOT);
7522 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007523 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007524
arthurhungdcef2dc2020-08-11 14:47:50 +08007525 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007526
7527 NotifyMotionArgs motionArgs;
7528
7529 // Two fingers down at once.
7530 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7531 processPosition(mapper, x1, y1);
7532 processId(mapper, 1);
7533 processSlot(mapper, 1);
7534 processPosition(mapper, x2, y2);
7535 processId(mapper, 2);
7536 processSync(mapper);
7537
7538 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7539 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7540 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7541 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7542 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7543 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7544 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7545
7546 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007547 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007548 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7549 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7550 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7551 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7552 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7553 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7554 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7555 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7556 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7557
7558 // Move.
7559 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7560 processSlot(mapper, 0);
7561 processPosition(mapper, x1, y1);
7562 processSlot(mapper, 1);
7563 processPosition(mapper, x2, y2);
7564 processSync(mapper);
7565
7566 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7567 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7568 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7569 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7570 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7571 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7572 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7573 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7574 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7575 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7576 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7577
7578 // First finger up.
7579 x2 += 15; y2 -= 20;
7580 processSlot(mapper, 0);
7581 processId(mapper, -1);
7582 processSlot(mapper, 1);
7583 processPosition(mapper, x2, y2);
7584 processSync(mapper);
7585
7586 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007587 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007588 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7589 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7590 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7591 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7592 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7593 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7594 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7595 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7596 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7597
7598 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7599 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7600 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7601 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7602 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7603 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7604 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7605
7606 // Move.
7607 x2 += 20; y2 -= 25;
7608 processPosition(mapper, x2, y2);
7609 processSync(mapper);
7610
7611 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7612 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7613 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7614 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7615 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7616 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7617 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7618
7619 // New finger down.
7620 int32_t x3 = 700, y3 = 300;
7621 processPosition(mapper, x2, y2);
7622 processSlot(mapper, 0);
7623 processId(mapper, 3);
7624 processPosition(mapper, x3, y3);
7625 processSync(mapper);
7626
7627 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007628 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007629 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7630 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7631 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7632 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7633 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7634 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7635 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7636 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7637 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7638
7639 // Second finger up.
7640 x3 += 30; y3 -= 20;
7641 processSlot(mapper, 1);
7642 processId(mapper, -1);
7643 processSlot(mapper, 0);
7644 processPosition(mapper, x3, y3);
7645 processSync(mapper);
7646
7647 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007648 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007649 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7650 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7651 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7652 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7653 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7654 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7655 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7656 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7657 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7658
7659 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7660 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7661 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7662 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7663 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7664 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7665 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7666
7667 // Last finger up.
7668 processId(mapper, -1);
7669 processSync(mapper);
7670
7671 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7672 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7673 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7674 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7675 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7676 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7677 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7678
7679 // Should not have sent any more keys or motions.
7680 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7681 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7682}
7683
7684TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007685 addConfigurationProperty("touch.deviceType", "touchScreen");
7686 prepareDisplay(DISPLAY_ORIENTATION_0);
7687 prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007688 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007689
7690 // These calculations are based on the input device calibration documentation.
7691 int32_t rawX = 100;
7692 int32_t rawY = 200;
7693 int32_t rawTouchMajor = 7;
7694 int32_t rawTouchMinor = 6;
7695 int32_t rawToolMajor = 9;
7696 int32_t rawToolMinor = 8;
7697 int32_t rawPressure = 11;
7698 int32_t rawDistance = 0;
7699 int32_t rawOrientation = 3;
7700 int32_t id = 5;
7701
7702 float x = toDisplayX(rawX);
7703 float y = toDisplayY(rawY);
7704 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
7705 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
7706 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
7707 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
7708 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
7709 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
7710 float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
7711 float distance = float(rawDistance);
7712
7713 processPosition(mapper, rawX, rawY);
7714 processTouchMajor(mapper, rawTouchMajor);
7715 processTouchMinor(mapper, rawTouchMinor);
7716 processToolMajor(mapper, rawToolMajor);
7717 processToolMinor(mapper, rawToolMinor);
7718 processPressure(mapper, rawPressure);
7719 processOrientation(mapper, rawOrientation);
7720 processDistance(mapper, rawDistance);
7721 processId(mapper, id);
7722 processMTSync(mapper);
7723 processSync(mapper);
7724
7725 NotifyMotionArgs args;
7726 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7727 ASSERT_EQ(0, args.pointerProperties[0].id);
7728 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7729 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
7730 orientation, distance));
7731}
7732
7733TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007734 addConfigurationProperty("touch.deviceType", "touchScreen");
7735 prepareDisplay(DISPLAY_ORIENTATION_0);
7736 prepareAxes(POSITION | TOUCH | TOOL | MINOR);
7737 addConfigurationProperty("touch.size.calibration", "geometric");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007738 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007739
7740 // These calculations are based on the input device calibration documentation.
7741 int32_t rawX = 100;
7742 int32_t rawY = 200;
7743 int32_t rawTouchMajor = 140;
7744 int32_t rawTouchMinor = 120;
7745 int32_t rawToolMajor = 180;
7746 int32_t rawToolMinor = 160;
7747
7748 float x = toDisplayX(rawX);
7749 float y = toDisplayY(rawY);
7750 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
7751 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
7752 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
7753 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
7754 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
7755
7756 processPosition(mapper, rawX, rawY);
7757 processTouchMajor(mapper, rawTouchMajor);
7758 processTouchMinor(mapper, rawTouchMinor);
7759 processToolMajor(mapper, rawToolMajor);
7760 processToolMinor(mapper, rawToolMinor);
7761 processMTSync(mapper);
7762 processSync(mapper);
7763
7764 NotifyMotionArgs args;
7765 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7766 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7767 x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
7768}
7769
7770TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007771 addConfigurationProperty("touch.deviceType", "touchScreen");
7772 prepareDisplay(DISPLAY_ORIENTATION_0);
7773 prepareAxes(POSITION | TOUCH | TOOL);
7774 addConfigurationProperty("touch.size.calibration", "diameter");
7775 addConfigurationProperty("touch.size.scale", "10");
7776 addConfigurationProperty("touch.size.bias", "160");
7777 addConfigurationProperty("touch.size.isSummed", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007778 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007779
7780 // These calculations are based on the input device calibration documentation.
7781 // Note: We only provide a single common touch/tool value because the device is assumed
7782 // not to emit separate values for each pointer (isSummed = 1).
7783 int32_t rawX = 100;
7784 int32_t rawY = 200;
7785 int32_t rawX2 = 150;
7786 int32_t rawY2 = 250;
7787 int32_t rawTouchMajor = 5;
7788 int32_t rawToolMajor = 8;
7789
7790 float x = toDisplayX(rawX);
7791 float y = toDisplayY(rawY);
7792 float x2 = toDisplayX(rawX2);
7793 float y2 = toDisplayY(rawY2);
7794 float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
7795 float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
7796 float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
7797
7798 processPosition(mapper, rawX, rawY);
7799 processTouchMajor(mapper, rawTouchMajor);
7800 processToolMajor(mapper, rawToolMajor);
7801 processMTSync(mapper);
7802 processPosition(mapper, rawX2, rawY2);
7803 processTouchMajor(mapper, rawTouchMajor);
7804 processToolMajor(mapper, rawToolMajor);
7805 processMTSync(mapper);
7806 processSync(mapper);
7807
7808 NotifyMotionArgs args;
7809 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7810 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
7811
7812 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007813 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007814 ASSERT_EQ(size_t(2), args.pointerCount);
7815 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7816 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
7817 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
7818 x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
7819}
7820
7821TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007822 addConfigurationProperty("touch.deviceType", "touchScreen");
7823 prepareDisplay(DISPLAY_ORIENTATION_0);
7824 prepareAxes(POSITION | TOUCH | TOOL);
7825 addConfigurationProperty("touch.size.calibration", "area");
7826 addConfigurationProperty("touch.size.scale", "43");
7827 addConfigurationProperty("touch.size.bias", "3");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007828 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007829
7830 // These calculations are based on the input device calibration documentation.
7831 int32_t rawX = 100;
7832 int32_t rawY = 200;
7833 int32_t rawTouchMajor = 5;
7834 int32_t rawToolMajor = 8;
7835
7836 float x = toDisplayX(rawX);
7837 float y = toDisplayY(rawY);
7838 float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
7839 float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
7840 float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
7841
7842 processPosition(mapper, rawX, rawY);
7843 processTouchMajor(mapper, rawTouchMajor);
7844 processToolMajor(mapper, rawToolMajor);
7845 processMTSync(mapper);
7846 processSync(mapper);
7847
7848 NotifyMotionArgs args;
7849 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7850 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7851 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
7852}
7853
7854TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007855 addConfigurationProperty("touch.deviceType", "touchScreen");
7856 prepareDisplay(DISPLAY_ORIENTATION_0);
7857 prepareAxes(POSITION | PRESSURE);
7858 addConfigurationProperty("touch.pressure.calibration", "amplitude");
7859 addConfigurationProperty("touch.pressure.scale", "0.01");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007860 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007861
Michael Wrightaa449c92017-12-13 21:21:43 +00007862 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007863 mapper.populateDeviceInfo(&info);
Michael Wrightaa449c92017-12-13 21:21:43 +00007864 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
7865 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TOUCHSCREEN,
7866 0.0f, RAW_PRESSURE_MAX * 0.01, 0.0f, 0.0f));
7867
Michael Wrightd02c5b62014-02-10 15:10:22 -08007868 // These calculations are based on the input device calibration documentation.
7869 int32_t rawX = 100;
7870 int32_t rawY = 200;
7871 int32_t rawPressure = 60;
7872
7873 float x = toDisplayX(rawX);
7874 float y = toDisplayY(rawY);
7875 float pressure = float(rawPressure) * 0.01f;
7876
7877 processPosition(mapper, rawX, rawY);
7878 processPressure(mapper, rawPressure);
7879 processMTSync(mapper);
7880 processSync(mapper);
7881
7882 NotifyMotionArgs args;
7883 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7884 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7885 x, y, pressure, 0, 0, 0, 0, 0, 0, 0));
7886}
7887
7888TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007889 addConfigurationProperty("touch.deviceType", "touchScreen");
7890 prepareDisplay(DISPLAY_ORIENTATION_0);
7891 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007892 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007893
7894 NotifyMotionArgs motionArgs;
7895 NotifyKeyArgs keyArgs;
7896
7897 processId(mapper, 1);
7898 processPosition(mapper, 100, 200);
7899 processSync(mapper);
7900 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7901 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7902 ASSERT_EQ(0, motionArgs.buttonState);
7903
7904 // press BTN_LEFT, release BTN_LEFT
7905 processKey(mapper, BTN_LEFT, 1);
7906 processSync(mapper);
7907 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7908 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7909 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
7910
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007911 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7912 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7913 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
7914
Michael Wrightd02c5b62014-02-10 15:10:22 -08007915 processKey(mapper, BTN_LEFT, 0);
7916 processSync(mapper);
7917 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007918 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007919 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007920
7921 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007922 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007923 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007924
7925 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
7926 processKey(mapper, BTN_RIGHT, 1);
7927 processKey(mapper, BTN_MIDDLE, 1);
7928 processSync(mapper);
7929 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7930 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7931 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
7932 motionArgs.buttonState);
7933
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007934 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7935 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7936 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
7937
7938 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7939 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7940 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
7941 motionArgs.buttonState);
7942
Michael Wrightd02c5b62014-02-10 15:10:22 -08007943 processKey(mapper, BTN_RIGHT, 0);
7944 processSync(mapper);
7945 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007946 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007947 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007948
7949 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007950 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007951 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007952
7953 processKey(mapper, BTN_MIDDLE, 0);
7954 processSync(mapper);
7955 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007956 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007957 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007958
7959 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007960 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007961 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007962
7963 // press BTN_BACK, release BTN_BACK
7964 processKey(mapper, BTN_BACK, 1);
7965 processSync(mapper);
7966 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7967 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
7968 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007969
Michael Wrightd02c5b62014-02-10 15:10:22 -08007970 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007971 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007972 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
7973
7974 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7975 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7976 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007977
7978 processKey(mapper, BTN_BACK, 0);
7979 processSync(mapper);
7980 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007981 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007982 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007983
7984 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007985 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007986 ASSERT_EQ(0, motionArgs.buttonState);
7987
Michael Wrightd02c5b62014-02-10 15:10:22 -08007988 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7989 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
7990 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
7991
7992 // press BTN_SIDE, release BTN_SIDE
7993 processKey(mapper, BTN_SIDE, 1);
7994 processSync(mapper);
7995 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7996 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
7997 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007998
Michael Wrightd02c5b62014-02-10 15:10:22 -08007999 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008000 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008001 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
8002
8003 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8004 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8005 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008006
8007 processKey(mapper, BTN_SIDE, 0);
8008 processSync(mapper);
8009 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008010 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008011 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008012
8013 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008014 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008015 ASSERT_EQ(0, motionArgs.buttonState);
8016
Michael Wrightd02c5b62014-02-10 15:10:22 -08008017 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8018 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8019 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
8020
8021 // press BTN_FORWARD, release BTN_FORWARD
8022 processKey(mapper, BTN_FORWARD, 1);
8023 processSync(mapper);
8024 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8025 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8026 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008027
Michael Wrightd02c5b62014-02-10 15:10:22 -08008028 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008029 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008030 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
8031
8032 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8033 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8034 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008035
8036 processKey(mapper, BTN_FORWARD, 0);
8037 processSync(mapper);
8038 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008039 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008040 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008041
8042 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008043 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008044 ASSERT_EQ(0, motionArgs.buttonState);
8045
Michael Wrightd02c5b62014-02-10 15:10:22 -08008046 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8047 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8048 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
8049
8050 // press BTN_EXTRA, release BTN_EXTRA
8051 processKey(mapper, BTN_EXTRA, 1);
8052 processSync(mapper);
8053 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8054 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8055 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008056
Michael Wrightd02c5b62014-02-10 15:10:22 -08008057 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008058 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008059 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
8060
8061 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8062 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8063 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008064
8065 processKey(mapper, BTN_EXTRA, 0);
8066 processSync(mapper);
8067 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008068 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008069 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008070
8071 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008072 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008073 ASSERT_EQ(0, motionArgs.buttonState);
8074
Michael Wrightd02c5b62014-02-10 15:10:22 -08008075 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8076 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8077 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
8078
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008079 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8080
Michael Wrightd02c5b62014-02-10 15:10:22 -08008081 // press BTN_STYLUS, release BTN_STYLUS
8082 processKey(mapper, BTN_STYLUS, 1);
8083 processSync(mapper);
8084 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8085 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008086 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
8087
8088 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8089 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8090 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008091
8092 processKey(mapper, BTN_STYLUS, 0);
8093 processSync(mapper);
8094 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008095 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008096 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008097
8098 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008099 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008100 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008101
8102 // press BTN_STYLUS2, release BTN_STYLUS2
8103 processKey(mapper, BTN_STYLUS2, 1);
8104 processSync(mapper);
8105 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8106 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008107 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
8108
8109 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8110 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8111 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008112
8113 processKey(mapper, BTN_STYLUS2, 0);
8114 processSync(mapper);
8115 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008116 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008117 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008118
8119 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008120 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008121 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008122
8123 // release touch
8124 processId(mapper, -1);
8125 processSync(mapper);
8126 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8127 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8128 ASSERT_EQ(0, motionArgs.buttonState);
8129}
8130
8131TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008132 addConfigurationProperty("touch.deviceType", "touchScreen");
8133 prepareDisplay(DISPLAY_ORIENTATION_0);
8134 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008135 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008136
8137 NotifyMotionArgs motionArgs;
8138
8139 // default tool type is finger
8140 processId(mapper, 1);
8141 processPosition(mapper, 100, 200);
8142 processSync(mapper);
8143 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8144 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8145 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8146
8147 // eraser
8148 processKey(mapper, BTN_TOOL_RUBBER, 1);
8149 processSync(mapper);
8150 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8151 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8152 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
8153
8154 // stylus
8155 processKey(mapper, BTN_TOOL_RUBBER, 0);
8156 processKey(mapper, BTN_TOOL_PEN, 1);
8157 processSync(mapper);
8158 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8159 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8160 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8161
8162 // brush
8163 processKey(mapper, BTN_TOOL_PEN, 0);
8164 processKey(mapper, BTN_TOOL_BRUSH, 1);
8165 processSync(mapper);
8166 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8167 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8168 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8169
8170 // pencil
8171 processKey(mapper, BTN_TOOL_BRUSH, 0);
8172 processKey(mapper, BTN_TOOL_PENCIL, 1);
8173 processSync(mapper);
8174 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8175 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8176 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8177
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08008178 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08008179 processKey(mapper, BTN_TOOL_PENCIL, 0);
8180 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
8181 processSync(mapper);
8182 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8183 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8184 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8185
8186 // mouse
8187 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
8188 processKey(mapper, BTN_TOOL_MOUSE, 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_MOUSE, motionArgs.pointerProperties[0].toolType);
8193
8194 // lens
8195 processKey(mapper, BTN_TOOL_MOUSE, 0);
8196 processKey(mapper, BTN_TOOL_LENS, 1);
8197 processSync(mapper);
8198 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8199 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8200 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
8201
8202 // double-tap
8203 processKey(mapper, BTN_TOOL_LENS, 0);
8204 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
8205 processSync(mapper);
8206 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8207 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8208 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8209
8210 // triple-tap
8211 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
8212 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
8213 processSync(mapper);
8214 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8215 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8216 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8217
8218 // quad-tap
8219 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
8220 processKey(mapper, BTN_TOOL_QUADTAP, 1);
8221 processSync(mapper);
8222 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8223 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8224 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8225
8226 // finger
8227 processKey(mapper, BTN_TOOL_QUADTAP, 0);
8228 processKey(mapper, BTN_TOOL_FINGER, 1);
8229 processSync(mapper);
8230 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8231 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8232 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8233
8234 // stylus trumps finger
8235 processKey(mapper, BTN_TOOL_PEN, 1);
8236 processSync(mapper);
8237 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8238 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8239 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8240
8241 // eraser trumps stylus
8242 processKey(mapper, BTN_TOOL_RUBBER, 1);
8243 processSync(mapper);
8244 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8245 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8246 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
8247
8248 // mouse trumps eraser
8249 processKey(mapper, BTN_TOOL_MOUSE, 1);
8250 processSync(mapper);
8251 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8252 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8253 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
8254
8255 // MT tool type trumps BTN tool types: MT_TOOL_FINGER
8256 processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
8257 processSync(mapper);
8258 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8259 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8260 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8261
8262 // MT tool type trumps BTN tool types: MT_TOOL_PEN
8263 processToolType(mapper, MT_TOOL_PEN);
8264 processSync(mapper);
8265 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8266 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8267 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8268
8269 // back to default tool type
8270 processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
8271 processKey(mapper, BTN_TOOL_MOUSE, 0);
8272 processKey(mapper, BTN_TOOL_RUBBER, 0);
8273 processKey(mapper, BTN_TOOL_PEN, 0);
8274 processKey(mapper, BTN_TOOL_FINGER, 0);
8275 processSync(mapper);
8276 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8277 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8278 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8279}
8280
8281TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008282 addConfigurationProperty("touch.deviceType", "touchScreen");
8283 prepareDisplay(DISPLAY_ORIENTATION_0);
8284 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008285 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008286 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008287
8288 NotifyMotionArgs motionArgs;
8289
8290 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
8291 processId(mapper, 1);
8292 processPosition(mapper, 100, 200);
8293 processSync(mapper);
8294 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8295 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8296 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8297 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8298
8299 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8300 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8301 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8302 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8303
8304 // move a little
8305 processPosition(mapper, 150, 250);
8306 processSync(mapper);
8307 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8308 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8309 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8310 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8311
8312 // down when BTN_TOUCH is pressed, pressure defaults to 1
8313 processKey(mapper, BTN_TOUCH, 1);
8314 processSync(mapper);
8315 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8316 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8317 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8318 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8319
8320 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8321 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8322 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8323 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8324
8325 // up when BTN_TOUCH is released, hover restored
8326 processKey(mapper, BTN_TOUCH, 0);
8327 processSync(mapper);
8328 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8329 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8330 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8331 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8332
8333 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8334 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8335 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8336 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8337
8338 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8339 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8340 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8341 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8342
8343 // exit hover when pointer goes away
8344 processId(mapper, -1);
8345 processSync(mapper);
8346 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8347 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8348 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8349 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8350}
8351
8352TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008353 addConfigurationProperty("touch.deviceType", "touchScreen");
8354 prepareDisplay(DISPLAY_ORIENTATION_0);
8355 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008356 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008357
8358 NotifyMotionArgs motionArgs;
8359
8360 // initially hovering because pressure is 0
8361 processId(mapper, 1);
8362 processPosition(mapper, 100, 200);
8363 processPressure(mapper, 0);
8364 processSync(mapper);
8365 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8366 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8367 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8368 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8369
8370 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8371 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8372 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8373 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8374
8375 // move a little
8376 processPosition(mapper, 150, 250);
8377 processSync(mapper);
8378 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8379 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8380 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8381 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8382
8383 // down when pressure becomes non-zero
8384 processPressure(mapper, RAW_PRESSURE_MAX);
8385 processSync(mapper);
8386 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8387 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8388 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8389 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8390
8391 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8392 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8393 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8394 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8395
8396 // up when pressure becomes 0, hover restored
8397 processPressure(mapper, 0);
8398 processSync(mapper);
8399 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8400 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8401 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8402 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8403
8404 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8405 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8406 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8407 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8408
8409 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8410 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8411 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8412 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8413
8414 // exit hover when pointer goes away
8415 processId(mapper, -1);
8416 processSync(mapper);
8417 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8418 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8419 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8420 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8421}
8422
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008423/**
8424 * Set the input device port <--> display port associations, and check that the
8425 * events are routed to the display that matches the display port.
8426 * This can be checked by looking at the displayId of the resulting NotifyMotionArgs.
8427 */
8428TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayPort) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008429 const std::string usb2 = "USB2";
8430 const uint8_t hdmi1 = 0;
8431 const uint8_t hdmi2 = 1;
8432 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008433 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008434
8435 addConfigurationProperty("touch.deviceType", "touchScreen");
8436 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008437 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008438
8439 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
8440 mFakePolicy->addInputPortAssociation(usb2, hdmi2);
8441
8442 // We are intentionally not adding the viewport for display 1 yet. Since the port association
8443 // for this input device is specified, and the matching viewport is not present,
8444 // the input device should be disabled (at the mapper level).
8445
8446 // Add viewport for display 2 on hdmi2
8447 prepareSecondaryDisplay(type, hdmi2);
8448 // Send a touch event
8449 processPosition(mapper, 100, 100);
8450 processSync(mapper);
8451 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8452
8453 // Add viewport for display 1 on hdmi1
8454 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
8455 // Send a touch event again
8456 processPosition(mapper, 100, 100);
8457 processSync(mapper);
8458
8459 NotifyMotionArgs args;
8460 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8461 ASSERT_EQ(DISPLAY_ID, args.displayId);
8462}
Michael Wrightd02c5b62014-02-10 15:10:22 -08008463
Arthur Hung6d5b4b22022-01-21 07:21:10 +00008464TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayUniqueId) {
8465 addConfigurationProperty("touch.deviceType", "touchScreen");
8466 prepareAxes(POSITION);
8467 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8468
8469 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
8470
8471 prepareDisplay(DISPLAY_ORIENTATION_0);
8472 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
8473
8474 // Send a touch event
8475 processPosition(mapper, 100, 100);
8476 processSync(mapper);
8477
8478 NotifyMotionArgs args;
8479 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8480 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
8481}
8482
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008483TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShouldHandleDisplayId) {
Garfield Tan888a6a42020-01-09 11:39:16 -08008484 // Setup for second display.
Michael Wright17db18e2020-06-26 20:51:44 +01008485 std::shared_ptr<FakePointerController> fakePointerController =
8486 std::make_shared<FakePointerController>();
Garfield Tan888a6a42020-01-09 11:39:16 -08008487 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008488 fakePointerController->setPosition(100, 200);
8489 fakePointerController->setButtonState(0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00008490 mFakePolicy->setPointerController(fakePointerController);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008491
Garfield Tan888a6a42020-01-09 11:39:16 -08008492 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008493 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08008494
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008495 prepareDisplay(DISPLAY_ORIENTATION_0);
8496 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008497 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008498
8499 // Check source is mouse that would obtain the PointerController.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008500 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008501
8502 NotifyMotionArgs motionArgs;
8503 processPosition(mapper, 100, 100);
8504 processSync(mapper);
8505
8506 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8507 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8508 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
8509}
8510
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00008511/**
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008512 * Ensure that the readTime is set to the SYN_REPORT value when processing touch events.
8513 */
8514TEST_F(MultiTouchInputMapperTest, Process_SendsReadTime) {
8515 addConfigurationProperty("touch.deviceType", "touchScreen");
8516 prepareAxes(POSITION);
8517 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8518
8519 prepareDisplay(DISPLAY_ORIENTATION_0);
8520 process(mapper, 10, 11 /*readTime*/, EV_ABS, ABS_MT_TRACKING_ID, 1);
8521 process(mapper, 15, 16 /*readTime*/, EV_ABS, ABS_MT_POSITION_X, 100);
8522 process(mapper, 20, 21 /*readTime*/, EV_ABS, ABS_MT_POSITION_Y, 100);
8523 process(mapper, 25, 26 /*readTime*/, EV_SYN, SYN_REPORT, 0);
8524
8525 NotifyMotionArgs args;
8526 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8527 ASSERT_EQ(26, args.readTime);
8528
8529 process(mapper, 30, 31 /*readTime*/, EV_ABS, ABS_MT_POSITION_X, 110);
8530 process(mapper, 30, 32 /*readTime*/, EV_ABS, ABS_MT_POSITION_Y, 220);
8531 process(mapper, 30, 33 /*readTime*/, EV_SYN, SYN_REPORT, 0);
8532
8533 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8534 ASSERT_EQ(33, args.readTime);
8535}
8536
8537/**
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00008538 * When the viewport is not active (isActive=false), the touch mapper should be disabled and the
8539 * events should not be delivered to the listener.
8540 */
8541TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreDropped) {
8542 addConfigurationProperty("touch.deviceType", "touchScreen");
8543 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
8544 DISPLAY_ORIENTATION_0, false /*isActive*/, UNIQUE_ID, NO_PORT,
8545 ViewportType::INTERNAL);
8546 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8547 prepareAxes(POSITION);
8548 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8549
8550 NotifyMotionArgs motionArgs;
8551 processPosition(mapper, 100, 100);
8552 processSync(mapper);
8553
8554 mFakeListener->assertNotifyMotionWasNotCalled();
8555}
8556
Garfield Tanc734e4f2021-01-15 20:01:39 -08008557TEST_F(MultiTouchInputMapperTest, Process_DeactivateViewport_AbortTouches) {
8558 addConfigurationProperty("touch.deviceType", "touchScreen");
8559 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
8560 DISPLAY_ORIENTATION_0, true /*isActive*/, UNIQUE_ID, NO_PORT,
8561 ViewportType::INTERNAL);
8562 std::optional<DisplayViewport> optionalDisplayViewport =
8563 mFakePolicy->getDisplayViewportByUniqueId(UNIQUE_ID);
8564 ASSERT_TRUE(optionalDisplayViewport.has_value());
8565 DisplayViewport displayViewport = *optionalDisplayViewport;
8566
8567 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8568 prepareAxes(POSITION);
8569 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8570
8571 // Finger down
8572 int32_t x = 100, y = 100;
8573 processPosition(mapper, x, y);
8574 processSync(mapper);
8575
8576 NotifyMotionArgs motionArgs;
8577 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8578 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8579
8580 // Deactivate display viewport
8581 displayViewport.isActive = false;
8582 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
8583 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8584
8585 // Finger move
8586 x += 10, y += 10;
8587 processPosition(mapper, x, y);
8588 processSync(mapper);
8589
8590 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8591 EXPECT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
8592
8593 // Reactivate display viewport
8594 displayViewport.isActive = true;
8595 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
8596 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8597
8598 // Finger move again
8599 x += 10, y += 10;
8600 processPosition(mapper, x, y);
8601 processSync(mapper);
8602
8603 // Gesture is aborted, so events after display is activated won't be dispatched until there is
8604 // no pointer on the touch device.
8605 mFakeListener->assertNotifyMotionWasNotCalled();
8606}
8607
Arthur Hung7c645402019-01-25 17:45:42 +08008608TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShowTouches) {
8609 // Setup the first touch screen device.
Arthur Hung7c645402019-01-25 17:45:42 +08008610 prepareAxes(POSITION | ID | SLOT);
8611 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008612 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung7c645402019-01-25 17:45:42 +08008613
8614 // Create the second touch screen device, and enable multi fingers.
8615 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08008616 const std::string DEVICE_NAME2 = "TOUCHSCREEN2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08008617 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008618 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08008619 std::shared_ptr<InputDevice> device2 =
8620 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07008621 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08008622
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008623 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
8624 0 /*flat*/, 0 /*fuzz*/);
8625 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
8626 0 /*flat*/, 0 /*fuzz*/);
8627 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX,
8628 0 /*flat*/, 0 /*fuzz*/);
8629 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX,
8630 0 /*flat*/, 0 /*fuzz*/);
8631 mFakeEventHub->setAbsoluteAxisValue(SECOND_EVENTHUB_ID, ABS_MT_SLOT, 0 /*value*/);
8632 mFakeEventHub->addConfigurationProperty(SECOND_EVENTHUB_ID, String8("touch.deviceType"),
8633 String8("touchScreen"));
Arthur Hung7c645402019-01-25 17:45:42 +08008634
8635 // Setup the second touch screen device.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008636 MultiTouchInputMapper& mapper2 = device2->addMapper<MultiTouchInputMapper>(SECOND_EVENTHUB_ID);
Arthur Hung7c645402019-01-25 17:45:42 +08008637 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
8638 device2->reset(ARBITRARY_TIME);
8639
8640 // Setup PointerController.
Michael Wright17db18e2020-06-26 20:51:44 +01008641 std::shared_ptr<FakePointerController> fakePointerController =
8642 std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00008643 mFakePolicy->setPointerController(fakePointerController);
Arthur Hung7c645402019-01-25 17:45:42 +08008644
8645 // Setup policy for associated displays and show touches.
8646 const uint8_t hdmi1 = 0;
8647 const uint8_t hdmi2 = 1;
8648 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
8649 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
8650 mFakePolicy->setShowTouches(true);
8651
8652 // Create displays.
8653 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008654 prepareSecondaryDisplay(ViewportType::EXTERNAL, hdmi2);
Arthur Hung7c645402019-01-25 17:45:42 +08008655
8656 // Default device will reconfigure above, need additional reconfiguration for another device.
8657 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan197e0862022-07-01 14:28:00 +00008658 InputReaderConfiguration::CHANGE_DISPLAY_INFO |
8659 InputReaderConfiguration::CHANGE_SHOW_TOUCHES);
Arthur Hung7c645402019-01-25 17:45:42 +08008660
8661 // Two fingers down at default display.
8662 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
8663 processPosition(mapper, x1, y1);
8664 processId(mapper, 1);
8665 processSlot(mapper, 1);
8666 processPosition(mapper, x2, y2);
8667 processId(mapper, 2);
8668 processSync(mapper);
8669
8670 std::map<int32_t, std::vector<int32_t>>::const_iterator iter =
8671 fakePointerController->getSpots().find(DISPLAY_ID);
8672 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
8673 ASSERT_EQ(size_t(2), iter->second.size());
8674
8675 // Two fingers down at second display.
8676 processPosition(mapper2, x1, y1);
8677 processId(mapper2, 1);
8678 processSlot(mapper2, 1);
8679 processPosition(mapper2, x2, y2);
8680 processId(mapper2, 2);
8681 processSync(mapper2);
8682
8683 iter = fakePointerController->getSpots().find(SECONDARY_DISPLAY_ID);
8684 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
8685 ASSERT_EQ(size_t(2), iter->second.size());
Prabir Pradhan197e0862022-07-01 14:28:00 +00008686
8687 // Disable the show touches configuration and ensure the spots are cleared.
8688 mFakePolicy->setShowTouches(false);
8689 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
8690 InputReaderConfiguration::CHANGE_SHOW_TOUCHES);
8691
8692 ASSERT_TRUE(fakePointerController->getSpots().empty());
Arthur Hung7c645402019-01-25 17:45:42 +08008693}
8694
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008695TEST_F(MultiTouchInputMapperTest, VideoFrames_ReceivedByListener) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008696 prepareAxes(POSITION);
8697 addConfigurationProperty("touch.deviceType", "touchScreen");
8698 prepareDisplay(DISPLAY_ORIENTATION_0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008699 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008700
8701 NotifyMotionArgs motionArgs;
8702 // Unrotated video frame
8703 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8704 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008705 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008706 processPosition(mapper, 100, 200);
8707 processSync(mapper);
8708 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8709 ASSERT_EQ(frames, motionArgs.videoFrames);
8710
8711 // Subsequent touch events should not have any videoframes
8712 // This is implemented separately in FakeEventHub,
8713 // but that should match the behaviour of TouchVideoDevice.
8714 processPosition(mapper, 200, 200);
8715 processSync(mapper);
8716 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8717 ASSERT_EQ(std::vector<TouchVideoFrame>(), motionArgs.videoFrames);
8718}
8719
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008720TEST_F(MultiTouchInputMapperTest, VideoFrames_AreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008721 prepareAxes(POSITION);
8722 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008723 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008724 // Unrotated video frame
8725 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8726 NotifyMotionArgs motionArgs;
8727
8728 // Test all 4 orientations
8729 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008730 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
8731 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
8732 clearViewports();
8733 prepareDisplay(orientation);
8734 std::vector<TouchVideoFrame> frames{frame};
8735 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
8736 processPosition(mapper, 100, 200);
8737 processSync(mapper);
8738 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8739 ASSERT_EQ(frames, motionArgs.videoFrames);
8740 }
8741}
8742
8743TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_AreRotated) {
8744 prepareAxes(POSITION);
8745 addConfigurationProperty("touch.deviceType", "touchScreen");
8746 // Since InputReader works in the un-rotated coordinate space, only devices that are not
8747 // orientation-aware are affected by display rotation.
8748 addConfigurationProperty("touch.orientationAware", "0");
8749 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8750 // Unrotated video frame
8751 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8752 NotifyMotionArgs motionArgs;
8753
8754 // Test all 4 orientations
8755 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008756 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
8757 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
8758 clearViewports();
8759 prepareDisplay(orientation);
8760 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008761 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008762 processPosition(mapper, 100, 200);
8763 processSync(mapper);
8764 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008765 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
8766 // compared to the display. This is so that when the window transform (which contains the
8767 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
8768 // window's coordinate space.
8769 frames[0].rotate(getInverseRotation(orientation));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008770 ASSERT_EQ(frames, motionArgs.videoFrames);
lilinnane34bc532022-07-19 16:00:50 +08008771
8772 // Release finger.
8773 processSync(mapper);
8774 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008775 }
8776}
8777
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008778TEST_F(MultiTouchInputMapperTest, VideoFrames_MultipleFramesAreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008779 prepareAxes(POSITION);
8780 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008781 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008782 // Unrotated video frames. There's no rule that they must all have the same dimensions,
8783 // so mix these.
8784 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8785 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
8786 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
8787 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
8788 NotifyMotionArgs motionArgs;
8789
8790 prepareDisplay(DISPLAY_ORIENTATION_90);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008791 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008792 processPosition(mapper, 100, 200);
8793 processSync(mapper);
8794 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008795 ASSERT_EQ(frames, motionArgs.videoFrames);
8796}
8797
8798TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_MultipleFramesAreRotated) {
8799 prepareAxes(POSITION);
8800 addConfigurationProperty("touch.deviceType", "touchScreen");
8801 // Since InputReader works in the un-rotated coordinate space, only devices that are not
8802 // orientation-aware are affected by display rotation.
8803 addConfigurationProperty("touch.orientationAware", "0");
8804 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8805 // Unrotated video frames. There's no rule that they must all have the same dimensions,
8806 // so mix these.
8807 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8808 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
8809 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
8810 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
8811 NotifyMotionArgs motionArgs;
8812
8813 prepareDisplay(DISPLAY_ORIENTATION_90);
8814 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
8815 processPosition(mapper, 100, 200);
8816 processSync(mapper);
8817 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8818 std::for_each(frames.begin(), frames.end(), [](TouchVideoFrame& frame) {
8819 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
8820 // compared to the display. This is so that when the window transform (which contains the
8821 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
8822 // window's coordinate space.
8823 frame.rotate(getInverseRotation(DISPLAY_ORIENTATION_90));
8824 });
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008825 ASSERT_EQ(frames, motionArgs.videoFrames);
8826}
8827
Arthur Hung9da14732019-09-02 16:16:58 +08008828/**
8829 * If we had defined port associations, but the viewport is not ready, the touch device would be
8830 * expected to be disabled, and it should be enabled after the viewport has found.
8831 */
8832TEST_F(MultiTouchInputMapperTest, Configure_EnabledForAssociatedDisplay) {
Arthur Hung9da14732019-09-02 16:16:58 +08008833 constexpr uint8_t hdmi2 = 1;
8834 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008835 constexpr ViewportType type = ViewportType::EXTERNAL;
Arthur Hung9da14732019-09-02 16:16:58 +08008836
8837 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi2);
8838
8839 addConfigurationProperty("touch.deviceType", "touchScreen");
8840 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008841 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung9da14732019-09-02 16:16:58 +08008842
8843 ASSERT_EQ(mDevice->isEnabled(), false);
8844
8845 // Add display on hdmi2, the device should be enabled and can receive touch event.
8846 prepareSecondaryDisplay(type, hdmi2);
8847 ASSERT_EQ(mDevice->isEnabled(), true);
8848
8849 // Send a touch event.
8850 processPosition(mapper, 100, 100);
8851 processSync(mapper);
8852
8853 NotifyMotionArgs args;
8854 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8855 ASSERT_EQ(SECONDARY_DISPLAY_ID, args.displayId);
8856}
8857
Arthur Hung421eb1c2020-01-16 00:09:42 +08008858TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleSingleTouch) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08008859 addConfigurationProperty("touch.deviceType", "touchScreen");
8860 prepareDisplay(DISPLAY_ORIENTATION_0);
8861 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008862 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08008863
8864 NotifyMotionArgs motionArgs;
8865
8866 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
8867 // finger down
8868 processId(mapper, 1);
8869 processPosition(mapper, x1, y1);
8870 processSync(mapper);
8871 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8872 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8873 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8874
8875 // finger move
8876 processId(mapper, 1);
8877 processPosition(mapper, x2, y2);
8878 processSync(mapper);
8879 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8880 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8881 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8882
8883 // finger up.
8884 processId(mapper, -1);
8885 processSync(mapper);
8886 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8887 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8888 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8889
8890 // new finger down
8891 processId(mapper, 1);
8892 processPosition(mapper, x3, y3);
8893 processSync(mapper);
8894 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8895 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8896 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8897}
8898
8899/**
arthurhungcc7f9802020-04-30 17:55:40 +08008900 * Test single touch should be canceled when received the MT_TOOL_PALM event, and the following
8901 * MOVE and UP events should be ignored.
Arthur Hung421eb1c2020-01-16 00:09:42 +08008902 */
arthurhungcc7f9802020-04-30 17:55:40 +08008903TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_SinglePointer) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08008904 addConfigurationProperty("touch.deviceType", "touchScreen");
8905 prepareDisplay(DISPLAY_ORIENTATION_0);
8906 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008907 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08008908
8909 NotifyMotionArgs motionArgs;
8910
8911 // default tool type is finger
8912 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
arthurhungcc7f9802020-04-30 17:55:40 +08008913 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008914 processPosition(mapper, x1, y1);
8915 processSync(mapper);
8916 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8917 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8918 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8919
8920 // Tool changed to MT_TOOL_PALM expect sending the cancel event.
8921 processToolType(mapper, MT_TOOL_PALM);
8922 processSync(mapper);
8923 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8924 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
8925
8926 // Ignore the following MOVE and UP events if had detect a palm event.
arthurhungcc7f9802020-04-30 17:55:40 +08008927 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008928 processPosition(mapper, x2, y2);
8929 processSync(mapper);
8930 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8931
8932 // finger up.
arthurhungcc7f9802020-04-30 17:55:40 +08008933 processId(mapper, INVALID_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008934 processSync(mapper);
8935 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8936
8937 // new finger down
arthurhungcc7f9802020-04-30 17:55:40 +08008938 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008939 processToolType(mapper, MT_TOOL_FINGER);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008940 processPosition(mapper, x3, y3);
8941 processSync(mapper);
8942 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8943 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8944 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8945}
8946
arthurhungbf89a482020-04-17 17:37:55 +08008947/**
arthurhungcc7f9802020-04-30 17:55:40 +08008948 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
8949 * and the rest active fingers could still be allowed to receive the events
arthurhungbf89a482020-04-17 17:37:55 +08008950 */
arthurhungcc7f9802020-04-30 17:55:40 +08008951TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_TwoPointers) {
arthurhungbf89a482020-04-17 17:37:55 +08008952 addConfigurationProperty("touch.deviceType", "touchScreen");
8953 prepareDisplay(DISPLAY_ORIENTATION_0);
8954 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
8955 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8956
8957 NotifyMotionArgs motionArgs;
8958
8959 // default tool type is finger
arthurhungcc7f9802020-04-30 17:55:40 +08008960 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
8961 processId(mapper, FIRST_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08008962 processPosition(mapper, x1, y1);
8963 processSync(mapper);
8964 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8965 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8966 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8967
8968 // Second finger down.
arthurhungcc7f9802020-04-30 17:55:40 +08008969 processSlot(mapper, SECOND_SLOT);
8970 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08008971 processPosition(mapper, x2, y2);
arthurhungcc7f9802020-04-30 17:55:40 +08008972 processSync(mapper);
8973 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008974 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08008975 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8976
8977 // If the tool type of the first finger changes to MT_TOOL_PALM,
8978 // we expect to receive ACTION_POINTER_UP with cancel flag.
8979 processSlot(mapper, FIRST_SLOT);
8980 processId(mapper, FIRST_TRACKING_ID);
8981 processToolType(mapper, MT_TOOL_PALM);
8982 processSync(mapper);
8983 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008984 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08008985 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
8986
8987 // The following MOVE events of second finger should be processed.
8988 processSlot(mapper, SECOND_SLOT);
8989 processId(mapper, SECOND_TRACKING_ID);
8990 processPosition(mapper, x2 + 1, y2 + 1);
8991 processSync(mapper);
8992 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8993 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8994 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8995
8996 // First finger up. It used to be in palm mode, and we already generated ACTION_POINTER_UP for
8997 // it. Second finger receive move.
8998 processSlot(mapper, FIRST_SLOT);
8999 processId(mapper, INVALID_TRACKING_ID);
9000 processSync(mapper);
9001 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9002 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9003 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9004
9005 // Second finger keeps moving.
9006 processSlot(mapper, SECOND_SLOT);
9007 processId(mapper, SECOND_TRACKING_ID);
9008 processPosition(mapper, x2 + 2, y2 + 2);
9009 processSync(mapper);
9010 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9011 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9012 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9013
9014 // Second finger up.
9015 processId(mapper, INVALID_TRACKING_ID);
9016 processSync(mapper);
9017 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9018 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9019 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9020}
9021
9022/**
9023 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event, if only 1 finger
9024 * is active, it should send CANCEL after receiving the MT_TOOL_PALM event.
9025 */
9026TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_ShouldCancelWhenAllTouchIsPalm) {
9027 addConfigurationProperty("touch.deviceType", "touchScreen");
9028 prepareDisplay(DISPLAY_ORIENTATION_0);
9029 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
9030 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9031
9032 NotifyMotionArgs motionArgs;
9033
9034 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
9035 // First finger down.
9036 processId(mapper, FIRST_TRACKING_ID);
9037 processPosition(mapper, x1, y1);
9038 processSync(mapper);
9039 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9040 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9041 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9042
9043 // Second finger down.
9044 processSlot(mapper, SECOND_SLOT);
9045 processId(mapper, SECOND_TRACKING_ID);
9046 processPosition(mapper, x2, y2);
arthurhungbf89a482020-04-17 17:37:55 +08009047 processSync(mapper);
9048 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009049 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungbf89a482020-04-17 17:37:55 +08009050 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9051
arthurhungcc7f9802020-04-30 17:55:40 +08009052 // If the tool type of the first finger changes to MT_TOOL_PALM,
9053 // we expect to receive ACTION_POINTER_UP with cancel flag.
9054 processSlot(mapper, FIRST_SLOT);
9055 processId(mapper, FIRST_TRACKING_ID);
9056 processToolType(mapper, MT_TOOL_PALM);
9057 processSync(mapper);
9058 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009059 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009060 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9061
9062 // Second finger keeps moving.
9063 processSlot(mapper, SECOND_SLOT);
9064 processId(mapper, SECOND_TRACKING_ID);
9065 processPosition(mapper, x2 + 1, y2 + 1);
9066 processSync(mapper);
9067 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9068 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9069
9070 // second finger becomes palm, receive cancel due to only 1 finger is active.
9071 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009072 processToolType(mapper, MT_TOOL_PALM);
9073 processSync(mapper);
9074 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9075 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9076
arthurhungcc7f9802020-04-30 17:55:40 +08009077 // third finger down.
9078 processSlot(mapper, THIRD_SLOT);
9079 processId(mapper, THIRD_TRACKING_ID);
9080 processToolType(mapper, MT_TOOL_FINGER);
arthurhungbf89a482020-04-17 17:37:55 +08009081 processPosition(mapper, x3, y3);
9082 processSync(mapper);
arthurhungbf89a482020-04-17 17:37:55 +08009083 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9084 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9085 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08009086 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9087
9088 // third finger move
9089 processId(mapper, THIRD_TRACKING_ID);
9090 processPosition(mapper, x3 + 1, y3 + 1);
9091 processSync(mapper);
9092 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9093 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9094
9095 // first finger up, third finger receive move.
9096 processSlot(mapper, FIRST_SLOT);
9097 processId(mapper, INVALID_TRACKING_ID);
9098 processSync(mapper);
9099 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9100 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9101 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9102
9103 // second finger up, third finger receive move.
9104 processSlot(mapper, SECOND_SLOT);
9105 processId(mapper, INVALID_TRACKING_ID);
9106 processSync(mapper);
9107 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9108 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9109 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9110
9111 // third finger up.
9112 processSlot(mapper, THIRD_SLOT);
9113 processId(mapper, INVALID_TRACKING_ID);
9114 processSync(mapper);
9115 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9116 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9117 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9118}
9119
9120/**
9121 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
9122 * and the active finger could still be allowed to receive the events
9123 */
9124TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_KeepFirstPointer) {
9125 addConfigurationProperty("touch.deviceType", "touchScreen");
9126 prepareDisplay(DISPLAY_ORIENTATION_0);
9127 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
9128 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9129
9130 NotifyMotionArgs motionArgs;
9131
9132 // default tool type is finger
9133 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
9134 processId(mapper, FIRST_TRACKING_ID);
9135 processPosition(mapper, x1, y1);
9136 processSync(mapper);
9137 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9138 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9139 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9140
9141 // Second finger down.
9142 processSlot(mapper, SECOND_SLOT);
9143 processId(mapper, SECOND_TRACKING_ID);
9144 processPosition(mapper, x2, y2);
9145 processSync(mapper);
9146 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009147 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009148 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9149
9150 // If the tool type of the second finger changes to MT_TOOL_PALM,
9151 // we expect to receive ACTION_POINTER_UP with cancel flag.
9152 processId(mapper, SECOND_TRACKING_ID);
9153 processToolType(mapper, MT_TOOL_PALM);
9154 processSync(mapper);
9155 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009156 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009157 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9158
9159 // The following MOVE event should be processed.
9160 processSlot(mapper, FIRST_SLOT);
9161 processId(mapper, FIRST_TRACKING_ID);
9162 processPosition(mapper, x1 + 1, y1 + 1);
9163 processSync(mapper);
9164 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9165 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9166 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9167
9168 // second finger up.
9169 processSlot(mapper, SECOND_SLOT);
9170 processId(mapper, INVALID_TRACKING_ID);
9171 processSync(mapper);
9172 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9173 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9174
9175 // first finger keep moving
9176 processSlot(mapper, FIRST_SLOT);
9177 processId(mapper, FIRST_TRACKING_ID);
9178 processPosition(mapper, x1 + 2, y1 + 2);
9179 processSync(mapper);
9180 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9181 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9182
9183 // first finger up.
9184 processId(mapper, INVALID_TRACKING_ID);
9185 processSync(mapper);
9186 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9187 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9188 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
arthurhungbf89a482020-04-17 17:37:55 +08009189}
9190
Arthur Hung9ad18942021-06-19 02:04:46 +00009191/**
9192 * Test multi-touch should sent ACTION_POINTER_UP/ACTION_UP when received the INVALID_TRACKING_ID,
9193 * to prevent the driver side may send unexpected data after set tracking id as INVALID_TRACKING_ID
9194 * cause slot be valid again.
9195 */
9196TEST_F(MultiTouchInputMapperTest, Process_MultiTouch_WithInvalidTrackingId) {
9197 addConfigurationProperty("touch.deviceType", "touchScreen");
9198 prepareDisplay(DISPLAY_ORIENTATION_0);
9199 prepareAxes(POSITION | ID | SLOT | PRESSURE);
9200 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9201
9202 NotifyMotionArgs motionArgs;
9203
9204 constexpr int32_t x1 = 100, y1 = 200, x2 = 0, y2 = 0;
9205 // First finger down.
9206 processId(mapper, FIRST_TRACKING_ID);
9207 processPosition(mapper, x1, y1);
9208 processPressure(mapper, RAW_PRESSURE_MAX);
9209 processSync(mapper);
9210 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9211 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9212 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9213
9214 // First finger move.
9215 processId(mapper, FIRST_TRACKING_ID);
9216 processPosition(mapper, x1 + 1, y1 + 1);
9217 processPressure(mapper, RAW_PRESSURE_MAX);
9218 processSync(mapper);
9219 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9220 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9221 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9222
9223 // Second finger down.
9224 processSlot(mapper, SECOND_SLOT);
9225 processId(mapper, SECOND_TRACKING_ID);
9226 processPosition(mapper, x2, y2);
9227 processPressure(mapper, RAW_PRESSURE_MAX);
9228 processSync(mapper);
9229 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009230 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Arthur Hung9ad18942021-06-19 02:04:46 +00009231 ASSERT_EQ(uint32_t(2), motionArgs.pointerCount);
9232
9233 // second finger up with some unexpected data.
9234 processSlot(mapper, SECOND_SLOT);
9235 processId(mapper, INVALID_TRACKING_ID);
9236 processPosition(mapper, x2, y2);
9237 processSync(mapper);
9238 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009239 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Arthur Hung9ad18942021-06-19 02:04:46 +00009240 ASSERT_EQ(uint32_t(2), motionArgs.pointerCount);
9241
9242 // first finger up with some unexpected data.
9243 processSlot(mapper, FIRST_SLOT);
9244 processId(mapper, INVALID_TRACKING_ID);
9245 processPosition(mapper, x2, y2);
9246 processPressure(mapper, RAW_PRESSURE_MAX);
9247 processSync(mapper);
9248 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9249 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9250 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9251}
9252
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009253// --- MultiTouchInputMapperTest_ExternalDevice ---
9254
9255class MultiTouchInputMapperTest_ExternalDevice : public MultiTouchInputMapperTest {
9256protected:
Chris Yea52ade12020-08-27 16:49:20 -07009257 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009258};
9259
9260/**
9261 * Expect fallback to internal viewport if device is external and external viewport is not present.
9262 */
9263TEST_F(MultiTouchInputMapperTest_ExternalDevice, Viewports_Fallback) {
9264 prepareAxes(POSITION);
9265 addConfigurationProperty("touch.deviceType", "touchScreen");
9266 prepareDisplay(DISPLAY_ORIENTATION_0);
9267 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9268
9269 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
9270
9271 NotifyMotionArgs motionArgs;
9272
9273 // Expect the event to be sent to the internal viewport,
9274 // because an external viewport is not present.
9275 processPosition(mapper, 100, 100);
9276 processSync(mapper);
9277 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9278 ASSERT_EQ(ADISPLAY_ID_DEFAULT, motionArgs.displayId);
9279
9280 // Expect the event to be sent to the external viewport if it is present.
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009281 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009282 processPosition(mapper, 100, 100);
9283 processSync(mapper);
9284 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9285 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
9286}
Arthur Hung4197f6b2020-03-16 15:39:59 +08009287
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009288TEST_F(MultiTouchInputMapperTest, Process_TouchpadCapture) {
9289 // we need a pointer controller for mouse mode of touchpad (start pointer at 0,0)
9290 std::shared_ptr<FakePointerController> fakePointerController =
9291 std::make_shared<FakePointerController>();
9292 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
9293 fakePointerController->setPosition(0, 0);
9294 fakePointerController->setButtonState(0);
9295
9296 // prepare device and capture
9297 prepareDisplay(DISPLAY_ORIENTATION_0);
9298 prepareAxes(POSITION | ID | SLOT);
9299 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
9300 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
9301 mFakePolicy->setPointerCapture(true);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009302 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009303 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9304
9305 // captured touchpad should be a touchpad source
9306 NotifyDeviceResetArgs resetArgs;
9307 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
9308 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
9309
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009310 InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
Chris Yef74dc422020-09-02 22:41:50 -07009311
9312 const InputDeviceInfo::MotionRange* relRangeX =
9313 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_X, AINPUT_SOURCE_TOUCHPAD);
9314 ASSERT_NE(relRangeX, nullptr);
9315 ASSERT_EQ(relRangeX->min, -(RAW_X_MAX - RAW_X_MIN));
9316 ASSERT_EQ(relRangeX->max, RAW_X_MAX - RAW_X_MIN);
9317 const InputDeviceInfo::MotionRange* relRangeY =
9318 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_Y, AINPUT_SOURCE_TOUCHPAD);
9319 ASSERT_NE(relRangeY, nullptr);
9320 ASSERT_EQ(relRangeY->min, -(RAW_Y_MAX - RAW_Y_MIN));
9321 ASSERT_EQ(relRangeY->max, RAW_Y_MAX - RAW_Y_MIN);
9322
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009323 // run captured pointer tests - note that this is unscaled, so input listener events should be
9324 // identical to what the hardware sends (accounting for any
9325 // calibration).
9326 // FINGER 0 DOWN
Chris Ye364fdb52020-08-05 15:07:56 -07009327 processSlot(mapper, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009328 processId(mapper, 1);
9329 processPosition(mapper, 100 + RAW_X_MIN, 100 + RAW_Y_MIN);
9330 processKey(mapper, BTN_TOUCH, 1);
9331 processSync(mapper);
9332
9333 // expect coord[0] to contain initial location of touch 0
9334 NotifyMotionArgs args;
9335 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9336 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
9337 ASSERT_EQ(1U, args.pointerCount);
9338 ASSERT_EQ(0, args.pointerProperties[0].id);
9339 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, args.source);
9340 ASSERT_NO_FATAL_FAILURE(
9341 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
9342
9343 // FINGER 1 DOWN
9344 processSlot(mapper, 1);
9345 processId(mapper, 2);
9346 processPosition(mapper, 560 + RAW_X_MIN, 154 + RAW_Y_MIN);
9347 processSync(mapper);
9348
9349 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
9350 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009351 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009352 ASSERT_EQ(2U, args.pointerCount);
9353 ASSERT_EQ(0, args.pointerProperties[0].id);
9354 ASSERT_EQ(1, args.pointerProperties[1].id);
9355 ASSERT_NO_FATAL_FAILURE(
9356 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
9357 ASSERT_NO_FATAL_FAILURE(
9358 assertPointerCoords(args.pointerCoords[1], 560, 154, 1, 0, 0, 0, 0, 0, 0, 0));
9359
9360 // FINGER 1 MOVE
9361 processPosition(mapper, 540 + RAW_X_MIN, 690 + RAW_Y_MIN);
9362 processSync(mapper);
9363
9364 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
9365 // from move
9366 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9367 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9368 ASSERT_NO_FATAL_FAILURE(
9369 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
9370 ASSERT_NO_FATAL_FAILURE(
9371 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
9372
9373 // FINGER 0 MOVE
9374 processSlot(mapper, 0);
9375 processPosition(mapper, 50 + RAW_X_MIN, 800 + RAW_Y_MIN);
9376 processSync(mapper);
9377
9378 // expect coord[0] to contain new touch 0 location, coord[1] to contain previous location
9379 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9380 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9381 ASSERT_NO_FATAL_FAILURE(
9382 assertPointerCoords(args.pointerCoords[0], 50, 800, 1, 0, 0, 0, 0, 0, 0, 0));
9383 ASSERT_NO_FATAL_FAILURE(
9384 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
9385
9386 // BUTTON DOWN
9387 processKey(mapper, BTN_LEFT, 1);
9388 processSync(mapper);
9389
9390 // touchinputmapper design sends a move before button press
9391 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9392 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9393 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9394 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
9395
9396 // BUTTON UP
9397 processKey(mapper, BTN_LEFT, 0);
9398 processSync(mapper);
9399
9400 // touchinputmapper design sends a move after button release
9401 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9402 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
9403 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9404 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9405
9406 // FINGER 0 UP
9407 processId(mapper, -1);
9408 processSync(mapper);
9409 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9410 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | 0x0000, args.action);
9411
9412 // FINGER 1 MOVE
9413 processSlot(mapper, 1);
9414 processPosition(mapper, 320 + RAW_X_MIN, 900 + RAW_Y_MIN);
9415 processSync(mapper);
9416
9417 // expect coord[0] to contain new location of touch 1, and properties[0].id to contain 1
9418 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9419 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9420 ASSERT_EQ(1U, args.pointerCount);
9421 ASSERT_EQ(1, args.pointerProperties[0].id);
9422 ASSERT_NO_FATAL_FAILURE(
9423 assertPointerCoords(args.pointerCoords[0], 320, 900, 1, 0, 0, 0, 0, 0, 0, 0));
9424
9425 // FINGER 1 UP
9426 processId(mapper, -1);
9427 processKey(mapper, BTN_TOUCH, 0);
9428 processSync(mapper);
9429 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9430 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
9431
9432 // non captured touchpad should be a mouse source
9433 mFakePolicy->setPointerCapture(false);
9434 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
9435 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
9436 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
9437}
9438
9439TEST_F(MultiTouchInputMapperTest, Process_UnCapturedTouchpadPointer) {
9440 std::shared_ptr<FakePointerController> fakePointerController =
9441 std::make_shared<FakePointerController>();
9442 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
9443 fakePointerController->setPosition(0, 0);
9444 fakePointerController->setButtonState(0);
9445
9446 // prepare device and capture
9447 prepareDisplay(DISPLAY_ORIENTATION_0);
9448 prepareAxes(POSITION | ID | SLOT);
9449 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
9450 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009451 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009452 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9453 // run uncaptured pointer tests - pushes out generic events
9454 // FINGER 0 DOWN
9455 processId(mapper, 3);
9456 processPosition(mapper, 100, 100);
9457 processKey(mapper, BTN_TOUCH, 1);
9458 processSync(mapper);
9459
9460 // start at (100,100), cursor should be at (0,0) * scale
9461 NotifyMotionArgs args;
9462 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9463 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
9464 ASSERT_NO_FATAL_FAILURE(
9465 assertPointerCoords(args.pointerCoords[0], 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
9466
9467 // FINGER 0 MOVE
9468 processPosition(mapper, 200, 200);
9469 processSync(mapper);
9470
9471 // compute scaling to help with touch position checking
9472 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
9473 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
9474 float scale =
9475 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
9476
9477 // translate from (100,100) -> (200,200), cursor should have changed to (100,100) * scale)
9478 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9479 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
9480 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 100 * scale, 100 * scale, 0,
9481 0, 0, 0, 0, 0, 0, 0));
9482}
9483
9484TEST_F(MultiTouchInputMapperTest, WhenCapturedAndNotCaptured_GetSources) {
9485 std::shared_ptr<FakePointerController> fakePointerController =
9486 std::make_shared<FakePointerController>();
9487
9488 prepareDisplay(DISPLAY_ORIENTATION_0);
9489 prepareAxes(POSITION | ID | SLOT);
9490 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009491 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009492 mFakePolicy->setPointerCapture(false);
9493 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9494
9495 // uncaptured touchpad should be a pointer device
9496 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
9497
9498 // captured touchpad should be a touchpad device
9499 mFakePolicy->setPointerCapture(true);
9500 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
9501 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
9502}
9503
Arthur Hung6d5b4b22022-01-21 07:21:10 +00009504// --- JoystickInputMapperTest ---
9505
9506class JoystickInputMapperTest : public InputMapperTest {
9507protected:
9508 static const int32_t RAW_X_MIN;
9509 static const int32_t RAW_X_MAX;
9510 static const int32_t RAW_Y_MIN;
9511 static const int32_t RAW_Y_MAX;
9512
9513 void SetUp() override {
9514 InputMapperTest::SetUp(InputDeviceClass::JOYSTICK | InputDeviceClass::EXTERNAL);
9515 }
9516 void prepareAxes() {
9517 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
9518 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
9519 }
9520
9521 void processAxis(JoystickInputMapper& mapper, int32_t axis, int32_t value) {
9522 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, axis, value);
9523 }
9524
9525 void processSync(JoystickInputMapper& mapper) {
9526 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
9527 }
9528
9529 void prepareVirtualDisplay(int32_t orientation) {
9530 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH,
9531 VIRTUAL_DISPLAY_HEIGHT, orientation, VIRTUAL_DISPLAY_UNIQUE_ID,
9532 NO_PORT, ViewportType::VIRTUAL);
9533 }
9534};
9535
9536const int32_t JoystickInputMapperTest::RAW_X_MIN = -32767;
9537const int32_t JoystickInputMapperTest::RAW_X_MAX = 32767;
9538const int32_t JoystickInputMapperTest::RAW_Y_MIN = -32767;
9539const int32_t JoystickInputMapperTest::RAW_Y_MAX = 32767;
9540
9541TEST_F(JoystickInputMapperTest, Configure_AssignsDisplayUniqueId) {
9542 prepareAxes();
9543 JoystickInputMapper& mapper = addMapperAndConfigure<JoystickInputMapper>();
9544
9545 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
9546
9547 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
9548
9549 // Send an axis event
9550 processAxis(mapper, ABS_X, 100);
9551 processSync(mapper);
9552
9553 NotifyMotionArgs args;
9554 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9555 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
9556
9557 // Send another axis event
9558 processAxis(mapper, ABS_Y, 100);
9559 processSync(mapper);
9560
9561 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9562 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
9563}
9564
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009565// --- PeripheralControllerTest ---
Chris Yee2b1e5c2021-03-10 22:45:12 -08009566
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009567class PeripheralControllerTest : public testing::Test {
Chris Yee2b1e5c2021-03-10 22:45:12 -08009568protected:
9569 static const char* DEVICE_NAME;
9570 static const char* DEVICE_LOCATION;
9571 static const int32_t DEVICE_ID;
9572 static const int32_t DEVICE_GENERATION;
9573 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07009574 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Chris Yee2b1e5c2021-03-10 22:45:12 -08009575 static const int32_t EVENTHUB_ID;
9576
9577 std::shared_ptr<FakeEventHub> mFakeEventHub;
9578 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07009579 std::unique_ptr<TestInputListener> mFakeListener;
Chris Yee2b1e5c2021-03-10 22:45:12 -08009580 std::unique_ptr<InstrumentedInputReader> mReader;
9581 std::shared_ptr<InputDevice> mDevice;
9582
Dominik Laskowski2f01d772022-03-23 16:01:29 -07009583 virtual void SetUp(ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -08009584 mFakeEventHub = std::make_unique<FakeEventHub>();
9585 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07009586 mFakeListener = std::make_unique<TestInputListener>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009587 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07009588 *mFakeListener);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009589 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
9590 }
9591
9592 void SetUp() override { SetUp(DEVICE_CLASSES); }
9593
9594 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07009595 mFakeListener.reset();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009596 mFakePolicy.clear();
9597 }
9598
9599 void configureDevice(uint32_t changes) {
9600 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
9601 mReader->requestRefreshConfiguration(changes);
9602 mReader->loopOnce();
9603 }
9604 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
9605 }
9606
9607 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
9608 const std::string& location, int32_t eventHubId,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07009609 ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -08009610 InputDeviceIdentifier identifier;
9611 identifier.name = name;
9612 identifier.location = location;
9613 std::shared_ptr<InputDevice> device =
9614 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
9615 identifier);
9616 mReader->pushNextDevice(device);
9617 mFakeEventHub->addDevice(eventHubId, name, classes);
9618 mReader->loopOnce();
9619 return device;
9620 }
9621
9622 template <class T, typename... Args>
9623 T& addControllerAndConfigure(Args... args) {
9624 T& controller = mDevice->addController<T>(EVENTHUB_ID, args...);
9625
9626 return controller;
9627 }
9628};
9629
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009630const char* PeripheralControllerTest::DEVICE_NAME = "device";
9631const char* PeripheralControllerTest::DEVICE_LOCATION = "BLUETOOTH";
9632const int32_t PeripheralControllerTest::DEVICE_ID = END_RESERVED_ID + 1000;
9633const int32_t PeripheralControllerTest::DEVICE_GENERATION = 2;
9634const int32_t PeripheralControllerTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07009635const ftl::Flags<InputDeviceClass> PeripheralControllerTest::DEVICE_CLASSES =
9636 ftl::Flags<InputDeviceClass>(0); // not needed for current tests
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009637const int32_t PeripheralControllerTest::EVENTHUB_ID = 1;
Chris Yee2b1e5c2021-03-10 22:45:12 -08009638
9639// --- BatteryControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009640class BatteryControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -08009641protected:
9642 void SetUp() override {
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009643 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::BATTERY);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009644 }
9645};
9646
9647TEST_F(BatteryControllerTest, GetBatteryCapacity) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009648 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009649
9650 ASSERT_TRUE(controller.getBatteryCapacity(DEFAULT_BATTERY));
9651 ASSERT_EQ(controller.getBatteryCapacity(DEFAULT_BATTERY).value_or(-1), BATTERY_CAPACITY);
9652}
9653
9654TEST_F(BatteryControllerTest, GetBatteryStatus) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009655 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009656
9657 ASSERT_TRUE(controller.getBatteryStatus(DEFAULT_BATTERY));
9658 ASSERT_EQ(controller.getBatteryStatus(DEFAULT_BATTERY).value_or(-1), BATTERY_STATUS);
9659}
9660
9661// --- LightControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009662class LightControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -08009663protected:
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009664 void SetUp() override {
9665 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::LIGHT);
9666 }
Chris Yee2b1e5c2021-03-10 22:45:12 -08009667};
9668
Chris Ye85758332021-05-16 23:05:17 -07009669TEST_F(LightControllerTest, MonoLight) {
9670 RawLightInfo infoMono = {.id = 1,
9671 .name = "Mono",
9672 .maxBrightness = 255,
9673 .flags = InputLightClass::BRIGHTNESS,
9674 .path = ""};
9675 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
Chris Yee2b1e5c2021-03-10 22:45:12 -08009676
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009677 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009678 InputDeviceInfo info;
9679 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009680 std::vector<InputDeviceLightInfo> lights = info.getLights();
9681 ASSERT_EQ(1U, lights.size());
9682 ASSERT_EQ(InputDeviceLightType::MONO, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009683
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009684 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
9685 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009686}
9687
9688TEST_F(LightControllerTest, RGBLight) {
9689 RawLightInfo infoRed = {.id = 1,
9690 .name = "red",
9691 .maxBrightness = 255,
9692 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
9693 .path = ""};
9694 RawLightInfo infoGreen = {.id = 2,
9695 .name = "green",
9696 .maxBrightness = 255,
9697 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
9698 .path = ""};
9699 RawLightInfo infoBlue = {.id = 3,
9700 .name = "blue",
9701 .maxBrightness = 255,
9702 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
9703 .path = ""};
9704 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
9705 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
9706 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
9707
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009708 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009709 InputDeviceInfo info;
9710 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009711 std::vector<InputDeviceLightInfo> lights = info.getLights();
9712 ASSERT_EQ(1U, lights.size());
9713 ASSERT_EQ(InputDeviceLightType::RGB, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009714
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009715 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
9716 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009717}
9718
9719TEST_F(LightControllerTest, MultiColorRGBLight) {
9720 RawLightInfo infoColor = {.id = 1,
9721 .name = "red",
9722 .maxBrightness = 255,
9723 .flags = InputLightClass::BRIGHTNESS |
9724 InputLightClass::MULTI_INTENSITY |
9725 InputLightClass::MULTI_INDEX,
9726 .path = ""};
9727
9728 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
9729
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009730 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009731 InputDeviceInfo info;
9732 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009733 std::vector<InputDeviceLightInfo> lights = info.getLights();
9734 ASSERT_EQ(1U, lights.size());
9735 ASSERT_EQ(InputDeviceLightType::MULTI_COLOR, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009736
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009737 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
9738 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009739}
9740
9741TEST_F(LightControllerTest, PlayerIdLight) {
9742 RawLightInfo info1 = {.id = 1,
9743 .name = "player1",
9744 .maxBrightness = 255,
9745 .flags = InputLightClass::BRIGHTNESS,
9746 .path = ""};
9747 RawLightInfo info2 = {.id = 2,
9748 .name = "player2",
9749 .maxBrightness = 255,
9750 .flags = InputLightClass::BRIGHTNESS,
9751 .path = ""};
9752 RawLightInfo info3 = {.id = 3,
9753 .name = "player3",
9754 .maxBrightness = 255,
9755 .flags = InputLightClass::BRIGHTNESS,
9756 .path = ""};
9757 RawLightInfo info4 = {.id = 4,
9758 .name = "player4",
9759 .maxBrightness = 255,
9760 .flags = InputLightClass::BRIGHTNESS,
9761 .path = ""};
9762 mFakeEventHub->addRawLightInfo(info1.id, std::move(info1));
9763 mFakeEventHub->addRawLightInfo(info2.id, std::move(info2));
9764 mFakeEventHub->addRawLightInfo(info3.id, std::move(info3));
9765 mFakeEventHub->addRawLightInfo(info4.id, std::move(info4));
9766
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009767 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009768 InputDeviceInfo info;
9769 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009770 std::vector<InputDeviceLightInfo> lights = info.getLights();
9771 ASSERT_EQ(1U, lights.size());
9772 ASSERT_EQ(InputDeviceLightType::PLAYER_ID, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009773
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009774 ASSERT_FALSE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
9775 ASSERT_TRUE(controller.setLightPlayerId(lights[0].id, LIGHT_PLAYER_ID));
9776 ASSERT_EQ(controller.getLightPlayerId(lights[0].id).value_or(-1), LIGHT_PLAYER_ID);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009777}
9778
Michael Wrightd02c5b62014-02-10 15:10:22 -08009779} // namespace android