blob: c36704bf15d95c3518f2ebb7290d8dbdce4cae54 [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);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002941 }
2942
Prabir Pradhanc14266f2021-05-12 15:56:24 -07002943 void SetUp() override {
Prabir Pradhanc14266f2021-05-12 15:56:24 -07002944 SetUp(DEVICE_CLASSES);
2945 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002946
Chris Yea52ade12020-08-27 16:49:20 -07002947 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002948 mFakeListener.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002949 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002950 }
2951
2952 void addConfigurationProperty(const char* key, const char* value) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002953 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, String8(key), String8(value));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002954 }
2955
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002956 void configureDevice(uint32_t changes) {
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00002957 if (!changes ||
2958 (changes &
2959 (InputReaderConfiguration::CHANGE_DISPLAY_INFO |
2960 InputReaderConfiguration::CHANGE_POINTER_CAPTURE))) {
arthurhungdcef2dc2020-08-11 14:47:50 +08002961 mReader->requestRefreshConfiguration(changes);
2962 mReader->loopOnce();
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -08002963 }
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002964 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
2965 }
2966
arthurhungdcef2dc2020-08-11 14:47:50 +08002967 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
2968 const std::string& location, int32_t eventHubId,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002969 ftl::Flags<InputDeviceClass> classes) {
arthurhungdcef2dc2020-08-11 14:47:50 +08002970 InputDeviceIdentifier identifier;
2971 identifier.name = name;
2972 identifier.location = location;
2973 std::shared_ptr<InputDevice> device =
2974 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
2975 identifier);
2976 mReader->pushNextDevice(device);
2977 mFakeEventHub->addDevice(eventHubId, name, classes);
2978 mReader->loopOnce();
2979 return device;
2980 }
2981
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002982 template <class T, typename... Args>
2983 T& addMapperAndConfigure(Args... args) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002984 T& mapper = mDevice->addMapper<T>(EVENTHUB_ID, args...);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002985 configureDevice(0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002986 mDevice->reset(ARBITRARY_TIME);
Chris Ye42b06822020-08-07 11:39:33 -07002987 mapper.reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002988 return mapper;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002989 }
2990
2991 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002992 int32_t orientation, const std::string& uniqueId,
2993 std::optional<uint8_t> physicalPort, ViewportType viewportType) {
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002994 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, true /*isActive*/,
2995 uniqueId, physicalPort, viewportType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07002996 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2997 }
2998
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002999 void clearViewports() {
3000 mFakePolicy->clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003001 }
3002
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003003 void process(InputMapper& mapper, nsecs_t when, nsecs_t readTime, int32_t type, int32_t code,
3004 int32_t value) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003005 RawEvent event;
3006 event.when = when;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003007 event.readTime = readTime;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003008 event.deviceId = mapper.getDeviceContext().getEventHubId();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003009 event.type = type;
3010 event.code = code;
3011 event.value = value;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003012 mapper.process(&event);
arthurhungdcef2dc2020-08-11 14:47:50 +08003013 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003014 }
3015
3016 static void assertMotionRange(const InputDeviceInfo& info,
3017 int32_t axis, uint32_t source, float min, float max, float flat, float fuzz) {
3018 const InputDeviceInfo::MotionRange* range = info.getMotionRange(axis, source);
Yi Kong9b14ac62018-07-17 13:48:38 -07003019 ASSERT_TRUE(range != nullptr) << "Axis: " << axis << " Source: " << source;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003020 ASSERT_EQ(axis, range->axis) << "Axis: " << axis << " Source: " << source;
3021 ASSERT_EQ(source, range->source) << "Axis: " << axis << " Source: " << source;
3022 ASSERT_NEAR(min, range->min, EPSILON) << "Axis: " << axis << " Source: " << source;
3023 ASSERT_NEAR(max, range->max, EPSILON) << "Axis: " << axis << " Source: " << source;
3024 ASSERT_NEAR(flat, range->flat, EPSILON) << "Axis: " << axis << " Source: " << source;
3025 ASSERT_NEAR(fuzz, range->fuzz, EPSILON) << "Axis: " << axis << " Source: " << source;
3026 }
3027
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003028 static void assertPointerCoords(const PointerCoords& coords, float x, float y, float pressure,
3029 float size, float touchMajor, float touchMinor, float toolMajor,
3030 float toolMinor, float orientation, float distance,
3031 float scaledAxisEpsilon = 1.f) {
3032 ASSERT_NEAR(x, coords.getAxisValue(AMOTION_EVENT_AXIS_X), scaledAxisEpsilon);
3033 ASSERT_NEAR(y, coords.getAxisValue(AMOTION_EVENT_AXIS_Y), scaledAxisEpsilon);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003034 ASSERT_NEAR(pressure, coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), EPSILON);
3035 ASSERT_NEAR(size, coords.getAxisValue(AMOTION_EVENT_AXIS_SIZE), EPSILON);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003036 ASSERT_NEAR(touchMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR),
3037 scaledAxisEpsilon);
3038 ASSERT_NEAR(touchMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR),
3039 scaledAxisEpsilon);
3040 ASSERT_NEAR(toolMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR),
3041 scaledAxisEpsilon);
3042 ASSERT_NEAR(toolMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR),
3043 scaledAxisEpsilon);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003044 ASSERT_NEAR(orientation, coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION), EPSILON);
3045 ASSERT_NEAR(distance, coords.getAxisValue(AMOTION_EVENT_AXIS_DISTANCE), EPSILON);
3046 }
3047
Michael Wright17db18e2020-06-26 20:51:44 +01003048 static void assertPosition(const FakePointerController& controller, float x, float y) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003049 float actualX, actualY;
Michael Wright17db18e2020-06-26 20:51:44 +01003050 controller.getPosition(&actualX, &actualY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003051 ASSERT_NEAR(x, actualX, 1);
3052 ASSERT_NEAR(y, actualY, 1);
3053 }
3054};
3055
3056const char* InputMapperTest::DEVICE_NAME = "device";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003057const char* InputMapperTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003058const int32_t InputMapperTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003059const int32_t InputMapperTest::DEVICE_GENERATION = 2;
3060const int32_t InputMapperTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003061const ftl::Flags<InputDeviceClass> InputMapperTest::DEVICE_CLASSES =
3062 ftl::Flags<InputDeviceClass>(0); // not needed for current tests
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003063const int32_t InputMapperTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003064
3065// --- SwitchInputMapperTest ---
3066
3067class SwitchInputMapperTest : public InputMapperTest {
3068protected:
3069};
3070
3071TEST_F(SwitchInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003072 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003073
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003074 ASSERT_EQ(uint32_t(AINPUT_SOURCE_SWITCH), mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003075}
3076
3077TEST_F(SwitchInputMapperTest, GetSwitchState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003078 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003079
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003080 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003081 ASSERT_EQ(1, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003082
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003083 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003084 ASSERT_EQ(0, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003085}
3086
3087TEST_F(SwitchInputMapperTest, Process) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003088 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003089
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003090 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_LID, 1);
3091 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_JACK_PHYSICAL_INSERT, 1);
3092 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_HEADPHONE_INSERT, 0);
3093 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003094
3095 NotifySwitchArgs args;
3096 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySwitchWasCalled(&args));
3097 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
Dan Albert1bd2fc02016-02-02 15:11:57 -08003098 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT), args.switchValues);
3099 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT) | (1 << SW_HEADPHONE_INSERT),
Michael Wrightd02c5b62014-02-10 15:10:22 -08003100 args.switchMask);
3101 ASSERT_EQ(uint32_t(0), args.policyFlags);
3102}
3103
Chris Ye87143712020-11-10 05:05:58 +00003104// --- VibratorInputMapperTest ---
3105class VibratorInputMapperTest : public InputMapperTest {
3106protected:
3107 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::VIBRATOR); }
3108};
3109
3110TEST_F(VibratorInputMapperTest, GetSources) {
3111 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
3112
3113 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mapper.getSources());
3114}
3115
3116TEST_F(VibratorInputMapperTest, GetVibratorIds) {
3117 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
3118
3119 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
3120}
3121
3122TEST_F(VibratorInputMapperTest, Vibrate) {
3123 constexpr uint8_t DEFAULT_AMPLITUDE = 192;
Chris Yefb552902021-02-03 17:18:37 -08003124 constexpr int32_t VIBRATION_TOKEN = 100;
Chris Ye87143712020-11-10 05:05:58 +00003125 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
3126
3127 VibrationElement pattern(2);
3128 VibrationSequence sequence(2);
3129 pattern.duration = std::chrono::milliseconds(200);
3130 pattern.channels = {{0 /* vibratorId */, DEFAULT_AMPLITUDE / 2},
3131 {1 /* vibratorId */, DEFAULT_AMPLITUDE}};
3132 sequence.addElement(pattern);
3133 pattern.duration = std::chrono::milliseconds(500);
3134 pattern.channels = {{0 /* vibratorId */, DEFAULT_AMPLITUDE / 4},
3135 {1 /* vibratorId */, DEFAULT_AMPLITUDE}};
3136 sequence.addElement(pattern);
3137
3138 std::vector<int64_t> timings = {0, 1};
3139 std::vector<uint8_t> amplitudes = {DEFAULT_AMPLITUDE, DEFAULT_AMPLITUDE / 2};
3140
3141 ASSERT_FALSE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08003142 // Start vibrating
3143 mapper.vibrate(sequence, -1 /* repeat */, VIBRATION_TOKEN);
Chris Ye87143712020-11-10 05:05:58 +00003144 ASSERT_TRUE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08003145 // Verify vibrator state listener was notified.
3146 mReader->loopOnce();
3147 NotifyVibratorStateArgs args;
3148 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyVibratorStateWasCalled(&args));
3149 ASSERT_EQ(DEVICE_ID, args.deviceId);
3150 ASSERT_TRUE(args.isOn);
3151 // Stop vibrating
3152 mapper.cancelVibrate(VIBRATION_TOKEN);
3153 ASSERT_FALSE(mapper.isVibrating());
3154 // Verify vibrator state listener was notified.
3155 mReader->loopOnce();
3156 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyVibratorStateWasCalled(&args));
3157 ASSERT_EQ(DEVICE_ID, args.deviceId);
3158 ASSERT_FALSE(args.isOn);
Chris Ye87143712020-11-10 05:05:58 +00003159}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003160
Chris Yef59a2f42020-10-16 12:55:26 -07003161// --- SensorInputMapperTest ---
3162
3163class SensorInputMapperTest : public InputMapperTest {
3164protected:
3165 static const int32_t ACCEL_RAW_MIN;
3166 static const int32_t ACCEL_RAW_MAX;
3167 static const int32_t ACCEL_RAW_FUZZ;
3168 static const int32_t ACCEL_RAW_FLAT;
3169 static const int32_t ACCEL_RAW_RESOLUTION;
3170
3171 static const int32_t GYRO_RAW_MIN;
3172 static const int32_t GYRO_RAW_MAX;
3173 static const int32_t GYRO_RAW_FUZZ;
3174 static const int32_t GYRO_RAW_FLAT;
3175 static const int32_t GYRO_RAW_RESOLUTION;
3176
3177 static const float GRAVITY_MS2_UNIT;
3178 static const float DEGREE_RADIAN_UNIT;
3179
3180 void prepareAccelAxes();
3181 void prepareGyroAxes();
3182 void setAccelProperties();
3183 void setGyroProperties();
3184 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::SENSOR); }
3185};
3186
3187const int32_t SensorInputMapperTest::ACCEL_RAW_MIN = -32768;
3188const int32_t SensorInputMapperTest::ACCEL_RAW_MAX = 32768;
3189const int32_t SensorInputMapperTest::ACCEL_RAW_FUZZ = 16;
3190const int32_t SensorInputMapperTest::ACCEL_RAW_FLAT = 0;
3191const int32_t SensorInputMapperTest::ACCEL_RAW_RESOLUTION = 8192;
3192
3193const int32_t SensorInputMapperTest::GYRO_RAW_MIN = -2097152;
3194const int32_t SensorInputMapperTest::GYRO_RAW_MAX = 2097152;
3195const int32_t SensorInputMapperTest::GYRO_RAW_FUZZ = 16;
3196const int32_t SensorInputMapperTest::GYRO_RAW_FLAT = 0;
3197const int32_t SensorInputMapperTest::GYRO_RAW_RESOLUTION = 1024;
3198
3199const float SensorInputMapperTest::GRAVITY_MS2_UNIT = 9.80665f;
3200const float SensorInputMapperTest::DEGREE_RADIAN_UNIT = 0.0174533f;
3201
3202void SensorInputMapperTest::prepareAccelAxes() {
3203 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3204 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3205 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3206 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3207 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Z, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3208 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3209}
3210
3211void SensorInputMapperTest::prepareGyroAxes() {
3212 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RX, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3213 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3214 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RY, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3215 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3216 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RZ, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3217 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3218}
3219
3220void SensorInputMapperTest::setAccelProperties() {
3221 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 0, InputDeviceSensorType::ACCELEROMETER,
3222 /* sensorDataIndex */ 0);
3223 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 1, InputDeviceSensorType::ACCELEROMETER,
3224 /* sensorDataIndex */ 1);
3225 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 2, InputDeviceSensorType::ACCELEROMETER,
3226 /* sensorDataIndex */ 2);
3227 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
3228 addConfigurationProperty("sensor.accelerometer.reportingMode", "0");
3229 addConfigurationProperty("sensor.accelerometer.maxDelay", "100000");
3230 addConfigurationProperty("sensor.accelerometer.minDelay", "5000");
3231 addConfigurationProperty("sensor.accelerometer.power", "1.5");
3232}
3233
3234void SensorInputMapperTest::setGyroProperties() {
3235 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 3, InputDeviceSensorType::GYROSCOPE,
3236 /* sensorDataIndex */ 0);
3237 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 4, InputDeviceSensorType::GYROSCOPE,
3238 /* sensorDataIndex */ 1);
3239 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 5, InputDeviceSensorType::GYROSCOPE,
3240 /* sensorDataIndex */ 2);
3241 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
3242 addConfigurationProperty("sensor.gyroscope.reportingMode", "0");
3243 addConfigurationProperty("sensor.gyroscope.maxDelay", "100000");
3244 addConfigurationProperty("sensor.gyroscope.minDelay", "5000");
3245 addConfigurationProperty("sensor.gyroscope.power", "0.8");
3246}
3247
3248TEST_F(SensorInputMapperTest, GetSources) {
3249 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3250
3251 ASSERT_EQ(static_cast<uint32_t>(AINPUT_SOURCE_SENSOR), mapper.getSources());
3252}
3253
3254TEST_F(SensorInputMapperTest, ProcessAccelerometerSensor) {
3255 setAccelProperties();
3256 prepareAccelAxes();
3257 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3258
3259 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::ACCELEROMETER,
3260 std::chrono::microseconds(10000),
3261 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08003262 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003263 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, 20000);
3264 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, -20000);
3265 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Z, 40000);
3266 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
3267 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07003268
3269 NotifySensorArgs args;
3270 std::vector<float> values = {20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
3271 -20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
3272 40000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT};
3273
3274 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
3275 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
3276 ASSERT_EQ(args.deviceId, DEVICE_ID);
3277 ASSERT_EQ(args.sensorType, InputDeviceSensorType::ACCELEROMETER);
3278 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
3279 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
3280 ASSERT_EQ(args.values, values);
3281 mapper.flushSensor(InputDeviceSensorType::ACCELEROMETER);
3282}
3283
3284TEST_F(SensorInputMapperTest, ProcessGyroscopeSensor) {
3285 setGyroProperties();
3286 prepareGyroAxes();
3287 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3288
3289 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::GYROSCOPE,
3290 std::chrono::microseconds(10000),
3291 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08003292 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003293 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RX, 20000);
3294 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RY, -20000);
3295 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RZ, 40000);
3296 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
3297 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07003298
3299 NotifySensorArgs args;
3300 std::vector<float> values = {20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
3301 -20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
3302 40000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT};
3303
3304 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
3305 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
3306 ASSERT_EQ(args.deviceId, DEVICE_ID);
3307 ASSERT_EQ(args.sensorType, InputDeviceSensorType::GYROSCOPE);
3308 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
3309 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
3310 ASSERT_EQ(args.values, values);
3311 mapper.flushSensor(InputDeviceSensorType::GYROSCOPE);
3312}
3313
Michael Wrightd02c5b62014-02-10 15:10:22 -08003314// --- KeyboardInputMapperTest ---
3315
3316class KeyboardInputMapperTest : public InputMapperTest {
3317protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003318 const std::string UNIQUE_ID = "local:0";
3319
3320 void prepareDisplay(int32_t orientation);
3321
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003322 void testDPadKeyRotation(KeyboardInputMapper& mapper, int32_t originalScanCode,
Arthur Hung2c9a3342019-07-23 14:18:59 +08003323 int32_t originalKeyCode, int32_t rotatedKeyCode,
3324 int32_t displayId = ADISPLAY_ID_NONE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003325};
3326
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003327/* Similar to setDisplayInfoAndReconfigure, but pre-populates all parameters except for the
3328 * orientation.
3329 */
3330void KeyboardInputMapperTest::prepareDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003331 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
3332 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003333}
3334
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003335void KeyboardInputMapperTest::testDPadKeyRotation(KeyboardInputMapper& mapper,
Arthur Hung2c9a3342019-07-23 14:18:59 +08003336 int32_t originalScanCode, int32_t originalKeyCode,
3337 int32_t rotatedKeyCode, int32_t displayId) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003338 NotifyKeyArgs args;
3339
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003340 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003341 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3342 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3343 ASSERT_EQ(originalScanCode, args.scanCode);
3344 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003345 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003346
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003347 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003348 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3349 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3350 ASSERT_EQ(originalScanCode, args.scanCode);
3351 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003352 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003353}
3354
Michael Wrightd02c5b62014-02-10 15:10:22 -08003355TEST_F(KeyboardInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003356 KeyboardInputMapper& mapper =
3357 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3358 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003359
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003360 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003361}
3362
3363TEST_F(KeyboardInputMapperTest, Process_SimpleKeyPress) {
3364 const int32_t USAGE_A = 0x070004;
3365 const int32_t USAGE_UNKNOWN = 0x07ffff;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003366 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3367 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
Chris Yea52ade12020-08-27 16:49:20 -07003368 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, POLICY_FLAG_WAKE);
3369 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, POLICY_FLAG_WAKE);
3370 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003371
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003372 KeyboardInputMapper& mapper =
3373 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3374 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003375 // Initial metastate is AMETA_NONE.
3376 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003377
3378 // Key down by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003379 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003380 NotifyKeyArgs args;
3381 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3382 ASSERT_EQ(DEVICE_ID, args.deviceId);
3383 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3384 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3385 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3386 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3387 ASSERT_EQ(KEY_HOME, args.scanCode);
3388 ASSERT_EQ(AMETA_NONE, args.metaState);
3389 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3390 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3391 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3392
3393 // Key up by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003394 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003395 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3396 ASSERT_EQ(DEVICE_ID, args.deviceId);
3397 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3398 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3399 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3400 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3401 ASSERT_EQ(KEY_HOME, args.scanCode);
3402 ASSERT_EQ(AMETA_NONE, args.metaState);
3403 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3404 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3405 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3406
3407 // Key down by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003408 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3409 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, 0, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003410 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3411 ASSERT_EQ(DEVICE_ID, args.deviceId);
3412 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3413 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3414 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3415 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3416 ASSERT_EQ(0, args.scanCode);
3417 ASSERT_EQ(AMETA_NONE, args.metaState);
3418 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3419 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3420 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3421
3422 // Key up by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003423 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3424 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003425 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3426 ASSERT_EQ(DEVICE_ID, args.deviceId);
3427 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3428 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3429 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3430 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3431 ASSERT_EQ(0, args.scanCode);
3432 ASSERT_EQ(AMETA_NONE, args.metaState);
3433 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3434 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3435 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3436
3437 // Key down with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003438 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3439 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UNKNOWN, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003440 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3441 ASSERT_EQ(DEVICE_ID, args.deviceId);
3442 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3443 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3444 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3445 ASSERT_EQ(0, args.keyCode);
3446 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3447 ASSERT_EQ(AMETA_NONE, args.metaState);
3448 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3449 ASSERT_EQ(0U, args.policyFlags);
3450 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3451
3452 // Key up with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003453 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3454 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003455 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3456 ASSERT_EQ(DEVICE_ID, args.deviceId);
3457 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3458 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3459 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3460 ASSERT_EQ(0, args.keyCode);
3461 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3462 ASSERT_EQ(AMETA_NONE, args.metaState);
3463 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3464 ASSERT_EQ(0U, args.policyFlags);
3465 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3466}
3467
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003468/**
3469 * Ensure that the readTime is set to the time when the EV_KEY is received.
3470 */
3471TEST_F(KeyboardInputMapperTest, Process_SendsReadTime) {
3472 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3473
3474 KeyboardInputMapper& mapper =
3475 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3476 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3477 NotifyKeyArgs args;
3478
3479 // Key down
3480 process(mapper, ARBITRARY_TIME, 12 /*readTime*/, EV_KEY, KEY_HOME, 1);
3481 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3482 ASSERT_EQ(12, args.readTime);
3483
3484 // Key up
3485 process(mapper, ARBITRARY_TIME, 15 /*readTime*/, EV_KEY, KEY_HOME, 1);
3486 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3487 ASSERT_EQ(15, args.readTime);
3488}
3489
Michael Wrightd02c5b62014-02-10 15:10:22 -08003490TEST_F(KeyboardInputMapperTest, Process_ShouldUpdateMetaState) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003491 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFTSHIFT, 0, AKEYCODE_SHIFT_LEFT, 0);
3492 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003493 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, 0);
3494 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, 0);
3495 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003496
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003497 KeyboardInputMapper& mapper =
3498 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3499 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003500
Arthur Hung95f68612022-04-07 14:08:22 +08003501 // Initial metastate is AMETA_NONE.
3502 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003503
3504 // Metakey down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003505 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003506 NotifyKeyArgs args;
3507 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3508 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003509 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003510 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003511
3512 // Key down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003513 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_A, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003514 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3515 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003516 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003517
3518 // Key up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003519 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, KEY_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003520 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3521 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003522 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003523
3524 // Metakey up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003525 process(mapper, ARBITRARY_TIME + 3, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003526 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3527 ASSERT_EQ(AMETA_NONE, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003528 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003529 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003530}
3531
3532TEST_F(KeyboardInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003533 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3534 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3535 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3536 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003537
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003538 KeyboardInputMapper& mapper =
3539 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3540 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003541
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003542 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003543 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3544 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
3545 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3546 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
3547 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3548 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
3549 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3550 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
3551}
3552
3553TEST_F(KeyboardInputMapperTest, Process_WhenOrientationAware_ShouldRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003554 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3555 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3556 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3557 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003558
Michael Wrightd02c5b62014-02-10 15:10:22 -08003559 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003560 KeyboardInputMapper& mapper =
3561 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3562 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003563
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003564 prepareDisplay(DISPLAY_ORIENTATION_0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003565 ASSERT_NO_FATAL_FAILURE(
3566 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3567 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3568 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3569 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3570 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3571 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3572 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003573
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003574 clearViewports();
3575 prepareDisplay(DISPLAY_ORIENTATION_90);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003576 ASSERT_NO_FATAL_FAILURE(
3577 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3578 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3579 AKEYCODE_DPAD_UP, DISPLAY_ID));
3580 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3581 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3582 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3583 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003584
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003585 clearViewports();
3586 prepareDisplay(DISPLAY_ORIENTATION_180);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003587 ASSERT_NO_FATAL_FAILURE(
3588 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3589 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3590 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3591 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3592 AKEYCODE_DPAD_UP, DISPLAY_ID));
3593 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3594 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003595
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003596 clearViewports();
3597 prepareDisplay(DISPLAY_ORIENTATION_270);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003598 ASSERT_NO_FATAL_FAILURE(
3599 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3600 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3601 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3602 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3603 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3604 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3605 AKEYCODE_DPAD_UP, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003606
3607 // Special case: if orientation changes while key is down, we still emit the same keycode
3608 // in the key up as we did in the key down.
3609 NotifyKeyArgs args;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003610 clearViewports();
3611 prepareDisplay(DISPLAY_ORIENTATION_270);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003612 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003613 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3614 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3615 ASSERT_EQ(KEY_UP, args.scanCode);
3616 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3617
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003618 clearViewports();
3619 prepareDisplay(DISPLAY_ORIENTATION_180);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003620 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003621 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3622 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3623 ASSERT_EQ(KEY_UP, args.scanCode);
3624 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3625}
3626
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003627TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_NotOrientationAware) {
3628 // If the keyboard is not orientation aware,
3629 // key events should not be associated with a specific display id
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003630 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003631
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003632 KeyboardInputMapper& mapper =
3633 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3634 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003635 NotifyKeyArgs args;
3636
3637 // Display id should be ADISPLAY_ID_NONE without any display configuration.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003638 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003639 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003640 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003641 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3642 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3643
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003644 prepareDisplay(DISPLAY_ORIENTATION_0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003645 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003646 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003647 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003648 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3649 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3650}
3651
3652TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_OrientationAware) {
3653 // If the keyboard is orientation aware,
3654 // key events should be associated with the internal viewport
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003655 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003656
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003657 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003658 KeyboardInputMapper& mapper =
3659 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3660 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003661 NotifyKeyArgs args;
3662
3663 // Display id should be ADISPLAY_ID_NONE without any display configuration.
3664 // ^--- already checked by the previous test
3665
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003666 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003667 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003668 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003669 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003670 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003671 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3672 ASSERT_EQ(DISPLAY_ID, args.displayId);
3673
3674 constexpr int32_t newDisplayId = 2;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003675 clearViewports();
3676 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003677 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003678 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003679 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003680 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003681 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3682 ASSERT_EQ(newDisplayId, args.displayId);
3683}
3684
Michael Wrightd02c5b62014-02-10 15:10:22 -08003685TEST_F(KeyboardInputMapperTest, GetKeyCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003686 KeyboardInputMapper& mapper =
3687 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3688 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003689
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003690 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003691 ASSERT_EQ(1, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003692
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003693 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003694 ASSERT_EQ(0, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003695}
3696
Philip Junker4af3b3d2021-12-14 10:36:55 +01003697TEST_F(KeyboardInputMapperTest, GetKeyCodeForKeyLocation) {
3698 KeyboardInputMapper& mapper =
3699 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3700 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3701
3702 mFakeEventHub->addKeyCodeMapping(EVENTHUB_ID, AKEYCODE_Y, AKEYCODE_Z);
3703 ASSERT_EQ(AKEYCODE_Z, mapper.getKeyCodeForKeyLocation(AKEYCODE_Y))
3704 << "If a mapping is available, the result is equal to the mapping";
3705
3706 ASSERT_EQ(AKEYCODE_A, mapper.getKeyCodeForKeyLocation(AKEYCODE_A))
3707 << "If no mapping is available, the result is the key location";
3708}
3709
Michael Wrightd02c5b62014-02-10 15:10:22 -08003710TEST_F(KeyboardInputMapperTest, GetScanCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003711 KeyboardInputMapper& mapper =
3712 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3713 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003714
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003715 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003716 ASSERT_EQ(1, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003717
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003718 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003719 ASSERT_EQ(0, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003720}
3721
3722TEST_F(KeyboardInputMapperTest, MarkSupportedKeyCodes) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003723 KeyboardInputMapper& mapper =
3724 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3725 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003726
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003727 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003728
3729 const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
3730 uint8_t flags[2] = { 0, 0 };
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003731 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, 1, keyCodes, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003732 ASSERT_TRUE(flags[0]);
3733 ASSERT_FALSE(flags[1]);
3734}
3735
3736TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleMetaStateAndLeds) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003737 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3738 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3739 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3740 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3741 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3742 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003743
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003744 KeyboardInputMapper& mapper =
3745 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3746 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003747 // Initial metastate is AMETA_NONE.
3748 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003749
3750 // Initialization should have turned all of the lights off.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003751 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3752 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3753 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003754
3755 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003756 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3757 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003758 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3759 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3760 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003761 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003762
3763 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003764 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3765 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003766 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3767 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3768 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003769 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003770
3771 // Toggle caps lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003772 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3773 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003774 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3775 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3776 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003777 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003778
3779 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003780 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3781 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003782 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3783 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3784 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003785 ASSERT_EQ(AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003786
3787 // Toggle num lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003788 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3789 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003790 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3791 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3792 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003793 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003794
3795 // Toggle scroll lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003796 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3797 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003798 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3799 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3800 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003801 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003802}
3803
Chris Yea52ade12020-08-27 16:49:20 -07003804TEST_F(KeyboardInputMapperTest, NoMetaStateWhenMetaKeysNotPresent) {
3805 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_BUTTON_A, 0);
3806 mFakeEventHub->addKey(EVENTHUB_ID, BTN_B, 0, AKEYCODE_BUTTON_B, 0);
3807 mFakeEventHub->addKey(EVENTHUB_ID, BTN_X, 0, AKEYCODE_BUTTON_X, 0);
3808 mFakeEventHub->addKey(EVENTHUB_ID, BTN_Y, 0, AKEYCODE_BUTTON_Y, 0);
3809
3810 KeyboardInputMapper& mapper =
3811 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3812 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
3813
Chris Yea52ade12020-08-27 16:49:20 -07003814 // Meta state should be AMETA_NONE after reset
3815 mapper.reset(ARBITRARY_TIME);
3816 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3817 // Meta state should be AMETA_NONE with update, as device doesn't have the keys.
3818 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
3819 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3820
3821 NotifyKeyArgs args;
3822 // Press button "A"
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003823 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_A, 1);
Chris Yea52ade12020-08-27 16:49:20 -07003824 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3825 ASSERT_EQ(AMETA_NONE, args.metaState);
3826 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3827 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3828 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3829
3830 // Button up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003831 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003832 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3833 ASSERT_EQ(AMETA_NONE, args.metaState);
3834 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3835 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3836 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3837}
3838
Arthur Hung2c9a3342019-07-23 14:18:59 +08003839TEST_F(KeyboardInputMapperTest, Configure_AssignsDisplayPort) {
3840 // keyboard 1.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003841 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3842 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3843 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3844 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003845
3846 // keyboard 2.
3847 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08003848 const std::string DEVICE_NAME2 = "KEYBOARD2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08003849 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003850 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08003851 std::shared_ptr<InputDevice> device2 =
3852 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003853 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08003854
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003855 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3856 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3857 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3858 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003859
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003860 KeyboardInputMapper& mapper =
3861 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3862 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003863
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003864 KeyboardInputMapper& mapper2 =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003865 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003866 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003867 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
3868 device2->reset(ARBITRARY_TIME);
3869
3870 // Prepared displays and associated info.
3871 constexpr uint8_t hdmi1 = 0;
3872 constexpr uint8_t hdmi2 = 1;
3873 const std::string SECONDARY_UNIQUE_ID = "local:1";
3874
3875 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
3876 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
3877
3878 // No associated display viewport found, should disable the device.
3879 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3880 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3881 ASSERT_FALSE(device2->isEnabled());
3882
3883 // Prepare second display.
3884 constexpr int32_t newDisplayId = 2;
3885 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003886 UNIQUE_ID, hdmi1, ViewportType::INTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003887 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003888 SECONDARY_UNIQUE_ID, hdmi2, ViewportType::EXTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003889 // Default device will reconfigure above, need additional reconfiguration for another device.
3890 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3891 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3892
3893 // Device should be enabled after the associated display is found.
3894 ASSERT_TRUE(mDevice->isEnabled());
3895 ASSERT_TRUE(device2->isEnabled());
3896
3897 // Test pad key events
3898 ASSERT_NO_FATAL_FAILURE(
3899 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3900 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3901 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3902 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3903 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3904 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3905 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3906
3907 ASSERT_NO_FATAL_FAILURE(
3908 testDPadKeyRotation(mapper2, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, newDisplayId));
3909 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3910 AKEYCODE_DPAD_RIGHT, newDisplayId));
3911 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3912 AKEYCODE_DPAD_DOWN, newDisplayId));
3913 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3914 AKEYCODE_DPAD_LEFT, newDisplayId));
3915}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003916
arthurhungc903df12020-08-11 15:08:42 +08003917TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleAfterReattach) {
3918 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3919 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3920 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3921 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3922 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3923 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3924
3925 KeyboardInputMapper& mapper =
3926 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3927 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003928 // Initial metastate is AMETA_NONE.
3929 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08003930
3931 // Initialization should have turned all of the lights off.
3932 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3933 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3934 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3935
3936 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003937 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3938 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003939 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3940 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
3941
3942 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003943 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3944 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003945 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3946 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
3947
3948 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003949 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3950 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003951 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3952 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
3953
3954 mFakeEventHub->removeDevice(EVENTHUB_ID);
3955 mReader->loopOnce();
3956
3957 // keyboard 2 should default toggle keys.
3958 const std::string USB2 = "USB2";
3959 const std::string DEVICE_NAME2 = "KEYBOARD2";
3960 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
3961 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
3962 std::shared_ptr<InputDevice> device2 =
3963 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003964 ftl::Flags<InputDeviceClass>(0));
arthurhungc903df12020-08-11 15:08:42 +08003965 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3966 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
3967 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3968 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3969 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3970 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3971
arthurhung6fe95782020-10-05 22:41:16 +08003972 KeyboardInputMapper& mapper2 =
3973 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
3974 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
arthurhungc903df12020-08-11 15:08:42 +08003975 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
3976 device2->reset(ARBITRARY_TIME);
3977
3978 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_CAPSL));
3979 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_NUML));
3980 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_SCROLLL));
arthurhung6fe95782020-10-05 22:41:16 +08003981 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON,
3982 mapper2.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08003983}
3984
Arthur Hungcb40a002021-08-03 14:31:01 +00003985TEST_F(KeyboardInputMapperTest, Process_toggleCapsLockState) {
3986 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3987 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3988 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3989
3990 // Suppose we have two mappers. (DPAD + KEYBOARD)
3991 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_DPAD,
3992 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
3993 KeyboardInputMapper& mapper =
3994 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3995 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003996 // Initial metastate is AMETA_NONE.
3997 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Arthur Hungcb40a002021-08-03 14:31:01 +00003998
3999 mReader->toggleCapsLockState(DEVICE_ID);
4000 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
4001}
4002
Arthur Hungfb3cc112022-04-13 07:39:50 +00004003TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleInMultiDevices) {
4004 // keyboard 1.
4005 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
4006 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
4007 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
4008 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4009 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4010 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
4011
4012 KeyboardInputMapper& mapper1 =
4013 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4014 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
4015
4016 // keyboard 2.
4017 const std::string USB2 = "USB2";
4018 const std::string DEVICE_NAME2 = "KEYBOARD2";
4019 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
4020 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
4021 std::shared_ptr<InputDevice> device2 =
4022 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
4023 ftl::Flags<InputDeviceClass>(0));
4024 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
4025 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
4026 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
4027 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4028 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4029 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
4030
4031 KeyboardInputMapper& mapper2 =
4032 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
4033 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
4034 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
4035 device2->reset(ARBITRARY_TIME);
4036
Arthur Hung95f68612022-04-07 14:08:22 +08004037 // Initial metastate is AMETA_NONE.
4038 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4039 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4040
4041 // Toggle num lock on and off.
4042 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
4043 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Arthur Hungfb3cc112022-04-13 07:39:50 +00004044 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4045 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper1.getMetaState());
4046 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper2.getMetaState());
4047
4048 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
4049 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
4050 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4051 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4052 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4053
4054 // Toggle caps lock on and off.
4055 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
4056 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
4057 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4058 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper1.getMetaState());
4059 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper2.getMetaState());
4060
4061 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
4062 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
4063 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4064 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4065 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4066
4067 // Toggle scroll lock on and off.
4068 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
4069 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
4070 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
4071 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper1.getMetaState());
4072 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper2.getMetaState());
4073
4074 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
4075 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
4076 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
4077 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4078 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4079}
4080
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004081// --- KeyboardInputMapperTest_ExternalDevice ---
4082
4083class KeyboardInputMapperTest_ExternalDevice : public InputMapperTest {
4084protected:
Chris Yea52ade12020-08-27 16:49:20 -07004085 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004086};
4087
4088TEST_F(KeyboardInputMapperTest_ExternalDevice, WakeBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07004089 // For external devices, non-media keys will trigger wake on key down. Media keys need to be
4090 // marked as WAKE in the keylayout file to trigger wake.
Powei Fengd041c5d2019-05-03 17:11:33 -07004091
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004092 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, 0);
4093 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, 0);
4094 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAYPAUSE, 0, AKEYCODE_MEDIA_PLAY_PAUSE,
4095 POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07004096
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004097 KeyboardInputMapper& mapper =
4098 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4099 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07004100
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004101 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004102 NotifyKeyArgs args;
4103 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4104 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4105
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004106 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004107 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4108 ASSERT_EQ(uint32_t(0), args.policyFlags);
4109
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004110 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004111 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4112 ASSERT_EQ(uint32_t(0), args.policyFlags);
4113
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004114 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004115 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4116 ASSERT_EQ(uint32_t(0), args.policyFlags);
4117
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004118 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004119 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4120 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4121
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004122 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004123 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4124 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4125}
4126
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004127TEST_F(KeyboardInputMapperTest_ExternalDevice, DoNotWakeByDefaultBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07004128 // Tv Remote key's wake behavior is prescribed by the keylayout file.
Powei Fengd041c5d2019-05-03 17:11:33 -07004129
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004130 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4131 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
4132 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07004133
Powei Fengd041c5d2019-05-03 17:11:33 -07004134 addConfigurationProperty("keyboard.doNotWakeByDefault", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004135 KeyboardInputMapper& mapper =
4136 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4137 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07004138
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004139 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004140 NotifyKeyArgs args;
4141 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4142 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4143
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004144 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004145 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4146 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4147
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004148 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_DOWN, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004149 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4150 ASSERT_EQ(uint32_t(0), args.policyFlags);
4151
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004152 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_DOWN, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004153 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4154 ASSERT_EQ(uint32_t(0), args.policyFlags);
4155
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004156 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004157 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4158 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4159
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004160 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004161 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4162 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4163}
4164
Michael Wrightd02c5b62014-02-10 15:10:22 -08004165// --- CursorInputMapperTest ---
4166
4167class CursorInputMapperTest : public InputMapperTest {
4168protected:
4169 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD;
4170
Michael Wright17db18e2020-06-26 20:51:44 +01004171 std::shared_ptr<FakePointerController> mFakePointerController;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004172
Chris Yea52ade12020-08-27 16:49:20 -07004173 void SetUp() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004174 InputMapperTest::SetUp();
4175
Michael Wright17db18e2020-06-26 20:51:44 +01004176 mFakePointerController = std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00004177 mFakePolicy->setPointerController(mFakePointerController);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004178 }
4179
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004180 void testMotionRotation(CursorInputMapper& mapper, int32_t originalX, int32_t originalY,
4181 int32_t rotatedX, int32_t rotatedY);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004182
4183 void prepareDisplay(int32_t orientation) {
4184 const std::string uniqueId = "local:0";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004185 const ViewportType viewportType = ViewportType::INTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004186 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
4187 orientation, uniqueId, NO_PORT, viewportType);
4188 }
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004189
4190 static void assertCursorPointerCoords(const PointerCoords& coords, float x, float y,
4191 float pressure) {
4192 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(coords, x, y, pressure, 0.0f, 0.0f, 0.0f, 0.0f,
4193 0.0f, 0.0f, 0.0f, EPSILON));
4194 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08004195};
4196
4197const int32_t CursorInputMapperTest::TRACKBALL_MOVEMENT_THRESHOLD = 6;
4198
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004199void CursorInputMapperTest::testMotionRotation(CursorInputMapper& mapper, int32_t originalX,
4200 int32_t originalY, int32_t rotatedX,
4201 int32_t rotatedY) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004202 NotifyMotionArgs args;
4203
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004204 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, originalX);
4205 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, originalY);
4206 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004207 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4208 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004209 ASSERT_NO_FATAL_FAILURE(
4210 assertCursorPointerCoords(args.pointerCoords[0],
4211 float(rotatedX) / TRACKBALL_MOVEMENT_THRESHOLD,
4212 float(rotatedY) / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004213}
4214
4215TEST_F(CursorInputMapperTest, WhenModeIsPointer_GetSources_ReturnsMouse) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004216 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004217 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004218
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004219 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004220}
4221
4222TEST_F(CursorInputMapperTest, WhenModeIsNavigation_GetSources_ReturnsTrackball) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004223 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004224 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004225
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004226 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004227}
4228
4229TEST_F(CursorInputMapperTest, WhenModeIsPointer_PopulateDeviceInfo_ReturnsRangeFromPointerController) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004230 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004231 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004232
4233 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004234 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004235
4236 // Initially there may not be a valid motion range.
Yi Kong9b14ac62018-07-17 13:48:38 -07004237 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE));
4238 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004239 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4240 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE, 0.0f, 1.0f, 0.0f, 0.0f));
4241
4242 // When the bounds are set, then there should be a valid motion range.
4243 mFakePointerController->setBounds(1, 2, 800 - 1, 480 - 1);
4244
4245 InputDeviceInfo info2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004246 mapper.populateDeviceInfo(&info2);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004247
4248 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
4249 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE,
4250 1, 800 - 1, 0.0f, 0.0f));
4251 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
4252 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE,
4253 2, 480 - 1, 0.0f, 0.0f));
4254 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
4255 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE,
4256 0.0f, 1.0f, 0.0f, 0.0f));
4257}
4258
4259TEST_F(CursorInputMapperTest, WhenModeIsNavigation_PopulateDeviceInfo_ReturnsScaledRange) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004260 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004261 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004262
4263 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004264 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004265
4266 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4267 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_TRACKBALL,
4268 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
4269 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4270 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_TRACKBALL,
4271 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
4272 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4273 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TRACKBALL,
4274 0.0f, 1.0f, 0.0f, 0.0f));
4275}
4276
4277TEST_F(CursorInputMapperTest, Process_ShouldSetAllFieldsAndIncludeGlobalMetaState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004278 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004279 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004280
arthurhungdcef2dc2020-08-11 14:47:50 +08004281 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004282
4283 NotifyMotionArgs args;
4284
4285 // Button press.
4286 // Mostly testing non x/y behavior here so we don't need to check again elsewhere.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004287 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4288 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004289 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4290 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4291 ASSERT_EQ(DEVICE_ID, args.deviceId);
4292 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4293 ASSERT_EQ(uint32_t(0), args.policyFlags);
4294 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
4295 ASSERT_EQ(0, args.flags);
4296 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4297 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
4298 ASSERT_EQ(0, args.edgeFlags);
4299 ASSERT_EQ(uint32_t(1), args.pointerCount);
4300 ASSERT_EQ(0, args.pointerProperties[0].id);
4301 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004302 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004303 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4304 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4305 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4306
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004307 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4308 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4309 ASSERT_EQ(DEVICE_ID, args.deviceId);
4310 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4311 ASSERT_EQ(uint32_t(0), args.policyFlags);
4312 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
4313 ASSERT_EQ(0, args.flags);
4314 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4315 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
4316 ASSERT_EQ(0, args.edgeFlags);
4317 ASSERT_EQ(uint32_t(1), args.pointerCount);
4318 ASSERT_EQ(0, args.pointerProperties[0].id);
4319 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004320 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004321 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4322 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4323 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4324
Michael Wrightd02c5b62014-02-10 15:10:22 -08004325 // Button release. Should have same down time.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004326 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4327 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004328 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4329 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
4330 ASSERT_EQ(DEVICE_ID, args.deviceId);
4331 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4332 ASSERT_EQ(uint32_t(0), args.policyFlags);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004333 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
4334 ASSERT_EQ(0, args.flags);
4335 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4336 ASSERT_EQ(0, args.buttonState);
4337 ASSERT_EQ(0, args.edgeFlags);
4338 ASSERT_EQ(uint32_t(1), args.pointerCount);
4339 ASSERT_EQ(0, args.pointerProperties[0].id);
4340 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004341 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004342 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4343 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4344 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4345
4346 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4347 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
4348 ASSERT_EQ(DEVICE_ID, args.deviceId);
4349 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4350 ASSERT_EQ(uint32_t(0), args.policyFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004351 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
4352 ASSERT_EQ(0, args.flags);
4353 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4354 ASSERT_EQ(0, args.buttonState);
4355 ASSERT_EQ(0, args.edgeFlags);
4356 ASSERT_EQ(uint32_t(1), args.pointerCount);
4357 ASSERT_EQ(0, args.pointerProperties[0].id);
4358 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004359 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004360 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4361 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4362 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4363}
4364
4365TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentXYUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004366 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004367 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004368
4369 NotifyMotionArgs args;
4370
4371 // Motion in X but not Y.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004372 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4373 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004374 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4375 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004376 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4377 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f,
4378 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004379
4380 // Motion in Y but not X.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004381 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
4382 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004383 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4384 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004385 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f,
4386 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004387}
4388
4389TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004390 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004391 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004392
4393 NotifyMotionArgs args;
4394
4395 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004396 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4397 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004398 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4399 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004400 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004401
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004402 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4403 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004404 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004405
Michael Wrightd02c5b62014-02-10 15:10:22 -08004406 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004407 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4408 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004409 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004410 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004411 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004412
4413 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004414 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004415 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004416}
4417
4418TEST_F(CursorInputMapperTest, Process_ShouldHandleCombinedXYAndButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004419 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004420 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004421
4422 NotifyMotionArgs args;
4423
4424 // Combined X, Y and Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004425 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4426 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
4427 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4428 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004429 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4430 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004431 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4432 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4433 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004434
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004435 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4436 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004437 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4438 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4439 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004440
Michael Wrightd02c5b62014-02-10 15:10:22 -08004441 // Move X, Y a bit while pressed.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004442 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 2);
4443 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 1);
4444 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004445 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4446 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004447 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4448 2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4449 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004450
4451 // Release Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004452 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4453 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004454 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004455 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004456 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004457
4458 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004459 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004460 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004461}
4462
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004463TEST_F(CursorInputMapperTest, Process_WhenOrientationAware_ShouldNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004464 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004465 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
4466 // need to be rotated.
4467 addConfigurationProperty("cursor.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004468 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004469
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004470 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004471 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4472 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4473 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4474 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4475 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4476 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4477 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4478 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4479}
4480
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004481TEST_F(CursorInputMapperTest, Process_WhenNotOrientationAware_ShouldRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004482 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004483 // Since InputReader works in the un-rotated coordinate space, only devices that are not
4484 // orientation-aware are affected by display rotation.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004485 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004486
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004487 prepareDisplay(DISPLAY_ORIENTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004488 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4489 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4490 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4491 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4492 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4493 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4494 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4495 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4496
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004497 prepareDisplay(DISPLAY_ORIENTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004498 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, -1, 0));
4499 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, 1));
4500 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, 1));
4501 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, 1));
4502 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 1, 0));
4503 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, -1));
4504 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, -1));
4505 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, -1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004506
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004507 prepareDisplay(DISPLAY_ORIENTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004508 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, -1));
4509 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, -1));
4510 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, -1, 0));
4511 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, 1));
4512 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, 1));
4513 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, 1));
4514 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 1, 0));
4515 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, -1));
4516
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004517 prepareDisplay(DISPLAY_ORIENTATION_270);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004518 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 1, 0));
4519 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, -1));
4520 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, -1));
4521 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, -1));
4522 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, -1, 0));
4523 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, 1));
4524 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, 1));
4525 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, 1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004526}
4527
4528TEST_F(CursorInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004529 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004530 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004531
4532 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4533 mFakePointerController->setPosition(100, 200);
4534 mFakePointerController->setButtonState(0);
4535
4536 NotifyMotionArgs motionArgs;
4537 NotifyKeyArgs keyArgs;
4538
4539 // press BTN_LEFT, release BTN_LEFT
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004540 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 1);
4541 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004542 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4543 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4544 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4545 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004546 ASSERT_NO_FATAL_FAILURE(
4547 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004548
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004549 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4550 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4551 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4552 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004553 ASSERT_NO_FATAL_FAILURE(
4554 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004555
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004556 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 0);
4557 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004558 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004559 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004560 ASSERT_EQ(0, motionArgs.buttonState);
4561 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004562 ASSERT_NO_FATAL_FAILURE(
4563 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004564
4565 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004566 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004567 ASSERT_EQ(0, motionArgs.buttonState);
4568 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004569 ASSERT_NO_FATAL_FAILURE(
4570 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004571
4572 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004573 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004574 ASSERT_EQ(0, motionArgs.buttonState);
4575 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004576 ASSERT_NO_FATAL_FAILURE(
4577 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004578
4579 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004580 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 1);
4581 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 1);
4582 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004583 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4584 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4585 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4586 motionArgs.buttonState);
4587 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4588 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004589 ASSERT_NO_FATAL_FAILURE(
4590 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004591
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004592 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4593 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4594 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4595 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4596 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004597 ASSERT_NO_FATAL_FAILURE(
4598 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004599
4600 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4601 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4602 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4603 motionArgs.buttonState);
4604 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4605 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004606 ASSERT_NO_FATAL_FAILURE(
4607 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004608
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004609 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 0);
4610 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004611 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004612 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004613 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4614 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004615 ASSERT_NO_FATAL_FAILURE(
4616 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004617
4618 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004619 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004620 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4621 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004622 ASSERT_NO_FATAL_FAILURE(
4623 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004624
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004625 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4626 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004627 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004628 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
4629 ASSERT_EQ(0, motionArgs.buttonState);
4630 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004631 ASSERT_NO_FATAL_FAILURE(
4632 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004633 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4634 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004635
4636 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004637 ASSERT_EQ(0, motionArgs.buttonState);
4638 ASSERT_EQ(0, mFakePointerController->getButtonState());
4639 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004640 ASSERT_NO_FATAL_FAILURE(
4641 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004642
Michael Wrightd02c5b62014-02-10 15:10:22 -08004643 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4644 ASSERT_EQ(0, motionArgs.buttonState);
4645 ASSERT_EQ(0, mFakePointerController->getButtonState());
4646 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004647 ASSERT_NO_FATAL_FAILURE(
4648 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004649
4650 // press BTN_BACK, release BTN_BACK
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004651 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 1);
4652 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004653 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4654 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4655 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004656
Michael Wrightd02c5b62014-02-10 15:10:22 -08004657 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004658 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004659 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4660 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004661 ASSERT_NO_FATAL_FAILURE(
4662 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004663
4664 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4665 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4666 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4667 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004668 ASSERT_NO_FATAL_FAILURE(
4669 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004670
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004671 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 0);
4672 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004673 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004674 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004675 ASSERT_EQ(0, motionArgs.buttonState);
4676 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004677 ASSERT_NO_FATAL_FAILURE(
4678 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004679
4680 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004681 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004682 ASSERT_EQ(0, motionArgs.buttonState);
4683 ASSERT_EQ(0, mFakePointerController->getButtonState());
4684
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004685 ASSERT_NO_FATAL_FAILURE(
4686 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004687 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4688 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4689 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4690
4691 // press BTN_SIDE, release BTN_SIDE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004692 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 1);
4693 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004694 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4695 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4696 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004697
Michael Wrightd02c5b62014-02-10 15:10:22 -08004698 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004699 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004700 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4701 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004702 ASSERT_NO_FATAL_FAILURE(
4703 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004704
4705 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4706 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4707 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4708 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004709 ASSERT_NO_FATAL_FAILURE(
4710 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004711
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004712 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 0);
4713 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004714 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004715 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004716 ASSERT_EQ(0, motionArgs.buttonState);
4717 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004718 ASSERT_NO_FATAL_FAILURE(
4719 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004720
4721 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4722 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4723 ASSERT_EQ(0, motionArgs.buttonState);
4724 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004725 ASSERT_NO_FATAL_FAILURE(
4726 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004727
Michael Wrightd02c5b62014-02-10 15:10:22 -08004728 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4729 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4730 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4731
4732 // press BTN_FORWARD, release BTN_FORWARD
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004733 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 1);
4734 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004735 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4736 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4737 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004738
Michael Wrightd02c5b62014-02-10 15:10:22 -08004739 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004740 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004741 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4742 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004743 ASSERT_NO_FATAL_FAILURE(
4744 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004745
4746 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4747 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4748 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4749 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004750 ASSERT_NO_FATAL_FAILURE(
4751 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004752
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004753 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 0);
4754 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004755 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004756 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004757 ASSERT_EQ(0, motionArgs.buttonState);
4758 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004759 ASSERT_NO_FATAL_FAILURE(
4760 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004761
4762 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4763 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4764 ASSERT_EQ(0, motionArgs.buttonState);
4765 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004766 ASSERT_NO_FATAL_FAILURE(
4767 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004768
Michael Wrightd02c5b62014-02-10 15:10:22 -08004769 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4770 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4771 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4772
4773 // press BTN_EXTRA, release BTN_EXTRA
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004774 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 1);
4775 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004776 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4777 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4778 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004779
Michael Wrightd02c5b62014-02-10 15:10:22 -08004780 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004781 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004782 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4783 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004784 ASSERT_NO_FATAL_FAILURE(
4785 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004786
4787 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4788 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4789 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4790 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004791 ASSERT_NO_FATAL_FAILURE(
4792 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004793
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004794 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 0);
4795 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004796 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004797 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004798 ASSERT_EQ(0, motionArgs.buttonState);
4799 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004800 ASSERT_NO_FATAL_FAILURE(
4801 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004802
4803 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4804 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4805 ASSERT_EQ(0, motionArgs.buttonState);
4806 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004807 ASSERT_NO_FATAL_FAILURE(
4808 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004809
Michael Wrightd02c5b62014-02-10 15:10:22 -08004810 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4811 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4812 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4813}
4814
4815TEST_F(CursorInputMapperTest, Process_WhenModeIsPointer_ShouldMoveThePointerAround) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004816 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004817 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004818
4819 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4820 mFakePointerController->setPosition(100, 200);
4821 mFakePointerController->setButtonState(0);
4822
4823 NotifyMotionArgs args;
4824
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004825 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4826 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4827 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004828 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004829 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4830 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4831 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4832 110.0f, 220.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
Michael Wright17db18e2020-06-26 20:51:44 +01004833 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004834}
4835
4836TEST_F(CursorInputMapperTest, Process_PointerCapture) {
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004837 addConfigurationProperty("cursor.mode", "pointer");
4838 mFakePolicy->setPointerCapture(true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004839 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004840
4841 NotifyDeviceResetArgs resetArgs;
4842 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4843 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4844 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4845
4846 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4847 mFakePointerController->setPosition(100, 200);
4848 mFakePointerController->setButtonState(0);
4849
4850 NotifyMotionArgs args;
4851
4852 // Move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004853 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4854 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4855 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004856 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4857 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4858 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4859 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4860 10.0f, 20.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
Michael Wright17db18e2020-06-26 20:51:44 +01004861 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004862
4863 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004864 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4865 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004866 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4867 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4868 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
4869 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4870 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4871 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4872 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4873 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
4874 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4875 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4876
4877 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004878 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4879 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004880 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4881 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4882 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
4883 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4884 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4885 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4886 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4887 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
4888 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4889 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4890
4891 // Another move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004892 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 30);
4893 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 40);
4894 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004895 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4896 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4897 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4898 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4899 30.0f, 40.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
Michael Wright17db18e2020-06-26 20:51:44 +01004900 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004901
4902 // Disable pointer capture and check that the device generation got bumped
4903 // and events are generated the usual way.
arthurhungdcef2dc2020-08-11 14:47:50 +08004904 const uint32_t generation = mReader->getContext()->getGeneration();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004905 mFakePolicy->setPointerCapture(false);
4906 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
arthurhungdcef2dc2020-08-11 14:47:50 +08004907 ASSERT_TRUE(mReader->getContext()->getGeneration() != generation);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004908
4909 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4910 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4911 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4912
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004913 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4914 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4915 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004916 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4917 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004918 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4919 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4920 110.0f, 220.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
Michael Wright17db18e2020-06-26 20:51:44 +01004921 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004922}
4923
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00004924/**
4925 * When Pointer Capture is enabled, we expect to report unprocessed relative movements, so any
4926 * pointer acceleration or speed processing should not be applied.
4927 */
4928TEST_F(CursorInputMapperTest, PointerCaptureDisablesVelocityProcessing) {
4929 addConfigurationProperty("cursor.mode", "pointer");
4930 const VelocityControlParameters testParams(5.f /*scale*/, 0.f /*low threshold*/,
4931 100.f /*high threshold*/, 10.f /*acceleration*/);
4932 mFakePolicy->setVelocityControlParams(testParams);
4933 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
4934
4935 NotifyDeviceResetArgs resetArgs;
4936 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4937 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4938 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4939
4940 NotifyMotionArgs args;
4941
4942 // Move and verify scale is applied.
4943 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4944 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4945 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4946 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4947 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4948 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4949 const float relX = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X);
4950 const float relY = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y);
4951 ASSERT_GT(relX, 10);
4952 ASSERT_GT(relY, 20);
4953
4954 // Enable Pointer Capture
4955 mFakePolicy->setPointerCapture(true);
4956 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
4957 NotifyPointerCaptureChangedArgs captureArgs;
4958 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyCaptureWasCalled(&captureArgs));
4959 ASSERT_TRUE(captureArgs.request.enable);
4960
4961 // Move and verify scale is not applied.
4962 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4963 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4964 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4965 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4966 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4967 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4968 ASSERT_EQ(10, args.pointerCoords[0].getX());
4969 ASSERT_EQ(20, args.pointerCoords[0].getY());
4970}
4971
Prabir Pradhan258e2b92022-06-24 18:37:04 +00004972TEST_F(CursorInputMapperTest, PointerCaptureDisablesOrientationChanges) {
4973 addConfigurationProperty("cursor.mode", "pointer");
4974 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
4975
4976 NotifyDeviceResetArgs resetArgs;
4977 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4978 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4979 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4980
4981 // Ensure the display is rotated.
4982 prepareDisplay(DISPLAY_ORIENTATION_90);
4983
4984 NotifyMotionArgs args;
4985
4986 // Verify that the coordinates are rotated.
4987 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4988 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4989 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4990 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4991 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4992 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4993 ASSERT_EQ(-20, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X));
4994 ASSERT_EQ(10, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y));
4995
4996 // Enable Pointer Capture.
4997 mFakePolicy->setPointerCapture(true);
4998 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
4999 NotifyPointerCaptureChangedArgs captureArgs;
5000 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyCaptureWasCalled(&captureArgs));
5001 ASSERT_TRUE(captureArgs.request.enable);
5002
5003 // Move and verify rotation is not applied.
5004 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5005 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5006 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5007 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5008 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
5009 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
5010 ASSERT_EQ(10, args.pointerCoords[0].getX());
5011 ASSERT_EQ(20, args.pointerCoords[0].getY());
5012}
5013
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005014TEST_F(CursorInputMapperTest, Process_ShouldHandleDisplayId) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005015 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005016
Garfield Tan888a6a42020-01-09 11:39:16 -08005017 // Setup for second display.
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005018 constexpr int32_t SECOND_DISPLAY_ID = 1;
Garfield Tan888a6a42020-01-09 11:39:16 -08005019 const std::string SECOND_DISPLAY_UNIQUE_ID = "local:1";
5020 mFakePolicy->addDisplayViewport(SECOND_DISPLAY_ID, 800, 480, DISPLAY_ORIENTATION_0,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00005021 true /*isActive*/, SECOND_DISPLAY_UNIQUE_ID, NO_PORT,
5022 ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08005023 mFakePolicy->setDefaultPointerDisplayId(SECOND_DISPLAY_ID);
5024 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
5025
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005026 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
5027 mFakePointerController->setPosition(100, 200);
5028 mFakePointerController->setButtonState(0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005029
5030 NotifyMotionArgs args;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005031 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5032 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5033 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005034 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5035 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
5036 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
5037 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5038 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 +01005039 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005040 ASSERT_EQ(SECOND_DISPLAY_ID, args.displayId);
5041}
5042
Michael Wrightd02c5b62014-02-10 15:10:22 -08005043// --- TouchInputMapperTest ---
5044
5045class TouchInputMapperTest : public InputMapperTest {
5046protected:
5047 static const int32_t RAW_X_MIN;
5048 static const int32_t RAW_X_MAX;
5049 static const int32_t RAW_Y_MIN;
5050 static const int32_t RAW_Y_MAX;
5051 static const int32_t RAW_TOUCH_MIN;
5052 static const int32_t RAW_TOUCH_MAX;
5053 static const int32_t RAW_TOOL_MIN;
5054 static const int32_t RAW_TOOL_MAX;
5055 static const int32_t RAW_PRESSURE_MIN;
5056 static const int32_t RAW_PRESSURE_MAX;
5057 static const int32_t RAW_ORIENTATION_MIN;
5058 static const int32_t RAW_ORIENTATION_MAX;
5059 static const int32_t RAW_DISTANCE_MIN;
5060 static const int32_t RAW_DISTANCE_MAX;
5061 static const int32_t RAW_TILT_MIN;
5062 static const int32_t RAW_TILT_MAX;
5063 static const int32_t RAW_ID_MIN;
5064 static const int32_t RAW_ID_MAX;
5065 static const int32_t RAW_SLOT_MIN;
5066 static const int32_t RAW_SLOT_MAX;
5067 static const float X_PRECISION;
5068 static const float Y_PRECISION;
Santos Cordonfa5cf462017-04-05 10:37:00 -07005069 static const float X_PRECISION_VIRTUAL;
5070 static const float Y_PRECISION_VIRTUAL;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005071
5072 static const float GEOMETRIC_SCALE;
Jason Gerecke489fda82012-09-07 17:19:40 -07005073 static const TouchAffineTransformation AFFINE_TRANSFORM;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005074
5075 static const VirtualKeyDefinition VIRTUAL_KEYS[2];
5076
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005077 const std::string UNIQUE_ID = "local:0";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005078 const std::string SECONDARY_UNIQUE_ID = "local:1";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005079
Michael Wrightd02c5b62014-02-10 15:10:22 -08005080 enum Axes {
5081 POSITION = 1 << 0,
5082 TOUCH = 1 << 1,
5083 TOOL = 1 << 2,
5084 PRESSURE = 1 << 3,
5085 ORIENTATION = 1 << 4,
5086 MINOR = 1 << 5,
5087 ID = 1 << 6,
5088 DISTANCE = 1 << 7,
5089 TILT = 1 << 8,
5090 SLOT = 1 << 9,
5091 TOOL_TYPE = 1 << 10,
5092 };
5093
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005094 void prepareDisplay(int32_t orientation, std::optional<uint8_t> port = NO_PORT);
5095 void prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port = NO_PORT);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005096 void prepareVirtualDisplay(int32_t orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005097 void prepareVirtualKeys();
Jason Gerecke489fda82012-09-07 17:19:40 -07005098 void prepareLocationCalibration();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005099 int32_t toRawX(float displayX);
5100 int32_t toRawY(float displayY);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005101 int32_t toRotatedRawX(float displayX);
5102 int32_t toRotatedRawY(float displayY);
Jason Gerecke489fda82012-09-07 17:19:40 -07005103 float toCookedX(float rawX, float rawY);
5104 float toCookedY(float rawX, float rawY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005105 float toDisplayX(int32_t rawX);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005106 float toDisplayX(int32_t rawX, int32_t displayWidth);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005107 float toDisplayY(int32_t rawY);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005108 float toDisplayY(int32_t rawY, int32_t displayHeight);
5109
Michael Wrightd02c5b62014-02-10 15:10:22 -08005110};
5111
5112const int32_t TouchInputMapperTest::RAW_X_MIN = 25;
5113const int32_t TouchInputMapperTest::RAW_X_MAX = 1019;
5114const int32_t TouchInputMapperTest::RAW_Y_MIN = 30;
5115const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009;
5116const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0;
5117const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31;
5118const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0;
5119const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15;
Michael Wrightaa449c92017-12-13 21:21:43 +00005120const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = 0;
5121const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = 255;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005122const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7;
5123const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
5124const int32_t TouchInputMapperTest::RAW_DISTANCE_MIN = 0;
5125const int32_t TouchInputMapperTest::RAW_DISTANCE_MAX = 7;
5126const int32_t TouchInputMapperTest::RAW_TILT_MIN = 0;
5127const int32_t TouchInputMapperTest::RAW_TILT_MAX = 150;
5128const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
5129const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
5130const int32_t TouchInputMapperTest::RAW_SLOT_MIN = 0;
5131const int32_t TouchInputMapperTest::RAW_SLOT_MAX = 9;
5132const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH;
5133const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT;
Santos Cordonfa5cf462017-04-05 10:37:00 -07005134const float TouchInputMapperTest::X_PRECISION_VIRTUAL =
5135 float(RAW_X_MAX - RAW_X_MIN + 1) / VIRTUAL_DISPLAY_WIDTH;
5136const float TouchInputMapperTest::Y_PRECISION_VIRTUAL =
5137 float(RAW_Y_MAX - RAW_Y_MIN + 1) / VIRTUAL_DISPLAY_HEIGHT;
Jason Gerecke489fda82012-09-07 17:19:40 -07005138const TouchAffineTransformation TouchInputMapperTest::AFFINE_TRANSFORM =
5139 TouchAffineTransformation(1, -2, 3, -4, 5, -6);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005140
5141const float TouchInputMapperTest::GEOMETRIC_SCALE =
5142 avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1),
5143 float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1));
5144
5145const VirtualKeyDefinition TouchInputMapperTest::VIRTUAL_KEYS[2] = {
5146 { KEY_HOME, 60, DISPLAY_HEIGHT + 15, 20, 20 },
5147 { KEY_MENU, DISPLAY_HEIGHT - 60, DISPLAY_WIDTH + 15, 20, 20 },
5148};
5149
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005150void TouchInputMapperTest::prepareDisplay(int32_t orientation, std::optional<uint8_t> port) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01005151 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
5152 port, ViewportType::INTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005153}
5154
5155void TouchInputMapperTest::prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port) {
5156 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
5157 DISPLAY_ORIENTATION_0, SECONDARY_UNIQUE_ID, port, type);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005158}
5159
Santos Cordonfa5cf462017-04-05 10:37:00 -07005160void TouchInputMapperTest::prepareVirtualDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01005161 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH, VIRTUAL_DISPLAY_HEIGHT,
5162 orientation, VIRTUAL_DISPLAY_UNIQUE_ID, NO_PORT,
5163 ViewportType::VIRTUAL);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005164}
5165
Michael Wrightd02c5b62014-02-10 15:10:22 -08005166void TouchInputMapperTest::prepareVirtualKeys() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005167 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[0]);
5168 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[1]);
5169 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
5170 mFakeEventHub->addKey(EVENTHUB_ID, KEY_MENU, 0, AKEYCODE_MENU, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005171}
5172
Jason Gerecke489fda82012-09-07 17:19:40 -07005173void TouchInputMapperTest::prepareLocationCalibration() {
5174 mFakePolicy->setTouchAffineTransformation(AFFINE_TRANSFORM);
5175}
5176
Michael Wrightd02c5b62014-02-10 15:10:22 -08005177int32_t TouchInputMapperTest::toRawX(float displayX) {
5178 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN);
5179}
5180
5181int32_t TouchInputMapperTest::toRawY(float displayY) {
5182 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN);
5183}
5184
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005185int32_t TouchInputMapperTest::toRotatedRawX(float displayX) {
5186 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_HEIGHT + RAW_X_MIN);
5187}
5188
5189int32_t TouchInputMapperTest::toRotatedRawY(float displayY) {
5190 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_WIDTH + RAW_Y_MIN);
5191}
5192
Jason Gerecke489fda82012-09-07 17:19:40 -07005193float TouchInputMapperTest::toCookedX(float rawX, float rawY) {
5194 AFFINE_TRANSFORM.applyTo(rawX, rawY);
5195 return rawX;
5196}
5197
5198float TouchInputMapperTest::toCookedY(float rawX, float rawY) {
5199 AFFINE_TRANSFORM.applyTo(rawX, rawY);
5200 return rawY;
5201}
5202
Michael Wrightd02c5b62014-02-10 15:10:22 -08005203float TouchInputMapperTest::toDisplayX(int32_t rawX) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005204 return toDisplayX(rawX, DISPLAY_WIDTH);
5205}
5206
5207float TouchInputMapperTest::toDisplayX(int32_t rawX, int32_t displayWidth) {
5208 return float(rawX - RAW_X_MIN) * displayWidth / (RAW_X_MAX - RAW_X_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005209}
5210
5211float TouchInputMapperTest::toDisplayY(int32_t rawY) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005212 return toDisplayY(rawY, DISPLAY_HEIGHT);
5213}
5214
5215float TouchInputMapperTest::toDisplayY(int32_t rawY, int32_t displayHeight) {
5216 return float(rawY - RAW_Y_MIN) * displayHeight / (RAW_Y_MAX - RAW_Y_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005217}
5218
5219
5220// --- SingleTouchInputMapperTest ---
5221
5222class SingleTouchInputMapperTest : public TouchInputMapperTest {
5223protected:
5224 void prepareButtons();
5225 void prepareAxes(int axes);
5226
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005227 void processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
5228 void processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
5229 void processUp(SingleTouchInputMapper& mappery);
5230 void processPressure(SingleTouchInputMapper& mapper, int32_t pressure);
5231 void processToolMajor(SingleTouchInputMapper& mapper, int32_t toolMajor);
5232 void processDistance(SingleTouchInputMapper& mapper, int32_t distance);
5233 void processTilt(SingleTouchInputMapper& mapper, int32_t tiltX, int32_t tiltY);
5234 void processKey(SingleTouchInputMapper& mapper, int32_t code, int32_t value);
5235 void processSync(SingleTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005236};
5237
5238void SingleTouchInputMapperTest::prepareButtons() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005239 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005240}
5241
5242void SingleTouchInputMapperTest::prepareAxes(int axes) {
5243 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005244 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
5245 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005246 }
5247 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005248 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MIN,
5249 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005250 }
5251 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005252 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TOOL_WIDTH, RAW_TOOL_MIN, RAW_TOOL_MAX, 0,
5253 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005254 }
5255 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005256 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_DISTANCE, RAW_DISTANCE_MIN,
5257 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005258 }
5259 if (axes & TILT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005260 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_X, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
5261 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_Y, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005262 }
5263}
5264
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005265void SingleTouchInputMapperTest::processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005266 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
5267 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
5268 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005269}
5270
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005271void SingleTouchInputMapperTest::processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005272 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
5273 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005274}
5275
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005276void SingleTouchInputMapperTest::processUp(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005277 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005278}
5279
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005280void SingleTouchInputMapperTest::processPressure(SingleTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005281 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005282}
5283
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005284void SingleTouchInputMapperTest::processToolMajor(SingleTouchInputMapper& mapper,
5285 int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005286 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TOOL_WIDTH, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005287}
5288
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005289void SingleTouchInputMapperTest::processDistance(SingleTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005290 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005291}
5292
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005293void SingleTouchInputMapperTest::processTilt(SingleTouchInputMapper& mapper, int32_t tiltX,
5294 int32_t tiltY) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005295 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_X, tiltX);
5296 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_Y, tiltY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005297}
5298
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005299void SingleTouchInputMapperTest::processKey(SingleTouchInputMapper& mapper, int32_t code,
5300 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005301 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005302}
5303
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005304void SingleTouchInputMapperTest::processSync(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005305 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005306}
5307
Michael Wrightd02c5b62014-02-10 15:10:22 -08005308TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005309 prepareButtons();
5310 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005311 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005312
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005313 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005314}
5315
5316TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndIsACursor_ReturnsTouchPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005317 mFakeEventHub->addRelativeAxis(EVENTHUB_ID, REL_X);
5318 mFakeEventHub->addRelativeAxis(EVENTHUB_ID, REL_Y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005319 prepareButtons();
5320 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005321 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005322
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005323 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005324}
5325
5326TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchPad_ReturnsTouchPad) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005327 prepareButtons();
5328 prepareAxes(POSITION);
5329 addConfigurationProperty("touch.deviceType", "touchPad");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005330 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005331
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005332 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005333}
5334
5335TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005336 prepareButtons();
5337 prepareAxes(POSITION);
5338 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005339 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005340
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005341 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005342}
5343
5344TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005345 addConfigurationProperty("touch.deviceType", "touchScreen");
5346 prepareDisplay(DISPLAY_ORIENTATION_0);
5347 prepareButtons();
5348 prepareAxes(POSITION);
5349 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005350 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005351
5352 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005353 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005354
5355 // Virtual key is down.
5356 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5357 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5358 processDown(mapper, x, y);
5359 processSync(mapper);
5360 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5361
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005362 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005363
5364 // Virtual key is up.
5365 processUp(mapper);
5366 processSync(mapper);
5367 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5368
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005369 ASSERT_EQ(AKEY_STATE_UP, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005370}
5371
5372TEST_F(SingleTouchInputMapperTest, GetScanCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005373 addConfigurationProperty("touch.deviceType", "touchScreen");
5374 prepareDisplay(DISPLAY_ORIENTATION_0);
5375 prepareButtons();
5376 prepareAxes(POSITION);
5377 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005378 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005379
5380 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005381 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005382
5383 // Virtual key is down.
5384 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5385 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5386 processDown(mapper, x, y);
5387 processSync(mapper);
5388 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5389
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005390 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005391
5392 // Virtual key is up.
5393 processUp(mapper);
5394 processSync(mapper);
5395 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5396
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005397 ASSERT_EQ(AKEY_STATE_UP, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005398}
5399
5400TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005401 addConfigurationProperty("touch.deviceType", "touchScreen");
5402 prepareDisplay(DISPLAY_ORIENTATION_0);
5403 prepareButtons();
5404 prepareAxes(POSITION);
5405 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005406 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005407
5408 const int32_t keys[2] = { AKEYCODE_HOME, AKEYCODE_A };
5409 uint8_t flags[2] = { 0, 0 };
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005410 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, 2, keys, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005411 ASSERT_TRUE(flags[0]);
5412 ASSERT_FALSE(flags[1]);
5413}
5414
5415TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005416 addConfigurationProperty("touch.deviceType", "touchScreen");
5417 prepareDisplay(DISPLAY_ORIENTATION_0);
5418 prepareButtons();
5419 prepareAxes(POSITION);
5420 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005421 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005422
arthurhungdcef2dc2020-08-11 14:47:50 +08005423 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005424
5425 NotifyKeyArgs args;
5426
5427 // Press virtual key.
5428 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5429 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5430 processDown(mapper, x, y);
5431 processSync(mapper);
5432
5433 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
5434 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5435 ASSERT_EQ(DEVICE_ID, args.deviceId);
5436 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
5437 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
5438 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
5439 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5440 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5441 ASSERT_EQ(KEY_HOME, args.scanCode);
5442 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5443 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5444
5445 // Release virtual key.
5446 processUp(mapper);
5447 processSync(mapper);
5448
5449 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
5450 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5451 ASSERT_EQ(DEVICE_ID, args.deviceId);
5452 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
5453 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
5454 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
5455 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5456 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5457 ASSERT_EQ(KEY_HOME, args.scanCode);
5458 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5459 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5460
5461 // Should not have sent any motions.
5462 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5463}
5464
5465TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005466 addConfigurationProperty("touch.deviceType", "touchScreen");
5467 prepareDisplay(DISPLAY_ORIENTATION_0);
5468 prepareButtons();
5469 prepareAxes(POSITION);
5470 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005471 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005472
arthurhungdcef2dc2020-08-11 14:47:50 +08005473 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005474
5475 NotifyKeyArgs keyArgs;
5476
5477 // Press virtual key.
5478 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5479 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5480 processDown(mapper, x, y);
5481 processSync(mapper);
5482
5483 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5484 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5485 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5486 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5487 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5488 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5489 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags);
5490 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5491 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5492 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5493 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5494
5495 // Move out of bounds. This should generate a cancel and a pointer down since we moved
5496 // into the display area.
5497 y -= 100;
5498 processMove(mapper, x, y);
5499 processSync(mapper);
5500
5501 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5502 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5503 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5504 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5505 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5506 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5507 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
5508 | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags);
5509 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5510 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5511 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5512 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5513
5514 NotifyMotionArgs motionArgs;
5515 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5516 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5517 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5518 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5519 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5520 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5521 ASSERT_EQ(0, motionArgs.flags);
5522 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5523 ASSERT_EQ(0, motionArgs.buttonState);
5524 ASSERT_EQ(0, motionArgs.edgeFlags);
5525 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5526 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5527 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5528 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5529 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5530 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5531 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5532 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5533
5534 // Keep moving out of bounds. Should generate a pointer move.
5535 y -= 50;
5536 processMove(mapper, x, y);
5537 processSync(mapper);
5538
5539 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5540 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5541 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5542 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5543 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5544 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5545 ASSERT_EQ(0, motionArgs.flags);
5546 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5547 ASSERT_EQ(0, motionArgs.buttonState);
5548 ASSERT_EQ(0, motionArgs.edgeFlags);
5549 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5550 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5551 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5552 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5553 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5554 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5555 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5556 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5557
5558 // Release out of bounds. Should generate a pointer up.
5559 processUp(mapper);
5560 processSync(mapper);
5561
5562 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5563 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5564 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5565 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5566 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5567 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5568 ASSERT_EQ(0, motionArgs.flags);
5569 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5570 ASSERT_EQ(0, motionArgs.buttonState);
5571 ASSERT_EQ(0, motionArgs.edgeFlags);
5572 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5573 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5574 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5575 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5576 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5577 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5578 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5579 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5580
5581 // Should not have sent any more keys or motions.
5582 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5583 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5584}
5585
5586TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005587 addConfigurationProperty("touch.deviceType", "touchScreen");
5588 prepareDisplay(DISPLAY_ORIENTATION_0);
5589 prepareButtons();
5590 prepareAxes(POSITION);
5591 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005592 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005593
arthurhungdcef2dc2020-08-11 14:47:50 +08005594 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005595
5596 NotifyMotionArgs motionArgs;
5597
5598 // Initially go down out of bounds.
5599 int32_t x = -10;
5600 int32_t y = -10;
5601 processDown(mapper, x, y);
5602 processSync(mapper);
5603
5604 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5605
5606 // Move into the display area. Should generate a pointer down.
5607 x = 50;
5608 y = 75;
5609 processMove(mapper, x, y);
5610 processSync(mapper);
5611
5612 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5613 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5614 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5615 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5616 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5617 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5618 ASSERT_EQ(0, motionArgs.flags);
5619 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5620 ASSERT_EQ(0, motionArgs.buttonState);
5621 ASSERT_EQ(0, motionArgs.edgeFlags);
5622 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5623 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5624 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5625 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5626 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5627 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5628 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5629 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5630
5631 // Release. Should generate a pointer up.
5632 processUp(mapper);
5633 processSync(mapper);
5634
5635 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5636 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5637 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5638 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5639 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5640 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5641 ASSERT_EQ(0, motionArgs.flags);
5642 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5643 ASSERT_EQ(0, motionArgs.buttonState);
5644 ASSERT_EQ(0, motionArgs.edgeFlags);
5645 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5646 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5647 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5648 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5649 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5650 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5651 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5652 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5653
5654 // Should not have sent any more keys or motions.
5655 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5656 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5657}
5658
Santos Cordonfa5cf462017-04-05 10:37:00 -07005659TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture_VirtualDisplay) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005660 addConfigurationProperty("touch.deviceType", "touchScreen");
5661 addConfigurationProperty("touch.displayId", VIRTUAL_DISPLAY_UNIQUE_ID);
5662
5663 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
5664 prepareButtons();
5665 prepareAxes(POSITION);
5666 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005667 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Santos Cordonfa5cf462017-04-05 10:37:00 -07005668
arthurhungdcef2dc2020-08-11 14:47:50 +08005669 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005670
5671 NotifyMotionArgs motionArgs;
5672
5673 // Down.
5674 int32_t x = 100;
5675 int32_t y = 125;
5676 processDown(mapper, x, y);
5677 processSync(mapper);
5678
5679 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5680 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5681 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5682 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5683 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5684 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5685 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5686 ASSERT_EQ(0, motionArgs.flags);
5687 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5688 ASSERT_EQ(0, motionArgs.buttonState);
5689 ASSERT_EQ(0, motionArgs.edgeFlags);
5690 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5691 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5692 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5693 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5694 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5695 1, 0, 0, 0, 0, 0, 0, 0));
5696 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5697 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5698 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5699
5700 // Move.
5701 x += 50;
5702 y += 75;
5703 processMove(mapper, x, y);
5704 processSync(mapper);
5705
5706 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5707 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5708 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5709 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5710 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5711 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5712 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5713 ASSERT_EQ(0, motionArgs.flags);
5714 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5715 ASSERT_EQ(0, motionArgs.buttonState);
5716 ASSERT_EQ(0, motionArgs.edgeFlags);
5717 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5718 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5719 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5720 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5721 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5722 1, 0, 0, 0, 0, 0, 0, 0));
5723 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5724 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5725 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5726
5727 // Up.
5728 processUp(mapper);
5729 processSync(mapper);
5730
5731 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5732 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5733 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5734 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5735 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5736 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5737 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5738 ASSERT_EQ(0, motionArgs.flags);
5739 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5740 ASSERT_EQ(0, motionArgs.buttonState);
5741 ASSERT_EQ(0, motionArgs.edgeFlags);
5742 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5743 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5744 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5745 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5746 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5747 1, 0, 0, 0, 0, 0, 0, 0));
5748 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5749 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5750 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5751
5752 // Should not have sent any more keys or motions.
5753 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5754 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5755}
5756
Michael Wrightd02c5b62014-02-10 15:10:22 -08005757TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005758 addConfigurationProperty("touch.deviceType", "touchScreen");
5759 prepareDisplay(DISPLAY_ORIENTATION_0);
5760 prepareButtons();
5761 prepareAxes(POSITION);
5762 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005763 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005764
arthurhungdcef2dc2020-08-11 14:47:50 +08005765 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005766
5767 NotifyMotionArgs motionArgs;
5768
5769 // Down.
5770 int32_t x = 100;
5771 int32_t y = 125;
5772 processDown(mapper, x, y);
5773 processSync(mapper);
5774
5775 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5776 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5777 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5778 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5779 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5780 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5781 ASSERT_EQ(0, motionArgs.flags);
5782 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5783 ASSERT_EQ(0, motionArgs.buttonState);
5784 ASSERT_EQ(0, motionArgs.edgeFlags);
5785 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5786 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5787 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5788 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5789 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5790 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5791 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5792 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5793
5794 // Move.
5795 x += 50;
5796 y += 75;
5797 processMove(mapper, x, y);
5798 processSync(mapper);
5799
5800 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5801 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5802 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5803 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5804 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5805 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5806 ASSERT_EQ(0, motionArgs.flags);
5807 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5808 ASSERT_EQ(0, motionArgs.buttonState);
5809 ASSERT_EQ(0, motionArgs.edgeFlags);
5810 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5811 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5812 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5813 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5814 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5815 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5816 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5817 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5818
5819 // Up.
5820 processUp(mapper);
5821 processSync(mapper);
5822
5823 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5824 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5825 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5826 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5827 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5828 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5829 ASSERT_EQ(0, motionArgs.flags);
5830 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5831 ASSERT_EQ(0, motionArgs.buttonState);
5832 ASSERT_EQ(0, motionArgs.edgeFlags);
5833 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5834 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5835 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5836 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5837 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5838 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5839 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5840 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5841
5842 // Should not have sent any more keys or motions.
5843 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5844 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5845}
5846
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005847TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_DoesNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005848 addConfigurationProperty("touch.deviceType", "touchScreen");
5849 prepareButtons();
5850 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005851 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
5852 // need to be rotated. Touchscreens are orientation-aware by default.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005853 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005854
5855 NotifyMotionArgs args;
5856
5857 // Rotation 90.
5858 prepareDisplay(DISPLAY_ORIENTATION_90);
5859 processDown(mapper, toRawX(50), toRawY(75));
5860 processSync(mapper);
5861
5862 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5863 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5864 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5865
5866 processUp(mapper);
5867 processSync(mapper);
5868 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5869}
5870
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005871TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_RotatesMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005872 addConfigurationProperty("touch.deviceType", "touchScreen");
5873 prepareButtons();
5874 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005875 // Since InputReader works in the un-rotated coordinate space, only devices that are not
5876 // orientation-aware are affected by display rotation.
5877 addConfigurationProperty("touch.orientationAware", "0");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005878 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005879
5880 NotifyMotionArgs args;
5881
5882 // Rotation 0.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005883 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005884 prepareDisplay(DISPLAY_ORIENTATION_0);
5885 processDown(mapper, toRawX(50), toRawY(75));
5886 processSync(mapper);
5887
5888 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5889 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5890 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5891
5892 processUp(mapper);
5893 processSync(mapper);
5894 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5895
5896 // Rotation 90.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005897 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005898 prepareDisplay(DISPLAY_ORIENTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005899 processDown(mapper, toRawX(75), RAW_Y_MAX - toRawY(50) + RAW_Y_MIN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005900 processSync(mapper);
5901
5902 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5903 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5904 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5905
5906 processUp(mapper);
5907 processSync(mapper);
5908 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5909
5910 // Rotation 180.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005911 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005912 prepareDisplay(DISPLAY_ORIENTATION_180);
5913 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
5914 processSync(mapper);
5915
5916 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5917 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5918 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5919
5920 processUp(mapper);
5921 processSync(mapper);
5922 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5923
5924 // Rotation 270.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005925 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005926 prepareDisplay(DISPLAY_ORIENTATION_270);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005927 processDown(mapper, RAW_X_MAX - toRawX(75) + RAW_X_MIN, toRawY(50));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005928 processSync(mapper);
5929
5930 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5931 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5932 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5933
5934 processUp(mapper);
5935 processSync(mapper);
5936 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5937}
5938
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005939TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation0_RotatesMotions) {
5940 addConfigurationProperty("touch.deviceType", "touchScreen");
5941 prepareButtons();
5942 prepareAxes(POSITION);
5943 addConfigurationProperty("touch.orientationAware", "1");
5944 addConfigurationProperty("touch.orientation", "ORIENTATION_0");
5945 clearViewports();
5946 prepareDisplay(DISPLAY_ORIENTATION_0);
5947 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5948 NotifyMotionArgs args;
5949
5950 // Orientation 0.
5951 processDown(mapper, toRawX(50), toRawY(75));
5952 processSync(mapper);
5953
5954 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5955 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5956 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5957
5958 processUp(mapper);
5959 processSync(mapper);
5960 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5961}
5962
5963TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation90_RotatesMotions) {
5964 addConfigurationProperty("touch.deviceType", "touchScreen");
5965 prepareButtons();
5966 prepareAxes(POSITION);
5967 addConfigurationProperty("touch.orientationAware", "1");
5968 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
5969 clearViewports();
5970 prepareDisplay(DISPLAY_ORIENTATION_0);
5971 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5972 NotifyMotionArgs args;
5973
5974 // Orientation 90.
5975 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
5976 processSync(mapper);
5977
5978 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5979 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5980 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5981
5982 processUp(mapper);
5983 processSync(mapper);
5984 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5985}
5986
5987TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation180_RotatesMotions) {
5988 addConfigurationProperty("touch.deviceType", "touchScreen");
5989 prepareButtons();
5990 prepareAxes(POSITION);
5991 addConfigurationProperty("touch.orientationAware", "1");
5992 addConfigurationProperty("touch.orientation", "ORIENTATION_180");
5993 clearViewports();
5994 prepareDisplay(DISPLAY_ORIENTATION_0);
5995 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5996 NotifyMotionArgs args;
5997
5998 // Orientation 180.
5999 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
6000 processSync(mapper);
6001
6002 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6003 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6004 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6005
6006 processUp(mapper);
6007 processSync(mapper);
6008 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6009}
6010
6011TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation270_RotatesMotions) {
6012 addConfigurationProperty("touch.deviceType", "touchScreen");
6013 prepareButtons();
6014 prepareAxes(POSITION);
6015 addConfigurationProperty("touch.orientationAware", "1");
6016 addConfigurationProperty("touch.orientation", "ORIENTATION_270");
6017 clearViewports();
6018 prepareDisplay(DISPLAY_ORIENTATION_0);
6019 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6020 NotifyMotionArgs args;
6021
6022 // Orientation 270.
6023 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
6024 processSync(mapper);
6025
6026 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6027 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6028 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6029
6030 processUp(mapper);
6031 processSync(mapper);
6032 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6033}
6034
6035TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationSpecified_RotatesMotionWithDisplay) {
6036 addConfigurationProperty("touch.deviceType", "touchScreen");
6037 prepareButtons();
6038 prepareAxes(POSITION);
6039 // Since InputReader works in the un-rotated coordinate space, only devices that are not
6040 // orientation-aware are affected by display rotation.
6041 addConfigurationProperty("touch.orientationAware", "0");
6042 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
6043 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6044
6045 NotifyMotionArgs args;
6046
6047 // Orientation 90, Rotation 0.
6048 clearViewports();
6049 prepareDisplay(DISPLAY_ORIENTATION_0);
6050 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
6051 processSync(mapper);
6052
6053 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6054 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6055 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6056
6057 processUp(mapper);
6058 processSync(mapper);
6059 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6060
6061 // Orientation 90, Rotation 90.
6062 clearViewports();
6063 prepareDisplay(DISPLAY_ORIENTATION_90);
6064 processDown(mapper, toRotatedRawX(50), toRotatedRawY(75));
6065 processSync(mapper);
6066
6067 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6068 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6069 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6070
6071 processUp(mapper);
6072 processSync(mapper);
6073 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6074
6075 // Orientation 90, Rotation 180.
6076 clearViewports();
6077 prepareDisplay(DISPLAY_ORIENTATION_180);
6078 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
6079 processSync(mapper);
6080
6081 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6082 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6083 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6084
6085 processUp(mapper);
6086 processSync(mapper);
6087 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6088
6089 // Orientation 90, Rotation 270.
6090 clearViewports();
6091 prepareDisplay(DISPLAY_ORIENTATION_270);
6092 processDown(mapper, RAW_X_MAX - toRotatedRawX(50) + RAW_X_MIN,
6093 RAW_Y_MAX - toRotatedRawY(75) + RAW_Y_MIN);
6094 processSync(mapper);
6095
6096 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6097 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6098 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6099
6100 processUp(mapper);
6101 processSync(mapper);
6102 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6103}
6104
Michael Wrightd02c5b62014-02-10 15:10:22 -08006105TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006106 addConfigurationProperty("touch.deviceType", "touchScreen");
6107 prepareDisplay(DISPLAY_ORIENTATION_0);
6108 prepareButtons();
6109 prepareAxes(POSITION | PRESSURE | TOOL | DISTANCE | TILT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006110 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006111
6112 // These calculations are based on the input device calibration documentation.
6113 int32_t rawX = 100;
6114 int32_t rawY = 200;
6115 int32_t rawPressure = 10;
6116 int32_t rawToolMajor = 12;
6117 int32_t rawDistance = 2;
6118 int32_t rawTiltX = 30;
6119 int32_t rawTiltY = 110;
6120
6121 float x = toDisplayX(rawX);
6122 float y = toDisplayY(rawY);
6123 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
6124 float size = float(rawToolMajor) / RAW_TOOL_MAX;
6125 float tool = float(rawToolMajor) * GEOMETRIC_SCALE;
6126 float distance = float(rawDistance);
6127
6128 float tiltCenter = (RAW_TILT_MAX + RAW_TILT_MIN) * 0.5f;
6129 float tiltScale = M_PI / 180;
6130 float tiltXAngle = (rawTiltX - tiltCenter) * tiltScale;
6131 float tiltYAngle = (rawTiltY - tiltCenter) * tiltScale;
6132 float orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
6133 float tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
6134
6135 processDown(mapper, rawX, rawY);
6136 processPressure(mapper, rawPressure);
6137 processToolMajor(mapper, rawToolMajor);
6138 processDistance(mapper, rawDistance);
6139 processTilt(mapper, rawTiltX, rawTiltY);
6140 processSync(mapper);
6141
6142 NotifyMotionArgs args;
6143 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6144 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6145 x, y, pressure, size, tool, tool, tool, tool, orientation, distance));
6146 ASSERT_EQ(tilt, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TILT));
6147}
6148
Jason Gerecke489fda82012-09-07 17:19:40 -07006149TEST_F(SingleTouchInputMapperTest, Process_XYAxes_AffineCalibration) {
Jason Gerecke489fda82012-09-07 17:19:40 -07006150 addConfigurationProperty("touch.deviceType", "touchScreen");
6151 prepareDisplay(DISPLAY_ORIENTATION_0);
6152 prepareLocationCalibration();
6153 prepareButtons();
6154 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006155 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Jason Gerecke489fda82012-09-07 17:19:40 -07006156
6157 int32_t rawX = 100;
6158 int32_t rawY = 200;
6159
6160 float x = toDisplayX(toCookedX(rawX, rawY));
6161 float y = toDisplayY(toCookedY(rawX, rawY));
6162
6163 processDown(mapper, rawX, rawY);
6164 processSync(mapper);
6165
6166 NotifyMotionArgs args;
6167 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6168 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6169 x, y, 1, 0, 0, 0, 0, 0, 0, 0));
6170}
6171
Michael Wrightd02c5b62014-02-10 15:10:22 -08006172TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006173 addConfigurationProperty("touch.deviceType", "touchScreen");
6174 prepareDisplay(DISPLAY_ORIENTATION_0);
6175 prepareButtons();
6176 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006177 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006178
6179 NotifyMotionArgs motionArgs;
6180 NotifyKeyArgs keyArgs;
6181
6182 processDown(mapper, 100, 200);
6183 processSync(mapper);
6184 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6185 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6186 ASSERT_EQ(0, motionArgs.buttonState);
6187
6188 // press BTN_LEFT, release BTN_LEFT
6189 processKey(mapper, BTN_LEFT, 1);
6190 processSync(mapper);
6191 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6192 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6193 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6194
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006195 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6196 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6197 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6198
Michael Wrightd02c5b62014-02-10 15:10:22 -08006199 processKey(mapper, BTN_LEFT, 0);
6200 processSync(mapper);
6201 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006202 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006203 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006204
6205 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006206 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006207 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006208
6209 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
6210 processKey(mapper, BTN_RIGHT, 1);
6211 processKey(mapper, BTN_MIDDLE, 1);
6212 processSync(mapper);
6213 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6214 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6215 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6216 motionArgs.buttonState);
6217
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006218 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6219 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6220 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
6221
6222 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6223 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6224 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6225 motionArgs.buttonState);
6226
Michael Wrightd02c5b62014-02-10 15:10:22 -08006227 processKey(mapper, BTN_RIGHT, 0);
6228 processSync(mapper);
6229 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006230 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006231 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006232
6233 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006234 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006235 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006236
6237 processKey(mapper, BTN_MIDDLE, 0);
6238 processSync(mapper);
6239 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006240 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006241 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006242
6243 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006244 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006245 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006246
6247 // press BTN_BACK, release BTN_BACK
6248 processKey(mapper, BTN_BACK, 1);
6249 processSync(mapper);
6250 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6251 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6252 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006253
Michael Wrightd02c5b62014-02-10 15:10:22 -08006254 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006255 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006256 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6257
6258 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6259 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6260 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006261
6262 processKey(mapper, BTN_BACK, 0);
6263 processSync(mapper);
6264 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006265 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006266 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006267
6268 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006269 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006270 ASSERT_EQ(0, motionArgs.buttonState);
6271
Michael Wrightd02c5b62014-02-10 15:10:22 -08006272 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6273 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6274 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6275
6276 // press BTN_SIDE, release BTN_SIDE
6277 processKey(mapper, BTN_SIDE, 1);
6278 processSync(mapper);
6279 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6280 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6281 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006282
Michael Wrightd02c5b62014-02-10 15:10:22 -08006283 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006284 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006285 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6286
6287 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6288 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6289 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006290
6291 processKey(mapper, BTN_SIDE, 0);
6292 processSync(mapper);
6293 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006294 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006295 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006296
6297 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006298 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006299 ASSERT_EQ(0, motionArgs.buttonState);
6300
Michael Wrightd02c5b62014-02-10 15:10:22 -08006301 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6302 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6303 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6304
6305 // press BTN_FORWARD, release BTN_FORWARD
6306 processKey(mapper, BTN_FORWARD, 1);
6307 processSync(mapper);
6308 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6309 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6310 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006311
Michael Wrightd02c5b62014-02-10 15:10:22 -08006312 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006313 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006314 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6315
6316 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6317 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6318 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006319
6320 processKey(mapper, BTN_FORWARD, 0);
6321 processSync(mapper);
6322 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006323 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006324 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006325
6326 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006327 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006328 ASSERT_EQ(0, motionArgs.buttonState);
6329
Michael Wrightd02c5b62014-02-10 15:10:22 -08006330 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6331 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6332 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6333
6334 // press BTN_EXTRA, release BTN_EXTRA
6335 processKey(mapper, BTN_EXTRA, 1);
6336 processSync(mapper);
6337 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6338 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6339 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006340
Michael Wrightd02c5b62014-02-10 15:10:22 -08006341 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006342 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006343 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6344
6345 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6346 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6347 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006348
6349 processKey(mapper, BTN_EXTRA, 0);
6350 processSync(mapper);
6351 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006352 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006353 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006354
6355 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006356 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006357 ASSERT_EQ(0, motionArgs.buttonState);
6358
Michael Wrightd02c5b62014-02-10 15:10:22 -08006359 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6360 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6361 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6362
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006363 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6364
Michael Wrightd02c5b62014-02-10 15:10:22 -08006365 // press BTN_STYLUS, release BTN_STYLUS
6366 processKey(mapper, BTN_STYLUS, 1);
6367 processSync(mapper);
6368 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6369 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006370 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
6371
6372 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6373 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6374 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006375
6376 processKey(mapper, BTN_STYLUS, 0);
6377 processSync(mapper);
6378 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006379 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006380 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006381
6382 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006383 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006384 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006385
6386 // press BTN_STYLUS2, release BTN_STYLUS2
6387 processKey(mapper, BTN_STYLUS2, 1);
6388 processSync(mapper);
6389 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6390 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006391 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
6392
6393 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6394 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6395 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006396
6397 processKey(mapper, BTN_STYLUS2, 0);
6398 processSync(mapper);
6399 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006400 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006401 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006402
6403 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006404 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006405 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006406
6407 // release touch
6408 processUp(mapper);
6409 processSync(mapper);
6410 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6411 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6412 ASSERT_EQ(0, motionArgs.buttonState);
6413}
6414
6415TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006416 addConfigurationProperty("touch.deviceType", "touchScreen");
6417 prepareDisplay(DISPLAY_ORIENTATION_0);
6418 prepareButtons();
6419 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006420 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006421
6422 NotifyMotionArgs motionArgs;
6423
6424 // default tool type is finger
6425 processDown(mapper, 100, 200);
6426 processSync(mapper);
6427 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6428 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6429 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6430
6431 // eraser
6432 processKey(mapper, BTN_TOOL_RUBBER, 1);
6433 processSync(mapper);
6434 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6435 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6436 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6437
6438 // stylus
6439 processKey(mapper, BTN_TOOL_RUBBER, 0);
6440 processKey(mapper, BTN_TOOL_PEN, 1);
6441 processSync(mapper);
6442 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6443 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6444 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6445
6446 // brush
6447 processKey(mapper, BTN_TOOL_PEN, 0);
6448 processKey(mapper, BTN_TOOL_BRUSH, 1);
6449 processSync(mapper);
6450 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6451 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6452 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6453
6454 // pencil
6455 processKey(mapper, BTN_TOOL_BRUSH, 0);
6456 processKey(mapper, BTN_TOOL_PENCIL, 1);
6457 processSync(mapper);
6458 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6459 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6460 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6461
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08006462 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08006463 processKey(mapper, BTN_TOOL_PENCIL, 0);
6464 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
6465 processSync(mapper);
6466 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6467 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6468 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6469
6470 // mouse
6471 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
6472 processKey(mapper, BTN_TOOL_MOUSE, 1);
6473 processSync(mapper);
6474 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6475 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6476 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6477
6478 // lens
6479 processKey(mapper, BTN_TOOL_MOUSE, 0);
6480 processKey(mapper, BTN_TOOL_LENS, 1);
6481 processSync(mapper);
6482 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6483 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6484 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6485
6486 // double-tap
6487 processKey(mapper, BTN_TOOL_LENS, 0);
6488 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
6489 processSync(mapper);
6490 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6491 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6492 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6493
6494 // triple-tap
6495 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
6496 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
6497 processSync(mapper);
6498 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6499 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6500 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6501
6502 // quad-tap
6503 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
6504 processKey(mapper, BTN_TOOL_QUADTAP, 1);
6505 processSync(mapper);
6506 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6507 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6508 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6509
6510 // finger
6511 processKey(mapper, BTN_TOOL_QUADTAP, 0);
6512 processKey(mapper, BTN_TOOL_FINGER, 1);
6513 processSync(mapper);
6514 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6515 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6516 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6517
6518 // stylus trumps finger
6519 processKey(mapper, BTN_TOOL_PEN, 1);
6520 processSync(mapper);
6521 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6522 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6523 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6524
6525 // eraser trumps stylus
6526 processKey(mapper, BTN_TOOL_RUBBER, 1);
6527 processSync(mapper);
6528 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6529 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6530 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6531
6532 // mouse trumps eraser
6533 processKey(mapper, BTN_TOOL_MOUSE, 1);
6534 processSync(mapper);
6535 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6536 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6537 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6538
6539 // back to default tool type
6540 processKey(mapper, BTN_TOOL_MOUSE, 0);
6541 processKey(mapper, BTN_TOOL_RUBBER, 0);
6542 processKey(mapper, BTN_TOOL_PEN, 0);
6543 processKey(mapper, BTN_TOOL_FINGER, 0);
6544 processSync(mapper);
6545 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6546 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6547 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6548}
6549
6550TEST_F(SingleTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006551 addConfigurationProperty("touch.deviceType", "touchScreen");
6552 prepareDisplay(DISPLAY_ORIENTATION_0);
6553 prepareButtons();
6554 prepareAxes(POSITION);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006555 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_FINGER, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006556 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006557
6558 NotifyMotionArgs motionArgs;
6559
6560 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
6561 processKey(mapper, BTN_TOOL_FINGER, 1);
6562 processMove(mapper, 100, 200);
6563 processSync(mapper);
6564 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6565 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6566 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6567 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6568
6569 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6570 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6571 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6572 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6573
6574 // move a little
6575 processMove(mapper, 150, 250);
6576 processSync(mapper);
6577 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6578 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6579 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6580 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6581
6582 // down when BTN_TOUCH is pressed, pressure defaults to 1
6583 processKey(mapper, BTN_TOUCH, 1);
6584 processSync(mapper);
6585 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6586 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6587 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6588 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6589
6590 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6591 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6592 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6593 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6594
6595 // up when BTN_TOUCH is released, hover restored
6596 processKey(mapper, BTN_TOUCH, 0);
6597 processSync(mapper);
6598 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6599 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6600 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6601 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6602
6603 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6604 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6605 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6606 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6607
6608 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6609 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6610 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6611 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6612
6613 // exit hover when pointer goes away
6614 processKey(mapper, BTN_TOOL_FINGER, 0);
6615 processSync(mapper);
6616 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6617 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6618 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6619 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6620}
6621
6622TEST_F(SingleTouchInputMapperTest, Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006623 addConfigurationProperty("touch.deviceType", "touchScreen");
6624 prepareDisplay(DISPLAY_ORIENTATION_0);
6625 prepareButtons();
6626 prepareAxes(POSITION | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006627 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006628
6629 NotifyMotionArgs motionArgs;
6630
6631 // initially hovering because pressure is 0
6632 processDown(mapper, 100, 200);
6633 processPressure(mapper, 0);
6634 processSync(mapper);
6635 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6636 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6637 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6638 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6639
6640 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6641 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6642 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6643 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6644
6645 // move a little
6646 processMove(mapper, 150, 250);
6647 processSync(mapper);
6648 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6649 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6650 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6651 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6652
6653 // down when pressure is non-zero
6654 processPressure(mapper, RAW_PRESSURE_MAX);
6655 processSync(mapper);
6656 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6657 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6658 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6659 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6660
6661 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6662 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6663 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6664 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6665
6666 // up when pressure becomes 0, hover restored
6667 processPressure(mapper, 0);
6668 processSync(mapper);
6669 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6670 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6671 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6672 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6673
6674 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6675 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6676 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6677 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6678
6679 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6680 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6681 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6682 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6683
6684 // exit hover when pointer goes away
6685 processUp(mapper);
6686 processSync(mapper);
6687 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6688 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6689 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6690 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6691}
6692
Prabir Pradhan5632d622021-09-06 07:57:20 -07006693// --- TouchDisplayProjectionTest ---
6694
6695class TouchDisplayProjectionTest : public SingleTouchInputMapperTest {
6696public:
6697 // The values inside DisplayViewport are expected to be pre-rotated. This updates the current
6698 // DisplayViewport to pre-rotate the values. The viewport's physical display will be set to the
6699 // rotated equivalent of the given un-rotated physical display bounds.
6700 void configurePhysicalDisplay(int32_t orientation, Rect naturalPhysicalDisplay) {
6701 uint32_t inverseRotationFlags;
6702 auto width = DISPLAY_WIDTH;
6703 auto height = DISPLAY_HEIGHT;
6704 switch (orientation) {
6705 case DISPLAY_ORIENTATION_90:
6706 inverseRotationFlags = ui::Transform::ROT_270;
6707 std::swap(width, height);
6708 break;
6709 case DISPLAY_ORIENTATION_180:
6710 inverseRotationFlags = ui::Transform::ROT_180;
6711 break;
6712 case DISPLAY_ORIENTATION_270:
6713 inverseRotationFlags = ui::Transform::ROT_90;
6714 std::swap(width, height);
6715 break;
6716 case DISPLAY_ORIENTATION_0:
6717 inverseRotationFlags = ui::Transform::ROT_0;
6718 break;
6719 default:
6720 FAIL() << "Invalid orientation: " << orientation;
6721 }
6722
6723 const ui::Transform rotation(inverseRotationFlags, width, height);
6724 const Rect rotatedPhysicalDisplay = rotation.transform(naturalPhysicalDisplay);
6725
6726 std::optional<DisplayViewport> internalViewport =
6727 *mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6728 DisplayViewport& v = *internalViewport;
6729 v.displayId = DISPLAY_ID;
6730 v.orientation = orientation;
6731
6732 v.logicalLeft = 0;
6733 v.logicalTop = 0;
6734 v.logicalRight = 100;
6735 v.logicalBottom = 100;
6736
6737 v.physicalLeft = rotatedPhysicalDisplay.left;
6738 v.physicalTop = rotatedPhysicalDisplay.top;
6739 v.physicalRight = rotatedPhysicalDisplay.right;
6740 v.physicalBottom = rotatedPhysicalDisplay.bottom;
6741
6742 v.deviceWidth = width;
6743 v.deviceHeight = height;
6744
6745 v.isActive = true;
6746 v.uniqueId = UNIQUE_ID;
6747 v.type = ViewportType::INTERNAL;
6748 mFakePolicy->updateViewport(v);
6749 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
6750 }
6751
6752 void assertReceivedMove(const Point& point) {
6753 NotifyMotionArgs motionArgs;
6754 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6755 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6756 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6757 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], point.x, point.y,
6758 1, 0, 0, 0, 0, 0, 0, 0));
6759 }
6760};
6761
6762TEST_F(TouchDisplayProjectionTest, IgnoresTouchesOutsidePhysicalDisplay) {
6763 addConfigurationProperty("touch.deviceType", "touchScreen");
6764 prepareDisplay(DISPLAY_ORIENTATION_0);
6765
6766 prepareButtons();
6767 prepareAxes(POSITION);
6768 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6769
6770 NotifyMotionArgs motionArgs;
6771
6772 // Configure the DisplayViewport such that the logical display maps to a subsection of
6773 // the display panel called the physical display. Here, the physical display is bounded by the
6774 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
6775 static const Rect kPhysicalDisplay{10, 20, 70, 160};
6776 static const std::array<Point, 6> kPointsOutsidePhysicalDisplay{
6777 {{-10, -10}, {0, 0}, {5, 100}, {50, 15}, {75, 100}, {50, 165}}};
6778
6779 for (auto orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90, DISPLAY_ORIENTATION_180,
6780 DISPLAY_ORIENTATION_270}) {
6781 configurePhysicalDisplay(orientation, kPhysicalDisplay);
6782
6783 // Touches outside the physical display should be ignored, and should not generate any
6784 // events. Ensure touches at the following points that lie outside of the physical display
6785 // area do not generate any events.
6786 for (const auto& point : kPointsOutsidePhysicalDisplay) {
6787 processDown(mapper, toRawX(point.x), toRawY(point.y));
6788 processSync(mapper);
6789 processUp(mapper);
6790 processSync(mapper);
6791 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled())
6792 << "Unexpected event generated for touch outside physical display at point: "
6793 << point.x << ", " << point.y;
6794 }
6795 }
6796}
6797
6798TEST_F(TouchDisplayProjectionTest, EmitsTouchDownAfterEnteringPhysicalDisplay) {
6799 addConfigurationProperty("touch.deviceType", "touchScreen");
6800 prepareDisplay(DISPLAY_ORIENTATION_0);
6801
6802 prepareButtons();
6803 prepareAxes(POSITION);
6804 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6805
6806 NotifyMotionArgs motionArgs;
6807
6808 // Configure the DisplayViewport such that the logical display maps to a subsection of
6809 // the display panel called the physical display. Here, the physical display is bounded by the
6810 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
6811 static const Rect kPhysicalDisplay{10, 20, 70, 160};
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 that start outside the physical display should be ignored until it enters the
6818 // physical display bounds, at which point it should generate a down event. Start a touch at
6819 // the point (5, 100), which is outside the physical display bounds.
6820 static const Point kOutsidePoint{5, 100};
6821 processDown(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
6822 processSync(mapper);
6823 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6824
6825 // Move the touch into the physical display area. This should generate a pointer down.
6826 processMove(mapper, toRawX(11), toRawY(21));
6827 processSync(mapper);
6828 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6829 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6830 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6831 ASSERT_NO_FATAL_FAILURE(
6832 assertPointerCoords(motionArgs.pointerCoords[0], 11, 21, 1, 0, 0, 0, 0, 0, 0, 0));
6833
6834 // Move the touch inside the physical display area. This should generate a pointer move.
6835 processMove(mapper, toRawX(69), toRawY(159));
6836 processSync(mapper);
6837 assertReceivedMove({69, 159});
6838
6839 // Move outside the physical display area. Since the pointer is already down, this should
6840 // now continue generating events.
6841 processMove(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
6842 processSync(mapper);
6843 assertReceivedMove(kOutsidePoint);
6844
6845 // Release. This should generate a pointer up.
6846 processUp(mapper);
6847 processSync(mapper);
6848 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6849 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6850 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], kOutsidePoint.x,
6851 kOutsidePoint.y, 1, 0, 0, 0, 0, 0, 0, 0));
6852
6853 // Ensure no more events were generated.
6854 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6855 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6856 }
6857}
6858
Michael Wrightd02c5b62014-02-10 15:10:22 -08006859// --- MultiTouchInputMapperTest ---
6860
6861class MultiTouchInputMapperTest : public TouchInputMapperTest {
6862protected:
6863 void prepareAxes(int axes);
6864
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006865 void processPosition(MultiTouchInputMapper& mapper, int32_t x, int32_t y);
6866 void processTouchMajor(MultiTouchInputMapper& mapper, int32_t touchMajor);
6867 void processTouchMinor(MultiTouchInputMapper& mapper, int32_t touchMinor);
6868 void processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor);
6869 void processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor);
6870 void processOrientation(MultiTouchInputMapper& mapper, int32_t orientation);
6871 void processPressure(MultiTouchInputMapper& mapper, int32_t pressure);
6872 void processDistance(MultiTouchInputMapper& mapper, int32_t distance);
6873 void processId(MultiTouchInputMapper& mapper, int32_t id);
6874 void processSlot(MultiTouchInputMapper& mapper, int32_t slot);
6875 void processToolType(MultiTouchInputMapper& mapper, int32_t toolType);
6876 void processKey(MultiTouchInputMapper& mapper, int32_t code, int32_t value);
6877 void processMTSync(MultiTouchInputMapper& mapper);
6878 void processSync(MultiTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006879};
6880
6881void MultiTouchInputMapperTest::prepareAxes(int axes) {
6882 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006883 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
6884 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006885 }
6886 if (axes & TOUCH) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006887 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN,
6888 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006889 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006890 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN,
6891 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006892 }
6893 }
6894 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006895 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
6896 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006897 if (axes & MINOR) {
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08006898 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006899 RAW_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006900 }
6901 }
6902 if (axes & ORIENTATION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006903 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_ORIENTATION, RAW_ORIENTATION_MIN,
6904 RAW_ORIENTATION_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006905 }
6906 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006907 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_PRESSURE, RAW_PRESSURE_MIN,
6908 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006909 }
6910 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006911 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_DISTANCE, RAW_DISTANCE_MIN,
6912 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006913 }
6914 if (axes & ID) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006915 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX, 0,
6916 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006917 }
6918 if (axes & SLOT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006919 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
6920 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_MT_SLOT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006921 }
6922 if (axes & TOOL_TYPE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006923 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOOL_TYPE, 0, MT_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006924 }
6925}
6926
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006927void MultiTouchInputMapperTest::processPosition(MultiTouchInputMapper& mapper, int32_t x,
6928 int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006929 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_X, x);
6930 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006931}
6932
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006933void MultiTouchInputMapperTest::processTouchMajor(MultiTouchInputMapper& mapper,
6934 int32_t touchMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006935 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006936}
6937
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006938void MultiTouchInputMapperTest::processTouchMinor(MultiTouchInputMapper& mapper,
6939 int32_t touchMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006940 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006941}
6942
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006943void MultiTouchInputMapperTest::processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006944 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006945}
6946
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006947void MultiTouchInputMapperTest::processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006948 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006949}
6950
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006951void MultiTouchInputMapperTest::processOrientation(MultiTouchInputMapper& mapper,
6952 int32_t orientation) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006953 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_ORIENTATION, orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006954}
6955
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006956void MultiTouchInputMapperTest::processPressure(MultiTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006957 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006958}
6959
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006960void MultiTouchInputMapperTest::processDistance(MultiTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006961 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006962}
6963
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006964void MultiTouchInputMapperTest::processId(MultiTouchInputMapper& mapper, int32_t id) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006965 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TRACKING_ID, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006966}
6967
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006968void MultiTouchInputMapperTest::processSlot(MultiTouchInputMapper& mapper, int32_t slot) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006969 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_SLOT, slot);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006970}
6971
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006972void MultiTouchInputMapperTest::processToolType(MultiTouchInputMapper& mapper, int32_t toolType) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006973 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006974}
6975
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006976void MultiTouchInputMapperTest::processKey(MultiTouchInputMapper& mapper, int32_t code,
6977 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006978 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006979}
6980
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006981void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006982 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_MT_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006983}
6984
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006985void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006986 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006987}
6988
Michael Wrightd02c5b62014-02-10 15:10:22 -08006989TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006990 addConfigurationProperty("touch.deviceType", "touchScreen");
6991 prepareDisplay(DISPLAY_ORIENTATION_0);
6992 prepareAxes(POSITION);
6993 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006994 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006995
arthurhungdcef2dc2020-08-11 14:47:50 +08006996 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006997
6998 NotifyMotionArgs motionArgs;
6999
7000 // Two fingers down at once.
7001 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7002 processPosition(mapper, x1, y1);
7003 processMTSync(mapper);
7004 processPosition(mapper, x2, y2);
7005 processMTSync(mapper);
7006 processSync(mapper);
7007
7008 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7009 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7010 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7011 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7012 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7013 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7014 ASSERT_EQ(0, motionArgs.flags);
7015 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7016 ASSERT_EQ(0, motionArgs.buttonState);
7017 ASSERT_EQ(0, motionArgs.edgeFlags);
7018 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7019 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7020 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7021 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7022 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7023 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7024 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7025 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7026
7027 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7028 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7029 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7030 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7031 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007032 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007033 ASSERT_EQ(0, motionArgs.flags);
7034 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7035 ASSERT_EQ(0, motionArgs.buttonState);
7036 ASSERT_EQ(0, motionArgs.edgeFlags);
7037 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7038 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7039 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7040 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7041 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7042 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7043 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7044 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7045 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7046 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7047 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7048 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7049
7050 // Move.
7051 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7052 processPosition(mapper, x1, y1);
7053 processMTSync(mapper);
7054 processPosition(mapper, x2, y2);
7055 processMTSync(mapper);
7056 processSync(mapper);
7057
7058 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7059 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7060 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7061 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7062 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7063 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7064 ASSERT_EQ(0, motionArgs.flags);
7065 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7066 ASSERT_EQ(0, motionArgs.buttonState);
7067 ASSERT_EQ(0, motionArgs.edgeFlags);
7068 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7069 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7070 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7071 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7072 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7073 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7074 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7075 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7076 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7077 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7078 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7079 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7080
7081 // First finger up.
7082 x2 += 15; y2 -= 20;
7083 processPosition(mapper, x2, y2);
7084 processMTSync(mapper);
7085 processSync(mapper);
7086
7087 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7088 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7089 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7090 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7091 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007092 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007093 ASSERT_EQ(0, motionArgs.flags);
7094 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7095 ASSERT_EQ(0, motionArgs.buttonState);
7096 ASSERT_EQ(0, motionArgs.edgeFlags);
7097 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7098 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7099 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7100 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7101 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7102 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7103 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7104 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7105 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7106 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7107 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7108 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7109
7110 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7111 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7112 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7113 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7114 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7115 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7116 ASSERT_EQ(0, motionArgs.flags);
7117 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7118 ASSERT_EQ(0, motionArgs.buttonState);
7119 ASSERT_EQ(0, motionArgs.edgeFlags);
7120 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7121 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7122 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7123 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7124 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7125 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7126 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7127 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7128
7129 // Move.
7130 x2 += 20; y2 -= 25;
7131 processPosition(mapper, x2, y2);
7132 processMTSync(mapper);
7133 processSync(mapper);
7134
7135 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7136 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7137 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7138 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7139 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7140 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7141 ASSERT_EQ(0, motionArgs.flags);
7142 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7143 ASSERT_EQ(0, motionArgs.buttonState);
7144 ASSERT_EQ(0, motionArgs.edgeFlags);
7145 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7146 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7147 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7148 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7149 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7150 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7151 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7152 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7153
7154 // New finger down.
7155 int32_t x3 = 700, y3 = 300;
7156 processPosition(mapper, x2, y2);
7157 processMTSync(mapper);
7158 processPosition(mapper, x3, y3);
7159 processMTSync(mapper);
7160 processSync(mapper);
7161
7162 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7163 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7164 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7165 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7166 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007167 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007168 ASSERT_EQ(0, motionArgs.flags);
7169 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7170 ASSERT_EQ(0, motionArgs.buttonState);
7171 ASSERT_EQ(0, motionArgs.edgeFlags);
7172 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7173 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7174 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7175 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7176 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7177 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7178 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7179 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7180 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7181 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7182 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7183 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7184
7185 // Second finger up.
7186 x3 += 30; y3 -= 20;
7187 processPosition(mapper, x3, y3);
7188 processMTSync(mapper);
7189 processSync(mapper);
7190
7191 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7192 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7193 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7194 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7195 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007196 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007197 ASSERT_EQ(0, motionArgs.flags);
7198 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7199 ASSERT_EQ(0, motionArgs.buttonState);
7200 ASSERT_EQ(0, motionArgs.edgeFlags);
7201 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7202 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7203 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7204 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7205 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7206 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7207 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7208 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7209 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7210 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7211 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7212 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7213
7214 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7215 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7216 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7217 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7218 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7219 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7220 ASSERT_EQ(0, motionArgs.flags);
7221 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7222 ASSERT_EQ(0, motionArgs.buttonState);
7223 ASSERT_EQ(0, motionArgs.edgeFlags);
7224 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7225 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7226 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7227 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7228 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7229 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7230 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7231 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7232
7233 // Last finger up.
7234 processMTSync(mapper);
7235 processSync(mapper);
7236
7237 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7238 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7239 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7240 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7241 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7242 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7243 ASSERT_EQ(0, motionArgs.flags);
7244 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7245 ASSERT_EQ(0, motionArgs.buttonState);
7246 ASSERT_EQ(0, motionArgs.edgeFlags);
7247 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7248 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7249 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7250 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7251 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7252 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7253 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7254 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7255
7256 // Should not have sent any more keys or motions.
7257 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7258 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7259}
7260
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007261TEST_F(MultiTouchInputMapperTest, AxisResolution_IsPopulated) {
7262 addConfigurationProperty("touch.deviceType", "touchScreen");
7263 prepareDisplay(DISPLAY_ORIENTATION_0);
7264
7265 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
7266 /*fuzz*/ 0, /*resolution*/ 10);
7267 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
7268 /*fuzz*/ 0, /*resolution*/ 11);
7269 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
7270 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 12);
7271 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
7272 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 13);
7273 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7274 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 14);
7275 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7276 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 15);
7277
7278 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7279
7280 // X and Y axes
7281 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_X, 10 / X_PRECISION);
7282 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_Y, 11 / Y_PRECISION);
7283 // Touch major and minor
7284 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR, 12 * GEOMETRIC_SCALE);
7285 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR, 13 * GEOMETRIC_SCALE);
7286 // Tool major and minor
7287 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR, 14 * GEOMETRIC_SCALE);
7288 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR, 15 * GEOMETRIC_SCALE);
7289}
7290
7291TEST_F(MultiTouchInputMapperTest, TouchMajorAndMinorAxes_DoNotAppearIfNotSupported) {
7292 addConfigurationProperty("touch.deviceType", "touchScreen");
7293 prepareDisplay(DISPLAY_ORIENTATION_0);
7294
7295 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
7296 /*fuzz*/ 0, /*resolution*/ 10);
7297 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
7298 /*fuzz*/ 0, /*resolution*/ 11);
7299
7300 // We do not add ABS_MT_TOUCH_MAJOR / MINOR or ABS_MT_WIDTH_MAJOR / MINOR axes
7301
7302 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7303
7304 // Touch major and minor
7305 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR);
7306 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR);
7307 // Tool major and minor
7308 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR);
7309 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR);
7310}
7311
Michael Wrightd02c5b62014-02-10 15:10:22 -08007312TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007313 addConfigurationProperty("touch.deviceType", "touchScreen");
7314 prepareDisplay(DISPLAY_ORIENTATION_0);
7315 prepareAxes(POSITION | ID);
7316 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007317 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007318
arthurhungdcef2dc2020-08-11 14:47:50 +08007319 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007320
7321 NotifyMotionArgs motionArgs;
7322
7323 // Two fingers down at once.
7324 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7325 processPosition(mapper, x1, y1);
7326 processId(mapper, 1);
7327 processMTSync(mapper);
7328 processPosition(mapper, x2, y2);
7329 processId(mapper, 2);
7330 processMTSync(mapper);
7331 processSync(mapper);
7332
7333 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7334 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7335 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7336 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7337 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7338 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7339 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7340
7341 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007342 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007343 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7344 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7345 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7346 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7347 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7348 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7349 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7350 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7351 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7352
7353 // Move.
7354 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7355 processPosition(mapper, x1, y1);
7356 processId(mapper, 1);
7357 processMTSync(mapper);
7358 processPosition(mapper, x2, y2);
7359 processId(mapper, 2);
7360 processMTSync(mapper);
7361 processSync(mapper);
7362
7363 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7364 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7365 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7366 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7367 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7368 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7369 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7370 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7371 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7372 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7373 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7374
7375 // First finger up.
7376 x2 += 15; y2 -= 20;
7377 processPosition(mapper, x2, y2);
7378 processId(mapper, 2);
7379 processMTSync(mapper);
7380 processSync(mapper);
7381
7382 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007383 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007384 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7385 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7386 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7387 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7388 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7389 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7390 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7391 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7392 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7393
7394 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7395 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7396 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7397 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7398 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7399 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7400 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7401
7402 // Move.
7403 x2 += 20; y2 -= 25;
7404 processPosition(mapper, x2, y2);
7405 processId(mapper, 2);
7406 processMTSync(mapper);
7407 processSync(mapper);
7408
7409 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7410 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7411 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7412 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7413 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7414 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7415 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7416
7417 // New finger down.
7418 int32_t x3 = 700, y3 = 300;
7419 processPosition(mapper, x2, y2);
7420 processId(mapper, 2);
7421 processMTSync(mapper);
7422 processPosition(mapper, x3, y3);
7423 processId(mapper, 3);
7424 processMTSync(mapper);
7425 processSync(mapper);
7426
7427 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007428 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007429 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7430 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7431 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7432 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7433 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7434 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7435 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7436 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7437 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7438
7439 // Second finger up.
7440 x3 += 30; y3 -= 20;
7441 processPosition(mapper, x3, y3);
7442 processId(mapper, 3);
7443 processMTSync(mapper);
7444 processSync(mapper);
7445
7446 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007447 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007448 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7449 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7450 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7451 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7452 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7453 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7454 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7455 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7456 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7457
7458 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7459 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7460 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7461 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7462 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7463 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7464 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7465
7466 // Last finger up.
7467 processMTSync(mapper);
7468 processSync(mapper);
7469
7470 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7471 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7472 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7473 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7474 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7475 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7476 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7477
7478 // Should not have sent any more keys or motions.
7479 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7480 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7481}
7482
7483TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007484 addConfigurationProperty("touch.deviceType", "touchScreen");
7485 prepareDisplay(DISPLAY_ORIENTATION_0);
7486 prepareAxes(POSITION | ID | SLOT);
7487 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007488 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007489
arthurhungdcef2dc2020-08-11 14:47:50 +08007490 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007491
7492 NotifyMotionArgs motionArgs;
7493
7494 // Two fingers down at once.
7495 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7496 processPosition(mapper, x1, y1);
7497 processId(mapper, 1);
7498 processSlot(mapper, 1);
7499 processPosition(mapper, x2, y2);
7500 processId(mapper, 2);
7501 processSync(mapper);
7502
7503 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7504 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7505 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7506 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7507 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7508 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7509 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7510
7511 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007512 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007513 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7514 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7515 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7516 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7517 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7518 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7519 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7520 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7521 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7522
7523 // Move.
7524 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7525 processSlot(mapper, 0);
7526 processPosition(mapper, x1, y1);
7527 processSlot(mapper, 1);
7528 processPosition(mapper, x2, y2);
7529 processSync(mapper);
7530
7531 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7532 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7533 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7534 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7535 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7536 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7537 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7538 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7539 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7540 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7541 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7542
7543 // First finger up.
7544 x2 += 15; y2 -= 20;
7545 processSlot(mapper, 0);
7546 processId(mapper, -1);
7547 processSlot(mapper, 1);
7548 processPosition(mapper, x2, y2);
7549 processSync(mapper);
7550
7551 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007552 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007553 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7554 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7555 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7556 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7557 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7558 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7559 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7560 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7561 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7562
7563 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7564 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7565 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7566 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7567 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7568 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7569 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7570
7571 // Move.
7572 x2 += 20; y2 -= 25;
7573 processPosition(mapper, x2, y2);
7574 processSync(mapper);
7575
7576 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7577 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7578 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7579 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7580 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7581 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7582 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7583
7584 // New finger down.
7585 int32_t x3 = 700, y3 = 300;
7586 processPosition(mapper, x2, y2);
7587 processSlot(mapper, 0);
7588 processId(mapper, 3);
7589 processPosition(mapper, x3, y3);
7590 processSync(mapper);
7591
7592 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007593 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007594 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7595 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7596 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7597 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7598 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7599 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7600 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7601 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7602 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7603
7604 // Second finger up.
7605 x3 += 30; y3 -= 20;
7606 processSlot(mapper, 1);
7607 processId(mapper, -1);
7608 processSlot(mapper, 0);
7609 processPosition(mapper, x3, y3);
7610 processSync(mapper);
7611
7612 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007613 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007614 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7615 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7616 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7617 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7618 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7619 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7620 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7621 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7622 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7623
7624 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7625 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7626 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7627 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7628 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7629 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7630 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7631
7632 // Last finger up.
7633 processId(mapper, -1);
7634 processSync(mapper);
7635
7636 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7637 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7638 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7639 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7640 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7641 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7642 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7643
7644 // Should not have sent any more keys or motions.
7645 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7646 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7647}
7648
7649TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007650 addConfigurationProperty("touch.deviceType", "touchScreen");
7651 prepareDisplay(DISPLAY_ORIENTATION_0);
7652 prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007653 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007654
7655 // These calculations are based on the input device calibration documentation.
7656 int32_t rawX = 100;
7657 int32_t rawY = 200;
7658 int32_t rawTouchMajor = 7;
7659 int32_t rawTouchMinor = 6;
7660 int32_t rawToolMajor = 9;
7661 int32_t rawToolMinor = 8;
7662 int32_t rawPressure = 11;
7663 int32_t rawDistance = 0;
7664 int32_t rawOrientation = 3;
7665 int32_t id = 5;
7666
7667 float x = toDisplayX(rawX);
7668 float y = toDisplayY(rawY);
7669 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
7670 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
7671 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
7672 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
7673 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
7674 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
7675 float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
7676 float distance = float(rawDistance);
7677
7678 processPosition(mapper, rawX, rawY);
7679 processTouchMajor(mapper, rawTouchMajor);
7680 processTouchMinor(mapper, rawTouchMinor);
7681 processToolMajor(mapper, rawToolMajor);
7682 processToolMinor(mapper, rawToolMinor);
7683 processPressure(mapper, rawPressure);
7684 processOrientation(mapper, rawOrientation);
7685 processDistance(mapper, rawDistance);
7686 processId(mapper, id);
7687 processMTSync(mapper);
7688 processSync(mapper);
7689
7690 NotifyMotionArgs args;
7691 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7692 ASSERT_EQ(0, args.pointerProperties[0].id);
7693 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7694 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
7695 orientation, distance));
7696}
7697
7698TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007699 addConfigurationProperty("touch.deviceType", "touchScreen");
7700 prepareDisplay(DISPLAY_ORIENTATION_0);
7701 prepareAxes(POSITION | TOUCH | TOOL | MINOR);
7702 addConfigurationProperty("touch.size.calibration", "geometric");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007703 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007704
7705 // These calculations are based on the input device calibration documentation.
7706 int32_t rawX = 100;
7707 int32_t rawY = 200;
7708 int32_t rawTouchMajor = 140;
7709 int32_t rawTouchMinor = 120;
7710 int32_t rawToolMajor = 180;
7711 int32_t rawToolMinor = 160;
7712
7713 float x = toDisplayX(rawX);
7714 float y = toDisplayY(rawY);
7715 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
7716 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
7717 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
7718 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
7719 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
7720
7721 processPosition(mapper, rawX, rawY);
7722 processTouchMajor(mapper, rawTouchMajor);
7723 processTouchMinor(mapper, rawTouchMinor);
7724 processToolMajor(mapper, rawToolMajor);
7725 processToolMinor(mapper, rawToolMinor);
7726 processMTSync(mapper);
7727 processSync(mapper);
7728
7729 NotifyMotionArgs args;
7730 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7731 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7732 x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
7733}
7734
7735TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007736 addConfigurationProperty("touch.deviceType", "touchScreen");
7737 prepareDisplay(DISPLAY_ORIENTATION_0);
7738 prepareAxes(POSITION | TOUCH | TOOL);
7739 addConfigurationProperty("touch.size.calibration", "diameter");
7740 addConfigurationProperty("touch.size.scale", "10");
7741 addConfigurationProperty("touch.size.bias", "160");
7742 addConfigurationProperty("touch.size.isSummed", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007743 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007744
7745 // These calculations are based on the input device calibration documentation.
7746 // Note: We only provide a single common touch/tool value because the device is assumed
7747 // not to emit separate values for each pointer (isSummed = 1).
7748 int32_t rawX = 100;
7749 int32_t rawY = 200;
7750 int32_t rawX2 = 150;
7751 int32_t rawY2 = 250;
7752 int32_t rawTouchMajor = 5;
7753 int32_t rawToolMajor = 8;
7754
7755 float x = toDisplayX(rawX);
7756 float y = toDisplayY(rawY);
7757 float x2 = toDisplayX(rawX2);
7758 float y2 = toDisplayY(rawY2);
7759 float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
7760 float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
7761 float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
7762
7763 processPosition(mapper, rawX, rawY);
7764 processTouchMajor(mapper, rawTouchMajor);
7765 processToolMajor(mapper, rawToolMajor);
7766 processMTSync(mapper);
7767 processPosition(mapper, rawX2, rawY2);
7768 processTouchMajor(mapper, rawTouchMajor);
7769 processToolMajor(mapper, rawToolMajor);
7770 processMTSync(mapper);
7771 processSync(mapper);
7772
7773 NotifyMotionArgs args;
7774 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7775 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
7776
7777 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007778 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007779 ASSERT_EQ(size_t(2), args.pointerCount);
7780 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7781 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
7782 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
7783 x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
7784}
7785
7786TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007787 addConfigurationProperty("touch.deviceType", "touchScreen");
7788 prepareDisplay(DISPLAY_ORIENTATION_0);
7789 prepareAxes(POSITION | TOUCH | TOOL);
7790 addConfigurationProperty("touch.size.calibration", "area");
7791 addConfigurationProperty("touch.size.scale", "43");
7792 addConfigurationProperty("touch.size.bias", "3");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007793 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007794
7795 // These calculations are based on the input device calibration documentation.
7796 int32_t rawX = 100;
7797 int32_t rawY = 200;
7798 int32_t rawTouchMajor = 5;
7799 int32_t rawToolMajor = 8;
7800
7801 float x = toDisplayX(rawX);
7802 float y = toDisplayY(rawY);
7803 float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
7804 float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
7805 float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
7806
7807 processPosition(mapper, rawX, rawY);
7808 processTouchMajor(mapper, rawTouchMajor);
7809 processToolMajor(mapper, rawToolMajor);
7810 processMTSync(mapper);
7811 processSync(mapper);
7812
7813 NotifyMotionArgs args;
7814 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7815 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7816 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
7817}
7818
7819TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007820 addConfigurationProperty("touch.deviceType", "touchScreen");
7821 prepareDisplay(DISPLAY_ORIENTATION_0);
7822 prepareAxes(POSITION | PRESSURE);
7823 addConfigurationProperty("touch.pressure.calibration", "amplitude");
7824 addConfigurationProperty("touch.pressure.scale", "0.01");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007825 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007826
Michael Wrightaa449c92017-12-13 21:21:43 +00007827 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007828 mapper.populateDeviceInfo(&info);
Michael Wrightaa449c92017-12-13 21:21:43 +00007829 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
7830 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TOUCHSCREEN,
7831 0.0f, RAW_PRESSURE_MAX * 0.01, 0.0f, 0.0f));
7832
Michael Wrightd02c5b62014-02-10 15:10:22 -08007833 // These calculations are based on the input device calibration documentation.
7834 int32_t rawX = 100;
7835 int32_t rawY = 200;
7836 int32_t rawPressure = 60;
7837
7838 float x = toDisplayX(rawX);
7839 float y = toDisplayY(rawY);
7840 float pressure = float(rawPressure) * 0.01f;
7841
7842 processPosition(mapper, rawX, rawY);
7843 processPressure(mapper, rawPressure);
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, pressure, 0, 0, 0, 0, 0, 0, 0));
7851}
7852
7853TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007854 addConfigurationProperty("touch.deviceType", "touchScreen");
7855 prepareDisplay(DISPLAY_ORIENTATION_0);
7856 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007857 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007858
7859 NotifyMotionArgs motionArgs;
7860 NotifyKeyArgs keyArgs;
7861
7862 processId(mapper, 1);
7863 processPosition(mapper, 100, 200);
7864 processSync(mapper);
7865 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7866 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7867 ASSERT_EQ(0, motionArgs.buttonState);
7868
7869 // press BTN_LEFT, release BTN_LEFT
7870 processKey(mapper, BTN_LEFT, 1);
7871 processSync(mapper);
7872 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7873 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7874 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
7875
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007876 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7877 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7878 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
7879
Michael Wrightd02c5b62014-02-10 15:10:22 -08007880 processKey(mapper, BTN_LEFT, 0);
7881 processSync(mapper);
7882 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007883 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007884 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007885
7886 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007887 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007888 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007889
7890 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
7891 processKey(mapper, BTN_RIGHT, 1);
7892 processKey(mapper, BTN_MIDDLE, 1);
7893 processSync(mapper);
7894 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7895 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7896 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
7897 motionArgs.buttonState);
7898
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007899 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7900 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7901 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
7902
7903 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7904 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7905 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
7906 motionArgs.buttonState);
7907
Michael Wrightd02c5b62014-02-10 15:10:22 -08007908 processKey(mapper, BTN_RIGHT, 0);
7909 processSync(mapper);
7910 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007911 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007912 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007913
7914 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007915 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007916 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007917
7918 processKey(mapper, BTN_MIDDLE, 0);
7919 processSync(mapper);
7920 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007921 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007922 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007923
7924 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007925 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007926 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007927
7928 // press BTN_BACK, release BTN_BACK
7929 processKey(mapper, BTN_BACK, 1);
7930 processSync(mapper);
7931 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7932 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
7933 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007934
Michael Wrightd02c5b62014-02-10 15:10:22 -08007935 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007936 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007937 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
7938
7939 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7940 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7941 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007942
7943 processKey(mapper, BTN_BACK, 0);
7944 processSync(mapper);
7945 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007946 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007947 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007948
7949 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007950 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007951 ASSERT_EQ(0, motionArgs.buttonState);
7952
Michael Wrightd02c5b62014-02-10 15:10:22 -08007953 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7954 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
7955 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
7956
7957 // press BTN_SIDE, release BTN_SIDE
7958 processKey(mapper, BTN_SIDE, 1);
7959 processSync(mapper);
7960 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7961 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
7962 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007963
Michael Wrightd02c5b62014-02-10 15:10:22 -08007964 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007965 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007966 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
7967
7968 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7969 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7970 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007971
7972 processKey(mapper, BTN_SIDE, 0);
7973 processSync(mapper);
7974 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007975 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007976 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007977
7978 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007979 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007980 ASSERT_EQ(0, motionArgs.buttonState);
7981
Michael Wrightd02c5b62014-02-10 15:10:22 -08007982 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7983 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
7984 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
7985
7986 // press BTN_FORWARD, release BTN_FORWARD
7987 processKey(mapper, BTN_FORWARD, 1);
7988 processSync(mapper);
7989 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7990 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
7991 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007992
Michael Wrightd02c5b62014-02-10 15:10:22 -08007993 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007994 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007995 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
7996
7997 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7998 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7999 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008000
8001 processKey(mapper, BTN_FORWARD, 0);
8002 processSync(mapper);
8003 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008004 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008005 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008006
8007 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008008 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008009 ASSERT_EQ(0, motionArgs.buttonState);
8010
Michael Wrightd02c5b62014-02-10 15:10:22 -08008011 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8012 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8013 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
8014
8015 // press BTN_EXTRA, release BTN_EXTRA
8016 processKey(mapper, BTN_EXTRA, 1);
8017 processSync(mapper);
8018 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8019 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8020 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008021
Michael Wrightd02c5b62014-02-10 15:10:22 -08008022 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008023 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008024 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
8025
8026 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8027 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8028 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008029
8030 processKey(mapper, BTN_EXTRA, 0);
8031 processSync(mapper);
8032 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008033 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008034 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008035
8036 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008037 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008038 ASSERT_EQ(0, motionArgs.buttonState);
8039
Michael Wrightd02c5b62014-02-10 15:10:22 -08008040 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8041 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8042 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
8043
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008044 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8045
Michael Wrightd02c5b62014-02-10 15:10:22 -08008046 // press BTN_STYLUS, release BTN_STYLUS
8047 processKey(mapper, BTN_STYLUS, 1);
8048 processSync(mapper);
8049 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8050 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008051 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
8052
8053 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8054 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8055 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008056
8057 processKey(mapper, BTN_STYLUS, 0);
8058 processSync(mapper);
8059 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008060 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008061 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008062
8063 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008064 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008065 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008066
8067 // press BTN_STYLUS2, release BTN_STYLUS2
8068 processKey(mapper, BTN_STYLUS2, 1);
8069 processSync(mapper);
8070 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8071 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008072 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
8073
8074 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8075 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8076 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008077
8078 processKey(mapper, BTN_STYLUS2, 0);
8079 processSync(mapper);
8080 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008081 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008082 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008083
8084 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008085 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008086 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008087
8088 // release touch
8089 processId(mapper, -1);
8090 processSync(mapper);
8091 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8092 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8093 ASSERT_EQ(0, motionArgs.buttonState);
8094}
8095
8096TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008097 addConfigurationProperty("touch.deviceType", "touchScreen");
8098 prepareDisplay(DISPLAY_ORIENTATION_0);
8099 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008100 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008101
8102 NotifyMotionArgs motionArgs;
8103
8104 // default tool type is finger
8105 processId(mapper, 1);
8106 processPosition(mapper, 100, 200);
8107 processSync(mapper);
8108 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8109 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8110 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8111
8112 // eraser
8113 processKey(mapper, BTN_TOOL_RUBBER, 1);
8114 processSync(mapper);
8115 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8116 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8117 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
8118
8119 // stylus
8120 processKey(mapper, BTN_TOOL_RUBBER, 0);
8121 processKey(mapper, BTN_TOOL_PEN, 1);
8122 processSync(mapper);
8123 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8124 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8125 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8126
8127 // brush
8128 processKey(mapper, BTN_TOOL_PEN, 0);
8129 processKey(mapper, BTN_TOOL_BRUSH, 1);
8130 processSync(mapper);
8131 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8132 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8133 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8134
8135 // pencil
8136 processKey(mapper, BTN_TOOL_BRUSH, 0);
8137 processKey(mapper, BTN_TOOL_PENCIL, 1);
8138 processSync(mapper);
8139 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8140 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8141 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8142
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08008143 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08008144 processKey(mapper, BTN_TOOL_PENCIL, 0);
8145 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
8146 processSync(mapper);
8147 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8148 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8149 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8150
8151 // mouse
8152 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
8153 processKey(mapper, BTN_TOOL_MOUSE, 1);
8154 processSync(mapper);
8155 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8156 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8157 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
8158
8159 // lens
8160 processKey(mapper, BTN_TOOL_MOUSE, 0);
8161 processKey(mapper, BTN_TOOL_LENS, 1);
8162 processSync(mapper);
8163 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8164 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8165 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
8166
8167 // double-tap
8168 processKey(mapper, BTN_TOOL_LENS, 0);
8169 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
8170 processSync(mapper);
8171 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8172 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8173 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8174
8175 // triple-tap
8176 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
8177 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
8178 processSync(mapper);
8179 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8180 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8181 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8182
8183 // quad-tap
8184 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
8185 processKey(mapper, BTN_TOOL_QUADTAP, 1);
8186 processSync(mapper);
8187 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8188 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8189 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8190
8191 // finger
8192 processKey(mapper, BTN_TOOL_QUADTAP, 0);
8193 processKey(mapper, BTN_TOOL_FINGER, 1);
8194 processSync(mapper);
8195 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8196 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8197 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8198
8199 // stylus trumps finger
8200 processKey(mapper, BTN_TOOL_PEN, 1);
8201 processSync(mapper);
8202 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8203 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8204 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8205
8206 // eraser trumps stylus
8207 processKey(mapper, BTN_TOOL_RUBBER, 1);
8208 processSync(mapper);
8209 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8210 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8211 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
8212
8213 // mouse trumps eraser
8214 processKey(mapper, BTN_TOOL_MOUSE, 1);
8215 processSync(mapper);
8216 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8217 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8218 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
8219
8220 // MT tool type trumps BTN tool types: MT_TOOL_FINGER
8221 processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
8222 processSync(mapper);
8223 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8224 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8225 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8226
8227 // MT tool type trumps BTN tool types: MT_TOOL_PEN
8228 processToolType(mapper, MT_TOOL_PEN);
8229 processSync(mapper);
8230 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8231 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8232 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8233
8234 // back to default tool type
8235 processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
8236 processKey(mapper, BTN_TOOL_MOUSE, 0);
8237 processKey(mapper, BTN_TOOL_RUBBER, 0);
8238 processKey(mapper, BTN_TOOL_PEN, 0);
8239 processKey(mapper, BTN_TOOL_FINGER, 0);
8240 processSync(mapper);
8241 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8242 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8243 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8244}
8245
8246TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008247 addConfigurationProperty("touch.deviceType", "touchScreen");
8248 prepareDisplay(DISPLAY_ORIENTATION_0);
8249 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008250 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008251 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008252
8253 NotifyMotionArgs motionArgs;
8254
8255 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
8256 processId(mapper, 1);
8257 processPosition(mapper, 100, 200);
8258 processSync(mapper);
8259 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8260 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8261 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8262 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8263
8264 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8265 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8266 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8267 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8268
8269 // move a little
8270 processPosition(mapper, 150, 250);
8271 processSync(mapper);
8272 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8273 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8274 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8275 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8276
8277 // down when BTN_TOUCH is pressed, pressure defaults to 1
8278 processKey(mapper, BTN_TOUCH, 1);
8279 processSync(mapper);
8280 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8281 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8282 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8283 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8284
8285 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8286 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8287 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8288 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8289
8290 // up when BTN_TOUCH is released, hover restored
8291 processKey(mapper, BTN_TOUCH, 0);
8292 processSync(mapper);
8293 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8294 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8295 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8296 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8297
8298 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8299 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8300 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8301 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8302
8303 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8304 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8305 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8306 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8307
8308 // exit hover when pointer goes away
8309 processId(mapper, -1);
8310 processSync(mapper);
8311 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8312 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8313 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8314 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8315}
8316
8317TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008318 addConfigurationProperty("touch.deviceType", "touchScreen");
8319 prepareDisplay(DISPLAY_ORIENTATION_0);
8320 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008321 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008322
8323 NotifyMotionArgs motionArgs;
8324
8325 // initially hovering because pressure is 0
8326 processId(mapper, 1);
8327 processPosition(mapper, 100, 200);
8328 processPressure(mapper, 0);
8329 processSync(mapper);
8330 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8331 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8332 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8333 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8334
8335 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8336 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8337 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8338 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8339
8340 // move a little
8341 processPosition(mapper, 150, 250);
8342 processSync(mapper);
8343 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8344 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8345 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8346 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8347
8348 // down when pressure becomes non-zero
8349 processPressure(mapper, RAW_PRESSURE_MAX);
8350 processSync(mapper);
8351 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8352 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8353 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8354 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8355
8356 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8357 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8358 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8359 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8360
8361 // up when pressure becomes 0, hover restored
8362 processPressure(mapper, 0);
8363 processSync(mapper);
8364 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8365 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8366 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8367 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8368
8369 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8370 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8371 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8372 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8373
8374 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8375 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8376 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8377 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8378
8379 // exit hover when pointer goes away
8380 processId(mapper, -1);
8381 processSync(mapper);
8382 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8383 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8384 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8385 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8386}
8387
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008388/**
8389 * Set the input device port <--> display port associations, and check that the
8390 * events are routed to the display that matches the display port.
8391 * This can be checked by looking at the displayId of the resulting NotifyMotionArgs.
8392 */
8393TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayPort) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008394 const std::string usb2 = "USB2";
8395 const uint8_t hdmi1 = 0;
8396 const uint8_t hdmi2 = 1;
8397 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008398 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008399
8400 addConfigurationProperty("touch.deviceType", "touchScreen");
8401 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008402 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008403
8404 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
8405 mFakePolicy->addInputPortAssociation(usb2, hdmi2);
8406
8407 // We are intentionally not adding the viewport for display 1 yet. Since the port association
8408 // for this input device is specified, and the matching viewport is not present,
8409 // the input device should be disabled (at the mapper level).
8410
8411 // Add viewport for display 2 on hdmi2
8412 prepareSecondaryDisplay(type, hdmi2);
8413 // Send a touch event
8414 processPosition(mapper, 100, 100);
8415 processSync(mapper);
8416 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8417
8418 // Add viewport for display 1 on hdmi1
8419 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
8420 // Send a touch event again
8421 processPosition(mapper, 100, 100);
8422 processSync(mapper);
8423
8424 NotifyMotionArgs args;
8425 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8426 ASSERT_EQ(DISPLAY_ID, args.displayId);
8427}
Michael Wrightd02c5b62014-02-10 15:10:22 -08008428
Arthur Hung6d5b4b22022-01-21 07:21:10 +00008429TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayUniqueId) {
8430 addConfigurationProperty("touch.deviceType", "touchScreen");
8431 prepareAxes(POSITION);
8432 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8433
8434 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
8435
8436 prepareDisplay(DISPLAY_ORIENTATION_0);
8437 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
8438
8439 // Send a touch event
8440 processPosition(mapper, 100, 100);
8441 processSync(mapper);
8442
8443 NotifyMotionArgs args;
8444 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8445 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
8446}
8447
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008448TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShouldHandleDisplayId) {
Garfield Tan888a6a42020-01-09 11:39:16 -08008449 // Setup for second display.
Michael Wright17db18e2020-06-26 20:51:44 +01008450 std::shared_ptr<FakePointerController> fakePointerController =
8451 std::make_shared<FakePointerController>();
Garfield Tan888a6a42020-01-09 11:39:16 -08008452 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008453 fakePointerController->setPosition(100, 200);
8454 fakePointerController->setButtonState(0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00008455 mFakePolicy->setPointerController(fakePointerController);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008456
Garfield Tan888a6a42020-01-09 11:39:16 -08008457 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008458 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08008459
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008460 prepareDisplay(DISPLAY_ORIENTATION_0);
8461 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008462 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008463
8464 // Check source is mouse that would obtain the PointerController.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008465 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008466
8467 NotifyMotionArgs motionArgs;
8468 processPosition(mapper, 100, 100);
8469 processSync(mapper);
8470
8471 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8472 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8473 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
8474}
8475
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00008476/**
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008477 * Ensure that the readTime is set to the SYN_REPORT value when processing touch events.
8478 */
8479TEST_F(MultiTouchInputMapperTest, Process_SendsReadTime) {
8480 addConfigurationProperty("touch.deviceType", "touchScreen");
8481 prepareAxes(POSITION);
8482 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8483
8484 prepareDisplay(DISPLAY_ORIENTATION_0);
8485 process(mapper, 10, 11 /*readTime*/, EV_ABS, ABS_MT_TRACKING_ID, 1);
8486 process(mapper, 15, 16 /*readTime*/, EV_ABS, ABS_MT_POSITION_X, 100);
8487 process(mapper, 20, 21 /*readTime*/, EV_ABS, ABS_MT_POSITION_Y, 100);
8488 process(mapper, 25, 26 /*readTime*/, EV_SYN, SYN_REPORT, 0);
8489
8490 NotifyMotionArgs args;
8491 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8492 ASSERT_EQ(26, args.readTime);
8493
8494 process(mapper, 30, 31 /*readTime*/, EV_ABS, ABS_MT_POSITION_X, 110);
8495 process(mapper, 30, 32 /*readTime*/, EV_ABS, ABS_MT_POSITION_Y, 220);
8496 process(mapper, 30, 33 /*readTime*/, EV_SYN, SYN_REPORT, 0);
8497
8498 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8499 ASSERT_EQ(33, args.readTime);
8500}
8501
8502/**
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00008503 * When the viewport is not active (isActive=false), the touch mapper should be disabled and the
8504 * events should not be delivered to the listener.
8505 */
8506TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreDropped) {
8507 addConfigurationProperty("touch.deviceType", "touchScreen");
8508 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
8509 DISPLAY_ORIENTATION_0, false /*isActive*/, UNIQUE_ID, NO_PORT,
8510 ViewportType::INTERNAL);
8511 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8512 prepareAxes(POSITION);
8513 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8514
8515 NotifyMotionArgs motionArgs;
8516 processPosition(mapper, 100, 100);
8517 processSync(mapper);
8518
8519 mFakeListener->assertNotifyMotionWasNotCalled();
8520}
8521
Garfield Tanc734e4f2021-01-15 20:01:39 -08008522TEST_F(MultiTouchInputMapperTest, Process_DeactivateViewport_AbortTouches) {
8523 addConfigurationProperty("touch.deviceType", "touchScreen");
8524 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
8525 DISPLAY_ORIENTATION_0, true /*isActive*/, UNIQUE_ID, NO_PORT,
8526 ViewportType::INTERNAL);
8527 std::optional<DisplayViewport> optionalDisplayViewport =
8528 mFakePolicy->getDisplayViewportByUniqueId(UNIQUE_ID);
8529 ASSERT_TRUE(optionalDisplayViewport.has_value());
8530 DisplayViewport displayViewport = *optionalDisplayViewport;
8531
8532 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8533 prepareAxes(POSITION);
8534 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8535
8536 // Finger down
8537 int32_t x = 100, y = 100;
8538 processPosition(mapper, x, y);
8539 processSync(mapper);
8540
8541 NotifyMotionArgs motionArgs;
8542 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8543 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8544
8545 // Deactivate display viewport
8546 displayViewport.isActive = false;
8547 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
8548 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8549
8550 // Finger move
8551 x += 10, y += 10;
8552 processPosition(mapper, x, y);
8553 processSync(mapper);
8554
8555 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8556 EXPECT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
8557
8558 // Reactivate display viewport
8559 displayViewport.isActive = true;
8560 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
8561 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8562
8563 // Finger move again
8564 x += 10, y += 10;
8565 processPosition(mapper, x, y);
8566 processSync(mapper);
8567
8568 // Gesture is aborted, so events after display is activated won't be dispatched until there is
8569 // no pointer on the touch device.
8570 mFakeListener->assertNotifyMotionWasNotCalled();
8571}
8572
Arthur Hung7c645402019-01-25 17:45:42 +08008573TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShowTouches) {
8574 // Setup the first touch screen device.
Arthur Hung7c645402019-01-25 17:45:42 +08008575 prepareAxes(POSITION | ID | SLOT);
8576 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008577 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung7c645402019-01-25 17:45:42 +08008578
8579 // Create the second touch screen device, and enable multi fingers.
8580 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08008581 const std::string DEVICE_NAME2 = "TOUCHSCREEN2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08008582 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008583 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08008584 std::shared_ptr<InputDevice> device2 =
8585 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07008586 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08008587
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008588 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
8589 0 /*flat*/, 0 /*fuzz*/);
8590 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
8591 0 /*flat*/, 0 /*fuzz*/);
8592 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX,
8593 0 /*flat*/, 0 /*fuzz*/);
8594 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX,
8595 0 /*flat*/, 0 /*fuzz*/);
8596 mFakeEventHub->setAbsoluteAxisValue(SECOND_EVENTHUB_ID, ABS_MT_SLOT, 0 /*value*/);
8597 mFakeEventHub->addConfigurationProperty(SECOND_EVENTHUB_ID, String8("touch.deviceType"),
8598 String8("touchScreen"));
Arthur Hung7c645402019-01-25 17:45:42 +08008599
8600 // Setup the second touch screen device.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008601 MultiTouchInputMapper& mapper2 = device2->addMapper<MultiTouchInputMapper>(SECOND_EVENTHUB_ID);
Arthur Hung7c645402019-01-25 17:45:42 +08008602 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
8603 device2->reset(ARBITRARY_TIME);
8604
8605 // Setup PointerController.
Michael Wright17db18e2020-06-26 20:51:44 +01008606 std::shared_ptr<FakePointerController> fakePointerController =
8607 std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00008608 mFakePolicy->setPointerController(fakePointerController);
Arthur Hung7c645402019-01-25 17:45:42 +08008609
8610 // Setup policy for associated displays and show touches.
8611 const uint8_t hdmi1 = 0;
8612 const uint8_t hdmi2 = 1;
8613 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
8614 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
8615 mFakePolicy->setShowTouches(true);
8616
8617 // Create displays.
8618 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008619 prepareSecondaryDisplay(ViewportType::EXTERNAL, hdmi2);
Arthur Hung7c645402019-01-25 17:45:42 +08008620
8621 // Default device will reconfigure above, need additional reconfiguration for another device.
8622 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008623 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung7c645402019-01-25 17:45:42 +08008624
8625 // Two fingers down at default display.
8626 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
8627 processPosition(mapper, x1, y1);
8628 processId(mapper, 1);
8629 processSlot(mapper, 1);
8630 processPosition(mapper, x2, y2);
8631 processId(mapper, 2);
8632 processSync(mapper);
8633
8634 std::map<int32_t, std::vector<int32_t>>::const_iterator iter =
8635 fakePointerController->getSpots().find(DISPLAY_ID);
8636 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
8637 ASSERT_EQ(size_t(2), iter->second.size());
8638
8639 // Two fingers down at second display.
8640 processPosition(mapper2, x1, y1);
8641 processId(mapper2, 1);
8642 processSlot(mapper2, 1);
8643 processPosition(mapper2, x2, y2);
8644 processId(mapper2, 2);
8645 processSync(mapper2);
8646
8647 iter = fakePointerController->getSpots().find(SECONDARY_DISPLAY_ID);
8648 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
8649 ASSERT_EQ(size_t(2), iter->second.size());
8650}
8651
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008652TEST_F(MultiTouchInputMapperTest, VideoFrames_ReceivedByListener) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008653 prepareAxes(POSITION);
8654 addConfigurationProperty("touch.deviceType", "touchScreen");
8655 prepareDisplay(DISPLAY_ORIENTATION_0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008656 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008657
8658 NotifyMotionArgs motionArgs;
8659 // Unrotated video frame
8660 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8661 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008662 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008663 processPosition(mapper, 100, 200);
8664 processSync(mapper);
8665 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8666 ASSERT_EQ(frames, motionArgs.videoFrames);
8667
8668 // Subsequent touch events should not have any videoframes
8669 // This is implemented separately in FakeEventHub,
8670 // but that should match the behaviour of TouchVideoDevice.
8671 processPosition(mapper, 200, 200);
8672 processSync(mapper);
8673 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8674 ASSERT_EQ(std::vector<TouchVideoFrame>(), motionArgs.videoFrames);
8675}
8676
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008677TEST_F(MultiTouchInputMapperTest, VideoFrames_AreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008678 prepareAxes(POSITION);
8679 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008680 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008681 // Unrotated video frame
8682 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8683 NotifyMotionArgs motionArgs;
8684
8685 // Test all 4 orientations
8686 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008687 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
8688 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
8689 clearViewports();
8690 prepareDisplay(orientation);
8691 std::vector<TouchVideoFrame> frames{frame};
8692 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
8693 processPosition(mapper, 100, 200);
8694 processSync(mapper);
8695 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8696 ASSERT_EQ(frames, motionArgs.videoFrames);
8697 }
8698}
8699
8700TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_AreRotated) {
8701 prepareAxes(POSITION);
8702 addConfigurationProperty("touch.deviceType", "touchScreen");
8703 // Since InputReader works in the un-rotated coordinate space, only devices that are not
8704 // orientation-aware are affected by display rotation.
8705 addConfigurationProperty("touch.orientationAware", "0");
8706 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8707 // Unrotated video frame
8708 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8709 NotifyMotionArgs motionArgs;
8710
8711 // Test all 4 orientations
8712 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008713 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
8714 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
8715 clearViewports();
8716 prepareDisplay(orientation);
8717 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008718 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008719 processPosition(mapper, 100, 200);
8720 processSync(mapper);
8721 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008722 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
8723 // compared to the display. This is so that when the window transform (which contains the
8724 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
8725 // window's coordinate space.
8726 frames[0].rotate(getInverseRotation(orientation));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008727 ASSERT_EQ(frames, motionArgs.videoFrames);
8728 }
8729}
8730
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008731TEST_F(MultiTouchInputMapperTest, VideoFrames_MultipleFramesAreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008732 prepareAxes(POSITION);
8733 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008734 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008735 // Unrotated video frames. There's no rule that they must all have the same dimensions,
8736 // so mix these.
8737 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8738 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
8739 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
8740 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
8741 NotifyMotionArgs motionArgs;
8742
8743 prepareDisplay(DISPLAY_ORIENTATION_90);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008744 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008745 processPosition(mapper, 100, 200);
8746 processSync(mapper);
8747 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008748 ASSERT_EQ(frames, motionArgs.videoFrames);
8749}
8750
8751TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_MultipleFramesAreRotated) {
8752 prepareAxes(POSITION);
8753 addConfigurationProperty("touch.deviceType", "touchScreen");
8754 // Since InputReader works in the un-rotated coordinate space, only devices that are not
8755 // orientation-aware are affected by display rotation.
8756 addConfigurationProperty("touch.orientationAware", "0");
8757 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8758 // Unrotated video frames. There's no rule that they must all have the same dimensions,
8759 // so mix these.
8760 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8761 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
8762 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
8763 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
8764 NotifyMotionArgs motionArgs;
8765
8766 prepareDisplay(DISPLAY_ORIENTATION_90);
8767 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
8768 processPosition(mapper, 100, 200);
8769 processSync(mapper);
8770 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8771 std::for_each(frames.begin(), frames.end(), [](TouchVideoFrame& frame) {
8772 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
8773 // compared to the display. This is so that when the window transform (which contains the
8774 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
8775 // window's coordinate space.
8776 frame.rotate(getInverseRotation(DISPLAY_ORIENTATION_90));
8777 });
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008778 ASSERT_EQ(frames, motionArgs.videoFrames);
8779}
8780
Arthur Hung9da14732019-09-02 16:16:58 +08008781/**
8782 * If we had defined port associations, but the viewport is not ready, the touch device would be
8783 * expected to be disabled, and it should be enabled after the viewport has found.
8784 */
8785TEST_F(MultiTouchInputMapperTest, Configure_EnabledForAssociatedDisplay) {
Arthur Hung9da14732019-09-02 16:16:58 +08008786 constexpr uint8_t hdmi2 = 1;
8787 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008788 constexpr ViewportType type = ViewportType::EXTERNAL;
Arthur Hung9da14732019-09-02 16:16:58 +08008789
8790 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi2);
8791
8792 addConfigurationProperty("touch.deviceType", "touchScreen");
8793 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008794 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung9da14732019-09-02 16:16:58 +08008795
8796 ASSERT_EQ(mDevice->isEnabled(), false);
8797
8798 // Add display on hdmi2, the device should be enabled and can receive touch event.
8799 prepareSecondaryDisplay(type, hdmi2);
8800 ASSERT_EQ(mDevice->isEnabled(), true);
8801
8802 // Send a touch event.
8803 processPosition(mapper, 100, 100);
8804 processSync(mapper);
8805
8806 NotifyMotionArgs args;
8807 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8808 ASSERT_EQ(SECONDARY_DISPLAY_ID, args.displayId);
8809}
8810
Arthur Hung421eb1c2020-01-16 00:09:42 +08008811TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleSingleTouch) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08008812 addConfigurationProperty("touch.deviceType", "touchScreen");
8813 prepareDisplay(DISPLAY_ORIENTATION_0);
8814 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008815 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08008816
8817 NotifyMotionArgs motionArgs;
8818
8819 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
8820 // finger down
8821 processId(mapper, 1);
8822 processPosition(mapper, x1, y1);
8823 processSync(mapper);
8824 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8825 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8826 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8827
8828 // finger move
8829 processId(mapper, 1);
8830 processPosition(mapper, x2, y2);
8831 processSync(mapper);
8832 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8833 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8834 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8835
8836 // finger up.
8837 processId(mapper, -1);
8838 processSync(mapper);
8839 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8840 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8841 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8842
8843 // new finger down
8844 processId(mapper, 1);
8845 processPosition(mapper, x3, y3);
8846 processSync(mapper);
8847 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8848 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8849 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8850}
8851
8852/**
arthurhungcc7f9802020-04-30 17:55:40 +08008853 * Test single touch should be canceled when received the MT_TOOL_PALM event, and the following
8854 * MOVE and UP events should be ignored.
Arthur Hung421eb1c2020-01-16 00:09:42 +08008855 */
arthurhungcc7f9802020-04-30 17:55:40 +08008856TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_SinglePointer) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08008857 addConfigurationProperty("touch.deviceType", "touchScreen");
8858 prepareDisplay(DISPLAY_ORIENTATION_0);
8859 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008860 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08008861
8862 NotifyMotionArgs motionArgs;
8863
8864 // default tool type is finger
8865 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
arthurhungcc7f9802020-04-30 17:55:40 +08008866 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008867 processPosition(mapper, x1, y1);
8868 processSync(mapper);
8869 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8870 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8871 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8872
8873 // Tool changed to MT_TOOL_PALM expect sending the cancel event.
8874 processToolType(mapper, MT_TOOL_PALM);
8875 processSync(mapper);
8876 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8877 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
8878
8879 // Ignore the following MOVE and UP events if had detect a palm event.
arthurhungcc7f9802020-04-30 17:55:40 +08008880 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008881 processPosition(mapper, x2, y2);
8882 processSync(mapper);
8883 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8884
8885 // finger up.
arthurhungcc7f9802020-04-30 17:55:40 +08008886 processId(mapper, INVALID_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008887 processSync(mapper);
8888 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8889
8890 // new finger down
arthurhungcc7f9802020-04-30 17:55:40 +08008891 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008892 processToolType(mapper, MT_TOOL_FINGER);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008893 processPosition(mapper, x3, y3);
8894 processSync(mapper);
8895 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8896 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8897 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8898}
8899
arthurhungbf89a482020-04-17 17:37:55 +08008900/**
arthurhungcc7f9802020-04-30 17:55:40 +08008901 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
8902 * and the rest active fingers could still be allowed to receive the events
arthurhungbf89a482020-04-17 17:37:55 +08008903 */
arthurhungcc7f9802020-04-30 17:55:40 +08008904TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_TwoPointers) {
arthurhungbf89a482020-04-17 17:37:55 +08008905 addConfigurationProperty("touch.deviceType", "touchScreen");
8906 prepareDisplay(DISPLAY_ORIENTATION_0);
8907 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
8908 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8909
8910 NotifyMotionArgs motionArgs;
8911
8912 // default tool type is finger
arthurhungcc7f9802020-04-30 17:55:40 +08008913 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
8914 processId(mapper, FIRST_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08008915 processPosition(mapper, x1, y1);
8916 processSync(mapper);
8917 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8918 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8919 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8920
8921 // Second finger down.
arthurhungcc7f9802020-04-30 17:55:40 +08008922 processSlot(mapper, SECOND_SLOT);
8923 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08008924 processPosition(mapper, x2, y2);
arthurhungcc7f9802020-04-30 17:55:40 +08008925 processSync(mapper);
8926 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008927 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08008928 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8929
8930 // If the tool type of the first finger changes to MT_TOOL_PALM,
8931 // we expect to receive ACTION_POINTER_UP with cancel flag.
8932 processSlot(mapper, FIRST_SLOT);
8933 processId(mapper, FIRST_TRACKING_ID);
8934 processToolType(mapper, MT_TOOL_PALM);
8935 processSync(mapper);
8936 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008937 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08008938 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
8939
8940 // The following MOVE events of second finger should be processed.
8941 processSlot(mapper, SECOND_SLOT);
8942 processId(mapper, SECOND_TRACKING_ID);
8943 processPosition(mapper, x2 + 1, y2 + 1);
8944 processSync(mapper);
8945 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8946 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8947 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8948
8949 // First finger up. It used to be in palm mode, and we already generated ACTION_POINTER_UP for
8950 // it. Second finger receive move.
8951 processSlot(mapper, FIRST_SLOT);
8952 processId(mapper, INVALID_TRACKING_ID);
8953 processSync(mapper);
8954 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8955 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8956 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8957
8958 // Second finger keeps moving.
8959 processSlot(mapper, SECOND_SLOT);
8960 processId(mapper, SECOND_TRACKING_ID);
8961 processPosition(mapper, x2 + 2, y2 + 2);
8962 processSync(mapper);
8963 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8964 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8965 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8966
8967 // Second finger up.
8968 processId(mapper, INVALID_TRACKING_ID);
8969 processSync(mapper);
8970 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8971 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8972 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
8973}
8974
8975/**
8976 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event, if only 1 finger
8977 * is active, it should send CANCEL after receiving the MT_TOOL_PALM event.
8978 */
8979TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_ShouldCancelWhenAllTouchIsPalm) {
8980 addConfigurationProperty("touch.deviceType", "touchScreen");
8981 prepareDisplay(DISPLAY_ORIENTATION_0);
8982 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
8983 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8984
8985 NotifyMotionArgs motionArgs;
8986
8987 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
8988 // First finger down.
8989 processId(mapper, FIRST_TRACKING_ID);
8990 processPosition(mapper, x1, y1);
8991 processSync(mapper);
8992 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8993 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8994 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8995
8996 // Second finger down.
8997 processSlot(mapper, SECOND_SLOT);
8998 processId(mapper, SECOND_TRACKING_ID);
8999 processPosition(mapper, x2, y2);
arthurhungbf89a482020-04-17 17:37:55 +08009000 processSync(mapper);
9001 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009002 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungbf89a482020-04-17 17:37:55 +08009003 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9004
arthurhungcc7f9802020-04-30 17:55:40 +08009005 // If the tool type of the first finger changes to MT_TOOL_PALM,
9006 // we expect to receive ACTION_POINTER_UP with cancel flag.
9007 processSlot(mapper, FIRST_SLOT);
9008 processId(mapper, FIRST_TRACKING_ID);
9009 processToolType(mapper, MT_TOOL_PALM);
9010 processSync(mapper);
9011 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009012 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009013 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9014
9015 // Second finger keeps moving.
9016 processSlot(mapper, SECOND_SLOT);
9017 processId(mapper, SECOND_TRACKING_ID);
9018 processPosition(mapper, x2 + 1, y2 + 1);
9019 processSync(mapper);
9020 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9021 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9022
9023 // second finger becomes palm, receive cancel due to only 1 finger is active.
9024 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009025 processToolType(mapper, MT_TOOL_PALM);
9026 processSync(mapper);
9027 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9028 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9029
arthurhungcc7f9802020-04-30 17:55:40 +08009030 // third finger down.
9031 processSlot(mapper, THIRD_SLOT);
9032 processId(mapper, THIRD_TRACKING_ID);
9033 processToolType(mapper, MT_TOOL_FINGER);
arthurhungbf89a482020-04-17 17:37:55 +08009034 processPosition(mapper, x3, y3);
9035 processSync(mapper);
arthurhungbf89a482020-04-17 17:37:55 +08009036 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9037 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9038 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08009039 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9040
9041 // third finger move
9042 processId(mapper, THIRD_TRACKING_ID);
9043 processPosition(mapper, x3 + 1, y3 + 1);
9044 processSync(mapper);
9045 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9046 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9047
9048 // first finger up, third finger receive move.
9049 processSlot(mapper, FIRST_SLOT);
9050 processId(mapper, INVALID_TRACKING_ID);
9051 processSync(mapper);
9052 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9053 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9054 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9055
9056 // second finger up, third finger receive move.
9057 processSlot(mapper, SECOND_SLOT);
9058 processId(mapper, INVALID_TRACKING_ID);
9059 processSync(mapper);
9060 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9061 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9062 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9063
9064 // third finger up.
9065 processSlot(mapper, THIRD_SLOT);
9066 processId(mapper, INVALID_TRACKING_ID);
9067 processSync(mapper);
9068 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9069 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9070 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9071}
9072
9073/**
9074 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
9075 * and the active finger could still be allowed to receive the events
9076 */
9077TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_KeepFirstPointer) {
9078 addConfigurationProperty("touch.deviceType", "touchScreen");
9079 prepareDisplay(DISPLAY_ORIENTATION_0);
9080 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
9081 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9082
9083 NotifyMotionArgs motionArgs;
9084
9085 // default tool type is finger
9086 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
9087 processId(mapper, FIRST_TRACKING_ID);
9088 processPosition(mapper, x1, y1);
9089 processSync(mapper);
9090 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9091 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9092 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9093
9094 // Second finger down.
9095 processSlot(mapper, SECOND_SLOT);
9096 processId(mapper, SECOND_TRACKING_ID);
9097 processPosition(mapper, x2, y2);
9098 processSync(mapper);
9099 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009100 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009101 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9102
9103 // If the tool type of the second finger changes to MT_TOOL_PALM,
9104 // we expect to receive ACTION_POINTER_UP with cancel flag.
9105 processId(mapper, SECOND_TRACKING_ID);
9106 processToolType(mapper, MT_TOOL_PALM);
9107 processSync(mapper);
9108 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009109 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009110 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9111
9112 // The following MOVE event should be processed.
9113 processSlot(mapper, FIRST_SLOT);
9114 processId(mapper, FIRST_TRACKING_ID);
9115 processPosition(mapper, x1 + 1, y1 + 1);
9116 processSync(mapper);
9117 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9118 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9119 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9120
9121 // second finger up.
9122 processSlot(mapper, SECOND_SLOT);
9123 processId(mapper, INVALID_TRACKING_ID);
9124 processSync(mapper);
9125 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9126 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9127
9128 // first finger keep moving
9129 processSlot(mapper, FIRST_SLOT);
9130 processId(mapper, FIRST_TRACKING_ID);
9131 processPosition(mapper, x1 + 2, y1 + 2);
9132 processSync(mapper);
9133 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9134 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9135
9136 // first finger up.
9137 processId(mapper, INVALID_TRACKING_ID);
9138 processSync(mapper);
9139 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9140 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9141 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
arthurhungbf89a482020-04-17 17:37:55 +08009142}
9143
Arthur Hung9ad18942021-06-19 02:04:46 +00009144/**
9145 * Test multi-touch should sent ACTION_POINTER_UP/ACTION_UP when received the INVALID_TRACKING_ID,
9146 * to prevent the driver side may send unexpected data after set tracking id as INVALID_TRACKING_ID
9147 * cause slot be valid again.
9148 */
9149TEST_F(MultiTouchInputMapperTest, Process_MultiTouch_WithInvalidTrackingId) {
9150 addConfigurationProperty("touch.deviceType", "touchScreen");
9151 prepareDisplay(DISPLAY_ORIENTATION_0);
9152 prepareAxes(POSITION | ID | SLOT | PRESSURE);
9153 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9154
9155 NotifyMotionArgs motionArgs;
9156
9157 constexpr int32_t x1 = 100, y1 = 200, x2 = 0, y2 = 0;
9158 // First finger down.
9159 processId(mapper, FIRST_TRACKING_ID);
9160 processPosition(mapper, x1, y1);
9161 processPressure(mapper, RAW_PRESSURE_MAX);
9162 processSync(mapper);
9163 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9164 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9165 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9166
9167 // First finger move.
9168 processId(mapper, FIRST_TRACKING_ID);
9169 processPosition(mapper, x1 + 1, y1 + 1);
9170 processPressure(mapper, RAW_PRESSURE_MAX);
9171 processSync(mapper);
9172 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9173 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9174 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9175
9176 // Second finger down.
9177 processSlot(mapper, SECOND_SLOT);
9178 processId(mapper, SECOND_TRACKING_ID);
9179 processPosition(mapper, x2, y2);
9180 processPressure(mapper, RAW_PRESSURE_MAX);
9181 processSync(mapper);
9182 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009183 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Arthur Hung9ad18942021-06-19 02:04:46 +00009184 ASSERT_EQ(uint32_t(2), motionArgs.pointerCount);
9185
9186 // second finger up with some unexpected data.
9187 processSlot(mapper, SECOND_SLOT);
9188 processId(mapper, INVALID_TRACKING_ID);
9189 processPosition(mapper, x2, y2);
9190 processSync(mapper);
9191 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009192 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Arthur Hung9ad18942021-06-19 02:04:46 +00009193 ASSERT_EQ(uint32_t(2), motionArgs.pointerCount);
9194
9195 // first finger up with some unexpected data.
9196 processSlot(mapper, FIRST_SLOT);
9197 processId(mapper, INVALID_TRACKING_ID);
9198 processPosition(mapper, x2, y2);
9199 processPressure(mapper, RAW_PRESSURE_MAX);
9200 processSync(mapper);
9201 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9202 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9203 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9204}
9205
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009206// --- MultiTouchInputMapperTest_ExternalDevice ---
9207
9208class MultiTouchInputMapperTest_ExternalDevice : public MultiTouchInputMapperTest {
9209protected:
Chris Yea52ade12020-08-27 16:49:20 -07009210 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009211};
9212
9213/**
9214 * Expect fallback to internal viewport if device is external and external viewport is not present.
9215 */
9216TEST_F(MultiTouchInputMapperTest_ExternalDevice, Viewports_Fallback) {
9217 prepareAxes(POSITION);
9218 addConfigurationProperty("touch.deviceType", "touchScreen");
9219 prepareDisplay(DISPLAY_ORIENTATION_0);
9220 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9221
9222 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
9223
9224 NotifyMotionArgs motionArgs;
9225
9226 // Expect the event to be sent to the internal viewport,
9227 // because an external viewport is not present.
9228 processPosition(mapper, 100, 100);
9229 processSync(mapper);
9230 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9231 ASSERT_EQ(ADISPLAY_ID_DEFAULT, motionArgs.displayId);
9232
9233 // Expect the event to be sent to the external viewport if it is present.
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009234 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009235 processPosition(mapper, 100, 100);
9236 processSync(mapper);
9237 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9238 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
9239}
Arthur Hung4197f6b2020-03-16 15:39:59 +08009240
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009241TEST_F(MultiTouchInputMapperTest, Process_TouchpadCapture) {
9242 // we need a pointer controller for mouse mode of touchpad (start pointer at 0,0)
9243 std::shared_ptr<FakePointerController> fakePointerController =
9244 std::make_shared<FakePointerController>();
9245 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
9246 fakePointerController->setPosition(0, 0);
9247 fakePointerController->setButtonState(0);
9248
9249 // prepare device and capture
9250 prepareDisplay(DISPLAY_ORIENTATION_0);
9251 prepareAxes(POSITION | ID | SLOT);
9252 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
9253 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
9254 mFakePolicy->setPointerCapture(true);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009255 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009256 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9257
9258 // captured touchpad should be a touchpad source
9259 NotifyDeviceResetArgs resetArgs;
9260 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
9261 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
9262
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009263 InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
Chris Yef74dc422020-09-02 22:41:50 -07009264
9265 const InputDeviceInfo::MotionRange* relRangeX =
9266 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_X, AINPUT_SOURCE_TOUCHPAD);
9267 ASSERT_NE(relRangeX, nullptr);
9268 ASSERT_EQ(relRangeX->min, -(RAW_X_MAX - RAW_X_MIN));
9269 ASSERT_EQ(relRangeX->max, RAW_X_MAX - RAW_X_MIN);
9270 const InputDeviceInfo::MotionRange* relRangeY =
9271 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_Y, AINPUT_SOURCE_TOUCHPAD);
9272 ASSERT_NE(relRangeY, nullptr);
9273 ASSERT_EQ(relRangeY->min, -(RAW_Y_MAX - RAW_Y_MIN));
9274 ASSERT_EQ(relRangeY->max, RAW_Y_MAX - RAW_Y_MIN);
9275
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009276 // run captured pointer tests - note that this is unscaled, so input listener events should be
9277 // identical to what the hardware sends (accounting for any
9278 // calibration).
9279 // FINGER 0 DOWN
Chris Ye364fdb52020-08-05 15:07:56 -07009280 processSlot(mapper, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009281 processId(mapper, 1);
9282 processPosition(mapper, 100 + RAW_X_MIN, 100 + RAW_Y_MIN);
9283 processKey(mapper, BTN_TOUCH, 1);
9284 processSync(mapper);
9285
9286 // expect coord[0] to contain initial location of touch 0
9287 NotifyMotionArgs args;
9288 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9289 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
9290 ASSERT_EQ(1U, args.pointerCount);
9291 ASSERT_EQ(0, args.pointerProperties[0].id);
9292 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, args.source);
9293 ASSERT_NO_FATAL_FAILURE(
9294 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
9295
9296 // FINGER 1 DOWN
9297 processSlot(mapper, 1);
9298 processId(mapper, 2);
9299 processPosition(mapper, 560 + RAW_X_MIN, 154 + RAW_Y_MIN);
9300 processSync(mapper);
9301
9302 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
9303 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009304 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009305 ASSERT_EQ(2U, args.pointerCount);
9306 ASSERT_EQ(0, args.pointerProperties[0].id);
9307 ASSERT_EQ(1, args.pointerProperties[1].id);
9308 ASSERT_NO_FATAL_FAILURE(
9309 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
9310 ASSERT_NO_FATAL_FAILURE(
9311 assertPointerCoords(args.pointerCoords[1], 560, 154, 1, 0, 0, 0, 0, 0, 0, 0));
9312
9313 // FINGER 1 MOVE
9314 processPosition(mapper, 540 + RAW_X_MIN, 690 + RAW_Y_MIN);
9315 processSync(mapper);
9316
9317 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
9318 // from move
9319 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9320 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9321 ASSERT_NO_FATAL_FAILURE(
9322 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
9323 ASSERT_NO_FATAL_FAILURE(
9324 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
9325
9326 // FINGER 0 MOVE
9327 processSlot(mapper, 0);
9328 processPosition(mapper, 50 + RAW_X_MIN, 800 + RAW_Y_MIN);
9329 processSync(mapper);
9330
9331 // expect coord[0] to contain new touch 0 location, coord[1] to contain previous location
9332 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9333 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9334 ASSERT_NO_FATAL_FAILURE(
9335 assertPointerCoords(args.pointerCoords[0], 50, 800, 1, 0, 0, 0, 0, 0, 0, 0));
9336 ASSERT_NO_FATAL_FAILURE(
9337 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
9338
9339 // BUTTON DOWN
9340 processKey(mapper, BTN_LEFT, 1);
9341 processSync(mapper);
9342
9343 // touchinputmapper design sends a move before button press
9344 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9345 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9346 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9347 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
9348
9349 // BUTTON UP
9350 processKey(mapper, BTN_LEFT, 0);
9351 processSync(mapper);
9352
9353 // touchinputmapper design sends a move after button release
9354 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9355 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
9356 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9357 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9358
9359 // FINGER 0 UP
9360 processId(mapper, -1);
9361 processSync(mapper);
9362 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9363 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | 0x0000, args.action);
9364
9365 // FINGER 1 MOVE
9366 processSlot(mapper, 1);
9367 processPosition(mapper, 320 + RAW_X_MIN, 900 + RAW_Y_MIN);
9368 processSync(mapper);
9369
9370 // expect coord[0] to contain new location of touch 1, and properties[0].id to contain 1
9371 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9372 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9373 ASSERT_EQ(1U, args.pointerCount);
9374 ASSERT_EQ(1, args.pointerProperties[0].id);
9375 ASSERT_NO_FATAL_FAILURE(
9376 assertPointerCoords(args.pointerCoords[0], 320, 900, 1, 0, 0, 0, 0, 0, 0, 0));
9377
9378 // FINGER 1 UP
9379 processId(mapper, -1);
9380 processKey(mapper, BTN_TOUCH, 0);
9381 processSync(mapper);
9382 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9383 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
9384
9385 // non captured touchpad should be a mouse source
9386 mFakePolicy->setPointerCapture(false);
9387 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
9388 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
9389 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
9390}
9391
9392TEST_F(MultiTouchInputMapperTest, Process_UnCapturedTouchpadPointer) {
9393 std::shared_ptr<FakePointerController> fakePointerController =
9394 std::make_shared<FakePointerController>();
9395 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
9396 fakePointerController->setPosition(0, 0);
9397 fakePointerController->setButtonState(0);
9398
9399 // prepare device and capture
9400 prepareDisplay(DISPLAY_ORIENTATION_0);
9401 prepareAxes(POSITION | ID | SLOT);
9402 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
9403 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009404 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009405 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9406 // run uncaptured pointer tests - pushes out generic events
9407 // FINGER 0 DOWN
9408 processId(mapper, 3);
9409 processPosition(mapper, 100, 100);
9410 processKey(mapper, BTN_TOUCH, 1);
9411 processSync(mapper);
9412
9413 // start at (100,100), cursor should be at (0,0) * scale
9414 NotifyMotionArgs args;
9415 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9416 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
9417 ASSERT_NO_FATAL_FAILURE(
9418 assertPointerCoords(args.pointerCoords[0], 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
9419
9420 // FINGER 0 MOVE
9421 processPosition(mapper, 200, 200);
9422 processSync(mapper);
9423
9424 // compute scaling to help with touch position checking
9425 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
9426 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
9427 float scale =
9428 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
9429
9430 // translate from (100,100) -> (200,200), cursor should have changed to (100,100) * scale)
9431 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9432 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
9433 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 100 * scale, 100 * scale, 0,
9434 0, 0, 0, 0, 0, 0, 0));
9435}
9436
9437TEST_F(MultiTouchInputMapperTest, WhenCapturedAndNotCaptured_GetSources) {
9438 std::shared_ptr<FakePointerController> fakePointerController =
9439 std::make_shared<FakePointerController>();
9440
9441 prepareDisplay(DISPLAY_ORIENTATION_0);
9442 prepareAxes(POSITION | ID | SLOT);
9443 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009444 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009445 mFakePolicy->setPointerCapture(false);
9446 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9447
9448 // uncaptured touchpad should be a pointer device
9449 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
9450
9451 // captured touchpad should be a touchpad device
9452 mFakePolicy->setPointerCapture(true);
9453 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
9454 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
9455}
9456
Arthur Hung6d5b4b22022-01-21 07:21:10 +00009457// --- JoystickInputMapperTest ---
9458
9459class JoystickInputMapperTest : public InputMapperTest {
9460protected:
9461 static const int32_t RAW_X_MIN;
9462 static const int32_t RAW_X_MAX;
9463 static const int32_t RAW_Y_MIN;
9464 static const int32_t RAW_Y_MAX;
9465
9466 void SetUp() override {
9467 InputMapperTest::SetUp(InputDeviceClass::JOYSTICK | InputDeviceClass::EXTERNAL);
9468 }
9469 void prepareAxes() {
9470 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
9471 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
9472 }
9473
9474 void processAxis(JoystickInputMapper& mapper, int32_t axis, int32_t value) {
9475 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, axis, value);
9476 }
9477
9478 void processSync(JoystickInputMapper& mapper) {
9479 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
9480 }
9481
9482 void prepareVirtualDisplay(int32_t orientation) {
9483 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH,
9484 VIRTUAL_DISPLAY_HEIGHT, orientation, VIRTUAL_DISPLAY_UNIQUE_ID,
9485 NO_PORT, ViewportType::VIRTUAL);
9486 }
9487};
9488
9489const int32_t JoystickInputMapperTest::RAW_X_MIN = -32767;
9490const int32_t JoystickInputMapperTest::RAW_X_MAX = 32767;
9491const int32_t JoystickInputMapperTest::RAW_Y_MIN = -32767;
9492const int32_t JoystickInputMapperTest::RAW_Y_MAX = 32767;
9493
9494TEST_F(JoystickInputMapperTest, Configure_AssignsDisplayUniqueId) {
9495 prepareAxes();
9496 JoystickInputMapper& mapper = addMapperAndConfigure<JoystickInputMapper>();
9497
9498 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
9499
9500 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
9501
9502 // Send an axis event
9503 processAxis(mapper, ABS_X, 100);
9504 processSync(mapper);
9505
9506 NotifyMotionArgs args;
9507 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9508 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
9509
9510 // Send another axis event
9511 processAxis(mapper, ABS_Y, 100);
9512 processSync(mapper);
9513
9514 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9515 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
9516}
9517
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009518// --- PeripheralControllerTest ---
Chris Yee2b1e5c2021-03-10 22:45:12 -08009519
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009520class PeripheralControllerTest : public testing::Test {
Chris Yee2b1e5c2021-03-10 22:45:12 -08009521protected:
9522 static const char* DEVICE_NAME;
9523 static const char* DEVICE_LOCATION;
9524 static const int32_t DEVICE_ID;
9525 static const int32_t DEVICE_GENERATION;
9526 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07009527 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Chris Yee2b1e5c2021-03-10 22:45:12 -08009528 static const int32_t EVENTHUB_ID;
9529
9530 std::shared_ptr<FakeEventHub> mFakeEventHub;
9531 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07009532 std::unique_ptr<TestInputListener> mFakeListener;
Chris Yee2b1e5c2021-03-10 22:45:12 -08009533 std::unique_ptr<InstrumentedInputReader> mReader;
9534 std::shared_ptr<InputDevice> mDevice;
9535
Dominik Laskowski2f01d772022-03-23 16:01:29 -07009536 virtual void SetUp(ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -08009537 mFakeEventHub = std::make_unique<FakeEventHub>();
9538 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07009539 mFakeListener = std::make_unique<TestInputListener>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009540 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07009541 *mFakeListener);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009542 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
9543 }
9544
9545 void SetUp() override { SetUp(DEVICE_CLASSES); }
9546
9547 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07009548 mFakeListener.reset();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009549 mFakePolicy.clear();
9550 }
9551
9552 void configureDevice(uint32_t changes) {
9553 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
9554 mReader->requestRefreshConfiguration(changes);
9555 mReader->loopOnce();
9556 }
9557 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
9558 }
9559
9560 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
9561 const std::string& location, int32_t eventHubId,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07009562 ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -08009563 InputDeviceIdentifier identifier;
9564 identifier.name = name;
9565 identifier.location = location;
9566 std::shared_ptr<InputDevice> device =
9567 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
9568 identifier);
9569 mReader->pushNextDevice(device);
9570 mFakeEventHub->addDevice(eventHubId, name, classes);
9571 mReader->loopOnce();
9572 return device;
9573 }
9574
9575 template <class T, typename... Args>
9576 T& addControllerAndConfigure(Args... args) {
9577 T& controller = mDevice->addController<T>(EVENTHUB_ID, args...);
9578
9579 return controller;
9580 }
9581};
9582
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009583const char* PeripheralControllerTest::DEVICE_NAME = "device";
9584const char* PeripheralControllerTest::DEVICE_LOCATION = "BLUETOOTH";
9585const int32_t PeripheralControllerTest::DEVICE_ID = END_RESERVED_ID + 1000;
9586const int32_t PeripheralControllerTest::DEVICE_GENERATION = 2;
9587const int32_t PeripheralControllerTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07009588const ftl::Flags<InputDeviceClass> PeripheralControllerTest::DEVICE_CLASSES =
9589 ftl::Flags<InputDeviceClass>(0); // not needed for current tests
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009590const int32_t PeripheralControllerTest::EVENTHUB_ID = 1;
Chris Yee2b1e5c2021-03-10 22:45:12 -08009591
9592// --- BatteryControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009593class BatteryControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -08009594protected:
9595 void SetUp() override {
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009596 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::BATTERY);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009597 }
9598};
9599
9600TEST_F(BatteryControllerTest, GetBatteryCapacity) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009601 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009602
9603 ASSERT_TRUE(controller.getBatteryCapacity(DEFAULT_BATTERY));
9604 ASSERT_EQ(controller.getBatteryCapacity(DEFAULT_BATTERY).value_or(-1), BATTERY_CAPACITY);
9605}
9606
9607TEST_F(BatteryControllerTest, GetBatteryStatus) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009608 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009609
9610 ASSERT_TRUE(controller.getBatteryStatus(DEFAULT_BATTERY));
9611 ASSERT_EQ(controller.getBatteryStatus(DEFAULT_BATTERY).value_or(-1), BATTERY_STATUS);
9612}
9613
9614// --- LightControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009615class LightControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -08009616protected:
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009617 void SetUp() override {
9618 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::LIGHT);
9619 }
Chris Yee2b1e5c2021-03-10 22:45:12 -08009620};
9621
Chris Ye85758332021-05-16 23:05:17 -07009622TEST_F(LightControllerTest, MonoLight) {
9623 RawLightInfo infoMono = {.id = 1,
9624 .name = "Mono",
9625 .maxBrightness = 255,
9626 .flags = InputLightClass::BRIGHTNESS,
9627 .path = ""};
9628 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
Chris Yee2b1e5c2021-03-10 22:45:12 -08009629
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009630 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009631 InputDeviceInfo info;
9632 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009633 std::vector<InputDeviceLightInfo> lights = info.getLights();
9634 ASSERT_EQ(1U, lights.size());
9635 ASSERT_EQ(InputDeviceLightType::MONO, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009636
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009637 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
9638 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009639}
9640
9641TEST_F(LightControllerTest, RGBLight) {
9642 RawLightInfo infoRed = {.id = 1,
9643 .name = "red",
9644 .maxBrightness = 255,
9645 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
9646 .path = ""};
9647 RawLightInfo infoGreen = {.id = 2,
9648 .name = "green",
9649 .maxBrightness = 255,
9650 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
9651 .path = ""};
9652 RawLightInfo infoBlue = {.id = 3,
9653 .name = "blue",
9654 .maxBrightness = 255,
9655 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
9656 .path = ""};
9657 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
9658 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
9659 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
9660
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009661 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009662 InputDeviceInfo info;
9663 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009664 std::vector<InputDeviceLightInfo> lights = info.getLights();
9665 ASSERT_EQ(1U, lights.size());
9666 ASSERT_EQ(InputDeviceLightType::RGB, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009667
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009668 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
9669 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009670}
9671
9672TEST_F(LightControllerTest, MultiColorRGBLight) {
9673 RawLightInfo infoColor = {.id = 1,
9674 .name = "red",
9675 .maxBrightness = 255,
9676 .flags = InputLightClass::BRIGHTNESS |
9677 InputLightClass::MULTI_INTENSITY |
9678 InputLightClass::MULTI_INDEX,
9679 .path = ""};
9680
9681 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
9682
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009683 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009684 InputDeviceInfo info;
9685 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009686 std::vector<InputDeviceLightInfo> lights = info.getLights();
9687 ASSERT_EQ(1U, lights.size());
9688 ASSERT_EQ(InputDeviceLightType::MULTI_COLOR, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009689
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009690 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
9691 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009692}
9693
9694TEST_F(LightControllerTest, PlayerIdLight) {
9695 RawLightInfo info1 = {.id = 1,
9696 .name = "player1",
9697 .maxBrightness = 255,
9698 .flags = InputLightClass::BRIGHTNESS,
9699 .path = ""};
9700 RawLightInfo info2 = {.id = 2,
9701 .name = "player2",
9702 .maxBrightness = 255,
9703 .flags = InputLightClass::BRIGHTNESS,
9704 .path = ""};
9705 RawLightInfo info3 = {.id = 3,
9706 .name = "player3",
9707 .maxBrightness = 255,
9708 .flags = InputLightClass::BRIGHTNESS,
9709 .path = ""};
9710 RawLightInfo info4 = {.id = 4,
9711 .name = "player4",
9712 .maxBrightness = 255,
9713 .flags = InputLightClass::BRIGHTNESS,
9714 .path = ""};
9715 mFakeEventHub->addRawLightInfo(info1.id, std::move(info1));
9716 mFakeEventHub->addRawLightInfo(info2.id, std::move(info2));
9717 mFakeEventHub->addRawLightInfo(info3.id, std::move(info3));
9718 mFakeEventHub->addRawLightInfo(info4.id, std::move(info4));
9719
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009720 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009721 InputDeviceInfo info;
9722 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009723 std::vector<InputDeviceLightInfo> lights = info.getLights();
9724 ASSERT_EQ(1U, lights.size());
9725 ASSERT_EQ(InputDeviceLightType::PLAYER_ID, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009726
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009727 ASSERT_FALSE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
9728 ASSERT_TRUE(controller.setLightPlayerId(lights[0].id, LIGHT_PLAYER_ID));
9729 ASSERT_EQ(controller.getLightPlayerId(lights[0].id).value_or(-1), LIGHT_PLAYER_ID);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009730}
9731
Michael Wrightd02c5b62014-02-10 15:10:22 -08009732} // namespace android