blob: 2c8afc7b8971e744d01c33562f99a35ad0739c5c [file] [log] [blame]
Michael Wrightd02c5b62014-02-10 15:10:22 -08001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Dominik Laskowski2f01d772022-03-23 16:01:29 -070017#include <cinttypes>
18#include <memory>
19
Prabir Pradhan2770d242019-09-02 18:07:11 -070020#include <CursorInputMapper.h>
21#include <InputDevice.h>
22#include <InputMapper.h>
23#include <InputReader.h>
Prabir Pradhan1aed8582019-12-30 11:46:51 -080024#include <InputReaderBase.h>
25#include <InputReaderFactory.h>
Arthur Hung6d5b4b22022-01-21 07:21:10 +000026#include <JoystickInputMapper.h>
Prabir Pradhan2770d242019-09-02 18:07:11 -070027#include <KeyboardInputMapper.h>
28#include <MultiTouchInputMapper.h>
Chris Ye1dd2e5c2021-04-04 23:12:41 -070029#include <PeripheralController.h>
Chris Yef59a2f42020-10-16 12:55:26 -070030#include <SensorInputMapper.h>
Prabir Pradhan2770d242019-09-02 18:07:11 -070031#include <SingleTouchInputMapper.h>
32#include <SwitchInputMapper.h>
33#include <TestInputListener.h>
34#include <TouchInputMapper.h>
Prabir Pradhan1aed8582019-12-30 11:46:51 -080035#include <UinputDevice.h>
Chris Ye87143712020-11-10 05:05:58 +000036#include <VibratorInputMapper.h>
Prabir Pradhan2574dfa2019-10-16 16:35:07 -070037#include <android-base/thread_annotations.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080038#include <gtest/gtest.h>
chaviw3277faf2021-05-19 16:45:23 -050039#include <gui/constants.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080040
Michael Wrightdde67b82020-10-27 16:09:22 +000041#include "input/DisplayViewport.h"
42#include "input/Input.h"
Michael Wright17db18e2020-06-26 20:51:44 +010043
Michael Wrightd02c5b62014-02-10 15:10:22 -080044namespace android {
45
Dominik Laskowski2f01d772022-03-23 16:01:29 -070046using namespace ftl::flag_operators;
47
Prabir Pradhan2574dfa2019-10-16 16:35:07 -070048using std::chrono_literals::operator""ms;
49
50// Timeout for waiting for an expected event
51static constexpr std::chrono::duration WAIT_TIMEOUT = 100ms;
52
Michael Wrightd02c5b62014-02-10 15:10:22 -080053// An arbitrary time value.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +000054static constexpr nsecs_t ARBITRARY_TIME = 1234;
55static constexpr nsecs_t READ_TIME = 4321;
Michael Wrightd02c5b62014-02-10 15:10:22 -080056
57// Arbitrary display properties.
arthurhungcc7f9802020-04-30 17:55:40 +080058static constexpr int32_t DISPLAY_ID = 0;
59static constexpr int32_t SECONDARY_DISPLAY_ID = DISPLAY_ID + 1;
60static constexpr int32_t DISPLAY_WIDTH = 480;
61static constexpr int32_t DISPLAY_HEIGHT = 800;
62static constexpr int32_t VIRTUAL_DISPLAY_ID = 1;
63static constexpr int32_t VIRTUAL_DISPLAY_WIDTH = 400;
64static constexpr int32_t VIRTUAL_DISPLAY_HEIGHT = 500;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -070065static const char* VIRTUAL_DISPLAY_UNIQUE_ID = "virtual:1";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -070066static constexpr std::optional<uint8_t> NO_PORT = std::nullopt; // no physical port is specified
Michael Wrightd02c5b62014-02-10 15:10:22 -080067
arthurhungcc7f9802020-04-30 17:55:40 +080068static constexpr int32_t FIRST_SLOT = 0;
69static constexpr int32_t SECOND_SLOT = 1;
70static constexpr int32_t THIRD_SLOT = 2;
71static constexpr int32_t INVALID_TRACKING_ID = -1;
72static constexpr int32_t FIRST_TRACKING_ID = 0;
73static constexpr int32_t SECOND_TRACKING_ID = 1;
74static constexpr int32_t THIRD_TRACKING_ID = 2;
Chris Yee2b1e5c2021-03-10 22:45:12 -080075static constexpr int32_t DEFAULT_BATTERY = 1;
Kim Low03ea0352020-11-06 12:45:07 -080076static constexpr int32_t BATTERY_STATUS = 4;
77static constexpr int32_t BATTERY_CAPACITY = 66;
Chris Ye3fdbfef2021-01-06 18:45:18 -080078static constexpr int32_t LIGHT_BRIGHTNESS = 0x55000000;
79static constexpr int32_t LIGHT_COLOR = 0x7F448866;
80static constexpr int32_t LIGHT_PLAYER_ID = 2;
arthurhungcc7f9802020-04-30 17:55:40 +080081
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080082static constexpr int32_t ACTION_POINTER_0_DOWN =
83 AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
84static constexpr int32_t ACTION_POINTER_0_UP =
85 AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
86static constexpr int32_t ACTION_POINTER_1_DOWN =
87 AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
88static constexpr int32_t ACTION_POINTER_1_UP =
89 AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
90
Michael Wrightd02c5b62014-02-10 15:10:22 -080091// Error tolerance for floating point assertions.
92static const float EPSILON = 0.001f;
93
94template<typename T>
95static inline T min(T a, T b) {
96 return a < b ? a : b;
97}
98
99static inline float avg(float x, float y) {
100 return (x + y) / 2;
101}
102
Chris Ye3fdbfef2021-01-06 18:45:18 -0800103// Mapping for light color name and the light color
104const std::unordered_map<std::string, LightColor> LIGHT_COLORS = {{"red", LightColor::RED},
105 {"green", LightColor::GREEN},
106 {"blue", LightColor::BLUE}};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800107
Prabir Pradhanc14266f2021-05-12 15:56:24 -0700108static int32_t getInverseRotation(int32_t orientation) {
109 switch (orientation) {
110 case DISPLAY_ORIENTATION_90:
111 return DISPLAY_ORIENTATION_270;
112 case DISPLAY_ORIENTATION_270:
113 return DISPLAY_ORIENTATION_90;
114 default:
115 return orientation;
116 }
117}
118
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800119static void assertAxisResolution(MultiTouchInputMapper& mapper, int axis, float resolution) {
120 InputDeviceInfo info;
121 mapper.populateDeviceInfo(&info);
122
123 const InputDeviceInfo::MotionRange* motionRange =
124 info.getMotionRange(axis, AINPUT_SOURCE_TOUCHSCREEN);
125 ASSERT_NEAR(motionRange->resolution, resolution, EPSILON);
126}
127
128static void assertAxisNotPresent(MultiTouchInputMapper& mapper, int axis) {
129 InputDeviceInfo info;
130 mapper.populateDeviceInfo(&info);
131
132 const InputDeviceInfo::MotionRange* motionRange =
133 info.getMotionRange(axis, AINPUT_SOURCE_TOUCHSCREEN);
134 ASSERT_EQ(nullptr, motionRange);
135}
136
Michael Wrightd02c5b62014-02-10 15:10:22 -0800137// --- FakePointerController ---
138
139class FakePointerController : public PointerControllerInterface {
140 bool mHaveBounds;
141 float mMinX, mMinY, mMaxX, mMaxY;
142 float mX, mY;
143 int32_t mButtonState;
Arthur Hungc7ad2d02018-12-18 17:41:29 +0800144 int32_t mDisplayId;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800145
Michael Wrightd02c5b62014-02-10 15:10:22 -0800146public:
147 FakePointerController() :
148 mHaveBounds(false), mMinX(0), mMinY(0), mMaxX(0), mMaxY(0), mX(0), mY(0),
Arthur Hungc7ad2d02018-12-18 17:41:29 +0800149 mButtonState(0), mDisplayId(ADISPLAY_ID_DEFAULT) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800150 }
151
Michael Wright17db18e2020-06-26 20:51:44 +0100152 virtual ~FakePointerController() {}
153
Michael Wrightd02c5b62014-02-10 15:10:22 -0800154 void setBounds(float minX, float minY, float maxX, float maxY) {
155 mHaveBounds = true;
156 mMinX = minX;
157 mMinY = minY;
158 mMaxX = maxX;
159 mMaxY = maxY;
160 }
161
Chris Yea52ade12020-08-27 16:49:20 -0700162 void setPosition(float x, float y) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800163 mX = x;
164 mY = y;
165 }
166
Chris Yea52ade12020-08-27 16:49:20 -0700167 void setButtonState(int32_t buttonState) override { mButtonState = buttonState; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800168
Chris Yea52ade12020-08-27 16:49:20 -0700169 int32_t getButtonState() const override { return mButtonState; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800170
Chris Yea52ade12020-08-27 16:49:20 -0700171 void getPosition(float* outX, float* outY) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800172 *outX = mX;
173 *outY = mY;
174 }
175
Chris Yea52ade12020-08-27 16:49:20 -0700176 int32_t getDisplayId() const override { return mDisplayId; }
Arthur Hungc7ad2d02018-12-18 17:41:29 +0800177
Chris Yea52ade12020-08-27 16:49:20 -0700178 void setDisplayViewport(const DisplayViewport& viewport) override {
Garfield Tan888a6a42020-01-09 11:39:16 -0800179 mDisplayId = viewport.displayId;
180 }
181
Arthur Hung7c645402019-01-25 17:45:42 +0800182 const std::map<int32_t, std::vector<int32_t>>& getSpots() {
183 return mSpotsByDisplay;
184 }
185
Michael Wrightd02c5b62014-02-10 15:10:22 -0800186private:
Chris Yea52ade12020-08-27 16:49:20 -0700187 bool getBounds(float* outMinX, float* outMinY, float* outMaxX, float* outMaxY) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800188 *outMinX = mMinX;
189 *outMinY = mMinY;
190 *outMaxX = mMaxX;
191 *outMaxY = mMaxY;
192 return mHaveBounds;
193 }
194
Chris Yea52ade12020-08-27 16:49:20 -0700195 void move(float deltaX, float deltaY) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800196 mX += deltaX;
197 if (mX < mMinX) mX = mMinX;
198 if (mX > mMaxX) mX = mMaxX;
199 mY += deltaY;
200 if (mY < mMinY) mY = mMinY;
201 if (mY > mMaxY) mY = mMaxY;
202 }
203
Chris Yea52ade12020-08-27 16:49:20 -0700204 void fade(Transition) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800205
Chris Yea52ade12020-08-27 16:49:20 -0700206 void unfade(Transition) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800207
Chris Yea52ade12020-08-27 16:49:20 -0700208 void setPresentation(Presentation) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800209
Chris Yea52ade12020-08-27 16:49:20 -0700210 void setSpots(const PointerCoords*, const uint32_t*, BitSet32 spotIdBits,
211 int32_t displayId) override {
Arthur Hung7c645402019-01-25 17:45:42 +0800212 std::vector<int32_t> newSpots;
213 // Add spots for fingers that are down.
214 for (BitSet32 idBits(spotIdBits); !idBits.isEmpty(); ) {
215 uint32_t id = idBits.clearFirstMarkedBit();
216 newSpots.push_back(id);
217 }
218
219 mSpotsByDisplay[displayId] = newSpots;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800220 }
221
Chris Yea52ade12020-08-27 16:49:20 -0700222 void clearSpots() override {}
Arthur Hung7c645402019-01-25 17:45:42 +0800223
224 std::map<int32_t, std::vector<int32_t>> mSpotsByDisplay;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800225};
226
227
228// --- FakeInputReaderPolicy ---
229
230class FakeInputReaderPolicy : public InputReaderPolicyInterface {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700231 std::mutex mLock;
232 std::condition_variable mDevicesChangedCondition;
233
Michael Wrightd02c5b62014-02-10 15:10:22 -0800234 InputReaderConfiguration mConfig;
Prabir Pradhan2853b7a2021-08-23 14:08:51 +0000235 std::shared_ptr<FakePointerController> mPointerController;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700236 std::vector<InputDeviceInfo> mInputDevices GUARDED_BY(mLock);
237 bool mInputDevicesChanged GUARDED_BY(mLock){false};
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100238 std::vector<DisplayViewport> mViewports;
Jason Gerecke489fda82012-09-07 17:19:40 -0700239 TouchAffineTransformation transform;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800240
241protected:
Chris Yea52ade12020-08-27 16:49:20 -0700242 virtual ~FakeInputReaderPolicy() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800243
244public:
245 FakeInputReaderPolicy() {
246 }
247
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700248 void assertInputDevicesChanged() {
Prabir Pradhan1aed8582019-12-30 11:46:51 -0800249 waitForInputDevices([](bool devicesChanged) {
250 if (!devicesChanged) {
251 FAIL() << "Timed out waiting for notifyInputDevicesChanged() to be called.";
252 }
253 });
254 }
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700255
Prabir Pradhan1aed8582019-12-30 11:46:51 -0800256 void assertInputDevicesNotChanged() {
257 waitForInputDevices([](bool devicesChanged) {
258 if (devicesChanged) {
259 FAIL() << "Expected notifyInputDevicesChanged() to not be called.";
260 }
261 });
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700262 }
263
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700264 virtual void clearViewports() {
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100265 mViewports.clear();
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100266 mConfig.setDisplayViewports(mViewports);
Santos Cordonfa5cf462017-04-05 10:37:00 -0700267 }
268
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700269 std::optional<DisplayViewport> getDisplayViewportByUniqueId(const std::string& uniqueId) const {
270 return mConfig.getDisplayViewportByUniqueId(uniqueId);
271 }
272 std::optional<DisplayViewport> getDisplayViewportByType(ViewportType type) const {
273 return mConfig.getDisplayViewportByType(type);
274 }
275
276 std::optional<DisplayViewport> getDisplayViewportByPort(uint8_t displayPort) const {
277 return mConfig.getDisplayViewportByPort(displayPort);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700278 }
279
Prabir Pradhan5632d622021-09-06 07:57:20 -0700280 void addDisplayViewport(DisplayViewport viewport) {
281 mViewports.push_back(std::move(viewport));
282 mConfig.setDisplayViewports(mViewports);
283 }
284
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700285 void addDisplayViewport(int32_t displayId, int32_t width, int32_t height, int32_t orientation,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +0000286 bool isActive, const std::string& uniqueId,
Prabir Pradhan5632d622021-09-06 07:57:20 -0700287 std::optional<uint8_t> physicalPort, ViewportType type) {
288 const bool isRotated =
289 (orientation == DISPLAY_ORIENTATION_90 || orientation == DISPLAY_ORIENTATION_270);
290 DisplayViewport v;
291 v.displayId = displayId;
292 v.orientation = orientation;
293 v.logicalLeft = 0;
294 v.logicalTop = 0;
295 v.logicalRight = isRotated ? height : width;
296 v.logicalBottom = isRotated ? width : height;
297 v.physicalLeft = 0;
298 v.physicalTop = 0;
299 v.physicalRight = isRotated ? height : width;
300 v.physicalBottom = isRotated ? width : height;
301 v.deviceWidth = isRotated ? height : width;
302 v.deviceHeight = isRotated ? width : height;
303 v.isActive = isActive;
304 v.uniqueId = uniqueId;
305 v.physicalPort = physicalPort;
306 v.type = type;
307
308 addDisplayViewport(v);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800309 }
310
Arthur Hung6cd19a42019-08-30 19:04:12 +0800311 bool updateViewport(const DisplayViewport& viewport) {
312 size_t count = mViewports.size();
313 for (size_t i = 0; i < count; i++) {
314 const DisplayViewport& currentViewport = mViewports[i];
315 if (currentViewport.displayId == viewport.displayId) {
316 mViewports[i] = viewport;
317 mConfig.setDisplayViewports(mViewports);
318 return true;
319 }
320 }
321 // no viewport found.
322 return false;
323 }
324
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100325 void addExcludedDeviceName(const std::string& deviceName) {
326 mConfig.excludedDeviceNames.push_back(deviceName);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800327 }
328
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700329 void addInputPortAssociation(const std::string& inputPort, uint8_t displayPort) {
330 mConfig.portAssociations.insert({inputPort, displayPort});
331 }
332
Christine Franks1ba71cc2021-04-07 14:37:42 -0700333 void addInputUniqueIdAssociation(const std::string& inputUniqueId,
334 const std::string& displayUniqueId) {
335 mConfig.uniqueIdAssociations.insert({inputUniqueId, displayUniqueId});
336 }
337
Siarhei Vishniakouc6f61192019-07-23 18:12:31 +0000338 void addDisabledDevice(int32_t deviceId) { mConfig.disabledDevices.insert(deviceId); }
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700339
Siarhei Vishniakouc6f61192019-07-23 18:12:31 +0000340 void removeDisabledDevice(int32_t deviceId) { mConfig.disabledDevices.erase(deviceId); }
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700341
Prabir Pradhan2853b7a2021-08-23 14:08:51 +0000342 void setPointerController(std::shared_ptr<FakePointerController> controller) {
343 mPointerController = std::move(controller);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800344 }
345
346 const InputReaderConfiguration* getReaderConfiguration() const {
347 return &mConfig;
348 }
349
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800350 const std::vector<InputDeviceInfo>& getInputDevices() const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800351 return mInputDevices;
352 }
353
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100354 TouchAffineTransformation getTouchAffineTransformation(const std::string& inputDeviceDescriptor,
Jason Gerecke71b16e82014-03-10 09:47:59 -0700355 int32_t surfaceRotation) {
Jason Gerecke489fda82012-09-07 17:19:40 -0700356 return transform;
357 }
358
359 void setTouchAffineTransformation(const TouchAffineTransformation t) {
360 transform = t;
Jason Gerecke12d6baa2014-01-27 18:34:20 -0800361 }
362
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000363 PointerCaptureRequest setPointerCapture(bool enabled) {
364 mConfig.pointerCaptureRequest = {enabled, mNextPointerCaptureSequenceNumber++};
365 return mConfig.pointerCaptureRequest;
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -0800366 }
367
Arthur Hung7c645402019-01-25 17:45:42 +0800368 void setShowTouches(bool enabled) {
369 mConfig.showTouches = enabled;
370 }
371
Garfield Tan888a6a42020-01-09 11:39:16 -0800372 void setDefaultPointerDisplayId(int32_t pointerDisplayId) {
373 mConfig.defaultPointerDisplayId = pointerDisplayId;
374 }
375
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -0800376 float getPointerGestureMovementSpeedRatio() { return mConfig.pointerGestureMovementSpeedRatio; }
377
Prabir Pradhanf99d6e72022-04-21 15:28:35 +0000378 void setVelocityControlParams(const VelocityControlParameters& params) {
379 mConfig.pointerVelocityControlParameters = params;
380 mConfig.wheelVelocityControlParameters = params;
381 }
382
Michael Wrightd02c5b62014-02-10 15:10:22 -0800383private:
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000384 uint32_t mNextPointerCaptureSequenceNumber = 0;
385
Chris Yea52ade12020-08-27 16:49:20 -0700386 void getReaderConfiguration(InputReaderConfiguration* outConfig) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800387 *outConfig = mConfig;
388 }
389
Prabir Pradhan2853b7a2021-08-23 14:08:51 +0000390 std::shared_ptr<PointerControllerInterface> obtainPointerController(
391 int32_t /*deviceId*/) override {
392 return mPointerController;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800393 }
394
Chris Yea52ade12020-08-27 16:49:20 -0700395 void notifyInputDevicesChanged(const std::vector<InputDeviceInfo>& inputDevices) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700396 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800397 mInputDevices = inputDevices;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700398 mInputDevicesChanged = true;
399 mDevicesChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800400 }
401
Chris Yea52ade12020-08-27 16:49:20 -0700402 std::shared_ptr<KeyCharacterMap> getKeyboardLayoutOverlay(
403 const InputDeviceIdentifier&) override {
Yi Kong9b14ac62018-07-17 13:48:38 -0700404 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800405 }
406
Chris Yea52ade12020-08-27 16:49:20 -0700407 std::string getDeviceAlias(const InputDeviceIdentifier&) override { return ""; }
Prabir Pradhan1aed8582019-12-30 11:46:51 -0800408
409 void waitForInputDevices(std::function<void(bool)> processDevicesChanged) {
410 std::unique_lock<std::mutex> lock(mLock);
411 base::ScopedLockAssertion assumeLocked(mLock);
412
413 const bool devicesChanged =
414 mDevicesChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
415 return mInputDevicesChanged;
416 });
417 ASSERT_NO_FATAL_FAILURE(processDevicesChanged(devicesChanged));
418 mInputDevicesChanged = false;
419 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800420};
421
Michael Wrightd02c5b62014-02-10 15:10:22 -0800422// --- FakeEventHub ---
423
424class FakeEventHub : public EventHubInterface {
425 struct KeyInfo {
426 int32_t keyCode;
427 uint32_t flags;
428 };
429
Chris Yef59a2f42020-10-16 12:55:26 -0700430 struct SensorInfo {
431 InputDeviceSensorType sensorType;
432 int32_t sensorDataIndex;
433 };
434
Michael Wrightd02c5b62014-02-10 15:10:22 -0800435 struct Device {
436 InputDeviceIdentifier identifier;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700437 ftl::Flags<InputDeviceClass> classes;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800438 PropertyMap configuration;
439 KeyedVector<int, RawAbsoluteAxisInfo> absoluteAxes;
440 KeyedVector<int, bool> relativeAxes;
441 KeyedVector<int32_t, int32_t> keyCodeStates;
442 KeyedVector<int32_t, int32_t> scanCodeStates;
443 KeyedVector<int32_t, int32_t> switchStates;
444 KeyedVector<int32_t, int32_t> absoluteAxisValue;
445 KeyedVector<int32_t, KeyInfo> keysByScanCode;
446 KeyedVector<int32_t, KeyInfo> keysByUsageCode;
447 KeyedVector<int32_t, bool> leds;
Philip Junker4af3b3d2021-12-14 10:36:55 +0100448 // fake mapping which would normally come from keyCharacterMap
449 std::unordered_map<int32_t, int32_t> keyCodeMapping;
Chris Yef59a2f42020-10-16 12:55:26 -0700450 std::unordered_map<int32_t, SensorInfo> sensorsByAbsCode;
451 BitArray<MSC_MAX> mscBitmask;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800452 std::vector<VirtualKeyDefinition> virtualKeys;
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700453 bool enabled;
454
455 status_t enable() {
456 enabled = true;
457 return OK;
458 }
459
460 status_t disable() {
461 enabled = false;
462 return OK;
463 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800464
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700465 explicit Device(ftl::Flags<InputDeviceClass> classes) : classes(classes), enabled(true) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800466 };
467
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700468 std::mutex mLock;
469 std::condition_variable mEventsCondition;
470
Michael Wrightd02c5b62014-02-10 15:10:22 -0800471 KeyedVector<int32_t, Device*> mDevices;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100472 std::vector<std::string> mExcludedDevices;
Siarhei Vishniakou370039c2021-02-04 22:09:01 +0000473 std::vector<RawEvent> mEvents GUARDED_BY(mLock);
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600474 std::unordered_map<int32_t /*deviceId*/, std::vector<TouchVideoFrame>> mVideoFrames;
Chris Ye87143712020-11-10 05:05:58 +0000475 std::vector<int32_t> mVibrators = {0, 1};
Chris Ye3fdbfef2021-01-06 18:45:18 -0800476 std::unordered_map<int32_t, RawLightInfo> mRawLightInfos;
477 // Simulates a device light brightness, from light id to light brightness.
478 std::unordered_map<int32_t /* lightId */, int32_t /* brightness*/> mLightBrightness;
479 // Simulates a device light intensities, from light id to light intensities map.
480 std::unordered_map<int32_t /* lightId */, std::unordered_map<LightColor, int32_t>>
481 mLightIntensities;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800482
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700483public:
Michael Wrightd02c5b62014-02-10 15:10:22 -0800484 virtual ~FakeEventHub() {
485 for (size_t i = 0; i < mDevices.size(); i++) {
486 delete mDevices.valueAt(i);
487 }
488 }
489
Michael Wrightd02c5b62014-02-10 15:10:22 -0800490 FakeEventHub() { }
491
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700492 void addDevice(int32_t deviceId, const std::string& name,
493 ftl::Flags<InputDeviceClass> classes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800494 Device* device = new Device(classes);
495 device->identifier.name = name;
496 mDevices.add(deviceId, device);
497
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000498 enqueueEvent(ARBITRARY_TIME, READ_TIME, deviceId, EventHubInterface::DEVICE_ADDED, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800499 }
500
501 void removeDevice(int32_t deviceId) {
502 delete mDevices.valueFor(deviceId);
503 mDevices.removeItem(deviceId);
504
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000505 enqueueEvent(ARBITRARY_TIME, READ_TIME, deviceId, EventHubInterface::DEVICE_REMOVED, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800506 }
507
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700508 bool isDeviceEnabled(int32_t deviceId) {
509 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700510 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700511 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
512 return false;
513 }
514 return device->enabled;
515 }
516
517 status_t enableDevice(int32_t deviceId) {
518 status_t result;
519 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700520 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700521 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
522 return BAD_VALUE;
523 }
524 if (device->enabled) {
525 ALOGW("Duplicate call to %s, device %" PRId32 " already enabled", __func__, deviceId);
526 return OK;
527 }
528 result = device->enable();
529 return result;
530 }
531
532 status_t disableDevice(int32_t deviceId) {
533 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700534 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700535 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
536 return BAD_VALUE;
537 }
538 if (!device->enabled) {
539 ALOGW("Duplicate call to %s, device %" PRId32 " already disabled", __func__, deviceId);
540 return OK;
541 }
542 return device->disable();
543 }
544
Michael Wrightd02c5b62014-02-10 15:10:22 -0800545 void finishDeviceScan() {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000546 enqueueEvent(ARBITRARY_TIME, READ_TIME, 0, EventHubInterface::FINISHED_DEVICE_SCAN, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800547 }
548
549 void addConfigurationProperty(int32_t deviceId, const String8& key, const String8& value) {
550 Device* device = getDevice(deviceId);
551 device->configuration.addProperty(key, value);
552 }
553
554 void addConfigurationMap(int32_t deviceId, const PropertyMap* configuration) {
555 Device* device = getDevice(deviceId);
556 device->configuration.addAll(configuration);
557 }
558
559 void addAbsoluteAxis(int32_t deviceId, int axis,
560 int32_t minValue, int32_t maxValue, int flat, int fuzz, int resolution = 0) {
561 Device* device = getDevice(deviceId);
562
563 RawAbsoluteAxisInfo info;
564 info.valid = true;
565 info.minValue = minValue;
566 info.maxValue = maxValue;
567 info.flat = flat;
568 info.fuzz = fuzz;
569 info.resolution = resolution;
570 device->absoluteAxes.add(axis, info);
571 }
572
573 void addRelativeAxis(int32_t deviceId, int32_t axis) {
574 Device* device = getDevice(deviceId);
575 device->relativeAxes.add(axis, true);
576 }
577
578 void setKeyCodeState(int32_t deviceId, int32_t keyCode, int32_t state) {
579 Device* device = getDevice(deviceId);
580 device->keyCodeStates.replaceValueFor(keyCode, state);
581 }
582
583 void setScanCodeState(int32_t deviceId, int32_t scanCode, int32_t state) {
584 Device* device = getDevice(deviceId);
585 device->scanCodeStates.replaceValueFor(scanCode, state);
586 }
587
588 void setSwitchState(int32_t deviceId, int32_t switchCode, int32_t state) {
589 Device* device = getDevice(deviceId);
590 device->switchStates.replaceValueFor(switchCode, state);
591 }
592
593 void setAbsoluteAxisValue(int32_t deviceId, int32_t axis, int32_t value) {
594 Device* device = getDevice(deviceId);
595 device->absoluteAxisValue.replaceValueFor(axis, value);
596 }
597
598 void addKey(int32_t deviceId, int32_t scanCode, int32_t usageCode,
599 int32_t keyCode, uint32_t flags) {
600 Device* device = getDevice(deviceId);
601 KeyInfo info;
602 info.keyCode = keyCode;
603 info.flags = flags;
604 if (scanCode) {
605 device->keysByScanCode.add(scanCode, info);
606 }
607 if (usageCode) {
608 device->keysByUsageCode.add(usageCode, info);
609 }
610 }
611
Philip Junker4af3b3d2021-12-14 10:36:55 +0100612 void addKeyCodeMapping(int32_t deviceId, int32_t fromKeyCode, int32_t toKeyCode) {
613 Device* device = getDevice(deviceId);
614 device->keyCodeMapping.insert_or_assign(fromKeyCode, toKeyCode);
615 }
616
Michael Wrightd02c5b62014-02-10 15:10:22 -0800617 void addLed(int32_t deviceId, int32_t led, bool initialState) {
618 Device* device = getDevice(deviceId);
619 device->leds.add(led, initialState);
620 }
621
Chris Yef59a2f42020-10-16 12:55:26 -0700622 void addSensorAxis(int32_t deviceId, int32_t absCode, InputDeviceSensorType sensorType,
623 int32_t sensorDataIndex) {
624 Device* device = getDevice(deviceId);
625 SensorInfo info;
626 info.sensorType = sensorType;
627 info.sensorDataIndex = sensorDataIndex;
628 device->sensorsByAbsCode.emplace(absCode, info);
629 }
630
631 void setMscEvent(int32_t deviceId, int32_t mscEvent) {
632 Device* device = getDevice(deviceId);
633 typename BitArray<MSC_MAX>::Buffer buffer;
634 buffer[mscEvent / 32] = 1 << mscEvent % 32;
635 device->mscBitmask.loadFromBuffer(buffer);
636 }
637
Chris Ye3fdbfef2021-01-06 18:45:18 -0800638 void addRawLightInfo(int32_t rawId, RawLightInfo&& info) {
639 mRawLightInfos.emplace(rawId, std::move(info));
640 }
641
642 void fakeLightBrightness(int32_t rawId, int32_t brightness) {
643 mLightBrightness.emplace(rawId, brightness);
644 }
645
646 void fakeLightIntensities(int32_t rawId,
647 const std::unordered_map<LightColor, int32_t> intensities) {
648 mLightIntensities.emplace(rawId, std::move(intensities));
649 }
650
Michael Wrightd02c5b62014-02-10 15:10:22 -0800651 bool getLedState(int32_t deviceId, int32_t led) {
652 Device* device = getDevice(deviceId);
653 return device->leds.valueFor(led);
654 }
655
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100656 std::vector<std::string>& getExcludedDevices() {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800657 return mExcludedDevices;
658 }
659
660 void addVirtualKeyDefinition(int32_t deviceId, const VirtualKeyDefinition& definition) {
661 Device* device = getDevice(deviceId);
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800662 device->virtualKeys.push_back(definition);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800663 }
664
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000665 void enqueueEvent(nsecs_t when, nsecs_t readTime, int32_t deviceId, int32_t type, int32_t code,
666 int32_t value) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700667 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800668 RawEvent event;
669 event.when = when;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000670 event.readTime = readTime;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800671 event.deviceId = deviceId;
672 event.type = type;
673 event.code = code;
674 event.value = value;
675 mEvents.push_back(event);
676
677 if (type == EV_ABS) {
678 setAbsoluteAxisValue(deviceId, code, value);
679 }
680 }
681
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600682 void setVideoFrames(std::unordered_map<int32_t /*deviceId*/,
683 std::vector<TouchVideoFrame>> videoFrames) {
684 mVideoFrames = std::move(videoFrames);
685 }
686
Michael Wrightd02c5b62014-02-10 15:10:22 -0800687 void assertQueueIsEmpty() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700688 std::unique_lock<std::mutex> lock(mLock);
689 base::ScopedLockAssertion assumeLocked(mLock);
690 const bool queueIsEmpty =
691 mEventsCondition.wait_for(lock, WAIT_TIMEOUT,
692 [this]() REQUIRES(mLock) { return mEvents.size() == 0; });
693 if (!queueIsEmpty) {
694 FAIL() << "Timed out waiting for EventHub queue to be emptied.";
695 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800696 }
697
698private:
699 Device* getDevice(int32_t deviceId) const {
700 ssize_t index = mDevices.indexOfKey(deviceId);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100701 return index >= 0 ? mDevices.valueAt(index) : nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800702 }
703
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700704 ftl::Flags<InputDeviceClass> getDeviceClasses(int32_t deviceId) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800705 Device* device = getDevice(deviceId);
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700706 return device ? device->classes : ftl::Flags<InputDeviceClass>(0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800707 }
708
Chris Yea52ade12020-08-27 16:49:20 -0700709 InputDeviceIdentifier getDeviceIdentifier(int32_t deviceId) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800710 Device* device = getDevice(deviceId);
711 return device ? device->identifier : InputDeviceIdentifier();
712 }
713
Chris Yea52ade12020-08-27 16:49:20 -0700714 int32_t getDeviceControllerNumber(int32_t) const override { return 0; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800715
Chris Yea52ade12020-08-27 16:49:20 -0700716 void getConfiguration(int32_t deviceId, PropertyMap* outConfiguration) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800717 Device* device = getDevice(deviceId);
718 if (device) {
719 *outConfiguration = device->configuration;
720 }
721 }
722
Chris Yea52ade12020-08-27 16:49:20 -0700723 status_t getAbsoluteAxisInfo(int32_t deviceId, int axis,
724 RawAbsoluteAxisInfo* outAxisInfo) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800725 Device* device = getDevice(deviceId);
Arthur Hung9da14732019-09-02 16:16:58 +0800726 if (device && device->enabled) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800727 ssize_t index = device->absoluteAxes.indexOfKey(axis);
728 if (index >= 0) {
729 *outAxisInfo = device->absoluteAxes.valueAt(index);
730 return OK;
731 }
732 }
733 outAxisInfo->clear();
734 return -1;
735 }
736
Chris Yea52ade12020-08-27 16:49:20 -0700737 bool hasRelativeAxis(int32_t deviceId, int axis) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800738 Device* device = getDevice(deviceId);
739 if (device) {
740 return device->relativeAxes.indexOfKey(axis) >= 0;
741 }
742 return false;
743 }
744
Chris Yea52ade12020-08-27 16:49:20 -0700745 bool hasInputProperty(int32_t, int) const override { return false; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800746
Chris Yef59a2f42020-10-16 12:55:26 -0700747 bool hasMscEvent(int32_t deviceId, int mscEvent) const override final {
748 Device* device = getDevice(deviceId);
749 if (device) {
750 return mscEvent >= 0 && mscEvent <= MSC_MAX ? device->mscBitmask.test(mscEvent) : false;
751 }
752 return false;
753 }
754
Chris Yea52ade12020-08-27 16:49:20 -0700755 status_t mapKey(int32_t deviceId, int32_t scanCode, int32_t usageCode, int32_t metaState,
756 int32_t* outKeycode, int32_t* outMetaState, uint32_t* outFlags) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800757 Device* device = getDevice(deviceId);
758 if (device) {
759 const KeyInfo* key = getKey(device, scanCode, usageCode);
760 if (key) {
761 if (outKeycode) {
762 *outKeycode = key->keyCode;
763 }
764 if (outFlags) {
765 *outFlags = key->flags;
766 }
Dmitry Torokhov0faaa0b2015-09-24 13:13:55 -0700767 if (outMetaState) {
768 *outMetaState = metaState;
769 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800770 return OK;
771 }
772 }
773 return NAME_NOT_FOUND;
774 }
775
776 const KeyInfo* getKey(Device* device, int32_t scanCode, int32_t usageCode) const {
777 if (usageCode) {
778 ssize_t index = device->keysByUsageCode.indexOfKey(usageCode);
779 if (index >= 0) {
780 return &device->keysByUsageCode.valueAt(index);
781 }
782 }
783 if (scanCode) {
784 ssize_t index = device->keysByScanCode.indexOfKey(scanCode);
785 if (index >= 0) {
786 return &device->keysByScanCode.valueAt(index);
787 }
788 }
Yi Kong9b14ac62018-07-17 13:48:38 -0700789 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800790 }
791
Chris Yea52ade12020-08-27 16:49:20 -0700792 status_t mapAxis(int32_t, int32_t, AxisInfo*) const override { return NAME_NOT_FOUND; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800793
Chris Yef59a2f42020-10-16 12:55:26 -0700794 base::Result<std::pair<InputDeviceSensorType, int32_t>> mapSensor(int32_t deviceId,
795 int32_t absCode) {
796 Device* device = getDevice(deviceId);
797 if (!device) {
798 return Errorf("Sensor device not found.");
799 }
800 auto it = device->sensorsByAbsCode.find(absCode);
801 if (it == device->sensorsByAbsCode.end()) {
802 return Errorf("Sensor map not found.");
803 }
804 const SensorInfo& info = it->second;
805 return std::make_pair(info.sensorType, info.sensorDataIndex);
806 }
807
Chris Yea52ade12020-08-27 16:49:20 -0700808 void setExcludedDevices(const std::vector<std::string>& devices) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800809 mExcludedDevices = devices;
810 }
811
Siarhei Vishniakou370039c2021-02-04 22:09:01 +0000812 size_t getEvents(int, RawEvent* buffer, size_t bufferSize) override {
813 std::scoped_lock lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800814
Siarhei Vishniakou370039c2021-02-04 22:09:01 +0000815 const size_t filledSize = std::min(mEvents.size(), bufferSize);
816 std::copy(mEvents.begin(), mEvents.begin() + filledSize, buffer);
817
818 mEvents.erase(mEvents.begin(), mEvents.begin() + filledSize);
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700819 mEventsCondition.notify_all();
Siarhei Vishniakou370039c2021-02-04 22:09:01 +0000820 return filledSize;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800821 }
822
Chris Yea52ade12020-08-27 16:49:20 -0700823 std::vector<TouchVideoFrame> getVideoFrames(int32_t deviceId) override {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600824 auto it = mVideoFrames.find(deviceId);
825 if (it != mVideoFrames.end()) {
826 std::vector<TouchVideoFrame> frames = std::move(it->second);
827 mVideoFrames.erase(deviceId);
828 return frames;
829 }
Siarhei Vishniakouadd89292018-12-13 19:23:36 -0800830 return {};
831 }
832
Chris Yea52ade12020-08-27 16:49:20 -0700833 int32_t getScanCodeState(int32_t deviceId, int32_t scanCode) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800834 Device* device = getDevice(deviceId);
835 if (device) {
836 ssize_t index = device->scanCodeStates.indexOfKey(scanCode);
837 if (index >= 0) {
838 return device->scanCodeStates.valueAt(index);
839 }
840 }
841 return AKEY_STATE_UNKNOWN;
842 }
843
Chris Yea52ade12020-08-27 16:49:20 -0700844 int32_t getKeyCodeState(int32_t deviceId, int32_t keyCode) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800845 Device* device = getDevice(deviceId);
846 if (device) {
847 ssize_t index = device->keyCodeStates.indexOfKey(keyCode);
848 if (index >= 0) {
849 return device->keyCodeStates.valueAt(index);
850 }
851 }
852 return AKEY_STATE_UNKNOWN;
853 }
854
Chris Yea52ade12020-08-27 16:49:20 -0700855 int32_t getSwitchState(int32_t deviceId, int32_t sw) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800856 Device* device = getDevice(deviceId);
857 if (device) {
858 ssize_t index = device->switchStates.indexOfKey(sw);
859 if (index >= 0) {
860 return device->switchStates.valueAt(index);
861 }
862 }
863 return AKEY_STATE_UNKNOWN;
864 }
865
Chris Yea52ade12020-08-27 16:49:20 -0700866 status_t getAbsoluteAxisValue(int32_t deviceId, int32_t axis,
867 int32_t* outValue) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800868 Device* device = getDevice(deviceId);
869 if (device) {
870 ssize_t index = device->absoluteAxisValue.indexOfKey(axis);
871 if (index >= 0) {
872 *outValue = device->absoluteAxisValue.valueAt(index);
873 return OK;
874 }
875 }
876 *outValue = 0;
877 return -1;
878 }
879
Philip Junker4af3b3d2021-12-14 10:36:55 +0100880 int32_t getKeyCodeForKeyLocation(int32_t deviceId, int32_t locationKeyCode) const override {
881 Device* device = getDevice(deviceId);
882 if (!device) {
883 return AKEYCODE_UNKNOWN;
884 }
885 auto it = device->keyCodeMapping.find(locationKeyCode);
886 return it != device->keyCodeMapping.end() ? it->second : locationKeyCode;
887 }
888
Chris Yea52ade12020-08-27 16:49:20 -0700889 // Return true if the device has non-empty key layout.
890 bool markSupportedKeyCodes(int32_t deviceId, size_t numCodes, const int32_t* keyCodes,
891 uint8_t* outFlags) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800892 bool result = false;
893 Device* device = getDevice(deviceId);
894 if (device) {
Chris Yea52ade12020-08-27 16:49:20 -0700895 result = device->keysByScanCode.size() > 0 || device->keysByUsageCode.size() > 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800896 for (size_t i = 0; i < numCodes; i++) {
897 for (size_t j = 0; j < device->keysByScanCode.size(); j++) {
898 if (keyCodes[i] == device->keysByScanCode.valueAt(j).keyCode) {
899 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800900 }
901 }
902 for (size_t j = 0; j < device->keysByUsageCode.size(); j++) {
903 if (keyCodes[i] == device->keysByUsageCode.valueAt(j).keyCode) {
904 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800905 }
906 }
907 }
908 }
909 return result;
910 }
911
Chris Yea52ade12020-08-27 16:49:20 -0700912 bool hasScanCode(int32_t deviceId, int32_t scanCode) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800913 Device* device = getDevice(deviceId);
914 if (device) {
915 ssize_t index = device->keysByScanCode.indexOfKey(scanCode);
916 return index >= 0;
917 }
918 return false;
919 }
920
Arthur Hungcb40a002021-08-03 14:31:01 +0000921 bool hasKeyCode(int32_t deviceId, int32_t keyCode) const override {
922 Device* device = getDevice(deviceId);
923 if (!device) {
924 return false;
925 }
926 for (size_t i = 0; i < device->keysByScanCode.size(); i++) {
927 if (keyCode == device->keysByScanCode.valueAt(i).keyCode) {
928 return true;
929 }
930 }
931 for (size_t j = 0; j < device->keysByUsageCode.size(); j++) {
932 if (keyCode == device->keysByUsageCode.valueAt(j).keyCode) {
933 return true;
934 }
935 }
936 return false;
937 }
938
Chris Yea52ade12020-08-27 16:49:20 -0700939 bool hasLed(int32_t deviceId, int32_t led) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800940 Device* device = getDevice(deviceId);
941 return device && device->leds.indexOfKey(led) >= 0;
942 }
943
Chris Yea52ade12020-08-27 16:49:20 -0700944 void setLedState(int32_t deviceId, int32_t led, bool on) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800945 Device* device = getDevice(deviceId);
946 if (device) {
947 ssize_t index = device->leds.indexOfKey(led);
948 if (index >= 0) {
949 device->leds.replaceValueAt(led, on);
950 } else {
951 ADD_FAILURE()
952 << "Attempted to set the state of an LED that the EventHub declared "
953 "was not present. led=" << led;
954 }
955 }
956 }
957
Chris Yea52ade12020-08-27 16:49:20 -0700958 void getVirtualKeyDefinitions(
959 int32_t deviceId, std::vector<VirtualKeyDefinition>& outVirtualKeys) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800960 outVirtualKeys.clear();
961
962 Device* device = getDevice(deviceId);
963 if (device) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800964 outVirtualKeys = device->virtualKeys;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800965 }
966 }
967
Chris Yea52ade12020-08-27 16:49:20 -0700968 const std::shared_ptr<KeyCharacterMap> getKeyCharacterMap(int32_t) const override {
Yi Kong9b14ac62018-07-17 13:48:38 -0700969 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800970 }
971
Chris Yea52ade12020-08-27 16:49:20 -0700972 bool setKeyboardLayoutOverlay(int32_t, std::shared_ptr<KeyCharacterMap>) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800973 return false;
974 }
975
Chris Yea52ade12020-08-27 16:49:20 -0700976 void vibrate(int32_t, const VibrationElement&) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800977
Chris Yea52ade12020-08-27 16:49:20 -0700978 void cancelVibrate(int32_t) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800979
Chris Ye87143712020-11-10 05:05:58 +0000980 std::vector<int32_t> getVibratorIds(int32_t deviceId) override { return mVibrators; };
981
Chris Yee2b1e5c2021-03-10 22:45:12 -0800982 std::optional<int32_t> getBatteryCapacity(int32_t, int32_t) const override {
983 return BATTERY_CAPACITY;
984 }
Kim Low03ea0352020-11-06 12:45:07 -0800985
Chris Yee2b1e5c2021-03-10 22:45:12 -0800986 std::optional<int32_t> getBatteryStatus(int32_t, int32_t) const override {
987 return BATTERY_STATUS;
988 }
989
990 const std::vector<int32_t> getRawBatteryIds(int32_t deviceId) { return {}; }
991
992 std::optional<RawBatteryInfo> getRawBatteryInfo(int32_t deviceId, int32_t batteryId) {
993 return std::nullopt;
994 }
Kim Low03ea0352020-11-06 12:45:07 -0800995
Chris Ye3fdbfef2021-01-06 18:45:18 -0800996 const std::vector<int32_t> getRawLightIds(int32_t deviceId) override {
997 std::vector<int32_t> ids;
998 for (const auto& [rawId, info] : mRawLightInfos) {
999 ids.push_back(rawId);
1000 }
1001 return ids;
1002 }
1003
1004 std::optional<RawLightInfo> getRawLightInfo(int32_t deviceId, int32_t lightId) override {
1005 auto it = mRawLightInfos.find(lightId);
1006 if (it == mRawLightInfos.end()) {
1007 return std::nullopt;
1008 }
1009 return it->second;
1010 }
1011
1012 void setLightBrightness(int32_t deviceId, int32_t lightId, int32_t brightness) override {
1013 mLightBrightness.emplace(lightId, brightness);
1014 }
1015
1016 void setLightIntensities(int32_t deviceId, int32_t lightId,
1017 std::unordered_map<LightColor, int32_t> intensities) override {
1018 mLightIntensities.emplace(lightId, intensities);
1019 };
1020
1021 std::optional<int32_t> getLightBrightness(int32_t deviceId, int32_t lightId) override {
1022 auto lightIt = mLightBrightness.find(lightId);
1023 if (lightIt == mLightBrightness.end()) {
1024 return std::nullopt;
1025 }
1026 return lightIt->second;
1027 }
1028
1029 std::optional<std::unordered_map<LightColor, int32_t>> getLightIntensities(
1030 int32_t deviceId, int32_t lightId) override {
1031 auto lightIt = mLightIntensities.find(lightId);
1032 if (lightIt == mLightIntensities.end()) {
1033 return std::nullopt;
1034 }
1035 return lightIt->second;
1036 };
1037
Narayan Kamath39efe3e2014-10-17 10:37:08 +01001038 virtual bool isExternal(int32_t) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001039 return false;
1040 }
1041
Chris Yea52ade12020-08-27 16:49:20 -07001042 void dump(std::string&) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001043
Chris Yea52ade12020-08-27 16:49:20 -07001044 void monitor() override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001045
Chris Yea52ade12020-08-27 16:49:20 -07001046 void requestReopenDevices() override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001047
Chris Yea52ade12020-08-27 16:49:20 -07001048 void wake() override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001049};
1050
Michael Wrightd02c5b62014-02-10 15:10:22 -08001051// --- FakeInputMapper ---
1052
1053class FakeInputMapper : public InputMapper {
1054 uint32_t mSources;
1055 int32_t mKeyboardType;
1056 int32_t mMetaState;
1057 KeyedVector<int32_t, int32_t> mKeyCodeStates;
1058 KeyedVector<int32_t, int32_t> mScanCodeStates;
1059 KeyedVector<int32_t, int32_t> mSwitchStates;
Philip Junker4af3b3d2021-12-14 10:36:55 +01001060 // fake mapping which would normally come from keyCharacterMap
1061 std::unordered_map<int32_t, int32_t> mKeyCodeMapping;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001062 std::vector<int32_t> mSupportedKeyCodes;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001063
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001064 std::mutex mLock;
1065 std::condition_variable mStateChangedCondition;
1066 bool mConfigureWasCalled GUARDED_BY(mLock);
1067 bool mResetWasCalled GUARDED_BY(mLock);
1068 bool mProcessWasCalled GUARDED_BY(mLock);
1069 RawEvent mLastEvent GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001070
Arthur Hungc23540e2018-11-29 20:42:11 +08001071 std::optional<DisplayViewport> mViewport;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001072public:
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001073 FakeInputMapper(InputDeviceContext& deviceContext, uint32_t sources)
1074 : InputMapper(deviceContext),
1075 mSources(sources),
1076 mKeyboardType(AINPUT_KEYBOARD_TYPE_NONE),
Michael Wrightd02c5b62014-02-10 15:10:22 -08001077 mMetaState(0),
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001078 mConfigureWasCalled(false),
1079 mResetWasCalled(false),
1080 mProcessWasCalled(false) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001081
Chris Yea52ade12020-08-27 16:49:20 -07001082 virtual ~FakeInputMapper() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001083
1084 void setKeyboardType(int32_t keyboardType) {
1085 mKeyboardType = keyboardType;
1086 }
1087
1088 void setMetaState(int32_t metaState) {
1089 mMetaState = metaState;
1090 }
1091
1092 void assertConfigureWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001093 std::unique_lock<std::mutex> lock(mLock);
1094 base::ScopedLockAssertion assumeLocked(mLock);
1095 const bool configureCalled =
1096 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1097 return mConfigureWasCalled;
1098 });
1099 if (!configureCalled) {
1100 FAIL() << "Expected configure() to have been called.";
1101 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001102 mConfigureWasCalled = false;
1103 }
1104
1105 void assertResetWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001106 std::unique_lock<std::mutex> lock(mLock);
1107 base::ScopedLockAssertion assumeLocked(mLock);
1108 const bool resetCalled =
1109 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1110 return mResetWasCalled;
1111 });
1112 if (!resetCalled) {
1113 FAIL() << "Expected reset() to have been called.";
1114 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001115 mResetWasCalled = false;
1116 }
1117
Yi Kong9b14ac62018-07-17 13:48:38 -07001118 void assertProcessWasCalled(RawEvent* outLastEvent = nullptr) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001119 std::unique_lock<std::mutex> lock(mLock);
1120 base::ScopedLockAssertion assumeLocked(mLock);
1121 const bool processCalled =
1122 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1123 return mProcessWasCalled;
1124 });
1125 if (!processCalled) {
1126 FAIL() << "Expected process() to have been called.";
1127 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001128 if (outLastEvent) {
1129 *outLastEvent = mLastEvent;
1130 }
1131 mProcessWasCalled = false;
1132 }
1133
1134 void setKeyCodeState(int32_t keyCode, int32_t state) {
1135 mKeyCodeStates.replaceValueFor(keyCode, state);
1136 }
1137
1138 void setScanCodeState(int32_t scanCode, int32_t state) {
1139 mScanCodeStates.replaceValueFor(scanCode, state);
1140 }
1141
1142 void setSwitchState(int32_t switchCode, int32_t state) {
1143 mSwitchStates.replaceValueFor(switchCode, state);
1144 }
1145
1146 void addSupportedKeyCode(int32_t keyCode) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001147 mSupportedKeyCodes.push_back(keyCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001148 }
1149
Philip Junker4af3b3d2021-12-14 10:36:55 +01001150 void addKeyCodeMapping(int32_t fromKeyCode, int32_t toKeyCode) {
1151 mKeyCodeMapping.insert_or_assign(fromKeyCode, toKeyCode);
1152 }
1153
Michael Wrightd02c5b62014-02-10 15:10:22 -08001154private:
Philip Junker4af3b3d2021-12-14 10:36:55 +01001155 uint32_t getSources() const override { return mSources; }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001156
Chris Yea52ade12020-08-27 16:49:20 -07001157 void populateDeviceInfo(InputDeviceInfo* deviceInfo) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001158 InputMapper::populateDeviceInfo(deviceInfo);
1159
1160 if (mKeyboardType != AINPUT_KEYBOARD_TYPE_NONE) {
1161 deviceInfo->setKeyboardType(mKeyboardType);
1162 }
1163 }
1164
Chris Yea52ade12020-08-27 16:49:20 -07001165 void configure(nsecs_t, const InputReaderConfiguration* config, uint32_t changes) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001166 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001167 mConfigureWasCalled = true;
Arthur Hungc23540e2018-11-29 20:42:11 +08001168
1169 // Find the associated viewport if exist.
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001170 const std::optional<uint8_t> displayPort = getDeviceContext().getAssociatedDisplayPort();
Arthur Hungc23540e2018-11-29 20:42:11 +08001171 if (displayPort && (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
1172 mViewport = config->getDisplayViewportByPort(*displayPort);
1173 }
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001174
1175 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001176 }
1177
Chris Yea52ade12020-08-27 16:49:20 -07001178 void reset(nsecs_t) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001179 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001180 mResetWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001181 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001182 }
1183
Chris Yea52ade12020-08-27 16:49:20 -07001184 void process(const RawEvent* rawEvent) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001185 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001186 mLastEvent = *rawEvent;
1187 mProcessWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001188 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001189 }
1190
Chris Yea52ade12020-08-27 16:49:20 -07001191 int32_t getKeyCodeState(uint32_t, int32_t keyCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001192 ssize_t index = mKeyCodeStates.indexOfKey(keyCode);
1193 return index >= 0 ? mKeyCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1194 }
1195
Philip Junker4af3b3d2021-12-14 10:36:55 +01001196 int32_t getKeyCodeForKeyLocation(int32_t locationKeyCode) const override {
1197 auto it = mKeyCodeMapping.find(locationKeyCode);
1198 return it != mKeyCodeMapping.end() ? it->second : locationKeyCode;
1199 }
1200
Chris Yea52ade12020-08-27 16:49:20 -07001201 int32_t getScanCodeState(uint32_t, int32_t scanCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001202 ssize_t index = mScanCodeStates.indexOfKey(scanCode);
1203 return index >= 0 ? mScanCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1204 }
1205
Chris Yea52ade12020-08-27 16:49:20 -07001206 int32_t getSwitchState(uint32_t, int32_t switchCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001207 ssize_t index = mSwitchStates.indexOfKey(switchCode);
1208 return index >= 0 ? mSwitchStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1209 }
1210
Chris Yea52ade12020-08-27 16:49:20 -07001211 // Return true if the device has non-empty key layout.
1212 bool markSupportedKeyCodes(uint32_t, size_t numCodes, const int32_t* keyCodes,
1213 uint8_t* outFlags) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001214 for (size_t i = 0; i < numCodes; i++) {
1215 for (size_t j = 0; j < mSupportedKeyCodes.size(); j++) {
1216 if (keyCodes[i] == mSupportedKeyCodes[j]) {
1217 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001218 }
1219 }
1220 }
Chris Yea52ade12020-08-27 16:49:20 -07001221 bool result = mSupportedKeyCodes.size() > 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001222 return result;
1223 }
1224
1225 virtual int32_t getMetaState() {
1226 return mMetaState;
1227 }
1228
1229 virtual void fadePointer() {
1230 }
Arthur Hungc23540e2018-11-29 20:42:11 +08001231
1232 virtual std::optional<int32_t> getAssociatedDisplay() {
1233 if (mViewport) {
1234 return std::make_optional(mViewport->displayId);
1235 }
1236 return std::nullopt;
1237 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001238};
1239
1240
1241// --- InstrumentedInputReader ---
1242
1243class InstrumentedInputReader : public InputReader {
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001244 std::queue<std::shared_ptr<InputDevice>> mNextDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001245
1246public:
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001247 InstrumentedInputReader(std::shared_ptr<EventHubInterface> eventHub,
1248 const sp<InputReaderPolicyInterface>& policy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001249 InputListenerInterface& listener)
arthurhungdcef2dc2020-08-11 14:47:50 +08001250 : InputReader(eventHub, policy, listener), mFakeContext(this) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001251
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001252 virtual ~InstrumentedInputReader() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001253
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001254 void pushNextDevice(std::shared_ptr<InputDevice> device) { mNextDevices.push(device); }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001255
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001256 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001257 const std::string& location = "") {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001258 InputDeviceIdentifier identifier;
1259 identifier.name = name;
Arthur Hungc23540e2018-11-29 20:42:11 +08001260 identifier.location = location;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001261 int32_t generation = deviceId + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08001262 return std::make_shared<InputDevice>(&mFakeContext, deviceId, generation, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001263 }
1264
Prabir Pradhan28efc192019-11-05 01:10:04 +00001265 // Make the protected loopOnce method accessible to tests.
1266 using InputReader::loopOnce;
1267
Michael Wrightd02c5b62014-02-10 15:10:22 -08001268protected:
Chris Ye1c2e0892020-11-30 21:41:44 -08001269 virtual std::shared_ptr<InputDevice> createDeviceLocked(int32_t eventHubId,
1270 const InputDeviceIdentifier& identifier)
1271 REQUIRES(mLock) {
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001272 if (!mNextDevices.empty()) {
1273 std::shared_ptr<InputDevice> device(std::move(mNextDevices.front()));
1274 mNextDevices.pop();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001275 return device;
1276 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001277 return InputReader::createDeviceLocked(eventHubId, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001278 }
1279
arthurhungdcef2dc2020-08-11 14:47:50 +08001280 // --- FakeInputReaderContext ---
1281 class FakeInputReaderContext : public ContextImpl {
1282 int32_t mGlobalMetaState;
1283 bool mUpdateGlobalMetaStateWasCalled;
1284 int32_t mGeneration;
1285
1286 public:
1287 FakeInputReaderContext(InputReader* reader)
1288 : ContextImpl(reader),
1289 mGlobalMetaState(0),
1290 mUpdateGlobalMetaStateWasCalled(false),
1291 mGeneration(1) {}
1292
1293 virtual ~FakeInputReaderContext() {}
1294
1295 void assertUpdateGlobalMetaStateWasCalled() {
1296 ASSERT_TRUE(mUpdateGlobalMetaStateWasCalled)
1297 << "Expected updateGlobalMetaState() to have been called.";
1298 mUpdateGlobalMetaStateWasCalled = false;
1299 }
1300
1301 void setGlobalMetaState(int32_t state) { mGlobalMetaState = state; }
1302
1303 uint32_t getGeneration() { return mGeneration; }
1304
1305 void updateGlobalMetaState() override {
1306 mUpdateGlobalMetaStateWasCalled = true;
1307 ContextImpl::updateGlobalMetaState();
1308 }
1309
1310 int32_t getGlobalMetaState() override {
1311 return mGlobalMetaState | ContextImpl::getGlobalMetaState();
1312 }
1313
1314 int32_t bumpGeneration() override {
1315 mGeneration = ContextImpl::bumpGeneration();
1316 return mGeneration;
1317 }
1318 } mFakeContext;
1319
Michael Wrightd02c5b62014-02-10 15:10:22 -08001320 friend class InputReaderTest;
arthurhungdcef2dc2020-08-11 14:47:50 +08001321
1322public:
1323 FakeInputReaderContext* getContext() { return &mFakeContext; }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001324};
1325
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001326// --- InputReaderPolicyTest ---
1327class InputReaderPolicyTest : public testing::Test {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001328protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001329 sp<FakeInputReaderPolicy> mFakePolicy;
1330
Chris Yea52ade12020-08-27 16:49:20 -07001331 void SetUp() override { mFakePolicy = new FakeInputReaderPolicy(); }
1332 void TearDown() override { mFakePolicy.clear(); }
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001333};
1334
1335/**
1336 * Check that empty set of viewports is an acceptable configuration.
1337 * Also try to get internal viewport two different ways - by type and by uniqueId.
1338 *
1339 * There will be confusion if two viewports with empty uniqueId and identical type are present.
1340 * Such configuration is not currently allowed.
1341 */
1342TEST_F(InputReaderPolicyTest, Viewports_GetCleared) {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001343 static const std::string uniqueId = "local:0";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001344
1345 // We didn't add any viewports yet, so there shouldn't be any.
1346 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001347 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001348 ASSERT_FALSE(internalViewport);
1349
1350 // Add an internal viewport, then clear it
1351 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001352 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId, NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001353 ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001354
1355 // Check matching by uniqueId
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001356 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001357 ASSERT_TRUE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001358 ASSERT_EQ(ViewportType::INTERNAL, internalViewport->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001359
1360 // Check matching by viewport type
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001361 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001362 ASSERT_TRUE(internalViewport);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001363 ASSERT_EQ(uniqueId, internalViewport->uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001364
1365 mFakePolicy->clearViewports();
1366 // Make sure nothing is found after clear
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001367 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001368 ASSERT_FALSE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001369 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001370 ASSERT_FALSE(internalViewport);
1371}
1372
1373TEST_F(InputReaderPolicyTest, Viewports_GetByType) {
1374 const std::string internalUniqueId = "local:0";
1375 const std::string externalUniqueId = "local:1";
1376 const std::string virtualUniqueId1 = "virtual:2";
1377 const std::string virtualUniqueId2 = "virtual:3";
1378 constexpr int32_t virtualDisplayId1 = 2;
1379 constexpr int32_t virtualDisplayId2 = 3;
1380
1381 // Add an internal viewport
1382 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001383 DISPLAY_ORIENTATION_0, true /*isActive*/, internalUniqueId,
1384 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001385 // Add an external viewport
1386 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001387 DISPLAY_ORIENTATION_0, true /*isActive*/, externalUniqueId,
1388 NO_PORT, ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001389 // Add an virtual viewport
1390 mFakePolicy->addDisplayViewport(virtualDisplayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001391 DISPLAY_ORIENTATION_0, true /*isActive*/, virtualUniqueId1,
1392 NO_PORT, ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001393 // Add another virtual viewport
1394 mFakePolicy->addDisplayViewport(virtualDisplayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001395 DISPLAY_ORIENTATION_0, true /*isActive*/, virtualUniqueId2,
1396 NO_PORT, ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001397
1398 // Check matching by type for internal
1399 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001400 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001401 ASSERT_TRUE(internalViewport);
1402 ASSERT_EQ(internalUniqueId, internalViewport->uniqueId);
1403
1404 // Check matching by type for external
1405 std::optional<DisplayViewport> externalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001406 mFakePolicy->getDisplayViewportByType(ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001407 ASSERT_TRUE(externalViewport);
1408 ASSERT_EQ(externalUniqueId, externalViewport->uniqueId);
1409
1410 // Check matching by uniqueId for virtual viewport #1
1411 std::optional<DisplayViewport> virtualViewport1 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001412 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001413 ASSERT_TRUE(virtualViewport1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001414 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport1->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001415 ASSERT_EQ(virtualUniqueId1, virtualViewport1->uniqueId);
1416 ASSERT_EQ(virtualDisplayId1, virtualViewport1->displayId);
1417
1418 // Check matching by uniqueId for virtual viewport #2
1419 std::optional<DisplayViewport> virtualViewport2 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001420 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001421 ASSERT_TRUE(virtualViewport2);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001422 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport2->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001423 ASSERT_EQ(virtualUniqueId2, virtualViewport2->uniqueId);
1424 ASSERT_EQ(virtualDisplayId2, virtualViewport2->displayId);
1425}
1426
1427
1428/**
1429 * We can have 2 viewports of the same kind. We can distinguish them by uniqueId, and confirm
1430 * that lookup works by checking display id.
1431 * Check that 2 viewports of each kind is possible, for all existing viewport types.
1432 */
1433TEST_F(InputReaderPolicyTest, Viewports_TwoOfSameType) {
1434 const std::string uniqueId1 = "uniqueId1";
1435 const std::string uniqueId2 = "uniqueId2";
1436 constexpr int32_t displayId1 = 2;
1437 constexpr int32_t displayId2 = 3;
1438
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001439 std::vector<ViewportType> types = {ViewportType::INTERNAL, ViewportType::EXTERNAL,
1440 ViewportType::VIRTUAL};
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001441 for (const ViewportType& type : types) {
1442 mFakePolicy->clearViewports();
1443 // Add a viewport
1444 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001445 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1,
1446 NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001447 // Add another viewport
1448 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001449 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2,
1450 NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001451
1452 // Check that correct display viewport was returned by comparing the display IDs.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001453 std::optional<DisplayViewport> viewport1 =
1454 mFakePolicy->getDisplayViewportByUniqueId(uniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001455 ASSERT_TRUE(viewport1);
1456 ASSERT_EQ(displayId1, viewport1->displayId);
1457 ASSERT_EQ(type, viewport1->type);
1458
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001459 std::optional<DisplayViewport> viewport2 =
1460 mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001461 ASSERT_TRUE(viewport2);
1462 ASSERT_EQ(displayId2, viewport2->displayId);
1463 ASSERT_EQ(type, viewport2->type);
1464
1465 // When there are multiple viewports of the same kind, and uniqueId is not specified
1466 // in the call to getDisplayViewport, then that situation is not supported.
1467 // The viewports can be stored in any order, so we cannot rely on the order, since that
1468 // is just implementation detail.
1469 // However, we can check that it still returns *a* viewport, we just cannot assert
1470 // which one specifically is returned.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001471 std::optional<DisplayViewport> someViewport = mFakePolicy->getDisplayViewportByType(type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001472 ASSERT_TRUE(someViewport);
1473 }
1474}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001475
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001476/**
Michael Wrightdde67b82020-10-27 16:09:22 +00001477 * When we have multiple internal displays make sure we always return the default display when
1478 * querying by type.
1479 */
1480TEST_F(InputReaderPolicyTest, Viewports_ByTypeReturnsDefaultForInternal) {
1481 const std::string uniqueId1 = "uniqueId1";
1482 const std::string uniqueId2 = "uniqueId2";
1483 constexpr int32_t nonDefaultDisplayId = 2;
1484 static_assert(nonDefaultDisplayId != ADISPLAY_ID_DEFAULT,
1485 "Test display ID should not be ADISPLAY_ID_DEFAULT");
1486
1487 // Add the default display first and ensure it gets returned.
1488 mFakePolicy->clearViewports();
1489 mFakePolicy->addDisplayViewport(ADISPLAY_ID_DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001490 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001491 ViewportType::INTERNAL);
1492 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001493 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001494 ViewportType::INTERNAL);
1495
1496 std::optional<DisplayViewport> viewport =
1497 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
1498 ASSERT_TRUE(viewport);
1499 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
1500 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
1501
1502 // Add the default display second to make sure order doesn't matter.
1503 mFakePolicy->clearViewports();
1504 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001505 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001506 ViewportType::INTERNAL);
1507 mFakePolicy->addDisplayViewport(ADISPLAY_ID_DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001508 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001509 ViewportType::INTERNAL);
1510
1511 viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
1512 ASSERT_TRUE(viewport);
1513 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
1514 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
1515}
1516
1517/**
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001518 * Check getDisplayViewportByPort
1519 */
1520TEST_F(InputReaderPolicyTest, Viewports_GetByPort) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001521 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001522 const std::string uniqueId1 = "uniqueId1";
1523 const std::string uniqueId2 = "uniqueId2";
1524 constexpr int32_t displayId1 = 1;
1525 constexpr int32_t displayId2 = 2;
1526 const uint8_t hdmi1 = 0;
1527 const uint8_t hdmi2 = 1;
1528 const uint8_t hdmi3 = 2;
1529
1530 mFakePolicy->clearViewports();
1531 // Add a viewport that's associated with some display port that's not of interest.
1532 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001533 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, hdmi3,
1534 type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001535 // Add another viewport, connected to HDMI1 port
1536 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001537 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, hdmi1,
1538 type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001539
1540 // Check that correct display viewport was returned by comparing the display ports.
1541 std::optional<DisplayViewport> hdmi1Viewport = mFakePolicy->getDisplayViewportByPort(hdmi1);
1542 ASSERT_TRUE(hdmi1Viewport);
1543 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1544 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1545
1546 // Check that we can still get the same viewport using the uniqueId
1547 hdmi1Viewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
1548 ASSERT_TRUE(hdmi1Viewport);
1549 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1550 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1551 ASSERT_EQ(type, hdmi1Viewport->type);
1552
1553 // Check that we cannot find a port with "HDMI2", because we never added one
1554 std::optional<DisplayViewport> hdmi2Viewport = mFakePolicy->getDisplayViewportByPort(hdmi2);
1555 ASSERT_FALSE(hdmi2Viewport);
1556}
1557
Michael Wrightd02c5b62014-02-10 15:10:22 -08001558// --- InputReaderTest ---
1559
1560class InputReaderTest : public testing::Test {
1561protected:
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001562 std::unique_ptr<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001563 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001564 std::shared_ptr<FakeEventHub> mFakeEventHub;
Prabir Pradhan28efc192019-11-05 01:10:04 +00001565 std::unique_ptr<InstrumentedInputReader> mReader;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001566
Chris Yea52ade12020-08-27 16:49:20 -07001567 void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001568 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001569 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001570 mFakeListener = std::make_unique<TestInputListener>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001571
Prabir Pradhan28efc192019-11-05 01:10:04 +00001572 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001573 *mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001574 }
1575
Chris Yea52ade12020-08-27 16:49:20 -07001576 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001577 mFakeListener.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001578 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001579 }
1580
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001581 void addDevice(int32_t eventHubId, const std::string& name,
1582 ftl::Flags<InputDeviceClass> classes, const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001583 mFakeEventHub->addDevice(eventHubId, name, classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001584
1585 if (configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001586 mFakeEventHub->addConfigurationMap(eventHubId, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001587 }
1588 mFakeEventHub->finishDeviceScan();
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001589 mReader->loopOnce();
1590 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001591 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1592 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001593 }
1594
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001595 void disableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001596 mFakePolicy->addDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001597 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001598 }
1599
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001600 void enableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001601 mFakePolicy->removeDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001602 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001603 }
1604
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001605 FakeInputMapper& addDeviceWithFakeInputMapper(int32_t deviceId, int32_t eventHubId,
Chris Ye1b0c7342020-07-28 21:57:03 -07001606 const std::string& name,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001607 ftl::Flags<InputDeviceClass> classes,
1608 uint32_t sources,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001609 const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001610 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, name);
1611 FakeInputMapper& mapper = device->addMapper<FakeInputMapper>(eventHubId, sources);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001612 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001613 addDevice(eventHubId, name, classes, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001614 return mapper;
1615 }
1616};
1617
Chris Ye98d3f532020-10-01 21:48:59 -07001618TEST_F(InputReaderTest, PolicyGetInputDevices) {
1619 ASSERT_NO_FATAL_FAILURE(addDevice(1, "keyboard", InputDeviceClass::KEYBOARD, nullptr));
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001620 ASSERT_NO_FATAL_FAILURE(addDevice(2, "ignored", ftl::Flags<InputDeviceClass>(0),
Chris Ye98d3f532020-10-01 21:48:59 -07001621 nullptr)); // no classes so device will be ignored
Michael Wrightd02c5b62014-02-10 15:10:22 -08001622
1623 // Should also have received a notification describing the new input devices.
Chris Ye98d3f532020-10-01 21:48:59 -07001624 const std::vector<InputDeviceInfo>& inputDevices = mFakePolicy->getInputDevices();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001625 ASSERT_EQ(1U, inputDevices.size());
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001626 ASSERT_EQ(END_RESERVED_ID + 1, inputDevices[0].getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001627 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001628 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
1629 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00001630 ASSERT_EQ(0U, inputDevices[0].getMotionRanges().size());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001631}
1632
Chris Yee7310032020-09-22 15:36:28 -07001633TEST_F(InputReaderTest, GetMergedInputDevices) {
1634 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1635 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1636 // Add two subdevices to device
1637 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1638 // Must add at least one mapper or the device will be ignored!
1639 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1640 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1641
1642 // Push same device instance for next device to be added, so they'll have same identifier.
1643 mReader->pushNextDevice(device);
1644 mReader->pushNextDevice(device);
1645 ASSERT_NO_FATAL_FAILURE(
1646 addDevice(eventHubIds[0], "fake1", InputDeviceClass::KEYBOARD, nullptr));
1647 ASSERT_NO_FATAL_FAILURE(
1648 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
1649
1650 // Two devices will be merged to one input device as they have same identifier
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00001651 ASSERT_EQ(1U, mFakePolicy->getInputDevices().size());
Chris Yee7310032020-09-22 15:36:28 -07001652}
1653
Chris Yee14523a2020-12-19 13:46:00 -08001654TEST_F(InputReaderTest, GetMergedInputDevicesEnabled) {
1655 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1656 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1657 // Add two subdevices to device
1658 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1659 // Must add at least one mapper or the device will be ignored!
1660 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1661 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1662
1663 // Push same device instance for next device to be added, so they'll have same identifier.
1664 mReader->pushNextDevice(device);
1665 mReader->pushNextDevice(device);
1666 // Sensor device is initially disabled
1667 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1",
1668 InputDeviceClass::KEYBOARD | InputDeviceClass::SENSOR,
1669 nullptr));
1670 // Device is disabled because the only sub device is a sensor device and disabled initially.
1671 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1672 ASSERT_FALSE(device->isEnabled());
1673 ASSERT_NO_FATAL_FAILURE(
1674 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
1675 // The merged device is enabled if any sub device is enabled
1676 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1677 ASSERT_TRUE(device->isEnabled());
1678}
1679
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001680TEST_F(InputReaderTest, WhenEnabledChanges_SendsDeviceResetNotification) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001681 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001682 constexpr ftl::Flags<InputDeviceClass> deviceClass(InputDeviceClass::KEYBOARD);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001683 constexpr int32_t eventHubId = 1;
1684 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001685 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001686 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001687 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001688 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001689
Yi Kong9b14ac62018-07-17 13:48:38 -07001690 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001691
1692 NotifyDeviceResetArgs resetArgs;
1693 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001694 ASSERT_EQ(deviceId, resetArgs.deviceId);
1695
1696 ASSERT_EQ(device->isEnabled(), true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001697 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001698 mReader->loopOnce();
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001699
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001700 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001701 ASSERT_EQ(deviceId, resetArgs.deviceId);
1702 ASSERT_EQ(device->isEnabled(), false);
1703
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001704 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001705 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001706 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
1707 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasNotCalled());
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001708 ASSERT_EQ(device->isEnabled(), false);
1709
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001710 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001711 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001712 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001713 ASSERT_EQ(deviceId, resetArgs.deviceId);
1714 ASSERT_EQ(device->isEnabled(), true);
1715}
1716
Michael Wrightd02c5b62014-02-10 15:10:22 -08001717TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001718 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001719 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001720 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001721 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001722 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001723 AINPUT_SOURCE_KEYBOARD, nullptr);
1724 mapper.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001725
1726 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(0,
1727 AINPUT_SOURCE_ANY, AKEYCODE_A))
1728 << "Should return unknown when the device id is >= 0 but unknown.";
1729
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001730 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1731 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1732 << "Should return unknown when the device id is valid but the sources are not "
1733 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001734
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001735 ASSERT_EQ(AKEY_STATE_DOWN,
1736 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1737 AKEYCODE_A))
1738 << "Should return value provided by mapper when device id is valid and the device "
1739 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001740
1741 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(-1,
1742 AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1743 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1744
1745 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getKeyCodeState(-1,
1746 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1747 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1748}
1749
Philip Junker4af3b3d2021-12-14 10:36:55 +01001750TEST_F(InputReaderTest, GetKeyCodeForKeyLocation_ForwardsRequestsToMappers) {
1751 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1752 constexpr int32_t eventHubId = 1;
1753 FakeInputMapper& mapper = addDeviceWithFakeInputMapper(deviceId, eventHubId, "keyboard",
1754 InputDeviceClass::KEYBOARD,
1755 AINPUT_SOURCE_KEYBOARD, nullptr);
1756 mapper.addKeyCodeMapping(AKEYCODE_Y, AKEYCODE_Z);
1757
1758 ASSERT_EQ(AKEYCODE_UNKNOWN, mReader->getKeyCodeForKeyLocation(0, AKEYCODE_Y))
1759 << "Should return unknown when the device with the specified id is not found.";
1760
1761 ASSERT_EQ(AKEYCODE_Z, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_Y))
1762 << "Should return correct mapping when device id is valid and mapping exists.";
1763
1764 ASSERT_EQ(AKEYCODE_A, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_A))
1765 << "Should return the location key code when device id is valid and there's no "
1766 "mapping.";
1767}
1768
1769TEST_F(InputReaderTest, GetKeyCodeForKeyLocation_NoKeyboardMapper) {
1770 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1771 constexpr int32_t eventHubId = 1;
1772 FakeInputMapper& mapper = addDeviceWithFakeInputMapper(deviceId, eventHubId, "joystick",
1773 InputDeviceClass::JOYSTICK,
1774 AINPUT_SOURCE_GAMEPAD, nullptr);
1775 mapper.addKeyCodeMapping(AKEYCODE_Y, AKEYCODE_Z);
1776
1777 ASSERT_EQ(AKEYCODE_UNKNOWN, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_Y))
1778 << "Should return unknown when the device id is valid but there is no keyboard mapper";
1779}
1780
Michael Wrightd02c5b62014-02-10 15:10:22 -08001781TEST_F(InputReaderTest, GetScanCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001782 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001783 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001784 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001785 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001786 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001787 AINPUT_SOURCE_KEYBOARD, nullptr);
1788 mapper.setScanCodeState(KEY_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001789
1790 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(0,
1791 AINPUT_SOURCE_ANY, KEY_A))
1792 << "Should return unknown when the device id is >= 0 but unknown.";
1793
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001794 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1795 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, KEY_A))
1796 << "Should return unknown when the device id is valid but the sources are not "
1797 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001798
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001799 ASSERT_EQ(AKEY_STATE_DOWN,
1800 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1801 KEY_A))
1802 << "Should return value provided by mapper when device id is valid and the device "
1803 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001804
1805 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(-1,
1806 AINPUT_SOURCE_TRACKBALL, KEY_A))
1807 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1808
1809 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getScanCodeState(-1,
1810 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, KEY_A))
1811 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1812}
1813
1814TEST_F(InputReaderTest, GetSwitchState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001815 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001816 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001817 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001818 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001819 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001820 AINPUT_SOURCE_KEYBOARD, nullptr);
1821 mapper.setSwitchState(SW_LID, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001822
1823 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(0,
1824 AINPUT_SOURCE_ANY, SW_LID))
1825 << "Should return unknown when the device id is >= 0 but unknown.";
1826
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001827 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1828 mReader->getSwitchState(deviceId, AINPUT_SOURCE_TRACKBALL, SW_LID))
1829 << "Should return unknown when the device id is valid but the sources are not "
1830 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001831
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001832 ASSERT_EQ(AKEY_STATE_DOWN,
1833 mReader->getSwitchState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1834 SW_LID))
1835 << "Should return value provided by mapper when device id is valid and the device "
1836 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001837
1838 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(-1,
1839 AINPUT_SOURCE_TRACKBALL, SW_LID))
1840 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1841
1842 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getSwitchState(-1,
1843 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, SW_LID))
1844 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1845}
1846
1847TEST_F(InputReaderTest, MarkSupportedKeyCodes_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001848 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001849 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001850 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001851 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001852 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001853 AINPUT_SOURCE_KEYBOARD, nullptr);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001854
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001855 mapper.addSupportedKeyCode(AKEYCODE_A);
1856 mapper.addSupportedKeyCode(AKEYCODE_B);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001857
1858 const int32_t keyCodes[4] = { AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2 };
1859 uint8_t flags[4] = { 0, 0, 0, 1 };
1860
1861 ASSERT_FALSE(mReader->hasKeys(0, AINPUT_SOURCE_ANY, 4, keyCodes, flags))
1862 << "Should return false when device id is >= 0 but unknown.";
1863 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1864
1865 flags[3] = 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001866 ASSERT_FALSE(mReader->hasKeys(deviceId, AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1867 << "Should return false when device id is valid but the sources are not supported by "
1868 "the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001869 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1870
1871 flags[3] = 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001872 ASSERT_TRUE(mReader->hasKeys(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, 4,
1873 keyCodes, flags))
1874 << "Should return value provided by mapper when device id is valid and the device "
1875 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001876 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1877
1878 flags[3] = 1;
1879 ASSERT_FALSE(mReader->hasKeys(-1, AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1880 << "Should return false when the device id is < 0 but the sources are not supported by any device.";
1881 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1882
1883 flags[3] = 1;
1884 ASSERT_TRUE(mReader->hasKeys(-1, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1885 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1886 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1887}
1888
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001889TEST_F(InputReaderTest, LoopOnce_WhenDeviceScanFinished_SendsConfigurationChanged) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001890 constexpr int32_t eventHubId = 1;
Chris Ye1b0c7342020-07-28 21:57:03 -07001891 addDevice(eventHubId, "ignored", InputDeviceClass::KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001892
1893 NotifyConfigurationChangedArgs args;
1894
1895 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(&args));
1896 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
1897}
1898
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001899TEST_F(InputReaderTest, LoopOnce_ForwardsRawEventsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001900 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001901 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001902 constexpr nsecs_t when = 0;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001903 constexpr int32_t eventHubId = 1;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001904 constexpr nsecs_t readTime = 2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001905 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001906 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001907 AINPUT_SOURCE_KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001908
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001909 mFakeEventHub->enqueueEvent(when, readTime, eventHubId, EV_KEY, KEY_A, 1);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001910 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001911 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
1912
1913 RawEvent event;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001914 ASSERT_NO_FATAL_FAILURE(mapper.assertProcessWasCalled(&event));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001915 ASSERT_EQ(when, event.when);
1916 ASSERT_EQ(readTime, event.readTime);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001917 ASSERT_EQ(eventHubId, event.deviceId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001918 ASSERT_EQ(EV_KEY, event.type);
1919 ASSERT_EQ(KEY_A, event.code);
1920 ASSERT_EQ(1, event.value);
1921}
1922
Garfield Tan1c7bc862020-01-28 13:24:04 -08001923TEST_F(InputReaderTest, DeviceReset_RandomId) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001924 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001925 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001926 constexpr int32_t eventHubId = 1;
1927 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Prabir Pradhan42611e02018-11-27 14:04:02 -08001928 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001929 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001930 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001931 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan42611e02018-11-27 14:04:02 -08001932
1933 NotifyDeviceResetArgs resetArgs;
1934 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001935 int32_t prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001936
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001937 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001938 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001939 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001940 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001941 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001942
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001943 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001944 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001945 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001946 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001947 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001948
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001949 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001950 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001951 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001952 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001953 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001954}
1955
Garfield Tan1c7bc862020-01-28 13:24:04 -08001956TEST_F(InputReaderTest, DeviceReset_GenerateIdWithInputReaderSource) {
1957 constexpr int32_t deviceId = 1;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001958 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Garfield Tan1c7bc862020-01-28 13:24:04 -08001959 constexpr int32_t eventHubId = 1;
1960 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1961 // Must add at least one mapper or the device will be ignored!
1962 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001963 mReader->pushNextDevice(device);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001964 ASSERT_NO_FATAL_FAILURE(addDevice(deviceId, "fake", deviceClass, nullptr));
1965
1966 NotifyDeviceResetArgs resetArgs;
1967 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1968 ASSERT_EQ(IdGenerator::Source::INPUT_READER, IdGenerator::getSource(resetArgs.id));
1969}
1970
Arthur Hungc23540e2018-11-29 20:42:11 +08001971TEST_F(InputReaderTest, Device_CanDispatchToDisplay) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001972 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001973 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001974 constexpr int32_t eventHubId = 1;
Arthur Hungc23540e2018-11-29 20:42:11 +08001975 const char* DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001976 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1977 FakeInputMapper& mapper =
1978 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001979 mReader->pushNextDevice(device);
Arthur Hungc23540e2018-11-29 20:42:11 +08001980
1981 const uint8_t hdmi1 = 1;
1982
1983 // Associated touch screen with second display.
1984 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
1985
1986 // Add default and second display.
Prabir Pradhan28efc192019-11-05 01:10:04 +00001987 mFakePolicy->clearViewports();
Arthur Hungc23540e2018-11-29 20:42:11 +08001988 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001989 DISPLAY_ORIENTATION_0, true /*isActive*/, "local:0", NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001990 ViewportType::INTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08001991 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001992 DISPLAY_ORIENTATION_0, true /*isActive*/, "local:1", hdmi1,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001993 ViewportType::EXTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08001994 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001995 mReader->loopOnce();
Prabir Pradhan28efc192019-11-05 01:10:04 +00001996
1997 // Add the device, and make sure all of the callbacks are triggered.
1998 // The device is added after the input port associations are processed since
1999 // we do not yet support dynamic device-to-display associations.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002000 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07002001 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled());
Prabir Pradhan28efc192019-11-05 01:10:04 +00002002 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002003 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
Arthur Hungc23540e2018-11-29 20:42:11 +08002004
Arthur Hung2c9a3342019-07-23 14:18:59 +08002005 // Device should only dispatch to the specified display.
Arthur Hungc23540e2018-11-29 20:42:11 +08002006 ASSERT_EQ(deviceId, device->getId());
2007 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, DISPLAY_ID));
2008 ASSERT_TRUE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hung2c9a3342019-07-23 14:18:59 +08002009
2010 // Can't dispatch event from a disabled device.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002011 disableDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00002012 mReader->loopOnce();
Arthur Hung2c9a3342019-07-23 14:18:59 +08002013 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hungc23540e2018-11-29 20:42:11 +08002014}
2015
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08002016TEST_F(InputReaderTest, WhenEnabledChanges_AllSubdevicesAreUpdated) {
2017 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002018 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08002019 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
2020 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
2021 // Must add at least one mapper or the device will be ignored!
2022 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
2023 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
2024 mReader->pushNextDevice(device);
2025 mReader->pushNextDevice(device);
2026 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
2027 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
2028
2029 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
2030
2031 NotifyDeviceResetArgs resetArgs;
2032 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2033 ASSERT_EQ(deviceId, resetArgs.deviceId);
2034 ASSERT_TRUE(device->isEnabled());
2035 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
2036 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
2037
2038 disableDevice(deviceId);
2039 mReader->loopOnce();
2040
2041 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2042 ASSERT_EQ(deviceId, resetArgs.deviceId);
2043 ASSERT_FALSE(device->isEnabled());
2044 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
2045 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
2046
2047 enableDevice(deviceId);
2048 mReader->loopOnce();
2049
2050 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2051 ASSERT_EQ(deviceId, resetArgs.deviceId);
2052 ASSERT_TRUE(device->isEnabled());
2053 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
2054 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
2055}
2056
2057TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToSubdeviceMappers) {
2058 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002059 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08002060 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
2061 // Add two subdevices to device
2062 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
2063 FakeInputMapper& mapperDevice1 =
2064 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
2065 FakeInputMapper& mapperDevice2 =
2066 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
2067 mReader->pushNextDevice(device);
2068 mReader->pushNextDevice(device);
2069 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
2070 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
2071
2072 mapperDevice1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
2073 mapperDevice2.setKeyCodeState(AKEYCODE_B, AKEY_STATE_DOWN);
2074
2075 ASSERT_EQ(AKEY_STATE_DOWN,
2076 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_A));
2077 ASSERT_EQ(AKEY_STATE_DOWN,
2078 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_B));
2079 ASSERT_EQ(AKEY_STATE_UNKNOWN,
2080 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_C));
2081}
2082
Prabir Pradhan7e186182020-11-10 13:56:45 -08002083TEST_F(InputReaderTest, ChangingPointerCaptureNotifiesInputListener) {
2084 NotifyPointerCaptureChangedArgs args;
2085
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002086 auto request = mFakePolicy->setPointerCapture(true);
Prabir Pradhan7e186182020-11-10 13:56:45 -08002087 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
2088 mReader->loopOnce();
2089 mFakeListener->assertNotifyCaptureWasCalled(&args);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002090 ASSERT_TRUE(args.request.enable) << "Pointer Capture should be enabled.";
2091 ASSERT_EQ(args.request, request) << "Pointer Capture sequence number should match.";
Prabir Pradhan7e186182020-11-10 13:56:45 -08002092
2093 mFakePolicy->setPointerCapture(false);
2094 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
2095 mReader->loopOnce();
2096 mFakeListener->assertNotifyCaptureWasCalled(&args);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002097 ASSERT_FALSE(args.request.enable) << "Pointer Capture should be disabled.";
Prabir Pradhan7e186182020-11-10 13:56:45 -08002098
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002099 // Verify that the Pointer Capture state is not updated when the configuration value
Prabir Pradhan7e186182020-11-10 13:56:45 -08002100 // does not change.
2101 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
2102 mReader->loopOnce();
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002103 mFakeListener->assertNotifyCaptureWasNotCalled();
Prabir Pradhan7e186182020-11-10 13:56:45 -08002104}
2105
Chris Ye87143712020-11-10 05:05:58 +00002106class FakeVibratorInputMapper : public FakeInputMapper {
2107public:
2108 FakeVibratorInputMapper(InputDeviceContext& deviceContext, uint32_t sources)
2109 : FakeInputMapper(deviceContext, sources) {}
2110
2111 std::vector<int32_t> getVibratorIds() override { return getDeviceContext().getVibratorIds(); }
2112};
2113
2114TEST_F(InputReaderTest, VibratorGetVibratorIds) {
2115 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002116 ftl::Flags<InputDeviceClass> deviceClass =
2117 InputDeviceClass::KEYBOARD | InputDeviceClass::VIBRATOR;
Chris Ye87143712020-11-10 05:05:58 +00002118 constexpr int32_t eventHubId = 1;
2119 const char* DEVICE_LOCATION = "BLUETOOTH";
2120 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
2121 FakeVibratorInputMapper& mapper =
2122 device->addMapper<FakeVibratorInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
2123 mReader->pushNextDevice(device);
2124
2125 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2126 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
2127
2128 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
2129 ASSERT_EQ(mReader->getVibratorIds(deviceId).size(), 2U);
2130}
2131
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002132// --- FakePeripheralController ---
Kim Low03ea0352020-11-06 12:45:07 -08002133
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002134class FakePeripheralController : public PeripheralControllerInterface {
Chris Yee2b1e5c2021-03-10 22:45:12 -08002135public:
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002136 FakePeripheralController(InputDeviceContext& deviceContext) : mDeviceContext(deviceContext) {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08002137
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002138 ~FakePeripheralController() override {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08002139
2140 void populateDeviceInfo(InputDeviceInfo* deviceInfo) override {}
2141
2142 void dump(std::string& dump) override {}
2143
2144 std::optional<int32_t> getBatteryCapacity(int32_t batteryId) override {
2145 return getDeviceContext().getBatteryCapacity(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08002146 }
2147
Chris Yee2b1e5c2021-03-10 22:45:12 -08002148 std::optional<int32_t> getBatteryStatus(int32_t batteryId) override {
2149 return getDeviceContext().getBatteryStatus(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08002150 }
Chris Ye3fdbfef2021-01-06 18:45:18 -08002151
2152 bool setLightColor(int32_t lightId, int32_t color) override {
2153 getDeviceContext().setLightBrightness(lightId, color >> 24);
2154 return true;
2155 }
2156
2157 std::optional<int32_t> getLightColor(int32_t lightId) override {
2158 std::optional<int32_t> result = getDeviceContext().getLightBrightness(lightId);
2159 if (!result.has_value()) {
2160 return std::nullopt;
2161 }
2162 return result.value() << 24;
2163 }
Chris Yee2b1e5c2021-03-10 22:45:12 -08002164
2165 bool setLightPlayerId(int32_t lightId, int32_t playerId) override { return true; }
2166
2167 std::optional<int32_t> getLightPlayerId(int32_t lightId) override { return std::nullopt; }
2168
2169private:
2170 InputDeviceContext& mDeviceContext;
2171 inline int32_t getDeviceId() { return mDeviceContext.getId(); }
2172 inline InputDeviceContext& getDeviceContext() { return mDeviceContext; }
Chris Ye3fdbfef2021-01-06 18:45:18 -08002173};
2174
Chris Yee2b1e5c2021-03-10 22:45:12 -08002175TEST_F(InputReaderTest, BatteryGetCapacity) {
2176 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002177 ftl::Flags<InputDeviceClass> deviceClass =
2178 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
Chris Yee2b1e5c2021-03-10 22:45:12 -08002179 constexpr int32_t eventHubId = 1;
2180 const char* DEVICE_LOCATION = "BLUETOOTH";
2181 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002182 FakePeripheralController& controller =
2183 device->addController<FakePeripheralController>(eventHubId);
Chris Yee2b1e5c2021-03-10 22:45:12 -08002184 mReader->pushNextDevice(device);
2185
2186 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2187
2188 ASSERT_EQ(controller.getBatteryCapacity(DEFAULT_BATTERY), BATTERY_CAPACITY);
2189 ASSERT_EQ(mReader->getBatteryCapacity(deviceId), BATTERY_CAPACITY);
2190}
2191
2192TEST_F(InputReaderTest, BatteryGetStatus) {
2193 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002194 ftl::Flags<InputDeviceClass> deviceClass =
2195 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
Chris Yee2b1e5c2021-03-10 22:45:12 -08002196 constexpr int32_t eventHubId = 1;
2197 const char* DEVICE_LOCATION = "BLUETOOTH";
2198 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002199 FakePeripheralController& controller =
2200 device->addController<FakePeripheralController>(eventHubId);
Chris Yee2b1e5c2021-03-10 22:45:12 -08002201 mReader->pushNextDevice(device);
2202
2203 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2204
2205 ASSERT_EQ(controller.getBatteryStatus(DEFAULT_BATTERY), BATTERY_STATUS);
2206 ASSERT_EQ(mReader->getBatteryStatus(deviceId), BATTERY_STATUS);
2207}
2208
Chris Ye3fdbfef2021-01-06 18:45:18 -08002209TEST_F(InputReaderTest, LightGetColor) {
2210 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002211 ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD | InputDeviceClass::LIGHT;
Chris Ye3fdbfef2021-01-06 18:45:18 -08002212 constexpr int32_t eventHubId = 1;
2213 const char* DEVICE_LOCATION = "BLUETOOTH";
2214 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002215 FakePeripheralController& controller =
2216 device->addController<FakePeripheralController>(eventHubId);
Chris Ye3fdbfef2021-01-06 18:45:18 -08002217 mReader->pushNextDevice(device);
2218 RawLightInfo info = {.id = 1,
2219 .name = "Mono",
2220 .maxBrightness = 255,
2221 .flags = InputLightClass::BRIGHTNESS,
2222 .path = ""};
2223 mFakeEventHub->addRawLightInfo(1 /* rawId */, std::move(info));
2224 mFakeEventHub->fakeLightBrightness(1 /* rawId */, 0x55);
2225
2226 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Chris Ye3fdbfef2021-01-06 18:45:18 -08002227
Chris Yee2b1e5c2021-03-10 22:45:12 -08002228 ASSERT_TRUE(controller.setLightColor(1 /* lightId */, LIGHT_BRIGHTNESS));
2229 ASSERT_EQ(controller.getLightColor(1 /* lightId */), LIGHT_BRIGHTNESS);
Chris Ye3fdbfef2021-01-06 18:45:18 -08002230 ASSERT_TRUE(mReader->setLightColor(deviceId, 1 /* lightId */, LIGHT_BRIGHTNESS));
2231 ASSERT_EQ(mReader->getLightColor(deviceId, 1 /* lightId */), LIGHT_BRIGHTNESS);
2232}
2233
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002234// --- InputReaderIntegrationTest ---
2235
2236// These tests create and interact with the InputReader only through its interface.
2237// The InputReader is started during SetUp(), which starts its processing in its own
2238// thread. The tests use linux uinput to emulate input devices.
2239// NOTE: Interacting with the physical device while these tests are running may cause
2240// the tests to fail.
2241class InputReaderIntegrationTest : public testing::Test {
2242protected:
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002243 std::unique_ptr<TestInputListener> mTestListener;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002244 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002245 std::unique_ptr<InputReaderInterface> mReader;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002246
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00002247 std::shared_ptr<FakePointerController> mFakePointerController;
2248
Chris Yea52ade12020-08-27 16:49:20 -07002249 void SetUp() override {
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002250 mFakePolicy = new FakeInputReaderPolicy();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00002251 mFakePointerController = std::make_shared<FakePointerController>();
2252 mFakePolicy->setPointerController(mFakePointerController);
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002253 mTestListener = std::make_unique<TestInputListener>(2000ms /*eventHappenedTimeout*/,
2254 30ms /*eventDidNotHappenTimeout*/);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002255
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002256 mReader = std::make_unique<InputReader>(std::make_shared<EventHub>(), mFakePolicy,
2257 *mTestListener);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002258 ASSERT_EQ(mReader->start(), OK);
2259
2260 // Since this test is run on a real device, all the input devices connected
2261 // to the test device will show up in mReader. We wait for those input devices to
2262 // show up before beginning the tests.
2263 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2264 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2265 }
2266
Chris Yea52ade12020-08-27 16:49:20 -07002267 void TearDown() override {
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002268 ASSERT_EQ(mReader->stop(), OK);
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002269 mReader.reset();
2270 mTestListener.reset();
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002271 mFakePolicy.clear();
2272 }
2273};
2274
2275TEST_F(InputReaderIntegrationTest, TestInvalidDevice) {
2276 // An invalid input device that is only used for this test.
2277 class InvalidUinputDevice : public UinputDevice {
2278 public:
2279 InvalidUinputDevice() : UinputDevice("Invalid Device") {}
2280
2281 private:
2282 void configureDevice(int fd, uinput_user_dev* device) override {}
2283 };
2284
2285 const size_t numDevices = mFakePolicy->getInputDevices().size();
2286
2287 // UinputDevice does not set any event or key bits, so InputReader should not
2288 // consider it as a valid device.
2289 std::unique_ptr<UinputDevice> invalidDevice = createUinputDevice<InvalidUinputDevice>();
2290 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
2291 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
2292 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
2293
2294 invalidDevice.reset();
2295 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
2296 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
2297 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
2298}
2299
2300TEST_F(InputReaderIntegrationTest, AddNewDevice) {
2301 const size_t initialNumDevices = mFakePolicy->getInputDevices().size();
2302
2303 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
2304 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2305 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2306 ASSERT_EQ(initialNumDevices + 1, mFakePolicy->getInputDevices().size());
2307
2308 // Find the test device by its name.
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002309 const std::vector<InputDeviceInfo> inputDevices = mFakePolicy->getInputDevices();
Chris Ye98d3f532020-10-01 21:48:59 -07002310 const auto& it =
2311 std::find_if(inputDevices.begin(), inputDevices.end(),
2312 [&keyboard](const InputDeviceInfo& info) {
2313 return info.getIdentifier().name == keyboard->getName();
2314 });
2315
2316 ASSERT_NE(it, inputDevices.end());
2317 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, it->getKeyboardType());
2318 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, it->getSources());
2319 ASSERT_EQ(0U, it->getMotionRanges().size());
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002320
2321 keyboard.reset();
2322 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2323 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2324 ASSERT_EQ(initialNumDevices, mFakePolicy->getInputDevices().size());
2325}
2326
2327TEST_F(InputReaderIntegrationTest, SendsEventsToInputListener) {
2328 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
2329 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2330
2331 NotifyConfigurationChangedArgs configChangedArgs;
2332 ASSERT_NO_FATAL_FAILURE(
2333 mTestListener->assertNotifyConfigurationChangedWasCalled(&configChangedArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002334 int32_t prevId = configChangedArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002335 nsecs_t prevTimestamp = configChangedArgs.eventTime;
2336
2337 NotifyKeyArgs keyArgs;
2338 keyboard->pressAndReleaseHomeKey();
2339 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
2340 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002341 ASSERT_NE(prevId, keyArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002342 prevId = keyArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002343 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002344 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002345 prevTimestamp = keyArgs.eventTime;
2346
2347 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
2348 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002349 ASSERT_NE(prevId, keyArgs.id);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002350 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002351 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002352}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002353
Siarhei Vishniakoua0d2b802020-05-13 14:00:31 -07002354/**
2355 * The Steam controller sends BTN_GEAR_DOWN and BTN_GEAR_UP for the two "paddle" buttons
2356 * on the back. In this test, we make sure that BTN_GEAR_DOWN / BTN_WHEEL and BTN_GEAR_UP
2357 * are passed to the listener.
2358 */
2359static_assert(BTN_GEAR_DOWN == BTN_WHEEL);
2360TEST_F(InputReaderIntegrationTest, SendsGearDownAndUpToInputListener) {
2361 std::unique_ptr<UinputSteamController> controller = createUinputDevice<UinputSteamController>();
2362 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2363 NotifyKeyArgs keyArgs;
2364
2365 controller->pressAndReleaseKey(BTN_GEAR_DOWN);
2366 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
2367 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
2368 ASSERT_EQ(BTN_GEAR_DOWN, keyArgs.scanCode);
2369
2370 controller->pressAndReleaseKey(BTN_GEAR_UP);
2371 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
2372 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
2373 ASSERT_EQ(BTN_GEAR_UP, keyArgs.scanCode);
2374}
2375
Arthur Hungaab25622020-01-16 11:22:11 +08002376// --- TouchProcessTest ---
2377class TouchIntegrationTest : public InputReaderIntegrationTest {
2378protected:
Arthur Hungaab25622020-01-16 11:22:11 +08002379 const std::string UNIQUE_ID = "local:0";
2380
Chris Yea52ade12020-08-27 16:49:20 -07002381 void SetUp() override {
Arthur Hungaab25622020-01-16 11:22:11 +08002382 InputReaderIntegrationTest::SetUp();
2383 // At least add an internal display.
2384 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2385 DISPLAY_ORIENTATION_0, UNIQUE_ID, NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002386 ViewportType::INTERNAL);
Arthur Hungaab25622020-01-16 11:22:11 +08002387
2388 mDevice = createUinputDevice<UinputTouchScreen>(Rect(0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT));
2389 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2390 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2391 }
2392
2393 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
2394 int32_t orientation, const std::string& uniqueId,
2395 std::optional<uint8_t> physicalPort,
2396 ViewportType viewportType) {
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002397 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, true /*isActive*/,
2398 uniqueId, physicalPort, viewportType);
Arthur Hungaab25622020-01-16 11:22:11 +08002399 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2400 }
2401
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002402 void assertReceivedMotion(int32_t action, const std::vector<Point>& points) {
2403 NotifyMotionArgs args;
2404 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2405 EXPECT_EQ(action, args.action);
2406 ASSERT_EQ(points.size(), args.pointerCount);
2407 for (size_t i = 0; i < args.pointerCount; i++) {
2408 EXPECT_EQ(points[i].x, args.pointerCoords[i].getX());
2409 EXPECT_EQ(points[i].y, args.pointerCoords[i].getY());
2410 }
2411 }
2412
Arthur Hungaab25622020-01-16 11:22:11 +08002413 std::unique_ptr<UinputTouchScreen> mDevice;
2414};
2415
2416TEST_F(TouchIntegrationTest, InputEvent_ProcessSingleTouch) {
2417 NotifyMotionArgs args;
2418 const Point centerPoint = mDevice->getCenterPoint();
2419
2420 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00002421 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002422 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002423 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002424 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2425 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2426
2427 // ACTION_MOVE
2428 mDevice->sendMove(centerPoint + Point(1, 1));
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002429 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002430 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2431 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2432
2433 // ACTION_UP
2434 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002435 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002436 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2437 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2438}
2439
2440TEST_F(TouchIntegrationTest, InputEvent_ProcessMultiTouch) {
2441 NotifyMotionArgs args;
2442 const Point centerPoint = mDevice->getCenterPoint();
2443
2444 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00002445 mDevice->sendSlot(FIRST_SLOT);
2446 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002447 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002448 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002449 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2450 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2451
2452 // ACTION_POINTER_DOWN (Second slot)
2453 const Point secondPoint = centerPoint + Point(100, 100);
2454 mDevice->sendSlot(SECOND_SLOT);
2455 mDevice->sendTrackingId(SECOND_TRACKING_ID);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002456 mDevice->sendDown(secondPoint);
2457 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002458 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08002459 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002460
2461 // ACTION_MOVE (Second slot)
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002462 mDevice->sendMove(secondPoint + Point(1, 1));
2463 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002464 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2465 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2466
2467 // ACTION_POINTER_UP (Second slot)
arthurhungcc7f9802020-04-30 17:55:40 +08002468 mDevice->sendPointerUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002469 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002470 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08002471 ASSERT_EQ(ACTION_POINTER_1_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002472
2473 // ACTION_UP
2474 mDevice->sendSlot(FIRST_SLOT);
2475 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002476 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002477 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2478 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2479}
2480
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002481/**
2482 * What happens when a pointer goes up while another pointer moves in the same frame? Are POINTER_UP
2483 * events guaranteed to contain the same data as a preceding MOVE, or can they contain different
2484 * data?
2485 * In this test, we try to send a change in coordinates in Pointer 0 in the same frame as the
2486 * liftoff of Pointer 1. We check that POINTER_UP event is generated first, and the MOVE event
2487 * for Pointer 0 only is generated after.
2488 * Suppose we are only interested in learning the movement of Pointer 0. If we only observe MOVE
2489 * events, we will not miss any information.
2490 * Even though the Pointer 1 up event contains updated Pointer 0 coordinates, there is another MOVE
2491 * event generated afterwards that contains the newest movement of pointer 0.
2492 * This is important for palm rejection. If there is a subsequent InputListener stage that detects
2493 * palms, and wants to cancel Pointer 1, then it is safe to simply drop POINTER_1_UP event without
2494 * losing information about non-palm pointers.
2495 */
2496TEST_F(TouchIntegrationTest, MultiTouch_PointerMoveAndSecondPointerUp) {
2497 NotifyMotionArgs args;
2498 const Point centerPoint = mDevice->getCenterPoint();
2499
2500 // ACTION_DOWN
2501 mDevice->sendSlot(FIRST_SLOT);
2502 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2503 mDevice->sendDown(centerPoint);
2504 mDevice->sendSync();
2505 assertReceivedMotion(AMOTION_EVENT_ACTION_DOWN, {centerPoint});
2506
2507 // ACTION_POINTER_DOWN (Second slot)
2508 const Point secondPoint = centerPoint + Point(100, 100);
2509 mDevice->sendSlot(SECOND_SLOT);
2510 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2511 mDevice->sendDown(secondPoint);
2512 mDevice->sendSync();
2513 assertReceivedMotion(ACTION_POINTER_1_DOWN, {centerPoint, secondPoint});
2514
2515 // ACTION_MOVE (First slot)
2516 mDevice->sendSlot(FIRST_SLOT);
2517 mDevice->sendMove(centerPoint + Point(5, 5));
2518 // ACTION_POINTER_UP (Second slot)
2519 mDevice->sendSlot(SECOND_SLOT);
2520 mDevice->sendPointerUp();
2521 // Send a single sync for the above 2 pointer updates
2522 mDevice->sendSync();
2523
2524 // First, we should get POINTER_UP for the second pointer
2525 assertReceivedMotion(ACTION_POINTER_1_UP,
2526 {/*first pointer */ centerPoint + Point(5, 5),
2527 /*second pointer*/ secondPoint});
2528
2529 // Next, the MOVE event for the first pointer
2530 assertReceivedMotion(AMOTION_EVENT_ACTION_MOVE, {centerPoint + Point(5, 5)});
2531}
2532
2533/**
2534 * Similar scenario as above. The difference is that when the second pointer goes up, it will first
2535 * move, and then it will go up, all in the same frame.
2536 * In this scenario, the movement of the second pointer just prior to liftoff is ignored, and never
2537 * gets sent to the listener.
2538 */
2539TEST_F(TouchIntegrationTest, MultiTouch_PointerMoveAndSecondPointerMoveAndUp) {
2540 NotifyMotionArgs args;
2541 const Point centerPoint = mDevice->getCenterPoint();
2542
2543 // ACTION_DOWN
2544 mDevice->sendSlot(FIRST_SLOT);
2545 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2546 mDevice->sendDown(centerPoint);
2547 mDevice->sendSync();
2548 assertReceivedMotion(AMOTION_EVENT_ACTION_DOWN, {centerPoint});
2549
2550 // ACTION_POINTER_DOWN (Second slot)
2551 const Point secondPoint = centerPoint + Point(100, 100);
2552 mDevice->sendSlot(SECOND_SLOT);
2553 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2554 mDevice->sendDown(secondPoint);
2555 mDevice->sendSync();
2556 assertReceivedMotion(ACTION_POINTER_1_DOWN, {centerPoint, secondPoint});
2557
2558 // ACTION_MOVE (First slot)
2559 mDevice->sendSlot(FIRST_SLOT);
2560 mDevice->sendMove(centerPoint + Point(5, 5));
2561 // ACTION_POINTER_UP (Second slot)
2562 mDevice->sendSlot(SECOND_SLOT);
2563 mDevice->sendMove(secondPoint + Point(6, 6));
2564 mDevice->sendPointerUp();
2565 // Send a single sync for the above 2 pointer updates
2566 mDevice->sendSync();
2567
2568 // First, we should get POINTER_UP for the second pointer
2569 // The movement of the second pointer during the liftoff frame is ignored.
2570 // The coordinates 'secondPoint + Point(6, 6)' are never sent to the listener.
2571 assertReceivedMotion(ACTION_POINTER_1_UP,
2572 {/*first pointer */ centerPoint + Point(5, 5),
2573 /*second pointer*/ secondPoint});
2574
2575 // Next, the MOVE event for the first pointer
2576 assertReceivedMotion(AMOTION_EVENT_ACTION_MOVE, {centerPoint + Point(5, 5)});
2577}
2578
Arthur Hungaab25622020-01-16 11:22:11 +08002579TEST_F(TouchIntegrationTest, InputEvent_ProcessPalm) {
2580 NotifyMotionArgs args;
2581 const Point centerPoint = mDevice->getCenterPoint();
2582
2583 // ACTION_DOWN
arthurhungcc7f9802020-04-30 17:55:40 +08002584 mDevice->sendSlot(FIRST_SLOT);
2585 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002586 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002587 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002588 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2589 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2590
arthurhungcc7f9802020-04-30 17:55:40 +08002591 // ACTION_POINTER_DOWN (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002592 const Point secondPoint = centerPoint + Point(100, 100);
2593 mDevice->sendSlot(SECOND_SLOT);
2594 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2595 mDevice->sendDown(secondPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002596 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002597 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08002598 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002599
arthurhungcc7f9802020-04-30 17:55:40 +08002600 // ACTION_MOVE (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002601 mDevice->sendMove(secondPoint + Point(1, 1));
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002602 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002603 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2604 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2605
arthurhungcc7f9802020-04-30 17:55:40 +08002606 // Send MT_TOOL_PALM (second slot), which indicates that the touch IC has determined this to be
2607 // a palm event.
2608 // Expect to receive the ACTION_POINTER_UP with cancel flag.
Arthur Hungaab25622020-01-16 11:22:11 +08002609 mDevice->sendToolType(MT_TOOL_PALM);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002610 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002611 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08002612 ASSERT_EQ(ACTION_POINTER_1_UP, args.action);
arthurhungcc7f9802020-04-30 17:55:40 +08002613 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, args.flags);
Arthur Hungaab25622020-01-16 11:22:11 +08002614
arthurhungcc7f9802020-04-30 17:55:40 +08002615 // Send up to second slot, expect first slot send moving.
2616 mDevice->sendPointerUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002617 mDevice->sendSync();
arthurhungcc7f9802020-04-30 17:55:40 +08002618 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2619 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002620
arthurhungcc7f9802020-04-30 17:55:40 +08002621 // Send ACTION_UP (first slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002622 mDevice->sendSlot(FIRST_SLOT);
2623 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002624 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002625
arthurhungcc7f9802020-04-30 17:55:40 +08002626 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2627 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002628}
2629
Michael Wrightd02c5b62014-02-10 15:10:22 -08002630// --- InputDeviceTest ---
Michael Wrightd02c5b62014-02-10 15:10:22 -08002631class InputDeviceTest : public testing::Test {
2632protected:
2633 static const char* DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002634 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002635 static const int32_t DEVICE_ID;
2636 static const int32_t DEVICE_GENERATION;
2637 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002638 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002639 static const int32_t EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002640
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002641 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002642 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002643 std::unique_ptr<TestInputListener> mFakeListener;
arthurhungdcef2dc2020-08-11 14:47:50 +08002644 std::unique_ptr<InstrumentedInputReader> mReader;
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00002645 std::shared_ptr<InputDevice> mDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002646
Chris Yea52ade12020-08-27 16:49:20 -07002647 void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002648 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002649 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002650 mFakeListener = std::make_unique<TestInputListener>();
arthurhungdcef2dc2020-08-11 14:47:50 +08002651 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002652 *mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002653 InputDeviceIdentifier identifier;
2654 identifier.name = DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002655 identifier.location = DEVICE_LOCATION;
arthurhungdcef2dc2020-08-11 14:47:50 +08002656 mDevice = std::make_shared<InputDevice>(mReader->getContext(), DEVICE_ID, DEVICE_GENERATION,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002657 identifier);
arthurhungdcef2dc2020-08-11 14:47:50 +08002658 mReader->pushNextDevice(mDevice);
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002659 mFakeEventHub->addDevice(EVENTHUB_ID, DEVICE_NAME, ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08002660 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002661 }
2662
Chris Yea52ade12020-08-27 16:49:20 -07002663 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002664 mFakeListener.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002665 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002666 }
2667};
2668
2669const char* InputDeviceTest::DEVICE_NAME = "device";
Arthur Hung2c9a3342019-07-23 14:18:59 +08002670const char* InputDeviceTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002671const int32_t InputDeviceTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002672const int32_t InputDeviceTest::DEVICE_GENERATION = 2;
2673const int32_t InputDeviceTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002674const ftl::Flags<InputDeviceClass> InputDeviceTest::DEVICE_CLASSES =
Chris Ye1b0c7342020-07-28 21:57:03 -07002675 InputDeviceClass::KEYBOARD | InputDeviceClass::TOUCH | InputDeviceClass::JOYSTICK;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002676const int32_t InputDeviceTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002677
2678TEST_F(InputDeviceTest, ImmutableProperties) {
2679 ASSERT_EQ(DEVICE_ID, mDevice->getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002680 ASSERT_STREQ(DEVICE_NAME, mDevice->getName().c_str());
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002681 ASSERT_EQ(ftl::Flags<InputDeviceClass>(0), mDevice->getClasses());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002682}
2683
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002684TEST_F(InputDeviceTest, WhenDeviceCreated_EnabledIsFalse) {
2685 ASSERT_EQ(mDevice->isEnabled(), false);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07002686}
2687
Michael Wrightd02c5b62014-02-10 15:10:22 -08002688TEST_F(InputDeviceTest, WhenNoMappersAreRegistered_DeviceIsIgnored) {
2689 // Configuration.
2690 InputReaderConfiguration config;
2691 mDevice->configure(ARBITRARY_TIME, &config, 0);
2692
2693 // Reset.
2694 mDevice->reset(ARBITRARY_TIME);
2695
2696 NotifyDeviceResetArgs resetArgs;
2697 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2698 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2699 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2700
2701 // Metadata.
2702 ASSERT_TRUE(mDevice->isIgnored());
2703 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mDevice->getSources());
2704
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002705 InputDeviceInfo info = mDevice->getDeviceInfo();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002706 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002707 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002708 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NONE, info.getKeyboardType());
2709 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, info.getSources());
2710
2711 // State queries.
2712 ASSERT_EQ(0, mDevice->getMetaState());
2713
2714 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2715 << "Ignored device should return unknown key code state.";
2716 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2717 << "Ignored device should return unknown scan code state.";
2718 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 0))
2719 << "Ignored device should return unknown switch state.";
2720
2721 const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
2722 uint8_t flags[2] = { 0, 1 };
2723 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, 2, keyCodes, flags))
2724 << "Ignored device should never mark any key codes.";
2725 ASSERT_EQ(0, flags[0]) << "Flag for unsupported key should be unchanged.";
2726 ASSERT_EQ(1, flags[1]) << "Flag for unsupported key should be unchanged.";
2727}
2728
2729TEST_F(InputDeviceTest, WhenMappersAreRegistered_DeviceIsNotIgnoredAndForwardsRequestsToMappers) {
2730 // Configuration.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002731 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, String8("key"), String8("value"));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002732
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002733 FakeInputMapper& mapper1 =
2734 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002735 mapper1.setKeyboardType(AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2736 mapper1.setMetaState(AMETA_ALT_ON);
2737 mapper1.addSupportedKeyCode(AKEYCODE_A);
2738 mapper1.addSupportedKeyCode(AKEYCODE_B);
2739 mapper1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
2740 mapper1.setKeyCodeState(AKEYCODE_B, AKEY_STATE_UP);
2741 mapper1.setScanCodeState(2, AKEY_STATE_DOWN);
2742 mapper1.setScanCodeState(3, AKEY_STATE_UP);
2743 mapper1.setSwitchState(4, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002744
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002745 FakeInputMapper& mapper2 =
2746 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002747 mapper2.setMetaState(AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002748
2749 InputReaderConfiguration config;
2750 mDevice->configure(ARBITRARY_TIME, &config, 0);
2751
2752 String8 propertyValue;
2753 ASSERT_TRUE(mDevice->getConfiguration().tryGetProperty(String8("key"), propertyValue))
2754 << "Device should have read configuration during configuration phase.";
2755 ASSERT_STREQ("value", propertyValue.string());
2756
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002757 ASSERT_NO_FATAL_FAILURE(mapper1.assertConfigureWasCalled());
2758 ASSERT_NO_FATAL_FAILURE(mapper2.assertConfigureWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002759
2760 // Reset
2761 mDevice->reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002762 ASSERT_NO_FATAL_FAILURE(mapper1.assertResetWasCalled());
2763 ASSERT_NO_FATAL_FAILURE(mapper2.assertResetWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002764
2765 NotifyDeviceResetArgs resetArgs;
2766 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2767 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2768 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2769
2770 // Metadata.
2771 ASSERT_FALSE(mDevice->isIgnored());
2772 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), mDevice->getSources());
2773
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002774 InputDeviceInfo info = mDevice->getDeviceInfo();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002775 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002776 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002777 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_ALPHABETIC, info.getKeyboardType());
2778 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), info.getSources());
2779
2780 // State queries.
2781 ASSERT_EQ(AMETA_ALT_ON | AMETA_SHIFT_ON, mDevice->getMetaState())
2782 << "Should query mappers and combine meta states.";
2783
2784 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2785 << "Should return unknown key code state when source not supported.";
2786 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2787 << "Should return unknown scan code state when source not supported.";
2788 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2789 << "Should return unknown switch state when source not supported.";
2790
2791 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, AKEYCODE_A))
2792 << "Should query mapper when source is supported.";
2793 ASSERT_EQ(AKEY_STATE_UP, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 3))
2794 << "Should query mapper when source is supported.";
2795 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 4))
2796 << "Should query mapper when source is supported.";
2797
2798 const int32_t keyCodes[4] = { AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2 };
2799 uint8_t flags[4] = { 0, 0, 0, 1 };
2800 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
2801 << "Should do nothing when source is unsupported.";
2802 ASSERT_EQ(0, flags[0]) << "Flag should be unchanged when source is unsupported.";
2803 ASSERT_EQ(0, flags[1]) << "Flag should be unchanged when source is unsupported.";
2804 ASSERT_EQ(0, flags[2]) << "Flag should be unchanged when source is unsupported.";
2805 ASSERT_EQ(1, flags[3]) << "Flag should be unchanged when source is unsupported.";
2806
2807 ASSERT_TRUE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, 4, keyCodes, flags))
2808 << "Should query mapper when source is supported.";
2809 ASSERT_EQ(1, flags[0]) << "Flag for supported key should be set.";
2810 ASSERT_EQ(1, flags[1]) << "Flag for supported key should be set.";
2811 ASSERT_EQ(0, flags[2]) << "Flag for unsupported key should be unchanged.";
2812 ASSERT_EQ(1, flags[3]) << "Flag for unsupported key should be unchanged.";
2813
2814 // Event handling.
2815 RawEvent event;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002816 event.deviceId = EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002817 mDevice->process(&event, 1);
2818
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002819 ASSERT_NO_FATAL_FAILURE(mapper1.assertProcessWasCalled());
2820 ASSERT_NO_FATAL_FAILURE(mapper2.assertProcessWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002821}
2822
Arthur Hung2c9a3342019-07-23 14:18:59 +08002823// A single input device is associated with a specific display. Check that:
2824// 1. Device is disabled if the viewport corresponding to the associated display is not found
2825// 2. Device is disabled when setEnabled API is called
2826TEST_F(InputDeviceTest, Configure_AssignsDisplayPort) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002827 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002828
2829 // First Configuration.
2830 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
2831
2832 // Device should be enabled by default.
2833 ASSERT_TRUE(mDevice->isEnabled());
2834
2835 // Prepare associated info.
2836 constexpr uint8_t hdmi = 1;
2837 const std::string UNIQUE_ID = "local:1";
2838
2839 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi);
2840 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2841 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2842 // Device should be disabled because it is associated with a specific display via
2843 // input port <-> display port association, but the corresponding display is not found
2844 ASSERT_FALSE(mDevice->isEnabled());
2845
2846 // Prepare displays.
2847 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002848 DISPLAY_ORIENTATION_0, true /*isActive*/, UNIQUE_ID, hdmi,
2849 ViewportType::INTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002850 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2851 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2852 ASSERT_TRUE(mDevice->isEnabled());
2853
2854 // Device should be disabled after set disable.
2855 mFakePolicy->addDisabledDevice(mDevice->getId());
2856 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2857 InputReaderConfiguration::CHANGE_ENABLED_STATE);
2858 ASSERT_FALSE(mDevice->isEnabled());
2859
2860 // Device should still be disabled even found the associated display.
2861 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2862 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2863 ASSERT_FALSE(mDevice->isEnabled());
2864}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002865
Christine Franks1ba71cc2021-04-07 14:37:42 -07002866TEST_F(InputDeviceTest, Configure_AssignsDisplayUniqueId) {
2867 // Device should be enabled by default.
2868 mFakePolicy->clearViewports();
2869 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
2870 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
2871 ASSERT_TRUE(mDevice->isEnabled());
2872
2873 // Device should be disabled because it is associated with a specific display, but the
2874 // corresponding display is not found.
2875 const std::string DISPLAY_UNIQUE_ID = "displayUniqueId";
Christine Franks2a2293c2022-01-18 11:51:16 -08002876 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002877 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2878 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2879 ASSERT_FALSE(mDevice->isEnabled());
2880
2881 // Device should be enabled when a display is found.
2882 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2883 DISPLAY_ORIENTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
2884 NO_PORT, ViewportType::INTERNAL);
2885 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2886 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2887 ASSERT_TRUE(mDevice->isEnabled());
2888
2889 // Device should be disabled after set disable.
2890 mFakePolicy->addDisabledDevice(mDevice->getId());
2891 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2892 InputReaderConfiguration::CHANGE_ENABLED_STATE);
2893 ASSERT_FALSE(mDevice->isEnabled());
2894
2895 // Device should still be disabled even found the associated display.
2896 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2897 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2898 ASSERT_FALSE(mDevice->isEnabled());
2899}
2900
Christine Franks2a2293c2022-01-18 11:51:16 -08002901TEST_F(InputDeviceTest, Configure_UniqueId_CorrectlyMatches) {
2902 mFakePolicy->clearViewports();
2903 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
2904 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
2905
2906 const std::string DISPLAY_UNIQUE_ID = "displayUniqueId";
2907 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
2908 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2909 DISPLAY_ORIENTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
2910 NO_PORT, ViewportType::INTERNAL);
2911 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2912 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2913 ASSERT_EQ(DISPLAY_UNIQUE_ID, mDevice->getAssociatedDisplayUniqueId());
2914}
2915
Michael Wrightd02c5b62014-02-10 15:10:22 -08002916// --- InputMapperTest ---
2917
2918class InputMapperTest : public testing::Test {
2919protected:
2920 static const char* DEVICE_NAME;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002921 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002922 static const int32_t DEVICE_ID;
2923 static const int32_t DEVICE_GENERATION;
2924 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002925 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002926 static const int32_t EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002927
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002928 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002929 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002930 std::unique_ptr<TestInputListener> mFakeListener;
arthurhungdcef2dc2020-08-11 14:47:50 +08002931 std::unique_ptr<InstrumentedInputReader> mReader;
2932 std::shared_ptr<InputDevice> mDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002933
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002934 virtual void SetUp(ftl::Flags<InputDeviceClass> classes) {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002935 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002936 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002937 mFakeListener = std::make_unique<TestInputListener>();
arthurhungdcef2dc2020-08-11 14:47:50 +08002938 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002939 *mFakeListener);
arthurhungdcef2dc2020-08-11 14:47:50 +08002940 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
Prabir Pradhan36690412022-08-05 22:26:56 +00002941 // Consume the device reset notification generated when adding a new device.
2942 mFakeListener->assertNotifyDeviceResetWasCalled();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002943 }
2944
Prabir Pradhanc14266f2021-05-12 15:56:24 -07002945 void SetUp() override {
Prabir Pradhanc14266f2021-05-12 15:56:24 -07002946 SetUp(DEVICE_CLASSES);
2947 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002948
Chris Yea52ade12020-08-27 16:49:20 -07002949 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002950 mFakeListener.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002951 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002952 }
2953
2954 void addConfigurationProperty(const char* key, const char* value) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002955 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, String8(key), String8(value));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002956 }
2957
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002958 void configureDevice(uint32_t changes) {
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00002959 if (!changes ||
2960 (changes &
2961 (InputReaderConfiguration::CHANGE_DISPLAY_INFO |
2962 InputReaderConfiguration::CHANGE_POINTER_CAPTURE))) {
arthurhungdcef2dc2020-08-11 14:47:50 +08002963 mReader->requestRefreshConfiguration(changes);
2964 mReader->loopOnce();
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -08002965 }
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002966 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
Prabir Pradhan36690412022-08-05 22:26:56 +00002967 // Loop the reader to flush the input listener queue.
2968 mReader->loopOnce();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002969 }
2970
arthurhungdcef2dc2020-08-11 14:47:50 +08002971 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
2972 const std::string& location, int32_t eventHubId,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002973 ftl::Flags<InputDeviceClass> classes) {
arthurhungdcef2dc2020-08-11 14:47:50 +08002974 InputDeviceIdentifier identifier;
2975 identifier.name = name;
2976 identifier.location = location;
2977 std::shared_ptr<InputDevice> device =
2978 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
2979 identifier);
2980 mReader->pushNextDevice(device);
2981 mFakeEventHub->addDevice(eventHubId, name, classes);
2982 mReader->loopOnce();
2983 return device;
2984 }
2985
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002986 template <class T, typename... Args>
2987 T& addMapperAndConfigure(Args... args) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002988 T& mapper = mDevice->addMapper<T>(EVENTHUB_ID, args...);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002989 configureDevice(0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002990 mDevice->reset(ARBITRARY_TIME);
Chris Ye42b06822020-08-07 11:39:33 -07002991 mapper.reset(ARBITRARY_TIME);
Prabir Pradhan36690412022-08-05 22:26:56 +00002992 // Loop the reader to flush the input listener queue.
2993 mReader->loopOnce();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002994 return mapper;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002995 }
2996
2997 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002998 int32_t orientation, const std::string& uniqueId,
2999 std::optional<uint8_t> physicalPort, ViewportType viewportType) {
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00003000 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, true /*isActive*/,
3001 uniqueId, physicalPort, viewportType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003002 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3003 }
3004
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003005 void clearViewports() {
3006 mFakePolicy->clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003007 }
3008
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003009 void process(InputMapper& mapper, nsecs_t when, nsecs_t readTime, int32_t type, int32_t code,
3010 int32_t value) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003011 RawEvent event;
3012 event.when = when;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003013 event.readTime = readTime;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003014 event.deviceId = mapper.getDeviceContext().getEventHubId();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003015 event.type = type;
3016 event.code = code;
3017 event.value = value;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003018 mapper.process(&event);
Prabir Pradhan36690412022-08-05 22:26:56 +00003019 // Loop the reader to flush the input listener queue.
arthurhungdcef2dc2020-08-11 14:47:50 +08003020 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003021 }
3022
3023 static void assertMotionRange(const InputDeviceInfo& info,
3024 int32_t axis, uint32_t source, float min, float max, float flat, float fuzz) {
3025 const InputDeviceInfo::MotionRange* range = info.getMotionRange(axis, source);
Yi Kong9b14ac62018-07-17 13:48:38 -07003026 ASSERT_TRUE(range != nullptr) << "Axis: " << axis << " Source: " << source;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003027 ASSERT_EQ(axis, range->axis) << "Axis: " << axis << " Source: " << source;
3028 ASSERT_EQ(source, range->source) << "Axis: " << axis << " Source: " << source;
3029 ASSERT_NEAR(min, range->min, EPSILON) << "Axis: " << axis << " Source: " << source;
3030 ASSERT_NEAR(max, range->max, EPSILON) << "Axis: " << axis << " Source: " << source;
3031 ASSERT_NEAR(flat, range->flat, EPSILON) << "Axis: " << axis << " Source: " << source;
3032 ASSERT_NEAR(fuzz, range->fuzz, EPSILON) << "Axis: " << axis << " Source: " << source;
3033 }
3034
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003035 static void assertPointerCoords(const PointerCoords& coords, float x, float y, float pressure,
3036 float size, float touchMajor, float touchMinor, float toolMajor,
3037 float toolMinor, float orientation, float distance,
3038 float scaledAxisEpsilon = 1.f) {
3039 ASSERT_NEAR(x, coords.getAxisValue(AMOTION_EVENT_AXIS_X), scaledAxisEpsilon);
3040 ASSERT_NEAR(y, coords.getAxisValue(AMOTION_EVENT_AXIS_Y), scaledAxisEpsilon);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003041 ASSERT_NEAR(pressure, coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), EPSILON);
3042 ASSERT_NEAR(size, coords.getAxisValue(AMOTION_EVENT_AXIS_SIZE), EPSILON);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003043 ASSERT_NEAR(touchMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR),
3044 scaledAxisEpsilon);
3045 ASSERT_NEAR(touchMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR),
3046 scaledAxisEpsilon);
3047 ASSERT_NEAR(toolMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR),
3048 scaledAxisEpsilon);
3049 ASSERT_NEAR(toolMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR),
3050 scaledAxisEpsilon);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003051 ASSERT_NEAR(orientation, coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION), EPSILON);
3052 ASSERT_NEAR(distance, coords.getAxisValue(AMOTION_EVENT_AXIS_DISTANCE), EPSILON);
3053 }
3054
Michael Wright17db18e2020-06-26 20:51:44 +01003055 static void assertPosition(const FakePointerController& controller, float x, float y) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003056 float actualX, actualY;
Michael Wright17db18e2020-06-26 20:51:44 +01003057 controller.getPosition(&actualX, &actualY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003058 ASSERT_NEAR(x, actualX, 1);
3059 ASSERT_NEAR(y, actualY, 1);
3060 }
3061};
3062
3063const char* InputMapperTest::DEVICE_NAME = "device";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003064const char* InputMapperTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003065const int32_t InputMapperTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003066const int32_t InputMapperTest::DEVICE_GENERATION = 2;
3067const int32_t InputMapperTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003068const ftl::Flags<InputDeviceClass> InputMapperTest::DEVICE_CLASSES =
3069 ftl::Flags<InputDeviceClass>(0); // not needed for current tests
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003070const int32_t InputMapperTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003071
3072// --- SwitchInputMapperTest ---
3073
3074class SwitchInputMapperTest : public InputMapperTest {
3075protected:
3076};
3077
3078TEST_F(SwitchInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003079 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003080
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003081 ASSERT_EQ(uint32_t(AINPUT_SOURCE_SWITCH), mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003082}
3083
3084TEST_F(SwitchInputMapperTest, GetSwitchState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003085 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003086
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003087 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003088 ASSERT_EQ(1, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003089
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003090 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003091 ASSERT_EQ(0, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003092}
3093
3094TEST_F(SwitchInputMapperTest, Process) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003095 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003096
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003097 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_LID, 1);
3098 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_JACK_PHYSICAL_INSERT, 1);
3099 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_HEADPHONE_INSERT, 0);
3100 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003101
3102 NotifySwitchArgs args;
3103 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySwitchWasCalled(&args));
3104 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
Dan Albert1bd2fc02016-02-02 15:11:57 -08003105 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT), args.switchValues);
3106 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT) | (1 << SW_HEADPHONE_INSERT),
Michael Wrightd02c5b62014-02-10 15:10:22 -08003107 args.switchMask);
3108 ASSERT_EQ(uint32_t(0), args.policyFlags);
3109}
3110
Chris Ye87143712020-11-10 05:05:58 +00003111// --- VibratorInputMapperTest ---
3112class VibratorInputMapperTest : public InputMapperTest {
3113protected:
3114 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::VIBRATOR); }
3115};
3116
3117TEST_F(VibratorInputMapperTest, GetSources) {
3118 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
3119
3120 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mapper.getSources());
3121}
3122
3123TEST_F(VibratorInputMapperTest, GetVibratorIds) {
3124 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
3125
3126 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
3127}
3128
3129TEST_F(VibratorInputMapperTest, Vibrate) {
3130 constexpr uint8_t DEFAULT_AMPLITUDE = 192;
Chris Yefb552902021-02-03 17:18:37 -08003131 constexpr int32_t VIBRATION_TOKEN = 100;
Chris Ye87143712020-11-10 05:05:58 +00003132 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
3133
3134 VibrationElement pattern(2);
3135 VibrationSequence sequence(2);
3136 pattern.duration = std::chrono::milliseconds(200);
3137 pattern.channels = {{0 /* vibratorId */, DEFAULT_AMPLITUDE / 2},
3138 {1 /* vibratorId */, DEFAULT_AMPLITUDE}};
3139 sequence.addElement(pattern);
3140 pattern.duration = std::chrono::milliseconds(500);
3141 pattern.channels = {{0 /* vibratorId */, DEFAULT_AMPLITUDE / 4},
3142 {1 /* vibratorId */, DEFAULT_AMPLITUDE}};
3143 sequence.addElement(pattern);
3144
3145 std::vector<int64_t> timings = {0, 1};
3146 std::vector<uint8_t> amplitudes = {DEFAULT_AMPLITUDE, DEFAULT_AMPLITUDE / 2};
3147
3148 ASSERT_FALSE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08003149 // Start vibrating
3150 mapper.vibrate(sequence, -1 /* repeat */, VIBRATION_TOKEN);
Chris Ye87143712020-11-10 05:05:58 +00003151 ASSERT_TRUE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08003152 // Verify vibrator state listener was notified.
3153 mReader->loopOnce();
3154 NotifyVibratorStateArgs args;
3155 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyVibratorStateWasCalled(&args));
3156 ASSERT_EQ(DEVICE_ID, args.deviceId);
3157 ASSERT_TRUE(args.isOn);
3158 // Stop vibrating
3159 mapper.cancelVibrate(VIBRATION_TOKEN);
3160 ASSERT_FALSE(mapper.isVibrating());
3161 // Verify vibrator state listener was notified.
3162 mReader->loopOnce();
3163 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyVibratorStateWasCalled(&args));
3164 ASSERT_EQ(DEVICE_ID, args.deviceId);
3165 ASSERT_FALSE(args.isOn);
Chris Ye87143712020-11-10 05:05:58 +00003166}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003167
Chris Yef59a2f42020-10-16 12:55:26 -07003168// --- SensorInputMapperTest ---
3169
3170class SensorInputMapperTest : public InputMapperTest {
3171protected:
3172 static const int32_t ACCEL_RAW_MIN;
3173 static const int32_t ACCEL_RAW_MAX;
3174 static const int32_t ACCEL_RAW_FUZZ;
3175 static const int32_t ACCEL_RAW_FLAT;
3176 static const int32_t ACCEL_RAW_RESOLUTION;
3177
3178 static const int32_t GYRO_RAW_MIN;
3179 static const int32_t GYRO_RAW_MAX;
3180 static const int32_t GYRO_RAW_FUZZ;
3181 static const int32_t GYRO_RAW_FLAT;
3182 static const int32_t GYRO_RAW_RESOLUTION;
3183
3184 static const float GRAVITY_MS2_UNIT;
3185 static const float DEGREE_RADIAN_UNIT;
3186
3187 void prepareAccelAxes();
3188 void prepareGyroAxes();
3189 void setAccelProperties();
3190 void setGyroProperties();
3191 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::SENSOR); }
3192};
3193
3194const int32_t SensorInputMapperTest::ACCEL_RAW_MIN = -32768;
3195const int32_t SensorInputMapperTest::ACCEL_RAW_MAX = 32768;
3196const int32_t SensorInputMapperTest::ACCEL_RAW_FUZZ = 16;
3197const int32_t SensorInputMapperTest::ACCEL_RAW_FLAT = 0;
3198const int32_t SensorInputMapperTest::ACCEL_RAW_RESOLUTION = 8192;
3199
3200const int32_t SensorInputMapperTest::GYRO_RAW_MIN = -2097152;
3201const int32_t SensorInputMapperTest::GYRO_RAW_MAX = 2097152;
3202const int32_t SensorInputMapperTest::GYRO_RAW_FUZZ = 16;
3203const int32_t SensorInputMapperTest::GYRO_RAW_FLAT = 0;
3204const int32_t SensorInputMapperTest::GYRO_RAW_RESOLUTION = 1024;
3205
3206const float SensorInputMapperTest::GRAVITY_MS2_UNIT = 9.80665f;
3207const float SensorInputMapperTest::DEGREE_RADIAN_UNIT = 0.0174533f;
3208
3209void SensorInputMapperTest::prepareAccelAxes() {
3210 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3211 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3212 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3213 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3214 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Z, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3215 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3216}
3217
3218void SensorInputMapperTest::prepareGyroAxes() {
3219 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RX, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3220 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3221 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RY, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3222 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3223 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RZ, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3224 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3225}
3226
3227void SensorInputMapperTest::setAccelProperties() {
3228 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 0, InputDeviceSensorType::ACCELEROMETER,
3229 /* sensorDataIndex */ 0);
3230 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 1, InputDeviceSensorType::ACCELEROMETER,
3231 /* sensorDataIndex */ 1);
3232 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 2, InputDeviceSensorType::ACCELEROMETER,
3233 /* sensorDataIndex */ 2);
3234 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
3235 addConfigurationProperty("sensor.accelerometer.reportingMode", "0");
3236 addConfigurationProperty("sensor.accelerometer.maxDelay", "100000");
3237 addConfigurationProperty("sensor.accelerometer.minDelay", "5000");
3238 addConfigurationProperty("sensor.accelerometer.power", "1.5");
3239}
3240
3241void SensorInputMapperTest::setGyroProperties() {
3242 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 3, InputDeviceSensorType::GYROSCOPE,
3243 /* sensorDataIndex */ 0);
3244 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 4, InputDeviceSensorType::GYROSCOPE,
3245 /* sensorDataIndex */ 1);
3246 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 5, InputDeviceSensorType::GYROSCOPE,
3247 /* sensorDataIndex */ 2);
3248 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
3249 addConfigurationProperty("sensor.gyroscope.reportingMode", "0");
3250 addConfigurationProperty("sensor.gyroscope.maxDelay", "100000");
3251 addConfigurationProperty("sensor.gyroscope.minDelay", "5000");
3252 addConfigurationProperty("sensor.gyroscope.power", "0.8");
3253}
3254
3255TEST_F(SensorInputMapperTest, GetSources) {
3256 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3257
3258 ASSERT_EQ(static_cast<uint32_t>(AINPUT_SOURCE_SENSOR), mapper.getSources());
3259}
3260
3261TEST_F(SensorInputMapperTest, ProcessAccelerometerSensor) {
3262 setAccelProperties();
3263 prepareAccelAxes();
3264 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3265
3266 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::ACCELEROMETER,
3267 std::chrono::microseconds(10000),
3268 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08003269 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003270 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, 20000);
3271 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, -20000);
3272 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Z, 40000);
3273 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
3274 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07003275
3276 NotifySensorArgs args;
3277 std::vector<float> values = {20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
3278 -20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
3279 40000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT};
3280
3281 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
3282 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
3283 ASSERT_EQ(args.deviceId, DEVICE_ID);
3284 ASSERT_EQ(args.sensorType, InputDeviceSensorType::ACCELEROMETER);
3285 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
3286 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
3287 ASSERT_EQ(args.values, values);
3288 mapper.flushSensor(InputDeviceSensorType::ACCELEROMETER);
3289}
3290
3291TEST_F(SensorInputMapperTest, ProcessGyroscopeSensor) {
3292 setGyroProperties();
3293 prepareGyroAxes();
3294 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3295
3296 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::GYROSCOPE,
3297 std::chrono::microseconds(10000),
3298 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08003299 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003300 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RX, 20000);
3301 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RY, -20000);
3302 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RZ, 40000);
3303 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
3304 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07003305
3306 NotifySensorArgs args;
3307 std::vector<float> values = {20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
3308 -20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
3309 40000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT};
3310
3311 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
3312 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
3313 ASSERT_EQ(args.deviceId, DEVICE_ID);
3314 ASSERT_EQ(args.sensorType, InputDeviceSensorType::GYROSCOPE);
3315 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
3316 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
3317 ASSERT_EQ(args.values, values);
3318 mapper.flushSensor(InputDeviceSensorType::GYROSCOPE);
3319}
3320
Michael Wrightd02c5b62014-02-10 15:10:22 -08003321// --- KeyboardInputMapperTest ---
3322
3323class KeyboardInputMapperTest : public InputMapperTest {
3324protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003325 const std::string UNIQUE_ID = "local:0";
3326
3327 void prepareDisplay(int32_t orientation);
3328
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003329 void testDPadKeyRotation(KeyboardInputMapper& mapper, int32_t originalScanCode,
Arthur Hung2c9a3342019-07-23 14:18:59 +08003330 int32_t originalKeyCode, int32_t rotatedKeyCode,
3331 int32_t displayId = ADISPLAY_ID_NONE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003332};
3333
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003334/* Similar to setDisplayInfoAndReconfigure, but pre-populates all parameters except for the
3335 * orientation.
3336 */
3337void KeyboardInputMapperTest::prepareDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003338 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
3339 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003340}
3341
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003342void KeyboardInputMapperTest::testDPadKeyRotation(KeyboardInputMapper& mapper,
Arthur Hung2c9a3342019-07-23 14:18:59 +08003343 int32_t originalScanCode, int32_t originalKeyCode,
3344 int32_t rotatedKeyCode, int32_t displayId) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003345 NotifyKeyArgs args;
3346
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003347 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003348 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3349 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3350 ASSERT_EQ(originalScanCode, args.scanCode);
3351 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003352 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003353
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003354 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003355 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3356 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3357 ASSERT_EQ(originalScanCode, args.scanCode);
3358 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003359 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003360}
3361
Michael Wrightd02c5b62014-02-10 15:10:22 -08003362TEST_F(KeyboardInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003363 KeyboardInputMapper& mapper =
3364 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3365 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003366
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003367 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003368}
3369
3370TEST_F(KeyboardInputMapperTest, Process_SimpleKeyPress) {
3371 const int32_t USAGE_A = 0x070004;
3372 const int32_t USAGE_UNKNOWN = 0x07ffff;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003373 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3374 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
Chris Yea52ade12020-08-27 16:49:20 -07003375 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, POLICY_FLAG_WAKE);
3376 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, POLICY_FLAG_WAKE);
3377 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003378
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003379 KeyboardInputMapper& mapper =
3380 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3381 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003382 // Initial metastate is AMETA_NONE.
3383 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003384
3385 // Key down by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003386 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003387 NotifyKeyArgs args;
3388 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3389 ASSERT_EQ(DEVICE_ID, args.deviceId);
3390 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3391 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3392 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3393 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3394 ASSERT_EQ(KEY_HOME, args.scanCode);
3395 ASSERT_EQ(AMETA_NONE, args.metaState);
3396 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3397 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3398 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3399
3400 // Key up by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003401 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003402 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3403 ASSERT_EQ(DEVICE_ID, args.deviceId);
3404 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3405 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3406 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3407 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3408 ASSERT_EQ(KEY_HOME, args.scanCode);
3409 ASSERT_EQ(AMETA_NONE, args.metaState);
3410 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3411 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3412 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3413
3414 // Key down by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003415 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3416 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, 0, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003417 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3418 ASSERT_EQ(DEVICE_ID, args.deviceId);
3419 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3420 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3421 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3422 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3423 ASSERT_EQ(0, args.scanCode);
3424 ASSERT_EQ(AMETA_NONE, args.metaState);
3425 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3426 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3427 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3428
3429 // Key up by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003430 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3431 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003432 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3433 ASSERT_EQ(DEVICE_ID, args.deviceId);
3434 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3435 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3436 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3437 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3438 ASSERT_EQ(0, args.scanCode);
3439 ASSERT_EQ(AMETA_NONE, args.metaState);
3440 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3441 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3442 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3443
3444 // Key down with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003445 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3446 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UNKNOWN, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003447 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3448 ASSERT_EQ(DEVICE_ID, args.deviceId);
3449 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3450 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3451 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3452 ASSERT_EQ(0, args.keyCode);
3453 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3454 ASSERT_EQ(AMETA_NONE, args.metaState);
3455 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3456 ASSERT_EQ(0U, args.policyFlags);
3457 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3458
3459 // Key up with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003460 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3461 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003462 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3463 ASSERT_EQ(DEVICE_ID, args.deviceId);
3464 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3465 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3466 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3467 ASSERT_EQ(0, args.keyCode);
3468 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3469 ASSERT_EQ(AMETA_NONE, args.metaState);
3470 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3471 ASSERT_EQ(0U, args.policyFlags);
3472 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3473}
3474
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003475/**
3476 * Ensure that the readTime is set to the time when the EV_KEY is received.
3477 */
3478TEST_F(KeyboardInputMapperTest, Process_SendsReadTime) {
3479 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3480
3481 KeyboardInputMapper& mapper =
3482 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3483 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3484 NotifyKeyArgs args;
3485
3486 // Key down
3487 process(mapper, ARBITRARY_TIME, 12 /*readTime*/, EV_KEY, KEY_HOME, 1);
3488 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3489 ASSERT_EQ(12, args.readTime);
3490
3491 // Key up
3492 process(mapper, ARBITRARY_TIME, 15 /*readTime*/, EV_KEY, KEY_HOME, 1);
3493 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3494 ASSERT_EQ(15, args.readTime);
3495}
3496
Michael Wrightd02c5b62014-02-10 15:10:22 -08003497TEST_F(KeyboardInputMapperTest, Process_ShouldUpdateMetaState) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003498 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFTSHIFT, 0, AKEYCODE_SHIFT_LEFT, 0);
3499 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003500 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, 0);
3501 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, 0);
3502 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003503
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003504 KeyboardInputMapper& mapper =
3505 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3506 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003507
Arthur Hung95f68612022-04-07 14:08:22 +08003508 // Initial metastate is AMETA_NONE.
3509 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003510
3511 // Metakey down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003512 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003513 NotifyKeyArgs args;
3514 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3515 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003516 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003517 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003518
3519 // Key down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003520 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_A, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003521 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3522 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003523 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003524
3525 // Key up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003526 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, KEY_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003527 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3528 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003529 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003530
3531 // Metakey up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003532 process(mapper, ARBITRARY_TIME + 3, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003533 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3534 ASSERT_EQ(AMETA_NONE, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003535 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003536 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003537}
3538
3539TEST_F(KeyboardInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003540 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3541 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3542 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3543 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003544
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003545 KeyboardInputMapper& mapper =
3546 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3547 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003548
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003549 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003550 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3551 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
3552 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3553 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
3554 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3555 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
3556 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3557 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
3558}
3559
3560TEST_F(KeyboardInputMapperTest, Process_WhenOrientationAware_ShouldRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003561 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3562 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3563 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3564 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003565
Michael Wrightd02c5b62014-02-10 15:10:22 -08003566 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003567 KeyboardInputMapper& mapper =
3568 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3569 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003570
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003571 prepareDisplay(DISPLAY_ORIENTATION_0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003572 ASSERT_NO_FATAL_FAILURE(
3573 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3574 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3575 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3576 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3577 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3578 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3579 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003580
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003581 clearViewports();
3582 prepareDisplay(DISPLAY_ORIENTATION_90);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003583 ASSERT_NO_FATAL_FAILURE(
3584 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3585 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3586 AKEYCODE_DPAD_UP, DISPLAY_ID));
3587 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3588 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3589 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3590 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003591
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003592 clearViewports();
3593 prepareDisplay(DISPLAY_ORIENTATION_180);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003594 ASSERT_NO_FATAL_FAILURE(
3595 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3596 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3597 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3598 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3599 AKEYCODE_DPAD_UP, DISPLAY_ID));
3600 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3601 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003602
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003603 clearViewports();
3604 prepareDisplay(DISPLAY_ORIENTATION_270);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003605 ASSERT_NO_FATAL_FAILURE(
3606 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3607 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3608 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3609 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3610 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3611 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3612 AKEYCODE_DPAD_UP, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003613
3614 // Special case: if orientation changes while key is down, we still emit the same keycode
3615 // in the key up as we did in the key down.
3616 NotifyKeyArgs args;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003617 clearViewports();
3618 prepareDisplay(DISPLAY_ORIENTATION_270);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003619 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003620 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3621 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3622 ASSERT_EQ(KEY_UP, args.scanCode);
3623 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3624
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003625 clearViewports();
3626 prepareDisplay(DISPLAY_ORIENTATION_180);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003627 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003628 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3629 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3630 ASSERT_EQ(KEY_UP, args.scanCode);
3631 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3632}
3633
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003634TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_NotOrientationAware) {
3635 // If the keyboard is not orientation aware,
3636 // key events should not be associated with a specific display id
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003637 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003638
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003639 KeyboardInputMapper& mapper =
3640 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3641 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003642 NotifyKeyArgs args;
3643
3644 // Display id should be ADISPLAY_ID_NONE without any display configuration.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003645 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003646 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003647 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003648 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3649 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3650
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003651 prepareDisplay(DISPLAY_ORIENTATION_0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003652 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003653 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003654 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003655 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3656 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3657}
3658
3659TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_OrientationAware) {
3660 // If the keyboard is orientation aware,
3661 // key events should be associated with the internal viewport
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003662 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003663
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003664 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003665 KeyboardInputMapper& mapper =
3666 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3667 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003668 NotifyKeyArgs args;
3669
3670 // Display id should be ADISPLAY_ID_NONE without any display configuration.
3671 // ^--- already checked by the previous test
3672
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003673 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003674 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003675 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003676 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003677 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003678 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3679 ASSERT_EQ(DISPLAY_ID, args.displayId);
3680
3681 constexpr int32_t newDisplayId = 2;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003682 clearViewports();
3683 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003684 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003685 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003686 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003687 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003688 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3689 ASSERT_EQ(newDisplayId, args.displayId);
3690}
3691
Michael Wrightd02c5b62014-02-10 15:10:22 -08003692TEST_F(KeyboardInputMapperTest, GetKeyCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003693 KeyboardInputMapper& mapper =
3694 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3695 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003696
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003697 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003698 ASSERT_EQ(1, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003699
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003700 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003701 ASSERT_EQ(0, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003702}
3703
Philip Junker4af3b3d2021-12-14 10:36:55 +01003704TEST_F(KeyboardInputMapperTest, GetKeyCodeForKeyLocation) {
3705 KeyboardInputMapper& mapper =
3706 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3707 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3708
3709 mFakeEventHub->addKeyCodeMapping(EVENTHUB_ID, AKEYCODE_Y, AKEYCODE_Z);
3710 ASSERT_EQ(AKEYCODE_Z, mapper.getKeyCodeForKeyLocation(AKEYCODE_Y))
3711 << "If a mapping is available, the result is equal to the mapping";
3712
3713 ASSERT_EQ(AKEYCODE_A, mapper.getKeyCodeForKeyLocation(AKEYCODE_A))
3714 << "If no mapping is available, the result is the key location";
3715}
3716
Michael Wrightd02c5b62014-02-10 15:10:22 -08003717TEST_F(KeyboardInputMapperTest, GetScanCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003718 KeyboardInputMapper& mapper =
3719 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3720 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003721
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003722 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003723 ASSERT_EQ(1, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003724
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003725 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003726 ASSERT_EQ(0, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003727}
3728
3729TEST_F(KeyboardInputMapperTest, MarkSupportedKeyCodes) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003730 KeyboardInputMapper& mapper =
3731 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3732 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003733
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003734 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003735
3736 const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
3737 uint8_t flags[2] = { 0, 0 };
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003738 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, 1, keyCodes, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003739 ASSERT_TRUE(flags[0]);
3740 ASSERT_FALSE(flags[1]);
3741}
3742
3743TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleMetaStateAndLeds) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003744 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3745 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3746 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3747 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3748 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3749 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003750
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003751 KeyboardInputMapper& mapper =
3752 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3753 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003754 // Initial metastate is AMETA_NONE.
3755 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003756
3757 // Initialization should have turned all of the lights off.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003758 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3759 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3760 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003761
3762 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003763 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3764 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003765 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3766 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3767 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003768 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003769
3770 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003771 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3772 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003773 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3774 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3775 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003776 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003777
3778 // Toggle caps lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003779 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3780 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003781 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3782 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3783 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003784 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003785
3786 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003787 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3788 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003789 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3790 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3791 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003792 ASSERT_EQ(AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003793
3794 // Toggle num lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003795 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3796 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003797 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3798 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3799 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003800 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003801
3802 // Toggle scroll lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003803 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3804 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003805 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3806 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3807 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003808 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003809}
3810
Chris Yea52ade12020-08-27 16:49:20 -07003811TEST_F(KeyboardInputMapperTest, NoMetaStateWhenMetaKeysNotPresent) {
3812 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_BUTTON_A, 0);
3813 mFakeEventHub->addKey(EVENTHUB_ID, BTN_B, 0, AKEYCODE_BUTTON_B, 0);
3814 mFakeEventHub->addKey(EVENTHUB_ID, BTN_X, 0, AKEYCODE_BUTTON_X, 0);
3815 mFakeEventHub->addKey(EVENTHUB_ID, BTN_Y, 0, AKEYCODE_BUTTON_Y, 0);
3816
3817 KeyboardInputMapper& mapper =
3818 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3819 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
3820
Chris Yea52ade12020-08-27 16:49:20 -07003821 // Meta state should be AMETA_NONE after reset
3822 mapper.reset(ARBITRARY_TIME);
3823 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3824 // Meta state should be AMETA_NONE with update, as device doesn't have the keys.
3825 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
3826 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3827
3828 NotifyKeyArgs args;
3829 // Press button "A"
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003830 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_A, 1);
Chris Yea52ade12020-08-27 16:49:20 -07003831 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3832 ASSERT_EQ(AMETA_NONE, args.metaState);
3833 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3834 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3835 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3836
3837 // Button up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003838 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003839 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3840 ASSERT_EQ(AMETA_NONE, args.metaState);
3841 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3842 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3843 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3844}
3845
Arthur Hung2c9a3342019-07-23 14:18:59 +08003846TEST_F(KeyboardInputMapperTest, Configure_AssignsDisplayPort) {
3847 // keyboard 1.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003848 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3849 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3850 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3851 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003852
3853 // keyboard 2.
3854 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08003855 const std::string DEVICE_NAME2 = "KEYBOARD2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08003856 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003857 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08003858 std::shared_ptr<InputDevice> device2 =
3859 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003860 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08003861
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003862 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3863 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3864 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3865 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003866
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003867 KeyboardInputMapper& mapper =
3868 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3869 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003870
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003871 KeyboardInputMapper& mapper2 =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003872 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003873 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003874 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
3875 device2->reset(ARBITRARY_TIME);
3876
3877 // Prepared displays and associated info.
3878 constexpr uint8_t hdmi1 = 0;
3879 constexpr uint8_t hdmi2 = 1;
3880 const std::string SECONDARY_UNIQUE_ID = "local:1";
3881
3882 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
3883 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
3884
3885 // No associated display viewport found, should disable the device.
3886 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3887 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3888 ASSERT_FALSE(device2->isEnabled());
3889
3890 // Prepare second display.
3891 constexpr int32_t newDisplayId = 2;
3892 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003893 UNIQUE_ID, hdmi1, ViewportType::INTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003894 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003895 SECONDARY_UNIQUE_ID, hdmi2, ViewportType::EXTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003896 // Default device will reconfigure above, need additional reconfiguration for another device.
3897 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3898 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3899
3900 // Device should be enabled after the associated display is found.
3901 ASSERT_TRUE(mDevice->isEnabled());
3902 ASSERT_TRUE(device2->isEnabled());
3903
3904 // Test pad key events
3905 ASSERT_NO_FATAL_FAILURE(
3906 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3907 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3908 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3909 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3910 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3911 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3912 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3913
3914 ASSERT_NO_FATAL_FAILURE(
3915 testDPadKeyRotation(mapper2, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, newDisplayId));
3916 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3917 AKEYCODE_DPAD_RIGHT, newDisplayId));
3918 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3919 AKEYCODE_DPAD_DOWN, newDisplayId));
3920 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3921 AKEYCODE_DPAD_LEFT, newDisplayId));
3922}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003923
arthurhungc903df12020-08-11 15:08:42 +08003924TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleAfterReattach) {
3925 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3926 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3927 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3928 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3929 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3930 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3931
3932 KeyboardInputMapper& mapper =
3933 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3934 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003935 // Initial metastate is AMETA_NONE.
3936 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08003937
3938 // Initialization should have turned all of the lights off.
3939 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3940 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3941 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3942
3943 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003944 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3945 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003946 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3947 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
3948
3949 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003950 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3951 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003952 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3953 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
3954
3955 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003956 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3957 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003958 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3959 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
3960
3961 mFakeEventHub->removeDevice(EVENTHUB_ID);
3962 mReader->loopOnce();
3963
3964 // keyboard 2 should default toggle keys.
3965 const std::string USB2 = "USB2";
3966 const std::string DEVICE_NAME2 = "KEYBOARD2";
3967 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
3968 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
3969 std::shared_ptr<InputDevice> device2 =
3970 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003971 ftl::Flags<InputDeviceClass>(0));
arthurhungc903df12020-08-11 15:08:42 +08003972 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3973 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
3974 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3975 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3976 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3977 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3978
arthurhung6fe95782020-10-05 22:41:16 +08003979 KeyboardInputMapper& mapper2 =
3980 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
3981 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
arthurhungc903df12020-08-11 15:08:42 +08003982 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
3983 device2->reset(ARBITRARY_TIME);
3984
3985 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_CAPSL));
3986 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_NUML));
3987 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_SCROLLL));
arthurhung6fe95782020-10-05 22:41:16 +08003988 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON,
3989 mapper2.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08003990}
3991
Arthur Hungcb40a002021-08-03 14:31:01 +00003992TEST_F(KeyboardInputMapperTest, Process_toggleCapsLockState) {
3993 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3994 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3995 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3996
3997 // Suppose we have two mappers. (DPAD + KEYBOARD)
3998 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_DPAD,
3999 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
4000 KeyboardInputMapper& mapper =
4001 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4002 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08004003 // Initial metastate is AMETA_NONE.
4004 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Arthur Hungcb40a002021-08-03 14:31:01 +00004005
4006 mReader->toggleCapsLockState(DEVICE_ID);
4007 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
4008}
4009
Arthur Hungfb3cc112022-04-13 07:39:50 +00004010TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleInMultiDevices) {
4011 // keyboard 1.
4012 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
4013 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
4014 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
4015 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4016 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4017 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
4018
4019 KeyboardInputMapper& mapper1 =
4020 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4021 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
4022
4023 // keyboard 2.
4024 const std::string USB2 = "USB2";
4025 const std::string DEVICE_NAME2 = "KEYBOARD2";
4026 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
4027 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
4028 std::shared_ptr<InputDevice> device2 =
4029 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
4030 ftl::Flags<InputDeviceClass>(0));
4031 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
4032 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
4033 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
4034 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4035 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4036 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
4037
4038 KeyboardInputMapper& mapper2 =
4039 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
4040 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
4041 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
4042 device2->reset(ARBITRARY_TIME);
4043
Arthur Hung95f68612022-04-07 14:08:22 +08004044 // Initial metastate is AMETA_NONE.
4045 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4046 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4047
4048 // Toggle num lock on and off.
4049 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
4050 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Arthur Hungfb3cc112022-04-13 07:39:50 +00004051 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4052 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper1.getMetaState());
4053 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper2.getMetaState());
4054
4055 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
4056 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
4057 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4058 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4059 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4060
4061 // Toggle caps lock on and off.
4062 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
4063 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
4064 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4065 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper1.getMetaState());
4066 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper2.getMetaState());
4067
4068 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
4069 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
4070 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4071 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4072 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4073
4074 // Toggle scroll lock on and off.
4075 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
4076 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
4077 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
4078 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper1.getMetaState());
4079 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper2.getMetaState());
4080
4081 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
4082 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
4083 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
4084 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4085 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4086}
4087
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004088// --- KeyboardInputMapperTest_ExternalDevice ---
4089
4090class KeyboardInputMapperTest_ExternalDevice : public InputMapperTest {
4091protected:
Chris Yea52ade12020-08-27 16:49:20 -07004092 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004093};
4094
4095TEST_F(KeyboardInputMapperTest_ExternalDevice, WakeBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07004096 // For external devices, non-media keys will trigger wake on key down. Media keys need to be
4097 // marked as WAKE in the keylayout file to trigger wake.
Powei Fengd041c5d2019-05-03 17:11:33 -07004098
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004099 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, 0);
4100 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, 0);
4101 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAYPAUSE, 0, AKEYCODE_MEDIA_PLAY_PAUSE,
4102 POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07004103
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004104 KeyboardInputMapper& mapper =
4105 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4106 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07004107
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004108 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004109 NotifyKeyArgs args;
4110 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4111 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4112
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004113 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004114 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4115 ASSERT_EQ(uint32_t(0), args.policyFlags);
4116
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004117 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004118 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4119 ASSERT_EQ(uint32_t(0), args.policyFlags);
4120
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004121 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004122 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4123 ASSERT_EQ(uint32_t(0), args.policyFlags);
4124
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004125 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004126 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4127 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4128
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004129 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004130 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4131 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4132}
4133
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004134TEST_F(KeyboardInputMapperTest_ExternalDevice, DoNotWakeByDefaultBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07004135 // Tv Remote key's wake behavior is prescribed by the keylayout file.
Powei Fengd041c5d2019-05-03 17:11:33 -07004136
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004137 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4138 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
4139 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07004140
Powei Fengd041c5d2019-05-03 17:11:33 -07004141 addConfigurationProperty("keyboard.doNotWakeByDefault", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004142 KeyboardInputMapper& mapper =
4143 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4144 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07004145
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004146 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004147 NotifyKeyArgs args;
4148 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4149 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4150
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004151 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004152 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4153 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4154
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004155 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_DOWN, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004156 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4157 ASSERT_EQ(uint32_t(0), args.policyFlags);
4158
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004159 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_DOWN, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004160 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4161 ASSERT_EQ(uint32_t(0), args.policyFlags);
4162
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004163 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004164 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4165 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4166
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004167 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004168 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4169 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4170}
4171
Michael Wrightd02c5b62014-02-10 15:10:22 -08004172// --- CursorInputMapperTest ---
4173
4174class CursorInputMapperTest : public InputMapperTest {
4175protected:
4176 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD;
4177
Michael Wright17db18e2020-06-26 20:51:44 +01004178 std::shared_ptr<FakePointerController> mFakePointerController;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004179
Chris Yea52ade12020-08-27 16:49:20 -07004180 void SetUp() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004181 InputMapperTest::SetUp();
4182
Michael Wright17db18e2020-06-26 20:51:44 +01004183 mFakePointerController = std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00004184 mFakePolicy->setPointerController(mFakePointerController);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004185 }
4186
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004187 void testMotionRotation(CursorInputMapper& mapper, int32_t originalX, int32_t originalY,
4188 int32_t rotatedX, int32_t rotatedY);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004189
4190 void prepareDisplay(int32_t orientation) {
4191 const std::string uniqueId = "local:0";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004192 const ViewportType viewportType = ViewportType::INTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004193 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
4194 orientation, uniqueId, NO_PORT, viewportType);
4195 }
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004196
4197 static void assertCursorPointerCoords(const PointerCoords& coords, float x, float y,
4198 float pressure) {
4199 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(coords, x, y, pressure, 0.0f, 0.0f, 0.0f, 0.0f,
4200 0.0f, 0.0f, 0.0f, EPSILON));
4201 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08004202};
4203
4204const int32_t CursorInputMapperTest::TRACKBALL_MOVEMENT_THRESHOLD = 6;
4205
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004206void CursorInputMapperTest::testMotionRotation(CursorInputMapper& mapper, int32_t originalX,
4207 int32_t originalY, int32_t rotatedX,
4208 int32_t rotatedY) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004209 NotifyMotionArgs args;
4210
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004211 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, originalX);
4212 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, originalY);
4213 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004214 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4215 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004216 ASSERT_NO_FATAL_FAILURE(
4217 assertCursorPointerCoords(args.pointerCoords[0],
4218 float(rotatedX) / TRACKBALL_MOVEMENT_THRESHOLD,
4219 float(rotatedY) / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004220}
4221
4222TEST_F(CursorInputMapperTest, WhenModeIsPointer_GetSources_ReturnsMouse) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004223 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004224 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004225
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004226 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004227}
4228
4229TEST_F(CursorInputMapperTest, WhenModeIsNavigation_GetSources_ReturnsTrackball) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004230 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004231 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004232
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004233 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004234}
4235
4236TEST_F(CursorInputMapperTest, WhenModeIsPointer_PopulateDeviceInfo_ReturnsRangeFromPointerController) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004237 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004238 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004239
4240 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004241 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004242
4243 // Initially there may not be a valid motion range.
Yi Kong9b14ac62018-07-17 13:48:38 -07004244 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE));
4245 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004246 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4247 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE, 0.0f, 1.0f, 0.0f, 0.0f));
4248
4249 // When the bounds are set, then there should be a valid motion range.
4250 mFakePointerController->setBounds(1, 2, 800 - 1, 480 - 1);
4251
4252 InputDeviceInfo info2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004253 mapper.populateDeviceInfo(&info2);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004254
4255 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
4256 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE,
4257 1, 800 - 1, 0.0f, 0.0f));
4258 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
4259 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE,
4260 2, 480 - 1, 0.0f, 0.0f));
4261 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
4262 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE,
4263 0.0f, 1.0f, 0.0f, 0.0f));
4264}
4265
4266TEST_F(CursorInputMapperTest, WhenModeIsNavigation_PopulateDeviceInfo_ReturnsScaledRange) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004267 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004268 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004269
4270 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004271 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004272
4273 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4274 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_TRACKBALL,
4275 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
4276 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4277 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_TRACKBALL,
4278 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
4279 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4280 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TRACKBALL,
4281 0.0f, 1.0f, 0.0f, 0.0f));
4282}
4283
4284TEST_F(CursorInputMapperTest, Process_ShouldSetAllFieldsAndIncludeGlobalMetaState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004285 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004286 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004287
arthurhungdcef2dc2020-08-11 14:47:50 +08004288 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004289
4290 NotifyMotionArgs args;
4291
4292 // Button press.
4293 // Mostly testing non x/y behavior here so we don't need to check again elsewhere.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004294 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4295 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004296 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4297 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4298 ASSERT_EQ(DEVICE_ID, args.deviceId);
4299 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4300 ASSERT_EQ(uint32_t(0), args.policyFlags);
4301 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
4302 ASSERT_EQ(0, args.flags);
4303 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4304 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
4305 ASSERT_EQ(0, args.edgeFlags);
4306 ASSERT_EQ(uint32_t(1), args.pointerCount);
4307 ASSERT_EQ(0, args.pointerProperties[0].id);
4308 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004309 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004310 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4311 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4312 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4313
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004314 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4315 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4316 ASSERT_EQ(DEVICE_ID, args.deviceId);
4317 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4318 ASSERT_EQ(uint32_t(0), args.policyFlags);
4319 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
4320 ASSERT_EQ(0, args.flags);
4321 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4322 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
4323 ASSERT_EQ(0, args.edgeFlags);
4324 ASSERT_EQ(uint32_t(1), args.pointerCount);
4325 ASSERT_EQ(0, args.pointerProperties[0].id);
4326 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004327 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004328 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4329 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4330 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4331
Michael Wrightd02c5b62014-02-10 15:10:22 -08004332 // Button release. Should have same down time.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004333 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4334 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004335 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4336 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
4337 ASSERT_EQ(DEVICE_ID, args.deviceId);
4338 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4339 ASSERT_EQ(uint32_t(0), args.policyFlags);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004340 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
4341 ASSERT_EQ(0, args.flags);
4342 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4343 ASSERT_EQ(0, args.buttonState);
4344 ASSERT_EQ(0, args.edgeFlags);
4345 ASSERT_EQ(uint32_t(1), args.pointerCount);
4346 ASSERT_EQ(0, args.pointerProperties[0].id);
4347 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004348 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004349 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4350 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4351 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4352
4353 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4354 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
4355 ASSERT_EQ(DEVICE_ID, args.deviceId);
4356 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4357 ASSERT_EQ(uint32_t(0), args.policyFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004358 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
4359 ASSERT_EQ(0, args.flags);
4360 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4361 ASSERT_EQ(0, args.buttonState);
4362 ASSERT_EQ(0, args.edgeFlags);
4363 ASSERT_EQ(uint32_t(1), args.pointerCount);
4364 ASSERT_EQ(0, args.pointerProperties[0].id);
4365 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004366 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004367 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4368 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4369 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4370}
4371
4372TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentXYUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004373 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004374 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004375
4376 NotifyMotionArgs args;
4377
4378 // Motion in X but not Y.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004379 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4380 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004381 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4382 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004383 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4384 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f,
4385 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004386
4387 // Motion in Y but not X.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004388 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
4389 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004390 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4391 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004392 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f,
4393 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004394}
4395
4396TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004397 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004398 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004399
4400 NotifyMotionArgs args;
4401
4402 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004403 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4404 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004405 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4406 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004407 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004408
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004409 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4410 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004411 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004412
Michael Wrightd02c5b62014-02-10 15:10:22 -08004413 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004414 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4415 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004416 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004417 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004418 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004419
4420 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004421 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004422 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004423}
4424
4425TEST_F(CursorInputMapperTest, Process_ShouldHandleCombinedXYAndButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004426 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004427 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004428
4429 NotifyMotionArgs args;
4430
4431 // Combined X, Y and Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004432 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4433 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
4434 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4435 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004436 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4437 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004438 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4439 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4440 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004441
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004442 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4443 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004444 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4445 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4446 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004447
Michael Wrightd02c5b62014-02-10 15:10:22 -08004448 // Move X, Y a bit while pressed.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004449 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 2);
4450 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 1);
4451 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004452 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4453 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004454 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4455 2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4456 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004457
4458 // Release Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004459 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4460 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004461 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004462 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004463 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004464
4465 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004466 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004467 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004468}
4469
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004470TEST_F(CursorInputMapperTest, Process_WhenOrientationAware_ShouldNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004471 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004472 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
4473 // need to be rotated.
4474 addConfigurationProperty("cursor.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004475 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004476
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004477 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004478 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4479 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4480 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4481 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4482 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4483 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4484 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4485 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4486}
4487
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004488TEST_F(CursorInputMapperTest, Process_WhenNotOrientationAware_ShouldRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004489 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004490 // Since InputReader works in the un-rotated coordinate space, only devices that are not
4491 // orientation-aware are affected by display rotation.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004492 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004493
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004494 prepareDisplay(DISPLAY_ORIENTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004495 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4496 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4497 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4498 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4499 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4500 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4501 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4502 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4503
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004504 prepareDisplay(DISPLAY_ORIENTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004505 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, -1, 0));
4506 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, 1));
4507 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, 1));
4508 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, 1));
4509 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 1, 0));
4510 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, -1));
4511 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, -1));
4512 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, -1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004513
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004514 prepareDisplay(DISPLAY_ORIENTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004515 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, -1));
4516 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, -1));
4517 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, -1, 0));
4518 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, 1));
4519 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, 1));
4520 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, 1));
4521 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 1, 0));
4522 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, -1));
4523
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004524 prepareDisplay(DISPLAY_ORIENTATION_270);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004525 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 1, 0));
4526 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, -1));
4527 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, -1));
4528 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, -1));
4529 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, -1, 0));
4530 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, 1));
4531 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, 1));
4532 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, 1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004533}
4534
4535TEST_F(CursorInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004536 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004537 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004538
4539 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4540 mFakePointerController->setPosition(100, 200);
4541 mFakePointerController->setButtonState(0);
4542
4543 NotifyMotionArgs motionArgs;
4544 NotifyKeyArgs keyArgs;
4545
4546 // press BTN_LEFT, release BTN_LEFT
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004547 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 1);
4548 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004549 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4550 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4551 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4552 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004553 ASSERT_NO_FATAL_FAILURE(
4554 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004555
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004556 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4557 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4558 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4559 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004560 ASSERT_NO_FATAL_FAILURE(
4561 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004562
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004563 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 0);
4564 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004565 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004566 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004567 ASSERT_EQ(0, motionArgs.buttonState);
4568 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004569 ASSERT_NO_FATAL_FAILURE(
4570 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004571
4572 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004573 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004574 ASSERT_EQ(0, motionArgs.buttonState);
4575 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004576 ASSERT_NO_FATAL_FAILURE(
4577 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004578
4579 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004580 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004581 ASSERT_EQ(0, motionArgs.buttonState);
4582 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004583 ASSERT_NO_FATAL_FAILURE(
4584 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004585
4586 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004587 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 1);
4588 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 1);
4589 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004590 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4591 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4592 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4593 motionArgs.buttonState);
4594 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4595 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004596 ASSERT_NO_FATAL_FAILURE(
4597 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004598
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004599 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4600 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4601 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4602 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4603 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004604 ASSERT_NO_FATAL_FAILURE(
4605 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004606
4607 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4608 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4609 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4610 motionArgs.buttonState);
4611 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4612 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004613 ASSERT_NO_FATAL_FAILURE(
4614 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004615
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004616 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 0);
4617 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004618 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004619 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004620 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4621 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004622 ASSERT_NO_FATAL_FAILURE(
4623 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004624
4625 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004626 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004627 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4628 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004629 ASSERT_NO_FATAL_FAILURE(
4630 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004631
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004632 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4633 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004634 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004635 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
4636 ASSERT_EQ(0, motionArgs.buttonState);
4637 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004638 ASSERT_NO_FATAL_FAILURE(
4639 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004640 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4641 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004642
4643 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004644 ASSERT_EQ(0, motionArgs.buttonState);
4645 ASSERT_EQ(0, mFakePointerController->getButtonState());
4646 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004647 ASSERT_NO_FATAL_FAILURE(
4648 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004649
Michael Wrightd02c5b62014-02-10 15:10:22 -08004650 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4651 ASSERT_EQ(0, motionArgs.buttonState);
4652 ASSERT_EQ(0, mFakePointerController->getButtonState());
4653 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004654 ASSERT_NO_FATAL_FAILURE(
4655 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004656
4657 // press BTN_BACK, release BTN_BACK
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004658 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 1);
4659 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004660 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4661 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4662 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004663
Michael Wrightd02c5b62014-02-10 15:10:22 -08004664 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004665 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004666 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4667 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004668 ASSERT_NO_FATAL_FAILURE(
4669 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004670
4671 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4672 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4673 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4674 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004675 ASSERT_NO_FATAL_FAILURE(
4676 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004677
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004678 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 0);
4679 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004680 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004681 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004682 ASSERT_EQ(0, motionArgs.buttonState);
4683 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004684 ASSERT_NO_FATAL_FAILURE(
4685 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004686
4687 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004688 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004689 ASSERT_EQ(0, motionArgs.buttonState);
4690 ASSERT_EQ(0, mFakePointerController->getButtonState());
4691
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004692 ASSERT_NO_FATAL_FAILURE(
4693 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004694 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4695 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4696 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4697
4698 // press BTN_SIDE, release BTN_SIDE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004699 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 1);
4700 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004701 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4702 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4703 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004704
Michael Wrightd02c5b62014-02-10 15:10:22 -08004705 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004706 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004707 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4708 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004709 ASSERT_NO_FATAL_FAILURE(
4710 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004711
4712 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4713 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4714 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4715 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004716 ASSERT_NO_FATAL_FAILURE(
4717 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004718
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004719 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 0);
4720 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004721 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004722 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004723 ASSERT_EQ(0, motionArgs.buttonState);
4724 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004725 ASSERT_NO_FATAL_FAILURE(
4726 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004727
4728 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4729 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4730 ASSERT_EQ(0, motionArgs.buttonState);
4731 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004732 ASSERT_NO_FATAL_FAILURE(
4733 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004734
Michael Wrightd02c5b62014-02-10 15:10:22 -08004735 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4736 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4737 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4738
4739 // press BTN_FORWARD, release BTN_FORWARD
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004740 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 1);
4741 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004742 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4743 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4744 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004745
Michael Wrightd02c5b62014-02-10 15:10:22 -08004746 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004747 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004748 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4749 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004750 ASSERT_NO_FATAL_FAILURE(
4751 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004752
4753 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4754 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4755 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4756 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004757 ASSERT_NO_FATAL_FAILURE(
4758 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004759
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004760 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 0);
4761 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004762 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004763 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004764 ASSERT_EQ(0, motionArgs.buttonState);
4765 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004766 ASSERT_NO_FATAL_FAILURE(
4767 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004768
4769 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4770 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4771 ASSERT_EQ(0, motionArgs.buttonState);
4772 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004773 ASSERT_NO_FATAL_FAILURE(
4774 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004775
Michael Wrightd02c5b62014-02-10 15:10:22 -08004776 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4777 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4778 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4779
4780 // press BTN_EXTRA, release BTN_EXTRA
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004781 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 1);
4782 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004783 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4784 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4785 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004786
Michael Wrightd02c5b62014-02-10 15:10:22 -08004787 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004788 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004789 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4790 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004791 ASSERT_NO_FATAL_FAILURE(
4792 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004793
4794 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4795 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4796 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4797 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004798 ASSERT_NO_FATAL_FAILURE(
4799 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004800
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004801 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 0);
4802 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004803 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004804 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004805 ASSERT_EQ(0, motionArgs.buttonState);
4806 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004807 ASSERT_NO_FATAL_FAILURE(
4808 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004809
4810 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4811 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4812 ASSERT_EQ(0, motionArgs.buttonState);
4813 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004814 ASSERT_NO_FATAL_FAILURE(
4815 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004816
Michael Wrightd02c5b62014-02-10 15:10:22 -08004817 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4818 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4819 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4820}
4821
4822TEST_F(CursorInputMapperTest, Process_WhenModeIsPointer_ShouldMoveThePointerAround) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004823 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004824 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004825
4826 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4827 mFakePointerController->setPosition(100, 200);
4828 mFakePointerController->setButtonState(0);
4829
4830 NotifyMotionArgs args;
4831
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004832 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4833 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4834 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004835 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004836 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4837 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4838 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4839 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 +01004840 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004841}
4842
4843TEST_F(CursorInputMapperTest, Process_PointerCapture) {
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004844 addConfigurationProperty("cursor.mode", "pointer");
4845 mFakePolicy->setPointerCapture(true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004846 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004847
4848 NotifyDeviceResetArgs resetArgs;
4849 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4850 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4851 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4852
4853 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4854 mFakePointerController->setPosition(100, 200);
4855 mFakePointerController->setButtonState(0);
4856
4857 NotifyMotionArgs args;
4858
4859 // Move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004860 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4861 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4862 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004863 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4864 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4865 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4866 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4867 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 +01004868 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004869
4870 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004871 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4872 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004873 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4874 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4875 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
4876 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4877 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4878 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4879 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4880 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
4881 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4882 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4883
4884 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004885 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4886 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004887 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4888 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4889 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
4890 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4891 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4892 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4893 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4894 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
4895 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4896 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4897
4898 // Another move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004899 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 30);
4900 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 40);
4901 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004902 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4903 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4904 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4905 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4906 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 +01004907 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004908
4909 // Disable pointer capture and check that the device generation got bumped
4910 // and events are generated the usual way.
arthurhungdcef2dc2020-08-11 14:47:50 +08004911 const uint32_t generation = mReader->getContext()->getGeneration();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004912 mFakePolicy->setPointerCapture(false);
4913 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
arthurhungdcef2dc2020-08-11 14:47:50 +08004914 ASSERT_TRUE(mReader->getContext()->getGeneration() != generation);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004915
4916 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004917 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4918
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004919 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4920 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4921 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004922 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4923 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004924 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4925 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4926 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 +01004927 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004928}
4929
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00004930/**
4931 * When Pointer Capture is enabled, we expect to report unprocessed relative movements, so any
4932 * pointer acceleration or speed processing should not be applied.
4933 */
4934TEST_F(CursorInputMapperTest, PointerCaptureDisablesVelocityProcessing) {
4935 addConfigurationProperty("cursor.mode", "pointer");
4936 const VelocityControlParameters testParams(5.f /*scale*/, 0.f /*low threshold*/,
4937 100.f /*high threshold*/, 10.f /*acceleration*/);
4938 mFakePolicy->setVelocityControlParams(testParams);
4939 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
4940
4941 NotifyDeviceResetArgs resetArgs;
4942 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4943 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4944 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4945
4946 NotifyMotionArgs args;
4947
4948 // Move and verify scale is applied.
4949 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4950 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4951 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4952 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4953 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4954 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4955 const float relX = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X);
4956 const float relY = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y);
4957 ASSERT_GT(relX, 10);
4958 ASSERT_GT(relY, 20);
4959
4960 // Enable Pointer Capture
4961 mFakePolicy->setPointerCapture(true);
4962 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
4963 NotifyPointerCaptureChangedArgs captureArgs;
4964 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyCaptureWasCalled(&captureArgs));
4965 ASSERT_TRUE(captureArgs.request.enable);
4966
4967 // Move and verify scale is not applied.
4968 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4969 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4970 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4971 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4972 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4973 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4974 ASSERT_EQ(10, args.pointerCoords[0].getX());
4975 ASSERT_EQ(20, args.pointerCoords[0].getY());
4976}
4977
Prabir Pradhan258e2b92022-06-24 18:37:04 +00004978TEST_F(CursorInputMapperTest, PointerCaptureDisablesOrientationChanges) {
4979 addConfigurationProperty("cursor.mode", "pointer");
4980 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
4981
4982 NotifyDeviceResetArgs resetArgs;
4983 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4984 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4985 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4986
4987 // Ensure the display is rotated.
4988 prepareDisplay(DISPLAY_ORIENTATION_90);
4989
4990 NotifyMotionArgs args;
4991
4992 // Verify that the coordinates are rotated.
4993 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4994 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4995 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4996 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4997 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4998 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4999 ASSERT_EQ(-20, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X));
5000 ASSERT_EQ(10, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y));
5001
5002 // Enable Pointer Capture.
5003 mFakePolicy->setPointerCapture(true);
5004 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
5005 NotifyPointerCaptureChangedArgs captureArgs;
5006 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyCaptureWasCalled(&captureArgs));
5007 ASSERT_TRUE(captureArgs.request.enable);
5008
5009 // Move and verify rotation is not applied.
5010 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5011 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5012 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5013 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5014 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
5015 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
5016 ASSERT_EQ(10, args.pointerCoords[0].getX());
5017 ASSERT_EQ(20, args.pointerCoords[0].getY());
5018}
5019
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005020TEST_F(CursorInputMapperTest, Process_ShouldHandleDisplayId) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005021 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005022
Garfield Tan888a6a42020-01-09 11:39:16 -08005023 // Setup for second display.
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005024 constexpr int32_t SECOND_DISPLAY_ID = 1;
Garfield Tan888a6a42020-01-09 11:39:16 -08005025 const std::string SECOND_DISPLAY_UNIQUE_ID = "local:1";
5026 mFakePolicy->addDisplayViewport(SECOND_DISPLAY_ID, 800, 480, DISPLAY_ORIENTATION_0,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00005027 true /*isActive*/, SECOND_DISPLAY_UNIQUE_ID, NO_PORT,
5028 ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08005029 mFakePolicy->setDefaultPointerDisplayId(SECOND_DISPLAY_ID);
5030 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
5031
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005032 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
5033 mFakePointerController->setPosition(100, 200);
5034 mFakePointerController->setButtonState(0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005035
5036 NotifyMotionArgs args;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005037 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5038 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5039 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005040 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5041 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
5042 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
5043 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5044 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 +01005045 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005046 ASSERT_EQ(SECOND_DISPLAY_ID, args.displayId);
5047}
5048
Michael Wrightd02c5b62014-02-10 15:10:22 -08005049// --- TouchInputMapperTest ---
5050
5051class TouchInputMapperTest : public InputMapperTest {
5052protected:
5053 static const int32_t RAW_X_MIN;
5054 static const int32_t RAW_X_MAX;
5055 static const int32_t RAW_Y_MIN;
5056 static const int32_t RAW_Y_MAX;
5057 static const int32_t RAW_TOUCH_MIN;
5058 static const int32_t RAW_TOUCH_MAX;
5059 static const int32_t RAW_TOOL_MIN;
5060 static const int32_t RAW_TOOL_MAX;
5061 static const int32_t RAW_PRESSURE_MIN;
5062 static const int32_t RAW_PRESSURE_MAX;
5063 static const int32_t RAW_ORIENTATION_MIN;
5064 static const int32_t RAW_ORIENTATION_MAX;
5065 static const int32_t RAW_DISTANCE_MIN;
5066 static const int32_t RAW_DISTANCE_MAX;
5067 static const int32_t RAW_TILT_MIN;
5068 static const int32_t RAW_TILT_MAX;
5069 static const int32_t RAW_ID_MIN;
5070 static const int32_t RAW_ID_MAX;
5071 static const int32_t RAW_SLOT_MIN;
5072 static const int32_t RAW_SLOT_MAX;
5073 static const float X_PRECISION;
5074 static const float Y_PRECISION;
Santos Cordonfa5cf462017-04-05 10:37:00 -07005075 static const float X_PRECISION_VIRTUAL;
5076 static const float Y_PRECISION_VIRTUAL;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005077
5078 static const float GEOMETRIC_SCALE;
Jason Gerecke489fda82012-09-07 17:19:40 -07005079 static const TouchAffineTransformation AFFINE_TRANSFORM;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005080
5081 static const VirtualKeyDefinition VIRTUAL_KEYS[2];
5082
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005083 const std::string UNIQUE_ID = "local:0";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005084 const std::string SECONDARY_UNIQUE_ID = "local:1";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005085
Michael Wrightd02c5b62014-02-10 15:10:22 -08005086 enum Axes {
5087 POSITION = 1 << 0,
5088 TOUCH = 1 << 1,
5089 TOOL = 1 << 2,
5090 PRESSURE = 1 << 3,
5091 ORIENTATION = 1 << 4,
5092 MINOR = 1 << 5,
5093 ID = 1 << 6,
5094 DISTANCE = 1 << 7,
5095 TILT = 1 << 8,
5096 SLOT = 1 << 9,
5097 TOOL_TYPE = 1 << 10,
5098 };
5099
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005100 void prepareDisplay(int32_t orientation, std::optional<uint8_t> port = NO_PORT);
5101 void prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port = NO_PORT);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005102 void prepareVirtualDisplay(int32_t orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005103 void prepareVirtualKeys();
Jason Gerecke489fda82012-09-07 17:19:40 -07005104 void prepareLocationCalibration();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005105 int32_t toRawX(float displayX);
5106 int32_t toRawY(float displayY);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005107 int32_t toRotatedRawX(float displayX);
5108 int32_t toRotatedRawY(float displayY);
Jason Gerecke489fda82012-09-07 17:19:40 -07005109 float toCookedX(float rawX, float rawY);
5110 float toCookedY(float rawX, float rawY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005111 float toDisplayX(int32_t rawX);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005112 float toDisplayX(int32_t rawX, int32_t displayWidth);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005113 float toDisplayY(int32_t rawY);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005114 float toDisplayY(int32_t rawY, int32_t displayHeight);
5115
Michael Wrightd02c5b62014-02-10 15:10:22 -08005116};
5117
5118const int32_t TouchInputMapperTest::RAW_X_MIN = 25;
5119const int32_t TouchInputMapperTest::RAW_X_MAX = 1019;
5120const int32_t TouchInputMapperTest::RAW_Y_MIN = 30;
5121const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009;
5122const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0;
5123const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31;
5124const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0;
5125const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15;
Michael Wrightaa449c92017-12-13 21:21:43 +00005126const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = 0;
5127const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = 255;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005128const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7;
5129const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
5130const int32_t TouchInputMapperTest::RAW_DISTANCE_MIN = 0;
5131const int32_t TouchInputMapperTest::RAW_DISTANCE_MAX = 7;
5132const int32_t TouchInputMapperTest::RAW_TILT_MIN = 0;
5133const int32_t TouchInputMapperTest::RAW_TILT_MAX = 150;
5134const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
5135const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
5136const int32_t TouchInputMapperTest::RAW_SLOT_MIN = 0;
5137const int32_t TouchInputMapperTest::RAW_SLOT_MAX = 9;
5138const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH;
5139const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT;
Santos Cordonfa5cf462017-04-05 10:37:00 -07005140const float TouchInputMapperTest::X_PRECISION_VIRTUAL =
5141 float(RAW_X_MAX - RAW_X_MIN + 1) / VIRTUAL_DISPLAY_WIDTH;
5142const float TouchInputMapperTest::Y_PRECISION_VIRTUAL =
5143 float(RAW_Y_MAX - RAW_Y_MIN + 1) / VIRTUAL_DISPLAY_HEIGHT;
Jason Gerecke489fda82012-09-07 17:19:40 -07005144const TouchAffineTransformation TouchInputMapperTest::AFFINE_TRANSFORM =
5145 TouchAffineTransformation(1, -2, 3, -4, 5, -6);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005146
5147const float TouchInputMapperTest::GEOMETRIC_SCALE =
5148 avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1),
5149 float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1));
5150
5151const VirtualKeyDefinition TouchInputMapperTest::VIRTUAL_KEYS[2] = {
5152 { KEY_HOME, 60, DISPLAY_HEIGHT + 15, 20, 20 },
5153 { KEY_MENU, DISPLAY_HEIGHT - 60, DISPLAY_WIDTH + 15, 20, 20 },
5154};
5155
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005156void TouchInputMapperTest::prepareDisplay(int32_t orientation, std::optional<uint8_t> port) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01005157 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
5158 port, ViewportType::INTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005159}
5160
5161void TouchInputMapperTest::prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port) {
5162 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
5163 DISPLAY_ORIENTATION_0, SECONDARY_UNIQUE_ID, port, type);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005164}
5165
Santos Cordonfa5cf462017-04-05 10:37:00 -07005166void TouchInputMapperTest::prepareVirtualDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01005167 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH, VIRTUAL_DISPLAY_HEIGHT,
5168 orientation, VIRTUAL_DISPLAY_UNIQUE_ID, NO_PORT,
5169 ViewportType::VIRTUAL);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005170}
5171
Michael Wrightd02c5b62014-02-10 15:10:22 -08005172void TouchInputMapperTest::prepareVirtualKeys() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005173 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[0]);
5174 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[1]);
5175 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
5176 mFakeEventHub->addKey(EVENTHUB_ID, KEY_MENU, 0, AKEYCODE_MENU, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005177}
5178
Jason Gerecke489fda82012-09-07 17:19:40 -07005179void TouchInputMapperTest::prepareLocationCalibration() {
5180 mFakePolicy->setTouchAffineTransformation(AFFINE_TRANSFORM);
5181}
5182
Michael Wrightd02c5b62014-02-10 15:10:22 -08005183int32_t TouchInputMapperTest::toRawX(float displayX) {
5184 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN);
5185}
5186
5187int32_t TouchInputMapperTest::toRawY(float displayY) {
5188 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN);
5189}
5190
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005191int32_t TouchInputMapperTest::toRotatedRawX(float displayX) {
5192 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_HEIGHT + RAW_X_MIN);
5193}
5194
5195int32_t TouchInputMapperTest::toRotatedRawY(float displayY) {
5196 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_WIDTH + RAW_Y_MIN);
5197}
5198
Jason Gerecke489fda82012-09-07 17:19:40 -07005199float TouchInputMapperTest::toCookedX(float rawX, float rawY) {
5200 AFFINE_TRANSFORM.applyTo(rawX, rawY);
5201 return rawX;
5202}
5203
5204float TouchInputMapperTest::toCookedY(float rawX, float rawY) {
5205 AFFINE_TRANSFORM.applyTo(rawX, rawY);
5206 return rawY;
5207}
5208
Michael Wrightd02c5b62014-02-10 15:10:22 -08005209float TouchInputMapperTest::toDisplayX(int32_t rawX) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005210 return toDisplayX(rawX, DISPLAY_WIDTH);
5211}
5212
5213float TouchInputMapperTest::toDisplayX(int32_t rawX, int32_t displayWidth) {
5214 return float(rawX - RAW_X_MIN) * displayWidth / (RAW_X_MAX - RAW_X_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005215}
5216
5217float TouchInputMapperTest::toDisplayY(int32_t rawY) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005218 return toDisplayY(rawY, DISPLAY_HEIGHT);
5219}
5220
5221float TouchInputMapperTest::toDisplayY(int32_t rawY, int32_t displayHeight) {
5222 return float(rawY - RAW_Y_MIN) * displayHeight / (RAW_Y_MAX - RAW_Y_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005223}
5224
5225
5226// --- SingleTouchInputMapperTest ---
5227
5228class SingleTouchInputMapperTest : public TouchInputMapperTest {
5229protected:
5230 void prepareButtons();
5231 void prepareAxes(int axes);
5232
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005233 void processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
5234 void processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
5235 void processUp(SingleTouchInputMapper& mappery);
5236 void processPressure(SingleTouchInputMapper& mapper, int32_t pressure);
5237 void processToolMajor(SingleTouchInputMapper& mapper, int32_t toolMajor);
5238 void processDistance(SingleTouchInputMapper& mapper, int32_t distance);
5239 void processTilt(SingleTouchInputMapper& mapper, int32_t tiltX, int32_t tiltY);
5240 void processKey(SingleTouchInputMapper& mapper, int32_t code, int32_t value);
5241 void processSync(SingleTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005242};
5243
5244void SingleTouchInputMapperTest::prepareButtons() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005245 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005246}
5247
5248void SingleTouchInputMapperTest::prepareAxes(int axes) {
5249 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005250 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
5251 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005252 }
5253 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005254 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MIN,
5255 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005256 }
5257 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005258 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TOOL_WIDTH, RAW_TOOL_MIN, RAW_TOOL_MAX, 0,
5259 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005260 }
5261 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005262 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_DISTANCE, RAW_DISTANCE_MIN,
5263 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005264 }
5265 if (axes & TILT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005266 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_X, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
5267 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_Y, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005268 }
5269}
5270
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005271void SingleTouchInputMapperTest::processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005272 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
5273 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
5274 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005275}
5276
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005277void SingleTouchInputMapperTest::processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005278 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
5279 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005280}
5281
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005282void SingleTouchInputMapperTest::processUp(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005283 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005284}
5285
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005286void SingleTouchInputMapperTest::processPressure(SingleTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005287 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005288}
5289
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005290void SingleTouchInputMapperTest::processToolMajor(SingleTouchInputMapper& mapper,
5291 int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005292 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TOOL_WIDTH, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005293}
5294
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005295void SingleTouchInputMapperTest::processDistance(SingleTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005296 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005297}
5298
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005299void SingleTouchInputMapperTest::processTilt(SingleTouchInputMapper& mapper, int32_t tiltX,
5300 int32_t tiltY) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005301 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_X, tiltX);
5302 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_Y, tiltY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005303}
5304
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005305void SingleTouchInputMapperTest::processKey(SingleTouchInputMapper& mapper, int32_t code,
5306 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005307 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005308}
5309
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005310void SingleTouchInputMapperTest::processSync(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005311 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005312}
5313
Michael Wrightd02c5b62014-02-10 15:10:22 -08005314TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005315 prepareButtons();
5316 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005317 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005318
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005319 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005320}
5321
5322TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndIsACursor_ReturnsTouchPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005323 mFakeEventHub->addRelativeAxis(EVENTHUB_ID, REL_X);
5324 mFakeEventHub->addRelativeAxis(EVENTHUB_ID, REL_Y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005325 prepareButtons();
5326 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005327 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005328
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005329 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005330}
5331
5332TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchPad_ReturnsTouchPad) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005333 prepareButtons();
5334 prepareAxes(POSITION);
5335 addConfigurationProperty("touch.deviceType", "touchPad");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005336 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005337
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005338 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005339}
5340
5341TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005342 prepareButtons();
5343 prepareAxes(POSITION);
5344 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005345 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005346
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005347 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005348}
5349
5350TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005351 addConfigurationProperty("touch.deviceType", "touchScreen");
5352 prepareDisplay(DISPLAY_ORIENTATION_0);
5353 prepareButtons();
5354 prepareAxes(POSITION);
5355 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005356 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005357
5358 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005359 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005360
5361 // Virtual key is down.
5362 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5363 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5364 processDown(mapper, x, y);
5365 processSync(mapper);
5366 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5367
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005368 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005369
5370 // Virtual key is up.
5371 processUp(mapper);
5372 processSync(mapper);
5373 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5374
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005375 ASSERT_EQ(AKEY_STATE_UP, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005376}
5377
5378TEST_F(SingleTouchInputMapperTest, GetScanCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005379 addConfigurationProperty("touch.deviceType", "touchScreen");
5380 prepareDisplay(DISPLAY_ORIENTATION_0);
5381 prepareButtons();
5382 prepareAxes(POSITION);
5383 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005384 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005385
5386 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005387 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005388
5389 // Virtual key is down.
5390 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5391 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5392 processDown(mapper, x, y);
5393 processSync(mapper);
5394 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5395
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005396 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005397
5398 // Virtual key is up.
5399 processUp(mapper);
5400 processSync(mapper);
5401 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5402
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005403 ASSERT_EQ(AKEY_STATE_UP, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005404}
5405
5406TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005407 addConfigurationProperty("touch.deviceType", "touchScreen");
5408 prepareDisplay(DISPLAY_ORIENTATION_0);
5409 prepareButtons();
5410 prepareAxes(POSITION);
5411 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005412 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005413
5414 const int32_t keys[2] = { AKEYCODE_HOME, AKEYCODE_A };
5415 uint8_t flags[2] = { 0, 0 };
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005416 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, 2, keys, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005417 ASSERT_TRUE(flags[0]);
5418 ASSERT_FALSE(flags[1]);
5419}
5420
5421TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005422 addConfigurationProperty("touch.deviceType", "touchScreen");
5423 prepareDisplay(DISPLAY_ORIENTATION_0);
5424 prepareButtons();
5425 prepareAxes(POSITION);
5426 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005427 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005428
arthurhungdcef2dc2020-08-11 14:47:50 +08005429 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005430
5431 NotifyKeyArgs args;
5432
5433 // Press virtual key.
5434 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5435 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5436 processDown(mapper, x, y);
5437 processSync(mapper);
5438
5439 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
5440 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5441 ASSERT_EQ(DEVICE_ID, args.deviceId);
5442 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
5443 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
5444 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
5445 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5446 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5447 ASSERT_EQ(KEY_HOME, args.scanCode);
5448 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5449 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5450
5451 // Release virtual key.
5452 processUp(mapper);
5453 processSync(mapper);
5454
5455 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
5456 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5457 ASSERT_EQ(DEVICE_ID, args.deviceId);
5458 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
5459 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
5460 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
5461 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5462 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5463 ASSERT_EQ(KEY_HOME, args.scanCode);
5464 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5465 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5466
5467 // Should not have sent any motions.
5468 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5469}
5470
5471TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005472 addConfigurationProperty("touch.deviceType", "touchScreen");
5473 prepareDisplay(DISPLAY_ORIENTATION_0);
5474 prepareButtons();
5475 prepareAxes(POSITION);
5476 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005477 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005478
arthurhungdcef2dc2020-08-11 14:47:50 +08005479 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005480
5481 NotifyKeyArgs keyArgs;
5482
5483 // Press virtual key.
5484 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5485 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5486 processDown(mapper, x, y);
5487 processSync(mapper);
5488
5489 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5490 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5491 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5492 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5493 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5494 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5495 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags);
5496 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5497 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5498 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5499 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5500
5501 // Move out of bounds. This should generate a cancel and a pointer down since we moved
5502 // into the display area.
5503 y -= 100;
5504 processMove(mapper, x, y);
5505 processSync(mapper);
5506
5507 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5508 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5509 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5510 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5511 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5512 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5513 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
5514 | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags);
5515 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5516 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5517 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5518 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5519
5520 NotifyMotionArgs motionArgs;
5521 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5522 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5523 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5524 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5525 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5526 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5527 ASSERT_EQ(0, motionArgs.flags);
5528 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5529 ASSERT_EQ(0, motionArgs.buttonState);
5530 ASSERT_EQ(0, motionArgs.edgeFlags);
5531 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5532 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5533 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5534 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5535 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5536 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5537 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5538 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5539
5540 // Keep moving out of bounds. Should generate a pointer move.
5541 y -= 50;
5542 processMove(mapper, x, y);
5543 processSync(mapper);
5544
5545 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5546 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5547 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5548 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5549 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5550 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5551 ASSERT_EQ(0, motionArgs.flags);
5552 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5553 ASSERT_EQ(0, motionArgs.buttonState);
5554 ASSERT_EQ(0, motionArgs.edgeFlags);
5555 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5556 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5557 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5558 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5559 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5560 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5561 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5562 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5563
5564 // Release out of bounds. Should generate a pointer up.
5565 processUp(mapper);
5566 processSync(mapper);
5567
5568 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5569 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5570 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5571 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5572 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5573 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5574 ASSERT_EQ(0, motionArgs.flags);
5575 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5576 ASSERT_EQ(0, motionArgs.buttonState);
5577 ASSERT_EQ(0, motionArgs.edgeFlags);
5578 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5579 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5580 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5581 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5582 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5583 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5584 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5585 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5586
5587 // Should not have sent any more keys or motions.
5588 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5589 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5590}
5591
5592TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005593 addConfigurationProperty("touch.deviceType", "touchScreen");
5594 prepareDisplay(DISPLAY_ORIENTATION_0);
5595 prepareButtons();
5596 prepareAxes(POSITION);
5597 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005598 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005599
arthurhungdcef2dc2020-08-11 14:47:50 +08005600 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005601
5602 NotifyMotionArgs motionArgs;
5603
5604 // Initially go down out of bounds.
5605 int32_t x = -10;
5606 int32_t y = -10;
5607 processDown(mapper, x, y);
5608 processSync(mapper);
5609
5610 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5611
5612 // Move into the display area. Should generate a pointer down.
5613 x = 50;
5614 y = 75;
5615 processMove(mapper, x, y);
5616 processSync(mapper);
5617
5618 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5619 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5620 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5621 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5622 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5623 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5624 ASSERT_EQ(0, motionArgs.flags);
5625 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5626 ASSERT_EQ(0, motionArgs.buttonState);
5627 ASSERT_EQ(0, motionArgs.edgeFlags);
5628 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5629 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5630 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5631 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5632 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5633 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5634 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5635 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5636
5637 // Release. Should generate a pointer up.
5638 processUp(mapper);
5639 processSync(mapper);
5640
5641 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5642 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5643 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5644 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5645 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5646 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5647 ASSERT_EQ(0, motionArgs.flags);
5648 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5649 ASSERT_EQ(0, motionArgs.buttonState);
5650 ASSERT_EQ(0, motionArgs.edgeFlags);
5651 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5652 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5653 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5654 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5655 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5656 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5657 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5658 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5659
5660 // Should not have sent any more keys or motions.
5661 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5662 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5663}
5664
Santos Cordonfa5cf462017-04-05 10:37:00 -07005665TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture_VirtualDisplay) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005666 addConfigurationProperty("touch.deviceType", "touchScreen");
5667 addConfigurationProperty("touch.displayId", VIRTUAL_DISPLAY_UNIQUE_ID);
5668
5669 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
5670 prepareButtons();
5671 prepareAxes(POSITION);
5672 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005673 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Santos Cordonfa5cf462017-04-05 10:37:00 -07005674
arthurhungdcef2dc2020-08-11 14:47:50 +08005675 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005676
5677 NotifyMotionArgs motionArgs;
5678
5679 // Down.
5680 int32_t x = 100;
5681 int32_t y = 125;
5682 processDown(mapper, x, y);
5683 processSync(mapper);
5684
5685 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5686 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5687 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5688 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5689 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5690 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5691 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5692 ASSERT_EQ(0, motionArgs.flags);
5693 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5694 ASSERT_EQ(0, motionArgs.buttonState);
5695 ASSERT_EQ(0, motionArgs.edgeFlags);
5696 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5697 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5698 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5699 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5700 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5701 1, 0, 0, 0, 0, 0, 0, 0));
5702 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5703 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5704 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5705
5706 // Move.
5707 x += 50;
5708 y += 75;
5709 processMove(mapper, x, y);
5710 processSync(mapper);
5711
5712 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5713 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5714 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5715 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5716 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5717 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5718 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5719 ASSERT_EQ(0, motionArgs.flags);
5720 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5721 ASSERT_EQ(0, motionArgs.buttonState);
5722 ASSERT_EQ(0, motionArgs.edgeFlags);
5723 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5724 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5725 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5726 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5727 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5728 1, 0, 0, 0, 0, 0, 0, 0));
5729 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5730 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5731 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5732
5733 // Up.
5734 processUp(mapper);
5735 processSync(mapper);
5736
5737 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5738 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5739 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5740 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5741 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5742 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5743 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5744 ASSERT_EQ(0, motionArgs.flags);
5745 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5746 ASSERT_EQ(0, motionArgs.buttonState);
5747 ASSERT_EQ(0, motionArgs.edgeFlags);
5748 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5749 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5750 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5751 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5752 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5753 1, 0, 0, 0, 0, 0, 0, 0));
5754 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5755 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5756 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5757
5758 // Should not have sent any more keys or motions.
5759 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5760 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5761}
5762
Michael Wrightd02c5b62014-02-10 15:10:22 -08005763TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005764 addConfigurationProperty("touch.deviceType", "touchScreen");
5765 prepareDisplay(DISPLAY_ORIENTATION_0);
5766 prepareButtons();
5767 prepareAxes(POSITION);
5768 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005769 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005770
arthurhungdcef2dc2020-08-11 14:47:50 +08005771 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005772
5773 NotifyMotionArgs motionArgs;
5774
5775 // Down.
5776 int32_t x = 100;
5777 int32_t y = 125;
5778 processDown(mapper, x, y);
5779 processSync(mapper);
5780
5781 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5782 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5783 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5784 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5785 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5786 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5787 ASSERT_EQ(0, motionArgs.flags);
5788 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5789 ASSERT_EQ(0, motionArgs.buttonState);
5790 ASSERT_EQ(0, motionArgs.edgeFlags);
5791 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5792 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5793 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5794 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5795 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5796 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5797 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5798 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5799
5800 // Move.
5801 x += 50;
5802 y += 75;
5803 processMove(mapper, x, y);
5804 processSync(mapper);
5805
5806 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5807 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5808 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5809 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5810 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5811 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5812 ASSERT_EQ(0, motionArgs.flags);
5813 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5814 ASSERT_EQ(0, motionArgs.buttonState);
5815 ASSERT_EQ(0, motionArgs.edgeFlags);
5816 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5817 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5818 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5819 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5820 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5821 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5822 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5823 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5824
5825 // Up.
5826 processUp(mapper);
5827 processSync(mapper);
5828
5829 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5830 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5831 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5832 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5833 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5834 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5835 ASSERT_EQ(0, motionArgs.flags);
5836 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5837 ASSERT_EQ(0, motionArgs.buttonState);
5838 ASSERT_EQ(0, motionArgs.edgeFlags);
5839 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5840 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5841 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5842 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5843 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5844 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5845 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5846 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5847
5848 // Should not have sent any more keys or motions.
5849 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5850 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5851}
5852
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005853TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_DoesNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005854 addConfigurationProperty("touch.deviceType", "touchScreen");
5855 prepareButtons();
5856 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005857 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
5858 // need to be rotated. Touchscreens are orientation-aware by default.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005859 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005860
5861 NotifyMotionArgs args;
5862
5863 // Rotation 90.
5864 prepareDisplay(DISPLAY_ORIENTATION_90);
5865 processDown(mapper, toRawX(50), toRawY(75));
5866 processSync(mapper);
5867
5868 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5869 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5870 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5871
5872 processUp(mapper);
5873 processSync(mapper);
5874 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5875}
5876
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005877TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_RotatesMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005878 addConfigurationProperty("touch.deviceType", "touchScreen");
5879 prepareButtons();
5880 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005881 // Since InputReader works in the un-rotated coordinate space, only devices that are not
5882 // orientation-aware are affected by display rotation.
5883 addConfigurationProperty("touch.orientationAware", "0");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005884 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005885
5886 NotifyMotionArgs args;
5887
5888 // Rotation 0.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005889 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005890 prepareDisplay(DISPLAY_ORIENTATION_0);
5891 processDown(mapper, toRawX(50), toRawY(75));
5892 processSync(mapper);
5893
5894 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5895 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5896 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5897
5898 processUp(mapper);
5899 processSync(mapper);
5900 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5901
5902 // Rotation 90.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005903 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005904 prepareDisplay(DISPLAY_ORIENTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005905 processDown(mapper, toRawX(75), RAW_Y_MAX - toRawY(50) + RAW_Y_MIN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005906 processSync(mapper);
5907
5908 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5909 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5910 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5911
5912 processUp(mapper);
5913 processSync(mapper);
5914 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5915
5916 // Rotation 180.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005917 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005918 prepareDisplay(DISPLAY_ORIENTATION_180);
5919 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
5920 processSync(mapper);
5921
5922 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5923 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5924 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5925
5926 processUp(mapper);
5927 processSync(mapper);
5928 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5929
5930 // Rotation 270.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005931 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005932 prepareDisplay(DISPLAY_ORIENTATION_270);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005933 processDown(mapper, RAW_X_MAX - toRawX(75) + RAW_X_MIN, toRawY(50));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005934 processSync(mapper);
5935
5936 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5937 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5938 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5939
5940 processUp(mapper);
5941 processSync(mapper);
5942 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5943}
5944
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005945TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation0_RotatesMotions) {
5946 addConfigurationProperty("touch.deviceType", "touchScreen");
5947 prepareButtons();
5948 prepareAxes(POSITION);
5949 addConfigurationProperty("touch.orientationAware", "1");
5950 addConfigurationProperty("touch.orientation", "ORIENTATION_0");
5951 clearViewports();
5952 prepareDisplay(DISPLAY_ORIENTATION_0);
5953 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5954 NotifyMotionArgs args;
5955
5956 // Orientation 0.
5957 processDown(mapper, toRawX(50), toRawY(75));
5958 processSync(mapper);
5959
5960 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5961 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5962 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5963
5964 processUp(mapper);
5965 processSync(mapper);
5966 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5967}
5968
5969TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation90_RotatesMotions) {
5970 addConfigurationProperty("touch.deviceType", "touchScreen");
5971 prepareButtons();
5972 prepareAxes(POSITION);
5973 addConfigurationProperty("touch.orientationAware", "1");
5974 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
5975 clearViewports();
5976 prepareDisplay(DISPLAY_ORIENTATION_0);
5977 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5978 NotifyMotionArgs args;
5979
5980 // Orientation 90.
5981 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
5982 processSync(mapper);
5983
5984 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5985 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5986 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5987
5988 processUp(mapper);
5989 processSync(mapper);
5990 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5991}
5992
5993TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation180_RotatesMotions) {
5994 addConfigurationProperty("touch.deviceType", "touchScreen");
5995 prepareButtons();
5996 prepareAxes(POSITION);
5997 addConfigurationProperty("touch.orientationAware", "1");
5998 addConfigurationProperty("touch.orientation", "ORIENTATION_180");
5999 clearViewports();
6000 prepareDisplay(DISPLAY_ORIENTATION_0);
6001 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6002 NotifyMotionArgs args;
6003
6004 // Orientation 180.
6005 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
6006 processSync(mapper);
6007
6008 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6009 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6010 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6011
6012 processUp(mapper);
6013 processSync(mapper);
6014 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6015}
6016
6017TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation270_RotatesMotions) {
6018 addConfigurationProperty("touch.deviceType", "touchScreen");
6019 prepareButtons();
6020 prepareAxes(POSITION);
6021 addConfigurationProperty("touch.orientationAware", "1");
6022 addConfigurationProperty("touch.orientation", "ORIENTATION_270");
6023 clearViewports();
6024 prepareDisplay(DISPLAY_ORIENTATION_0);
6025 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6026 NotifyMotionArgs args;
6027
6028 // Orientation 270.
6029 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
6030 processSync(mapper);
6031
6032 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6033 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6034 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6035
6036 processUp(mapper);
6037 processSync(mapper);
6038 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6039}
6040
6041TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationSpecified_RotatesMotionWithDisplay) {
6042 addConfigurationProperty("touch.deviceType", "touchScreen");
6043 prepareButtons();
6044 prepareAxes(POSITION);
6045 // Since InputReader works in the un-rotated coordinate space, only devices that are not
6046 // orientation-aware are affected by display rotation.
6047 addConfigurationProperty("touch.orientationAware", "0");
6048 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
6049 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6050
6051 NotifyMotionArgs args;
6052
6053 // Orientation 90, Rotation 0.
6054 clearViewports();
6055 prepareDisplay(DISPLAY_ORIENTATION_0);
6056 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
6057 processSync(mapper);
6058
6059 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6060 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6061 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6062
6063 processUp(mapper);
6064 processSync(mapper);
6065 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6066
6067 // Orientation 90, Rotation 90.
6068 clearViewports();
6069 prepareDisplay(DISPLAY_ORIENTATION_90);
6070 processDown(mapper, toRotatedRawX(50), toRotatedRawY(75));
6071 processSync(mapper);
6072
6073 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6074 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6075 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6076
6077 processUp(mapper);
6078 processSync(mapper);
6079 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6080
6081 // Orientation 90, Rotation 180.
6082 clearViewports();
6083 prepareDisplay(DISPLAY_ORIENTATION_180);
6084 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
6085 processSync(mapper);
6086
6087 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6088 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6089 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6090
6091 processUp(mapper);
6092 processSync(mapper);
6093 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6094
6095 // Orientation 90, Rotation 270.
6096 clearViewports();
6097 prepareDisplay(DISPLAY_ORIENTATION_270);
6098 processDown(mapper, RAW_X_MAX - toRotatedRawX(50) + RAW_X_MIN,
6099 RAW_Y_MAX - toRotatedRawY(75) + RAW_Y_MIN);
6100 processSync(mapper);
6101
6102 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6103 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6104 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6105
6106 processUp(mapper);
6107 processSync(mapper);
6108 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6109}
6110
Michael Wrightd02c5b62014-02-10 15:10:22 -08006111TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006112 addConfigurationProperty("touch.deviceType", "touchScreen");
6113 prepareDisplay(DISPLAY_ORIENTATION_0);
6114 prepareButtons();
6115 prepareAxes(POSITION | PRESSURE | TOOL | DISTANCE | TILT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006116 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006117
6118 // These calculations are based on the input device calibration documentation.
6119 int32_t rawX = 100;
6120 int32_t rawY = 200;
6121 int32_t rawPressure = 10;
6122 int32_t rawToolMajor = 12;
6123 int32_t rawDistance = 2;
6124 int32_t rawTiltX = 30;
6125 int32_t rawTiltY = 110;
6126
6127 float x = toDisplayX(rawX);
6128 float y = toDisplayY(rawY);
6129 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
6130 float size = float(rawToolMajor) / RAW_TOOL_MAX;
6131 float tool = float(rawToolMajor) * GEOMETRIC_SCALE;
6132 float distance = float(rawDistance);
6133
6134 float tiltCenter = (RAW_TILT_MAX + RAW_TILT_MIN) * 0.5f;
6135 float tiltScale = M_PI / 180;
6136 float tiltXAngle = (rawTiltX - tiltCenter) * tiltScale;
6137 float tiltYAngle = (rawTiltY - tiltCenter) * tiltScale;
6138 float orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
6139 float tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
6140
6141 processDown(mapper, rawX, rawY);
6142 processPressure(mapper, rawPressure);
6143 processToolMajor(mapper, rawToolMajor);
6144 processDistance(mapper, rawDistance);
6145 processTilt(mapper, rawTiltX, rawTiltY);
6146 processSync(mapper);
6147
6148 NotifyMotionArgs args;
6149 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6150 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6151 x, y, pressure, size, tool, tool, tool, tool, orientation, distance));
6152 ASSERT_EQ(tilt, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TILT));
6153}
6154
Jason Gerecke489fda82012-09-07 17:19:40 -07006155TEST_F(SingleTouchInputMapperTest, Process_XYAxes_AffineCalibration) {
Jason Gerecke489fda82012-09-07 17:19:40 -07006156 addConfigurationProperty("touch.deviceType", "touchScreen");
6157 prepareDisplay(DISPLAY_ORIENTATION_0);
6158 prepareLocationCalibration();
6159 prepareButtons();
6160 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006161 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Jason Gerecke489fda82012-09-07 17:19:40 -07006162
6163 int32_t rawX = 100;
6164 int32_t rawY = 200;
6165
6166 float x = toDisplayX(toCookedX(rawX, rawY));
6167 float y = toDisplayY(toCookedY(rawX, rawY));
6168
6169 processDown(mapper, rawX, rawY);
6170 processSync(mapper);
6171
6172 NotifyMotionArgs args;
6173 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6174 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6175 x, y, 1, 0, 0, 0, 0, 0, 0, 0));
6176}
6177
Michael Wrightd02c5b62014-02-10 15:10:22 -08006178TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006179 addConfigurationProperty("touch.deviceType", "touchScreen");
6180 prepareDisplay(DISPLAY_ORIENTATION_0);
6181 prepareButtons();
6182 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006183 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006184
6185 NotifyMotionArgs motionArgs;
6186 NotifyKeyArgs keyArgs;
6187
6188 processDown(mapper, 100, 200);
6189 processSync(mapper);
6190 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6191 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6192 ASSERT_EQ(0, motionArgs.buttonState);
6193
6194 // press BTN_LEFT, release BTN_LEFT
6195 processKey(mapper, BTN_LEFT, 1);
6196 processSync(mapper);
6197 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6198 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6199 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6200
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006201 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6202 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6203 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6204
Michael Wrightd02c5b62014-02-10 15:10:22 -08006205 processKey(mapper, BTN_LEFT, 0);
6206 processSync(mapper);
6207 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006208 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006209 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006210
6211 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006212 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006213 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006214
6215 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
6216 processKey(mapper, BTN_RIGHT, 1);
6217 processKey(mapper, BTN_MIDDLE, 1);
6218 processSync(mapper);
6219 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6220 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6221 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6222 motionArgs.buttonState);
6223
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006224 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6225 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6226 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
6227
6228 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6229 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6230 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6231 motionArgs.buttonState);
6232
Michael Wrightd02c5b62014-02-10 15:10:22 -08006233 processKey(mapper, BTN_RIGHT, 0);
6234 processSync(mapper);
6235 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006236 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006237 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006238
6239 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006240 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006241 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006242
6243 processKey(mapper, BTN_MIDDLE, 0);
6244 processSync(mapper);
6245 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006246 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006247 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006248
6249 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006250 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006251 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006252
6253 // press BTN_BACK, release BTN_BACK
6254 processKey(mapper, BTN_BACK, 1);
6255 processSync(mapper);
6256 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6257 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6258 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006259
Michael Wrightd02c5b62014-02-10 15:10:22 -08006260 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006261 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006262 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6263
6264 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6265 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6266 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006267
6268 processKey(mapper, BTN_BACK, 0);
6269 processSync(mapper);
6270 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006271 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006272 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006273
6274 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006275 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006276 ASSERT_EQ(0, motionArgs.buttonState);
6277
Michael Wrightd02c5b62014-02-10 15:10:22 -08006278 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6279 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6280 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6281
6282 // press BTN_SIDE, release BTN_SIDE
6283 processKey(mapper, BTN_SIDE, 1);
6284 processSync(mapper);
6285 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6286 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6287 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006288
Michael Wrightd02c5b62014-02-10 15:10:22 -08006289 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006290 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006291 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6292
6293 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6294 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6295 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006296
6297 processKey(mapper, BTN_SIDE, 0);
6298 processSync(mapper);
6299 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006300 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006301 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006302
6303 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006304 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006305 ASSERT_EQ(0, motionArgs.buttonState);
6306
Michael Wrightd02c5b62014-02-10 15:10:22 -08006307 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6308 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6309 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6310
6311 // press BTN_FORWARD, release BTN_FORWARD
6312 processKey(mapper, BTN_FORWARD, 1);
6313 processSync(mapper);
6314 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6315 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6316 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006317
Michael Wrightd02c5b62014-02-10 15:10:22 -08006318 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006319 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006320 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6321
6322 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6323 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6324 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006325
6326 processKey(mapper, BTN_FORWARD, 0);
6327 processSync(mapper);
6328 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006329 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006330 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006331
6332 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006333 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006334 ASSERT_EQ(0, motionArgs.buttonState);
6335
Michael Wrightd02c5b62014-02-10 15:10:22 -08006336 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6337 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6338 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6339
6340 // press BTN_EXTRA, release BTN_EXTRA
6341 processKey(mapper, BTN_EXTRA, 1);
6342 processSync(mapper);
6343 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6344 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6345 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006346
Michael Wrightd02c5b62014-02-10 15:10:22 -08006347 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006348 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006349 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6350
6351 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6352 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6353 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006354
6355 processKey(mapper, BTN_EXTRA, 0);
6356 processSync(mapper);
6357 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006358 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006359 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006360
6361 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006362 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006363 ASSERT_EQ(0, motionArgs.buttonState);
6364
Michael Wrightd02c5b62014-02-10 15:10:22 -08006365 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6366 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6367 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6368
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006369 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6370
Michael Wrightd02c5b62014-02-10 15:10:22 -08006371 // press BTN_STYLUS, release BTN_STYLUS
6372 processKey(mapper, BTN_STYLUS, 1);
6373 processSync(mapper);
6374 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6375 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006376 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
6377
6378 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6379 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6380 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006381
6382 processKey(mapper, BTN_STYLUS, 0);
6383 processSync(mapper);
6384 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006385 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006386 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006387
6388 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006389 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006390 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006391
6392 // press BTN_STYLUS2, release BTN_STYLUS2
6393 processKey(mapper, BTN_STYLUS2, 1);
6394 processSync(mapper);
6395 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6396 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006397 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
6398
6399 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6400 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6401 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006402
6403 processKey(mapper, BTN_STYLUS2, 0);
6404 processSync(mapper);
6405 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006406 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006407 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006408
6409 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006410 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006411 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006412
6413 // release touch
6414 processUp(mapper);
6415 processSync(mapper);
6416 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6417 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6418 ASSERT_EQ(0, motionArgs.buttonState);
6419}
6420
6421TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006422 addConfigurationProperty("touch.deviceType", "touchScreen");
6423 prepareDisplay(DISPLAY_ORIENTATION_0);
6424 prepareButtons();
6425 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006426 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006427
6428 NotifyMotionArgs motionArgs;
6429
6430 // default tool type is finger
6431 processDown(mapper, 100, 200);
6432 processSync(mapper);
6433 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6434 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6435 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6436
6437 // eraser
6438 processKey(mapper, BTN_TOOL_RUBBER, 1);
6439 processSync(mapper);
6440 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6441 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6442 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6443
6444 // stylus
6445 processKey(mapper, BTN_TOOL_RUBBER, 0);
6446 processKey(mapper, BTN_TOOL_PEN, 1);
6447 processSync(mapper);
6448 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6449 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6450 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6451
6452 // brush
6453 processKey(mapper, BTN_TOOL_PEN, 0);
6454 processKey(mapper, BTN_TOOL_BRUSH, 1);
6455 processSync(mapper);
6456 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6457 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6458 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6459
6460 // pencil
6461 processKey(mapper, BTN_TOOL_BRUSH, 0);
6462 processKey(mapper, BTN_TOOL_PENCIL, 1);
6463 processSync(mapper);
6464 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6465 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6466 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6467
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08006468 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08006469 processKey(mapper, BTN_TOOL_PENCIL, 0);
6470 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
6471 processSync(mapper);
6472 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6473 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6474 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6475
6476 // mouse
6477 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
6478 processKey(mapper, BTN_TOOL_MOUSE, 1);
6479 processSync(mapper);
6480 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6481 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6482 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6483
6484 // lens
6485 processKey(mapper, BTN_TOOL_MOUSE, 0);
6486 processKey(mapper, BTN_TOOL_LENS, 1);
6487 processSync(mapper);
6488 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6489 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6490 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6491
6492 // double-tap
6493 processKey(mapper, BTN_TOOL_LENS, 0);
6494 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
6495 processSync(mapper);
6496 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6497 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6498 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6499
6500 // triple-tap
6501 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
6502 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
6503 processSync(mapper);
6504 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6505 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6506 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6507
6508 // quad-tap
6509 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
6510 processKey(mapper, BTN_TOOL_QUADTAP, 1);
6511 processSync(mapper);
6512 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6513 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6514 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6515
6516 // finger
6517 processKey(mapper, BTN_TOOL_QUADTAP, 0);
6518 processKey(mapper, BTN_TOOL_FINGER, 1);
6519 processSync(mapper);
6520 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6521 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6522 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6523
6524 // stylus trumps finger
6525 processKey(mapper, BTN_TOOL_PEN, 1);
6526 processSync(mapper);
6527 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6528 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6529 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6530
6531 // eraser trumps stylus
6532 processKey(mapper, BTN_TOOL_RUBBER, 1);
6533 processSync(mapper);
6534 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6535 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6536 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6537
6538 // mouse trumps eraser
6539 processKey(mapper, BTN_TOOL_MOUSE, 1);
6540 processSync(mapper);
6541 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6542 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6543 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6544
6545 // back to default tool type
6546 processKey(mapper, BTN_TOOL_MOUSE, 0);
6547 processKey(mapper, BTN_TOOL_RUBBER, 0);
6548 processKey(mapper, BTN_TOOL_PEN, 0);
6549 processKey(mapper, BTN_TOOL_FINGER, 0);
6550 processSync(mapper);
6551 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6552 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6553 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6554}
6555
6556TEST_F(SingleTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006557 addConfigurationProperty("touch.deviceType", "touchScreen");
6558 prepareDisplay(DISPLAY_ORIENTATION_0);
6559 prepareButtons();
6560 prepareAxes(POSITION);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006561 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_FINGER, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006562 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006563
6564 NotifyMotionArgs motionArgs;
6565
6566 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
6567 processKey(mapper, BTN_TOOL_FINGER, 1);
6568 processMove(mapper, 100, 200);
6569 processSync(mapper);
6570 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6571 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6572 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6573 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6574
6575 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6576 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6577 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6578 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6579
6580 // move a little
6581 processMove(mapper, 150, 250);
6582 processSync(mapper);
6583 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6584 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6585 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6586 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6587
6588 // down when BTN_TOUCH is pressed, pressure defaults to 1
6589 processKey(mapper, BTN_TOUCH, 1);
6590 processSync(mapper);
6591 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6592 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6593 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6594 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6595
6596 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6597 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6598 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6599 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6600
6601 // up when BTN_TOUCH is released, hover restored
6602 processKey(mapper, BTN_TOUCH, 0);
6603 processSync(mapper);
6604 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6605 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6606 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6607 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6608
6609 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6610 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6611 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6612 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6613
6614 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6615 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6616 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6617 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6618
6619 // exit hover when pointer goes away
6620 processKey(mapper, BTN_TOOL_FINGER, 0);
6621 processSync(mapper);
6622 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6623 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6624 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6625 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6626}
6627
6628TEST_F(SingleTouchInputMapperTest, Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006629 addConfigurationProperty("touch.deviceType", "touchScreen");
6630 prepareDisplay(DISPLAY_ORIENTATION_0);
6631 prepareButtons();
6632 prepareAxes(POSITION | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006633 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006634
6635 NotifyMotionArgs motionArgs;
6636
6637 // initially hovering because pressure is 0
6638 processDown(mapper, 100, 200);
6639 processPressure(mapper, 0);
6640 processSync(mapper);
6641 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6642 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6643 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6644 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6645
6646 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6647 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6648 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6649 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6650
6651 // move a little
6652 processMove(mapper, 150, 250);
6653 processSync(mapper);
6654 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6655 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6656 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6657 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6658
6659 // down when pressure is non-zero
6660 processPressure(mapper, RAW_PRESSURE_MAX);
6661 processSync(mapper);
6662 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6663 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6664 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6665 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6666
6667 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6668 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6669 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6670 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6671
6672 // up when pressure becomes 0, hover restored
6673 processPressure(mapper, 0);
6674 processSync(mapper);
6675 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6676 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6677 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6678 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6679
6680 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6681 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6682 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6683 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6684
6685 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6686 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6687 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6688 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6689
6690 // exit hover when pointer goes away
6691 processUp(mapper);
6692 processSync(mapper);
6693 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6694 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6695 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6696 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6697}
6698
lilinnane74b35f2022-07-19 16:00:50 +08006699TEST_F(SingleTouchInputMapperTest,
6700 Process_WhenViewportDisplayIdChanged_TouchIsCanceledAndDeviceIsReset) {
6701 addConfigurationProperty("touch.deviceType", "touchScreen");
6702 prepareDisplay(DISPLAY_ORIENTATION_0);
6703 prepareButtons();
6704 prepareAxes(POSITION);
6705 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6706 NotifyMotionArgs motionArgs;
6707
6708 // Down.
6709 processDown(mapper, 100, 200);
6710 processSync(mapper);
6711
6712 // We should receive a down event
6713 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6714 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6715
6716 // Change display id
6717 clearViewports();
6718 prepareSecondaryDisplay(ViewportType::INTERNAL);
6719
6720 // We should receive a cancel event
6721 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6722 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
6723 // Then receive reset called
6724 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6725}
6726
Prabir Pradhan5632d622021-09-06 07:57:20 -07006727// --- TouchDisplayProjectionTest ---
6728
6729class TouchDisplayProjectionTest : public SingleTouchInputMapperTest {
6730public:
6731 // The values inside DisplayViewport are expected to be pre-rotated. This updates the current
6732 // DisplayViewport to pre-rotate the values. The viewport's physical display will be set to the
6733 // rotated equivalent of the given un-rotated physical display bounds.
6734 void configurePhysicalDisplay(int32_t orientation, Rect naturalPhysicalDisplay) {
6735 uint32_t inverseRotationFlags;
6736 auto width = DISPLAY_WIDTH;
6737 auto height = DISPLAY_HEIGHT;
6738 switch (orientation) {
6739 case DISPLAY_ORIENTATION_90:
6740 inverseRotationFlags = ui::Transform::ROT_270;
6741 std::swap(width, height);
6742 break;
6743 case DISPLAY_ORIENTATION_180:
6744 inverseRotationFlags = ui::Transform::ROT_180;
6745 break;
6746 case DISPLAY_ORIENTATION_270:
6747 inverseRotationFlags = ui::Transform::ROT_90;
6748 std::swap(width, height);
6749 break;
6750 case DISPLAY_ORIENTATION_0:
6751 inverseRotationFlags = ui::Transform::ROT_0;
6752 break;
6753 default:
6754 FAIL() << "Invalid orientation: " << orientation;
6755 }
6756
6757 const ui::Transform rotation(inverseRotationFlags, width, height);
6758 const Rect rotatedPhysicalDisplay = rotation.transform(naturalPhysicalDisplay);
6759
6760 std::optional<DisplayViewport> internalViewport =
6761 *mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6762 DisplayViewport& v = *internalViewport;
6763 v.displayId = DISPLAY_ID;
6764 v.orientation = orientation;
6765
6766 v.logicalLeft = 0;
6767 v.logicalTop = 0;
6768 v.logicalRight = 100;
6769 v.logicalBottom = 100;
6770
6771 v.physicalLeft = rotatedPhysicalDisplay.left;
6772 v.physicalTop = rotatedPhysicalDisplay.top;
6773 v.physicalRight = rotatedPhysicalDisplay.right;
6774 v.physicalBottom = rotatedPhysicalDisplay.bottom;
6775
6776 v.deviceWidth = width;
6777 v.deviceHeight = height;
6778
6779 v.isActive = true;
6780 v.uniqueId = UNIQUE_ID;
6781 v.type = ViewportType::INTERNAL;
6782 mFakePolicy->updateViewport(v);
6783 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
6784 }
6785
6786 void assertReceivedMove(const Point& point) {
6787 NotifyMotionArgs motionArgs;
6788 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6789 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6790 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6791 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], point.x, point.y,
6792 1, 0, 0, 0, 0, 0, 0, 0));
6793 }
6794};
6795
6796TEST_F(TouchDisplayProjectionTest, IgnoresTouchesOutsidePhysicalDisplay) {
6797 addConfigurationProperty("touch.deviceType", "touchScreen");
6798 prepareDisplay(DISPLAY_ORIENTATION_0);
6799
6800 prepareButtons();
6801 prepareAxes(POSITION);
6802 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6803
6804 NotifyMotionArgs motionArgs;
6805
6806 // Configure the DisplayViewport such that the logical display maps to a subsection of
6807 // the display panel called the physical display. Here, the physical display is bounded by the
6808 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
6809 static const Rect kPhysicalDisplay{10, 20, 70, 160};
6810 static const std::array<Point, 6> kPointsOutsidePhysicalDisplay{
6811 {{-10, -10}, {0, 0}, {5, 100}, {50, 15}, {75, 100}, {50, 165}}};
6812
6813 for (auto orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90, DISPLAY_ORIENTATION_180,
6814 DISPLAY_ORIENTATION_270}) {
6815 configurePhysicalDisplay(orientation, kPhysicalDisplay);
6816
6817 // Touches outside the physical display should be ignored, and should not generate any
6818 // events. Ensure touches at the following points that lie outside of the physical display
6819 // area do not generate any events.
6820 for (const auto& point : kPointsOutsidePhysicalDisplay) {
6821 processDown(mapper, toRawX(point.x), toRawY(point.y));
6822 processSync(mapper);
6823 processUp(mapper);
6824 processSync(mapper);
6825 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled())
6826 << "Unexpected event generated for touch outside physical display at point: "
6827 << point.x << ", " << point.y;
6828 }
6829 }
6830}
6831
6832TEST_F(TouchDisplayProjectionTest, EmitsTouchDownAfterEnteringPhysicalDisplay) {
6833 addConfigurationProperty("touch.deviceType", "touchScreen");
6834 prepareDisplay(DISPLAY_ORIENTATION_0);
6835
6836 prepareButtons();
6837 prepareAxes(POSITION);
6838 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6839
6840 NotifyMotionArgs motionArgs;
6841
6842 // Configure the DisplayViewport such that the logical display maps to a subsection of
6843 // the display panel called the physical display. Here, the physical display is bounded by the
6844 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
6845 static const Rect kPhysicalDisplay{10, 20, 70, 160};
6846
6847 for (auto orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90, DISPLAY_ORIENTATION_180,
6848 DISPLAY_ORIENTATION_270}) {
6849 configurePhysicalDisplay(orientation, kPhysicalDisplay);
6850
6851 // Touches that start outside the physical display should be ignored until it enters the
6852 // physical display bounds, at which point it should generate a down event. Start a touch at
6853 // the point (5, 100), which is outside the physical display bounds.
6854 static const Point kOutsidePoint{5, 100};
6855 processDown(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
6856 processSync(mapper);
6857 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6858
6859 // Move the touch into the physical display area. This should generate a pointer down.
6860 processMove(mapper, toRawX(11), toRawY(21));
6861 processSync(mapper);
6862 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6863 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6864 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6865 ASSERT_NO_FATAL_FAILURE(
6866 assertPointerCoords(motionArgs.pointerCoords[0], 11, 21, 1, 0, 0, 0, 0, 0, 0, 0));
6867
6868 // Move the touch inside the physical display area. This should generate a pointer move.
6869 processMove(mapper, toRawX(69), toRawY(159));
6870 processSync(mapper);
6871 assertReceivedMove({69, 159});
6872
6873 // Move outside the physical display area. Since the pointer is already down, this should
6874 // now continue generating events.
6875 processMove(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
6876 processSync(mapper);
6877 assertReceivedMove(kOutsidePoint);
6878
6879 // Release. This should generate a pointer up.
6880 processUp(mapper);
6881 processSync(mapper);
6882 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6883 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6884 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], kOutsidePoint.x,
6885 kOutsidePoint.y, 1, 0, 0, 0, 0, 0, 0, 0));
6886
6887 // Ensure no more events were generated.
6888 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6889 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6890 }
6891}
6892
Michael Wrightd02c5b62014-02-10 15:10:22 -08006893// --- MultiTouchInputMapperTest ---
6894
6895class MultiTouchInputMapperTest : public TouchInputMapperTest {
6896protected:
6897 void prepareAxes(int axes);
6898
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006899 void processPosition(MultiTouchInputMapper& mapper, int32_t x, int32_t y);
6900 void processTouchMajor(MultiTouchInputMapper& mapper, int32_t touchMajor);
6901 void processTouchMinor(MultiTouchInputMapper& mapper, int32_t touchMinor);
6902 void processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor);
6903 void processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor);
6904 void processOrientation(MultiTouchInputMapper& mapper, int32_t orientation);
6905 void processPressure(MultiTouchInputMapper& mapper, int32_t pressure);
6906 void processDistance(MultiTouchInputMapper& mapper, int32_t distance);
6907 void processId(MultiTouchInputMapper& mapper, int32_t id);
6908 void processSlot(MultiTouchInputMapper& mapper, int32_t slot);
6909 void processToolType(MultiTouchInputMapper& mapper, int32_t toolType);
6910 void processKey(MultiTouchInputMapper& mapper, int32_t code, int32_t value);
6911 void processMTSync(MultiTouchInputMapper& mapper);
6912 void processSync(MultiTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006913};
6914
6915void MultiTouchInputMapperTest::prepareAxes(int axes) {
6916 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006917 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
6918 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006919 }
6920 if (axes & TOUCH) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006921 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN,
6922 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006923 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006924 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN,
6925 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006926 }
6927 }
6928 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006929 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
6930 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006931 if (axes & MINOR) {
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08006932 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006933 RAW_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006934 }
6935 }
6936 if (axes & ORIENTATION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006937 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_ORIENTATION, RAW_ORIENTATION_MIN,
6938 RAW_ORIENTATION_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006939 }
6940 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006941 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_PRESSURE, RAW_PRESSURE_MIN,
6942 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006943 }
6944 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006945 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_DISTANCE, RAW_DISTANCE_MIN,
6946 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006947 }
6948 if (axes & ID) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006949 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX, 0,
6950 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006951 }
6952 if (axes & SLOT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006953 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
6954 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_MT_SLOT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006955 }
6956 if (axes & TOOL_TYPE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006957 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOOL_TYPE, 0, MT_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006958 }
6959}
6960
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006961void MultiTouchInputMapperTest::processPosition(MultiTouchInputMapper& mapper, int32_t x,
6962 int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006963 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_X, x);
6964 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006965}
6966
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006967void MultiTouchInputMapperTest::processTouchMajor(MultiTouchInputMapper& mapper,
6968 int32_t touchMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006969 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006970}
6971
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006972void MultiTouchInputMapperTest::processTouchMinor(MultiTouchInputMapper& mapper,
6973 int32_t touchMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006974 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006975}
6976
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006977void MultiTouchInputMapperTest::processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006978 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006979}
6980
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006981void MultiTouchInputMapperTest::processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006982 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006983}
6984
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006985void MultiTouchInputMapperTest::processOrientation(MultiTouchInputMapper& mapper,
6986 int32_t orientation) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006987 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_ORIENTATION, orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006988}
6989
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006990void MultiTouchInputMapperTest::processPressure(MultiTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006991 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006992}
6993
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006994void MultiTouchInputMapperTest::processDistance(MultiTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006995 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006996}
6997
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006998void MultiTouchInputMapperTest::processId(MultiTouchInputMapper& mapper, int32_t id) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006999 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TRACKING_ID, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007000}
7001
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007002void MultiTouchInputMapperTest::processSlot(MultiTouchInputMapper& mapper, int32_t slot) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007003 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_SLOT, slot);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007004}
7005
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007006void MultiTouchInputMapperTest::processToolType(MultiTouchInputMapper& mapper, int32_t toolType) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007007 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007008}
7009
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007010void MultiTouchInputMapperTest::processKey(MultiTouchInputMapper& mapper, int32_t code,
7011 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007012 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007013}
7014
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007015void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007016 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_MT_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007017}
7018
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007019void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007020 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007021}
7022
Michael Wrightd02c5b62014-02-10 15:10:22 -08007023TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007024 addConfigurationProperty("touch.deviceType", "touchScreen");
7025 prepareDisplay(DISPLAY_ORIENTATION_0);
7026 prepareAxes(POSITION);
7027 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007028 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007029
arthurhungdcef2dc2020-08-11 14:47:50 +08007030 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007031
7032 NotifyMotionArgs motionArgs;
7033
7034 // Two fingers down at once.
7035 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7036 processPosition(mapper, x1, y1);
7037 processMTSync(mapper);
7038 processPosition(mapper, x2, y2);
7039 processMTSync(mapper);
7040 processSync(mapper);
7041
7042 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7043 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7044 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7045 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7046 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7047 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7048 ASSERT_EQ(0, motionArgs.flags);
7049 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7050 ASSERT_EQ(0, motionArgs.buttonState);
7051 ASSERT_EQ(0, motionArgs.edgeFlags);
7052 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7053 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7054 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7055 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7056 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7057 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7058 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7059 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7060
7061 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7062 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7063 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7064 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7065 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007066 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007067 ASSERT_EQ(0, motionArgs.flags);
7068 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7069 ASSERT_EQ(0, motionArgs.buttonState);
7070 ASSERT_EQ(0, motionArgs.edgeFlags);
7071 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7072 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7073 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7074 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7075 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7076 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7077 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7078 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7079 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7080 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7081 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7082 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7083
7084 // Move.
7085 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7086 processPosition(mapper, x1, y1);
7087 processMTSync(mapper);
7088 processPosition(mapper, x2, y2);
7089 processMTSync(mapper);
7090 processSync(mapper);
7091
7092 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7093 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7094 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7095 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7096 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7097 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7098 ASSERT_EQ(0, motionArgs.flags);
7099 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7100 ASSERT_EQ(0, motionArgs.buttonState);
7101 ASSERT_EQ(0, motionArgs.edgeFlags);
7102 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7103 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7104 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7105 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7106 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7107 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7108 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7109 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7110 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7111 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7112 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7113 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7114
7115 // First finger up.
7116 x2 += 15; y2 -= 20;
7117 processPosition(mapper, x2, y2);
7118 processMTSync(mapper);
7119 processSync(mapper);
7120
7121 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7122 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7123 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7124 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7125 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007126 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007127 ASSERT_EQ(0, motionArgs.flags);
7128 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7129 ASSERT_EQ(0, motionArgs.buttonState);
7130 ASSERT_EQ(0, motionArgs.edgeFlags);
7131 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7132 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7133 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7134 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7135 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7136 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7137 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7138 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7139 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7140 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7141 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7142 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7143
7144 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7145 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7146 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7147 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7148 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7149 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7150 ASSERT_EQ(0, motionArgs.flags);
7151 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7152 ASSERT_EQ(0, motionArgs.buttonState);
7153 ASSERT_EQ(0, motionArgs.edgeFlags);
7154 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7155 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7156 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7157 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7158 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7159 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7160 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7161 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7162
7163 // Move.
7164 x2 += 20; y2 -= 25;
7165 processPosition(mapper, x2, y2);
7166 processMTSync(mapper);
7167 processSync(mapper);
7168
7169 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7170 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7171 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7172 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7173 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7174 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7175 ASSERT_EQ(0, motionArgs.flags);
7176 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7177 ASSERT_EQ(0, motionArgs.buttonState);
7178 ASSERT_EQ(0, motionArgs.edgeFlags);
7179 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7180 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7181 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7182 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7183 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7184 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7185 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7186 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7187
7188 // New finger down.
7189 int32_t x3 = 700, y3 = 300;
7190 processPosition(mapper, x2, y2);
7191 processMTSync(mapper);
7192 processPosition(mapper, x3, y3);
7193 processMTSync(mapper);
7194 processSync(mapper);
7195
7196 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7197 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7198 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7199 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7200 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007201 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007202 ASSERT_EQ(0, motionArgs.flags);
7203 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7204 ASSERT_EQ(0, motionArgs.buttonState);
7205 ASSERT_EQ(0, motionArgs.edgeFlags);
7206 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7207 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7208 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7209 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7210 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7211 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7212 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7213 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7214 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7215 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7216 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7217 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7218
7219 // Second finger up.
7220 x3 += 30; y3 -= 20;
7221 processPosition(mapper, x3, y3);
7222 processMTSync(mapper);
7223 processSync(mapper);
7224
7225 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7226 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7227 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7228 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7229 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007230 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007231 ASSERT_EQ(0, motionArgs.flags);
7232 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7233 ASSERT_EQ(0, motionArgs.buttonState);
7234 ASSERT_EQ(0, motionArgs.edgeFlags);
7235 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7236 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7237 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7238 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7239 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7240 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7241 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7242 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7243 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7244 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7245 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7246 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7247
7248 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7249 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7250 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7251 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7252 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7253 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7254 ASSERT_EQ(0, motionArgs.flags);
7255 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7256 ASSERT_EQ(0, motionArgs.buttonState);
7257 ASSERT_EQ(0, motionArgs.edgeFlags);
7258 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7259 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7260 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7261 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7262 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7263 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7264 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7265 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7266
7267 // Last finger up.
7268 processMTSync(mapper);
7269 processSync(mapper);
7270
7271 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7272 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7273 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7274 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7275 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7276 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7277 ASSERT_EQ(0, motionArgs.flags);
7278 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7279 ASSERT_EQ(0, motionArgs.buttonState);
7280 ASSERT_EQ(0, motionArgs.edgeFlags);
7281 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7282 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7283 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7284 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7285 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7286 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7287 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7288 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7289
7290 // Should not have sent any more keys or motions.
7291 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7292 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7293}
7294
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007295TEST_F(MultiTouchInputMapperTest, AxisResolution_IsPopulated) {
7296 addConfigurationProperty("touch.deviceType", "touchScreen");
7297 prepareDisplay(DISPLAY_ORIENTATION_0);
7298
7299 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
7300 /*fuzz*/ 0, /*resolution*/ 10);
7301 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
7302 /*fuzz*/ 0, /*resolution*/ 11);
7303 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
7304 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 12);
7305 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
7306 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 13);
7307 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7308 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 14);
7309 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7310 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 15);
7311
7312 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7313
7314 // X and Y axes
7315 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_X, 10 / X_PRECISION);
7316 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_Y, 11 / Y_PRECISION);
7317 // Touch major and minor
7318 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR, 12 * GEOMETRIC_SCALE);
7319 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR, 13 * GEOMETRIC_SCALE);
7320 // Tool major and minor
7321 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR, 14 * GEOMETRIC_SCALE);
7322 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR, 15 * GEOMETRIC_SCALE);
7323}
7324
7325TEST_F(MultiTouchInputMapperTest, TouchMajorAndMinorAxes_DoNotAppearIfNotSupported) {
7326 addConfigurationProperty("touch.deviceType", "touchScreen");
7327 prepareDisplay(DISPLAY_ORIENTATION_0);
7328
7329 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
7330 /*fuzz*/ 0, /*resolution*/ 10);
7331 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
7332 /*fuzz*/ 0, /*resolution*/ 11);
7333
7334 // We do not add ABS_MT_TOUCH_MAJOR / MINOR or ABS_MT_WIDTH_MAJOR / MINOR axes
7335
7336 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7337
7338 // Touch major and minor
7339 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR);
7340 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR);
7341 // Tool major and minor
7342 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR);
7343 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR);
7344}
7345
Michael Wrightd02c5b62014-02-10 15:10:22 -08007346TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007347 addConfigurationProperty("touch.deviceType", "touchScreen");
7348 prepareDisplay(DISPLAY_ORIENTATION_0);
7349 prepareAxes(POSITION | ID);
7350 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007351 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007352
arthurhungdcef2dc2020-08-11 14:47:50 +08007353 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007354
7355 NotifyMotionArgs motionArgs;
7356
7357 // Two fingers down at once.
7358 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7359 processPosition(mapper, x1, y1);
7360 processId(mapper, 1);
7361 processMTSync(mapper);
7362 processPosition(mapper, x2, y2);
7363 processId(mapper, 2);
7364 processMTSync(mapper);
7365 processSync(mapper);
7366
7367 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7368 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7369 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7370 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7371 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7372 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7373 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7374
7375 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007376 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007377 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7378 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7379 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7380 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7381 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7382 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7383 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7384 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7385 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7386
7387 // Move.
7388 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7389 processPosition(mapper, x1, y1);
7390 processId(mapper, 1);
7391 processMTSync(mapper);
7392 processPosition(mapper, x2, y2);
7393 processId(mapper, 2);
7394 processMTSync(mapper);
7395 processSync(mapper);
7396
7397 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7398 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7399 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7400 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7401 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7402 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7403 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7404 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7405 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7406 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7407 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7408
7409 // First finger up.
7410 x2 += 15; y2 -= 20;
7411 processPosition(mapper, x2, y2);
7412 processId(mapper, 2);
7413 processMTSync(mapper);
7414 processSync(mapper);
7415
7416 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007417 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007418 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7419 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7420 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7421 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7422 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7423 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7424 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7425 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7426 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7427
7428 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7429 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7430 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7431 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7432 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7433 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7434 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7435
7436 // Move.
7437 x2 += 20; y2 -= 25;
7438 processPosition(mapper, x2, y2);
7439 processId(mapper, 2);
7440 processMTSync(mapper);
7441 processSync(mapper);
7442
7443 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7444 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7445 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7446 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7447 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7448 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7449 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7450
7451 // New finger down.
7452 int32_t x3 = 700, y3 = 300;
7453 processPosition(mapper, x2, y2);
7454 processId(mapper, 2);
7455 processMTSync(mapper);
7456 processPosition(mapper, x3, y3);
7457 processId(mapper, 3);
7458 processMTSync(mapper);
7459 processSync(mapper);
7460
7461 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007462 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007463 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7464 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7465 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7466 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7467 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7468 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7469 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7470 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7471 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7472
7473 // Second finger up.
7474 x3 += 30; y3 -= 20;
7475 processPosition(mapper, x3, y3);
7476 processId(mapper, 3);
7477 processMTSync(mapper);
7478 processSync(mapper);
7479
7480 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007481 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007482 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7483 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7484 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7485 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7486 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7487 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7488 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7489 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7490 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7491
7492 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7493 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7494 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7495 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7496 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7497 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7498 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7499
7500 // Last finger up.
7501 processMTSync(mapper);
7502 processSync(mapper);
7503
7504 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7505 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7506 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7507 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7508 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7509 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7510 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7511
7512 // Should not have sent any more keys or motions.
7513 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7514 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7515}
7516
7517TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007518 addConfigurationProperty("touch.deviceType", "touchScreen");
7519 prepareDisplay(DISPLAY_ORIENTATION_0);
7520 prepareAxes(POSITION | ID | SLOT);
7521 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007522 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007523
arthurhungdcef2dc2020-08-11 14:47:50 +08007524 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007525
7526 NotifyMotionArgs motionArgs;
7527
7528 // Two fingers down at once.
7529 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7530 processPosition(mapper, x1, y1);
7531 processId(mapper, 1);
7532 processSlot(mapper, 1);
7533 processPosition(mapper, x2, y2);
7534 processId(mapper, 2);
7535 processSync(mapper);
7536
7537 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7538 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7539 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7540 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7541 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7542 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7543 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7544
7545 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007546 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007547 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7548 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7549 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7550 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7551 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7552 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7553 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7554 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7555 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7556
7557 // Move.
7558 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7559 processSlot(mapper, 0);
7560 processPosition(mapper, x1, y1);
7561 processSlot(mapper, 1);
7562 processPosition(mapper, x2, y2);
7563 processSync(mapper);
7564
7565 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7566 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7567 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7568 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7569 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7570 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7571 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7572 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7573 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7574 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7575 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7576
7577 // First finger up.
7578 x2 += 15; y2 -= 20;
7579 processSlot(mapper, 0);
7580 processId(mapper, -1);
7581 processSlot(mapper, 1);
7582 processPosition(mapper, x2, y2);
7583 processSync(mapper);
7584
7585 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007586 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007587 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7588 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7589 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7590 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7591 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7592 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7593 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7594 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7595 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7596
7597 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7598 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7599 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7600 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7601 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7602 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7603 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7604
7605 // Move.
7606 x2 += 20; y2 -= 25;
7607 processPosition(mapper, x2, y2);
7608 processSync(mapper);
7609
7610 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7611 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7612 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7613 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7614 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7615 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7616 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7617
7618 // New finger down.
7619 int32_t x3 = 700, y3 = 300;
7620 processPosition(mapper, x2, y2);
7621 processSlot(mapper, 0);
7622 processId(mapper, 3);
7623 processPosition(mapper, x3, y3);
7624 processSync(mapper);
7625
7626 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007627 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007628 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7629 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7630 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7631 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7632 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7633 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7634 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7635 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7636 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7637
7638 // Second finger up.
7639 x3 += 30; y3 -= 20;
7640 processSlot(mapper, 1);
7641 processId(mapper, -1);
7642 processSlot(mapper, 0);
7643 processPosition(mapper, x3, y3);
7644 processSync(mapper);
7645
7646 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007647 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007648 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7649 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7650 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7651 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7652 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7653 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7654 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7655 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7656 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7657
7658 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7659 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7660 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7661 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7662 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7663 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7664 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7665
7666 // Last finger up.
7667 processId(mapper, -1);
7668 processSync(mapper);
7669
7670 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7671 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7672 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7673 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7674 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7675 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7676 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7677
7678 // Should not have sent any more keys or motions.
7679 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7680 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7681}
7682
7683TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007684 addConfigurationProperty("touch.deviceType", "touchScreen");
7685 prepareDisplay(DISPLAY_ORIENTATION_0);
7686 prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007687 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007688
7689 // These calculations are based on the input device calibration documentation.
7690 int32_t rawX = 100;
7691 int32_t rawY = 200;
7692 int32_t rawTouchMajor = 7;
7693 int32_t rawTouchMinor = 6;
7694 int32_t rawToolMajor = 9;
7695 int32_t rawToolMinor = 8;
7696 int32_t rawPressure = 11;
7697 int32_t rawDistance = 0;
7698 int32_t rawOrientation = 3;
7699 int32_t id = 5;
7700
7701 float x = toDisplayX(rawX);
7702 float y = toDisplayY(rawY);
7703 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
7704 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
7705 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
7706 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
7707 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
7708 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
7709 float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
7710 float distance = float(rawDistance);
7711
7712 processPosition(mapper, rawX, rawY);
7713 processTouchMajor(mapper, rawTouchMajor);
7714 processTouchMinor(mapper, rawTouchMinor);
7715 processToolMajor(mapper, rawToolMajor);
7716 processToolMinor(mapper, rawToolMinor);
7717 processPressure(mapper, rawPressure);
7718 processOrientation(mapper, rawOrientation);
7719 processDistance(mapper, rawDistance);
7720 processId(mapper, id);
7721 processMTSync(mapper);
7722 processSync(mapper);
7723
7724 NotifyMotionArgs args;
7725 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7726 ASSERT_EQ(0, args.pointerProperties[0].id);
7727 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7728 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
7729 orientation, distance));
7730}
7731
7732TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007733 addConfigurationProperty("touch.deviceType", "touchScreen");
7734 prepareDisplay(DISPLAY_ORIENTATION_0);
7735 prepareAxes(POSITION | TOUCH | TOOL | MINOR);
7736 addConfigurationProperty("touch.size.calibration", "geometric");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007737 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007738
7739 // These calculations are based on the input device calibration documentation.
7740 int32_t rawX = 100;
7741 int32_t rawY = 200;
7742 int32_t rawTouchMajor = 140;
7743 int32_t rawTouchMinor = 120;
7744 int32_t rawToolMajor = 180;
7745 int32_t rawToolMinor = 160;
7746
7747 float x = toDisplayX(rawX);
7748 float y = toDisplayY(rawY);
7749 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
7750 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
7751 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
7752 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
7753 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
7754
7755 processPosition(mapper, rawX, rawY);
7756 processTouchMajor(mapper, rawTouchMajor);
7757 processTouchMinor(mapper, rawTouchMinor);
7758 processToolMajor(mapper, rawToolMajor);
7759 processToolMinor(mapper, rawToolMinor);
7760 processMTSync(mapper);
7761 processSync(mapper);
7762
7763 NotifyMotionArgs args;
7764 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7765 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7766 x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
7767}
7768
7769TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007770 addConfigurationProperty("touch.deviceType", "touchScreen");
7771 prepareDisplay(DISPLAY_ORIENTATION_0);
7772 prepareAxes(POSITION | TOUCH | TOOL);
7773 addConfigurationProperty("touch.size.calibration", "diameter");
7774 addConfigurationProperty("touch.size.scale", "10");
7775 addConfigurationProperty("touch.size.bias", "160");
7776 addConfigurationProperty("touch.size.isSummed", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007777 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007778
7779 // These calculations are based on the input device calibration documentation.
7780 // Note: We only provide a single common touch/tool value because the device is assumed
7781 // not to emit separate values for each pointer (isSummed = 1).
7782 int32_t rawX = 100;
7783 int32_t rawY = 200;
7784 int32_t rawX2 = 150;
7785 int32_t rawY2 = 250;
7786 int32_t rawTouchMajor = 5;
7787 int32_t rawToolMajor = 8;
7788
7789 float x = toDisplayX(rawX);
7790 float y = toDisplayY(rawY);
7791 float x2 = toDisplayX(rawX2);
7792 float y2 = toDisplayY(rawY2);
7793 float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
7794 float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
7795 float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
7796
7797 processPosition(mapper, rawX, rawY);
7798 processTouchMajor(mapper, rawTouchMajor);
7799 processToolMajor(mapper, rawToolMajor);
7800 processMTSync(mapper);
7801 processPosition(mapper, rawX2, rawY2);
7802 processTouchMajor(mapper, rawTouchMajor);
7803 processToolMajor(mapper, rawToolMajor);
7804 processMTSync(mapper);
7805 processSync(mapper);
7806
7807 NotifyMotionArgs args;
7808 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7809 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
7810
7811 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007812 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007813 ASSERT_EQ(size_t(2), args.pointerCount);
7814 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7815 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
7816 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
7817 x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
7818}
7819
7820TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007821 addConfigurationProperty("touch.deviceType", "touchScreen");
7822 prepareDisplay(DISPLAY_ORIENTATION_0);
7823 prepareAxes(POSITION | TOUCH | TOOL);
7824 addConfigurationProperty("touch.size.calibration", "area");
7825 addConfigurationProperty("touch.size.scale", "43");
7826 addConfigurationProperty("touch.size.bias", "3");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007827 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007828
7829 // These calculations are based on the input device calibration documentation.
7830 int32_t rawX = 100;
7831 int32_t rawY = 200;
7832 int32_t rawTouchMajor = 5;
7833 int32_t rawToolMajor = 8;
7834
7835 float x = toDisplayX(rawX);
7836 float y = toDisplayY(rawY);
7837 float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
7838 float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
7839 float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
7840
7841 processPosition(mapper, rawX, rawY);
7842 processTouchMajor(mapper, rawTouchMajor);
7843 processToolMajor(mapper, rawToolMajor);
7844 processMTSync(mapper);
7845 processSync(mapper);
7846
7847 NotifyMotionArgs args;
7848 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7849 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7850 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
7851}
7852
7853TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007854 addConfigurationProperty("touch.deviceType", "touchScreen");
7855 prepareDisplay(DISPLAY_ORIENTATION_0);
7856 prepareAxes(POSITION | PRESSURE);
7857 addConfigurationProperty("touch.pressure.calibration", "amplitude");
7858 addConfigurationProperty("touch.pressure.scale", "0.01");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007859 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007860
Michael Wrightaa449c92017-12-13 21:21:43 +00007861 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007862 mapper.populateDeviceInfo(&info);
Michael Wrightaa449c92017-12-13 21:21:43 +00007863 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
7864 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TOUCHSCREEN,
7865 0.0f, RAW_PRESSURE_MAX * 0.01, 0.0f, 0.0f));
7866
Michael Wrightd02c5b62014-02-10 15:10:22 -08007867 // These calculations are based on the input device calibration documentation.
7868 int32_t rawX = 100;
7869 int32_t rawY = 200;
7870 int32_t rawPressure = 60;
7871
7872 float x = toDisplayX(rawX);
7873 float y = toDisplayY(rawY);
7874 float pressure = float(rawPressure) * 0.01f;
7875
7876 processPosition(mapper, rawX, rawY);
7877 processPressure(mapper, rawPressure);
7878 processMTSync(mapper);
7879 processSync(mapper);
7880
7881 NotifyMotionArgs args;
7882 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7883 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7884 x, y, pressure, 0, 0, 0, 0, 0, 0, 0));
7885}
7886
7887TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007888 addConfigurationProperty("touch.deviceType", "touchScreen");
7889 prepareDisplay(DISPLAY_ORIENTATION_0);
7890 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007891 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007892
7893 NotifyMotionArgs motionArgs;
7894 NotifyKeyArgs keyArgs;
7895
7896 processId(mapper, 1);
7897 processPosition(mapper, 100, 200);
7898 processSync(mapper);
7899 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7900 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7901 ASSERT_EQ(0, motionArgs.buttonState);
7902
7903 // press BTN_LEFT, release BTN_LEFT
7904 processKey(mapper, BTN_LEFT, 1);
7905 processSync(mapper);
7906 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7907 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7908 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
7909
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007910 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7911 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7912 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
7913
Michael Wrightd02c5b62014-02-10 15:10:22 -08007914 processKey(mapper, BTN_LEFT, 0);
7915 processSync(mapper);
7916 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007917 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007918 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007919
7920 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007921 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007922 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007923
7924 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
7925 processKey(mapper, BTN_RIGHT, 1);
7926 processKey(mapper, BTN_MIDDLE, 1);
7927 processSync(mapper);
7928 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7929 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7930 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
7931 motionArgs.buttonState);
7932
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007933 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7934 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7935 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
7936
7937 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7938 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7939 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
7940 motionArgs.buttonState);
7941
Michael Wrightd02c5b62014-02-10 15:10:22 -08007942 processKey(mapper, BTN_RIGHT, 0);
7943 processSync(mapper);
7944 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007945 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007946 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007947
7948 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007949 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007950 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007951
7952 processKey(mapper, BTN_MIDDLE, 0);
7953 processSync(mapper);
7954 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007955 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007956 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007957
7958 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007959 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007960 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007961
7962 // press BTN_BACK, release BTN_BACK
7963 processKey(mapper, BTN_BACK, 1);
7964 processSync(mapper);
7965 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7966 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
7967 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007968
Michael Wrightd02c5b62014-02-10 15:10:22 -08007969 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007970 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007971 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
7972
7973 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7974 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7975 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007976
7977 processKey(mapper, BTN_BACK, 0);
7978 processSync(mapper);
7979 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007980 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007981 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007982
7983 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007984 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007985 ASSERT_EQ(0, motionArgs.buttonState);
7986
Michael Wrightd02c5b62014-02-10 15:10:22 -08007987 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7988 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
7989 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
7990
7991 // press BTN_SIDE, release BTN_SIDE
7992 processKey(mapper, BTN_SIDE, 1);
7993 processSync(mapper);
7994 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7995 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
7996 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007997
Michael Wrightd02c5b62014-02-10 15:10:22 -08007998 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007999 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008000 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
8001
8002 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8003 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8004 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008005
8006 processKey(mapper, BTN_SIDE, 0);
8007 processSync(mapper);
8008 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008009 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008010 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008011
8012 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008013 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008014 ASSERT_EQ(0, motionArgs.buttonState);
8015
Michael Wrightd02c5b62014-02-10 15:10:22 -08008016 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8017 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8018 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
8019
8020 // press BTN_FORWARD, release BTN_FORWARD
8021 processKey(mapper, BTN_FORWARD, 1);
8022 processSync(mapper);
8023 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8024 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8025 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008026
Michael Wrightd02c5b62014-02-10 15:10:22 -08008027 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008028 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008029 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
8030
8031 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8032 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8033 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008034
8035 processKey(mapper, BTN_FORWARD, 0);
8036 processSync(mapper);
8037 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008038 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008039 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008040
8041 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008042 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008043 ASSERT_EQ(0, motionArgs.buttonState);
8044
Michael Wrightd02c5b62014-02-10 15:10:22 -08008045 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8046 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8047 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
8048
8049 // press BTN_EXTRA, release BTN_EXTRA
8050 processKey(mapper, BTN_EXTRA, 1);
8051 processSync(mapper);
8052 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8053 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8054 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008055
Michael Wrightd02c5b62014-02-10 15:10:22 -08008056 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008057 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008058 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
8059
8060 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8061 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8062 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008063
8064 processKey(mapper, BTN_EXTRA, 0);
8065 processSync(mapper);
8066 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008067 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008068 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008069
8070 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008071 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008072 ASSERT_EQ(0, motionArgs.buttonState);
8073
Michael Wrightd02c5b62014-02-10 15:10:22 -08008074 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8075 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8076 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
8077
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008078 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8079
Michael Wrightd02c5b62014-02-10 15:10:22 -08008080 // press BTN_STYLUS, release BTN_STYLUS
8081 processKey(mapper, BTN_STYLUS, 1);
8082 processSync(mapper);
8083 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8084 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008085 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
8086
8087 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8088 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8089 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008090
8091 processKey(mapper, BTN_STYLUS, 0);
8092 processSync(mapper);
8093 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008094 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008095 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008096
8097 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008098 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008099 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008100
8101 // press BTN_STYLUS2, release BTN_STYLUS2
8102 processKey(mapper, BTN_STYLUS2, 1);
8103 processSync(mapper);
8104 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8105 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008106 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
8107
8108 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8109 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8110 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008111
8112 processKey(mapper, BTN_STYLUS2, 0);
8113 processSync(mapper);
8114 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008115 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008116 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008117
8118 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008119 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008120 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008121
8122 // release touch
8123 processId(mapper, -1);
8124 processSync(mapper);
8125 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8126 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8127 ASSERT_EQ(0, motionArgs.buttonState);
8128}
8129
8130TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008131 addConfigurationProperty("touch.deviceType", "touchScreen");
8132 prepareDisplay(DISPLAY_ORIENTATION_0);
8133 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008134 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008135
8136 NotifyMotionArgs motionArgs;
8137
8138 // default tool type is finger
8139 processId(mapper, 1);
8140 processPosition(mapper, 100, 200);
8141 processSync(mapper);
8142 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8143 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8144 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8145
8146 // eraser
8147 processKey(mapper, BTN_TOOL_RUBBER, 1);
8148 processSync(mapper);
8149 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8150 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8151 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
8152
8153 // stylus
8154 processKey(mapper, BTN_TOOL_RUBBER, 0);
8155 processKey(mapper, BTN_TOOL_PEN, 1);
8156 processSync(mapper);
8157 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8158 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8159 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8160
8161 // brush
8162 processKey(mapper, BTN_TOOL_PEN, 0);
8163 processKey(mapper, BTN_TOOL_BRUSH, 1);
8164 processSync(mapper);
8165 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8166 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8167 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8168
8169 // pencil
8170 processKey(mapper, BTN_TOOL_BRUSH, 0);
8171 processKey(mapper, BTN_TOOL_PENCIL, 1);
8172 processSync(mapper);
8173 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8174 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8175 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8176
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08008177 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08008178 processKey(mapper, BTN_TOOL_PENCIL, 0);
8179 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
8180 processSync(mapper);
8181 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8182 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8183 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8184
8185 // mouse
8186 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
8187 processKey(mapper, BTN_TOOL_MOUSE, 1);
8188 processSync(mapper);
8189 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8190 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8191 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
8192
8193 // lens
8194 processKey(mapper, BTN_TOOL_MOUSE, 0);
8195 processKey(mapper, BTN_TOOL_LENS, 1);
8196 processSync(mapper);
8197 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8198 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8199 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
8200
8201 // double-tap
8202 processKey(mapper, BTN_TOOL_LENS, 0);
8203 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
8204 processSync(mapper);
8205 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8206 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8207 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8208
8209 // triple-tap
8210 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
8211 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
8212 processSync(mapper);
8213 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8214 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8215 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8216
8217 // quad-tap
8218 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
8219 processKey(mapper, BTN_TOOL_QUADTAP, 1);
8220 processSync(mapper);
8221 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8222 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8223 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8224
8225 // finger
8226 processKey(mapper, BTN_TOOL_QUADTAP, 0);
8227 processKey(mapper, BTN_TOOL_FINGER, 1);
8228 processSync(mapper);
8229 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8230 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8231 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8232
8233 // stylus trumps finger
8234 processKey(mapper, BTN_TOOL_PEN, 1);
8235 processSync(mapper);
8236 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8237 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8238 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8239
8240 // eraser trumps stylus
8241 processKey(mapper, BTN_TOOL_RUBBER, 1);
8242 processSync(mapper);
8243 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8244 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8245 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
8246
8247 // mouse trumps eraser
8248 processKey(mapper, BTN_TOOL_MOUSE, 1);
8249 processSync(mapper);
8250 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8251 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8252 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
8253
8254 // MT tool type trumps BTN tool types: MT_TOOL_FINGER
8255 processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
8256 processSync(mapper);
8257 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8258 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8259 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8260
8261 // MT tool type trumps BTN tool types: MT_TOOL_PEN
8262 processToolType(mapper, MT_TOOL_PEN);
8263 processSync(mapper);
8264 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8265 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8266 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8267
8268 // back to default tool type
8269 processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
8270 processKey(mapper, BTN_TOOL_MOUSE, 0);
8271 processKey(mapper, BTN_TOOL_RUBBER, 0);
8272 processKey(mapper, BTN_TOOL_PEN, 0);
8273 processKey(mapper, BTN_TOOL_FINGER, 0);
8274 processSync(mapper);
8275 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8276 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8277 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8278}
8279
8280TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008281 addConfigurationProperty("touch.deviceType", "touchScreen");
8282 prepareDisplay(DISPLAY_ORIENTATION_0);
8283 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008284 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008285 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008286
8287 NotifyMotionArgs motionArgs;
8288
8289 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
8290 processId(mapper, 1);
8291 processPosition(mapper, 100, 200);
8292 processSync(mapper);
8293 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8294 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8295 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8296 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8297
8298 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8299 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8300 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8301 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8302
8303 // move a little
8304 processPosition(mapper, 150, 250);
8305 processSync(mapper);
8306 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8307 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8308 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8309 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8310
8311 // down when BTN_TOUCH is pressed, pressure defaults to 1
8312 processKey(mapper, BTN_TOUCH, 1);
8313 processSync(mapper);
8314 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8315 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8316 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8317 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8318
8319 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8320 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8321 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8322 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8323
8324 // up when BTN_TOUCH is released, hover restored
8325 processKey(mapper, BTN_TOUCH, 0);
8326 processSync(mapper);
8327 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8328 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8329 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8330 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8331
8332 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8333 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8334 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8335 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8336
8337 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8338 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8339 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8340 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8341
8342 // exit hover when pointer goes away
8343 processId(mapper, -1);
8344 processSync(mapper);
8345 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8346 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8347 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8348 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8349}
8350
8351TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008352 addConfigurationProperty("touch.deviceType", "touchScreen");
8353 prepareDisplay(DISPLAY_ORIENTATION_0);
8354 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008355 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008356
8357 NotifyMotionArgs motionArgs;
8358
8359 // initially hovering because pressure is 0
8360 processId(mapper, 1);
8361 processPosition(mapper, 100, 200);
8362 processPressure(mapper, 0);
8363 processSync(mapper);
8364 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8365 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8366 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8367 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8368
8369 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8370 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8371 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8372 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8373
8374 // move a little
8375 processPosition(mapper, 150, 250);
8376 processSync(mapper);
8377 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8378 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8379 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8380 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8381
8382 // down when pressure becomes non-zero
8383 processPressure(mapper, RAW_PRESSURE_MAX);
8384 processSync(mapper);
8385 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8386 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8387 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8388 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8389
8390 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8391 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8392 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8393 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8394
8395 // up when pressure becomes 0, hover restored
8396 processPressure(mapper, 0);
8397 processSync(mapper);
8398 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8399 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8400 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8401 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8402
8403 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8404 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8405 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8406 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8407
8408 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8409 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8410 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8411 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8412
8413 // exit hover when pointer goes away
8414 processId(mapper, -1);
8415 processSync(mapper);
8416 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8417 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8418 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8419 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8420}
8421
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008422/**
8423 * Set the input device port <--> display port associations, and check that the
8424 * events are routed to the display that matches the display port.
8425 * This can be checked by looking at the displayId of the resulting NotifyMotionArgs.
8426 */
8427TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayPort) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008428 const std::string usb2 = "USB2";
8429 const uint8_t hdmi1 = 0;
8430 const uint8_t hdmi2 = 1;
8431 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008432 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008433
8434 addConfigurationProperty("touch.deviceType", "touchScreen");
8435 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008436 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008437
8438 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
8439 mFakePolicy->addInputPortAssociation(usb2, hdmi2);
8440
8441 // We are intentionally not adding the viewport for display 1 yet. Since the port association
8442 // for this input device is specified, and the matching viewport is not present,
8443 // the input device should be disabled (at the mapper level).
8444
8445 // Add viewport for display 2 on hdmi2
8446 prepareSecondaryDisplay(type, hdmi2);
8447 // Send a touch event
8448 processPosition(mapper, 100, 100);
8449 processSync(mapper);
8450 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8451
8452 // Add viewport for display 1 on hdmi1
8453 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
8454 // Send a touch event again
8455 processPosition(mapper, 100, 100);
8456 processSync(mapper);
8457
8458 NotifyMotionArgs args;
8459 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8460 ASSERT_EQ(DISPLAY_ID, args.displayId);
8461}
Michael Wrightd02c5b62014-02-10 15:10:22 -08008462
Arthur Hung6d5b4b22022-01-21 07:21:10 +00008463TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayUniqueId) {
8464 addConfigurationProperty("touch.deviceType", "touchScreen");
8465 prepareAxes(POSITION);
8466 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8467
8468 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
8469
8470 prepareDisplay(DISPLAY_ORIENTATION_0);
8471 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
8472
8473 // Send a touch event
8474 processPosition(mapper, 100, 100);
8475 processSync(mapper);
8476
8477 NotifyMotionArgs args;
8478 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8479 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
8480}
8481
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008482TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShouldHandleDisplayId) {
Garfield Tan888a6a42020-01-09 11:39:16 -08008483 // Setup for second display.
Michael Wright17db18e2020-06-26 20:51:44 +01008484 std::shared_ptr<FakePointerController> fakePointerController =
8485 std::make_shared<FakePointerController>();
Garfield Tan888a6a42020-01-09 11:39:16 -08008486 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008487 fakePointerController->setPosition(100, 200);
8488 fakePointerController->setButtonState(0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00008489 mFakePolicy->setPointerController(fakePointerController);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008490
Garfield Tan888a6a42020-01-09 11:39:16 -08008491 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008492 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08008493
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008494 prepareDisplay(DISPLAY_ORIENTATION_0);
8495 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008496 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008497
8498 // Check source is mouse that would obtain the PointerController.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008499 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008500
8501 NotifyMotionArgs motionArgs;
8502 processPosition(mapper, 100, 100);
8503 processSync(mapper);
8504
8505 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8506 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8507 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
8508}
8509
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00008510/**
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008511 * Ensure that the readTime is set to the SYN_REPORT value when processing touch events.
8512 */
8513TEST_F(MultiTouchInputMapperTest, Process_SendsReadTime) {
8514 addConfigurationProperty("touch.deviceType", "touchScreen");
8515 prepareAxes(POSITION);
8516 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8517
8518 prepareDisplay(DISPLAY_ORIENTATION_0);
8519 process(mapper, 10, 11 /*readTime*/, EV_ABS, ABS_MT_TRACKING_ID, 1);
8520 process(mapper, 15, 16 /*readTime*/, EV_ABS, ABS_MT_POSITION_X, 100);
8521 process(mapper, 20, 21 /*readTime*/, EV_ABS, ABS_MT_POSITION_Y, 100);
8522 process(mapper, 25, 26 /*readTime*/, EV_SYN, SYN_REPORT, 0);
8523
8524 NotifyMotionArgs args;
8525 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8526 ASSERT_EQ(26, args.readTime);
8527
8528 process(mapper, 30, 31 /*readTime*/, EV_ABS, ABS_MT_POSITION_X, 110);
8529 process(mapper, 30, 32 /*readTime*/, EV_ABS, ABS_MT_POSITION_Y, 220);
8530 process(mapper, 30, 33 /*readTime*/, EV_SYN, SYN_REPORT, 0);
8531
8532 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8533 ASSERT_EQ(33, args.readTime);
8534}
8535
8536/**
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00008537 * When the viewport is not active (isActive=false), the touch mapper should be disabled and the
8538 * events should not be delivered to the listener.
8539 */
8540TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreDropped) {
8541 addConfigurationProperty("touch.deviceType", "touchScreen");
8542 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
8543 DISPLAY_ORIENTATION_0, false /*isActive*/, UNIQUE_ID, NO_PORT,
8544 ViewportType::INTERNAL);
8545 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8546 prepareAxes(POSITION);
8547 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8548
8549 NotifyMotionArgs motionArgs;
8550 processPosition(mapper, 100, 100);
8551 processSync(mapper);
8552
8553 mFakeListener->assertNotifyMotionWasNotCalled();
8554}
8555
Garfield Tanc734e4f2021-01-15 20:01:39 -08008556TEST_F(MultiTouchInputMapperTest, Process_DeactivateViewport_AbortTouches) {
8557 addConfigurationProperty("touch.deviceType", "touchScreen");
8558 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
8559 DISPLAY_ORIENTATION_0, true /*isActive*/, UNIQUE_ID, NO_PORT,
8560 ViewportType::INTERNAL);
8561 std::optional<DisplayViewport> optionalDisplayViewport =
8562 mFakePolicy->getDisplayViewportByUniqueId(UNIQUE_ID);
8563 ASSERT_TRUE(optionalDisplayViewport.has_value());
8564 DisplayViewport displayViewport = *optionalDisplayViewport;
8565
8566 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8567 prepareAxes(POSITION);
8568 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8569
8570 // Finger down
8571 int32_t x = 100, y = 100;
8572 processPosition(mapper, x, y);
8573 processSync(mapper);
8574
8575 NotifyMotionArgs motionArgs;
8576 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8577 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8578
8579 // Deactivate display viewport
8580 displayViewport.isActive = false;
8581 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
8582 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8583
8584 // Finger move
8585 x += 10, y += 10;
8586 processPosition(mapper, x, y);
8587 processSync(mapper);
8588
8589 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8590 EXPECT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
8591
8592 // Reactivate display viewport
8593 displayViewport.isActive = true;
8594 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
8595 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8596
8597 // Finger move again
8598 x += 10, y += 10;
8599 processPosition(mapper, x, y);
8600 processSync(mapper);
8601
8602 // Gesture is aborted, so events after display is activated won't be dispatched until there is
8603 // no pointer on the touch device.
8604 mFakeListener->assertNotifyMotionWasNotCalled();
8605}
8606
Arthur Hung7c645402019-01-25 17:45:42 +08008607TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShowTouches) {
8608 // Setup the first touch screen device.
Arthur Hung7c645402019-01-25 17:45:42 +08008609 prepareAxes(POSITION | ID | SLOT);
8610 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008611 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung7c645402019-01-25 17:45:42 +08008612
8613 // Create the second touch screen device, and enable multi fingers.
8614 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08008615 const std::string DEVICE_NAME2 = "TOUCHSCREEN2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08008616 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008617 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08008618 std::shared_ptr<InputDevice> device2 =
8619 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07008620 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08008621
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008622 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
8623 0 /*flat*/, 0 /*fuzz*/);
8624 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
8625 0 /*flat*/, 0 /*fuzz*/);
8626 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX,
8627 0 /*flat*/, 0 /*fuzz*/);
8628 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX,
8629 0 /*flat*/, 0 /*fuzz*/);
8630 mFakeEventHub->setAbsoluteAxisValue(SECOND_EVENTHUB_ID, ABS_MT_SLOT, 0 /*value*/);
8631 mFakeEventHub->addConfigurationProperty(SECOND_EVENTHUB_ID, String8("touch.deviceType"),
8632 String8("touchScreen"));
Arthur Hung7c645402019-01-25 17:45:42 +08008633
8634 // Setup the second touch screen device.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008635 MultiTouchInputMapper& mapper2 = device2->addMapper<MultiTouchInputMapper>(SECOND_EVENTHUB_ID);
Arthur Hung7c645402019-01-25 17:45:42 +08008636 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
8637 device2->reset(ARBITRARY_TIME);
8638
8639 // Setup PointerController.
Michael Wright17db18e2020-06-26 20:51:44 +01008640 std::shared_ptr<FakePointerController> fakePointerController =
8641 std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00008642 mFakePolicy->setPointerController(fakePointerController);
Arthur Hung7c645402019-01-25 17:45:42 +08008643
8644 // Setup policy for associated displays and show touches.
8645 const uint8_t hdmi1 = 0;
8646 const uint8_t hdmi2 = 1;
8647 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
8648 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
8649 mFakePolicy->setShowTouches(true);
8650
8651 // Create displays.
8652 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008653 prepareSecondaryDisplay(ViewportType::EXTERNAL, hdmi2);
Arthur Hung7c645402019-01-25 17:45:42 +08008654
8655 // Default device will reconfigure above, need additional reconfiguration for another device.
8656 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008657 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung7c645402019-01-25 17:45:42 +08008658
8659 // Two fingers down at default display.
8660 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
8661 processPosition(mapper, x1, y1);
8662 processId(mapper, 1);
8663 processSlot(mapper, 1);
8664 processPosition(mapper, x2, y2);
8665 processId(mapper, 2);
8666 processSync(mapper);
8667
8668 std::map<int32_t, std::vector<int32_t>>::const_iterator iter =
8669 fakePointerController->getSpots().find(DISPLAY_ID);
8670 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
8671 ASSERT_EQ(size_t(2), iter->second.size());
8672
8673 // Two fingers down at second display.
8674 processPosition(mapper2, x1, y1);
8675 processId(mapper2, 1);
8676 processSlot(mapper2, 1);
8677 processPosition(mapper2, x2, y2);
8678 processId(mapper2, 2);
8679 processSync(mapper2);
8680
8681 iter = fakePointerController->getSpots().find(SECONDARY_DISPLAY_ID);
8682 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
8683 ASSERT_EQ(size_t(2), iter->second.size());
8684}
8685
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008686TEST_F(MultiTouchInputMapperTest, VideoFrames_ReceivedByListener) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008687 prepareAxes(POSITION);
8688 addConfigurationProperty("touch.deviceType", "touchScreen");
8689 prepareDisplay(DISPLAY_ORIENTATION_0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008690 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008691
8692 NotifyMotionArgs motionArgs;
8693 // Unrotated video frame
8694 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8695 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008696 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008697 processPosition(mapper, 100, 200);
8698 processSync(mapper);
8699 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8700 ASSERT_EQ(frames, motionArgs.videoFrames);
8701
8702 // Subsequent touch events should not have any videoframes
8703 // This is implemented separately in FakeEventHub,
8704 // but that should match the behaviour of TouchVideoDevice.
8705 processPosition(mapper, 200, 200);
8706 processSync(mapper);
8707 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8708 ASSERT_EQ(std::vector<TouchVideoFrame>(), motionArgs.videoFrames);
8709}
8710
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008711TEST_F(MultiTouchInputMapperTest, VideoFrames_AreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008712 prepareAxes(POSITION);
8713 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008714 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008715 // Unrotated video frame
8716 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8717 NotifyMotionArgs motionArgs;
8718
8719 // Test all 4 orientations
8720 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008721 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
8722 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
8723 clearViewports();
8724 prepareDisplay(orientation);
8725 std::vector<TouchVideoFrame> frames{frame};
8726 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
8727 processPosition(mapper, 100, 200);
8728 processSync(mapper);
8729 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8730 ASSERT_EQ(frames, motionArgs.videoFrames);
8731 }
8732}
8733
8734TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_AreRotated) {
8735 prepareAxes(POSITION);
8736 addConfigurationProperty("touch.deviceType", "touchScreen");
8737 // Since InputReader works in the un-rotated coordinate space, only devices that are not
8738 // orientation-aware are affected by display rotation.
8739 addConfigurationProperty("touch.orientationAware", "0");
8740 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8741 // Unrotated video frame
8742 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8743 NotifyMotionArgs motionArgs;
8744
8745 // Test all 4 orientations
8746 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008747 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
8748 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
8749 clearViewports();
8750 prepareDisplay(orientation);
8751 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008752 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008753 processPosition(mapper, 100, 200);
8754 processSync(mapper);
8755 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008756 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
8757 // compared to the display. This is so that when the window transform (which contains the
8758 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
8759 // window's coordinate space.
8760 frames[0].rotate(getInverseRotation(orientation));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008761 ASSERT_EQ(frames, motionArgs.videoFrames);
lilinnane74b35f2022-07-19 16:00:50 +08008762
8763 // Release finger.
8764 processSync(mapper);
8765 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008766 }
8767}
8768
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008769TEST_F(MultiTouchInputMapperTest, VideoFrames_MultipleFramesAreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008770 prepareAxes(POSITION);
8771 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008772 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008773 // Unrotated video frames. There's no rule that they must all have the same dimensions,
8774 // so mix these.
8775 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8776 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
8777 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
8778 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
8779 NotifyMotionArgs motionArgs;
8780
8781 prepareDisplay(DISPLAY_ORIENTATION_90);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008782 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008783 processPosition(mapper, 100, 200);
8784 processSync(mapper);
8785 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008786 ASSERT_EQ(frames, motionArgs.videoFrames);
8787}
8788
8789TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_MultipleFramesAreRotated) {
8790 prepareAxes(POSITION);
8791 addConfigurationProperty("touch.deviceType", "touchScreen");
8792 // Since InputReader works in the un-rotated coordinate space, only devices that are not
8793 // orientation-aware are affected by display rotation.
8794 addConfigurationProperty("touch.orientationAware", "0");
8795 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8796 // Unrotated video frames. There's no rule that they must all have the same dimensions,
8797 // so mix these.
8798 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8799 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
8800 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
8801 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
8802 NotifyMotionArgs motionArgs;
8803
8804 prepareDisplay(DISPLAY_ORIENTATION_90);
8805 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
8806 processPosition(mapper, 100, 200);
8807 processSync(mapper);
8808 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8809 std::for_each(frames.begin(), frames.end(), [](TouchVideoFrame& frame) {
8810 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
8811 // compared to the display. This is so that when the window transform (which contains the
8812 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
8813 // window's coordinate space.
8814 frame.rotate(getInverseRotation(DISPLAY_ORIENTATION_90));
8815 });
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008816 ASSERT_EQ(frames, motionArgs.videoFrames);
8817}
8818
Arthur Hung9da14732019-09-02 16:16:58 +08008819/**
8820 * If we had defined port associations, but the viewport is not ready, the touch device would be
8821 * expected to be disabled, and it should be enabled after the viewport has found.
8822 */
8823TEST_F(MultiTouchInputMapperTest, Configure_EnabledForAssociatedDisplay) {
Arthur Hung9da14732019-09-02 16:16:58 +08008824 constexpr uint8_t hdmi2 = 1;
8825 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008826 constexpr ViewportType type = ViewportType::EXTERNAL;
Arthur Hung9da14732019-09-02 16:16:58 +08008827
8828 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi2);
8829
8830 addConfigurationProperty("touch.deviceType", "touchScreen");
8831 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008832 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung9da14732019-09-02 16:16:58 +08008833
8834 ASSERT_EQ(mDevice->isEnabled(), false);
8835
8836 // Add display on hdmi2, the device should be enabled and can receive touch event.
8837 prepareSecondaryDisplay(type, hdmi2);
8838 ASSERT_EQ(mDevice->isEnabled(), true);
8839
8840 // Send a touch event.
8841 processPosition(mapper, 100, 100);
8842 processSync(mapper);
8843
8844 NotifyMotionArgs args;
8845 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8846 ASSERT_EQ(SECONDARY_DISPLAY_ID, args.displayId);
8847}
8848
Arthur Hung421eb1c2020-01-16 00:09:42 +08008849TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleSingleTouch) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08008850 addConfigurationProperty("touch.deviceType", "touchScreen");
8851 prepareDisplay(DISPLAY_ORIENTATION_0);
8852 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008853 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08008854
8855 NotifyMotionArgs motionArgs;
8856
8857 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
8858 // finger down
8859 processId(mapper, 1);
8860 processPosition(mapper, x1, y1);
8861 processSync(mapper);
8862 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8863 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8864 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8865
8866 // finger move
8867 processId(mapper, 1);
8868 processPosition(mapper, x2, y2);
8869 processSync(mapper);
8870 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8871 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8872 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8873
8874 // finger up.
8875 processId(mapper, -1);
8876 processSync(mapper);
8877 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8878 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8879 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8880
8881 // new finger down
8882 processId(mapper, 1);
8883 processPosition(mapper, x3, y3);
8884 processSync(mapper);
8885 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8886 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8887 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8888}
8889
8890/**
arthurhungcc7f9802020-04-30 17:55:40 +08008891 * Test single touch should be canceled when received the MT_TOOL_PALM event, and the following
8892 * MOVE and UP events should be ignored.
Arthur Hung421eb1c2020-01-16 00:09:42 +08008893 */
arthurhungcc7f9802020-04-30 17:55:40 +08008894TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_SinglePointer) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08008895 addConfigurationProperty("touch.deviceType", "touchScreen");
8896 prepareDisplay(DISPLAY_ORIENTATION_0);
8897 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008898 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08008899
8900 NotifyMotionArgs motionArgs;
8901
8902 // default tool type is finger
8903 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
arthurhungcc7f9802020-04-30 17:55:40 +08008904 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008905 processPosition(mapper, x1, y1);
8906 processSync(mapper);
8907 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8908 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8909 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8910
8911 // Tool changed to MT_TOOL_PALM expect sending the cancel event.
8912 processToolType(mapper, MT_TOOL_PALM);
8913 processSync(mapper);
8914 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8915 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
8916
8917 // Ignore the following MOVE and UP events if had detect a palm event.
arthurhungcc7f9802020-04-30 17:55:40 +08008918 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008919 processPosition(mapper, x2, y2);
8920 processSync(mapper);
8921 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8922
8923 // finger up.
arthurhungcc7f9802020-04-30 17:55:40 +08008924 processId(mapper, INVALID_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008925 processSync(mapper);
8926 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8927
8928 // new finger down
arthurhungcc7f9802020-04-30 17:55:40 +08008929 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008930 processToolType(mapper, MT_TOOL_FINGER);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008931 processPosition(mapper, x3, y3);
8932 processSync(mapper);
8933 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8934 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8935 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8936}
8937
arthurhungbf89a482020-04-17 17:37:55 +08008938/**
arthurhungcc7f9802020-04-30 17:55:40 +08008939 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
8940 * and the rest active fingers could still be allowed to receive the events
arthurhungbf89a482020-04-17 17:37:55 +08008941 */
arthurhungcc7f9802020-04-30 17:55:40 +08008942TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_TwoPointers) {
arthurhungbf89a482020-04-17 17:37:55 +08008943 addConfigurationProperty("touch.deviceType", "touchScreen");
8944 prepareDisplay(DISPLAY_ORIENTATION_0);
8945 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
8946 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8947
8948 NotifyMotionArgs motionArgs;
8949
8950 // default tool type is finger
arthurhungcc7f9802020-04-30 17:55:40 +08008951 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
8952 processId(mapper, FIRST_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08008953 processPosition(mapper, x1, y1);
8954 processSync(mapper);
8955 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8956 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8957 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8958
8959 // Second finger down.
arthurhungcc7f9802020-04-30 17:55:40 +08008960 processSlot(mapper, SECOND_SLOT);
8961 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08008962 processPosition(mapper, x2, y2);
arthurhungcc7f9802020-04-30 17:55:40 +08008963 processSync(mapper);
8964 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008965 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08008966 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8967
8968 // If the tool type of the first finger changes to MT_TOOL_PALM,
8969 // we expect to receive ACTION_POINTER_UP with cancel flag.
8970 processSlot(mapper, FIRST_SLOT);
8971 processId(mapper, FIRST_TRACKING_ID);
8972 processToolType(mapper, MT_TOOL_PALM);
8973 processSync(mapper);
8974 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008975 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08008976 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
8977
8978 // The following MOVE events of second finger should be processed.
8979 processSlot(mapper, SECOND_SLOT);
8980 processId(mapper, SECOND_TRACKING_ID);
8981 processPosition(mapper, x2 + 1, y2 + 1);
8982 processSync(mapper);
8983 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8984 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8985 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8986
8987 // First finger up. It used to be in palm mode, and we already generated ACTION_POINTER_UP for
8988 // it. Second finger receive move.
8989 processSlot(mapper, FIRST_SLOT);
8990 processId(mapper, INVALID_TRACKING_ID);
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 // Second finger keeps moving.
8997 processSlot(mapper, SECOND_SLOT);
8998 processId(mapper, SECOND_TRACKING_ID);
8999 processPosition(mapper, x2 + 2, y2 + 2);
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 up.
9006 processId(mapper, INVALID_TRACKING_ID);
9007 processSync(mapper);
9008 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9009 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9010 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9011}
9012
9013/**
9014 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event, if only 1 finger
9015 * is active, it should send CANCEL after receiving the MT_TOOL_PALM event.
9016 */
9017TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_ShouldCancelWhenAllTouchIsPalm) {
9018 addConfigurationProperty("touch.deviceType", "touchScreen");
9019 prepareDisplay(DISPLAY_ORIENTATION_0);
9020 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
9021 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9022
9023 NotifyMotionArgs motionArgs;
9024
9025 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
9026 // First finger down.
9027 processId(mapper, FIRST_TRACKING_ID);
9028 processPosition(mapper, x1, y1);
9029 processSync(mapper);
9030 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9031 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9032 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9033
9034 // Second finger down.
9035 processSlot(mapper, SECOND_SLOT);
9036 processId(mapper, SECOND_TRACKING_ID);
9037 processPosition(mapper, x2, y2);
arthurhungbf89a482020-04-17 17:37:55 +08009038 processSync(mapper);
9039 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009040 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungbf89a482020-04-17 17:37:55 +08009041 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9042
arthurhungcc7f9802020-04-30 17:55:40 +08009043 // If the tool type of the first finger changes to MT_TOOL_PALM,
9044 // we expect to receive ACTION_POINTER_UP with cancel flag.
9045 processSlot(mapper, FIRST_SLOT);
9046 processId(mapper, FIRST_TRACKING_ID);
9047 processToolType(mapper, MT_TOOL_PALM);
9048 processSync(mapper);
9049 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009050 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009051 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9052
9053 // Second finger keeps moving.
9054 processSlot(mapper, SECOND_SLOT);
9055 processId(mapper, SECOND_TRACKING_ID);
9056 processPosition(mapper, x2 + 1, y2 + 1);
9057 processSync(mapper);
9058 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9059 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9060
9061 // second finger becomes palm, receive cancel due to only 1 finger is active.
9062 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009063 processToolType(mapper, MT_TOOL_PALM);
9064 processSync(mapper);
9065 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9066 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9067
arthurhungcc7f9802020-04-30 17:55:40 +08009068 // third finger down.
9069 processSlot(mapper, THIRD_SLOT);
9070 processId(mapper, THIRD_TRACKING_ID);
9071 processToolType(mapper, MT_TOOL_FINGER);
arthurhungbf89a482020-04-17 17:37:55 +08009072 processPosition(mapper, x3, y3);
9073 processSync(mapper);
arthurhungbf89a482020-04-17 17:37:55 +08009074 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9075 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9076 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08009077 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9078
9079 // third finger move
9080 processId(mapper, THIRD_TRACKING_ID);
9081 processPosition(mapper, x3 + 1, y3 + 1);
9082 processSync(mapper);
9083 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9084 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9085
9086 // first finger up, third finger receive move.
9087 processSlot(mapper, FIRST_SLOT);
9088 processId(mapper, INVALID_TRACKING_ID);
9089 processSync(mapper);
9090 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9091 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9092 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9093
9094 // second finger up, third finger receive move.
9095 processSlot(mapper, SECOND_SLOT);
9096 processId(mapper, INVALID_TRACKING_ID);
9097 processSync(mapper);
9098 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9099 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9100 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9101
9102 // third finger up.
9103 processSlot(mapper, THIRD_SLOT);
9104 processId(mapper, INVALID_TRACKING_ID);
9105 processSync(mapper);
9106 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9107 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9108 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9109}
9110
9111/**
9112 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
9113 * and the active finger could still be allowed to receive the events
9114 */
9115TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_KeepFirstPointer) {
9116 addConfigurationProperty("touch.deviceType", "touchScreen");
9117 prepareDisplay(DISPLAY_ORIENTATION_0);
9118 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
9119 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9120
9121 NotifyMotionArgs motionArgs;
9122
9123 // default tool type is finger
9124 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
9125 processId(mapper, FIRST_TRACKING_ID);
9126 processPosition(mapper, x1, y1);
9127 processSync(mapper);
9128 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9129 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9130 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9131
9132 // Second finger down.
9133 processSlot(mapper, SECOND_SLOT);
9134 processId(mapper, SECOND_TRACKING_ID);
9135 processPosition(mapper, x2, y2);
9136 processSync(mapper);
9137 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009138 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009139 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9140
9141 // If the tool type of the second finger changes to MT_TOOL_PALM,
9142 // we expect to receive ACTION_POINTER_UP with cancel flag.
9143 processId(mapper, SECOND_TRACKING_ID);
9144 processToolType(mapper, MT_TOOL_PALM);
9145 processSync(mapper);
9146 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009147 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009148 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9149
9150 // The following MOVE event should be processed.
9151 processSlot(mapper, FIRST_SLOT);
9152 processId(mapper, FIRST_TRACKING_ID);
9153 processPosition(mapper, x1 + 1, y1 + 1);
9154 processSync(mapper);
9155 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9156 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9157 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9158
9159 // second finger up.
9160 processSlot(mapper, SECOND_SLOT);
9161 processId(mapper, INVALID_TRACKING_ID);
9162 processSync(mapper);
9163 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9164 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9165
9166 // first finger keep moving
9167 processSlot(mapper, FIRST_SLOT);
9168 processId(mapper, FIRST_TRACKING_ID);
9169 processPosition(mapper, x1 + 2, y1 + 2);
9170 processSync(mapper);
9171 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9172 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9173
9174 // first finger up.
9175 processId(mapper, INVALID_TRACKING_ID);
9176 processSync(mapper);
9177 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9178 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9179 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
arthurhungbf89a482020-04-17 17:37:55 +08009180}
9181
Arthur Hung9ad18942021-06-19 02:04:46 +00009182/**
9183 * Test multi-touch should sent ACTION_POINTER_UP/ACTION_UP when received the INVALID_TRACKING_ID,
9184 * to prevent the driver side may send unexpected data after set tracking id as INVALID_TRACKING_ID
9185 * cause slot be valid again.
9186 */
9187TEST_F(MultiTouchInputMapperTest, Process_MultiTouch_WithInvalidTrackingId) {
9188 addConfigurationProperty("touch.deviceType", "touchScreen");
9189 prepareDisplay(DISPLAY_ORIENTATION_0);
9190 prepareAxes(POSITION | ID | SLOT | PRESSURE);
9191 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9192
9193 NotifyMotionArgs motionArgs;
9194
9195 constexpr int32_t x1 = 100, y1 = 200, x2 = 0, y2 = 0;
9196 // First finger down.
9197 processId(mapper, FIRST_TRACKING_ID);
9198 processPosition(mapper, x1, y1);
9199 processPressure(mapper, RAW_PRESSURE_MAX);
9200 processSync(mapper);
9201 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9202 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9203 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9204
9205 // First finger move.
9206 processId(mapper, FIRST_TRACKING_ID);
9207 processPosition(mapper, x1 + 1, y1 + 1);
9208 processPressure(mapper, RAW_PRESSURE_MAX);
9209 processSync(mapper);
9210 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9211 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9212 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9213
9214 // Second finger down.
9215 processSlot(mapper, SECOND_SLOT);
9216 processId(mapper, SECOND_TRACKING_ID);
9217 processPosition(mapper, x2, y2);
9218 processPressure(mapper, RAW_PRESSURE_MAX);
9219 processSync(mapper);
9220 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009221 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Arthur Hung9ad18942021-06-19 02:04:46 +00009222 ASSERT_EQ(uint32_t(2), motionArgs.pointerCount);
9223
9224 // second finger up with some unexpected data.
9225 processSlot(mapper, SECOND_SLOT);
9226 processId(mapper, INVALID_TRACKING_ID);
9227 processPosition(mapper, x2, y2);
9228 processSync(mapper);
9229 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009230 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Arthur Hung9ad18942021-06-19 02:04:46 +00009231 ASSERT_EQ(uint32_t(2), motionArgs.pointerCount);
9232
9233 // first finger up with some unexpected data.
9234 processSlot(mapper, FIRST_SLOT);
9235 processId(mapper, INVALID_TRACKING_ID);
9236 processPosition(mapper, x2, y2);
9237 processPressure(mapper, RAW_PRESSURE_MAX);
9238 processSync(mapper);
9239 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9240 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9241 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9242}
9243
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009244// --- MultiTouchInputMapperTest_ExternalDevice ---
9245
9246class MultiTouchInputMapperTest_ExternalDevice : public MultiTouchInputMapperTest {
9247protected:
Chris Yea52ade12020-08-27 16:49:20 -07009248 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009249};
9250
9251/**
9252 * Expect fallback to internal viewport if device is external and external viewport is not present.
9253 */
9254TEST_F(MultiTouchInputMapperTest_ExternalDevice, Viewports_Fallback) {
9255 prepareAxes(POSITION);
9256 addConfigurationProperty("touch.deviceType", "touchScreen");
9257 prepareDisplay(DISPLAY_ORIENTATION_0);
9258 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9259
9260 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
9261
9262 NotifyMotionArgs motionArgs;
9263
9264 // Expect the event to be sent to the internal viewport,
9265 // because an external viewport is not present.
9266 processPosition(mapper, 100, 100);
9267 processSync(mapper);
9268 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9269 ASSERT_EQ(ADISPLAY_ID_DEFAULT, motionArgs.displayId);
9270
9271 // Expect the event to be sent to the external viewport if it is present.
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009272 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009273 processPosition(mapper, 100, 100);
9274 processSync(mapper);
9275 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9276 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
9277}
Arthur Hung4197f6b2020-03-16 15:39:59 +08009278
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009279TEST_F(MultiTouchInputMapperTest, Process_TouchpadCapture) {
9280 // we need a pointer controller for mouse mode of touchpad (start pointer at 0,0)
9281 std::shared_ptr<FakePointerController> fakePointerController =
9282 std::make_shared<FakePointerController>();
9283 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
9284 fakePointerController->setPosition(0, 0);
9285 fakePointerController->setButtonState(0);
9286
9287 // prepare device and capture
9288 prepareDisplay(DISPLAY_ORIENTATION_0);
9289 prepareAxes(POSITION | ID | SLOT);
9290 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
9291 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
9292 mFakePolicy->setPointerCapture(true);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009293 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009294 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9295
9296 // captured touchpad should be a touchpad source
9297 NotifyDeviceResetArgs resetArgs;
9298 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
9299 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
9300
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009301 InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
Chris Yef74dc422020-09-02 22:41:50 -07009302
9303 const InputDeviceInfo::MotionRange* relRangeX =
9304 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_X, AINPUT_SOURCE_TOUCHPAD);
9305 ASSERT_NE(relRangeX, nullptr);
9306 ASSERT_EQ(relRangeX->min, -(RAW_X_MAX - RAW_X_MIN));
9307 ASSERT_EQ(relRangeX->max, RAW_X_MAX - RAW_X_MIN);
9308 const InputDeviceInfo::MotionRange* relRangeY =
9309 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_Y, AINPUT_SOURCE_TOUCHPAD);
9310 ASSERT_NE(relRangeY, nullptr);
9311 ASSERT_EQ(relRangeY->min, -(RAW_Y_MAX - RAW_Y_MIN));
9312 ASSERT_EQ(relRangeY->max, RAW_Y_MAX - RAW_Y_MIN);
9313
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009314 // run captured pointer tests - note that this is unscaled, so input listener events should be
9315 // identical to what the hardware sends (accounting for any
9316 // calibration).
9317 // FINGER 0 DOWN
Chris Ye364fdb52020-08-05 15:07:56 -07009318 processSlot(mapper, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009319 processId(mapper, 1);
9320 processPosition(mapper, 100 + RAW_X_MIN, 100 + RAW_Y_MIN);
9321 processKey(mapper, BTN_TOUCH, 1);
9322 processSync(mapper);
9323
9324 // expect coord[0] to contain initial location of touch 0
9325 NotifyMotionArgs args;
9326 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9327 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
9328 ASSERT_EQ(1U, args.pointerCount);
9329 ASSERT_EQ(0, args.pointerProperties[0].id);
9330 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, args.source);
9331 ASSERT_NO_FATAL_FAILURE(
9332 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
9333
9334 // FINGER 1 DOWN
9335 processSlot(mapper, 1);
9336 processId(mapper, 2);
9337 processPosition(mapper, 560 + RAW_X_MIN, 154 + RAW_Y_MIN);
9338 processSync(mapper);
9339
9340 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
9341 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009342 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009343 ASSERT_EQ(2U, args.pointerCount);
9344 ASSERT_EQ(0, args.pointerProperties[0].id);
9345 ASSERT_EQ(1, args.pointerProperties[1].id);
9346 ASSERT_NO_FATAL_FAILURE(
9347 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
9348 ASSERT_NO_FATAL_FAILURE(
9349 assertPointerCoords(args.pointerCoords[1], 560, 154, 1, 0, 0, 0, 0, 0, 0, 0));
9350
9351 // FINGER 1 MOVE
9352 processPosition(mapper, 540 + RAW_X_MIN, 690 + RAW_Y_MIN);
9353 processSync(mapper);
9354
9355 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
9356 // from move
9357 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9358 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9359 ASSERT_NO_FATAL_FAILURE(
9360 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
9361 ASSERT_NO_FATAL_FAILURE(
9362 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
9363
9364 // FINGER 0 MOVE
9365 processSlot(mapper, 0);
9366 processPosition(mapper, 50 + RAW_X_MIN, 800 + RAW_Y_MIN);
9367 processSync(mapper);
9368
9369 // expect coord[0] to contain new touch 0 location, coord[1] to contain previous location
9370 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9371 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9372 ASSERT_NO_FATAL_FAILURE(
9373 assertPointerCoords(args.pointerCoords[0], 50, 800, 1, 0, 0, 0, 0, 0, 0, 0));
9374 ASSERT_NO_FATAL_FAILURE(
9375 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
9376
9377 // BUTTON DOWN
9378 processKey(mapper, BTN_LEFT, 1);
9379 processSync(mapper);
9380
9381 // touchinputmapper design sends a move before button press
9382 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9383 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9384 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9385 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
9386
9387 // BUTTON UP
9388 processKey(mapper, BTN_LEFT, 0);
9389 processSync(mapper);
9390
9391 // touchinputmapper design sends a move after button release
9392 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9393 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
9394 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9395 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9396
9397 // FINGER 0 UP
9398 processId(mapper, -1);
9399 processSync(mapper);
9400 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9401 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | 0x0000, args.action);
9402
9403 // FINGER 1 MOVE
9404 processSlot(mapper, 1);
9405 processPosition(mapper, 320 + RAW_X_MIN, 900 + RAW_Y_MIN);
9406 processSync(mapper);
9407
9408 // expect coord[0] to contain new location of touch 1, and properties[0].id to contain 1
9409 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9410 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9411 ASSERT_EQ(1U, args.pointerCount);
9412 ASSERT_EQ(1, args.pointerProperties[0].id);
9413 ASSERT_NO_FATAL_FAILURE(
9414 assertPointerCoords(args.pointerCoords[0], 320, 900, 1, 0, 0, 0, 0, 0, 0, 0));
9415
9416 // FINGER 1 UP
9417 processId(mapper, -1);
9418 processKey(mapper, BTN_TOUCH, 0);
9419 processSync(mapper);
9420 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9421 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
9422
9423 // non captured touchpad should be a mouse source
9424 mFakePolicy->setPointerCapture(false);
9425 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
9426 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
9427 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
9428}
9429
9430TEST_F(MultiTouchInputMapperTest, Process_UnCapturedTouchpadPointer) {
9431 std::shared_ptr<FakePointerController> fakePointerController =
9432 std::make_shared<FakePointerController>();
9433 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
9434 fakePointerController->setPosition(0, 0);
9435 fakePointerController->setButtonState(0);
9436
9437 // prepare device and capture
9438 prepareDisplay(DISPLAY_ORIENTATION_0);
9439 prepareAxes(POSITION | ID | SLOT);
9440 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
9441 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009442 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009443 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9444 // run uncaptured pointer tests - pushes out generic events
9445 // FINGER 0 DOWN
9446 processId(mapper, 3);
9447 processPosition(mapper, 100, 100);
9448 processKey(mapper, BTN_TOUCH, 1);
9449 processSync(mapper);
9450
9451 // start at (100,100), cursor should be at (0,0) * scale
9452 NotifyMotionArgs args;
9453 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9454 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
9455 ASSERT_NO_FATAL_FAILURE(
9456 assertPointerCoords(args.pointerCoords[0], 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
9457
9458 // FINGER 0 MOVE
9459 processPosition(mapper, 200, 200);
9460 processSync(mapper);
9461
9462 // compute scaling to help with touch position checking
9463 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
9464 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
9465 float scale =
9466 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
9467
9468 // translate from (100,100) -> (200,200), cursor should have changed to (100,100) * scale)
9469 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9470 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
9471 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 100 * scale, 100 * scale, 0,
9472 0, 0, 0, 0, 0, 0, 0));
9473}
9474
9475TEST_F(MultiTouchInputMapperTest, WhenCapturedAndNotCaptured_GetSources) {
9476 std::shared_ptr<FakePointerController> fakePointerController =
9477 std::make_shared<FakePointerController>();
9478
9479 prepareDisplay(DISPLAY_ORIENTATION_0);
9480 prepareAxes(POSITION | ID | SLOT);
9481 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009482 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009483 mFakePolicy->setPointerCapture(false);
9484 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9485
9486 // uncaptured touchpad should be a pointer device
9487 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
9488
9489 // captured touchpad should be a touchpad device
9490 mFakePolicy->setPointerCapture(true);
9491 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
9492 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
9493}
9494
Arthur Hung6d5b4b22022-01-21 07:21:10 +00009495// --- JoystickInputMapperTest ---
9496
9497class JoystickInputMapperTest : public InputMapperTest {
9498protected:
9499 static const int32_t RAW_X_MIN;
9500 static const int32_t RAW_X_MAX;
9501 static const int32_t RAW_Y_MIN;
9502 static const int32_t RAW_Y_MAX;
9503
9504 void SetUp() override {
9505 InputMapperTest::SetUp(InputDeviceClass::JOYSTICK | InputDeviceClass::EXTERNAL);
9506 }
9507 void prepareAxes() {
9508 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
9509 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
9510 }
9511
9512 void processAxis(JoystickInputMapper& mapper, int32_t axis, int32_t value) {
9513 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, axis, value);
9514 }
9515
9516 void processSync(JoystickInputMapper& mapper) {
9517 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
9518 }
9519
9520 void prepareVirtualDisplay(int32_t orientation) {
9521 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH,
9522 VIRTUAL_DISPLAY_HEIGHT, orientation, VIRTUAL_DISPLAY_UNIQUE_ID,
9523 NO_PORT, ViewportType::VIRTUAL);
9524 }
9525};
9526
9527const int32_t JoystickInputMapperTest::RAW_X_MIN = -32767;
9528const int32_t JoystickInputMapperTest::RAW_X_MAX = 32767;
9529const int32_t JoystickInputMapperTest::RAW_Y_MIN = -32767;
9530const int32_t JoystickInputMapperTest::RAW_Y_MAX = 32767;
9531
9532TEST_F(JoystickInputMapperTest, Configure_AssignsDisplayUniqueId) {
9533 prepareAxes();
9534 JoystickInputMapper& mapper = addMapperAndConfigure<JoystickInputMapper>();
9535
9536 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
9537
9538 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
9539
9540 // Send an axis event
9541 processAxis(mapper, ABS_X, 100);
9542 processSync(mapper);
9543
9544 NotifyMotionArgs args;
9545 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9546 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
9547
9548 // Send another axis event
9549 processAxis(mapper, ABS_Y, 100);
9550 processSync(mapper);
9551
9552 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9553 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
9554}
9555
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009556// --- PeripheralControllerTest ---
Chris Yee2b1e5c2021-03-10 22:45:12 -08009557
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009558class PeripheralControllerTest : public testing::Test {
Chris Yee2b1e5c2021-03-10 22:45:12 -08009559protected:
9560 static const char* DEVICE_NAME;
9561 static const char* DEVICE_LOCATION;
9562 static const int32_t DEVICE_ID;
9563 static const int32_t DEVICE_GENERATION;
9564 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07009565 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Chris Yee2b1e5c2021-03-10 22:45:12 -08009566 static const int32_t EVENTHUB_ID;
9567
9568 std::shared_ptr<FakeEventHub> mFakeEventHub;
9569 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07009570 std::unique_ptr<TestInputListener> mFakeListener;
Chris Yee2b1e5c2021-03-10 22:45:12 -08009571 std::unique_ptr<InstrumentedInputReader> mReader;
9572 std::shared_ptr<InputDevice> mDevice;
9573
Dominik Laskowski2f01d772022-03-23 16:01:29 -07009574 virtual void SetUp(ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -08009575 mFakeEventHub = std::make_unique<FakeEventHub>();
9576 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07009577 mFakeListener = std::make_unique<TestInputListener>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009578 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07009579 *mFakeListener);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009580 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
9581 }
9582
9583 void SetUp() override { SetUp(DEVICE_CLASSES); }
9584
9585 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07009586 mFakeListener.reset();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009587 mFakePolicy.clear();
9588 }
9589
9590 void configureDevice(uint32_t changes) {
9591 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
9592 mReader->requestRefreshConfiguration(changes);
9593 mReader->loopOnce();
9594 }
9595 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
9596 }
9597
9598 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
9599 const std::string& location, int32_t eventHubId,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07009600 ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -08009601 InputDeviceIdentifier identifier;
9602 identifier.name = name;
9603 identifier.location = location;
9604 std::shared_ptr<InputDevice> device =
9605 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
9606 identifier);
9607 mReader->pushNextDevice(device);
9608 mFakeEventHub->addDevice(eventHubId, name, classes);
9609 mReader->loopOnce();
9610 return device;
9611 }
9612
9613 template <class T, typename... Args>
9614 T& addControllerAndConfigure(Args... args) {
9615 T& controller = mDevice->addController<T>(EVENTHUB_ID, args...);
9616
9617 return controller;
9618 }
9619};
9620
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009621const char* PeripheralControllerTest::DEVICE_NAME = "device";
9622const char* PeripheralControllerTest::DEVICE_LOCATION = "BLUETOOTH";
9623const int32_t PeripheralControllerTest::DEVICE_ID = END_RESERVED_ID + 1000;
9624const int32_t PeripheralControllerTest::DEVICE_GENERATION = 2;
9625const int32_t PeripheralControllerTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07009626const ftl::Flags<InputDeviceClass> PeripheralControllerTest::DEVICE_CLASSES =
9627 ftl::Flags<InputDeviceClass>(0); // not needed for current tests
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009628const int32_t PeripheralControllerTest::EVENTHUB_ID = 1;
Chris Yee2b1e5c2021-03-10 22:45:12 -08009629
9630// --- BatteryControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009631class BatteryControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -08009632protected:
9633 void SetUp() override {
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009634 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::BATTERY);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009635 }
9636};
9637
9638TEST_F(BatteryControllerTest, GetBatteryCapacity) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009639 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009640
9641 ASSERT_TRUE(controller.getBatteryCapacity(DEFAULT_BATTERY));
9642 ASSERT_EQ(controller.getBatteryCapacity(DEFAULT_BATTERY).value_or(-1), BATTERY_CAPACITY);
9643}
9644
9645TEST_F(BatteryControllerTest, GetBatteryStatus) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009646 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009647
9648 ASSERT_TRUE(controller.getBatteryStatus(DEFAULT_BATTERY));
9649 ASSERT_EQ(controller.getBatteryStatus(DEFAULT_BATTERY).value_or(-1), BATTERY_STATUS);
9650}
9651
9652// --- LightControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009653class LightControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -08009654protected:
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009655 void SetUp() override {
9656 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::LIGHT);
9657 }
Chris Yee2b1e5c2021-03-10 22:45:12 -08009658};
9659
Chris Ye85758332021-05-16 23:05:17 -07009660TEST_F(LightControllerTest, MonoLight) {
9661 RawLightInfo infoMono = {.id = 1,
9662 .name = "Mono",
9663 .maxBrightness = 255,
9664 .flags = InputLightClass::BRIGHTNESS,
9665 .path = ""};
9666 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
Chris Yee2b1e5c2021-03-10 22:45:12 -08009667
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009668 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009669 InputDeviceInfo info;
9670 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009671 std::vector<InputDeviceLightInfo> lights = info.getLights();
9672 ASSERT_EQ(1U, lights.size());
9673 ASSERT_EQ(InputDeviceLightType::MONO, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009674
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009675 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
9676 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009677}
9678
9679TEST_F(LightControllerTest, RGBLight) {
9680 RawLightInfo infoRed = {.id = 1,
9681 .name = "red",
9682 .maxBrightness = 255,
9683 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
9684 .path = ""};
9685 RawLightInfo infoGreen = {.id = 2,
9686 .name = "green",
9687 .maxBrightness = 255,
9688 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
9689 .path = ""};
9690 RawLightInfo infoBlue = {.id = 3,
9691 .name = "blue",
9692 .maxBrightness = 255,
9693 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
9694 .path = ""};
9695 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
9696 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
9697 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
9698
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009699 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009700 InputDeviceInfo info;
9701 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009702 std::vector<InputDeviceLightInfo> lights = info.getLights();
9703 ASSERT_EQ(1U, lights.size());
9704 ASSERT_EQ(InputDeviceLightType::RGB, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009705
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009706 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
9707 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009708}
9709
9710TEST_F(LightControllerTest, MultiColorRGBLight) {
9711 RawLightInfo infoColor = {.id = 1,
9712 .name = "red",
9713 .maxBrightness = 255,
9714 .flags = InputLightClass::BRIGHTNESS |
9715 InputLightClass::MULTI_INTENSITY |
9716 InputLightClass::MULTI_INDEX,
9717 .path = ""};
9718
9719 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
9720
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009721 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009722 InputDeviceInfo info;
9723 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009724 std::vector<InputDeviceLightInfo> lights = info.getLights();
9725 ASSERT_EQ(1U, lights.size());
9726 ASSERT_EQ(InputDeviceLightType::MULTI_COLOR, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009727
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009728 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
9729 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009730}
9731
9732TEST_F(LightControllerTest, PlayerIdLight) {
9733 RawLightInfo info1 = {.id = 1,
9734 .name = "player1",
9735 .maxBrightness = 255,
9736 .flags = InputLightClass::BRIGHTNESS,
9737 .path = ""};
9738 RawLightInfo info2 = {.id = 2,
9739 .name = "player2",
9740 .maxBrightness = 255,
9741 .flags = InputLightClass::BRIGHTNESS,
9742 .path = ""};
9743 RawLightInfo info3 = {.id = 3,
9744 .name = "player3",
9745 .maxBrightness = 255,
9746 .flags = InputLightClass::BRIGHTNESS,
9747 .path = ""};
9748 RawLightInfo info4 = {.id = 4,
9749 .name = "player4",
9750 .maxBrightness = 255,
9751 .flags = InputLightClass::BRIGHTNESS,
9752 .path = ""};
9753 mFakeEventHub->addRawLightInfo(info1.id, std::move(info1));
9754 mFakeEventHub->addRawLightInfo(info2.id, std::move(info2));
9755 mFakeEventHub->addRawLightInfo(info3.id, std::move(info3));
9756 mFakeEventHub->addRawLightInfo(info4.id, std::move(info4));
9757
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009758 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009759 InputDeviceInfo info;
9760 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009761 std::vector<InputDeviceLightInfo> lights = info.getLights();
9762 ASSERT_EQ(1U, lights.size());
9763 ASSERT_EQ(InputDeviceLightType::PLAYER_ID, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009764
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009765 ASSERT_FALSE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
9766 ASSERT_TRUE(controller.setLightPlayerId(lights[0].id, LIGHT_PLAYER_ID));
9767 ASSERT_EQ(controller.getLightPlayerId(lights[0].id).value_or(-1), LIGHT_PLAYER_ID);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009768}
9769
Michael Wrightd02c5b62014-02-10 15:10:22 -08009770} // namespace android