blob: bda77555134dfb9ceac71bbcb791847f0db33078 [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
Michael Wrightd02c5b62014-02-10 15:10:22 -0800378private:
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000379 uint32_t mNextPointerCaptureSequenceNumber = 0;
380
Chris Yea52ade12020-08-27 16:49:20 -0700381 void getReaderConfiguration(InputReaderConfiguration* outConfig) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800382 *outConfig = mConfig;
383 }
384
Prabir Pradhan2853b7a2021-08-23 14:08:51 +0000385 std::shared_ptr<PointerControllerInterface> obtainPointerController(
386 int32_t /*deviceId*/) override {
387 return mPointerController;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800388 }
389
Chris Yea52ade12020-08-27 16:49:20 -0700390 void notifyInputDevicesChanged(const std::vector<InputDeviceInfo>& inputDevices) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700391 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800392 mInputDevices = inputDevices;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700393 mInputDevicesChanged = true;
394 mDevicesChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800395 }
396
Chris Yea52ade12020-08-27 16:49:20 -0700397 std::shared_ptr<KeyCharacterMap> getKeyboardLayoutOverlay(
398 const InputDeviceIdentifier&) override {
Yi Kong9b14ac62018-07-17 13:48:38 -0700399 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800400 }
401
Chris Yea52ade12020-08-27 16:49:20 -0700402 std::string getDeviceAlias(const InputDeviceIdentifier&) override { return ""; }
Prabir Pradhan1aed8582019-12-30 11:46:51 -0800403
404 void waitForInputDevices(std::function<void(bool)> processDevicesChanged) {
405 std::unique_lock<std::mutex> lock(mLock);
406 base::ScopedLockAssertion assumeLocked(mLock);
407
408 const bool devicesChanged =
409 mDevicesChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
410 return mInputDevicesChanged;
411 });
412 ASSERT_NO_FATAL_FAILURE(processDevicesChanged(devicesChanged));
413 mInputDevicesChanged = false;
414 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800415};
416
Michael Wrightd02c5b62014-02-10 15:10:22 -0800417// --- FakeEventHub ---
418
419class FakeEventHub : public EventHubInterface {
420 struct KeyInfo {
421 int32_t keyCode;
422 uint32_t flags;
423 };
424
Chris Yef59a2f42020-10-16 12:55:26 -0700425 struct SensorInfo {
426 InputDeviceSensorType sensorType;
427 int32_t sensorDataIndex;
428 };
429
Michael Wrightd02c5b62014-02-10 15:10:22 -0800430 struct Device {
431 InputDeviceIdentifier identifier;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700432 ftl::Flags<InputDeviceClass> classes;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800433 PropertyMap configuration;
434 KeyedVector<int, RawAbsoluteAxisInfo> absoluteAxes;
435 KeyedVector<int, bool> relativeAxes;
436 KeyedVector<int32_t, int32_t> keyCodeStates;
437 KeyedVector<int32_t, int32_t> scanCodeStates;
438 KeyedVector<int32_t, int32_t> switchStates;
439 KeyedVector<int32_t, int32_t> absoluteAxisValue;
440 KeyedVector<int32_t, KeyInfo> keysByScanCode;
441 KeyedVector<int32_t, KeyInfo> keysByUsageCode;
442 KeyedVector<int32_t, bool> leds;
Philip Junker4af3b3d2021-12-14 10:36:55 +0100443 // fake mapping which would normally come from keyCharacterMap
444 std::unordered_map<int32_t, int32_t> keyCodeMapping;
Chris Yef59a2f42020-10-16 12:55:26 -0700445 std::unordered_map<int32_t, SensorInfo> sensorsByAbsCode;
446 BitArray<MSC_MAX> mscBitmask;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800447 std::vector<VirtualKeyDefinition> virtualKeys;
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700448 bool enabled;
449
450 status_t enable() {
451 enabled = true;
452 return OK;
453 }
454
455 status_t disable() {
456 enabled = false;
457 return OK;
458 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800459
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700460 explicit Device(ftl::Flags<InputDeviceClass> classes) : classes(classes), enabled(true) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800461 };
462
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700463 std::mutex mLock;
464 std::condition_variable mEventsCondition;
465
Michael Wrightd02c5b62014-02-10 15:10:22 -0800466 KeyedVector<int32_t, Device*> mDevices;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100467 std::vector<std::string> mExcludedDevices;
Siarhei Vishniakou370039c2021-02-04 22:09:01 +0000468 std::vector<RawEvent> mEvents GUARDED_BY(mLock);
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600469 std::unordered_map<int32_t /*deviceId*/, std::vector<TouchVideoFrame>> mVideoFrames;
Chris Ye87143712020-11-10 05:05:58 +0000470 std::vector<int32_t> mVibrators = {0, 1};
Chris Ye3fdbfef2021-01-06 18:45:18 -0800471 std::unordered_map<int32_t, RawLightInfo> mRawLightInfos;
472 // Simulates a device light brightness, from light id to light brightness.
473 std::unordered_map<int32_t /* lightId */, int32_t /* brightness*/> mLightBrightness;
474 // Simulates a device light intensities, from light id to light intensities map.
475 std::unordered_map<int32_t /* lightId */, std::unordered_map<LightColor, int32_t>>
476 mLightIntensities;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800477
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700478public:
Michael Wrightd02c5b62014-02-10 15:10:22 -0800479 virtual ~FakeEventHub() {
480 for (size_t i = 0; i < mDevices.size(); i++) {
481 delete mDevices.valueAt(i);
482 }
483 }
484
Michael Wrightd02c5b62014-02-10 15:10:22 -0800485 FakeEventHub() { }
486
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700487 void addDevice(int32_t deviceId, const std::string& name,
488 ftl::Flags<InputDeviceClass> classes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800489 Device* device = new Device(classes);
490 device->identifier.name = name;
491 mDevices.add(deviceId, device);
492
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000493 enqueueEvent(ARBITRARY_TIME, READ_TIME, deviceId, EventHubInterface::DEVICE_ADDED, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800494 }
495
496 void removeDevice(int32_t deviceId) {
497 delete mDevices.valueFor(deviceId);
498 mDevices.removeItem(deviceId);
499
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000500 enqueueEvent(ARBITRARY_TIME, READ_TIME, deviceId, EventHubInterface::DEVICE_REMOVED, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800501 }
502
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700503 bool isDeviceEnabled(int32_t deviceId) {
504 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700505 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700506 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
507 return false;
508 }
509 return device->enabled;
510 }
511
512 status_t enableDevice(int32_t deviceId) {
513 status_t result;
514 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700515 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700516 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
517 return BAD_VALUE;
518 }
519 if (device->enabled) {
520 ALOGW("Duplicate call to %s, device %" PRId32 " already enabled", __func__, deviceId);
521 return OK;
522 }
523 result = device->enable();
524 return result;
525 }
526
527 status_t disableDevice(int32_t deviceId) {
528 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700529 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700530 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
531 return BAD_VALUE;
532 }
533 if (!device->enabled) {
534 ALOGW("Duplicate call to %s, device %" PRId32 " already disabled", __func__, deviceId);
535 return OK;
536 }
537 return device->disable();
538 }
539
Michael Wrightd02c5b62014-02-10 15:10:22 -0800540 void finishDeviceScan() {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000541 enqueueEvent(ARBITRARY_TIME, READ_TIME, 0, EventHubInterface::FINISHED_DEVICE_SCAN, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800542 }
543
544 void addConfigurationProperty(int32_t deviceId, const String8& key, const String8& value) {
545 Device* device = getDevice(deviceId);
546 device->configuration.addProperty(key, value);
547 }
548
549 void addConfigurationMap(int32_t deviceId, const PropertyMap* configuration) {
550 Device* device = getDevice(deviceId);
551 device->configuration.addAll(configuration);
552 }
553
554 void addAbsoluteAxis(int32_t deviceId, int axis,
555 int32_t minValue, int32_t maxValue, int flat, int fuzz, int resolution = 0) {
556 Device* device = getDevice(deviceId);
557
558 RawAbsoluteAxisInfo info;
559 info.valid = true;
560 info.minValue = minValue;
561 info.maxValue = maxValue;
562 info.flat = flat;
563 info.fuzz = fuzz;
564 info.resolution = resolution;
565 device->absoluteAxes.add(axis, info);
566 }
567
568 void addRelativeAxis(int32_t deviceId, int32_t axis) {
569 Device* device = getDevice(deviceId);
570 device->relativeAxes.add(axis, true);
571 }
572
573 void setKeyCodeState(int32_t deviceId, int32_t keyCode, int32_t state) {
574 Device* device = getDevice(deviceId);
575 device->keyCodeStates.replaceValueFor(keyCode, state);
576 }
577
578 void setScanCodeState(int32_t deviceId, int32_t scanCode, int32_t state) {
579 Device* device = getDevice(deviceId);
580 device->scanCodeStates.replaceValueFor(scanCode, state);
581 }
582
583 void setSwitchState(int32_t deviceId, int32_t switchCode, int32_t state) {
584 Device* device = getDevice(deviceId);
585 device->switchStates.replaceValueFor(switchCode, state);
586 }
587
588 void setAbsoluteAxisValue(int32_t deviceId, int32_t axis, int32_t value) {
589 Device* device = getDevice(deviceId);
590 device->absoluteAxisValue.replaceValueFor(axis, value);
591 }
592
593 void addKey(int32_t deviceId, int32_t scanCode, int32_t usageCode,
594 int32_t keyCode, uint32_t flags) {
595 Device* device = getDevice(deviceId);
596 KeyInfo info;
597 info.keyCode = keyCode;
598 info.flags = flags;
599 if (scanCode) {
600 device->keysByScanCode.add(scanCode, info);
601 }
602 if (usageCode) {
603 device->keysByUsageCode.add(usageCode, info);
604 }
605 }
606
Philip Junker4af3b3d2021-12-14 10:36:55 +0100607 void addKeyCodeMapping(int32_t deviceId, int32_t fromKeyCode, int32_t toKeyCode) {
608 Device* device = getDevice(deviceId);
609 device->keyCodeMapping.insert_or_assign(fromKeyCode, toKeyCode);
610 }
611
Michael Wrightd02c5b62014-02-10 15:10:22 -0800612 void addLed(int32_t deviceId, int32_t led, bool initialState) {
613 Device* device = getDevice(deviceId);
614 device->leds.add(led, initialState);
615 }
616
Chris Yef59a2f42020-10-16 12:55:26 -0700617 void addSensorAxis(int32_t deviceId, int32_t absCode, InputDeviceSensorType sensorType,
618 int32_t sensorDataIndex) {
619 Device* device = getDevice(deviceId);
620 SensorInfo info;
621 info.sensorType = sensorType;
622 info.sensorDataIndex = sensorDataIndex;
623 device->sensorsByAbsCode.emplace(absCode, info);
624 }
625
626 void setMscEvent(int32_t deviceId, int32_t mscEvent) {
627 Device* device = getDevice(deviceId);
628 typename BitArray<MSC_MAX>::Buffer buffer;
629 buffer[mscEvent / 32] = 1 << mscEvent % 32;
630 device->mscBitmask.loadFromBuffer(buffer);
631 }
632
Chris Ye3fdbfef2021-01-06 18:45:18 -0800633 void addRawLightInfo(int32_t rawId, RawLightInfo&& info) {
634 mRawLightInfos.emplace(rawId, std::move(info));
635 }
636
637 void fakeLightBrightness(int32_t rawId, int32_t brightness) {
638 mLightBrightness.emplace(rawId, brightness);
639 }
640
641 void fakeLightIntensities(int32_t rawId,
642 const std::unordered_map<LightColor, int32_t> intensities) {
643 mLightIntensities.emplace(rawId, std::move(intensities));
644 }
645
Michael Wrightd02c5b62014-02-10 15:10:22 -0800646 bool getLedState(int32_t deviceId, int32_t led) {
647 Device* device = getDevice(deviceId);
648 return device->leds.valueFor(led);
649 }
650
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100651 std::vector<std::string>& getExcludedDevices() {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800652 return mExcludedDevices;
653 }
654
655 void addVirtualKeyDefinition(int32_t deviceId, const VirtualKeyDefinition& definition) {
656 Device* device = getDevice(deviceId);
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800657 device->virtualKeys.push_back(definition);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800658 }
659
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000660 void enqueueEvent(nsecs_t when, nsecs_t readTime, int32_t deviceId, int32_t type, int32_t code,
661 int32_t value) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700662 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800663 RawEvent event;
664 event.when = when;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000665 event.readTime = readTime;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800666 event.deviceId = deviceId;
667 event.type = type;
668 event.code = code;
669 event.value = value;
670 mEvents.push_back(event);
671
672 if (type == EV_ABS) {
673 setAbsoluteAxisValue(deviceId, code, value);
674 }
675 }
676
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600677 void setVideoFrames(std::unordered_map<int32_t /*deviceId*/,
678 std::vector<TouchVideoFrame>> videoFrames) {
679 mVideoFrames = std::move(videoFrames);
680 }
681
Michael Wrightd02c5b62014-02-10 15:10:22 -0800682 void assertQueueIsEmpty() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700683 std::unique_lock<std::mutex> lock(mLock);
684 base::ScopedLockAssertion assumeLocked(mLock);
685 const bool queueIsEmpty =
686 mEventsCondition.wait_for(lock, WAIT_TIMEOUT,
687 [this]() REQUIRES(mLock) { return mEvents.size() == 0; });
688 if (!queueIsEmpty) {
689 FAIL() << "Timed out waiting for EventHub queue to be emptied.";
690 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800691 }
692
693private:
694 Device* getDevice(int32_t deviceId) const {
695 ssize_t index = mDevices.indexOfKey(deviceId);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100696 return index >= 0 ? mDevices.valueAt(index) : nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800697 }
698
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700699 ftl::Flags<InputDeviceClass> getDeviceClasses(int32_t deviceId) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800700 Device* device = getDevice(deviceId);
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700701 return device ? device->classes : ftl::Flags<InputDeviceClass>(0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800702 }
703
Chris Yea52ade12020-08-27 16:49:20 -0700704 InputDeviceIdentifier getDeviceIdentifier(int32_t deviceId) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800705 Device* device = getDevice(deviceId);
706 return device ? device->identifier : InputDeviceIdentifier();
707 }
708
Chris Yea52ade12020-08-27 16:49:20 -0700709 int32_t getDeviceControllerNumber(int32_t) const override { return 0; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800710
Chris Yea52ade12020-08-27 16:49:20 -0700711 void getConfiguration(int32_t deviceId, PropertyMap* outConfiguration) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800712 Device* device = getDevice(deviceId);
713 if (device) {
714 *outConfiguration = device->configuration;
715 }
716 }
717
Chris Yea52ade12020-08-27 16:49:20 -0700718 status_t getAbsoluteAxisInfo(int32_t deviceId, int axis,
719 RawAbsoluteAxisInfo* outAxisInfo) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800720 Device* device = getDevice(deviceId);
Arthur Hung9da14732019-09-02 16:16:58 +0800721 if (device && device->enabled) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800722 ssize_t index = device->absoluteAxes.indexOfKey(axis);
723 if (index >= 0) {
724 *outAxisInfo = device->absoluteAxes.valueAt(index);
725 return OK;
726 }
727 }
728 outAxisInfo->clear();
729 return -1;
730 }
731
Chris Yea52ade12020-08-27 16:49:20 -0700732 bool hasRelativeAxis(int32_t deviceId, int axis) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800733 Device* device = getDevice(deviceId);
734 if (device) {
735 return device->relativeAxes.indexOfKey(axis) >= 0;
736 }
737 return false;
738 }
739
Chris Yea52ade12020-08-27 16:49:20 -0700740 bool hasInputProperty(int32_t, int) const override { return false; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800741
Chris Yef59a2f42020-10-16 12:55:26 -0700742 bool hasMscEvent(int32_t deviceId, int mscEvent) const override final {
743 Device* device = getDevice(deviceId);
744 if (device) {
745 return mscEvent >= 0 && mscEvent <= MSC_MAX ? device->mscBitmask.test(mscEvent) : false;
746 }
747 return false;
748 }
749
Chris Yea52ade12020-08-27 16:49:20 -0700750 status_t mapKey(int32_t deviceId, int32_t scanCode, int32_t usageCode, int32_t metaState,
751 int32_t* outKeycode, int32_t* outMetaState, uint32_t* outFlags) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800752 Device* device = getDevice(deviceId);
753 if (device) {
754 const KeyInfo* key = getKey(device, scanCode, usageCode);
755 if (key) {
756 if (outKeycode) {
757 *outKeycode = key->keyCode;
758 }
759 if (outFlags) {
760 *outFlags = key->flags;
761 }
Dmitry Torokhov0faaa0b2015-09-24 13:13:55 -0700762 if (outMetaState) {
763 *outMetaState = metaState;
764 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800765 return OK;
766 }
767 }
768 return NAME_NOT_FOUND;
769 }
770
771 const KeyInfo* getKey(Device* device, int32_t scanCode, int32_t usageCode) const {
772 if (usageCode) {
773 ssize_t index = device->keysByUsageCode.indexOfKey(usageCode);
774 if (index >= 0) {
775 return &device->keysByUsageCode.valueAt(index);
776 }
777 }
778 if (scanCode) {
779 ssize_t index = device->keysByScanCode.indexOfKey(scanCode);
780 if (index >= 0) {
781 return &device->keysByScanCode.valueAt(index);
782 }
783 }
Yi Kong9b14ac62018-07-17 13:48:38 -0700784 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800785 }
786
Chris Yea52ade12020-08-27 16:49:20 -0700787 status_t mapAxis(int32_t, int32_t, AxisInfo*) const override { return NAME_NOT_FOUND; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800788
Chris Yef59a2f42020-10-16 12:55:26 -0700789 base::Result<std::pair<InputDeviceSensorType, int32_t>> mapSensor(int32_t deviceId,
790 int32_t absCode) {
791 Device* device = getDevice(deviceId);
792 if (!device) {
793 return Errorf("Sensor device not found.");
794 }
795 auto it = device->sensorsByAbsCode.find(absCode);
796 if (it == device->sensorsByAbsCode.end()) {
797 return Errorf("Sensor map not found.");
798 }
799 const SensorInfo& info = it->second;
800 return std::make_pair(info.sensorType, info.sensorDataIndex);
801 }
802
Chris Yea52ade12020-08-27 16:49:20 -0700803 void setExcludedDevices(const std::vector<std::string>& devices) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800804 mExcludedDevices = devices;
805 }
806
Siarhei Vishniakou370039c2021-02-04 22:09:01 +0000807 size_t getEvents(int, RawEvent* buffer, size_t bufferSize) override {
808 std::scoped_lock lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800809
Siarhei Vishniakou370039c2021-02-04 22:09:01 +0000810 const size_t filledSize = std::min(mEvents.size(), bufferSize);
811 std::copy(mEvents.begin(), mEvents.begin() + filledSize, buffer);
812
813 mEvents.erase(mEvents.begin(), mEvents.begin() + filledSize);
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700814 mEventsCondition.notify_all();
Siarhei Vishniakou370039c2021-02-04 22:09:01 +0000815 return filledSize;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800816 }
817
Chris Yea52ade12020-08-27 16:49:20 -0700818 std::vector<TouchVideoFrame> getVideoFrames(int32_t deviceId) override {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600819 auto it = mVideoFrames.find(deviceId);
820 if (it != mVideoFrames.end()) {
821 std::vector<TouchVideoFrame> frames = std::move(it->second);
822 mVideoFrames.erase(deviceId);
823 return frames;
824 }
Siarhei Vishniakouadd89292018-12-13 19:23:36 -0800825 return {};
826 }
827
Chris Yea52ade12020-08-27 16:49:20 -0700828 int32_t getScanCodeState(int32_t deviceId, int32_t scanCode) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800829 Device* device = getDevice(deviceId);
830 if (device) {
831 ssize_t index = device->scanCodeStates.indexOfKey(scanCode);
832 if (index >= 0) {
833 return device->scanCodeStates.valueAt(index);
834 }
835 }
836 return AKEY_STATE_UNKNOWN;
837 }
838
Chris Yea52ade12020-08-27 16:49:20 -0700839 int32_t getKeyCodeState(int32_t deviceId, int32_t keyCode) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800840 Device* device = getDevice(deviceId);
841 if (device) {
842 ssize_t index = device->keyCodeStates.indexOfKey(keyCode);
843 if (index >= 0) {
844 return device->keyCodeStates.valueAt(index);
845 }
846 }
847 return AKEY_STATE_UNKNOWN;
848 }
849
Chris Yea52ade12020-08-27 16:49:20 -0700850 int32_t getSwitchState(int32_t deviceId, int32_t sw) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800851 Device* device = getDevice(deviceId);
852 if (device) {
853 ssize_t index = device->switchStates.indexOfKey(sw);
854 if (index >= 0) {
855 return device->switchStates.valueAt(index);
856 }
857 }
858 return AKEY_STATE_UNKNOWN;
859 }
860
Chris Yea52ade12020-08-27 16:49:20 -0700861 status_t getAbsoluteAxisValue(int32_t deviceId, int32_t axis,
862 int32_t* outValue) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800863 Device* device = getDevice(deviceId);
864 if (device) {
865 ssize_t index = device->absoluteAxisValue.indexOfKey(axis);
866 if (index >= 0) {
867 *outValue = device->absoluteAxisValue.valueAt(index);
868 return OK;
869 }
870 }
871 *outValue = 0;
872 return -1;
873 }
874
Philip Junker4af3b3d2021-12-14 10:36:55 +0100875 int32_t getKeyCodeForKeyLocation(int32_t deviceId, int32_t locationKeyCode) const override {
876 Device* device = getDevice(deviceId);
877 if (!device) {
878 return AKEYCODE_UNKNOWN;
879 }
880 auto it = device->keyCodeMapping.find(locationKeyCode);
881 return it != device->keyCodeMapping.end() ? it->second : locationKeyCode;
882 }
883
Chris Yea52ade12020-08-27 16:49:20 -0700884 // Return true if the device has non-empty key layout.
885 bool markSupportedKeyCodes(int32_t deviceId, size_t numCodes, const int32_t* keyCodes,
886 uint8_t* outFlags) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800887 bool result = false;
888 Device* device = getDevice(deviceId);
889 if (device) {
Chris Yea52ade12020-08-27 16:49:20 -0700890 result = device->keysByScanCode.size() > 0 || device->keysByUsageCode.size() > 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800891 for (size_t i = 0; i < numCodes; i++) {
892 for (size_t j = 0; j < device->keysByScanCode.size(); j++) {
893 if (keyCodes[i] == device->keysByScanCode.valueAt(j).keyCode) {
894 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800895 }
896 }
897 for (size_t j = 0; j < device->keysByUsageCode.size(); j++) {
898 if (keyCodes[i] == device->keysByUsageCode.valueAt(j).keyCode) {
899 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800900 }
901 }
902 }
903 }
904 return result;
905 }
906
Chris Yea52ade12020-08-27 16:49:20 -0700907 bool hasScanCode(int32_t deviceId, int32_t scanCode) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800908 Device* device = getDevice(deviceId);
909 if (device) {
910 ssize_t index = device->keysByScanCode.indexOfKey(scanCode);
911 return index >= 0;
912 }
913 return false;
914 }
915
Arthur Hungcb40a002021-08-03 14:31:01 +0000916 bool hasKeyCode(int32_t deviceId, int32_t keyCode) const override {
917 Device* device = getDevice(deviceId);
918 if (!device) {
919 return false;
920 }
921 for (size_t i = 0; i < device->keysByScanCode.size(); i++) {
922 if (keyCode == device->keysByScanCode.valueAt(i).keyCode) {
923 return true;
924 }
925 }
926 for (size_t j = 0; j < device->keysByUsageCode.size(); j++) {
927 if (keyCode == device->keysByUsageCode.valueAt(j).keyCode) {
928 return true;
929 }
930 }
931 return false;
932 }
933
Chris Yea52ade12020-08-27 16:49:20 -0700934 bool hasLed(int32_t deviceId, int32_t led) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800935 Device* device = getDevice(deviceId);
936 return device && device->leds.indexOfKey(led) >= 0;
937 }
938
Chris Yea52ade12020-08-27 16:49:20 -0700939 void setLedState(int32_t deviceId, int32_t led, bool on) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800940 Device* device = getDevice(deviceId);
941 if (device) {
942 ssize_t index = device->leds.indexOfKey(led);
943 if (index >= 0) {
944 device->leds.replaceValueAt(led, on);
945 } else {
946 ADD_FAILURE()
947 << "Attempted to set the state of an LED that the EventHub declared "
948 "was not present. led=" << led;
949 }
950 }
951 }
952
Chris Yea52ade12020-08-27 16:49:20 -0700953 void getVirtualKeyDefinitions(
954 int32_t deviceId, std::vector<VirtualKeyDefinition>& outVirtualKeys) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800955 outVirtualKeys.clear();
956
957 Device* device = getDevice(deviceId);
958 if (device) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800959 outVirtualKeys = device->virtualKeys;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800960 }
961 }
962
Chris Yea52ade12020-08-27 16:49:20 -0700963 const std::shared_ptr<KeyCharacterMap> getKeyCharacterMap(int32_t) const override {
Yi Kong9b14ac62018-07-17 13:48:38 -0700964 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800965 }
966
Chris Yea52ade12020-08-27 16:49:20 -0700967 bool setKeyboardLayoutOverlay(int32_t, std::shared_ptr<KeyCharacterMap>) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800968 return false;
969 }
970
Chris Yea52ade12020-08-27 16:49:20 -0700971 void vibrate(int32_t, const VibrationElement&) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800972
Chris Yea52ade12020-08-27 16:49:20 -0700973 void cancelVibrate(int32_t) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800974
Chris Ye87143712020-11-10 05:05:58 +0000975 std::vector<int32_t> getVibratorIds(int32_t deviceId) override { return mVibrators; };
976
Chris Yee2b1e5c2021-03-10 22:45:12 -0800977 std::optional<int32_t> getBatteryCapacity(int32_t, int32_t) const override {
978 return BATTERY_CAPACITY;
979 }
Kim Low03ea0352020-11-06 12:45:07 -0800980
Chris Yee2b1e5c2021-03-10 22:45:12 -0800981 std::optional<int32_t> getBatteryStatus(int32_t, int32_t) const override {
982 return BATTERY_STATUS;
983 }
984
985 const std::vector<int32_t> getRawBatteryIds(int32_t deviceId) { return {}; }
986
987 std::optional<RawBatteryInfo> getRawBatteryInfo(int32_t deviceId, int32_t batteryId) {
988 return std::nullopt;
989 }
Kim Low03ea0352020-11-06 12:45:07 -0800990
Chris Ye3fdbfef2021-01-06 18:45:18 -0800991 const std::vector<int32_t> getRawLightIds(int32_t deviceId) override {
992 std::vector<int32_t> ids;
993 for (const auto& [rawId, info] : mRawLightInfos) {
994 ids.push_back(rawId);
995 }
996 return ids;
997 }
998
999 std::optional<RawLightInfo> getRawLightInfo(int32_t deviceId, int32_t lightId) override {
1000 auto it = mRawLightInfos.find(lightId);
1001 if (it == mRawLightInfos.end()) {
1002 return std::nullopt;
1003 }
1004 return it->second;
1005 }
1006
1007 void setLightBrightness(int32_t deviceId, int32_t lightId, int32_t brightness) override {
1008 mLightBrightness.emplace(lightId, brightness);
1009 }
1010
1011 void setLightIntensities(int32_t deviceId, int32_t lightId,
1012 std::unordered_map<LightColor, int32_t> intensities) override {
1013 mLightIntensities.emplace(lightId, intensities);
1014 };
1015
1016 std::optional<int32_t> getLightBrightness(int32_t deviceId, int32_t lightId) override {
1017 auto lightIt = mLightBrightness.find(lightId);
1018 if (lightIt == mLightBrightness.end()) {
1019 return std::nullopt;
1020 }
1021 return lightIt->second;
1022 }
1023
1024 std::optional<std::unordered_map<LightColor, int32_t>> getLightIntensities(
1025 int32_t deviceId, int32_t lightId) override {
1026 auto lightIt = mLightIntensities.find(lightId);
1027 if (lightIt == mLightIntensities.end()) {
1028 return std::nullopt;
1029 }
1030 return lightIt->second;
1031 };
1032
Narayan Kamath39efe3e2014-10-17 10:37:08 +01001033 virtual bool isExternal(int32_t) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001034 return false;
1035 }
1036
Chris Yea52ade12020-08-27 16:49:20 -07001037 void dump(std::string&) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001038
Chris Yea52ade12020-08-27 16:49:20 -07001039 void monitor() override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001040
Chris Yea52ade12020-08-27 16:49:20 -07001041 void requestReopenDevices() override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001042
Chris Yea52ade12020-08-27 16:49:20 -07001043 void wake() override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001044};
1045
Michael Wrightd02c5b62014-02-10 15:10:22 -08001046// --- FakeInputMapper ---
1047
1048class FakeInputMapper : public InputMapper {
1049 uint32_t mSources;
1050 int32_t mKeyboardType;
1051 int32_t mMetaState;
1052 KeyedVector<int32_t, int32_t> mKeyCodeStates;
1053 KeyedVector<int32_t, int32_t> mScanCodeStates;
1054 KeyedVector<int32_t, int32_t> mSwitchStates;
Philip Junker4af3b3d2021-12-14 10:36:55 +01001055 // fake mapping which would normally come from keyCharacterMap
1056 std::unordered_map<int32_t, int32_t> mKeyCodeMapping;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001057 std::vector<int32_t> mSupportedKeyCodes;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001058
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001059 std::mutex mLock;
1060 std::condition_variable mStateChangedCondition;
1061 bool mConfigureWasCalled GUARDED_BY(mLock);
1062 bool mResetWasCalled GUARDED_BY(mLock);
1063 bool mProcessWasCalled GUARDED_BY(mLock);
1064 RawEvent mLastEvent GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001065
Arthur Hungc23540e2018-11-29 20:42:11 +08001066 std::optional<DisplayViewport> mViewport;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001067public:
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001068 FakeInputMapper(InputDeviceContext& deviceContext, uint32_t sources)
1069 : InputMapper(deviceContext),
1070 mSources(sources),
1071 mKeyboardType(AINPUT_KEYBOARD_TYPE_NONE),
Michael Wrightd02c5b62014-02-10 15:10:22 -08001072 mMetaState(0),
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001073 mConfigureWasCalled(false),
1074 mResetWasCalled(false),
1075 mProcessWasCalled(false) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001076
Chris Yea52ade12020-08-27 16:49:20 -07001077 virtual ~FakeInputMapper() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001078
1079 void setKeyboardType(int32_t keyboardType) {
1080 mKeyboardType = keyboardType;
1081 }
1082
1083 void setMetaState(int32_t metaState) {
1084 mMetaState = metaState;
1085 }
1086
1087 void assertConfigureWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001088 std::unique_lock<std::mutex> lock(mLock);
1089 base::ScopedLockAssertion assumeLocked(mLock);
1090 const bool configureCalled =
1091 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1092 return mConfigureWasCalled;
1093 });
1094 if (!configureCalled) {
1095 FAIL() << "Expected configure() to have been called.";
1096 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001097 mConfigureWasCalled = false;
1098 }
1099
1100 void assertResetWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001101 std::unique_lock<std::mutex> lock(mLock);
1102 base::ScopedLockAssertion assumeLocked(mLock);
1103 const bool resetCalled =
1104 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1105 return mResetWasCalled;
1106 });
1107 if (!resetCalled) {
1108 FAIL() << "Expected reset() to have been called.";
1109 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001110 mResetWasCalled = false;
1111 }
1112
Yi Kong9b14ac62018-07-17 13:48:38 -07001113 void assertProcessWasCalled(RawEvent* outLastEvent = nullptr) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001114 std::unique_lock<std::mutex> lock(mLock);
1115 base::ScopedLockAssertion assumeLocked(mLock);
1116 const bool processCalled =
1117 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1118 return mProcessWasCalled;
1119 });
1120 if (!processCalled) {
1121 FAIL() << "Expected process() to have been called.";
1122 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001123 if (outLastEvent) {
1124 *outLastEvent = mLastEvent;
1125 }
1126 mProcessWasCalled = false;
1127 }
1128
1129 void setKeyCodeState(int32_t keyCode, int32_t state) {
1130 mKeyCodeStates.replaceValueFor(keyCode, state);
1131 }
1132
1133 void setScanCodeState(int32_t scanCode, int32_t state) {
1134 mScanCodeStates.replaceValueFor(scanCode, state);
1135 }
1136
1137 void setSwitchState(int32_t switchCode, int32_t state) {
1138 mSwitchStates.replaceValueFor(switchCode, state);
1139 }
1140
1141 void addSupportedKeyCode(int32_t keyCode) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001142 mSupportedKeyCodes.push_back(keyCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001143 }
1144
Philip Junker4af3b3d2021-12-14 10:36:55 +01001145 void addKeyCodeMapping(int32_t fromKeyCode, int32_t toKeyCode) {
1146 mKeyCodeMapping.insert_or_assign(fromKeyCode, toKeyCode);
1147 }
1148
Michael Wrightd02c5b62014-02-10 15:10:22 -08001149private:
Philip Junker4af3b3d2021-12-14 10:36:55 +01001150 uint32_t getSources() const override { return mSources; }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001151
Chris Yea52ade12020-08-27 16:49:20 -07001152 void populateDeviceInfo(InputDeviceInfo* deviceInfo) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001153 InputMapper::populateDeviceInfo(deviceInfo);
1154
1155 if (mKeyboardType != AINPUT_KEYBOARD_TYPE_NONE) {
1156 deviceInfo->setKeyboardType(mKeyboardType);
1157 }
1158 }
1159
Chris Yea52ade12020-08-27 16:49:20 -07001160 void configure(nsecs_t, const InputReaderConfiguration* config, uint32_t changes) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001161 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001162 mConfigureWasCalled = true;
Arthur Hungc23540e2018-11-29 20:42:11 +08001163
1164 // Find the associated viewport if exist.
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001165 const std::optional<uint8_t> displayPort = getDeviceContext().getAssociatedDisplayPort();
Arthur Hungc23540e2018-11-29 20:42:11 +08001166 if (displayPort && (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
1167 mViewport = config->getDisplayViewportByPort(*displayPort);
1168 }
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001169
1170 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001171 }
1172
Chris Yea52ade12020-08-27 16:49:20 -07001173 void reset(nsecs_t) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001174 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001175 mResetWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001176 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001177 }
1178
Chris Yea52ade12020-08-27 16:49:20 -07001179 void process(const RawEvent* rawEvent) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001180 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001181 mLastEvent = *rawEvent;
1182 mProcessWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001183 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001184 }
1185
Chris Yea52ade12020-08-27 16:49:20 -07001186 int32_t getKeyCodeState(uint32_t, int32_t keyCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001187 ssize_t index = mKeyCodeStates.indexOfKey(keyCode);
1188 return index >= 0 ? mKeyCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1189 }
1190
Philip Junker4af3b3d2021-12-14 10:36:55 +01001191 int32_t getKeyCodeForKeyLocation(int32_t locationKeyCode) const override {
1192 auto it = mKeyCodeMapping.find(locationKeyCode);
1193 return it != mKeyCodeMapping.end() ? it->second : locationKeyCode;
1194 }
1195
Chris Yea52ade12020-08-27 16:49:20 -07001196 int32_t getScanCodeState(uint32_t, int32_t scanCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001197 ssize_t index = mScanCodeStates.indexOfKey(scanCode);
1198 return index >= 0 ? mScanCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1199 }
1200
Chris Yea52ade12020-08-27 16:49:20 -07001201 int32_t getSwitchState(uint32_t, int32_t switchCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001202 ssize_t index = mSwitchStates.indexOfKey(switchCode);
1203 return index >= 0 ? mSwitchStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1204 }
1205
Chris Yea52ade12020-08-27 16:49:20 -07001206 // Return true if the device has non-empty key layout.
1207 bool markSupportedKeyCodes(uint32_t, size_t numCodes, const int32_t* keyCodes,
1208 uint8_t* outFlags) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001209 for (size_t i = 0; i < numCodes; i++) {
1210 for (size_t j = 0; j < mSupportedKeyCodes.size(); j++) {
1211 if (keyCodes[i] == mSupportedKeyCodes[j]) {
1212 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001213 }
1214 }
1215 }
Chris Yea52ade12020-08-27 16:49:20 -07001216 bool result = mSupportedKeyCodes.size() > 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001217 return result;
1218 }
1219
1220 virtual int32_t getMetaState() {
1221 return mMetaState;
1222 }
1223
1224 virtual void fadePointer() {
1225 }
Arthur Hungc23540e2018-11-29 20:42:11 +08001226
1227 virtual std::optional<int32_t> getAssociatedDisplay() {
1228 if (mViewport) {
1229 return std::make_optional(mViewport->displayId);
1230 }
1231 return std::nullopt;
1232 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001233};
1234
1235
1236// --- InstrumentedInputReader ---
1237
1238class InstrumentedInputReader : public InputReader {
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001239 std::queue<std::shared_ptr<InputDevice>> mNextDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001240
1241public:
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001242 InstrumentedInputReader(std::shared_ptr<EventHubInterface> eventHub,
1243 const sp<InputReaderPolicyInterface>& policy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001244 InputListenerInterface& listener)
arthurhungdcef2dc2020-08-11 14:47:50 +08001245 : InputReader(eventHub, policy, listener), mFakeContext(this) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001246
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001247 virtual ~InstrumentedInputReader() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001248
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001249 void pushNextDevice(std::shared_ptr<InputDevice> device) { mNextDevices.push(device); }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001250
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001251 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001252 const std::string& location = "") {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001253 InputDeviceIdentifier identifier;
1254 identifier.name = name;
Arthur Hungc23540e2018-11-29 20:42:11 +08001255 identifier.location = location;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001256 int32_t generation = deviceId + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08001257 return std::make_shared<InputDevice>(&mFakeContext, deviceId, generation, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001258 }
1259
Prabir Pradhan28efc192019-11-05 01:10:04 +00001260 // Make the protected loopOnce method accessible to tests.
1261 using InputReader::loopOnce;
1262
Michael Wrightd02c5b62014-02-10 15:10:22 -08001263protected:
Chris Ye1c2e0892020-11-30 21:41:44 -08001264 virtual std::shared_ptr<InputDevice> createDeviceLocked(int32_t eventHubId,
1265 const InputDeviceIdentifier& identifier)
1266 REQUIRES(mLock) {
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001267 if (!mNextDevices.empty()) {
1268 std::shared_ptr<InputDevice> device(std::move(mNextDevices.front()));
1269 mNextDevices.pop();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001270 return device;
1271 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001272 return InputReader::createDeviceLocked(eventHubId, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001273 }
1274
arthurhungdcef2dc2020-08-11 14:47:50 +08001275 // --- FakeInputReaderContext ---
1276 class FakeInputReaderContext : public ContextImpl {
1277 int32_t mGlobalMetaState;
1278 bool mUpdateGlobalMetaStateWasCalled;
1279 int32_t mGeneration;
1280
1281 public:
1282 FakeInputReaderContext(InputReader* reader)
1283 : ContextImpl(reader),
1284 mGlobalMetaState(0),
1285 mUpdateGlobalMetaStateWasCalled(false),
1286 mGeneration(1) {}
1287
1288 virtual ~FakeInputReaderContext() {}
1289
1290 void assertUpdateGlobalMetaStateWasCalled() {
1291 ASSERT_TRUE(mUpdateGlobalMetaStateWasCalled)
1292 << "Expected updateGlobalMetaState() to have been called.";
1293 mUpdateGlobalMetaStateWasCalled = false;
1294 }
1295
1296 void setGlobalMetaState(int32_t state) { mGlobalMetaState = state; }
1297
1298 uint32_t getGeneration() { return mGeneration; }
1299
1300 void updateGlobalMetaState() override {
1301 mUpdateGlobalMetaStateWasCalled = true;
1302 ContextImpl::updateGlobalMetaState();
1303 }
1304
1305 int32_t getGlobalMetaState() override {
1306 return mGlobalMetaState | ContextImpl::getGlobalMetaState();
1307 }
1308
1309 int32_t bumpGeneration() override {
1310 mGeneration = ContextImpl::bumpGeneration();
1311 return mGeneration;
1312 }
1313 } mFakeContext;
1314
Michael Wrightd02c5b62014-02-10 15:10:22 -08001315 friend class InputReaderTest;
arthurhungdcef2dc2020-08-11 14:47:50 +08001316
1317public:
1318 FakeInputReaderContext* getContext() { return &mFakeContext; }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001319};
1320
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001321// --- InputReaderPolicyTest ---
1322class InputReaderPolicyTest : public testing::Test {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001323protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001324 sp<FakeInputReaderPolicy> mFakePolicy;
1325
Chris Yea52ade12020-08-27 16:49:20 -07001326 void SetUp() override { mFakePolicy = new FakeInputReaderPolicy(); }
1327 void TearDown() override { mFakePolicy.clear(); }
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001328};
1329
1330/**
1331 * Check that empty set of viewports is an acceptable configuration.
1332 * Also try to get internal viewport two different ways - by type and by uniqueId.
1333 *
1334 * There will be confusion if two viewports with empty uniqueId and identical type are present.
1335 * Such configuration is not currently allowed.
1336 */
1337TEST_F(InputReaderPolicyTest, Viewports_GetCleared) {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001338 static const std::string uniqueId = "local:0";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001339
1340 // We didn't add any viewports yet, so there shouldn't be any.
1341 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001342 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001343 ASSERT_FALSE(internalViewport);
1344
1345 // Add an internal viewport, then clear it
1346 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001347 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId, NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001348 ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001349
1350 // Check matching by uniqueId
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001351 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001352 ASSERT_TRUE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001353 ASSERT_EQ(ViewportType::INTERNAL, internalViewport->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001354
1355 // Check matching by viewport type
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001356 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001357 ASSERT_TRUE(internalViewport);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001358 ASSERT_EQ(uniqueId, internalViewport->uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001359
1360 mFakePolicy->clearViewports();
1361 // Make sure nothing is found after clear
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001362 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001363 ASSERT_FALSE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001364 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001365 ASSERT_FALSE(internalViewport);
1366}
1367
1368TEST_F(InputReaderPolicyTest, Viewports_GetByType) {
1369 const std::string internalUniqueId = "local:0";
1370 const std::string externalUniqueId = "local:1";
1371 const std::string virtualUniqueId1 = "virtual:2";
1372 const std::string virtualUniqueId2 = "virtual:3";
1373 constexpr int32_t virtualDisplayId1 = 2;
1374 constexpr int32_t virtualDisplayId2 = 3;
1375
1376 // Add an internal viewport
1377 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001378 DISPLAY_ORIENTATION_0, true /*isActive*/, internalUniqueId,
1379 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001380 // Add an external viewport
1381 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001382 DISPLAY_ORIENTATION_0, true /*isActive*/, externalUniqueId,
1383 NO_PORT, ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001384 // Add an virtual viewport
1385 mFakePolicy->addDisplayViewport(virtualDisplayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001386 DISPLAY_ORIENTATION_0, true /*isActive*/, virtualUniqueId1,
1387 NO_PORT, ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001388 // Add another virtual viewport
1389 mFakePolicy->addDisplayViewport(virtualDisplayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001390 DISPLAY_ORIENTATION_0, true /*isActive*/, virtualUniqueId2,
1391 NO_PORT, ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001392
1393 // Check matching by type for internal
1394 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001395 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001396 ASSERT_TRUE(internalViewport);
1397 ASSERT_EQ(internalUniqueId, internalViewport->uniqueId);
1398
1399 // Check matching by type for external
1400 std::optional<DisplayViewport> externalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001401 mFakePolicy->getDisplayViewportByType(ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001402 ASSERT_TRUE(externalViewport);
1403 ASSERT_EQ(externalUniqueId, externalViewport->uniqueId);
1404
1405 // Check matching by uniqueId for virtual viewport #1
1406 std::optional<DisplayViewport> virtualViewport1 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001407 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001408 ASSERT_TRUE(virtualViewport1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001409 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport1->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001410 ASSERT_EQ(virtualUniqueId1, virtualViewport1->uniqueId);
1411 ASSERT_EQ(virtualDisplayId1, virtualViewport1->displayId);
1412
1413 // Check matching by uniqueId for virtual viewport #2
1414 std::optional<DisplayViewport> virtualViewport2 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001415 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001416 ASSERT_TRUE(virtualViewport2);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001417 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport2->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001418 ASSERT_EQ(virtualUniqueId2, virtualViewport2->uniqueId);
1419 ASSERT_EQ(virtualDisplayId2, virtualViewport2->displayId);
1420}
1421
1422
1423/**
1424 * We can have 2 viewports of the same kind. We can distinguish them by uniqueId, and confirm
1425 * that lookup works by checking display id.
1426 * Check that 2 viewports of each kind is possible, for all existing viewport types.
1427 */
1428TEST_F(InputReaderPolicyTest, Viewports_TwoOfSameType) {
1429 const std::string uniqueId1 = "uniqueId1";
1430 const std::string uniqueId2 = "uniqueId2";
1431 constexpr int32_t displayId1 = 2;
1432 constexpr int32_t displayId2 = 3;
1433
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001434 std::vector<ViewportType> types = {ViewportType::INTERNAL, ViewportType::EXTERNAL,
1435 ViewportType::VIRTUAL};
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001436 for (const ViewportType& type : types) {
1437 mFakePolicy->clearViewports();
1438 // Add a viewport
1439 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001440 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1,
1441 NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001442 // Add another viewport
1443 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001444 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2,
1445 NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001446
1447 // Check that correct display viewport was returned by comparing the display IDs.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001448 std::optional<DisplayViewport> viewport1 =
1449 mFakePolicy->getDisplayViewportByUniqueId(uniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001450 ASSERT_TRUE(viewport1);
1451 ASSERT_EQ(displayId1, viewport1->displayId);
1452 ASSERT_EQ(type, viewport1->type);
1453
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001454 std::optional<DisplayViewport> viewport2 =
1455 mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001456 ASSERT_TRUE(viewport2);
1457 ASSERT_EQ(displayId2, viewport2->displayId);
1458 ASSERT_EQ(type, viewport2->type);
1459
1460 // When there are multiple viewports of the same kind, and uniqueId is not specified
1461 // in the call to getDisplayViewport, then that situation is not supported.
1462 // The viewports can be stored in any order, so we cannot rely on the order, since that
1463 // is just implementation detail.
1464 // However, we can check that it still returns *a* viewport, we just cannot assert
1465 // which one specifically is returned.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001466 std::optional<DisplayViewport> someViewport = mFakePolicy->getDisplayViewportByType(type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001467 ASSERT_TRUE(someViewport);
1468 }
1469}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001470
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001471/**
Michael Wrightdde67b82020-10-27 16:09:22 +00001472 * When we have multiple internal displays make sure we always return the default display when
1473 * querying by type.
1474 */
1475TEST_F(InputReaderPolicyTest, Viewports_ByTypeReturnsDefaultForInternal) {
1476 const std::string uniqueId1 = "uniqueId1";
1477 const std::string uniqueId2 = "uniqueId2";
1478 constexpr int32_t nonDefaultDisplayId = 2;
1479 static_assert(nonDefaultDisplayId != ADISPLAY_ID_DEFAULT,
1480 "Test display ID should not be ADISPLAY_ID_DEFAULT");
1481
1482 // Add the default display first and ensure it gets returned.
1483 mFakePolicy->clearViewports();
1484 mFakePolicy->addDisplayViewport(ADISPLAY_ID_DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001485 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001486 ViewportType::INTERNAL);
1487 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001488 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001489 ViewportType::INTERNAL);
1490
1491 std::optional<DisplayViewport> viewport =
1492 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
1493 ASSERT_TRUE(viewport);
1494 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
1495 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
1496
1497 // Add the default display second to make sure order doesn't matter.
1498 mFakePolicy->clearViewports();
1499 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001500 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001501 ViewportType::INTERNAL);
1502 mFakePolicy->addDisplayViewport(ADISPLAY_ID_DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001503 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001504 ViewportType::INTERNAL);
1505
1506 viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
1507 ASSERT_TRUE(viewport);
1508 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
1509 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
1510}
1511
1512/**
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001513 * Check getDisplayViewportByPort
1514 */
1515TEST_F(InputReaderPolicyTest, Viewports_GetByPort) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001516 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001517 const std::string uniqueId1 = "uniqueId1";
1518 const std::string uniqueId2 = "uniqueId2";
1519 constexpr int32_t displayId1 = 1;
1520 constexpr int32_t displayId2 = 2;
1521 const uint8_t hdmi1 = 0;
1522 const uint8_t hdmi2 = 1;
1523 const uint8_t hdmi3 = 2;
1524
1525 mFakePolicy->clearViewports();
1526 // Add a viewport that's associated with some display port that's not of interest.
1527 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001528 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, hdmi3,
1529 type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001530 // Add another viewport, connected to HDMI1 port
1531 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001532 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, hdmi1,
1533 type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001534
1535 // Check that correct display viewport was returned by comparing the display ports.
1536 std::optional<DisplayViewport> hdmi1Viewport = mFakePolicy->getDisplayViewportByPort(hdmi1);
1537 ASSERT_TRUE(hdmi1Viewport);
1538 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1539 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1540
1541 // Check that we can still get the same viewport using the uniqueId
1542 hdmi1Viewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
1543 ASSERT_TRUE(hdmi1Viewport);
1544 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1545 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1546 ASSERT_EQ(type, hdmi1Viewport->type);
1547
1548 // Check that we cannot find a port with "HDMI2", because we never added one
1549 std::optional<DisplayViewport> hdmi2Viewport = mFakePolicy->getDisplayViewportByPort(hdmi2);
1550 ASSERT_FALSE(hdmi2Viewport);
1551}
1552
Michael Wrightd02c5b62014-02-10 15:10:22 -08001553// --- InputReaderTest ---
1554
1555class InputReaderTest : public testing::Test {
1556protected:
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001557 std::unique_ptr<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001558 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001559 std::shared_ptr<FakeEventHub> mFakeEventHub;
Prabir Pradhan28efc192019-11-05 01:10:04 +00001560 std::unique_ptr<InstrumentedInputReader> mReader;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001561
Chris Yea52ade12020-08-27 16:49:20 -07001562 void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001563 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001564 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001565 mFakeListener = std::make_unique<TestInputListener>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001566
Prabir Pradhan28efc192019-11-05 01:10:04 +00001567 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001568 *mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001569 }
1570
Chris Yea52ade12020-08-27 16:49:20 -07001571 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001572 mFakeListener.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001573 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001574 }
1575
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001576 void addDevice(int32_t eventHubId, const std::string& name,
1577 ftl::Flags<InputDeviceClass> classes, const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001578 mFakeEventHub->addDevice(eventHubId, name, classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001579
1580 if (configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001581 mFakeEventHub->addConfigurationMap(eventHubId, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001582 }
1583 mFakeEventHub->finishDeviceScan();
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001584 mReader->loopOnce();
1585 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001586 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1587 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001588 }
1589
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001590 void disableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001591 mFakePolicy->addDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001592 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001593 }
1594
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001595 void enableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001596 mFakePolicy->removeDisabledDevice(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. Lewisa7b82e12020-02-12 15:40:45 -08001600 FakeInputMapper& addDeviceWithFakeInputMapper(int32_t deviceId, int32_t eventHubId,
Chris Ye1b0c7342020-07-28 21:57:03 -07001601 const std::string& name,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001602 ftl::Flags<InputDeviceClass> classes,
1603 uint32_t sources,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001604 const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001605 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, name);
1606 FakeInputMapper& mapper = device->addMapper<FakeInputMapper>(eventHubId, sources);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001607 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001608 addDevice(eventHubId, name, classes, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001609 return mapper;
1610 }
1611};
1612
Chris Ye98d3f532020-10-01 21:48:59 -07001613TEST_F(InputReaderTest, PolicyGetInputDevices) {
1614 ASSERT_NO_FATAL_FAILURE(addDevice(1, "keyboard", InputDeviceClass::KEYBOARD, nullptr));
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001615 ASSERT_NO_FATAL_FAILURE(addDevice(2, "ignored", ftl::Flags<InputDeviceClass>(0),
Chris Ye98d3f532020-10-01 21:48:59 -07001616 nullptr)); // no classes so device will be ignored
Michael Wrightd02c5b62014-02-10 15:10:22 -08001617
1618 // Should also have received a notification describing the new input devices.
Chris Ye98d3f532020-10-01 21:48:59 -07001619 const std::vector<InputDeviceInfo>& inputDevices = mFakePolicy->getInputDevices();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001620 ASSERT_EQ(1U, inputDevices.size());
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001621 ASSERT_EQ(END_RESERVED_ID + 1, inputDevices[0].getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001622 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001623 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
1624 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00001625 ASSERT_EQ(0U, inputDevices[0].getMotionRanges().size());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001626}
1627
Chris Yee7310032020-09-22 15:36:28 -07001628TEST_F(InputReaderTest, GetMergedInputDevices) {
1629 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1630 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1631 // Add two subdevices to device
1632 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1633 // Must add at least one mapper or the device will be ignored!
1634 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1635 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1636
1637 // Push same device instance for next device to be added, so they'll have same identifier.
1638 mReader->pushNextDevice(device);
1639 mReader->pushNextDevice(device);
1640 ASSERT_NO_FATAL_FAILURE(
1641 addDevice(eventHubIds[0], "fake1", InputDeviceClass::KEYBOARD, nullptr));
1642 ASSERT_NO_FATAL_FAILURE(
1643 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
1644
1645 // Two devices will be merged to one input device as they have same identifier
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00001646 ASSERT_EQ(1U, mFakePolicy->getInputDevices().size());
Chris Yee7310032020-09-22 15:36:28 -07001647}
1648
Chris Yee14523a2020-12-19 13:46:00 -08001649TEST_F(InputReaderTest, GetMergedInputDevicesEnabled) {
1650 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1651 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1652 // Add two subdevices to device
1653 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1654 // Must add at least one mapper or the device will be ignored!
1655 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1656 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1657
1658 // Push same device instance for next device to be added, so they'll have same identifier.
1659 mReader->pushNextDevice(device);
1660 mReader->pushNextDevice(device);
1661 // Sensor device is initially disabled
1662 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1",
1663 InputDeviceClass::KEYBOARD | InputDeviceClass::SENSOR,
1664 nullptr));
1665 // Device is disabled because the only sub device is a sensor device and disabled initially.
1666 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1667 ASSERT_FALSE(device->isEnabled());
1668 ASSERT_NO_FATAL_FAILURE(
1669 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
1670 // The merged device is enabled if any sub device is enabled
1671 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1672 ASSERT_TRUE(device->isEnabled());
1673}
1674
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001675TEST_F(InputReaderTest, WhenEnabledChanges_SendsDeviceResetNotification) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001676 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001677 constexpr ftl::Flags<InputDeviceClass> deviceClass(InputDeviceClass::KEYBOARD);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001678 constexpr int32_t eventHubId = 1;
1679 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001680 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001681 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001682 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001683 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001684
Yi Kong9b14ac62018-07-17 13:48:38 -07001685 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001686
1687 NotifyDeviceResetArgs resetArgs;
1688 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001689 ASSERT_EQ(deviceId, resetArgs.deviceId);
1690
1691 ASSERT_EQ(device->isEnabled(), true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001692 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001693 mReader->loopOnce();
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001694
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001695 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001696 ASSERT_EQ(deviceId, resetArgs.deviceId);
1697 ASSERT_EQ(device->isEnabled(), false);
1698
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001699 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001700 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001701 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
1702 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasNotCalled());
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001703 ASSERT_EQ(device->isEnabled(), false);
1704
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001705 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001706 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001707 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001708 ASSERT_EQ(deviceId, resetArgs.deviceId);
1709 ASSERT_EQ(device->isEnabled(), true);
1710}
1711
Michael Wrightd02c5b62014-02-10 15:10:22 -08001712TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001713 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001714 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001715 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001716 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001717 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001718 AINPUT_SOURCE_KEYBOARD, nullptr);
1719 mapper.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001720
1721 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(0,
1722 AINPUT_SOURCE_ANY, AKEYCODE_A))
1723 << "Should return unknown when the device id is >= 0 but unknown.";
1724
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001725 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1726 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1727 << "Should return unknown when the device id is valid but the sources are not "
1728 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001729
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001730 ASSERT_EQ(AKEY_STATE_DOWN,
1731 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1732 AKEYCODE_A))
1733 << "Should return value provided by mapper when device id is valid and the device "
1734 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001735
1736 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(-1,
1737 AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1738 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1739
1740 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getKeyCodeState(-1,
1741 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1742 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1743}
1744
Philip Junker4af3b3d2021-12-14 10:36:55 +01001745TEST_F(InputReaderTest, GetKeyCodeForKeyLocation_ForwardsRequestsToMappers) {
1746 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1747 constexpr int32_t eventHubId = 1;
1748 FakeInputMapper& mapper = addDeviceWithFakeInputMapper(deviceId, eventHubId, "keyboard",
1749 InputDeviceClass::KEYBOARD,
1750 AINPUT_SOURCE_KEYBOARD, nullptr);
1751 mapper.addKeyCodeMapping(AKEYCODE_Y, AKEYCODE_Z);
1752
1753 ASSERT_EQ(AKEYCODE_UNKNOWN, mReader->getKeyCodeForKeyLocation(0, AKEYCODE_Y))
1754 << "Should return unknown when the device with the specified id is not found.";
1755
1756 ASSERT_EQ(AKEYCODE_Z, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_Y))
1757 << "Should return correct mapping when device id is valid and mapping exists.";
1758
1759 ASSERT_EQ(AKEYCODE_A, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_A))
1760 << "Should return the location key code when device id is valid and there's no "
1761 "mapping.";
1762}
1763
1764TEST_F(InputReaderTest, GetKeyCodeForKeyLocation_NoKeyboardMapper) {
1765 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1766 constexpr int32_t eventHubId = 1;
1767 FakeInputMapper& mapper = addDeviceWithFakeInputMapper(deviceId, eventHubId, "joystick",
1768 InputDeviceClass::JOYSTICK,
1769 AINPUT_SOURCE_GAMEPAD, nullptr);
1770 mapper.addKeyCodeMapping(AKEYCODE_Y, AKEYCODE_Z);
1771
1772 ASSERT_EQ(AKEYCODE_UNKNOWN, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_Y))
1773 << "Should return unknown when the device id is valid but there is no keyboard mapper";
1774}
1775
Michael Wrightd02c5b62014-02-10 15:10:22 -08001776TEST_F(InputReaderTest, GetScanCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001777 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001778 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001779 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001780 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001781 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001782 AINPUT_SOURCE_KEYBOARD, nullptr);
1783 mapper.setScanCodeState(KEY_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001784
1785 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(0,
1786 AINPUT_SOURCE_ANY, KEY_A))
1787 << "Should return unknown when the device id is >= 0 but unknown.";
1788
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001789 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1790 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, KEY_A))
1791 << "Should return unknown when the device id is valid but the sources are not "
1792 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001793
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001794 ASSERT_EQ(AKEY_STATE_DOWN,
1795 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1796 KEY_A))
1797 << "Should return value provided by mapper when device id is valid and the device "
1798 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001799
1800 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(-1,
1801 AINPUT_SOURCE_TRACKBALL, KEY_A))
1802 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1803
1804 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getScanCodeState(-1,
1805 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, KEY_A))
1806 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1807}
1808
1809TEST_F(InputReaderTest, GetSwitchState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001810 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001811 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001812 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001813 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001814 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001815 AINPUT_SOURCE_KEYBOARD, nullptr);
1816 mapper.setSwitchState(SW_LID, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001817
1818 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(0,
1819 AINPUT_SOURCE_ANY, SW_LID))
1820 << "Should return unknown when the device id is >= 0 but unknown.";
1821
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001822 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1823 mReader->getSwitchState(deviceId, AINPUT_SOURCE_TRACKBALL, SW_LID))
1824 << "Should return unknown when the device id is valid but the sources are not "
1825 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001826
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001827 ASSERT_EQ(AKEY_STATE_DOWN,
1828 mReader->getSwitchState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1829 SW_LID))
1830 << "Should return value provided by mapper when device id is valid and the device "
1831 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001832
1833 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(-1,
1834 AINPUT_SOURCE_TRACKBALL, SW_LID))
1835 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1836
1837 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getSwitchState(-1,
1838 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, SW_LID))
1839 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1840}
1841
1842TEST_F(InputReaderTest, MarkSupportedKeyCodes_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001843 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001844 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001845 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001846 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001847 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001848 AINPUT_SOURCE_KEYBOARD, nullptr);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001849
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001850 mapper.addSupportedKeyCode(AKEYCODE_A);
1851 mapper.addSupportedKeyCode(AKEYCODE_B);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001852
1853 const int32_t keyCodes[4] = { AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2 };
1854 uint8_t flags[4] = { 0, 0, 0, 1 };
1855
1856 ASSERT_FALSE(mReader->hasKeys(0, AINPUT_SOURCE_ANY, 4, keyCodes, flags))
1857 << "Should return false when device id is >= 0 but unknown.";
1858 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1859
1860 flags[3] = 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001861 ASSERT_FALSE(mReader->hasKeys(deviceId, AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1862 << "Should return false when device id is valid but the sources are not supported by "
1863 "the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001864 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1865
1866 flags[3] = 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001867 ASSERT_TRUE(mReader->hasKeys(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, 4,
1868 keyCodes, flags))
1869 << "Should return value provided by mapper when device id is valid and the device "
1870 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001871 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1872
1873 flags[3] = 1;
1874 ASSERT_FALSE(mReader->hasKeys(-1, AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1875 << "Should return false when the device id is < 0 but the sources are not supported by any device.";
1876 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1877
1878 flags[3] = 1;
1879 ASSERT_TRUE(mReader->hasKeys(-1, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1880 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1881 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1882}
1883
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001884TEST_F(InputReaderTest, LoopOnce_WhenDeviceScanFinished_SendsConfigurationChanged) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001885 constexpr int32_t eventHubId = 1;
Chris Ye1b0c7342020-07-28 21:57:03 -07001886 addDevice(eventHubId, "ignored", InputDeviceClass::KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001887
1888 NotifyConfigurationChangedArgs args;
1889
1890 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(&args));
1891 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
1892}
1893
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001894TEST_F(InputReaderTest, LoopOnce_ForwardsRawEventsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001895 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001896 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001897 constexpr nsecs_t when = 0;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001898 constexpr int32_t eventHubId = 1;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001899 constexpr nsecs_t readTime = 2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001900 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001901 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001902 AINPUT_SOURCE_KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001903
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001904 mFakeEventHub->enqueueEvent(when, readTime, eventHubId, EV_KEY, KEY_A, 1);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001905 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001906 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
1907
1908 RawEvent event;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001909 ASSERT_NO_FATAL_FAILURE(mapper.assertProcessWasCalled(&event));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001910 ASSERT_EQ(when, event.when);
1911 ASSERT_EQ(readTime, event.readTime);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001912 ASSERT_EQ(eventHubId, event.deviceId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001913 ASSERT_EQ(EV_KEY, event.type);
1914 ASSERT_EQ(KEY_A, event.code);
1915 ASSERT_EQ(1, event.value);
1916}
1917
Garfield Tan1c7bc862020-01-28 13:24:04 -08001918TEST_F(InputReaderTest, DeviceReset_RandomId) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001919 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001920 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001921 constexpr int32_t eventHubId = 1;
1922 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Prabir Pradhan42611e02018-11-27 14:04:02 -08001923 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001924 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001925 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001926 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan42611e02018-11-27 14:04:02 -08001927
1928 NotifyDeviceResetArgs resetArgs;
1929 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001930 int32_t prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001931
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001932 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001933 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001934 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001935 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001936 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001937
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001938 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001939 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001940 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001941 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001942 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001943
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001944 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001945 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001946 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001947 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001948 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001949}
1950
Garfield Tan1c7bc862020-01-28 13:24:04 -08001951TEST_F(InputReaderTest, DeviceReset_GenerateIdWithInputReaderSource) {
1952 constexpr int32_t deviceId = 1;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001953 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Garfield Tan1c7bc862020-01-28 13:24:04 -08001954 constexpr int32_t eventHubId = 1;
1955 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1956 // Must add at least one mapper or the device will be ignored!
1957 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001958 mReader->pushNextDevice(device);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001959 ASSERT_NO_FATAL_FAILURE(addDevice(deviceId, "fake", deviceClass, nullptr));
1960
1961 NotifyDeviceResetArgs resetArgs;
1962 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1963 ASSERT_EQ(IdGenerator::Source::INPUT_READER, IdGenerator::getSource(resetArgs.id));
1964}
1965
Arthur Hungc23540e2018-11-29 20:42:11 +08001966TEST_F(InputReaderTest, Device_CanDispatchToDisplay) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001967 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001968 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001969 constexpr int32_t eventHubId = 1;
Arthur Hungc23540e2018-11-29 20:42:11 +08001970 const char* DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001971 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1972 FakeInputMapper& mapper =
1973 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001974 mReader->pushNextDevice(device);
Arthur Hungc23540e2018-11-29 20:42:11 +08001975
1976 const uint8_t hdmi1 = 1;
1977
1978 // Associated touch screen with second display.
1979 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
1980
1981 // Add default and second display.
Prabir Pradhan28efc192019-11-05 01:10:04 +00001982 mFakePolicy->clearViewports();
Arthur Hungc23540e2018-11-29 20:42:11 +08001983 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001984 DISPLAY_ORIENTATION_0, true /*isActive*/, "local:0", NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001985 ViewportType::INTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08001986 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001987 DISPLAY_ORIENTATION_0, true /*isActive*/, "local:1", hdmi1,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001988 ViewportType::EXTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08001989 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001990 mReader->loopOnce();
Prabir Pradhan28efc192019-11-05 01:10:04 +00001991
1992 // Add the device, and make sure all of the callbacks are triggered.
1993 // The device is added after the input port associations are processed since
1994 // we do not yet support dynamic device-to-display associations.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001995 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001996 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled());
Prabir Pradhan28efc192019-11-05 01:10:04 +00001997 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001998 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
Arthur Hungc23540e2018-11-29 20:42:11 +08001999
Arthur Hung2c9a3342019-07-23 14:18:59 +08002000 // Device should only dispatch to the specified display.
Arthur Hungc23540e2018-11-29 20:42:11 +08002001 ASSERT_EQ(deviceId, device->getId());
2002 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, DISPLAY_ID));
2003 ASSERT_TRUE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hung2c9a3342019-07-23 14:18:59 +08002004
2005 // Can't dispatch event from a disabled device.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002006 disableDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00002007 mReader->loopOnce();
Arthur Hung2c9a3342019-07-23 14:18:59 +08002008 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hungc23540e2018-11-29 20:42:11 +08002009}
2010
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08002011TEST_F(InputReaderTest, WhenEnabledChanges_AllSubdevicesAreUpdated) {
2012 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002013 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08002014 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
2015 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
2016 // Must add at least one mapper or the device will be ignored!
2017 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
2018 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
2019 mReader->pushNextDevice(device);
2020 mReader->pushNextDevice(device);
2021 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
2022 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
2023
2024 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
2025
2026 NotifyDeviceResetArgs resetArgs;
2027 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2028 ASSERT_EQ(deviceId, resetArgs.deviceId);
2029 ASSERT_TRUE(device->isEnabled());
2030 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
2031 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
2032
2033 disableDevice(deviceId);
2034 mReader->loopOnce();
2035
2036 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2037 ASSERT_EQ(deviceId, resetArgs.deviceId);
2038 ASSERT_FALSE(device->isEnabled());
2039 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
2040 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
2041
2042 enableDevice(deviceId);
2043 mReader->loopOnce();
2044
2045 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2046 ASSERT_EQ(deviceId, resetArgs.deviceId);
2047 ASSERT_TRUE(device->isEnabled());
2048 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
2049 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
2050}
2051
2052TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToSubdeviceMappers) {
2053 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002054 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08002055 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
2056 // Add two subdevices to device
2057 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
2058 FakeInputMapper& mapperDevice1 =
2059 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
2060 FakeInputMapper& mapperDevice2 =
2061 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
2062 mReader->pushNextDevice(device);
2063 mReader->pushNextDevice(device);
2064 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
2065 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
2066
2067 mapperDevice1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
2068 mapperDevice2.setKeyCodeState(AKEYCODE_B, AKEY_STATE_DOWN);
2069
2070 ASSERT_EQ(AKEY_STATE_DOWN,
2071 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_A));
2072 ASSERT_EQ(AKEY_STATE_DOWN,
2073 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_B));
2074 ASSERT_EQ(AKEY_STATE_UNKNOWN,
2075 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_C));
2076}
2077
Prabir Pradhan7e186182020-11-10 13:56:45 -08002078TEST_F(InputReaderTest, ChangingPointerCaptureNotifiesInputListener) {
2079 NotifyPointerCaptureChangedArgs args;
2080
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002081 auto request = mFakePolicy->setPointerCapture(true);
Prabir Pradhan7e186182020-11-10 13:56:45 -08002082 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
2083 mReader->loopOnce();
2084 mFakeListener->assertNotifyCaptureWasCalled(&args);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002085 ASSERT_TRUE(args.request.enable) << "Pointer Capture should be enabled.";
2086 ASSERT_EQ(args.request, request) << "Pointer Capture sequence number should match.";
Prabir Pradhan7e186182020-11-10 13:56:45 -08002087
2088 mFakePolicy->setPointerCapture(false);
2089 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
2090 mReader->loopOnce();
2091 mFakeListener->assertNotifyCaptureWasCalled(&args);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002092 ASSERT_FALSE(args.request.enable) << "Pointer Capture should be disabled.";
Prabir Pradhan7e186182020-11-10 13:56:45 -08002093
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002094 // Verify that the Pointer Capture state is not updated when the configuration value
Prabir Pradhan7e186182020-11-10 13:56:45 -08002095 // does not change.
2096 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
2097 mReader->loopOnce();
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002098 mFakeListener->assertNotifyCaptureWasNotCalled();
Prabir Pradhan7e186182020-11-10 13:56:45 -08002099}
2100
Chris Ye87143712020-11-10 05:05:58 +00002101class FakeVibratorInputMapper : public FakeInputMapper {
2102public:
2103 FakeVibratorInputMapper(InputDeviceContext& deviceContext, uint32_t sources)
2104 : FakeInputMapper(deviceContext, sources) {}
2105
2106 std::vector<int32_t> getVibratorIds() override { return getDeviceContext().getVibratorIds(); }
2107};
2108
2109TEST_F(InputReaderTest, VibratorGetVibratorIds) {
2110 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002111 ftl::Flags<InputDeviceClass> deviceClass =
2112 InputDeviceClass::KEYBOARD | InputDeviceClass::VIBRATOR;
Chris Ye87143712020-11-10 05:05:58 +00002113 constexpr int32_t eventHubId = 1;
2114 const char* DEVICE_LOCATION = "BLUETOOTH";
2115 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
2116 FakeVibratorInputMapper& mapper =
2117 device->addMapper<FakeVibratorInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
2118 mReader->pushNextDevice(device);
2119
2120 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2121 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
2122
2123 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
2124 ASSERT_EQ(mReader->getVibratorIds(deviceId).size(), 2U);
2125}
2126
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002127// --- FakePeripheralController ---
Kim Low03ea0352020-11-06 12:45:07 -08002128
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002129class FakePeripheralController : public PeripheralControllerInterface {
Chris Yee2b1e5c2021-03-10 22:45:12 -08002130public:
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002131 FakePeripheralController(InputDeviceContext& deviceContext) : mDeviceContext(deviceContext) {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08002132
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002133 ~FakePeripheralController() override {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08002134
2135 void populateDeviceInfo(InputDeviceInfo* deviceInfo) override {}
2136
2137 void dump(std::string& dump) override {}
2138
2139 std::optional<int32_t> getBatteryCapacity(int32_t batteryId) override {
2140 return getDeviceContext().getBatteryCapacity(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08002141 }
2142
Chris Yee2b1e5c2021-03-10 22:45:12 -08002143 std::optional<int32_t> getBatteryStatus(int32_t batteryId) override {
2144 return getDeviceContext().getBatteryStatus(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08002145 }
Chris Ye3fdbfef2021-01-06 18:45:18 -08002146
2147 bool setLightColor(int32_t lightId, int32_t color) override {
2148 getDeviceContext().setLightBrightness(lightId, color >> 24);
2149 return true;
2150 }
2151
2152 std::optional<int32_t> getLightColor(int32_t lightId) override {
2153 std::optional<int32_t> result = getDeviceContext().getLightBrightness(lightId);
2154 if (!result.has_value()) {
2155 return std::nullopt;
2156 }
2157 return result.value() << 24;
2158 }
Chris Yee2b1e5c2021-03-10 22:45:12 -08002159
2160 bool setLightPlayerId(int32_t lightId, int32_t playerId) override { return true; }
2161
2162 std::optional<int32_t> getLightPlayerId(int32_t lightId) override { return std::nullopt; }
2163
2164private:
2165 InputDeviceContext& mDeviceContext;
2166 inline int32_t getDeviceId() { return mDeviceContext.getId(); }
2167 inline InputDeviceContext& getDeviceContext() { return mDeviceContext; }
Chris Ye3fdbfef2021-01-06 18:45:18 -08002168};
2169
Chris Yee2b1e5c2021-03-10 22:45:12 -08002170TEST_F(InputReaderTest, BatteryGetCapacity) {
2171 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002172 ftl::Flags<InputDeviceClass> deviceClass =
2173 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
Chris Yee2b1e5c2021-03-10 22:45:12 -08002174 constexpr int32_t eventHubId = 1;
2175 const char* DEVICE_LOCATION = "BLUETOOTH";
2176 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002177 FakePeripheralController& controller =
2178 device->addController<FakePeripheralController>(eventHubId);
Chris Yee2b1e5c2021-03-10 22:45:12 -08002179 mReader->pushNextDevice(device);
2180
2181 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2182
2183 ASSERT_EQ(controller.getBatteryCapacity(DEFAULT_BATTERY), BATTERY_CAPACITY);
2184 ASSERT_EQ(mReader->getBatteryCapacity(deviceId), BATTERY_CAPACITY);
2185}
2186
2187TEST_F(InputReaderTest, BatteryGetStatus) {
2188 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002189 ftl::Flags<InputDeviceClass> deviceClass =
2190 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
Chris Yee2b1e5c2021-03-10 22:45:12 -08002191 constexpr int32_t eventHubId = 1;
2192 const char* DEVICE_LOCATION = "BLUETOOTH";
2193 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002194 FakePeripheralController& controller =
2195 device->addController<FakePeripheralController>(eventHubId);
Chris Yee2b1e5c2021-03-10 22:45:12 -08002196 mReader->pushNextDevice(device);
2197
2198 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2199
2200 ASSERT_EQ(controller.getBatteryStatus(DEFAULT_BATTERY), BATTERY_STATUS);
2201 ASSERT_EQ(mReader->getBatteryStatus(deviceId), BATTERY_STATUS);
2202}
2203
Chris Ye3fdbfef2021-01-06 18:45:18 -08002204TEST_F(InputReaderTest, LightGetColor) {
2205 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002206 ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD | InputDeviceClass::LIGHT;
Chris Ye3fdbfef2021-01-06 18:45:18 -08002207 constexpr int32_t eventHubId = 1;
2208 const char* DEVICE_LOCATION = "BLUETOOTH";
2209 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002210 FakePeripheralController& controller =
2211 device->addController<FakePeripheralController>(eventHubId);
Chris Ye3fdbfef2021-01-06 18:45:18 -08002212 mReader->pushNextDevice(device);
2213 RawLightInfo info = {.id = 1,
2214 .name = "Mono",
2215 .maxBrightness = 255,
2216 .flags = InputLightClass::BRIGHTNESS,
2217 .path = ""};
2218 mFakeEventHub->addRawLightInfo(1 /* rawId */, std::move(info));
2219 mFakeEventHub->fakeLightBrightness(1 /* rawId */, 0x55);
2220
2221 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Chris Ye3fdbfef2021-01-06 18:45:18 -08002222
Chris Yee2b1e5c2021-03-10 22:45:12 -08002223 ASSERT_TRUE(controller.setLightColor(1 /* lightId */, LIGHT_BRIGHTNESS));
2224 ASSERT_EQ(controller.getLightColor(1 /* lightId */), LIGHT_BRIGHTNESS);
Chris Ye3fdbfef2021-01-06 18:45:18 -08002225 ASSERT_TRUE(mReader->setLightColor(deviceId, 1 /* lightId */, LIGHT_BRIGHTNESS));
2226 ASSERT_EQ(mReader->getLightColor(deviceId, 1 /* lightId */), LIGHT_BRIGHTNESS);
2227}
2228
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002229// --- InputReaderIntegrationTest ---
2230
2231// These tests create and interact with the InputReader only through its interface.
2232// The InputReader is started during SetUp(), which starts its processing in its own
2233// thread. The tests use linux uinput to emulate input devices.
2234// NOTE: Interacting with the physical device while these tests are running may cause
2235// the tests to fail.
2236class InputReaderIntegrationTest : public testing::Test {
2237protected:
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002238 std::unique_ptr<TestInputListener> mTestListener;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002239 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002240 std::unique_ptr<InputReaderInterface> mReader;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002241
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00002242 std::shared_ptr<FakePointerController> mFakePointerController;
2243
Chris Yea52ade12020-08-27 16:49:20 -07002244 void SetUp() override {
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002245 mFakePolicy = new FakeInputReaderPolicy();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00002246 mFakePointerController = std::make_shared<FakePointerController>();
2247 mFakePolicy->setPointerController(mFakePointerController);
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002248 mTestListener = std::make_unique<TestInputListener>(2000ms /*eventHappenedTimeout*/,
2249 30ms /*eventDidNotHappenTimeout*/);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002250
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002251 mReader = std::make_unique<InputReader>(std::make_shared<EventHub>(), mFakePolicy,
2252 *mTestListener);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002253 ASSERT_EQ(mReader->start(), OK);
2254
2255 // Since this test is run on a real device, all the input devices connected
2256 // to the test device will show up in mReader. We wait for those input devices to
2257 // show up before beginning the tests.
2258 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2259 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2260 }
2261
Chris Yea52ade12020-08-27 16:49:20 -07002262 void TearDown() override {
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002263 ASSERT_EQ(mReader->stop(), OK);
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002264 mReader.reset();
2265 mTestListener.reset();
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002266 mFakePolicy.clear();
2267 }
2268};
2269
2270TEST_F(InputReaderIntegrationTest, TestInvalidDevice) {
2271 // An invalid input device that is only used for this test.
2272 class InvalidUinputDevice : public UinputDevice {
2273 public:
2274 InvalidUinputDevice() : UinputDevice("Invalid Device") {}
2275
2276 private:
2277 void configureDevice(int fd, uinput_user_dev* device) override {}
2278 };
2279
2280 const size_t numDevices = mFakePolicy->getInputDevices().size();
2281
2282 // UinputDevice does not set any event or key bits, so InputReader should not
2283 // consider it as a valid device.
2284 std::unique_ptr<UinputDevice> invalidDevice = createUinputDevice<InvalidUinputDevice>();
2285 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
2286 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
2287 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
2288
2289 invalidDevice.reset();
2290 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
2291 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
2292 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
2293}
2294
2295TEST_F(InputReaderIntegrationTest, AddNewDevice) {
2296 const size_t initialNumDevices = mFakePolicy->getInputDevices().size();
2297
2298 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
2299 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2300 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2301 ASSERT_EQ(initialNumDevices + 1, mFakePolicy->getInputDevices().size());
2302
2303 // Find the test device by its name.
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002304 const std::vector<InputDeviceInfo> inputDevices = mFakePolicy->getInputDevices();
Chris Ye98d3f532020-10-01 21:48:59 -07002305 const auto& it =
2306 std::find_if(inputDevices.begin(), inputDevices.end(),
2307 [&keyboard](const InputDeviceInfo& info) {
2308 return info.getIdentifier().name == keyboard->getName();
2309 });
2310
2311 ASSERT_NE(it, inputDevices.end());
2312 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, it->getKeyboardType());
2313 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, it->getSources());
2314 ASSERT_EQ(0U, it->getMotionRanges().size());
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002315
2316 keyboard.reset();
2317 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2318 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2319 ASSERT_EQ(initialNumDevices, mFakePolicy->getInputDevices().size());
2320}
2321
2322TEST_F(InputReaderIntegrationTest, SendsEventsToInputListener) {
2323 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
2324 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2325
2326 NotifyConfigurationChangedArgs configChangedArgs;
2327 ASSERT_NO_FATAL_FAILURE(
2328 mTestListener->assertNotifyConfigurationChangedWasCalled(&configChangedArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002329 int32_t prevId = configChangedArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002330 nsecs_t prevTimestamp = configChangedArgs.eventTime;
2331
2332 NotifyKeyArgs keyArgs;
2333 keyboard->pressAndReleaseHomeKey();
2334 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
2335 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002336 ASSERT_NE(prevId, keyArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002337 prevId = keyArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002338 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002339 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002340 prevTimestamp = keyArgs.eventTime;
2341
2342 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
2343 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002344 ASSERT_NE(prevId, keyArgs.id);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002345 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002346 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002347}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002348
Siarhei Vishniakoua0d2b802020-05-13 14:00:31 -07002349/**
2350 * The Steam controller sends BTN_GEAR_DOWN and BTN_GEAR_UP for the two "paddle" buttons
2351 * on the back. In this test, we make sure that BTN_GEAR_DOWN / BTN_WHEEL and BTN_GEAR_UP
2352 * are passed to the listener.
2353 */
2354static_assert(BTN_GEAR_DOWN == BTN_WHEEL);
2355TEST_F(InputReaderIntegrationTest, SendsGearDownAndUpToInputListener) {
2356 std::unique_ptr<UinputSteamController> controller = createUinputDevice<UinputSteamController>();
2357 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2358 NotifyKeyArgs keyArgs;
2359
2360 controller->pressAndReleaseKey(BTN_GEAR_DOWN);
2361 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
2362 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
2363 ASSERT_EQ(BTN_GEAR_DOWN, keyArgs.scanCode);
2364
2365 controller->pressAndReleaseKey(BTN_GEAR_UP);
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_UP, keyArgs.scanCode);
2369}
2370
Arthur Hungaab25622020-01-16 11:22:11 +08002371// --- TouchProcessTest ---
2372class TouchIntegrationTest : public InputReaderIntegrationTest {
2373protected:
Arthur Hungaab25622020-01-16 11:22:11 +08002374 const std::string UNIQUE_ID = "local:0";
2375
Chris Yea52ade12020-08-27 16:49:20 -07002376 void SetUp() override {
Arthur Hungaab25622020-01-16 11:22:11 +08002377 InputReaderIntegrationTest::SetUp();
2378 // At least add an internal display.
2379 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2380 DISPLAY_ORIENTATION_0, UNIQUE_ID, NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002381 ViewportType::INTERNAL);
Arthur Hungaab25622020-01-16 11:22:11 +08002382
2383 mDevice = createUinputDevice<UinputTouchScreen>(Rect(0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT));
2384 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2385 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2386 }
2387
2388 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
2389 int32_t orientation, const std::string& uniqueId,
2390 std::optional<uint8_t> physicalPort,
2391 ViewportType viewportType) {
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002392 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, true /*isActive*/,
2393 uniqueId, physicalPort, viewportType);
Arthur Hungaab25622020-01-16 11:22:11 +08002394 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2395 }
2396
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002397 void assertReceivedMotion(int32_t action, const std::vector<Point>& points) {
2398 NotifyMotionArgs args;
2399 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2400 EXPECT_EQ(action, args.action);
2401 ASSERT_EQ(points.size(), args.pointerCount);
2402 for (size_t i = 0; i < args.pointerCount; i++) {
2403 EXPECT_EQ(points[i].x, args.pointerCoords[i].getX());
2404 EXPECT_EQ(points[i].y, args.pointerCoords[i].getY());
2405 }
2406 }
2407
Arthur Hungaab25622020-01-16 11:22:11 +08002408 std::unique_ptr<UinputTouchScreen> mDevice;
2409};
2410
2411TEST_F(TouchIntegrationTest, InputEvent_ProcessSingleTouch) {
2412 NotifyMotionArgs args;
2413 const Point centerPoint = mDevice->getCenterPoint();
2414
2415 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00002416 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002417 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002418 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002419 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2420 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2421
2422 // ACTION_MOVE
2423 mDevice->sendMove(centerPoint + Point(1, 1));
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002424 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002425 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2426 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2427
2428 // ACTION_UP
2429 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002430 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002431 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2432 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2433}
2434
2435TEST_F(TouchIntegrationTest, InputEvent_ProcessMultiTouch) {
2436 NotifyMotionArgs args;
2437 const Point centerPoint = mDevice->getCenterPoint();
2438
2439 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00002440 mDevice->sendSlot(FIRST_SLOT);
2441 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002442 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002443 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002444 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2445 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2446
2447 // ACTION_POINTER_DOWN (Second slot)
2448 const Point secondPoint = centerPoint + Point(100, 100);
2449 mDevice->sendSlot(SECOND_SLOT);
2450 mDevice->sendTrackingId(SECOND_TRACKING_ID);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002451 mDevice->sendDown(secondPoint);
2452 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002453 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08002454 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002455
2456 // ACTION_MOVE (Second slot)
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002457 mDevice->sendMove(secondPoint + Point(1, 1));
2458 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002459 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2460 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2461
2462 // ACTION_POINTER_UP (Second slot)
arthurhungcc7f9802020-04-30 17:55:40 +08002463 mDevice->sendPointerUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002464 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002465 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08002466 ASSERT_EQ(ACTION_POINTER_1_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002467
2468 // ACTION_UP
2469 mDevice->sendSlot(FIRST_SLOT);
2470 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002471 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002472 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2473 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2474}
2475
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002476/**
2477 * What happens when a pointer goes up while another pointer moves in the same frame? Are POINTER_UP
2478 * events guaranteed to contain the same data as a preceding MOVE, or can they contain different
2479 * data?
2480 * In this test, we try to send a change in coordinates in Pointer 0 in the same frame as the
2481 * liftoff of Pointer 1. We check that POINTER_UP event is generated first, and the MOVE event
2482 * for Pointer 0 only is generated after.
2483 * Suppose we are only interested in learning the movement of Pointer 0. If we only observe MOVE
2484 * events, we will not miss any information.
2485 * Even though the Pointer 1 up event contains updated Pointer 0 coordinates, there is another MOVE
2486 * event generated afterwards that contains the newest movement of pointer 0.
2487 * This is important for palm rejection. If there is a subsequent InputListener stage that detects
2488 * palms, and wants to cancel Pointer 1, then it is safe to simply drop POINTER_1_UP event without
2489 * losing information about non-palm pointers.
2490 */
2491TEST_F(TouchIntegrationTest, MultiTouch_PointerMoveAndSecondPointerUp) {
2492 NotifyMotionArgs args;
2493 const Point centerPoint = mDevice->getCenterPoint();
2494
2495 // ACTION_DOWN
2496 mDevice->sendSlot(FIRST_SLOT);
2497 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2498 mDevice->sendDown(centerPoint);
2499 mDevice->sendSync();
2500 assertReceivedMotion(AMOTION_EVENT_ACTION_DOWN, {centerPoint});
2501
2502 // ACTION_POINTER_DOWN (Second slot)
2503 const Point secondPoint = centerPoint + Point(100, 100);
2504 mDevice->sendSlot(SECOND_SLOT);
2505 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2506 mDevice->sendDown(secondPoint);
2507 mDevice->sendSync();
2508 assertReceivedMotion(ACTION_POINTER_1_DOWN, {centerPoint, secondPoint});
2509
2510 // ACTION_MOVE (First slot)
2511 mDevice->sendSlot(FIRST_SLOT);
2512 mDevice->sendMove(centerPoint + Point(5, 5));
2513 // ACTION_POINTER_UP (Second slot)
2514 mDevice->sendSlot(SECOND_SLOT);
2515 mDevice->sendPointerUp();
2516 // Send a single sync for the above 2 pointer updates
2517 mDevice->sendSync();
2518
2519 // First, we should get POINTER_UP for the second pointer
2520 assertReceivedMotion(ACTION_POINTER_1_UP,
2521 {/*first pointer */ centerPoint + Point(5, 5),
2522 /*second pointer*/ secondPoint});
2523
2524 // Next, the MOVE event for the first pointer
2525 assertReceivedMotion(AMOTION_EVENT_ACTION_MOVE, {centerPoint + Point(5, 5)});
2526}
2527
2528/**
2529 * Similar scenario as above. The difference is that when the second pointer goes up, it will first
2530 * move, and then it will go up, all in the same frame.
2531 * In this scenario, the movement of the second pointer just prior to liftoff is ignored, and never
2532 * gets sent to the listener.
2533 */
2534TEST_F(TouchIntegrationTest, MultiTouch_PointerMoveAndSecondPointerMoveAndUp) {
2535 NotifyMotionArgs args;
2536 const Point centerPoint = mDevice->getCenterPoint();
2537
2538 // ACTION_DOWN
2539 mDevice->sendSlot(FIRST_SLOT);
2540 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2541 mDevice->sendDown(centerPoint);
2542 mDevice->sendSync();
2543 assertReceivedMotion(AMOTION_EVENT_ACTION_DOWN, {centerPoint});
2544
2545 // ACTION_POINTER_DOWN (Second slot)
2546 const Point secondPoint = centerPoint + Point(100, 100);
2547 mDevice->sendSlot(SECOND_SLOT);
2548 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2549 mDevice->sendDown(secondPoint);
2550 mDevice->sendSync();
2551 assertReceivedMotion(ACTION_POINTER_1_DOWN, {centerPoint, secondPoint});
2552
2553 // ACTION_MOVE (First slot)
2554 mDevice->sendSlot(FIRST_SLOT);
2555 mDevice->sendMove(centerPoint + Point(5, 5));
2556 // ACTION_POINTER_UP (Second slot)
2557 mDevice->sendSlot(SECOND_SLOT);
2558 mDevice->sendMove(secondPoint + Point(6, 6));
2559 mDevice->sendPointerUp();
2560 // Send a single sync for the above 2 pointer updates
2561 mDevice->sendSync();
2562
2563 // First, we should get POINTER_UP for the second pointer
2564 // The movement of the second pointer during the liftoff frame is ignored.
2565 // The coordinates 'secondPoint + Point(6, 6)' are never sent to the listener.
2566 assertReceivedMotion(ACTION_POINTER_1_UP,
2567 {/*first pointer */ centerPoint + Point(5, 5),
2568 /*second pointer*/ secondPoint});
2569
2570 // Next, the MOVE event for the first pointer
2571 assertReceivedMotion(AMOTION_EVENT_ACTION_MOVE, {centerPoint + Point(5, 5)});
2572}
2573
Arthur Hungaab25622020-01-16 11:22:11 +08002574TEST_F(TouchIntegrationTest, InputEvent_ProcessPalm) {
2575 NotifyMotionArgs args;
2576 const Point centerPoint = mDevice->getCenterPoint();
2577
2578 // ACTION_DOWN
arthurhungcc7f9802020-04-30 17:55:40 +08002579 mDevice->sendSlot(FIRST_SLOT);
2580 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002581 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002582 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002583 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2584 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2585
arthurhungcc7f9802020-04-30 17:55:40 +08002586 // ACTION_POINTER_DOWN (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002587 const Point secondPoint = centerPoint + Point(100, 100);
2588 mDevice->sendSlot(SECOND_SLOT);
2589 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2590 mDevice->sendDown(secondPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002591 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002592 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08002593 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002594
arthurhungcc7f9802020-04-30 17:55:40 +08002595 // ACTION_MOVE (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002596 mDevice->sendMove(secondPoint + Point(1, 1));
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002597 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002598 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2599 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2600
arthurhungcc7f9802020-04-30 17:55:40 +08002601 // Send MT_TOOL_PALM (second slot), which indicates that the touch IC has determined this to be
2602 // a palm event.
2603 // Expect to receive the ACTION_POINTER_UP with cancel flag.
Arthur Hungaab25622020-01-16 11:22:11 +08002604 mDevice->sendToolType(MT_TOOL_PALM);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002605 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002606 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08002607 ASSERT_EQ(ACTION_POINTER_1_UP, args.action);
arthurhungcc7f9802020-04-30 17:55:40 +08002608 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, args.flags);
Arthur Hungaab25622020-01-16 11:22:11 +08002609
arthurhungcc7f9802020-04-30 17:55:40 +08002610 // Send up to second slot, expect first slot send moving.
2611 mDevice->sendPointerUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002612 mDevice->sendSync();
arthurhungcc7f9802020-04-30 17:55:40 +08002613 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2614 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002615
arthurhungcc7f9802020-04-30 17:55:40 +08002616 // Send ACTION_UP (first slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002617 mDevice->sendSlot(FIRST_SLOT);
2618 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002619 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002620
arthurhungcc7f9802020-04-30 17:55:40 +08002621 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2622 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002623}
2624
Michael Wrightd02c5b62014-02-10 15:10:22 -08002625// --- InputDeviceTest ---
Michael Wrightd02c5b62014-02-10 15:10:22 -08002626class InputDeviceTest : public testing::Test {
2627protected:
2628 static const char* DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002629 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002630 static const int32_t DEVICE_ID;
2631 static const int32_t DEVICE_GENERATION;
2632 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002633 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002634 static const int32_t EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002635
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002636 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002637 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002638 std::unique_ptr<TestInputListener> mFakeListener;
arthurhungdcef2dc2020-08-11 14:47:50 +08002639 std::unique_ptr<InstrumentedInputReader> mReader;
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00002640 std::shared_ptr<InputDevice> mDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002641
Chris Yea52ade12020-08-27 16:49:20 -07002642 void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002643 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002644 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002645 mFakeListener = std::make_unique<TestInputListener>();
arthurhungdcef2dc2020-08-11 14:47:50 +08002646 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002647 *mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002648 InputDeviceIdentifier identifier;
2649 identifier.name = DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002650 identifier.location = DEVICE_LOCATION;
arthurhungdcef2dc2020-08-11 14:47:50 +08002651 mDevice = std::make_shared<InputDevice>(mReader->getContext(), DEVICE_ID, DEVICE_GENERATION,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002652 identifier);
arthurhungdcef2dc2020-08-11 14:47:50 +08002653 mReader->pushNextDevice(mDevice);
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002654 mFakeEventHub->addDevice(EVENTHUB_ID, DEVICE_NAME, ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08002655 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002656 }
2657
Chris Yea52ade12020-08-27 16:49:20 -07002658 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002659 mFakeListener.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002660 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002661 }
2662};
2663
2664const char* InputDeviceTest::DEVICE_NAME = "device";
Arthur Hung2c9a3342019-07-23 14:18:59 +08002665const char* InputDeviceTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002666const int32_t InputDeviceTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002667const int32_t InputDeviceTest::DEVICE_GENERATION = 2;
2668const int32_t InputDeviceTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002669const ftl::Flags<InputDeviceClass> InputDeviceTest::DEVICE_CLASSES =
Chris Ye1b0c7342020-07-28 21:57:03 -07002670 InputDeviceClass::KEYBOARD | InputDeviceClass::TOUCH | InputDeviceClass::JOYSTICK;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002671const int32_t InputDeviceTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002672
2673TEST_F(InputDeviceTest, ImmutableProperties) {
2674 ASSERT_EQ(DEVICE_ID, mDevice->getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002675 ASSERT_STREQ(DEVICE_NAME, mDevice->getName().c_str());
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002676 ASSERT_EQ(ftl::Flags<InputDeviceClass>(0), mDevice->getClasses());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002677}
2678
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002679TEST_F(InputDeviceTest, WhenDeviceCreated_EnabledIsFalse) {
2680 ASSERT_EQ(mDevice->isEnabled(), false);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07002681}
2682
Michael Wrightd02c5b62014-02-10 15:10:22 -08002683TEST_F(InputDeviceTest, WhenNoMappersAreRegistered_DeviceIsIgnored) {
2684 // Configuration.
2685 InputReaderConfiguration config;
2686 mDevice->configure(ARBITRARY_TIME, &config, 0);
2687
2688 // Reset.
2689 mDevice->reset(ARBITRARY_TIME);
2690
2691 NotifyDeviceResetArgs resetArgs;
2692 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2693 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2694 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2695
2696 // Metadata.
2697 ASSERT_TRUE(mDevice->isIgnored());
2698 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mDevice->getSources());
2699
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002700 InputDeviceInfo info = mDevice->getDeviceInfo();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002701 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002702 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002703 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NONE, info.getKeyboardType());
2704 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, info.getSources());
2705
2706 // State queries.
2707 ASSERT_EQ(0, mDevice->getMetaState());
2708
2709 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2710 << "Ignored device should return unknown key code state.";
2711 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2712 << "Ignored device should return unknown scan code state.";
2713 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 0))
2714 << "Ignored device should return unknown switch state.";
2715
2716 const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
2717 uint8_t flags[2] = { 0, 1 };
2718 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, 2, keyCodes, flags))
2719 << "Ignored device should never mark any key codes.";
2720 ASSERT_EQ(0, flags[0]) << "Flag for unsupported key should be unchanged.";
2721 ASSERT_EQ(1, flags[1]) << "Flag for unsupported key should be unchanged.";
2722}
2723
2724TEST_F(InputDeviceTest, WhenMappersAreRegistered_DeviceIsNotIgnoredAndForwardsRequestsToMappers) {
2725 // Configuration.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002726 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, String8("key"), String8("value"));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002727
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002728 FakeInputMapper& mapper1 =
2729 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002730 mapper1.setKeyboardType(AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2731 mapper1.setMetaState(AMETA_ALT_ON);
2732 mapper1.addSupportedKeyCode(AKEYCODE_A);
2733 mapper1.addSupportedKeyCode(AKEYCODE_B);
2734 mapper1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
2735 mapper1.setKeyCodeState(AKEYCODE_B, AKEY_STATE_UP);
2736 mapper1.setScanCodeState(2, AKEY_STATE_DOWN);
2737 mapper1.setScanCodeState(3, AKEY_STATE_UP);
2738 mapper1.setSwitchState(4, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002739
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002740 FakeInputMapper& mapper2 =
2741 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002742 mapper2.setMetaState(AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002743
2744 InputReaderConfiguration config;
2745 mDevice->configure(ARBITRARY_TIME, &config, 0);
2746
2747 String8 propertyValue;
2748 ASSERT_TRUE(mDevice->getConfiguration().tryGetProperty(String8("key"), propertyValue))
2749 << "Device should have read configuration during configuration phase.";
2750 ASSERT_STREQ("value", propertyValue.string());
2751
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002752 ASSERT_NO_FATAL_FAILURE(mapper1.assertConfigureWasCalled());
2753 ASSERT_NO_FATAL_FAILURE(mapper2.assertConfigureWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002754
2755 // Reset
2756 mDevice->reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002757 ASSERT_NO_FATAL_FAILURE(mapper1.assertResetWasCalled());
2758 ASSERT_NO_FATAL_FAILURE(mapper2.assertResetWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002759
2760 NotifyDeviceResetArgs resetArgs;
2761 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2762 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2763 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2764
2765 // Metadata.
2766 ASSERT_FALSE(mDevice->isIgnored());
2767 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), mDevice->getSources());
2768
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002769 InputDeviceInfo info = mDevice->getDeviceInfo();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002770 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002771 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002772 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_ALPHABETIC, info.getKeyboardType());
2773 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), info.getSources());
2774
2775 // State queries.
2776 ASSERT_EQ(AMETA_ALT_ON | AMETA_SHIFT_ON, mDevice->getMetaState())
2777 << "Should query mappers and combine meta states.";
2778
2779 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2780 << "Should return unknown key code state when source not supported.";
2781 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2782 << "Should return unknown scan code state when source not supported.";
2783 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2784 << "Should return unknown switch state when source not supported.";
2785
2786 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, AKEYCODE_A))
2787 << "Should query mapper when source is supported.";
2788 ASSERT_EQ(AKEY_STATE_UP, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 3))
2789 << "Should query mapper when source is supported.";
2790 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 4))
2791 << "Should query mapper when source is supported.";
2792
2793 const int32_t keyCodes[4] = { AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2 };
2794 uint8_t flags[4] = { 0, 0, 0, 1 };
2795 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
2796 << "Should do nothing when source is unsupported.";
2797 ASSERT_EQ(0, flags[0]) << "Flag should be unchanged when source is unsupported.";
2798 ASSERT_EQ(0, flags[1]) << "Flag should be unchanged when source is unsupported.";
2799 ASSERT_EQ(0, flags[2]) << "Flag should be unchanged when source is unsupported.";
2800 ASSERT_EQ(1, flags[3]) << "Flag should be unchanged when source is unsupported.";
2801
2802 ASSERT_TRUE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, 4, keyCodes, flags))
2803 << "Should query mapper when source is supported.";
2804 ASSERT_EQ(1, flags[0]) << "Flag for supported key should be set.";
2805 ASSERT_EQ(1, flags[1]) << "Flag for supported key should be set.";
2806 ASSERT_EQ(0, flags[2]) << "Flag for unsupported key should be unchanged.";
2807 ASSERT_EQ(1, flags[3]) << "Flag for unsupported key should be unchanged.";
2808
2809 // Event handling.
2810 RawEvent event;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002811 event.deviceId = EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002812 mDevice->process(&event, 1);
2813
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002814 ASSERT_NO_FATAL_FAILURE(mapper1.assertProcessWasCalled());
2815 ASSERT_NO_FATAL_FAILURE(mapper2.assertProcessWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002816}
2817
Arthur Hung2c9a3342019-07-23 14:18:59 +08002818// A single input device is associated with a specific display. Check that:
2819// 1. Device is disabled if the viewport corresponding to the associated display is not found
2820// 2. Device is disabled when setEnabled API is called
2821TEST_F(InputDeviceTest, Configure_AssignsDisplayPort) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002822 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002823
2824 // First Configuration.
2825 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
2826
2827 // Device should be enabled by default.
2828 ASSERT_TRUE(mDevice->isEnabled());
2829
2830 // Prepare associated info.
2831 constexpr uint8_t hdmi = 1;
2832 const std::string UNIQUE_ID = "local:1";
2833
2834 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi);
2835 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2836 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2837 // Device should be disabled because it is associated with a specific display via
2838 // input port <-> display port association, but the corresponding display is not found
2839 ASSERT_FALSE(mDevice->isEnabled());
2840
2841 // Prepare displays.
2842 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002843 DISPLAY_ORIENTATION_0, true /*isActive*/, UNIQUE_ID, hdmi,
2844 ViewportType::INTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002845 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2846 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2847 ASSERT_TRUE(mDevice->isEnabled());
2848
2849 // Device should be disabled after set disable.
2850 mFakePolicy->addDisabledDevice(mDevice->getId());
2851 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2852 InputReaderConfiguration::CHANGE_ENABLED_STATE);
2853 ASSERT_FALSE(mDevice->isEnabled());
2854
2855 // Device should still be disabled even found the associated display.
2856 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2857 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2858 ASSERT_FALSE(mDevice->isEnabled());
2859}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002860
Christine Franks1ba71cc2021-04-07 14:37:42 -07002861TEST_F(InputDeviceTest, Configure_AssignsDisplayUniqueId) {
2862 // Device should be enabled by default.
2863 mFakePolicy->clearViewports();
2864 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
2865 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
2866 ASSERT_TRUE(mDevice->isEnabled());
2867
2868 // Device should be disabled because it is associated with a specific display, but the
2869 // corresponding display is not found.
2870 const std::string DISPLAY_UNIQUE_ID = "displayUniqueId";
Christine Franks2a2293c2022-01-18 11:51:16 -08002871 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002872 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2873 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2874 ASSERT_FALSE(mDevice->isEnabled());
2875
2876 // Device should be enabled when a display is found.
2877 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2878 DISPLAY_ORIENTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
2879 NO_PORT, ViewportType::INTERNAL);
2880 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2881 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2882 ASSERT_TRUE(mDevice->isEnabled());
2883
2884 // Device should be disabled after set disable.
2885 mFakePolicy->addDisabledDevice(mDevice->getId());
2886 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2887 InputReaderConfiguration::CHANGE_ENABLED_STATE);
2888 ASSERT_FALSE(mDevice->isEnabled());
2889
2890 // Device should still be disabled even found the associated display.
2891 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2892 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2893 ASSERT_FALSE(mDevice->isEnabled());
2894}
2895
Christine Franks2a2293c2022-01-18 11:51:16 -08002896TEST_F(InputDeviceTest, Configure_UniqueId_CorrectlyMatches) {
2897 mFakePolicy->clearViewports();
2898 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
2899 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
2900
2901 const std::string DISPLAY_UNIQUE_ID = "displayUniqueId";
2902 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
2903 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2904 DISPLAY_ORIENTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
2905 NO_PORT, ViewportType::INTERNAL);
2906 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2907 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2908 ASSERT_EQ(DISPLAY_UNIQUE_ID, mDevice->getAssociatedDisplayUniqueId());
2909}
2910
Michael Wrightd02c5b62014-02-10 15:10:22 -08002911// --- InputMapperTest ---
2912
2913class InputMapperTest : public testing::Test {
2914protected:
2915 static const char* DEVICE_NAME;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002916 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002917 static const int32_t DEVICE_ID;
2918 static const int32_t DEVICE_GENERATION;
2919 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002920 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002921 static const int32_t EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002922
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002923 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002924 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002925 std::unique_ptr<TestInputListener> mFakeListener;
arthurhungdcef2dc2020-08-11 14:47:50 +08002926 std::unique_ptr<InstrumentedInputReader> mReader;
2927 std::shared_ptr<InputDevice> mDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002928
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002929 virtual void SetUp(ftl::Flags<InputDeviceClass> classes) {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002930 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002931 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002932 mFakeListener = std::make_unique<TestInputListener>();
arthurhungdcef2dc2020-08-11 14:47:50 +08002933 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002934 *mFakeListener);
arthurhungdcef2dc2020-08-11 14:47:50 +08002935 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002936 }
2937
Prabir Pradhanc14266f2021-05-12 15:56:24 -07002938 void SetUp() override {
Prabir Pradhanc14266f2021-05-12 15:56:24 -07002939 SetUp(DEVICE_CLASSES);
2940 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002941
Chris Yea52ade12020-08-27 16:49:20 -07002942 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002943 mFakeListener.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002944 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002945 }
2946
2947 void addConfigurationProperty(const char* key, const char* value) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002948 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, String8(key), String8(value));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002949 }
2950
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002951 void configureDevice(uint32_t changes) {
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -08002952 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
arthurhungdcef2dc2020-08-11 14:47:50 +08002953 mReader->requestRefreshConfiguration(changes);
2954 mReader->loopOnce();
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -08002955 }
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002956 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
2957 }
2958
arthurhungdcef2dc2020-08-11 14:47:50 +08002959 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
2960 const std::string& location, int32_t eventHubId,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002961 ftl::Flags<InputDeviceClass> classes) {
arthurhungdcef2dc2020-08-11 14:47:50 +08002962 InputDeviceIdentifier identifier;
2963 identifier.name = name;
2964 identifier.location = location;
2965 std::shared_ptr<InputDevice> device =
2966 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
2967 identifier);
2968 mReader->pushNextDevice(device);
2969 mFakeEventHub->addDevice(eventHubId, name, classes);
2970 mReader->loopOnce();
2971 return device;
2972 }
2973
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002974 template <class T, typename... Args>
2975 T& addMapperAndConfigure(Args... args) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002976 T& mapper = mDevice->addMapper<T>(EVENTHUB_ID, args...);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002977 configureDevice(0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002978 mDevice->reset(ARBITRARY_TIME);
Chris Ye42b06822020-08-07 11:39:33 -07002979 mapper.reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002980 return mapper;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002981 }
2982
2983 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002984 int32_t orientation, const std::string& uniqueId,
2985 std::optional<uint8_t> physicalPort, ViewportType viewportType) {
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002986 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, true /*isActive*/,
2987 uniqueId, physicalPort, viewportType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07002988 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2989 }
2990
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002991 void clearViewports() {
2992 mFakePolicy->clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002993 }
2994
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002995 void process(InputMapper& mapper, nsecs_t when, nsecs_t readTime, int32_t type, int32_t code,
2996 int32_t value) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002997 RawEvent event;
2998 event.when = when;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002999 event.readTime = readTime;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003000 event.deviceId = mapper.getDeviceContext().getEventHubId();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003001 event.type = type;
3002 event.code = code;
3003 event.value = value;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003004 mapper.process(&event);
arthurhungdcef2dc2020-08-11 14:47:50 +08003005 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003006 }
3007
3008 static void assertMotionRange(const InputDeviceInfo& info,
3009 int32_t axis, uint32_t source, float min, float max, float flat, float fuzz) {
3010 const InputDeviceInfo::MotionRange* range = info.getMotionRange(axis, source);
Yi Kong9b14ac62018-07-17 13:48:38 -07003011 ASSERT_TRUE(range != nullptr) << "Axis: " << axis << " Source: " << source;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003012 ASSERT_EQ(axis, range->axis) << "Axis: " << axis << " Source: " << source;
3013 ASSERT_EQ(source, range->source) << "Axis: " << axis << " Source: " << source;
3014 ASSERT_NEAR(min, range->min, EPSILON) << "Axis: " << axis << " Source: " << source;
3015 ASSERT_NEAR(max, range->max, EPSILON) << "Axis: " << axis << " Source: " << source;
3016 ASSERT_NEAR(flat, range->flat, EPSILON) << "Axis: " << axis << " Source: " << source;
3017 ASSERT_NEAR(fuzz, range->fuzz, EPSILON) << "Axis: " << axis << " Source: " << source;
3018 }
3019
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003020 static void assertPointerCoords(const PointerCoords& coords, float x, float y, float pressure,
3021 float size, float touchMajor, float touchMinor, float toolMajor,
3022 float toolMinor, float orientation, float distance,
3023 float scaledAxisEpsilon = 1.f) {
3024 ASSERT_NEAR(x, coords.getAxisValue(AMOTION_EVENT_AXIS_X), scaledAxisEpsilon);
3025 ASSERT_NEAR(y, coords.getAxisValue(AMOTION_EVENT_AXIS_Y), scaledAxisEpsilon);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003026 ASSERT_NEAR(pressure, coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), EPSILON);
3027 ASSERT_NEAR(size, coords.getAxisValue(AMOTION_EVENT_AXIS_SIZE), EPSILON);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003028 ASSERT_NEAR(touchMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR),
3029 scaledAxisEpsilon);
3030 ASSERT_NEAR(touchMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR),
3031 scaledAxisEpsilon);
3032 ASSERT_NEAR(toolMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR),
3033 scaledAxisEpsilon);
3034 ASSERT_NEAR(toolMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR),
3035 scaledAxisEpsilon);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003036 ASSERT_NEAR(orientation, coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION), EPSILON);
3037 ASSERT_NEAR(distance, coords.getAxisValue(AMOTION_EVENT_AXIS_DISTANCE), EPSILON);
3038 }
3039
Michael Wright17db18e2020-06-26 20:51:44 +01003040 static void assertPosition(const FakePointerController& controller, float x, float y) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003041 float actualX, actualY;
Michael Wright17db18e2020-06-26 20:51:44 +01003042 controller.getPosition(&actualX, &actualY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003043 ASSERT_NEAR(x, actualX, 1);
3044 ASSERT_NEAR(y, actualY, 1);
3045 }
3046};
3047
3048const char* InputMapperTest::DEVICE_NAME = "device";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003049const char* InputMapperTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003050const int32_t InputMapperTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003051const int32_t InputMapperTest::DEVICE_GENERATION = 2;
3052const int32_t InputMapperTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003053const ftl::Flags<InputDeviceClass> InputMapperTest::DEVICE_CLASSES =
3054 ftl::Flags<InputDeviceClass>(0); // not needed for current tests
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003055const int32_t InputMapperTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003056
3057// --- SwitchInputMapperTest ---
3058
3059class SwitchInputMapperTest : public InputMapperTest {
3060protected:
3061};
3062
3063TEST_F(SwitchInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003064 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003065
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003066 ASSERT_EQ(uint32_t(AINPUT_SOURCE_SWITCH), mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003067}
3068
3069TEST_F(SwitchInputMapperTest, GetSwitchState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003070 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003071
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003072 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003073 ASSERT_EQ(1, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003074
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003075 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003076 ASSERT_EQ(0, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003077}
3078
3079TEST_F(SwitchInputMapperTest, Process) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003080 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003081
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003082 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_LID, 1);
3083 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_JACK_PHYSICAL_INSERT, 1);
3084 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_HEADPHONE_INSERT, 0);
3085 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003086
3087 NotifySwitchArgs args;
3088 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySwitchWasCalled(&args));
3089 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
Dan Albert1bd2fc02016-02-02 15:11:57 -08003090 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT), args.switchValues);
3091 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT) | (1 << SW_HEADPHONE_INSERT),
Michael Wrightd02c5b62014-02-10 15:10:22 -08003092 args.switchMask);
3093 ASSERT_EQ(uint32_t(0), args.policyFlags);
3094}
3095
Chris Ye87143712020-11-10 05:05:58 +00003096// --- VibratorInputMapperTest ---
3097class VibratorInputMapperTest : public InputMapperTest {
3098protected:
3099 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::VIBRATOR); }
3100};
3101
3102TEST_F(VibratorInputMapperTest, GetSources) {
3103 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
3104
3105 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mapper.getSources());
3106}
3107
3108TEST_F(VibratorInputMapperTest, GetVibratorIds) {
3109 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
3110
3111 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
3112}
3113
3114TEST_F(VibratorInputMapperTest, Vibrate) {
3115 constexpr uint8_t DEFAULT_AMPLITUDE = 192;
Chris Yefb552902021-02-03 17:18:37 -08003116 constexpr int32_t VIBRATION_TOKEN = 100;
Chris Ye87143712020-11-10 05:05:58 +00003117 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
3118
3119 VibrationElement pattern(2);
3120 VibrationSequence sequence(2);
3121 pattern.duration = std::chrono::milliseconds(200);
3122 pattern.channels = {{0 /* vibratorId */, DEFAULT_AMPLITUDE / 2},
3123 {1 /* vibratorId */, DEFAULT_AMPLITUDE}};
3124 sequence.addElement(pattern);
3125 pattern.duration = std::chrono::milliseconds(500);
3126 pattern.channels = {{0 /* vibratorId */, DEFAULT_AMPLITUDE / 4},
3127 {1 /* vibratorId */, DEFAULT_AMPLITUDE}};
3128 sequence.addElement(pattern);
3129
3130 std::vector<int64_t> timings = {0, 1};
3131 std::vector<uint8_t> amplitudes = {DEFAULT_AMPLITUDE, DEFAULT_AMPLITUDE / 2};
3132
3133 ASSERT_FALSE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08003134 // Start vibrating
3135 mapper.vibrate(sequence, -1 /* repeat */, VIBRATION_TOKEN);
Chris Ye87143712020-11-10 05:05:58 +00003136 ASSERT_TRUE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08003137 // Verify vibrator state listener was notified.
3138 mReader->loopOnce();
3139 NotifyVibratorStateArgs args;
3140 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyVibratorStateWasCalled(&args));
3141 ASSERT_EQ(DEVICE_ID, args.deviceId);
3142 ASSERT_TRUE(args.isOn);
3143 // Stop vibrating
3144 mapper.cancelVibrate(VIBRATION_TOKEN);
3145 ASSERT_FALSE(mapper.isVibrating());
3146 // Verify vibrator state listener was notified.
3147 mReader->loopOnce();
3148 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyVibratorStateWasCalled(&args));
3149 ASSERT_EQ(DEVICE_ID, args.deviceId);
3150 ASSERT_FALSE(args.isOn);
Chris Ye87143712020-11-10 05:05:58 +00003151}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003152
Chris Yef59a2f42020-10-16 12:55:26 -07003153// --- SensorInputMapperTest ---
3154
3155class SensorInputMapperTest : public InputMapperTest {
3156protected:
3157 static const int32_t ACCEL_RAW_MIN;
3158 static const int32_t ACCEL_RAW_MAX;
3159 static const int32_t ACCEL_RAW_FUZZ;
3160 static const int32_t ACCEL_RAW_FLAT;
3161 static const int32_t ACCEL_RAW_RESOLUTION;
3162
3163 static const int32_t GYRO_RAW_MIN;
3164 static const int32_t GYRO_RAW_MAX;
3165 static const int32_t GYRO_RAW_FUZZ;
3166 static const int32_t GYRO_RAW_FLAT;
3167 static const int32_t GYRO_RAW_RESOLUTION;
3168
3169 static const float GRAVITY_MS2_UNIT;
3170 static const float DEGREE_RADIAN_UNIT;
3171
3172 void prepareAccelAxes();
3173 void prepareGyroAxes();
3174 void setAccelProperties();
3175 void setGyroProperties();
3176 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::SENSOR); }
3177};
3178
3179const int32_t SensorInputMapperTest::ACCEL_RAW_MIN = -32768;
3180const int32_t SensorInputMapperTest::ACCEL_RAW_MAX = 32768;
3181const int32_t SensorInputMapperTest::ACCEL_RAW_FUZZ = 16;
3182const int32_t SensorInputMapperTest::ACCEL_RAW_FLAT = 0;
3183const int32_t SensorInputMapperTest::ACCEL_RAW_RESOLUTION = 8192;
3184
3185const int32_t SensorInputMapperTest::GYRO_RAW_MIN = -2097152;
3186const int32_t SensorInputMapperTest::GYRO_RAW_MAX = 2097152;
3187const int32_t SensorInputMapperTest::GYRO_RAW_FUZZ = 16;
3188const int32_t SensorInputMapperTest::GYRO_RAW_FLAT = 0;
3189const int32_t SensorInputMapperTest::GYRO_RAW_RESOLUTION = 1024;
3190
3191const float SensorInputMapperTest::GRAVITY_MS2_UNIT = 9.80665f;
3192const float SensorInputMapperTest::DEGREE_RADIAN_UNIT = 0.0174533f;
3193
3194void SensorInputMapperTest::prepareAccelAxes() {
3195 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3196 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3197 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3198 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3199 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Z, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3200 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3201}
3202
3203void SensorInputMapperTest::prepareGyroAxes() {
3204 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RX, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3205 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3206 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RY, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3207 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3208 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RZ, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3209 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3210}
3211
3212void SensorInputMapperTest::setAccelProperties() {
3213 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 0, InputDeviceSensorType::ACCELEROMETER,
3214 /* sensorDataIndex */ 0);
3215 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 1, InputDeviceSensorType::ACCELEROMETER,
3216 /* sensorDataIndex */ 1);
3217 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 2, InputDeviceSensorType::ACCELEROMETER,
3218 /* sensorDataIndex */ 2);
3219 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
3220 addConfigurationProperty("sensor.accelerometer.reportingMode", "0");
3221 addConfigurationProperty("sensor.accelerometer.maxDelay", "100000");
3222 addConfigurationProperty("sensor.accelerometer.minDelay", "5000");
3223 addConfigurationProperty("sensor.accelerometer.power", "1.5");
3224}
3225
3226void SensorInputMapperTest::setGyroProperties() {
3227 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 3, InputDeviceSensorType::GYROSCOPE,
3228 /* sensorDataIndex */ 0);
3229 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 4, InputDeviceSensorType::GYROSCOPE,
3230 /* sensorDataIndex */ 1);
3231 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 5, InputDeviceSensorType::GYROSCOPE,
3232 /* sensorDataIndex */ 2);
3233 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
3234 addConfigurationProperty("sensor.gyroscope.reportingMode", "0");
3235 addConfigurationProperty("sensor.gyroscope.maxDelay", "100000");
3236 addConfigurationProperty("sensor.gyroscope.minDelay", "5000");
3237 addConfigurationProperty("sensor.gyroscope.power", "0.8");
3238}
3239
3240TEST_F(SensorInputMapperTest, GetSources) {
3241 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3242
3243 ASSERT_EQ(static_cast<uint32_t>(AINPUT_SOURCE_SENSOR), mapper.getSources());
3244}
3245
3246TEST_F(SensorInputMapperTest, ProcessAccelerometerSensor) {
3247 setAccelProperties();
3248 prepareAccelAxes();
3249 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3250
3251 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::ACCELEROMETER,
3252 std::chrono::microseconds(10000),
3253 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08003254 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003255 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, 20000);
3256 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, -20000);
3257 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Z, 40000);
3258 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
3259 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07003260
3261 NotifySensorArgs args;
3262 std::vector<float> values = {20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
3263 -20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
3264 40000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT};
3265
3266 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
3267 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
3268 ASSERT_EQ(args.deviceId, DEVICE_ID);
3269 ASSERT_EQ(args.sensorType, InputDeviceSensorType::ACCELEROMETER);
3270 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
3271 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
3272 ASSERT_EQ(args.values, values);
3273 mapper.flushSensor(InputDeviceSensorType::ACCELEROMETER);
3274}
3275
3276TEST_F(SensorInputMapperTest, ProcessGyroscopeSensor) {
3277 setGyroProperties();
3278 prepareGyroAxes();
3279 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3280
3281 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::GYROSCOPE,
3282 std::chrono::microseconds(10000),
3283 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08003284 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003285 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RX, 20000);
3286 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RY, -20000);
3287 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RZ, 40000);
3288 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
3289 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07003290
3291 NotifySensorArgs args;
3292 std::vector<float> values = {20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
3293 -20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
3294 40000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT};
3295
3296 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
3297 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
3298 ASSERT_EQ(args.deviceId, DEVICE_ID);
3299 ASSERT_EQ(args.sensorType, InputDeviceSensorType::GYROSCOPE);
3300 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
3301 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
3302 ASSERT_EQ(args.values, values);
3303 mapper.flushSensor(InputDeviceSensorType::GYROSCOPE);
3304}
3305
Michael Wrightd02c5b62014-02-10 15:10:22 -08003306// --- KeyboardInputMapperTest ---
3307
3308class KeyboardInputMapperTest : public InputMapperTest {
3309protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003310 const std::string UNIQUE_ID = "local:0";
3311
3312 void prepareDisplay(int32_t orientation);
3313
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003314 void testDPadKeyRotation(KeyboardInputMapper& mapper, int32_t originalScanCode,
Arthur Hung2c9a3342019-07-23 14:18:59 +08003315 int32_t originalKeyCode, int32_t rotatedKeyCode,
3316 int32_t displayId = ADISPLAY_ID_NONE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003317};
3318
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003319/* Similar to setDisplayInfoAndReconfigure, but pre-populates all parameters except for the
3320 * orientation.
3321 */
3322void KeyboardInputMapperTest::prepareDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003323 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
3324 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003325}
3326
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003327void KeyboardInputMapperTest::testDPadKeyRotation(KeyboardInputMapper& mapper,
Arthur Hung2c9a3342019-07-23 14:18:59 +08003328 int32_t originalScanCode, int32_t originalKeyCode,
3329 int32_t rotatedKeyCode, int32_t displayId) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003330 NotifyKeyArgs args;
3331
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003332 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003333 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3334 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3335 ASSERT_EQ(originalScanCode, args.scanCode);
3336 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003337 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003338
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003339 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003340 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3341 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3342 ASSERT_EQ(originalScanCode, args.scanCode);
3343 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003344 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003345}
3346
Michael Wrightd02c5b62014-02-10 15:10:22 -08003347TEST_F(KeyboardInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003348 KeyboardInputMapper& mapper =
3349 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3350 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003351
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003352 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003353}
3354
3355TEST_F(KeyboardInputMapperTest, Process_SimpleKeyPress) {
3356 const int32_t USAGE_A = 0x070004;
3357 const int32_t USAGE_UNKNOWN = 0x07ffff;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003358 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3359 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
Chris Yea52ade12020-08-27 16:49:20 -07003360 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, POLICY_FLAG_WAKE);
3361 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, POLICY_FLAG_WAKE);
3362 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003363
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003364 KeyboardInputMapper& mapper =
3365 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3366 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
arthurhungc903df12020-08-11 15:08:42 +08003367 // Initial metastate to AMETA_NONE.
3368 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
3369 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003370
3371 // Key down by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003372 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003373 NotifyKeyArgs args;
3374 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3375 ASSERT_EQ(DEVICE_ID, args.deviceId);
3376 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3377 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3378 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3379 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3380 ASSERT_EQ(KEY_HOME, args.scanCode);
3381 ASSERT_EQ(AMETA_NONE, args.metaState);
3382 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3383 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3384 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3385
3386 // Key up by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003387 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003388 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3389 ASSERT_EQ(DEVICE_ID, args.deviceId);
3390 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3391 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3392 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3393 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3394 ASSERT_EQ(KEY_HOME, args.scanCode);
3395 ASSERT_EQ(AMETA_NONE, args.metaState);
3396 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3397 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3398 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3399
3400 // Key down by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003401 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3402 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, 0, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003403 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3404 ASSERT_EQ(DEVICE_ID, args.deviceId);
3405 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3406 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3407 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3408 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3409 ASSERT_EQ(0, args.scanCode);
3410 ASSERT_EQ(AMETA_NONE, args.metaState);
3411 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3412 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3413 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3414
3415 // Key up by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003416 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3417 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003418 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3419 ASSERT_EQ(DEVICE_ID, args.deviceId);
3420 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3421 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3422 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3423 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3424 ASSERT_EQ(0, args.scanCode);
3425 ASSERT_EQ(AMETA_NONE, args.metaState);
3426 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3427 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3428 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3429
3430 // Key down with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003431 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3432 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UNKNOWN, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003433 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3434 ASSERT_EQ(DEVICE_ID, args.deviceId);
3435 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3436 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3437 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3438 ASSERT_EQ(0, args.keyCode);
3439 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3440 ASSERT_EQ(AMETA_NONE, args.metaState);
3441 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3442 ASSERT_EQ(0U, args.policyFlags);
3443 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3444
3445 // Key up with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003446 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3447 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003448 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3449 ASSERT_EQ(DEVICE_ID, args.deviceId);
3450 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3451 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3452 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3453 ASSERT_EQ(0, args.keyCode);
3454 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3455 ASSERT_EQ(AMETA_NONE, args.metaState);
3456 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3457 ASSERT_EQ(0U, args.policyFlags);
3458 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3459}
3460
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003461/**
3462 * Ensure that the readTime is set to the time when the EV_KEY is received.
3463 */
3464TEST_F(KeyboardInputMapperTest, Process_SendsReadTime) {
3465 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3466
3467 KeyboardInputMapper& mapper =
3468 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3469 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3470 NotifyKeyArgs args;
3471
3472 // Key down
3473 process(mapper, ARBITRARY_TIME, 12 /*readTime*/, EV_KEY, KEY_HOME, 1);
3474 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3475 ASSERT_EQ(12, args.readTime);
3476
3477 // Key up
3478 process(mapper, ARBITRARY_TIME, 15 /*readTime*/, EV_KEY, KEY_HOME, 1);
3479 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3480 ASSERT_EQ(15, args.readTime);
3481}
3482
Michael Wrightd02c5b62014-02-10 15:10:22 -08003483TEST_F(KeyboardInputMapperTest, Process_ShouldUpdateMetaState) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003484 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFTSHIFT, 0, AKEYCODE_SHIFT_LEFT, 0);
3485 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003486 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, 0);
3487 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, 0);
3488 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003489
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003490 KeyboardInputMapper& mapper =
3491 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3492 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003493
arthurhungc903df12020-08-11 15:08:42 +08003494 // Initial metastate to AMETA_NONE.
3495 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
3496 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003497
3498 // Metakey down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003499 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003500 NotifyKeyArgs args;
3501 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3502 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003503 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003504 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003505
3506 // Key down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003507 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_A, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003508 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3509 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003510 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003511
3512 // Key up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003513 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, KEY_A, 0);
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 // Metakey up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003519 process(mapper, ARBITRARY_TIME + 3, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003520 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3521 ASSERT_EQ(AMETA_NONE, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003522 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003523 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003524}
3525
3526TEST_F(KeyboardInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003527 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3528 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3529 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3530 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003531
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003532 KeyboardInputMapper& mapper =
3533 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3534 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003535
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003536 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003537 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3538 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
3539 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3540 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
3541 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3542 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
3543 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3544 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
3545}
3546
3547TEST_F(KeyboardInputMapperTest, Process_WhenOrientationAware_ShouldRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003548 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3549 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3550 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3551 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003552
Michael Wrightd02c5b62014-02-10 15:10:22 -08003553 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003554 KeyboardInputMapper& mapper =
3555 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3556 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003557
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003558 prepareDisplay(DISPLAY_ORIENTATION_0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003559 ASSERT_NO_FATAL_FAILURE(
3560 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3561 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3562 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3563 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3564 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3565 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3566 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003567
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003568 clearViewports();
3569 prepareDisplay(DISPLAY_ORIENTATION_90);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003570 ASSERT_NO_FATAL_FAILURE(
3571 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3572 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3573 AKEYCODE_DPAD_UP, DISPLAY_ID));
3574 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3575 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3576 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3577 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003578
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003579 clearViewports();
3580 prepareDisplay(DISPLAY_ORIENTATION_180);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003581 ASSERT_NO_FATAL_FAILURE(
3582 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3583 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3584 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3585 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3586 AKEYCODE_DPAD_UP, DISPLAY_ID));
3587 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3588 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003589
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003590 clearViewports();
3591 prepareDisplay(DISPLAY_ORIENTATION_270);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003592 ASSERT_NO_FATAL_FAILURE(
3593 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3594 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3595 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3596 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3597 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3598 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3599 AKEYCODE_DPAD_UP, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003600
3601 // Special case: if orientation changes while key is down, we still emit the same keycode
3602 // in the key up as we did in the key down.
3603 NotifyKeyArgs args;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003604 clearViewports();
3605 prepareDisplay(DISPLAY_ORIENTATION_270);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003606 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003607 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3608 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3609 ASSERT_EQ(KEY_UP, args.scanCode);
3610 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3611
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003612 clearViewports();
3613 prepareDisplay(DISPLAY_ORIENTATION_180);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003614 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003615 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3616 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3617 ASSERT_EQ(KEY_UP, args.scanCode);
3618 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3619}
3620
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003621TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_NotOrientationAware) {
3622 // If the keyboard is not orientation aware,
3623 // key events should not be associated with a specific display id
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003624 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003625
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003626 KeyboardInputMapper& mapper =
3627 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3628 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003629 NotifyKeyArgs args;
3630
3631 // Display id should be ADISPLAY_ID_NONE without any display configuration.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003632 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003633 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003634 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003635 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3636 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3637
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003638 prepareDisplay(DISPLAY_ORIENTATION_0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003639 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003640 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003641 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003642 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3643 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3644}
3645
3646TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_OrientationAware) {
3647 // If the keyboard is orientation aware,
3648 // key events should be associated with the internal viewport
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003649 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003650
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003651 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003652 KeyboardInputMapper& mapper =
3653 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3654 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003655 NotifyKeyArgs args;
3656
3657 // Display id should be ADISPLAY_ID_NONE without any display configuration.
3658 // ^--- already checked by the previous test
3659
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003660 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003661 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003662 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003663 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003664 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003665 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3666 ASSERT_EQ(DISPLAY_ID, args.displayId);
3667
3668 constexpr int32_t newDisplayId = 2;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003669 clearViewports();
3670 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003671 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003672 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003673 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003674 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003675 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3676 ASSERT_EQ(newDisplayId, args.displayId);
3677}
3678
Michael Wrightd02c5b62014-02-10 15:10:22 -08003679TEST_F(KeyboardInputMapperTest, GetKeyCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003680 KeyboardInputMapper& mapper =
3681 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3682 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003683
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003684 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003685 ASSERT_EQ(1, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003686
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003687 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003688 ASSERT_EQ(0, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003689}
3690
Philip Junker4af3b3d2021-12-14 10:36:55 +01003691TEST_F(KeyboardInputMapperTest, GetKeyCodeForKeyLocation) {
3692 KeyboardInputMapper& mapper =
3693 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3694 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3695
3696 mFakeEventHub->addKeyCodeMapping(EVENTHUB_ID, AKEYCODE_Y, AKEYCODE_Z);
3697 ASSERT_EQ(AKEYCODE_Z, mapper.getKeyCodeForKeyLocation(AKEYCODE_Y))
3698 << "If a mapping is available, the result is equal to the mapping";
3699
3700 ASSERT_EQ(AKEYCODE_A, mapper.getKeyCodeForKeyLocation(AKEYCODE_A))
3701 << "If no mapping is available, the result is the key location";
3702}
3703
Michael Wrightd02c5b62014-02-10 15:10:22 -08003704TEST_F(KeyboardInputMapperTest, GetScanCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003705 KeyboardInputMapper& mapper =
3706 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3707 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003708
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003709 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003710 ASSERT_EQ(1, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003711
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003712 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003713 ASSERT_EQ(0, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003714}
3715
3716TEST_F(KeyboardInputMapperTest, MarkSupportedKeyCodes) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003717 KeyboardInputMapper& mapper =
3718 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3719 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003720
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003721 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003722
3723 const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
3724 uint8_t flags[2] = { 0, 0 };
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003725 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, 1, keyCodes, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003726 ASSERT_TRUE(flags[0]);
3727 ASSERT_FALSE(flags[1]);
3728}
3729
3730TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleMetaStateAndLeds) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003731 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3732 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3733 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3734 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3735 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3736 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003737
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003738 KeyboardInputMapper& mapper =
3739 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3740 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Chris Yea52ade12020-08-27 16:49:20 -07003741 // Initialize metastate to AMETA_NUM_LOCK_ON.
arthurhungc903df12020-08-11 15:08:42 +08003742 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
3743 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003744
3745 // Initialization should have turned all of the lights off.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003746 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3747 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3748 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003749
3750 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003751 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3752 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003753 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3754 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3755 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003756 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003757
3758 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003759 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3760 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003761 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3762 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3763 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003764 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003765
3766 // Toggle caps lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003767 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3768 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003769 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3770 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3771 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003772 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003773
3774 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003775 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3776 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003777 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3778 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3779 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003780 ASSERT_EQ(AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003781
3782 // Toggle num lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003783 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3784 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003785 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3786 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3787 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003788 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003789
3790 // Toggle scroll lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003791 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3792 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003793 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3794 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3795 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003796 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003797}
3798
Chris Yea52ade12020-08-27 16:49:20 -07003799TEST_F(KeyboardInputMapperTest, NoMetaStateWhenMetaKeysNotPresent) {
3800 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_BUTTON_A, 0);
3801 mFakeEventHub->addKey(EVENTHUB_ID, BTN_B, 0, AKEYCODE_BUTTON_B, 0);
3802 mFakeEventHub->addKey(EVENTHUB_ID, BTN_X, 0, AKEYCODE_BUTTON_X, 0);
3803 mFakeEventHub->addKey(EVENTHUB_ID, BTN_Y, 0, AKEYCODE_BUTTON_Y, 0);
3804
3805 KeyboardInputMapper& mapper =
3806 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3807 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
3808
3809 // Initial metastate should be AMETA_NONE as no meta keys added.
3810 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3811 // Meta state should be AMETA_NONE after reset
3812 mapper.reset(ARBITRARY_TIME);
3813 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3814 // Meta state should be AMETA_NONE with update, as device doesn't have the keys.
3815 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
3816 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3817
3818 NotifyKeyArgs args;
3819 // Press button "A"
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003820 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_A, 1);
Chris Yea52ade12020-08-27 16:49:20 -07003821 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3822 ASSERT_EQ(AMETA_NONE, args.metaState);
3823 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3824 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3825 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3826
3827 // Button up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003828 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003829 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3830 ASSERT_EQ(AMETA_NONE, args.metaState);
3831 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3832 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3833 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3834}
3835
Arthur Hung2c9a3342019-07-23 14:18:59 +08003836TEST_F(KeyboardInputMapperTest, Configure_AssignsDisplayPort) {
3837 // keyboard 1.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003838 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3839 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3840 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3841 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003842
3843 // keyboard 2.
3844 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08003845 const std::string DEVICE_NAME2 = "KEYBOARD2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08003846 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003847 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08003848 std::shared_ptr<InputDevice> device2 =
3849 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003850 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08003851
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003852 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3853 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3854 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3855 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003856
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003857 KeyboardInputMapper& mapper =
3858 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3859 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003860
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003861 KeyboardInputMapper& mapper2 =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003862 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003863 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003864 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
3865 device2->reset(ARBITRARY_TIME);
3866
3867 // Prepared displays and associated info.
3868 constexpr uint8_t hdmi1 = 0;
3869 constexpr uint8_t hdmi2 = 1;
3870 const std::string SECONDARY_UNIQUE_ID = "local:1";
3871
3872 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
3873 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
3874
3875 // No associated display viewport found, should disable the device.
3876 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3877 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3878 ASSERT_FALSE(device2->isEnabled());
3879
3880 // Prepare second display.
3881 constexpr int32_t newDisplayId = 2;
3882 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003883 UNIQUE_ID, hdmi1, ViewportType::INTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003884 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003885 SECONDARY_UNIQUE_ID, hdmi2, ViewportType::EXTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003886 // Default device will reconfigure above, need additional reconfiguration for another device.
3887 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3888 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3889
3890 // Device should be enabled after the associated display is found.
3891 ASSERT_TRUE(mDevice->isEnabled());
3892 ASSERT_TRUE(device2->isEnabled());
3893
3894 // Test pad key events
3895 ASSERT_NO_FATAL_FAILURE(
3896 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3897 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3898 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3899 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3900 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3901 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3902 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3903
3904 ASSERT_NO_FATAL_FAILURE(
3905 testDPadKeyRotation(mapper2, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, newDisplayId));
3906 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3907 AKEYCODE_DPAD_RIGHT, newDisplayId));
3908 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3909 AKEYCODE_DPAD_DOWN, newDisplayId));
3910 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3911 AKEYCODE_DPAD_LEFT, newDisplayId));
3912}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003913
arthurhungc903df12020-08-11 15:08:42 +08003914TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleAfterReattach) {
3915 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3916 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3917 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3918 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3919 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3920 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3921
3922 KeyboardInputMapper& mapper =
3923 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3924 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3925 // Initial metastate to AMETA_NONE.
3926 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
3927 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
3928
3929 // Initialization should have turned all of the lights off.
3930 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3931 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3932 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3933
3934 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003935 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3936 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003937 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3938 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
3939
3940 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003941 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3942 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003943 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3944 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
3945
3946 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003947 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3948 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003949 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3950 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
3951
3952 mFakeEventHub->removeDevice(EVENTHUB_ID);
3953 mReader->loopOnce();
3954
3955 // keyboard 2 should default toggle keys.
3956 const std::string USB2 = "USB2";
3957 const std::string DEVICE_NAME2 = "KEYBOARD2";
3958 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
3959 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
3960 std::shared_ptr<InputDevice> device2 =
3961 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003962 ftl::Flags<InputDeviceClass>(0));
arthurhungc903df12020-08-11 15:08:42 +08003963 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3964 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
3965 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3966 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3967 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3968 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3969
arthurhung6fe95782020-10-05 22:41:16 +08003970 KeyboardInputMapper& mapper2 =
3971 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
3972 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
arthurhungc903df12020-08-11 15:08:42 +08003973 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
3974 device2->reset(ARBITRARY_TIME);
3975
3976 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_CAPSL));
3977 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_NUML));
3978 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_SCROLLL));
arthurhung6fe95782020-10-05 22:41:16 +08003979 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON,
3980 mapper2.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08003981}
3982
Arthur Hungcb40a002021-08-03 14:31:01 +00003983TEST_F(KeyboardInputMapperTest, Process_toggleCapsLockState) {
3984 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3985 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3986 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3987
3988 // Suppose we have two mappers. (DPAD + KEYBOARD)
3989 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_DPAD,
3990 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
3991 KeyboardInputMapper& mapper =
3992 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3993 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3994 // Initialize metastate to AMETA_NUM_LOCK_ON.
3995 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
3996 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
3997
3998 mReader->toggleCapsLockState(DEVICE_ID);
3999 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
4000}
4001
Arthur Hungfb3cc112022-04-13 07:39:50 +00004002TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleInMultiDevices) {
4003 // keyboard 1.
4004 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
4005 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
4006 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
4007 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4008 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4009 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
4010
4011 KeyboardInputMapper& mapper1 =
4012 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4013 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
4014
4015 // keyboard 2.
4016 const std::string USB2 = "USB2";
4017 const std::string DEVICE_NAME2 = "KEYBOARD2";
4018 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
4019 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
4020 std::shared_ptr<InputDevice> device2 =
4021 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
4022 ftl::Flags<InputDeviceClass>(0));
4023 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
4024 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
4025 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
4026 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4027 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4028 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
4029
4030 KeyboardInputMapper& mapper2 =
4031 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
4032 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
4033 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
4034 device2->reset(ARBITRARY_TIME);
4035
4036 // Initial metastate is AMETA_NUM_LOCK_ON, turn it off.
4037 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4038 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper1.getMetaState());
4039 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper2.getMetaState());
4040
4041 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
4042 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
4043 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4044 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4045 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4046
4047 // Toggle caps lock on and off.
4048 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
4049 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
4050 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4051 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper1.getMetaState());
4052 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper2.getMetaState());
4053
4054 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
4055 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
4056 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4057 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4058 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4059
4060 // Toggle scroll lock on and off.
4061 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
4062 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
4063 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
4064 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper1.getMetaState());
4065 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper2.getMetaState());
4066
4067 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
4068 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
4069 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
4070 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4071 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4072}
4073
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004074// --- KeyboardInputMapperTest_ExternalDevice ---
4075
4076class KeyboardInputMapperTest_ExternalDevice : public InputMapperTest {
4077protected:
Chris Yea52ade12020-08-27 16:49:20 -07004078 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004079};
4080
4081TEST_F(KeyboardInputMapperTest_ExternalDevice, WakeBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07004082 // For external devices, non-media keys will trigger wake on key down. Media keys need to be
4083 // marked as WAKE in the keylayout file to trigger wake.
Powei Fengd041c5d2019-05-03 17:11:33 -07004084
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004085 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, 0);
4086 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, 0);
4087 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAYPAUSE, 0, AKEYCODE_MEDIA_PLAY_PAUSE,
4088 POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07004089
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004090 KeyboardInputMapper& mapper =
4091 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4092 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07004093
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004094 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004095 NotifyKeyArgs args;
4096 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4097 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4098
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004099 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004100 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4101 ASSERT_EQ(uint32_t(0), args.policyFlags);
4102
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004103 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004104 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4105 ASSERT_EQ(uint32_t(0), args.policyFlags);
4106
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004107 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004108 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4109 ASSERT_EQ(uint32_t(0), args.policyFlags);
4110
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004111 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004112 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4113 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4114
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004115 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004116 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4117 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4118}
4119
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004120TEST_F(KeyboardInputMapperTest_ExternalDevice, DoNotWakeByDefaultBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07004121 // Tv Remote key's wake behavior is prescribed by the keylayout file.
Powei Fengd041c5d2019-05-03 17:11:33 -07004122
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004123 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4124 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
4125 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07004126
Powei Fengd041c5d2019-05-03 17:11:33 -07004127 addConfigurationProperty("keyboard.doNotWakeByDefault", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004128 KeyboardInputMapper& mapper =
4129 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4130 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07004131
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004132 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004133 NotifyKeyArgs args;
4134 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4135 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4136
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004137 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004138 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4139 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4140
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004141 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_DOWN, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004142 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4143 ASSERT_EQ(uint32_t(0), args.policyFlags);
4144
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004145 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_DOWN, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004146 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4147 ASSERT_EQ(uint32_t(0), args.policyFlags);
4148
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004149 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004150 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4151 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4152
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004153 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004154 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4155 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4156}
4157
Michael Wrightd02c5b62014-02-10 15:10:22 -08004158// --- CursorInputMapperTest ---
4159
4160class CursorInputMapperTest : public InputMapperTest {
4161protected:
4162 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD;
4163
Michael Wright17db18e2020-06-26 20:51:44 +01004164 std::shared_ptr<FakePointerController> mFakePointerController;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004165
Chris Yea52ade12020-08-27 16:49:20 -07004166 void SetUp() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004167 InputMapperTest::SetUp();
4168
Michael Wright17db18e2020-06-26 20:51:44 +01004169 mFakePointerController = std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00004170 mFakePolicy->setPointerController(mFakePointerController);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004171 }
4172
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004173 void testMotionRotation(CursorInputMapper& mapper, int32_t originalX, int32_t originalY,
4174 int32_t rotatedX, int32_t rotatedY);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004175
4176 void prepareDisplay(int32_t orientation) {
4177 const std::string uniqueId = "local:0";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004178 const ViewportType viewportType = ViewportType::INTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004179 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
4180 orientation, uniqueId, NO_PORT, viewportType);
4181 }
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004182
4183 static void assertCursorPointerCoords(const PointerCoords& coords, float x, float y,
4184 float pressure) {
4185 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(coords, x, y, pressure, 0.0f, 0.0f, 0.0f, 0.0f,
4186 0.0f, 0.0f, 0.0f, EPSILON));
4187 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08004188};
4189
4190const int32_t CursorInputMapperTest::TRACKBALL_MOVEMENT_THRESHOLD = 6;
4191
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004192void CursorInputMapperTest::testMotionRotation(CursorInputMapper& mapper, int32_t originalX,
4193 int32_t originalY, int32_t rotatedX,
4194 int32_t rotatedY) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004195 NotifyMotionArgs args;
4196
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004197 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, originalX);
4198 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, originalY);
4199 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004200 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4201 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004202 ASSERT_NO_FATAL_FAILURE(
4203 assertCursorPointerCoords(args.pointerCoords[0],
4204 float(rotatedX) / TRACKBALL_MOVEMENT_THRESHOLD,
4205 float(rotatedY) / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004206}
4207
4208TEST_F(CursorInputMapperTest, WhenModeIsPointer_GetSources_ReturnsMouse) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004209 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004210 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004211
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004212 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004213}
4214
4215TEST_F(CursorInputMapperTest, WhenModeIsNavigation_GetSources_ReturnsTrackball) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004216 addConfigurationProperty("cursor.mode", "navigation");
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_TRACKBALL, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004220}
4221
4222TEST_F(CursorInputMapperTest, WhenModeIsPointer_PopulateDeviceInfo_ReturnsRangeFromPointerController) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004223 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004224 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004225
4226 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004227 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004228
4229 // Initially there may not be a valid motion range.
Yi Kong9b14ac62018-07-17 13:48:38 -07004230 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE));
4231 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004232 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4233 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE, 0.0f, 1.0f, 0.0f, 0.0f));
4234
4235 // When the bounds are set, then there should be a valid motion range.
4236 mFakePointerController->setBounds(1, 2, 800 - 1, 480 - 1);
4237
4238 InputDeviceInfo info2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004239 mapper.populateDeviceInfo(&info2);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004240
4241 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
4242 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE,
4243 1, 800 - 1, 0.0f, 0.0f));
4244 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
4245 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE,
4246 2, 480 - 1, 0.0f, 0.0f));
4247 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
4248 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE,
4249 0.0f, 1.0f, 0.0f, 0.0f));
4250}
4251
4252TEST_F(CursorInputMapperTest, WhenModeIsNavigation_PopulateDeviceInfo_ReturnsScaledRange) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004253 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004254 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004255
4256 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004257 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004258
4259 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4260 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_TRACKBALL,
4261 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
4262 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4263 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_TRACKBALL,
4264 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
4265 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4266 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TRACKBALL,
4267 0.0f, 1.0f, 0.0f, 0.0f));
4268}
4269
4270TEST_F(CursorInputMapperTest, Process_ShouldSetAllFieldsAndIncludeGlobalMetaState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004271 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004272 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004273
arthurhungdcef2dc2020-08-11 14:47:50 +08004274 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004275
4276 NotifyMotionArgs args;
4277
4278 // Button press.
4279 // Mostly testing non x/y behavior here so we don't need to check again elsewhere.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004280 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4281 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004282 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4283 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4284 ASSERT_EQ(DEVICE_ID, args.deviceId);
4285 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4286 ASSERT_EQ(uint32_t(0), args.policyFlags);
4287 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
4288 ASSERT_EQ(0, args.flags);
4289 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4290 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
4291 ASSERT_EQ(0, args.edgeFlags);
4292 ASSERT_EQ(uint32_t(1), args.pointerCount);
4293 ASSERT_EQ(0, args.pointerProperties[0].id);
4294 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004295 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004296 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4297 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4298 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4299
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004300 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4301 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4302 ASSERT_EQ(DEVICE_ID, args.deviceId);
4303 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4304 ASSERT_EQ(uint32_t(0), args.policyFlags);
4305 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
4306 ASSERT_EQ(0, args.flags);
4307 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4308 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
4309 ASSERT_EQ(0, args.edgeFlags);
4310 ASSERT_EQ(uint32_t(1), args.pointerCount);
4311 ASSERT_EQ(0, args.pointerProperties[0].id);
4312 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004313 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004314 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4315 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4316 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4317
Michael Wrightd02c5b62014-02-10 15:10:22 -08004318 // Button release. Should have same down time.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004319 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4320 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004321 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4322 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
4323 ASSERT_EQ(DEVICE_ID, args.deviceId);
4324 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4325 ASSERT_EQ(uint32_t(0), args.policyFlags);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004326 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
4327 ASSERT_EQ(0, args.flags);
4328 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4329 ASSERT_EQ(0, args.buttonState);
4330 ASSERT_EQ(0, args.edgeFlags);
4331 ASSERT_EQ(uint32_t(1), args.pointerCount);
4332 ASSERT_EQ(0, args.pointerProperties[0].id);
4333 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004334 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004335 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4336 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4337 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4338
4339 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4340 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
4341 ASSERT_EQ(DEVICE_ID, args.deviceId);
4342 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4343 ASSERT_EQ(uint32_t(0), args.policyFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004344 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
4345 ASSERT_EQ(0, args.flags);
4346 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4347 ASSERT_EQ(0, args.buttonState);
4348 ASSERT_EQ(0, args.edgeFlags);
4349 ASSERT_EQ(uint32_t(1), args.pointerCount);
4350 ASSERT_EQ(0, args.pointerProperties[0].id);
4351 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004352 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004353 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4354 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4355 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4356}
4357
4358TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentXYUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004359 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004360 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004361
4362 NotifyMotionArgs args;
4363
4364 // Motion in X but not Y.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004365 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4366 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004367 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4368 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004369 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4370 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f,
4371 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004372
4373 // Motion in Y but not X.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004374 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
4375 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004376 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4377 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004378 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f,
4379 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004380}
4381
4382TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004383 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004384 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004385
4386 NotifyMotionArgs args;
4387
4388 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004389 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4390 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004391 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4392 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004393 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004394
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004395 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4396 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004397 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004398
Michael Wrightd02c5b62014-02-10 15:10:22 -08004399 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004400 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4401 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004402 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004403 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004404 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004405
4406 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004407 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004408 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004409}
4410
4411TEST_F(CursorInputMapperTest, Process_ShouldHandleCombinedXYAndButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004412 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004413 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004414
4415 NotifyMotionArgs args;
4416
4417 // Combined X, Y and Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004418 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4419 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
4420 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4421 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004422 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4423 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004424 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4425 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4426 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004427
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004428 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4429 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004430 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4431 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4432 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004433
Michael Wrightd02c5b62014-02-10 15:10:22 -08004434 // Move X, Y a bit while pressed.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004435 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 2);
4436 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 1);
4437 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004438 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4439 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004440 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4441 2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4442 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004443
4444 // Release Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004445 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4446 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004447 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004448 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004449 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004450
4451 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004452 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004453 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004454}
4455
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004456TEST_F(CursorInputMapperTest, Process_WhenOrientationAware_ShouldNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004457 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004458 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
4459 // need to be rotated.
4460 addConfigurationProperty("cursor.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004461 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004462
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004463 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004464 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4465 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4466 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4467 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4468 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4469 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4470 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4471 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4472}
4473
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004474TEST_F(CursorInputMapperTest, Process_WhenNotOrientationAware_ShouldRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004475 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004476 // Since InputReader works in the un-rotated coordinate space, only devices that are not
4477 // orientation-aware are affected by display rotation.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004478 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004479
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004480 prepareDisplay(DISPLAY_ORIENTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004481 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4482 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4483 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4484 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4485 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4486 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4487 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4488 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4489
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004490 prepareDisplay(DISPLAY_ORIENTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004491 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, -1, 0));
4492 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, 1));
4493 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, 1));
4494 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, 1));
4495 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 1, 0));
4496 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, -1));
4497 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, -1));
4498 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, -1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004499
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004500 prepareDisplay(DISPLAY_ORIENTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004501 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, -1));
4502 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, -1));
4503 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, -1, 0));
4504 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, 1));
4505 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, 1));
4506 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, 1));
4507 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 1, 0));
4508 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, -1));
4509
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004510 prepareDisplay(DISPLAY_ORIENTATION_270);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004511 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 1, 0));
4512 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, -1));
4513 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, -1));
4514 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, -1));
4515 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, -1, 0));
4516 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, 1));
4517 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, 1));
4518 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, 1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004519}
4520
4521TEST_F(CursorInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004522 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004523 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004524
4525 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4526 mFakePointerController->setPosition(100, 200);
4527 mFakePointerController->setButtonState(0);
4528
4529 NotifyMotionArgs motionArgs;
4530 NotifyKeyArgs keyArgs;
4531
4532 // press BTN_LEFT, release BTN_LEFT
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004533 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 1);
4534 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004535 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4536 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4537 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4538 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004539 ASSERT_NO_FATAL_FAILURE(
4540 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004541
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004542 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4543 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, 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));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004548
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004549 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 0);
4550 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004551 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004552 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004553 ASSERT_EQ(0, motionArgs.buttonState);
4554 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004555 ASSERT_NO_FATAL_FAILURE(
4556 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004557
4558 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004559 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, 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));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004564
4565 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004566 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004567 ASSERT_EQ(0, motionArgs.buttonState);
4568 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004569 ASSERT_NO_FATAL_FAILURE(
4570 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004571
4572 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004573 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 1);
4574 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 1);
4575 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004576 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4577 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4578 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4579 motionArgs.buttonState);
4580 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4581 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004582 ASSERT_NO_FATAL_FAILURE(
4583 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004584
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004585 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4586 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4587 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4588 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4589 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004590 ASSERT_NO_FATAL_FAILURE(
4591 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004592
4593 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4594 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4595 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4596 motionArgs.buttonState);
4597 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4598 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004599 ASSERT_NO_FATAL_FAILURE(
4600 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004601
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004602 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 0);
4603 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004604 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004605 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004606 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4607 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004608 ASSERT_NO_FATAL_FAILURE(
4609 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004610
4611 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004612 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -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));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004617
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004618 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4619 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004620 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004621 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
4622 ASSERT_EQ(0, motionArgs.buttonState);
4623 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004624 ASSERT_NO_FATAL_FAILURE(
4625 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004626 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4627 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004628
4629 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004630 ASSERT_EQ(0, motionArgs.buttonState);
4631 ASSERT_EQ(0, mFakePointerController->getButtonState());
4632 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004633 ASSERT_NO_FATAL_FAILURE(
4634 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004635
Michael Wrightd02c5b62014-02-10 15:10:22 -08004636 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4637 ASSERT_EQ(0, motionArgs.buttonState);
4638 ASSERT_EQ(0, mFakePointerController->getButtonState());
4639 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, 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));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004642
4643 // press BTN_BACK, release BTN_BACK
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004644 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 1);
4645 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004646 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4647 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4648 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004649
Michael Wrightd02c5b62014-02-10 15:10:22 -08004650 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004651 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004652 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4653 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004654 ASSERT_NO_FATAL_FAILURE(
4655 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004656
4657 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4658 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4659 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));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004663
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004664 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 0);
4665 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004666 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004667 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004668 ASSERT_EQ(0, motionArgs.buttonState);
4669 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004670 ASSERT_NO_FATAL_FAILURE(
4671 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004672
4673 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004674 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004675 ASSERT_EQ(0, motionArgs.buttonState);
4676 ASSERT_EQ(0, mFakePointerController->getButtonState());
4677
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004678 ASSERT_NO_FATAL_FAILURE(
4679 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004680 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4681 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4682 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4683
4684 // press BTN_SIDE, release BTN_SIDE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004685 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 1);
4686 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004687 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4688 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4689 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004690
Michael Wrightd02c5b62014-02-10 15:10:22 -08004691 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004692 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004693 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4694 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004695 ASSERT_NO_FATAL_FAILURE(
4696 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004697
4698 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4699 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4700 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));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004704
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004705 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 0);
4706 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004707 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004708 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004709 ASSERT_EQ(0, motionArgs.buttonState);
4710 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004711 ASSERT_NO_FATAL_FAILURE(
4712 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004713
4714 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4715 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4716 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
Michael Wrightd02c5b62014-02-10 15:10:22 -08004721 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4722 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4723 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4724
4725 // press BTN_FORWARD, release BTN_FORWARD
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004726 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 1);
4727 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004728 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4729 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4730 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004731
Michael Wrightd02c5b62014-02-10 15:10:22 -08004732 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004733 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004734 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4735 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004736 ASSERT_NO_FATAL_FAILURE(
4737 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004738
4739 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4740 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4741 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));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004745
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004746 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 0);
4747 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004748 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004749 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004750 ASSERT_EQ(0, motionArgs.buttonState);
4751 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004752 ASSERT_NO_FATAL_FAILURE(
4753 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004754
4755 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4756 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4757 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
Michael Wrightd02c5b62014-02-10 15:10:22 -08004762 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4763 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4764 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4765
4766 // press BTN_EXTRA, release BTN_EXTRA
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004767 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 1);
4768 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004769 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4770 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4771 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004772
Michael Wrightd02c5b62014-02-10 15:10:22 -08004773 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004774 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004775 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4776 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004777 ASSERT_NO_FATAL_FAILURE(
4778 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004779
4780 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4781 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4782 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));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004786
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004787 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 0);
4788 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004789 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004790 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004791 ASSERT_EQ(0, motionArgs.buttonState);
4792 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004793 ASSERT_NO_FATAL_FAILURE(
4794 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004795
4796 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4797 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4798 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
Michael Wrightd02c5b62014-02-10 15:10:22 -08004803 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4804 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4805 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4806}
4807
4808TEST_F(CursorInputMapperTest, Process_WhenModeIsPointer_ShouldMoveThePointerAround) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004809 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004810 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004811
4812 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4813 mFakePointerController->setPosition(100, 200);
4814 mFakePointerController->setButtonState(0);
4815
4816 NotifyMotionArgs args;
4817
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004818 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4819 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4820 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004821 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004822 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4823 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4824 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4825 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 +01004826 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004827}
4828
4829TEST_F(CursorInputMapperTest, Process_PointerCapture) {
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004830 addConfigurationProperty("cursor.mode", "pointer");
4831 mFakePolicy->setPointerCapture(true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004832 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004833
4834 NotifyDeviceResetArgs resetArgs;
4835 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4836 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4837 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4838
4839 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4840 mFakePointerController->setPosition(100, 200);
4841 mFakePointerController->setButtonState(0);
4842
4843 NotifyMotionArgs args;
4844
4845 // Move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004846 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4847 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4848 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004849 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4850 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4851 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4852 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4853 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 +01004854 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004855
4856 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004857 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4858 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004859 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4860 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4861 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
4862 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4863 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4864 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4865 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4866 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
4867 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4868 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4869
4870 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004871 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4872 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004873 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4874 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4875 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
4876 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4877 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4878 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4879 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4880 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
4881 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4882 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4883
4884 // Another move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004885 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 30);
4886 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 40);
4887 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004888 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4889 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4890 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4891 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4892 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 +01004893 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004894
4895 // Disable pointer capture and check that the device generation got bumped
4896 // and events are generated the usual way.
arthurhungdcef2dc2020-08-11 14:47:50 +08004897 const uint32_t generation = mReader->getContext()->getGeneration();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004898 mFakePolicy->setPointerCapture(false);
4899 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
arthurhungdcef2dc2020-08-11 14:47:50 +08004900 ASSERT_TRUE(mReader->getContext()->getGeneration() != generation);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004901
4902 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4903 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4904 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4905
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004906 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4907 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4908 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004909 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4910 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004911 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4912 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4913 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 +01004914 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004915}
4916
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004917TEST_F(CursorInputMapperTest, Process_ShouldHandleDisplayId) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004918 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004919
Garfield Tan888a6a42020-01-09 11:39:16 -08004920 // Setup for second display.
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004921 constexpr int32_t SECOND_DISPLAY_ID = 1;
Garfield Tan888a6a42020-01-09 11:39:16 -08004922 const std::string SECOND_DISPLAY_UNIQUE_ID = "local:1";
4923 mFakePolicy->addDisplayViewport(SECOND_DISPLAY_ID, 800, 480, DISPLAY_ORIENTATION_0,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00004924 true /*isActive*/, SECOND_DISPLAY_UNIQUE_ID, NO_PORT,
4925 ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08004926 mFakePolicy->setDefaultPointerDisplayId(SECOND_DISPLAY_ID);
4927 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
4928
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004929 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4930 mFakePointerController->setPosition(100, 200);
4931 mFakePointerController->setButtonState(0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004932
4933 NotifyMotionArgs args;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004934 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4935 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4936 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004937 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4938 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4939 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4940 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4941 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 +01004942 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004943 ASSERT_EQ(SECOND_DISPLAY_ID, args.displayId);
4944}
4945
Michael Wrightd02c5b62014-02-10 15:10:22 -08004946// --- TouchInputMapperTest ---
4947
4948class TouchInputMapperTest : public InputMapperTest {
4949protected:
4950 static const int32_t RAW_X_MIN;
4951 static const int32_t RAW_X_MAX;
4952 static const int32_t RAW_Y_MIN;
4953 static const int32_t RAW_Y_MAX;
4954 static const int32_t RAW_TOUCH_MIN;
4955 static const int32_t RAW_TOUCH_MAX;
4956 static const int32_t RAW_TOOL_MIN;
4957 static const int32_t RAW_TOOL_MAX;
4958 static const int32_t RAW_PRESSURE_MIN;
4959 static const int32_t RAW_PRESSURE_MAX;
4960 static const int32_t RAW_ORIENTATION_MIN;
4961 static const int32_t RAW_ORIENTATION_MAX;
4962 static const int32_t RAW_DISTANCE_MIN;
4963 static const int32_t RAW_DISTANCE_MAX;
4964 static const int32_t RAW_TILT_MIN;
4965 static const int32_t RAW_TILT_MAX;
4966 static const int32_t RAW_ID_MIN;
4967 static const int32_t RAW_ID_MAX;
4968 static const int32_t RAW_SLOT_MIN;
4969 static const int32_t RAW_SLOT_MAX;
4970 static const float X_PRECISION;
4971 static const float Y_PRECISION;
Santos Cordonfa5cf462017-04-05 10:37:00 -07004972 static const float X_PRECISION_VIRTUAL;
4973 static const float Y_PRECISION_VIRTUAL;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004974
4975 static const float GEOMETRIC_SCALE;
Jason Gerecke489fda82012-09-07 17:19:40 -07004976 static const TouchAffineTransformation AFFINE_TRANSFORM;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004977
4978 static const VirtualKeyDefinition VIRTUAL_KEYS[2];
4979
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004980 const std::string UNIQUE_ID = "local:0";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004981 const std::string SECONDARY_UNIQUE_ID = "local:1";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004982
Michael Wrightd02c5b62014-02-10 15:10:22 -08004983 enum Axes {
4984 POSITION = 1 << 0,
4985 TOUCH = 1 << 1,
4986 TOOL = 1 << 2,
4987 PRESSURE = 1 << 3,
4988 ORIENTATION = 1 << 4,
4989 MINOR = 1 << 5,
4990 ID = 1 << 6,
4991 DISTANCE = 1 << 7,
4992 TILT = 1 << 8,
4993 SLOT = 1 << 9,
4994 TOOL_TYPE = 1 << 10,
4995 };
4996
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004997 void prepareDisplay(int32_t orientation, std::optional<uint8_t> port = NO_PORT);
4998 void prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port = NO_PORT);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004999 void prepareVirtualDisplay(int32_t orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005000 void prepareVirtualKeys();
Jason Gerecke489fda82012-09-07 17:19:40 -07005001 void prepareLocationCalibration();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005002 int32_t toRawX(float displayX);
5003 int32_t toRawY(float displayY);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005004 int32_t toRotatedRawX(float displayX);
5005 int32_t toRotatedRawY(float displayY);
Jason Gerecke489fda82012-09-07 17:19:40 -07005006 float toCookedX(float rawX, float rawY);
5007 float toCookedY(float rawX, float rawY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005008 float toDisplayX(int32_t rawX);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005009 float toDisplayX(int32_t rawX, int32_t displayWidth);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005010 float toDisplayY(int32_t rawY);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005011 float toDisplayY(int32_t rawY, int32_t displayHeight);
5012
Michael Wrightd02c5b62014-02-10 15:10:22 -08005013};
5014
5015const int32_t TouchInputMapperTest::RAW_X_MIN = 25;
5016const int32_t TouchInputMapperTest::RAW_X_MAX = 1019;
5017const int32_t TouchInputMapperTest::RAW_Y_MIN = 30;
5018const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009;
5019const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0;
5020const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31;
5021const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0;
5022const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15;
Michael Wrightaa449c92017-12-13 21:21:43 +00005023const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = 0;
5024const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = 255;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005025const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7;
5026const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
5027const int32_t TouchInputMapperTest::RAW_DISTANCE_MIN = 0;
5028const int32_t TouchInputMapperTest::RAW_DISTANCE_MAX = 7;
5029const int32_t TouchInputMapperTest::RAW_TILT_MIN = 0;
5030const int32_t TouchInputMapperTest::RAW_TILT_MAX = 150;
5031const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
5032const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
5033const int32_t TouchInputMapperTest::RAW_SLOT_MIN = 0;
5034const int32_t TouchInputMapperTest::RAW_SLOT_MAX = 9;
5035const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH;
5036const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT;
Santos Cordonfa5cf462017-04-05 10:37:00 -07005037const float TouchInputMapperTest::X_PRECISION_VIRTUAL =
5038 float(RAW_X_MAX - RAW_X_MIN + 1) / VIRTUAL_DISPLAY_WIDTH;
5039const float TouchInputMapperTest::Y_PRECISION_VIRTUAL =
5040 float(RAW_Y_MAX - RAW_Y_MIN + 1) / VIRTUAL_DISPLAY_HEIGHT;
Jason Gerecke489fda82012-09-07 17:19:40 -07005041const TouchAffineTransformation TouchInputMapperTest::AFFINE_TRANSFORM =
5042 TouchAffineTransformation(1, -2, 3, -4, 5, -6);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005043
5044const float TouchInputMapperTest::GEOMETRIC_SCALE =
5045 avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1),
5046 float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1));
5047
5048const VirtualKeyDefinition TouchInputMapperTest::VIRTUAL_KEYS[2] = {
5049 { KEY_HOME, 60, DISPLAY_HEIGHT + 15, 20, 20 },
5050 { KEY_MENU, DISPLAY_HEIGHT - 60, DISPLAY_WIDTH + 15, 20, 20 },
5051};
5052
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005053void TouchInputMapperTest::prepareDisplay(int32_t orientation, std::optional<uint8_t> port) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01005054 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
5055 port, ViewportType::INTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005056}
5057
5058void TouchInputMapperTest::prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port) {
5059 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
5060 DISPLAY_ORIENTATION_0, SECONDARY_UNIQUE_ID, port, type);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005061}
5062
Santos Cordonfa5cf462017-04-05 10:37:00 -07005063void TouchInputMapperTest::prepareVirtualDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01005064 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH, VIRTUAL_DISPLAY_HEIGHT,
5065 orientation, VIRTUAL_DISPLAY_UNIQUE_ID, NO_PORT,
5066 ViewportType::VIRTUAL);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005067}
5068
Michael Wrightd02c5b62014-02-10 15:10:22 -08005069void TouchInputMapperTest::prepareVirtualKeys() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005070 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[0]);
5071 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[1]);
5072 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
5073 mFakeEventHub->addKey(EVENTHUB_ID, KEY_MENU, 0, AKEYCODE_MENU, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005074}
5075
Jason Gerecke489fda82012-09-07 17:19:40 -07005076void TouchInputMapperTest::prepareLocationCalibration() {
5077 mFakePolicy->setTouchAffineTransformation(AFFINE_TRANSFORM);
5078}
5079
Michael Wrightd02c5b62014-02-10 15:10:22 -08005080int32_t TouchInputMapperTest::toRawX(float displayX) {
5081 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN);
5082}
5083
5084int32_t TouchInputMapperTest::toRawY(float displayY) {
5085 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN);
5086}
5087
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005088int32_t TouchInputMapperTest::toRotatedRawX(float displayX) {
5089 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_HEIGHT + RAW_X_MIN);
5090}
5091
5092int32_t TouchInputMapperTest::toRotatedRawY(float displayY) {
5093 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_WIDTH + RAW_Y_MIN);
5094}
5095
Jason Gerecke489fda82012-09-07 17:19:40 -07005096float TouchInputMapperTest::toCookedX(float rawX, float rawY) {
5097 AFFINE_TRANSFORM.applyTo(rawX, rawY);
5098 return rawX;
5099}
5100
5101float TouchInputMapperTest::toCookedY(float rawX, float rawY) {
5102 AFFINE_TRANSFORM.applyTo(rawX, rawY);
5103 return rawY;
5104}
5105
Michael Wrightd02c5b62014-02-10 15:10:22 -08005106float TouchInputMapperTest::toDisplayX(int32_t rawX) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005107 return toDisplayX(rawX, DISPLAY_WIDTH);
5108}
5109
5110float TouchInputMapperTest::toDisplayX(int32_t rawX, int32_t displayWidth) {
5111 return float(rawX - RAW_X_MIN) * displayWidth / (RAW_X_MAX - RAW_X_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005112}
5113
5114float TouchInputMapperTest::toDisplayY(int32_t rawY) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005115 return toDisplayY(rawY, DISPLAY_HEIGHT);
5116}
5117
5118float TouchInputMapperTest::toDisplayY(int32_t rawY, int32_t displayHeight) {
5119 return float(rawY - RAW_Y_MIN) * displayHeight / (RAW_Y_MAX - RAW_Y_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005120}
5121
5122
5123// --- SingleTouchInputMapperTest ---
5124
5125class SingleTouchInputMapperTest : public TouchInputMapperTest {
5126protected:
5127 void prepareButtons();
5128 void prepareAxes(int axes);
5129
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005130 void processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
5131 void processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
5132 void processUp(SingleTouchInputMapper& mappery);
5133 void processPressure(SingleTouchInputMapper& mapper, int32_t pressure);
5134 void processToolMajor(SingleTouchInputMapper& mapper, int32_t toolMajor);
5135 void processDistance(SingleTouchInputMapper& mapper, int32_t distance);
5136 void processTilt(SingleTouchInputMapper& mapper, int32_t tiltX, int32_t tiltY);
5137 void processKey(SingleTouchInputMapper& mapper, int32_t code, int32_t value);
5138 void processSync(SingleTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005139};
5140
5141void SingleTouchInputMapperTest::prepareButtons() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005142 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005143}
5144
5145void SingleTouchInputMapperTest::prepareAxes(int axes) {
5146 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005147 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
5148 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005149 }
5150 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005151 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MIN,
5152 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005153 }
5154 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005155 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TOOL_WIDTH, RAW_TOOL_MIN, RAW_TOOL_MAX, 0,
5156 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005157 }
5158 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005159 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_DISTANCE, RAW_DISTANCE_MIN,
5160 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005161 }
5162 if (axes & TILT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005163 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_X, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
5164 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_Y, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005165 }
5166}
5167
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005168void SingleTouchInputMapperTest::processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005169 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
5170 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
5171 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005172}
5173
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005174void SingleTouchInputMapperTest::processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005175 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
5176 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005177}
5178
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005179void SingleTouchInputMapperTest::processUp(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005180 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005181}
5182
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005183void SingleTouchInputMapperTest::processPressure(SingleTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005184 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005185}
5186
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005187void SingleTouchInputMapperTest::processToolMajor(SingleTouchInputMapper& mapper,
5188 int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005189 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TOOL_WIDTH, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005190}
5191
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005192void SingleTouchInputMapperTest::processDistance(SingleTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005193 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005194}
5195
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005196void SingleTouchInputMapperTest::processTilt(SingleTouchInputMapper& mapper, int32_t tiltX,
5197 int32_t tiltY) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005198 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_X, tiltX);
5199 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_Y, tiltY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005200}
5201
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005202void SingleTouchInputMapperTest::processKey(SingleTouchInputMapper& mapper, int32_t code,
5203 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005204 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005205}
5206
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005207void SingleTouchInputMapperTest::processSync(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005208 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005209}
5210
Michael Wrightd02c5b62014-02-10 15:10:22 -08005211TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005212 prepareButtons();
5213 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005214 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005215
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005216 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005217}
5218
5219TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndIsACursor_ReturnsTouchPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005220 mFakeEventHub->addRelativeAxis(EVENTHUB_ID, REL_X);
5221 mFakeEventHub->addRelativeAxis(EVENTHUB_ID, REL_Y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005222 prepareButtons();
5223 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005224 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005225
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005226 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005227}
5228
5229TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchPad_ReturnsTouchPad) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005230 prepareButtons();
5231 prepareAxes(POSITION);
5232 addConfigurationProperty("touch.deviceType", "touchPad");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005233 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005234
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005235 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005236}
5237
5238TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005239 prepareButtons();
5240 prepareAxes(POSITION);
5241 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005242 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005243
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005244 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005245}
5246
5247TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005248 addConfigurationProperty("touch.deviceType", "touchScreen");
5249 prepareDisplay(DISPLAY_ORIENTATION_0);
5250 prepareButtons();
5251 prepareAxes(POSITION);
5252 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005253 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005254
5255 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005256 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005257
5258 // Virtual key is down.
5259 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5260 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5261 processDown(mapper, x, y);
5262 processSync(mapper);
5263 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5264
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005265 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005266
5267 // Virtual key is up.
5268 processUp(mapper);
5269 processSync(mapper);
5270 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5271
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005272 ASSERT_EQ(AKEY_STATE_UP, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005273}
5274
5275TEST_F(SingleTouchInputMapperTest, GetScanCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005276 addConfigurationProperty("touch.deviceType", "touchScreen");
5277 prepareDisplay(DISPLAY_ORIENTATION_0);
5278 prepareButtons();
5279 prepareAxes(POSITION);
5280 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005281 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005282
5283 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005284 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005285
5286 // Virtual key is down.
5287 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5288 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5289 processDown(mapper, x, y);
5290 processSync(mapper);
5291 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5292
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005293 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005294
5295 // Virtual key is up.
5296 processUp(mapper);
5297 processSync(mapper);
5298 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5299
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005300 ASSERT_EQ(AKEY_STATE_UP, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005301}
5302
5303TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005304 addConfigurationProperty("touch.deviceType", "touchScreen");
5305 prepareDisplay(DISPLAY_ORIENTATION_0);
5306 prepareButtons();
5307 prepareAxes(POSITION);
5308 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005309 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005310
5311 const int32_t keys[2] = { AKEYCODE_HOME, AKEYCODE_A };
5312 uint8_t flags[2] = { 0, 0 };
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005313 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, 2, keys, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005314 ASSERT_TRUE(flags[0]);
5315 ASSERT_FALSE(flags[1]);
5316}
5317
5318TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005319 addConfigurationProperty("touch.deviceType", "touchScreen");
5320 prepareDisplay(DISPLAY_ORIENTATION_0);
5321 prepareButtons();
5322 prepareAxes(POSITION);
5323 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005324 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005325
arthurhungdcef2dc2020-08-11 14:47:50 +08005326 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005327
5328 NotifyKeyArgs args;
5329
5330 // Press virtual key.
5331 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5332 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5333 processDown(mapper, x, y);
5334 processSync(mapper);
5335
5336 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
5337 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5338 ASSERT_EQ(DEVICE_ID, args.deviceId);
5339 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
5340 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
5341 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
5342 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5343 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5344 ASSERT_EQ(KEY_HOME, args.scanCode);
5345 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5346 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5347
5348 // Release virtual key.
5349 processUp(mapper);
5350 processSync(mapper);
5351
5352 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
5353 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5354 ASSERT_EQ(DEVICE_ID, args.deviceId);
5355 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
5356 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
5357 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
5358 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5359 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5360 ASSERT_EQ(KEY_HOME, args.scanCode);
5361 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5362 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5363
5364 // Should not have sent any motions.
5365 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5366}
5367
5368TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005369 addConfigurationProperty("touch.deviceType", "touchScreen");
5370 prepareDisplay(DISPLAY_ORIENTATION_0);
5371 prepareButtons();
5372 prepareAxes(POSITION);
5373 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005374 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005375
arthurhungdcef2dc2020-08-11 14:47:50 +08005376 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005377
5378 NotifyKeyArgs keyArgs;
5379
5380 // Press virtual key.
5381 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5382 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5383 processDown(mapper, x, y);
5384 processSync(mapper);
5385
5386 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5387 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5388 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5389 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5390 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5391 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5392 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags);
5393 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5394 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5395 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5396 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5397
5398 // Move out of bounds. This should generate a cancel and a pointer down since we moved
5399 // into the display area.
5400 y -= 100;
5401 processMove(mapper, x, y);
5402 processSync(mapper);
5403
5404 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5405 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5406 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5407 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5408 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5409 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5410 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
5411 | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags);
5412 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5413 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5414 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5415 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5416
5417 NotifyMotionArgs motionArgs;
5418 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5419 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5420 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5421 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5422 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5423 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5424 ASSERT_EQ(0, motionArgs.flags);
5425 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5426 ASSERT_EQ(0, motionArgs.buttonState);
5427 ASSERT_EQ(0, motionArgs.edgeFlags);
5428 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5429 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5430 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5431 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5432 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5433 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5434 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5435 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5436
5437 // Keep moving out of bounds. Should generate a pointer move.
5438 y -= 50;
5439 processMove(mapper, x, y);
5440 processSync(mapper);
5441
5442 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5443 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5444 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5445 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5446 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5447 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5448 ASSERT_EQ(0, motionArgs.flags);
5449 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5450 ASSERT_EQ(0, motionArgs.buttonState);
5451 ASSERT_EQ(0, motionArgs.edgeFlags);
5452 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5453 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5454 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5455 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5456 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5457 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5458 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5459 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5460
5461 // Release out of bounds. Should generate a pointer up.
5462 processUp(mapper);
5463 processSync(mapper);
5464
5465 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5466 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5467 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5468 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5469 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5470 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5471 ASSERT_EQ(0, motionArgs.flags);
5472 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5473 ASSERT_EQ(0, motionArgs.buttonState);
5474 ASSERT_EQ(0, motionArgs.edgeFlags);
5475 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5476 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5477 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5478 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5479 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5480 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5481 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5482 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5483
5484 // Should not have sent any more keys or motions.
5485 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5486 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5487}
5488
5489TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005490 addConfigurationProperty("touch.deviceType", "touchScreen");
5491 prepareDisplay(DISPLAY_ORIENTATION_0);
5492 prepareButtons();
5493 prepareAxes(POSITION);
5494 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005495 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005496
arthurhungdcef2dc2020-08-11 14:47:50 +08005497 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005498
5499 NotifyMotionArgs motionArgs;
5500
5501 // Initially go down out of bounds.
5502 int32_t x = -10;
5503 int32_t y = -10;
5504 processDown(mapper, x, y);
5505 processSync(mapper);
5506
5507 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5508
5509 // Move into the display area. Should generate a pointer down.
5510 x = 50;
5511 y = 75;
5512 processMove(mapper, x, y);
5513 processSync(mapper);
5514
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 // Release. Should generate a pointer up.
5535 processUp(mapper);
5536 processSync(mapper);
5537
5538 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5539 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5540 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5541 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5542 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5543 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5544 ASSERT_EQ(0, motionArgs.flags);
5545 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5546 ASSERT_EQ(0, motionArgs.buttonState);
5547 ASSERT_EQ(0, motionArgs.edgeFlags);
5548 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5549 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5550 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5551 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5552 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5553 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5554 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5555 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5556
5557 // Should not have sent any more keys or motions.
5558 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5559 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5560}
5561
Santos Cordonfa5cf462017-04-05 10:37:00 -07005562TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture_VirtualDisplay) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005563 addConfigurationProperty("touch.deviceType", "touchScreen");
5564 addConfigurationProperty("touch.displayId", VIRTUAL_DISPLAY_UNIQUE_ID);
5565
5566 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
5567 prepareButtons();
5568 prepareAxes(POSITION);
5569 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005570 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Santos Cordonfa5cf462017-04-05 10:37:00 -07005571
arthurhungdcef2dc2020-08-11 14:47:50 +08005572 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005573
5574 NotifyMotionArgs motionArgs;
5575
5576 // Down.
5577 int32_t x = 100;
5578 int32_t y = 125;
5579 processDown(mapper, x, y);
5580 processSync(mapper);
5581
5582 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5583 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5584 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5585 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5586 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5587 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5588 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5589 ASSERT_EQ(0, motionArgs.flags);
5590 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5591 ASSERT_EQ(0, motionArgs.buttonState);
5592 ASSERT_EQ(0, motionArgs.edgeFlags);
5593 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5594 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5595 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5596 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5597 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5598 1, 0, 0, 0, 0, 0, 0, 0));
5599 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5600 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5601 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5602
5603 // Move.
5604 x += 50;
5605 y += 75;
5606 processMove(mapper, x, y);
5607 processSync(mapper);
5608
5609 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5610 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5611 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5612 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5613 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5614 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5615 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5616 ASSERT_EQ(0, motionArgs.flags);
5617 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5618 ASSERT_EQ(0, motionArgs.buttonState);
5619 ASSERT_EQ(0, motionArgs.edgeFlags);
5620 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5621 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5622 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5623 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5624 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5625 1, 0, 0, 0, 0, 0, 0, 0));
5626 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5627 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5628 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5629
5630 // Up.
5631 processUp(mapper);
5632 processSync(mapper);
5633
5634 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5635 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5636 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5637 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
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, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5650 1, 0, 0, 0, 0, 0, 0, 0));
5651 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5652 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5653 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5654
5655 // Should not have sent any more keys or motions.
5656 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5657 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5658}
5659
Michael Wrightd02c5b62014-02-10 15:10:22 -08005660TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005661 addConfigurationProperty("touch.deviceType", "touchScreen");
5662 prepareDisplay(DISPLAY_ORIENTATION_0);
5663 prepareButtons();
5664 prepareAxes(POSITION);
5665 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005666 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005667
arthurhungdcef2dc2020-08-11 14:47:50 +08005668 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005669
5670 NotifyMotionArgs motionArgs;
5671
5672 // Down.
5673 int32_t x = 100;
5674 int32_t y = 125;
5675 processDown(mapper, x, y);
5676 processSync(mapper);
5677
5678 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5679 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5680 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5681 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5682 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5683 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5684 ASSERT_EQ(0, motionArgs.flags);
5685 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5686 ASSERT_EQ(0, motionArgs.buttonState);
5687 ASSERT_EQ(0, motionArgs.edgeFlags);
5688 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5689 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5690 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5691 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5692 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5693 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5694 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5695 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5696
5697 // Move.
5698 x += 50;
5699 y += 75;
5700 processMove(mapper, x, y);
5701 processSync(mapper);
5702
5703 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5704 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5705 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5706 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5707 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5708 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5709 ASSERT_EQ(0, motionArgs.flags);
5710 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5711 ASSERT_EQ(0, motionArgs.buttonState);
5712 ASSERT_EQ(0, motionArgs.edgeFlags);
5713 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5714 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5715 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5716 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5717 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5718 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5719 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5720 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5721
5722 // Up.
5723 processUp(mapper);
5724 processSync(mapper);
5725
5726 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5727 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5728 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5729 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5730 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5731 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5732 ASSERT_EQ(0, motionArgs.flags);
5733 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5734 ASSERT_EQ(0, motionArgs.buttonState);
5735 ASSERT_EQ(0, motionArgs.edgeFlags);
5736 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5737 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5738 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5739 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5740 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5741 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5742 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5743 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5744
5745 // Should not have sent any more keys or motions.
5746 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5747 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5748}
5749
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005750TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_DoesNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005751 addConfigurationProperty("touch.deviceType", "touchScreen");
5752 prepareButtons();
5753 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005754 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
5755 // need to be rotated. Touchscreens are orientation-aware by default.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005756 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005757
5758 NotifyMotionArgs args;
5759
5760 // Rotation 90.
5761 prepareDisplay(DISPLAY_ORIENTATION_90);
5762 processDown(mapper, toRawX(50), toRawY(75));
5763 processSync(mapper);
5764
5765 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5766 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5767 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5768
5769 processUp(mapper);
5770 processSync(mapper);
5771 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5772}
5773
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005774TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_RotatesMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005775 addConfigurationProperty("touch.deviceType", "touchScreen");
5776 prepareButtons();
5777 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005778 // Since InputReader works in the un-rotated coordinate space, only devices that are not
5779 // orientation-aware are affected by display rotation.
5780 addConfigurationProperty("touch.orientationAware", "0");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005781 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005782
5783 NotifyMotionArgs args;
5784
5785 // Rotation 0.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005786 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005787 prepareDisplay(DISPLAY_ORIENTATION_0);
5788 processDown(mapper, toRawX(50), toRawY(75));
5789 processSync(mapper);
5790
5791 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5792 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5793 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5794
5795 processUp(mapper);
5796 processSync(mapper);
5797 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5798
5799 // Rotation 90.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005800 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005801 prepareDisplay(DISPLAY_ORIENTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005802 processDown(mapper, toRawX(75), RAW_Y_MAX - toRawY(50) + RAW_Y_MIN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005803 processSync(mapper);
5804
5805 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5806 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5807 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5808
5809 processUp(mapper);
5810 processSync(mapper);
5811 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5812
5813 // Rotation 180.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005814 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005815 prepareDisplay(DISPLAY_ORIENTATION_180);
5816 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
5817 processSync(mapper);
5818
5819 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5820 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5821 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5822
5823 processUp(mapper);
5824 processSync(mapper);
5825 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5826
5827 // Rotation 270.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005828 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005829 prepareDisplay(DISPLAY_ORIENTATION_270);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005830 processDown(mapper, RAW_X_MAX - toRawX(75) + RAW_X_MIN, toRawY(50));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005831 processSync(mapper);
5832
5833 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5834 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5835 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5836
5837 processUp(mapper);
5838 processSync(mapper);
5839 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5840}
5841
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005842TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation0_RotatesMotions) {
5843 addConfigurationProperty("touch.deviceType", "touchScreen");
5844 prepareButtons();
5845 prepareAxes(POSITION);
5846 addConfigurationProperty("touch.orientationAware", "1");
5847 addConfigurationProperty("touch.orientation", "ORIENTATION_0");
5848 clearViewports();
5849 prepareDisplay(DISPLAY_ORIENTATION_0);
5850 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5851 NotifyMotionArgs args;
5852
5853 // Orientation 0.
5854 processDown(mapper, toRawX(50), toRawY(75));
5855 processSync(mapper);
5856
5857 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5858 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5859 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5860
5861 processUp(mapper);
5862 processSync(mapper);
5863 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5864}
5865
5866TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation90_RotatesMotions) {
5867 addConfigurationProperty("touch.deviceType", "touchScreen");
5868 prepareButtons();
5869 prepareAxes(POSITION);
5870 addConfigurationProperty("touch.orientationAware", "1");
5871 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
5872 clearViewports();
5873 prepareDisplay(DISPLAY_ORIENTATION_0);
5874 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5875 NotifyMotionArgs args;
5876
5877 // Orientation 90.
5878 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
5879 processSync(mapper);
5880
5881 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5882 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5883 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5884
5885 processUp(mapper);
5886 processSync(mapper);
5887 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5888}
5889
5890TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation180_RotatesMotions) {
5891 addConfigurationProperty("touch.deviceType", "touchScreen");
5892 prepareButtons();
5893 prepareAxes(POSITION);
5894 addConfigurationProperty("touch.orientationAware", "1");
5895 addConfigurationProperty("touch.orientation", "ORIENTATION_180");
5896 clearViewports();
5897 prepareDisplay(DISPLAY_ORIENTATION_0);
5898 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5899 NotifyMotionArgs args;
5900
5901 // Orientation 180.
5902 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
5903 processSync(mapper);
5904
5905 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5906 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5907 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5908
5909 processUp(mapper);
5910 processSync(mapper);
5911 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5912}
5913
5914TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation270_RotatesMotions) {
5915 addConfigurationProperty("touch.deviceType", "touchScreen");
5916 prepareButtons();
5917 prepareAxes(POSITION);
5918 addConfigurationProperty("touch.orientationAware", "1");
5919 addConfigurationProperty("touch.orientation", "ORIENTATION_270");
5920 clearViewports();
5921 prepareDisplay(DISPLAY_ORIENTATION_0);
5922 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5923 NotifyMotionArgs args;
5924
5925 // Orientation 270.
5926 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
5927 processSync(mapper);
5928
5929 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5930 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5931 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5932
5933 processUp(mapper);
5934 processSync(mapper);
5935 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5936}
5937
5938TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationSpecified_RotatesMotionWithDisplay) {
5939 addConfigurationProperty("touch.deviceType", "touchScreen");
5940 prepareButtons();
5941 prepareAxes(POSITION);
5942 // Since InputReader works in the un-rotated coordinate space, only devices that are not
5943 // orientation-aware are affected by display rotation.
5944 addConfigurationProperty("touch.orientationAware", "0");
5945 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
5946 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5947
5948 NotifyMotionArgs args;
5949
5950 // Orientation 90, Rotation 0.
5951 clearViewports();
5952 prepareDisplay(DISPLAY_ORIENTATION_0);
5953 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
5954 processSync(mapper);
5955
5956 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5957 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5958 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5959
5960 processUp(mapper);
5961 processSync(mapper);
5962 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5963
5964 // Orientation 90, Rotation 90.
5965 clearViewports();
5966 prepareDisplay(DISPLAY_ORIENTATION_90);
5967 processDown(mapper, toRotatedRawX(50), toRotatedRawY(75));
5968 processSync(mapper);
5969
5970 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5971 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5972 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5973
5974 processUp(mapper);
5975 processSync(mapper);
5976 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5977
5978 // Orientation 90, Rotation 180.
5979 clearViewports();
5980 prepareDisplay(DISPLAY_ORIENTATION_180);
5981 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
5982 processSync(mapper);
5983
5984 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5985 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5986 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5987
5988 processUp(mapper);
5989 processSync(mapper);
5990 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5991
5992 // Orientation 90, Rotation 270.
5993 clearViewports();
5994 prepareDisplay(DISPLAY_ORIENTATION_270);
5995 processDown(mapper, RAW_X_MAX - toRotatedRawX(50) + RAW_X_MIN,
5996 RAW_Y_MAX - toRotatedRawY(75) + RAW_Y_MIN);
5997 processSync(mapper);
5998
5999 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6000 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6001 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6002
6003 processUp(mapper);
6004 processSync(mapper);
6005 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6006}
6007
Michael Wrightd02c5b62014-02-10 15:10:22 -08006008TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006009 addConfigurationProperty("touch.deviceType", "touchScreen");
6010 prepareDisplay(DISPLAY_ORIENTATION_0);
6011 prepareButtons();
6012 prepareAxes(POSITION | PRESSURE | TOOL | DISTANCE | TILT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006013 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006014
6015 // These calculations are based on the input device calibration documentation.
6016 int32_t rawX = 100;
6017 int32_t rawY = 200;
6018 int32_t rawPressure = 10;
6019 int32_t rawToolMajor = 12;
6020 int32_t rawDistance = 2;
6021 int32_t rawTiltX = 30;
6022 int32_t rawTiltY = 110;
6023
6024 float x = toDisplayX(rawX);
6025 float y = toDisplayY(rawY);
6026 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
6027 float size = float(rawToolMajor) / RAW_TOOL_MAX;
6028 float tool = float(rawToolMajor) * GEOMETRIC_SCALE;
6029 float distance = float(rawDistance);
6030
6031 float tiltCenter = (RAW_TILT_MAX + RAW_TILT_MIN) * 0.5f;
6032 float tiltScale = M_PI / 180;
6033 float tiltXAngle = (rawTiltX - tiltCenter) * tiltScale;
6034 float tiltYAngle = (rawTiltY - tiltCenter) * tiltScale;
6035 float orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
6036 float tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
6037
6038 processDown(mapper, rawX, rawY);
6039 processPressure(mapper, rawPressure);
6040 processToolMajor(mapper, rawToolMajor);
6041 processDistance(mapper, rawDistance);
6042 processTilt(mapper, rawTiltX, rawTiltY);
6043 processSync(mapper);
6044
6045 NotifyMotionArgs args;
6046 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6047 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6048 x, y, pressure, size, tool, tool, tool, tool, orientation, distance));
6049 ASSERT_EQ(tilt, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TILT));
6050}
6051
Jason Gerecke489fda82012-09-07 17:19:40 -07006052TEST_F(SingleTouchInputMapperTest, Process_XYAxes_AffineCalibration) {
Jason Gerecke489fda82012-09-07 17:19:40 -07006053 addConfigurationProperty("touch.deviceType", "touchScreen");
6054 prepareDisplay(DISPLAY_ORIENTATION_0);
6055 prepareLocationCalibration();
6056 prepareButtons();
6057 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006058 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Jason Gerecke489fda82012-09-07 17:19:40 -07006059
6060 int32_t rawX = 100;
6061 int32_t rawY = 200;
6062
6063 float x = toDisplayX(toCookedX(rawX, rawY));
6064 float y = toDisplayY(toCookedY(rawX, rawY));
6065
6066 processDown(mapper, rawX, rawY);
6067 processSync(mapper);
6068
6069 NotifyMotionArgs args;
6070 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6071 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6072 x, y, 1, 0, 0, 0, 0, 0, 0, 0));
6073}
6074
Michael Wrightd02c5b62014-02-10 15:10:22 -08006075TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006076 addConfigurationProperty("touch.deviceType", "touchScreen");
6077 prepareDisplay(DISPLAY_ORIENTATION_0);
6078 prepareButtons();
6079 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006080 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006081
6082 NotifyMotionArgs motionArgs;
6083 NotifyKeyArgs keyArgs;
6084
6085 processDown(mapper, 100, 200);
6086 processSync(mapper);
6087 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6088 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6089 ASSERT_EQ(0, motionArgs.buttonState);
6090
6091 // press BTN_LEFT, release BTN_LEFT
6092 processKey(mapper, BTN_LEFT, 1);
6093 processSync(mapper);
6094 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6095 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6096 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6097
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006098 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6099 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6100 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6101
Michael Wrightd02c5b62014-02-10 15:10:22 -08006102 processKey(mapper, BTN_LEFT, 0);
6103 processSync(mapper);
6104 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006105 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006106 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006107
6108 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006109 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006110 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006111
6112 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
6113 processKey(mapper, BTN_RIGHT, 1);
6114 processKey(mapper, BTN_MIDDLE, 1);
6115 processSync(mapper);
6116 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6117 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6118 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6119 motionArgs.buttonState);
6120
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006121 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6122 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6123 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
6124
6125 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6126 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6127 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6128 motionArgs.buttonState);
6129
Michael Wrightd02c5b62014-02-10 15:10:22 -08006130 processKey(mapper, BTN_RIGHT, 0);
6131 processSync(mapper);
6132 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006133 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006134 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006135
6136 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006137 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006138 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006139
6140 processKey(mapper, BTN_MIDDLE, 0);
6141 processSync(mapper);
6142 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006143 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006144 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006145
6146 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006147 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006148 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006149
6150 // press BTN_BACK, release BTN_BACK
6151 processKey(mapper, BTN_BACK, 1);
6152 processSync(mapper);
6153 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6154 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6155 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006156
Michael Wrightd02c5b62014-02-10 15:10:22 -08006157 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006158 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006159 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6160
6161 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6162 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6163 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006164
6165 processKey(mapper, BTN_BACK, 0);
6166 processSync(mapper);
6167 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006168 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006169 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006170
6171 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006172 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006173 ASSERT_EQ(0, motionArgs.buttonState);
6174
Michael Wrightd02c5b62014-02-10 15:10:22 -08006175 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6176 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6177 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6178
6179 // press BTN_SIDE, release BTN_SIDE
6180 processKey(mapper, BTN_SIDE, 1);
6181 processSync(mapper);
6182 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6183 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6184 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006185
Michael Wrightd02c5b62014-02-10 15:10:22 -08006186 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006187 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006188 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6189
6190 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6191 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6192 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006193
6194 processKey(mapper, BTN_SIDE, 0);
6195 processSync(mapper);
6196 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006197 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006198 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006199
6200 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006201 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006202 ASSERT_EQ(0, motionArgs.buttonState);
6203
Michael Wrightd02c5b62014-02-10 15:10:22 -08006204 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6205 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6206 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6207
6208 // press BTN_FORWARD, release BTN_FORWARD
6209 processKey(mapper, BTN_FORWARD, 1);
6210 processSync(mapper);
6211 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6212 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6213 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006214
Michael Wrightd02c5b62014-02-10 15:10:22 -08006215 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006216 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006217 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6218
6219 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6220 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6221 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006222
6223 processKey(mapper, BTN_FORWARD, 0);
6224 processSync(mapper);
6225 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006226 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006227 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006228
6229 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006230 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006231 ASSERT_EQ(0, motionArgs.buttonState);
6232
Michael Wrightd02c5b62014-02-10 15:10:22 -08006233 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6234 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6235 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6236
6237 // press BTN_EXTRA, release BTN_EXTRA
6238 processKey(mapper, BTN_EXTRA, 1);
6239 processSync(mapper);
6240 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6241 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6242 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006243
Michael Wrightd02c5b62014-02-10 15:10:22 -08006244 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006245 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006246 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6247
6248 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6249 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6250 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006251
6252 processKey(mapper, BTN_EXTRA, 0);
6253 processSync(mapper);
6254 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006255 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006256 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006257
6258 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006259 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006260 ASSERT_EQ(0, motionArgs.buttonState);
6261
Michael Wrightd02c5b62014-02-10 15:10:22 -08006262 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6263 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6264 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6265
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006266 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6267
Michael Wrightd02c5b62014-02-10 15:10:22 -08006268 // press BTN_STYLUS, release BTN_STYLUS
6269 processKey(mapper, BTN_STYLUS, 1);
6270 processSync(mapper);
6271 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6272 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006273 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
6274
6275 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6276 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6277 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006278
6279 processKey(mapper, BTN_STYLUS, 0);
6280 processSync(mapper);
6281 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006282 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006283 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006284
6285 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006286 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006287 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006288
6289 // press BTN_STYLUS2, release BTN_STYLUS2
6290 processKey(mapper, BTN_STYLUS2, 1);
6291 processSync(mapper);
6292 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6293 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006294 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
6295
6296 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6297 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6298 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006299
6300 processKey(mapper, BTN_STYLUS2, 0);
6301 processSync(mapper);
6302 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006303 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006304 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006305
6306 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006307 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006308 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006309
6310 // release touch
6311 processUp(mapper);
6312 processSync(mapper);
6313 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6314 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6315 ASSERT_EQ(0, motionArgs.buttonState);
6316}
6317
6318TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006319 addConfigurationProperty("touch.deviceType", "touchScreen");
6320 prepareDisplay(DISPLAY_ORIENTATION_0);
6321 prepareButtons();
6322 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006323 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006324
6325 NotifyMotionArgs motionArgs;
6326
6327 // default tool type is finger
6328 processDown(mapper, 100, 200);
6329 processSync(mapper);
6330 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6331 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6332 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6333
6334 // eraser
6335 processKey(mapper, BTN_TOOL_RUBBER, 1);
6336 processSync(mapper);
6337 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6338 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6339 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6340
6341 // stylus
6342 processKey(mapper, BTN_TOOL_RUBBER, 0);
6343 processKey(mapper, BTN_TOOL_PEN, 1);
6344 processSync(mapper);
6345 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6346 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6347 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6348
6349 // brush
6350 processKey(mapper, BTN_TOOL_PEN, 0);
6351 processKey(mapper, BTN_TOOL_BRUSH, 1);
6352 processSync(mapper);
6353 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6354 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6355 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6356
6357 // pencil
6358 processKey(mapper, BTN_TOOL_BRUSH, 0);
6359 processKey(mapper, BTN_TOOL_PENCIL, 1);
6360 processSync(mapper);
6361 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6362 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6363 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6364
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08006365 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08006366 processKey(mapper, BTN_TOOL_PENCIL, 0);
6367 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
6368 processSync(mapper);
6369 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6370 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6371 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6372
6373 // mouse
6374 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
6375 processKey(mapper, BTN_TOOL_MOUSE, 1);
6376 processSync(mapper);
6377 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6378 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6379 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6380
6381 // lens
6382 processKey(mapper, BTN_TOOL_MOUSE, 0);
6383 processKey(mapper, BTN_TOOL_LENS, 1);
6384 processSync(mapper);
6385 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6386 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6387 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6388
6389 // double-tap
6390 processKey(mapper, BTN_TOOL_LENS, 0);
6391 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
6392 processSync(mapper);
6393 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6394 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6395 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6396
6397 // triple-tap
6398 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
6399 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
6400 processSync(mapper);
6401 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6402 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6403 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6404
6405 // quad-tap
6406 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
6407 processKey(mapper, BTN_TOOL_QUADTAP, 1);
6408 processSync(mapper);
6409 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6410 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6411 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6412
6413 // finger
6414 processKey(mapper, BTN_TOOL_QUADTAP, 0);
6415 processKey(mapper, BTN_TOOL_FINGER, 1);
6416 processSync(mapper);
6417 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6418 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6419 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6420
6421 // stylus trumps finger
6422 processKey(mapper, BTN_TOOL_PEN, 1);
6423 processSync(mapper);
6424 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6425 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6426 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6427
6428 // eraser trumps stylus
6429 processKey(mapper, BTN_TOOL_RUBBER, 1);
6430 processSync(mapper);
6431 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6432 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6433 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6434
6435 // mouse trumps eraser
6436 processKey(mapper, BTN_TOOL_MOUSE, 1);
6437 processSync(mapper);
6438 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6439 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6440 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6441
6442 // back to default tool type
6443 processKey(mapper, BTN_TOOL_MOUSE, 0);
6444 processKey(mapper, BTN_TOOL_RUBBER, 0);
6445 processKey(mapper, BTN_TOOL_PEN, 0);
6446 processKey(mapper, BTN_TOOL_FINGER, 0);
6447 processSync(mapper);
6448 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6449 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6450 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6451}
6452
6453TEST_F(SingleTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006454 addConfigurationProperty("touch.deviceType", "touchScreen");
6455 prepareDisplay(DISPLAY_ORIENTATION_0);
6456 prepareButtons();
6457 prepareAxes(POSITION);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006458 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_FINGER, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006459 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006460
6461 NotifyMotionArgs motionArgs;
6462
6463 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
6464 processKey(mapper, BTN_TOOL_FINGER, 1);
6465 processMove(mapper, 100, 200);
6466 processSync(mapper);
6467 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6468 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6469 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6470 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6471
6472 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6473 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6474 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6475 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6476
6477 // move a little
6478 processMove(mapper, 150, 250);
6479 processSync(mapper);
6480 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6481 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6482 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6483 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6484
6485 // down when BTN_TOUCH is pressed, pressure defaults to 1
6486 processKey(mapper, BTN_TOUCH, 1);
6487 processSync(mapper);
6488 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6489 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6490 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6491 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6492
6493 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6494 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6495 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6496 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6497
6498 // up when BTN_TOUCH is released, hover restored
6499 processKey(mapper, BTN_TOUCH, 0);
6500 processSync(mapper);
6501 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6502 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6503 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6504 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6505
6506 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6507 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6508 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6509 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6510
6511 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6512 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6513 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6514 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6515
6516 // exit hover when pointer goes away
6517 processKey(mapper, BTN_TOOL_FINGER, 0);
6518 processSync(mapper);
6519 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6520 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6521 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6522 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6523}
6524
6525TEST_F(SingleTouchInputMapperTest, Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006526 addConfigurationProperty("touch.deviceType", "touchScreen");
6527 prepareDisplay(DISPLAY_ORIENTATION_0);
6528 prepareButtons();
6529 prepareAxes(POSITION | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006530 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006531
6532 NotifyMotionArgs motionArgs;
6533
6534 // initially hovering because pressure is 0
6535 processDown(mapper, 100, 200);
6536 processPressure(mapper, 0);
6537 processSync(mapper);
6538 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6539 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6540 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6541 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6542
6543 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6544 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6545 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6546 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6547
6548 // move a little
6549 processMove(mapper, 150, 250);
6550 processSync(mapper);
6551 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6552 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6553 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6554 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6555
6556 // down when pressure is non-zero
6557 processPressure(mapper, RAW_PRESSURE_MAX);
6558 processSync(mapper);
6559 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6560 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6561 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6562 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6563
6564 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6565 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6566 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6567 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6568
6569 // up when pressure becomes 0, hover restored
6570 processPressure(mapper, 0);
6571 processSync(mapper);
6572 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6573 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6574 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6575 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6576
6577 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6578 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, 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 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6583 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6584 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6585 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6586
6587 // exit hover when pointer goes away
6588 processUp(mapper);
6589 processSync(mapper);
6590 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6591 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6592 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6593 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6594}
6595
Prabir Pradhan5632d622021-09-06 07:57:20 -07006596// --- TouchDisplayProjectionTest ---
6597
6598class TouchDisplayProjectionTest : public SingleTouchInputMapperTest {
6599public:
6600 // The values inside DisplayViewport are expected to be pre-rotated. This updates the current
6601 // DisplayViewport to pre-rotate the values. The viewport's physical display will be set to the
6602 // rotated equivalent of the given un-rotated physical display bounds.
6603 void configurePhysicalDisplay(int32_t orientation, Rect naturalPhysicalDisplay) {
6604 uint32_t inverseRotationFlags;
6605 auto width = DISPLAY_WIDTH;
6606 auto height = DISPLAY_HEIGHT;
6607 switch (orientation) {
6608 case DISPLAY_ORIENTATION_90:
6609 inverseRotationFlags = ui::Transform::ROT_270;
6610 std::swap(width, height);
6611 break;
6612 case DISPLAY_ORIENTATION_180:
6613 inverseRotationFlags = ui::Transform::ROT_180;
6614 break;
6615 case DISPLAY_ORIENTATION_270:
6616 inverseRotationFlags = ui::Transform::ROT_90;
6617 std::swap(width, height);
6618 break;
6619 case DISPLAY_ORIENTATION_0:
6620 inverseRotationFlags = ui::Transform::ROT_0;
6621 break;
6622 default:
6623 FAIL() << "Invalid orientation: " << orientation;
6624 }
6625
6626 const ui::Transform rotation(inverseRotationFlags, width, height);
6627 const Rect rotatedPhysicalDisplay = rotation.transform(naturalPhysicalDisplay);
6628
6629 std::optional<DisplayViewport> internalViewport =
6630 *mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6631 DisplayViewport& v = *internalViewport;
6632 v.displayId = DISPLAY_ID;
6633 v.orientation = orientation;
6634
6635 v.logicalLeft = 0;
6636 v.logicalTop = 0;
6637 v.logicalRight = 100;
6638 v.logicalBottom = 100;
6639
6640 v.physicalLeft = rotatedPhysicalDisplay.left;
6641 v.physicalTop = rotatedPhysicalDisplay.top;
6642 v.physicalRight = rotatedPhysicalDisplay.right;
6643 v.physicalBottom = rotatedPhysicalDisplay.bottom;
6644
6645 v.deviceWidth = width;
6646 v.deviceHeight = height;
6647
6648 v.isActive = true;
6649 v.uniqueId = UNIQUE_ID;
6650 v.type = ViewportType::INTERNAL;
6651 mFakePolicy->updateViewport(v);
6652 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
6653 }
6654
6655 void assertReceivedMove(const Point& point) {
6656 NotifyMotionArgs motionArgs;
6657 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6658 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6659 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6660 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], point.x, point.y,
6661 1, 0, 0, 0, 0, 0, 0, 0));
6662 }
6663};
6664
6665TEST_F(TouchDisplayProjectionTest, IgnoresTouchesOutsidePhysicalDisplay) {
6666 addConfigurationProperty("touch.deviceType", "touchScreen");
6667 prepareDisplay(DISPLAY_ORIENTATION_0);
6668
6669 prepareButtons();
6670 prepareAxes(POSITION);
6671 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6672
6673 NotifyMotionArgs motionArgs;
6674
6675 // Configure the DisplayViewport such that the logical display maps to a subsection of
6676 // the display panel called the physical display. Here, the physical display is bounded by the
6677 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
6678 static const Rect kPhysicalDisplay{10, 20, 70, 160};
6679 static const std::array<Point, 6> kPointsOutsidePhysicalDisplay{
6680 {{-10, -10}, {0, 0}, {5, 100}, {50, 15}, {75, 100}, {50, 165}}};
6681
6682 for (auto orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90, DISPLAY_ORIENTATION_180,
6683 DISPLAY_ORIENTATION_270}) {
6684 configurePhysicalDisplay(orientation, kPhysicalDisplay);
6685
6686 // Touches outside the physical display should be ignored, and should not generate any
6687 // events. Ensure touches at the following points that lie outside of the physical display
6688 // area do not generate any events.
6689 for (const auto& point : kPointsOutsidePhysicalDisplay) {
6690 processDown(mapper, toRawX(point.x), toRawY(point.y));
6691 processSync(mapper);
6692 processUp(mapper);
6693 processSync(mapper);
6694 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled())
6695 << "Unexpected event generated for touch outside physical display at point: "
6696 << point.x << ", " << point.y;
6697 }
6698 }
6699}
6700
6701TEST_F(TouchDisplayProjectionTest, EmitsTouchDownAfterEnteringPhysicalDisplay) {
6702 addConfigurationProperty("touch.deviceType", "touchScreen");
6703 prepareDisplay(DISPLAY_ORIENTATION_0);
6704
6705 prepareButtons();
6706 prepareAxes(POSITION);
6707 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6708
6709 NotifyMotionArgs motionArgs;
6710
6711 // Configure the DisplayViewport such that the logical display maps to a subsection of
6712 // the display panel called the physical display. Here, the physical display is bounded by the
6713 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
6714 static const Rect kPhysicalDisplay{10, 20, 70, 160};
6715
6716 for (auto orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90, DISPLAY_ORIENTATION_180,
6717 DISPLAY_ORIENTATION_270}) {
6718 configurePhysicalDisplay(orientation, kPhysicalDisplay);
6719
6720 // Touches that start outside the physical display should be ignored until it enters the
6721 // physical display bounds, at which point it should generate a down event. Start a touch at
6722 // the point (5, 100), which is outside the physical display bounds.
6723 static const Point kOutsidePoint{5, 100};
6724 processDown(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
6725 processSync(mapper);
6726 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6727
6728 // Move the touch into the physical display area. This should generate a pointer down.
6729 processMove(mapper, toRawX(11), toRawY(21));
6730 processSync(mapper);
6731 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6732 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6733 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6734 ASSERT_NO_FATAL_FAILURE(
6735 assertPointerCoords(motionArgs.pointerCoords[0], 11, 21, 1, 0, 0, 0, 0, 0, 0, 0));
6736
6737 // Move the touch inside the physical display area. This should generate a pointer move.
6738 processMove(mapper, toRawX(69), toRawY(159));
6739 processSync(mapper);
6740 assertReceivedMove({69, 159});
6741
6742 // Move outside the physical display area. Since the pointer is already down, this should
6743 // now continue generating events.
6744 processMove(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
6745 processSync(mapper);
6746 assertReceivedMove(kOutsidePoint);
6747
6748 // Release. This should generate a pointer up.
6749 processUp(mapper);
6750 processSync(mapper);
6751 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6752 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6753 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], kOutsidePoint.x,
6754 kOutsidePoint.y, 1, 0, 0, 0, 0, 0, 0, 0));
6755
6756 // Ensure no more events were generated.
6757 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6758 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6759 }
6760}
6761
Michael Wrightd02c5b62014-02-10 15:10:22 -08006762// --- MultiTouchInputMapperTest ---
6763
6764class MultiTouchInputMapperTest : public TouchInputMapperTest {
6765protected:
6766 void prepareAxes(int axes);
6767
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006768 void processPosition(MultiTouchInputMapper& mapper, int32_t x, int32_t y);
6769 void processTouchMajor(MultiTouchInputMapper& mapper, int32_t touchMajor);
6770 void processTouchMinor(MultiTouchInputMapper& mapper, int32_t touchMinor);
6771 void processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor);
6772 void processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor);
6773 void processOrientation(MultiTouchInputMapper& mapper, int32_t orientation);
6774 void processPressure(MultiTouchInputMapper& mapper, int32_t pressure);
6775 void processDistance(MultiTouchInputMapper& mapper, int32_t distance);
6776 void processId(MultiTouchInputMapper& mapper, int32_t id);
6777 void processSlot(MultiTouchInputMapper& mapper, int32_t slot);
6778 void processToolType(MultiTouchInputMapper& mapper, int32_t toolType);
6779 void processKey(MultiTouchInputMapper& mapper, int32_t code, int32_t value);
6780 void processMTSync(MultiTouchInputMapper& mapper);
6781 void processSync(MultiTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006782};
6783
6784void MultiTouchInputMapperTest::prepareAxes(int axes) {
6785 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006786 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
6787 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006788 }
6789 if (axes & TOUCH) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006790 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN,
6791 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006792 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006793 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN,
6794 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006795 }
6796 }
6797 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006798 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
6799 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006800 if (axes & MINOR) {
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08006801 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006802 RAW_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006803 }
6804 }
6805 if (axes & ORIENTATION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006806 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_ORIENTATION, RAW_ORIENTATION_MIN,
6807 RAW_ORIENTATION_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006808 }
6809 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006810 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_PRESSURE, RAW_PRESSURE_MIN,
6811 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006812 }
6813 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006814 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_DISTANCE, RAW_DISTANCE_MIN,
6815 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006816 }
6817 if (axes & ID) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006818 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX, 0,
6819 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006820 }
6821 if (axes & SLOT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006822 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
6823 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_MT_SLOT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006824 }
6825 if (axes & TOOL_TYPE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006826 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOOL_TYPE, 0, MT_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006827 }
6828}
6829
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006830void MultiTouchInputMapperTest::processPosition(MultiTouchInputMapper& mapper, int32_t x,
6831 int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006832 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_X, x);
6833 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006834}
6835
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006836void MultiTouchInputMapperTest::processTouchMajor(MultiTouchInputMapper& mapper,
6837 int32_t touchMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006838 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006839}
6840
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006841void MultiTouchInputMapperTest::processTouchMinor(MultiTouchInputMapper& mapper,
6842 int32_t touchMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006843 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006844}
6845
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006846void MultiTouchInputMapperTest::processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006847 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006848}
6849
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006850void MultiTouchInputMapperTest::processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006851 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006852}
6853
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006854void MultiTouchInputMapperTest::processOrientation(MultiTouchInputMapper& mapper,
6855 int32_t orientation) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006856 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_ORIENTATION, orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006857}
6858
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006859void MultiTouchInputMapperTest::processPressure(MultiTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006860 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006861}
6862
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006863void MultiTouchInputMapperTest::processDistance(MultiTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006864 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006865}
6866
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006867void MultiTouchInputMapperTest::processId(MultiTouchInputMapper& mapper, int32_t id) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006868 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TRACKING_ID, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006869}
6870
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006871void MultiTouchInputMapperTest::processSlot(MultiTouchInputMapper& mapper, int32_t slot) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006872 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_SLOT, slot);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006873}
6874
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006875void MultiTouchInputMapperTest::processToolType(MultiTouchInputMapper& mapper, int32_t toolType) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006876 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006877}
6878
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006879void MultiTouchInputMapperTest::processKey(MultiTouchInputMapper& mapper, int32_t code,
6880 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006881 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006882}
6883
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006884void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006885 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_MT_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006886}
6887
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006888void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006889 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006890}
6891
Michael Wrightd02c5b62014-02-10 15:10:22 -08006892TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006893 addConfigurationProperty("touch.deviceType", "touchScreen");
6894 prepareDisplay(DISPLAY_ORIENTATION_0);
6895 prepareAxes(POSITION);
6896 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006897 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006898
arthurhungdcef2dc2020-08-11 14:47:50 +08006899 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006900
6901 NotifyMotionArgs motionArgs;
6902
6903 // Two fingers down at once.
6904 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
6905 processPosition(mapper, x1, y1);
6906 processMTSync(mapper);
6907 processPosition(mapper, x2, y2);
6908 processMTSync(mapper);
6909 processSync(mapper);
6910
6911 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6912 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6913 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6914 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6915 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6916 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6917 ASSERT_EQ(0, motionArgs.flags);
6918 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6919 ASSERT_EQ(0, motionArgs.buttonState);
6920 ASSERT_EQ(0, motionArgs.edgeFlags);
6921 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6922 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6923 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6924 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6925 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6926 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6927 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6928 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6929
6930 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6931 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6932 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6933 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6934 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08006935 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006936 ASSERT_EQ(0, motionArgs.flags);
6937 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6938 ASSERT_EQ(0, motionArgs.buttonState);
6939 ASSERT_EQ(0, motionArgs.edgeFlags);
6940 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6941 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6942 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6943 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6944 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6945 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6946 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6947 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6948 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6949 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6950 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6951 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6952
6953 // Move.
6954 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
6955 processPosition(mapper, x1, y1);
6956 processMTSync(mapper);
6957 processPosition(mapper, x2, y2);
6958 processMTSync(mapper);
6959 processSync(mapper);
6960
6961 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6962 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6963 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6964 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6965 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6966 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6967 ASSERT_EQ(0, motionArgs.flags);
6968 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6969 ASSERT_EQ(0, motionArgs.buttonState);
6970 ASSERT_EQ(0, motionArgs.edgeFlags);
6971 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6972 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6973 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6974 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6975 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6976 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6977 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6978 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6979 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6980 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6981 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6982 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6983
6984 // First finger up.
6985 x2 += 15; y2 -= 20;
6986 processPosition(mapper, x2, y2);
6987 processMTSync(mapper);
6988 processSync(mapper);
6989
6990 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6991 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6992 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6993 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6994 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08006995 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006996 ASSERT_EQ(0, motionArgs.flags);
6997 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6998 ASSERT_EQ(0, motionArgs.buttonState);
6999 ASSERT_EQ(0, motionArgs.edgeFlags);
7000 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7001 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7002 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7003 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7004 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7005 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7006 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7007 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7008 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7009 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7010 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7011 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7012
7013 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7014 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7015 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7016 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7017 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7018 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7019 ASSERT_EQ(0, motionArgs.flags);
7020 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7021 ASSERT_EQ(0, motionArgs.buttonState);
7022 ASSERT_EQ(0, motionArgs.edgeFlags);
7023 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7024 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7025 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7026 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7027 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7028 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7029 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7030 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7031
7032 // Move.
7033 x2 += 20; y2 -= 25;
7034 processPosition(mapper, x2, y2);
7035 processMTSync(mapper);
7036 processSync(mapper);
7037
7038 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7039 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7040 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7041 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7042 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7043 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7044 ASSERT_EQ(0, motionArgs.flags);
7045 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7046 ASSERT_EQ(0, motionArgs.buttonState);
7047 ASSERT_EQ(0, motionArgs.edgeFlags);
7048 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7049 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7050 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7051 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7052 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7053 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7054 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7055 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7056
7057 // New finger down.
7058 int32_t x3 = 700, y3 = 300;
7059 processPosition(mapper, x2, y2);
7060 processMTSync(mapper);
7061 processPosition(mapper, x3, y3);
7062 processMTSync(mapper);
7063 processSync(mapper);
7064
7065 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7066 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7067 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7068 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7069 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007070 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007071 ASSERT_EQ(0, motionArgs.flags);
7072 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7073 ASSERT_EQ(0, motionArgs.buttonState);
7074 ASSERT_EQ(0, motionArgs.edgeFlags);
7075 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7076 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7077 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7078 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7079 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7080 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7081 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7082 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7083 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7084 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7085 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7086 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7087
7088 // Second finger up.
7089 x3 += 30; y3 -= 20;
7090 processPosition(mapper, x3, y3);
7091 processMTSync(mapper);
7092 processSync(mapper);
7093
7094 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7095 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7096 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7097 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7098 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007099 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007100 ASSERT_EQ(0, motionArgs.flags);
7101 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7102 ASSERT_EQ(0, motionArgs.buttonState);
7103 ASSERT_EQ(0, motionArgs.edgeFlags);
7104 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7105 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7106 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7107 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7108 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7109 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7110 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7111 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7112 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7113 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7114 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7115 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7116
7117 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7118 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7119 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7120 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7121 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7122 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7123 ASSERT_EQ(0, motionArgs.flags);
7124 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7125 ASSERT_EQ(0, motionArgs.buttonState);
7126 ASSERT_EQ(0, motionArgs.edgeFlags);
7127 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7128 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7129 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7130 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7131 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7132 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7133 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7134 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7135
7136 // Last finger up.
7137 processMTSync(mapper);
7138 processSync(mapper);
7139
7140 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7141 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7142 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7143 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7144 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7145 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7146 ASSERT_EQ(0, motionArgs.flags);
7147 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7148 ASSERT_EQ(0, motionArgs.buttonState);
7149 ASSERT_EQ(0, motionArgs.edgeFlags);
7150 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7151 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7152 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7153 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7154 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7155 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7156 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7157 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7158
7159 // Should not have sent any more keys or motions.
7160 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7161 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7162}
7163
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007164TEST_F(MultiTouchInputMapperTest, AxisResolution_IsPopulated) {
7165 addConfigurationProperty("touch.deviceType", "touchScreen");
7166 prepareDisplay(DISPLAY_ORIENTATION_0);
7167
7168 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
7169 /*fuzz*/ 0, /*resolution*/ 10);
7170 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
7171 /*fuzz*/ 0, /*resolution*/ 11);
7172 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
7173 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 12);
7174 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
7175 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 13);
7176 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7177 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 14);
7178 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7179 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 15);
7180
7181 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7182
7183 // X and Y axes
7184 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_X, 10 / X_PRECISION);
7185 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_Y, 11 / Y_PRECISION);
7186 // Touch major and minor
7187 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR, 12 * GEOMETRIC_SCALE);
7188 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR, 13 * GEOMETRIC_SCALE);
7189 // Tool major and minor
7190 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR, 14 * GEOMETRIC_SCALE);
7191 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR, 15 * GEOMETRIC_SCALE);
7192}
7193
7194TEST_F(MultiTouchInputMapperTest, TouchMajorAndMinorAxes_DoNotAppearIfNotSupported) {
7195 addConfigurationProperty("touch.deviceType", "touchScreen");
7196 prepareDisplay(DISPLAY_ORIENTATION_0);
7197
7198 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
7199 /*fuzz*/ 0, /*resolution*/ 10);
7200 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
7201 /*fuzz*/ 0, /*resolution*/ 11);
7202
7203 // We do not add ABS_MT_TOUCH_MAJOR / MINOR or ABS_MT_WIDTH_MAJOR / MINOR axes
7204
7205 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7206
7207 // Touch major and minor
7208 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR);
7209 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR);
7210 // Tool major and minor
7211 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR);
7212 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR);
7213}
7214
Michael Wrightd02c5b62014-02-10 15:10:22 -08007215TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007216 addConfigurationProperty("touch.deviceType", "touchScreen");
7217 prepareDisplay(DISPLAY_ORIENTATION_0);
7218 prepareAxes(POSITION | ID);
7219 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007220 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007221
arthurhungdcef2dc2020-08-11 14:47:50 +08007222 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007223
7224 NotifyMotionArgs motionArgs;
7225
7226 // Two fingers down at once.
7227 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7228 processPosition(mapper, x1, y1);
7229 processId(mapper, 1);
7230 processMTSync(mapper);
7231 processPosition(mapper, x2, y2);
7232 processId(mapper, 2);
7233 processMTSync(mapper);
7234 processSync(mapper);
7235
7236 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7237 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7238 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7239 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7240 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7241 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7242 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7243
7244 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007245 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007246 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7247 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7248 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7249 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7250 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7251 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7252 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7253 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7254 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7255
7256 // Move.
7257 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7258 processPosition(mapper, x1, y1);
7259 processId(mapper, 1);
7260 processMTSync(mapper);
7261 processPosition(mapper, x2, y2);
7262 processId(mapper, 2);
7263 processMTSync(mapper);
7264 processSync(mapper);
7265
7266 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7267 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7268 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7269 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7270 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7271 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7272 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7273 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7274 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7275 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7276 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7277
7278 // First finger up.
7279 x2 += 15; y2 -= 20;
7280 processPosition(mapper, x2, y2);
7281 processId(mapper, 2);
7282 processMTSync(mapper);
7283 processSync(mapper);
7284
7285 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007286 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007287 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7288 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7289 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7290 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7291 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7292 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7293 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7294 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7295 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7296
7297 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7298 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7299 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7300 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7301 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7302 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7303 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7304
7305 // Move.
7306 x2 += 20; y2 -= 25;
7307 processPosition(mapper, x2, y2);
7308 processId(mapper, 2);
7309 processMTSync(mapper);
7310 processSync(mapper);
7311
7312 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7313 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7314 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7315 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7316 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7317 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7318 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7319
7320 // New finger down.
7321 int32_t x3 = 700, y3 = 300;
7322 processPosition(mapper, x2, y2);
7323 processId(mapper, 2);
7324 processMTSync(mapper);
7325 processPosition(mapper, x3, y3);
7326 processId(mapper, 3);
7327 processMTSync(mapper);
7328 processSync(mapper);
7329
7330 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007331 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007332 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7333 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7334 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7335 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7336 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7337 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7338 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7339 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7340 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7341
7342 // Second finger up.
7343 x3 += 30; y3 -= 20;
7344 processPosition(mapper, x3, y3);
7345 processId(mapper, 3);
7346 processMTSync(mapper);
7347 processSync(mapper);
7348
7349 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007350 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007351 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7352 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7353 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7354 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7355 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7356 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7357 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7358 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7359 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7360
7361 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7362 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7363 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7364 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7365 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7366 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7367 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7368
7369 // Last finger up.
7370 processMTSync(mapper);
7371 processSync(mapper);
7372
7373 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7374 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7375 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7376 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7377 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7378 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7379 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7380
7381 // Should not have sent any more keys or motions.
7382 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7383 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7384}
7385
7386TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007387 addConfigurationProperty("touch.deviceType", "touchScreen");
7388 prepareDisplay(DISPLAY_ORIENTATION_0);
7389 prepareAxes(POSITION | ID | SLOT);
7390 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007391 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007392
arthurhungdcef2dc2020-08-11 14:47:50 +08007393 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007394
7395 NotifyMotionArgs motionArgs;
7396
7397 // Two fingers down at once.
7398 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7399 processPosition(mapper, x1, y1);
7400 processId(mapper, 1);
7401 processSlot(mapper, 1);
7402 processPosition(mapper, x2, y2);
7403 processId(mapper, 2);
7404 processSync(mapper);
7405
7406 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7407 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7408 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7409 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7410 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7411 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7412 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7413
7414 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007415 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007416 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7417 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7418 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7419 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7420 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7421 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7422 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7423 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7424 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7425
7426 // Move.
7427 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7428 processSlot(mapper, 0);
7429 processPosition(mapper, x1, y1);
7430 processSlot(mapper, 1);
7431 processPosition(mapper, x2, y2);
7432 processSync(mapper);
7433
7434 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7435 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7436 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7437 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7438 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7439 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7440 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7441 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7442 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7443 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7444 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7445
7446 // First finger up.
7447 x2 += 15; y2 -= 20;
7448 processSlot(mapper, 0);
7449 processId(mapper, -1);
7450 processSlot(mapper, 1);
7451 processPosition(mapper, x2, y2);
7452 processSync(mapper);
7453
7454 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007455 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007456 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7457 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7458 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7459 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7460 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7461 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7462 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7463 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7464 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7465
7466 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7467 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7468 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7469 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7470 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7471 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7472 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7473
7474 // Move.
7475 x2 += 20; y2 -= 25;
7476 processPosition(mapper, x2, y2);
7477 processSync(mapper);
7478
7479 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7480 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7481 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7482 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7483 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7484 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7485 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7486
7487 // New finger down.
7488 int32_t x3 = 700, y3 = 300;
7489 processPosition(mapper, x2, y2);
7490 processSlot(mapper, 0);
7491 processId(mapper, 3);
7492 processPosition(mapper, x3, y3);
7493 processSync(mapper);
7494
7495 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007496 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007497 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7498 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7499 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7500 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7501 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7502 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7503 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7504 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7505 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7506
7507 // Second finger up.
7508 x3 += 30; y3 -= 20;
7509 processSlot(mapper, 1);
7510 processId(mapper, -1);
7511 processSlot(mapper, 0);
7512 processPosition(mapper, x3, y3);
7513 processSync(mapper);
7514
7515 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007516 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007517 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7518 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7519 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7520 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7521 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7522 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7523 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7524 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7525 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7526
7527 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7528 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7529 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7530 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7531 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7532 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7533 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7534
7535 // Last finger up.
7536 processId(mapper, -1);
7537 processSync(mapper);
7538
7539 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7540 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7541 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7542 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7543 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7544 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7545 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7546
7547 // Should not have sent any more keys or motions.
7548 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7549 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7550}
7551
7552TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007553 addConfigurationProperty("touch.deviceType", "touchScreen");
7554 prepareDisplay(DISPLAY_ORIENTATION_0);
7555 prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007556 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007557
7558 // These calculations are based on the input device calibration documentation.
7559 int32_t rawX = 100;
7560 int32_t rawY = 200;
7561 int32_t rawTouchMajor = 7;
7562 int32_t rawTouchMinor = 6;
7563 int32_t rawToolMajor = 9;
7564 int32_t rawToolMinor = 8;
7565 int32_t rawPressure = 11;
7566 int32_t rawDistance = 0;
7567 int32_t rawOrientation = 3;
7568 int32_t id = 5;
7569
7570 float x = toDisplayX(rawX);
7571 float y = toDisplayY(rawY);
7572 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
7573 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
7574 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
7575 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
7576 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
7577 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
7578 float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
7579 float distance = float(rawDistance);
7580
7581 processPosition(mapper, rawX, rawY);
7582 processTouchMajor(mapper, rawTouchMajor);
7583 processTouchMinor(mapper, rawTouchMinor);
7584 processToolMajor(mapper, rawToolMajor);
7585 processToolMinor(mapper, rawToolMinor);
7586 processPressure(mapper, rawPressure);
7587 processOrientation(mapper, rawOrientation);
7588 processDistance(mapper, rawDistance);
7589 processId(mapper, id);
7590 processMTSync(mapper);
7591 processSync(mapper);
7592
7593 NotifyMotionArgs args;
7594 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7595 ASSERT_EQ(0, args.pointerProperties[0].id);
7596 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7597 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
7598 orientation, distance));
7599}
7600
7601TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007602 addConfigurationProperty("touch.deviceType", "touchScreen");
7603 prepareDisplay(DISPLAY_ORIENTATION_0);
7604 prepareAxes(POSITION | TOUCH | TOOL | MINOR);
7605 addConfigurationProperty("touch.size.calibration", "geometric");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007606 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007607
7608 // These calculations are based on the input device calibration documentation.
7609 int32_t rawX = 100;
7610 int32_t rawY = 200;
7611 int32_t rawTouchMajor = 140;
7612 int32_t rawTouchMinor = 120;
7613 int32_t rawToolMajor = 180;
7614 int32_t rawToolMinor = 160;
7615
7616 float x = toDisplayX(rawX);
7617 float y = toDisplayY(rawY);
7618 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
7619 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
7620 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
7621 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
7622 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
7623
7624 processPosition(mapper, rawX, rawY);
7625 processTouchMajor(mapper, rawTouchMajor);
7626 processTouchMinor(mapper, rawTouchMinor);
7627 processToolMajor(mapper, rawToolMajor);
7628 processToolMinor(mapper, rawToolMinor);
7629 processMTSync(mapper);
7630 processSync(mapper);
7631
7632 NotifyMotionArgs args;
7633 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7634 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7635 x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
7636}
7637
7638TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007639 addConfigurationProperty("touch.deviceType", "touchScreen");
7640 prepareDisplay(DISPLAY_ORIENTATION_0);
7641 prepareAxes(POSITION | TOUCH | TOOL);
7642 addConfigurationProperty("touch.size.calibration", "diameter");
7643 addConfigurationProperty("touch.size.scale", "10");
7644 addConfigurationProperty("touch.size.bias", "160");
7645 addConfigurationProperty("touch.size.isSummed", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007646 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007647
7648 // These calculations are based on the input device calibration documentation.
7649 // Note: We only provide a single common touch/tool value because the device is assumed
7650 // not to emit separate values for each pointer (isSummed = 1).
7651 int32_t rawX = 100;
7652 int32_t rawY = 200;
7653 int32_t rawX2 = 150;
7654 int32_t rawY2 = 250;
7655 int32_t rawTouchMajor = 5;
7656 int32_t rawToolMajor = 8;
7657
7658 float x = toDisplayX(rawX);
7659 float y = toDisplayY(rawY);
7660 float x2 = toDisplayX(rawX2);
7661 float y2 = toDisplayY(rawY2);
7662 float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
7663 float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
7664 float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
7665
7666 processPosition(mapper, rawX, rawY);
7667 processTouchMajor(mapper, rawTouchMajor);
7668 processToolMajor(mapper, rawToolMajor);
7669 processMTSync(mapper);
7670 processPosition(mapper, rawX2, rawY2);
7671 processTouchMajor(mapper, rawTouchMajor);
7672 processToolMajor(mapper, rawToolMajor);
7673 processMTSync(mapper);
7674 processSync(mapper);
7675
7676 NotifyMotionArgs args;
7677 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7678 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
7679
7680 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007681 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007682 ASSERT_EQ(size_t(2), args.pointerCount);
7683 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7684 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
7685 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
7686 x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
7687}
7688
7689TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007690 addConfigurationProperty("touch.deviceType", "touchScreen");
7691 prepareDisplay(DISPLAY_ORIENTATION_0);
7692 prepareAxes(POSITION | TOUCH | TOOL);
7693 addConfigurationProperty("touch.size.calibration", "area");
7694 addConfigurationProperty("touch.size.scale", "43");
7695 addConfigurationProperty("touch.size.bias", "3");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007696 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007697
7698 // These calculations are based on the input device calibration documentation.
7699 int32_t rawX = 100;
7700 int32_t rawY = 200;
7701 int32_t rawTouchMajor = 5;
7702 int32_t rawToolMajor = 8;
7703
7704 float x = toDisplayX(rawX);
7705 float y = toDisplayY(rawY);
7706 float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
7707 float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
7708 float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
7709
7710 processPosition(mapper, rawX, rawY);
7711 processTouchMajor(mapper, rawTouchMajor);
7712 processToolMajor(mapper, rawToolMajor);
7713 processMTSync(mapper);
7714 processSync(mapper);
7715
7716 NotifyMotionArgs args;
7717 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7718 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7719 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
7720}
7721
7722TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007723 addConfigurationProperty("touch.deviceType", "touchScreen");
7724 prepareDisplay(DISPLAY_ORIENTATION_0);
7725 prepareAxes(POSITION | PRESSURE);
7726 addConfigurationProperty("touch.pressure.calibration", "amplitude");
7727 addConfigurationProperty("touch.pressure.scale", "0.01");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007728 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007729
Michael Wrightaa449c92017-12-13 21:21:43 +00007730 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007731 mapper.populateDeviceInfo(&info);
Michael Wrightaa449c92017-12-13 21:21:43 +00007732 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
7733 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TOUCHSCREEN,
7734 0.0f, RAW_PRESSURE_MAX * 0.01, 0.0f, 0.0f));
7735
Michael Wrightd02c5b62014-02-10 15:10:22 -08007736 // These calculations are based on the input device calibration documentation.
7737 int32_t rawX = 100;
7738 int32_t rawY = 200;
7739 int32_t rawPressure = 60;
7740
7741 float x = toDisplayX(rawX);
7742 float y = toDisplayY(rawY);
7743 float pressure = float(rawPressure) * 0.01f;
7744
7745 processPosition(mapper, rawX, rawY);
7746 processPressure(mapper, rawPressure);
7747 processMTSync(mapper);
7748 processSync(mapper);
7749
7750 NotifyMotionArgs args;
7751 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7752 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7753 x, y, pressure, 0, 0, 0, 0, 0, 0, 0));
7754}
7755
7756TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007757 addConfigurationProperty("touch.deviceType", "touchScreen");
7758 prepareDisplay(DISPLAY_ORIENTATION_0);
7759 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007760 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007761
7762 NotifyMotionArgs motionArgs;
7763 NotifyKeyArgs keyArgs;
7764
7765 processId(mapper, 1);
7766 processPosition(mapper, 100, 200);
7767 processSync(mapper);
7768 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7769 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7770 ASSERT_EQ(0, motionArgs.buttonState);
7771
7772 // press BTN_LEFT, release BTN_LEFT
7773 processKey(mapper, BTN_LEFT, 1);
7774 processSync(mapper);
7775 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7776 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7777 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
7778
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007779 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7780 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7781 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
7782
Michael Wrightd02c5b62014-02-10 15:10:22 -08007783 processKey(mapper, BTN_LEFT, 0);
7784 processSync(mapper);
7785 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007786 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007787 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007788
7789 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007790 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007791 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007792
7793 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
7794 processKey(mapper, BTN_RIGHT, 1);
7795 processKey(mapper, BTN_MIDDLE, 1);
7796 processSync(mapper);
7797 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7798 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7799 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
7800 motionArgs.buttonState);
7801
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007802 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7803 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7804 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
7805
7806 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7807 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7808 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
7809 motionArgs.buttonState);
7810
Michael Wrightd02c5b62014-02-10 15:10:22 -08007811 processKey(mapper, BTN_RIGHT, 0);
7812 processSync(mapper);
7813 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007814 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007815 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007816
7817 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007818 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007819 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007820
7821 processKey(mapper, BTN_MIDDLE, 0);
7822 processSync(mapper);
7823 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007824 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007825 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007826
7827 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007828 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007829 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007830
7831 // press BTN_BACK, release BTN_BACK
7832 processKey(mapper, BTN_BACK, 1);
7833 processSync(mapper);
7834 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7835 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
7836 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007837
Michael Wrightd02c5b62014-02-10 15:10:22 -08007838 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007839 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007840 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
7841
7842 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7843 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7844 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007845
7846 processKey(mapper, BTN_BACK, 0);
7847 processSync(mapper);
7848 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007849 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007850 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007851
7852 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007853 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007854 ASSERT_EQ(0, motionArgs.buttonState);
7855
Michael Wrightd02c5b62014-02-10 15:10:22 -08007856 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7857 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
7858 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
7859
7860 // press BTN_SIDE, release BTN_SIDE
7861 processKey(mapper, BTN_SIDE, 1);
7862 processSync(mapper);
7863 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7864 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
7865 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007866
Michael Wrightd02c5b62014-02-10 15:10:22 -08007867 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007868 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007869 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
7870
7871 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7872 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7873 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007874
7875 processKey(mapper, BTN_SIDE, 0);
7876 processSync(mapper);
7877 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007878 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007879 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007880
7881 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007882 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007883 ASSERT_EQ(0, motionArgs.buttonState);
7884
Michael Wrightd02c5b62014-02-10 15:10:22 -08007885 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7886 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
7887 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
7888
7889 // press BTN_FORWARD, release BTN_FORWARD
7890 processKey(mapper, BTN_FORWARD, 1);
7891 processSync(mapper);
7892 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7893 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
7894 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007895
Michael Wrightd02c5b62014-02-10 15:10:22 -08007896 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007897 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007898 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
7899
7900 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7901 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7902 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007903
7904 processKey(mapper, BTN_FORWARD, 0);
7905 processSync(mapper);
7906 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007907 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007908 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007909
7910 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007911 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007912 ASSERT_EQ(0, motionArgs.buttonState);
7913
Michael Wrightd02c5b62014-02-10 15:10:22 -08007914 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7915 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
7916 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
7917
7918 // press BTN_EXTRA, release BTN_EXTRA
7919 processKey(mapper, BTN_EXTRA, 1);
7920 processSync(mapper);
7921 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7922 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
7923 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007924
Michael Wrightd02c5b62014-02-10 15:10:22 -08007925 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007926 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007927 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
7928
7929 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7930 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7931 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007932
7933 processKey(mapper, BTN_EXTRA, 0);
7934 processSync(mapper);
7935 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007936 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007937 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007938
7939 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007940 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007941 ASSERT_EQ(0, motionArgs.buttonState);
7942
Michael Wrightd02c5b62014-02-10 15:10:22 -08007943 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7944 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
7945 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
7946
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007947 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7948
Michael Wrightd02c5b62014-02-10 15:10:22 -08007949 // press BTN_STYLUS, release BTN_STYLUS
7950 processKey(mapper, BTN_STYLUS, 1);
7951 processSync(mapper);
7952 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7953 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007954 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
7955
7956 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7957 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7958 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007959
7960 processKey(mapper, BTN_STYLUS, 0);
7961 processSync(mapper);
7962 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007963 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007964 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007965
7966 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007967 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007968 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007969
7970 // press BTN_STYLUS2, release BTN_STYLUS2
7971 processKey(mapper, BTN_STYLUS2, 1);
7972 processSync(mapper);
7973 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7974 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007975 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
7976
7977 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7978 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7979 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007980
7981 processKey(mapper, BTN_STYLUS2, 0);
7982 processSync(mapper);
7983 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007984 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007985 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007986
7987 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007988 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007989 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007990
7991 // release touch
7992 processId(mapper, -1);
7993 processSync(mapper);
7994 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7995 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7996 ASSERT_EQ(0, motionArgs.buttonState);
7997}
7998
7999TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008000 addConfigurationProperty("touch.deviceType", "touchScreen");
8001 prepareDisplay(DISPLAY_ORIENTATION_0);
8002 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008003 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008004
8005 NotifyMotionArgs motionArgs;
8006
8007 // default tool type is finger
8008 processId(mapper, 1);
8009 processPosition(mapper, 100, 200);
8010 processSync(mapper);
8011 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8012 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8013 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8014
8015 // eraser
8016 processKey(mapper, BTN_TOOL_RUBBER, 1);
8017 processSync(mapper);
8018 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8019 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8020 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
8021
8022 // stylus
8023 processKey(mapper, BTN_TOOL_RUBBER, 0);
8024 processKey(mapper, BTN_TOOL_PEN, 1);
8025 processSync(mapper);
8026 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8027 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8028 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8029
8030 // brush
8031 processKey(mapper, BTN_TOOL_PEN, 0);
8032 processKey(mapper, BTN_TOOL_BRUSH, 1);
8033 processSync(mapper);
8034 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8035 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8036 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8037
8038 // pencil
8039 processKey(mapper, BTN_TOOL_BRUSH, 0);
8040 processKey(mapper, BTN_TOOL_PENCIL, 1);
8041 processSync(mapper);
8042 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8043 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8044 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8045
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08008046 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08008047 processKey(mapper, BTN_TOOL_PENCIL, 0);
8048 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
8049 processSync(mapper);
8050 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8051 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8052 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8053
8054 // mouse
8055 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
8056 processKey(mapper, BTN_TOOL_MOUSE, 1);
8057 processSync(mapper);
8058 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8059 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8060 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
8061
8062 // lens
8063 processKey(mapper, BTN_TOOL_MOUSE, 0);
8064 processKey(mapper, BTN_TOOL_LENS, 1);
8065 processSync(mapper);
8066 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8067 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8068 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
8069
8070 // double-tap
8071 processKey(mapper, BTN_TOOL_LENS, 0);
8072 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
8073 processSync(mapper);
8074 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8075 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8076 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8077
8078 // triple-tap
8079 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
8080 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
8081 processSync(mapper);
8082 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8083 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8084 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8085
8086 // quad-tap
8087 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
8088 processKey(mapper, BTN_TOOL_QUADTAP, 1);
8089 processSync(mapper);
8090 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8091 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8092 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8093
8094 // finger
8095 processKey(mapper, BTN_TOOL_QUADTAP, 0);
8096 processKey(mapper, BTN_TOOL_FINGER, 1);
8097 processSync(mapper);
8098 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8099 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8100 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8101
8102 // stylus trumps finger
8103 processKey(mapper, BTN_TOOL_PEN, 1);
8104 processSync(mapper);
8105 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8106 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8107 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8108
8109 // eraser trumps stylus
8110 processKey(mapper, BTN_TOOL_RUBBER, 1);
8111 processSync(mapper);
8112 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8113 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8114 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
8115
8116 // mouse trumps eraser
8117 processKey(mapper, BTN_TOOL_MOUSE, 1);
8118 processSync(mapper);
8119 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8120 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8121 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
8122
8123 // MT tool type trumps BTN tool types: MT_TOOL_FINGER
8124 processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
8125 processSync(mapper);
8126 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8127 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8128 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8129
8130 // MT tool type trumps BTN tool types: MT_TOOL_PEN
8131 processToolType(mapper, MT_TOOL_PEN);
8132 processSync(mapper);
8133 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8134 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8135 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8136
8137 // back to default tool type
8138 processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
8139 processKey(mapper, BTN_TOOL_MOUSE, 0);
8140 processKey(mapper, BTN_TOOL_RUBBER, 0);
8141 processKey(mapper, BTN_TOOL_PEN, 0);
8142 processKey(mapper, BTN_TOOL_FINGER, 0);
8143 processSync(mapper);
8144 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8145 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8146 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8147}
8148
8149TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008150 addConfigurationProperty("touch.deviceType", "touchScreen");
8151 prepareDisplay(DISPLAY_ORIENTATION_0);
8152 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008153 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008154 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008155
8156 NotifyMotionArgs motionArgs;
8157
8158 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
8159 processId(mapper, 1);
8160 processPosition(mapper, 100, 200);
8161 processSync(mapper);
8162 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8163 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8164 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8165 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8166
8167 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8168 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8169 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8170 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8171
8172 // move a little
8173 processPosition(mapper, 150, 250);
8174 processSync(mapper);
8175 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8176 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8177 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8178 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8179
8180 // down when BTN_TOUCH is pressed, pressure defaults to 1
8181 processKey(mapper, BTN_TOUCH, 1);
8182 processSync(mapper);
8183 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8184 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8185 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8186 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8187
8188 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8189 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8190 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8191 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8192
8193 // up when BTN_TOUCH is released, hover restored
8194 processKey(mapper, BTN_TOUCH, 0);
8195 processSync(mapper);
8196 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8197 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8198 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8199 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8200
8201 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8202 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8203 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8204 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8205
8206 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8207 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8208 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8209 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8210
8211 // exit hover when pointer goes away
8212 processId(mapper, -1);
8213 processSync(mapper);
8214 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8215 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8216 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8217 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8218}
8219
8220TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008221 addConfigurationProperty("touch.deviceType", "touchScreen");
8222 prepareDisplay(DISPLAY_ORIENTATION_0);
8223 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008224 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008225
8226 NotifyMotionArgs motionArgs;
8227
8228 // initially hovering because pressure is 0
8229 processId(mapper, 1);
8230 processPosition(mapper, 100, 200);
8231 processPressure(mapper, 0);
8232 processSync(mapper);
8233 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8234 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8235 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8236 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8237
8238 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8239 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8240 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8241 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8242
8243 // move a little
8244 processPosition(mapper, 150, 250);
8245 processSync(mapper);
8246 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8247 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8248 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8249 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8250
8251 // down when pressure becomes non-zero
8252 processPressure(mapper, RAW_PRESSURE_MAX);
8253 processSync(mapper);
8254 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8255 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8256 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8257 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8258
8259 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8260 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8261 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8262 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8263
8264 // up when pressure becomes 0, hover restored
8265 processPressure(mapper, 0);
8266 processSync(mapper);
8267 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8268 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8269 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8270 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8271
8272 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8273 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, 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 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8278 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8279 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8280 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8281
8282 // exit hover when pointer goes away
8283 processId(mapper, -1);
8284 processSync(mapper);
8285 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8286 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8287 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8288 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8289}
8290
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008291/**
8292 * Set the input device port <--> display port associations, and check that the
8293 * events are routed to the display that matches the display port.
8294 * This can be checked by looking at the displayId of the resulting NotifyMotionArgs.
8295 */
8296TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayPort) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008297 const std::string usb2 = "USB2";
8298 const uint8_t hdmi1 = 0;
8299 const uint8_t hdmi2 = 1;
8300 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008301 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008302
8303 addConfigurationProperty("touch.deviceType", "touchScreen");
8304 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008305 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008306
8307 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
8308 mFakePolicy->addInputPortAssociation(usb2, hdmi2);
8309
8310 // We are intentionally not adding the viewport for display 1 yet. Since the port association
8311 // for this input device is specified, and the matching viewport is not present,
8312 // the input device should be disabled (at the mapper level).
8313
8314 // Add viewport for display 2 on hdmi2
8315 prepareSecondaryDisplay(type, hdmi2);
8316 // Send a touch event
8317 processPosition(mapper, 100, 100);
8318 processSync(mapper);
8319 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8320
8321 // Add viewport for display 1 on hdmi1
8322 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
8323 // Send a touch event again
8324 processPosition(mapper, 100, 100);
8325 processSync(mapper);
8326
8327 NotifyMotionArgs args;
8328 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8329 ASSERT_EQ(DISPLAY_ID, args.displayId);
8330}
Michael Wrightd02c5b62014-02-10 15:10:22 -08008331
Arthur Hung6d5b4b22022-01-21 07:21:10 +00008332TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayUniqueId) {
8333 addConfigurationProperty("touch.deviceType", "touchScreen");
8334 prepareAxes(POSITION);
8335 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8336
8337 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
8338
8339 prepareDisplay(DISPLAY_ORIENTATION_0);
8340 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
8341
8342 // Send a touch event
8343 processPosition(mapper, 100, 100);
8344 processSync(mapper);
8345
8346 NotifyMotionArgs args;
8347 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8348 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
8349}
8350
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008351TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShouldHandleDisplayId) {
Garfield Tan888a6a42020-01-09 11:39:16 -08008352 // Setup for second display.
Michael Wright17db18e2020-06-26 20:51:44 +01008353 std::shared_ptr<FakePointerController> fakePointerController =
8354 std::make_shared<FakePointerController>();
Garfield Tan888a6a42020-01-09 11:39:16 -08008355 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008356 fakePointerController->setPosition(100, 200);
8357 fakePointerController->setButtonState(0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00008358 mFakePolicy->setPointerController(fakePointerController);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008359
Garfield Tan888a6a42020-01-09 11:39:16 -08008360 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008361 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08008362
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008363 prepareDisplay(DISPLAY_ORIENTATION_0);
8364 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008365 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008366
8367 // Check source is mouse that would obtain the PointerController.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008368 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008369
8370 NotifyMotionArgs motionArgs;
8371 processPosition(mapper, 100, 100);
8372 processSync(mapper);
8373
8374 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8375 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8376 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
8377}
8378
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00008379/**
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008380 * Ensure that the readTime is set to the SYN_REPORT value when processing touch events.
8381 */
8382TEST_F(MultiTouchInputMapperTest, Process_SendsReadTime) {
8383 addConfigurationProperty("touch.deviceType", "touchScreen");
8384 prepareAxes(POSITION);
8385 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8386
8387 prepareDisplay(DISPLAY_ORIENTATION_0);
8388 process(mapper, 10, 11 /*readTime*/, EV_ABS, ABS_MT_TRACKING_ID, 1);
8389 process(mapper, 15, 16 /*readTime*/, EV_ABS, ABS_MT_POSITION_X, 100);
8390 process(mapper, 20, 21 /*readTime*/, EV_ABS, ABS_MT_POSITION_Y, 100);
8391 process(mapper, 25, 26 /*readTime*/, EV_SYN, SYN_REPORT, 0);
8392
8393 NotifyMotionArgs args;
8394 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8395 ASSERT_EQ(26, args.readTime);
8396
8397 process(mapper, 30, 31 /*readTime*/, EV_ABS, ABS_MT_POSITION_X, 110);
8398 process(mapper, 30, 32 /*readTime*/, EV_ABS, ABS_MT_POSITION_Y, 220);
8399 process(mapper, 30, 33 /*readTime*/, EV_SYN, SYN_REPORT, 0);
8400
8401 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8402 ASSERT_EQ(33, args.readTime);
8403}
8404
8405/**
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00008406 * When the viewport is not active (isActive=false), the touch mapper should be disabled and the
8407 * events should not be delivered to the listener.
8408 */
8409TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreDropped) {
8410 addConfigurationProperty("touch.deviceType", "touchScreen");
8411 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
8412 DISPLAY_ORIENTATION_0, false /*isActive*/, UNIQUE_ID, NO_PORT,
8413 ViewportType::INTERNAL);
8414 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8415 prepareAxes(POSITION);
8416 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8417
8418 NotifyMotionArgs motionArgs;
8419 processPosition(mapper, 100, 100);
8420 processSync(mapper);
8421
8422 mFakeListener->assertNotifyMotionWasNotCalled();
8423}
8424
Garfield Tanc734e4f2021-01-15 20:01:39 -08008425TEST_F(MultiTouchInputMapperTest, Process_DeactivateViewport_AbortTouches) {
8426 addConfigurationProperty("touch.deviceType", "touchScreen");
8427 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
8428 DISPLAY_ORIENTATION_0, true /*isActive*/, UNIQUE_ID, NO_PORT,
8429 ViewportType::INTERNAL);
8430 std::optional<DisplayViewport> optionalDisplayViewport =
8431 mFakePolicy->getDisplayViewportByUniqueId(UNIQUE_ID);
8432 ASSERT_TRUE(optionalDisplayViewport.has_value());
8433 DisplayViewport displayViewport = *optionalDisplayViewport;
8434
8435 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8436 prepareAxes(POSITION);
8437 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8438
8439 // Finger down
8440 int32_t x = 100, y = 100;
8441 processPosition(mapper, x, y);
8442 processSync(mapper);
8443
8444 NotifyMotionArgs motionArgs;
8445 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8446 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8447
8448 // Deactivate display viewport
8449 displayViewport.isActive = false;
8450 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
8451 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8452
8453 // Finger move
8454 x += 10, y += 10;
8455 processPosition(mapper, x, y);
8456 processSync(mapper);
8457
8458 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8459 EXPECT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
8460
8461 // Reactivate display viewport
8462 displayViewport.isActive = true;
8463 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
8464 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8465
8466 // Finger move again
8467 x += 10, y += 10;
8468 processPosition(mapper, x, y);
8469 processSync(mapper);
8470
8471 // Gesture is aborted, so events after display is activated won't be dispatched until there is
8472 // no pointer on the touch device.
8473 mFakeListener->assertNotifyMotionWasNotCalled();
8474}
8475
Arthur Hung7c645402019-01-25 17:45:42 +08008476TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShowTouches) {
8477 // Setup the first touch screen device.
Arthur Hung7c645402019-01-25 17:45:42 +08008478 prepareAxes(POSITION | ID | SLOT);
8479 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008480 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung7c645402019-01-25 17:45:42 +08008481
8482 // Create the second touch screen device, and enable multi fingers.
8483 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08008484 const std::string DEVICE_NAME2 = "TOUCHSCREEN2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08008485 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008486 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08008487 std::shared_ptr<InputDevice> device2 =
8488 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07008489 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08008490
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008491 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
8492 0 /*flat*/, 0 /*fuzz*/);
8493 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
8494 0 /*flat*/, 0 /*fuzz*/);
8495 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX,
8496 0 /*flat*/, 0 /*fuzz*/);
8497 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX,
8498 0 /*flat*/, 0 /*fuzz*/);
8499 mFakeEventHub->setAbsoluteAxisValue(SECOND_EVENTHUB_ID, ABS_MT_SLOT, 0 /*value*/);
8500 mFakeEventHub->addConfigurationProperty(SECOND_EVENTHUB_ID, String8("touch.deviceType"),
8501 String8("touchScreen"));
Arthur Hung7c645402019-01-25 17:45:42 +08008502
8503 // Setup the second touch screen device.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008504 MultiTouchInputMapper& mapper2 = device2->addMapper<MultiTouchInputMapper>(SECOND_EVENTHUB_ID);
Arthur Hung7c645402019-01-25 17:45:42 +08008505 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
8506 device2->reset(ARBITRARY_TIME);
8507
8508 // Setup PointerController.
Michael Wright17db18e2020-06-26 20:51:44 +01008509 std::shared_ptr<FakePointerController> fakePointerController =
8510 std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00008511 mFakePolicy->setPointerController(fakePointerController);
Arthur Hung7c645402019-01-25 17:45:42 +08008512
8513 // Setup policy for associated displays and show touches.
8514 const uint8_t hdmi1 = 0;
8515 const uint8_t hdmi2 = 1;
8516 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
8517 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
8518 mFakePolicy->setShowTouches(true);
8519
8520 // Create displays.
8521 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008522 prepareSecondaryDisplay(ViewportType::EXTERNAL, hdmi2);
Arthur Hung7c645402019-01-25 17:45:42 +08008523
8524 // Default device will reconfigure above, need additional reconfiguration for another device.
8525 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008526 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung7c645402019-01-25 17:45:42 +08008527
8528 // Two fingers down at default display.
8529 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
8530 processPosition(mapper, x1, y1);
8531 processId(mapper, 1);
8532 processSlot(mapper, 1);
8533 processPosition(mapper, x2, y2);
8534 processId(mapper, 2);
8535 processSync(mapper);
8536
8537 std::map<int32_t, std::vector<int32_t>>::const_iterator iter =
8538 fakePointerController->getSpots().find(DISPLAY_ID);
8539 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
8540 ASSERT_EQ(size_t(2), iter->second.size());
8541
8542 // Two fingers down at second display.
8543 processPosition(mapper2, x1, y1);
8544 processId(mapper2, 1);
8545 processSlot(mapper2, 1);
8546 processPosition(mapper2, x2, y2);
8547 processId(mapper2, 2);
8548 processSync(mapper2);
8549
8550 iter = fakePointerController->getSpots().find(SECONDARY_DISPLAY_ID);
8551 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
8552 ASSERT_EQ(size_t(2), iter->second.size());
8553}
8554
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008555TEST_F(MultiTouchInputMapperTest, VideoFrames_ReceivedByListener) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008556 prepareAxes(POSITION);
8557 addConfigurationProperty("touch.deviceType", "touchScreen");
8558 prepareDisplay(DISPLAY_ORIENTATION_0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008559 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008560
8561 NotifyMotionArgs motionArgs;
8562 // Unrotated video frame
8563 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8564 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008565 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008566 processPosition(mapper, 100, 200);
8567 processSync(mapper);
8568 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8569 ASSERT_EQ(frames, motionArgs.videoFrames);
8570
8571 // Subsequent touch events should not have any videoframes
8572 // This is implemented separately in FakeEventHub,
8573 // but that should match the behaviour of TouchVideoDevice.
8574 processPosition(mapper, 200, 200);
8575 processSync(mapper);
8576 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8577 ASSERT_EQ(std::vector<TouchVideoFrame>(), motionArgs.videoFrames);
8578}
8579
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008580TEST_F(MultiTouchInputMapperTest, VideoFrames_AreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008581 prepareAxes(POSITION);
8582 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008583 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008584 // Unrotated video frame
8585 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8586 NotifyMotionArgs motionArgs;
8587
8588 // Test all 4 orientations
8589 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008590 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
8591 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
8592 clearViewports();
8593 prepareDisplay(orientation);
8594 std::vector<TouchVideoFrame> frames{frame};
8595 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
8596 processPosition(mapper, 100, 200);
8597 processSync(mapper);
8598 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8599 ASSERT_EQ(frames, motionArgs.videoFrames);
8600 }
8601}
8602
8603TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_AreRotated) {
8604 prepareAxes(POSITION);
8605 addConfigurationProperty("touch.deviceType", "touchScreen");
8606 // Since InputReader works in the un-rotated coordinate space, only devices that are not
8607 // orientation-aware are affected by display rotation.
8608 addConfigurationProperty("touch.orientationAware", "0");
8609 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8610 // Unrotated video frame
8611 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8612 NotifyMotionArgs motionArgs;
8613
8614 // Test all 4 orientations
8615 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008616 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
8617 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
8618 clearViewports();
8619 prepareDisplay(orientation);
8620 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008621 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008622 processPosition(mapper, 100, 200);
8623 processSync(mapper);
8624 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008625 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
8626 // compared to the display. This is so that when the window transform (which contains the
8627 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
8628 // window's coordinate space.
8629 frames[0].rotate(getInverseRotation(orientation));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008630 ASSERT_EQ(frames, motionArgs.videoFrames);
8631 }
8632}
8633
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008634TEST_F(MultiTouchInputMapperTest, VideoFrames_MultipleFramesAreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008635 prepareAxes(POSITION);
8636 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008637 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008638 // Unrotated video frames. There's no rule that they must all have the same dimensions,
8639 // so mix these.
8640 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8641 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
8642 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
8643 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
8644 NotifyMotionArgs motionArgs;
8645
8646 prepareDisplay(DISPLAY_ORIENTATION_90);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008647 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008648 processPosition(mapper, 100, 200);
8649 processSync(mapper);
8650 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008651 ASSERT_EQ(frames, motionArgs.videoFrames);
8652}
8653
8654TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_MultipleFramesAreRotated) {
8655 prepareAxes(POSITION);
8656 addConfigurationProperty("touch.deviceType", "touchScreen");
8657 // Since InputReader works in the un-rotated coordinate space, only devices that are not
8658 // orientation-aware are affected by display rotation.
8659 addConfigurationProperty("touch.orientationAware", "0");
8660 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8661 // Unrotated video frames. There's no rule that they must all have the same dimensions,
8662 // so mix these.
8663 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8664 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
8665 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
8666 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
8667 NotifyMotionArgs motionArgs;
8668
8669 prepareDisplay(DISPLAY_ORIENTATION_90);
8670 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
8671 processPosition(mapper, 100, 200);
8672 processSync(mapper);
8673 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8674 std::for_each(frames.begin(), frames.end(), [](TouchVideoFrame& frame) {
8675 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
8676 // compared to the display. This is so that when the window transform (which contains the
8677 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
8678 // window's coordinate space.
8679 frame.rotate(getInverseRotation(DISPLAY_ORIENTATION_90));
8680 });
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008681 ASSERT_EQ(frames, motionArgs.videoFrames);
8682}
8683
Arthur Hung9da14732019-09-02 16:16:58 +08008684/**
8685 * If we had defined port associations, but the viewport is not ready, the touch device would be
8686 * expected to be disabled, and it should be enabled after the viewport has found.
8687 */
8688TEST_F(MultiTouchInputMapperTest, Configure_EnabledForAssociatedDisplay) {
Arthur Hung9da14732019-09-02 16:16:58 +08008689 constexpr uint8_t hdmi2 = 1;
8690 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008691 constexpr ViewportType type = ViewportType::EXTERNAL;
Arthur Hung9da14732019-09-02 16:16:58 +08008692
8693 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi2);
8694
8695 addConfigurationProperty("touch.deviceType", "touchScreen");
8696 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008697 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung9da14732019-09-02 16:16:58 +08008698
8699 ASSERT_EQ(mDevice->isEnabled(), false);
8700
8701 // Add display on hdmi2, the device should be enabled and can receive touch event.
8702 prepareSecondaryDisplay(type, hdmi2);
8703 ASSERT_EQ(mDevice->isEnabled(), true);
8704
8705 // Send a touch event.
8706 processPosition(mapper, 100, 100);
8707 processSync(mapper);
8708
8709 NotifyMotionArgs args;
8710 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8711 ASSERT_EQ(SECONDARY_DISPLAY_ID, args.displayId);
8712}
8713
Arthur Hung421eb1c2020-01-16 00:09:42 +08008714TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleSingleTouch) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08008715 addConfigurationProperty("touch.deviceType", "touchScreen");
8716 prepareDisplay(DISPLAY_ORIENTATION_0);
8717 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008718 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08008719
8720 NotifyMotionArgs motionArgs;
8721
8722 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
8723 // finger down
8724 processId(mapper, 1);
8725 processPosition(mapper, x1, y1);
8726 processSync(mapper);
8727 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8728 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8729 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8730
8731 // finger move
8732 processId(mapper, 1);
8733 processPosition(mapper, x2, y2);
8734 processSync(mapper);
8735 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8736 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8737 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8738
8739 // finger up.
8740 processId(mapper, -1);
8741 processSync(mapper);
8742 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8743 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8744 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8745
8746 // new finger down
8747 processId(mapper, 1);
8748 processPosition(mapper, x3, y3);
8749 processSync(mapper);
8750 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8751 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8752 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8753}
8754
8755/**
arthurhungcc7f9802020-04-30 17:55:40 +08008756 * Test single touch should be canceled when received the MT_TOOL_PALM event, and the following
8757 * MOVE and UP events should be ignored.
Arthur Hung421eb1c2020-01-16 00:09:42 +08008758 */
arthurhungcc7f9802020-04-30 17:55:40 +08008759TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_SinglePointer) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08008760 addConfigurationProperty("touch.deviceType", "touchScreen");
8761 prepareDisplay(DISPLAY_ORIENTATION_0);
8762 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008763 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08008764
8765 NotifyMotionArgs motionArgs;
8766
8767 // default tool type is finger
8768 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
arthurhungcc7f9802020-04-30 17:55:40 +08008769 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008770 processPosition(mapper, x1, y1);
8771 processSync(mapper);
8772 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8773 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8774 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8775
8776 // Tool changed to MT_TOOL_PALM expect sending the cancel event.
8777 processToolType(mapper, MT_TOOL_PALM);
8778 processSync(mapper);
8779 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8780 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
8781
8782 // Ignore the following MOVE and UP events if had detect a palm event.
arthurhungcc7f9802020-04-30 17:55:40 +08008783 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008784 processPosition(mapper, x2, y2);
8785 processSync(mapper);
8786 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8787
8788 // finger up.
arthurhungcc7f9802020-04-30 17:55:40 +08008789 processId(mapper, INVALID_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008790 processSync(mapper);
8791 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8792
8793 // new finger down
arthurhungcc7f9802020-04-30 17:55:40 +08008794 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008795 processToolType(mapper, MT_TOOL_FINGER);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008796 processPosition(mapper, x3, y3);
8797 processSync(mapper);
8798 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8799 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8800 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8801}
8802
arthurhungbf89a482020-04-17 17:37:55 +08008803/**
arthurhungcc7f9802020-04-30 17:55:40 +08008804 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
8805 * and the rest active fingers could still be allowed to receive the events
arthurhungbf89a482020-04-17 17:37:55 +08008806 */
arthurhungcc7f9802020-04-30 17:55:40 +08008807TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_TwoPointers) {
arthurhungbf89a482020-04-17 17:37:55 +08008808 addConfigurationProperty("touch.deviceType", "touchScreen");
8809 prepareDisplay(DISPLAY_ORIENTATION_0);
8810 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
8811 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8812
8813 NotifyMotionArgs motionArgs;
8814
8815 // default tool type is finger
arthurhungcc7f9802020-04-30 17:55:40 +08008816 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
8817 processId(mapper, FIRST_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08008818 processPosition(mapper, x1, y1);
8819 processSync(mapper);
8820 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8821 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8822 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8823
8824 // Second finger down.
arthurhungcc7f9802020-04-30 17:55:40 +08008825 processSlot(mapper, SECOND_SLOT);
8826 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08008827 processPosition(mapper, x2, y2);
arthurhungcc7f9802020-04-30 17:55:40 +08008828 processSync(mapper);
8829 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008830 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08008831 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8832
8833 // If the tool type of the first finger changes to MT_TOOL_PALM,
8834 // we expect to receive ACTION_POINTER_UP with cancel flag.
8835 processSlot(mapper, FIRST_SLOT);
8836 processId(mapper, FIRST_TRACKING_ID);
8837 processToolType(mapper, MT_TOOL_PALM);
8838 processSync(mapper);
8839 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008840 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08008841 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
8842
8843 // The following MOVE events of second finger should be processed.
8844 processSlot(mapper, SECOND_SLOT);
8845 processId(mapper, SECOND_TRACKING_ID);
8846 processPosition(mapper, x2 + 1, y2 + 1);
8847 processSync(mapper);
8848 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8849 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8850 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8851
8852 // First finger up. It used to be in palm mode, and we already generated ACTION_POINTER_UP for
8853 // it. Second finger receive move.
8854 processSlot(mapper, FIRST_SLOT);
8855 processId(mapper, INVALID_TRACKING_ID);
8856 processSync(mapper);
8857 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8858 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8859 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8860
8861 // Second finger keeps moving.
8862 processSlot(mapper, SECOND_SLOT);
8863 processId(mapper, SECOND_TRACKING_ID);
8864 processPosition(mapper, x2 + 2, y2 + 2);
8865 processSync(mapper);
8866 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8867 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8868 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8869
8870 // Second finger up.
8871 processId(mapper, INVALID_TRACKING_ID);
8872 processSync(mapper);
8873 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8874 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8875 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
8876}
8877
8878/**
8879 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event, if only 1 finger
8880 * is active, it should send CANCEL after receiving the MT_TOOL_PALM event.
8881 */
8882TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_ShouldCancelWhenAllTouchIsPalm) {
8883 addConfigurationProperty("touch.deviceType", "touchScreen");
8884 prepareDisplay(DISPLAY_ORIENTATION_0);
8885 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
8886 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8887
8888 NotifyMotionArgs motionArgs;
8889
8890 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
8891 // First finger down.
8892 processId(mapper, FIRST_TRACKING_ID);
8893 processPosition(mapper, x1, y1);
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 // Second finger down.
8900 processSlot(mapper, SECOND_SLOT);
8901 processId(mapper, SECOND_TRACKING_ID);
8902 processPosition(mapper, x2, y2);
arthurhungbf89a482020-04-17 17:37:55 +08008903 processSync(mapper);
8904 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008905 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungbf89a482020-04-17 17:37:55 +08008906 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8907
arthurhungcc7f9802020-04-30 17:55:40 +08008908 // If the tool type of the first finger changes to MT_TOOL_PALM,
8909 // we expect to receive ACTION_POINTER_UP with cancel flag.
8910 processSlot(mapper, FIRST_SLOT);
8911 processId(mapper, FIRST_TRACKING_ID);
8912 processToolType(mapper, MT_TOOL_PALM);
8913 processSync(mapper);
8914 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008915 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08008916 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
8917
8918 // Second finger keeps moving.
8919 processSlot(mapper, SECOND_SLOT);
8920 processId(mapper, SECOND_TRACKING_ID);
8921 processPosition(mapper, x2 + 1, y2 + 1);
8922 processSync(mapper);
8923 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8924 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8925
8926 // second finger becomes palm, receive cancel due to only 1 finger is active.
8927 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08008928 processToolType(mapper, MT_TOOL_PALM);
8929 processSync(mapper);
8930 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8931 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
8932
arthurhungcc7f9802020-04-30 17:55:40 +08008933 // third finger down.
8934 processSlot(mapper, THIRD_SLOT);
8935 processId(mapper, THIRD_TRACKING_ID);
8936 processToolType(mapper, MT_TOOL_FINGER);
arthurhungbf89a482020-04-17 17:37:55 +08008937 processPosition(mapper, x3, y3);
8938 processSync(mapper);
arthurhungbf89a482020-04-17 17:37:55 +08008939 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8940 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8941 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08008942 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8943
8944 // third finger move
8945 processId(mapper, THIRD_TRACKING_ID);
8946 processPosition(mapper, x3 + 1, y3 + 1);
8947 processSync(mapper);
8948 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8949 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8950
8951 // first finger up, third finger receive move.
8952 processSlot(mapper, FIRST_SLOT);
8953 processId(mapper, INVALID_TRACKING_ID);
8954 processSync(mapper);
8955 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8956 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8957 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8958
8959 // second finger up, third finger receive move.
8960 processSlot(mapper, SECOND_SLOT);
8961 processId(mapper, INVALID_TRACKING_ID);
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 // third finger up.
8968 processSlot(mapper, THIRD_SLOT);
8969 processId(mapper, INVALID_TRACKING_ID);
8970 processSync(mapper);
8971 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8972 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8973 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
8974}
8975
8976/**
8977 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
8978 * and the active finger could still be allowed to receive the events
8979 */
8980TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_KeepFirstPointer) {
8981 addConfigurationProperty("touch.deviceType", "touchScreen");
8982 prepareDisplay(DISPLAY_ORIENTATION_0);
8983 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
8984 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8985
8986 NotifyMotionArgs motionArgs;
8987
8988 // default tool type is finger
8989 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
8990 processId(mapper, FIRST_TRACKING_ID);
8991 processPosition(mapper, x1, y1);
8992 processSync(mapper);
8993 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8994 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8995 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8996
8997 // Second finger down.
8998 processSlot(mapper, SECOND_SLOT);
8999 processId(mapper, SECOND_TRACKING_ID);
9000 processPosition(mapper, x2, y2);
9001 processSync(mapper);
9002 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009003 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009004 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9005
9006 // If the tool type of the second finger changes to MT_TOOL_PALM,
9007 // we expect to receive ACTION_POINTER_UP with cancel flag.
9008 processId(mapper, SECOND_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_1_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009013 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9014
9015 // The following MOVE event should be processed.
9016 processSlot(mapper, FIRST_SLOT);
9017 processId(mapper, FIRST_TRACKING_ID);
9018 processPosition(mapper, x1 + 1, y1 + 1);
9019 processSync(mapper);
9020 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9021 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9022 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9023
9024 // second finger up.
9025 processSlot(mapper, SECOND_SLOT);
9026 processId(mapper, INVALID_TRACKING_ID);
9027 processSync(mapper);
9028 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9029 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9030
9031 // first finger keep moving
9032 processSlot(mapper, FIRST_SLOT);
9033 processId(mapper, FIRST_TRACKING_ID);
9034 processPosition(mapper, x1 + 2, y1 + 2);
9035 processSync(mapper);
9036 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9037 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9038
9039 // first finger up.
9040 processId(mapper, INVALID_TRACKING_ID);
9041 processSync(mapper);
9042 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9043 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9044 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
arthurhungbf89a482020-04-17 17:37:55 +08009045}
9046
Arthur Hung9ad18942021-06-19 02:04:46 +00009047/**
9048 * Test multi-touch should sent ACTION_POINTER_UP/ACTION_UP when received the INVALID_TRACKING_ID,
9049 * to prevent the driver side may send unexpected data after set tracking id as INVALID_TRACKING_ID
9050 * cause slot be valid again.
9051 */
9052TEST_F(MultiTouchInputMapperTest, Process_MultiTouch_WithInvalidTrackingId) {
9053 addConfigurationProperty("touch.deviceType", "touchScreen");
9054 prepareDisplay(DISPLAY_ORIENTATION_0);
9055 prepareAxes(POSITION | ID | SLOT | PRESSURE);
9056 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9057
9058 NotifyMotionArgs motionArgs;
9059
9060 constexpr int32_t x1 = 100, y1 = 200, x2 = 0, y2 = 0;
9061 // First finger down.
9062 processId(mapper, FIRST_TRACKING_ID);
9063 processPosition(mapper, x1, y1);
9064 processPressure(mapper, RAW_PRESSURE_MAX);
9065 processSync(mapper);
9066 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9067 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9068 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9069
9070 // First finger move.
9071 processId(mapper, FIRST_TRACKING_ID);
9072 processPosition(mapper, x1 + 1, y1 + 1);
9073 processPressure(mapper, RAW_PRESSURE_MAX);
9074 processSync(mapper);
9075 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9076 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9077 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9078
9079 // Second finger down.
9080 processSlot(mapper, SECOND_SLOT);
9081 processId(mapper, SECOND_TRACKING_ID);
9082 processPosition(mapper, x2, y2);
9083 processPressure(mapper, RAW_PRESSURE_MAX);
9084 processSync(mapper);
9085 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009086 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Arthur Hung9ad18942021-06-19 02:04:46 +00009087 ASSERT_EQ(uint32_t(2), motionArgs.pointerCount);
9088
9089 // second finger up with some unexpected data.
9090 processSlot(mapper, SECOND_SLOT);
9091 processId(mapper, INVALID_TRACKING_ID);
9092 processPosition(mapper, x2, y2);
9093 processSync(mapper);
9094 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009095 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Arthur Hung9ad18942021-06-19 02:04:46 +00009096 ASSERT_EQ(uint32_t(2), motionArgs.pointerCount);
9097
9098 // first finger up with some unexpected data.
9099 processSlot(mapper, FIRST_SLOT);
9100 processId(mapper, INVALID_TRACKING_ID);
9101 processPosition(mapper, x2, y2);
9102 processPressure(mapper, RAW_PRESSURE_MAX);
9103 processSync(mapper);
9104 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9105 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9106 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9107}
9108
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009109// --- MultiTouchInputMapperTest_ExternalDevice ---
9110
9111class MultiTouchInputMapperTest_ExternalDevice : public MultiTouchInputMapperTest {
9112protected:
Chris Yea52ade12020-08-27 16:49:20 -07009113 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009114};
9115
9116/**
9117 * Expect fallback to internal viewport if device is external and external viewport is not present.
9118 */
9119TEST_F(MultiTouchInputMapperTest_ExternalDevice, Viewports_Fallback) {
9120 prepareAxes(POSITION);
9121 addConfigurationProperty("touch.deviceType", "touchScreen");
9122 prepareDisplay(DISPLAY_ORIENTATION_0);
9123 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9124
9125 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
9126
9127 NotifyMotionArgs motionArgs;
9128
9129 // Expect the event to be sent to the internal viewport,
9130 // because an external viewport is not present.
9131 processPosition(mapper, 100, 100);
9132 processSync(mapper);
9133 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9134 ASSERT_EQ(ADISPLAY_ID_DEFAULT, motionArgs.displayId);
9135
9136 // Expect the event to be sent to the external viewport if it is present.
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009137 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009138 processPosition(mapper, 100, 100);
9139 processSync(mapper);
9140 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9141 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
9142}
Arthur Hung4197f6b2020-03-16 15:39:59 +08009143
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009144TEST_F(MultiTouchInputMapperTest, Process_TouchpadCapture) {
9145 // we need a pointer controller for mouse mode of touchpad (start pointer at 0,0)
9146 std::shared_ptr<FakePointerController> fakePointerController =
9147 std::make_shared<FakePointerController>();
9148 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
9149 fakePointerController->setPosition(0, 0);
9150 fakePointerController->setButtonState(0);
9151
9152 // prepare device and capture
9153 prepareDisplay(DISPLAY_ORIENTATION_0);
9154 prepareAxes(POSITION | ID | SLOT);
9155 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
9156 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
9157 mFakePolicy->setPointerCapture(true);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009158 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009159 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9160
9161 // captured touchpad should be a touchpad source
9162 NotifyDeviceResetArgs resetArgs;
9163 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
9164 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
9165
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009166 InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
Chris Yef74dc422020-09-02 22:41:50 -07009167
9168 const InputDeviceInfo::MotionRange* relRangeX =
9169 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_X, AINPUT_SOURCE_TOUCHPAD);
9170 ASSERT_NE(relRangeX, nullptr);
9171 ASSERT_EQ(relRangeX->min, -(RAW_X_MAX - RAW_X_MIN));
9172 ASSERT_EQ(relRangeX->max, RAW_X_MAX - RAW_X_MIN);
9173 const InputDeviceInfo::MotionRange* relRangeY =
9174 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_Y, AINPUT_SOURCE_TOUCHPAD);
9175 ASSERT_NE(relRangeY, nullptr);
9176 ASSERT_EQ(relRangeY->min, -(RAW_Y_MAX - RAW_Y_MIN));
9177 ASSERT_EQ(relRangeY->max, RAW_Y_MAX - RAW_Y_MIN);
9178
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009179 // run captured pointer tests - note that this is unscaled, so input listener events should be
9180 // identical to what the hardware sends (accounting for any
9181 // calibration).
9182 // FINGER 0 DOWN
Chris Ye364fdb52020-08-05 15:07:56 -07009183 processSlot(mapper, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009184 processId(mapper, 1);
9185 processPosition(mapper, 100 + RAW_X_MIN, 100 + RAW_Y_MIN);
9186 processKey(mapper, BTN_TOUCH, 1);
9187 processSync(mapper);
9188
9189 // expect coord[0] to contain initial location of touch 0
9190 NotifyMotionArgs args;
9191 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9192 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
9193 ASSERT_EQ(1U, args.pointerCount);
9194 ASSERT_EQ(0, args.pointerProperties[0].id);
9195 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, args.source);
9196 ASSERT_NO_FATAL_FAILURE(
9197 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
9198
9199 // FINGER 1 DOWN
9200 processSlot(mapper, 1);
9201 processId(mapper, 2);
9202 processPosition(mapper, 560 + RAW_X_MIN, 154 + RAW_Y_MIN);
9203 processSync(mapper);
9204
9205 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
9206 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009207 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009208 ASSERT_EQ(2U, args.pointerCount);
9209 ASSERT_EQ(0, args.pointerProperties[0].id);
9210 ASSERT_EQ(1, args.pointerProperties[1].id);
9211 ASSERT_NO_FATAL_FAILURE(
9212 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
9213 ASSERT_NO_FATAL_FAILURE(
9214 assertPointerCoords(args.pointerCoords[1], 560, 154, 1, 0, 0, 0, 0, 0, 0, 0));
9215
9216 // FINGER 1 MOVE
9217 processPosition(mapper, 540 + RAW_X_MIN, 690 + RAW_Y_MIN);
9218 processSync(mapper);
9219
9220 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
9221 // from move
9222 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9223 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9224 ASSERT_NO_FATAL_FAILURE(
9225 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
9226 ASSERT_NO_FATAL_FAILURE(
9227 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
9228
9229 // FINGER 0 MOVE
9230 processSlot(mapper, 0);
9231 processPosition(mapper, 50 + RAW_X_MIN, 800 + RAW_Y_MIN);
9232 processSync(mapper);
9233
9234 // expect coord[0] to contain new touch 0 location, coord[1] to contain previous location
9235 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9236 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9237 ASSERT_NO_FATAL_FAILURE(
9238 assertPointerCoords(args.pointerCoords[0], 50, 800, 1, 0, 0, 0, 0, 0, 0, 0));
9239 ASSERT_NO_FATAL_FAILURE(
9240 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
9241
9242 // BUTTON DOWN
9243 processKey(mapper, BTN_LEFT, 1);
9244 processSync(mapper);
9245
9246 // touchinputmapper design sends a move before button press
9247 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9248 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9249 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9250 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
9251
9252 // BUTTON UP
9253 processKey(mapper, BTN_LEFT, 0);
9254 processSync(mapper);
9255
9256 // touchinputmapper design sends a move after button release
9257 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9258 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
9259 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9260 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9261
9262 // FINGER 0 UP
9263 processId(mapper, -1);
9264 processSync(mapper);
9265 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9266 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | 0x0000, args.action);
9267
9268 // FINGER 1 MOVE
9269 processSlot(mapper, 1);
9270 processPosition(mapper, 320 + RAW_X_MIN, 900 + RAW_Y_MIN);
9271 processSync(mapper);
9272
9273 // expect coord[0] to contain new location of touch 1, and properties[0].id to contain 1
9274 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9275 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9276 ASSERT_EQ(1U, args.pointerCount);
9277 ASSERT_EQ(1, args.pointerProperties[0].id);
9278 ASSERT_NO_FATAL_FAILURE(
9279 assertPointerCoords(args.pointerCoords[0], 320, 900, 1, 0, 0, 0, 0, 0, 0, 0));
9280
9281 // FINGER 1 UP
9282 processId(mapper, -1);
9283 processKey(mapper, BTN_TOUCH, 0);
9284 processSync(mapper);
9285 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9286 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
9287
9288 // non captured touchpad should be a mouse source
9289 mFakePolicy->setPointerCapture(false);
9290 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
9291 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
9292 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
9293}
9294
9295TEST_F(MultiTouchInputMapperTest, Process_UnCapturedTouchpadPointer) {
9296 std::shared_ptr<FakePointerController> fakePointerController =
9297 std::make_shared<FakePointerController>();
9298 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
9299 fakePointerController->setPosition(0, 0);
9300 fakePointerController->setButtonState(0);
9301
9302 // prepare device and capture
9303 prepareDisplay(DISPLAY_ORIENTATION_0);
9304 prepareAxes(POSITION | ID | SLOT);
9305 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
9306 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009307 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009308 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9309 // run uncaptured pointer tests - pushes out generic events
9310 // FINGER 0 DOWN
9311 processId(mapper, 3);
9312 processPosition(mapper, 100, 100);
9313 processKey(mapper, BTN_TOUCH, 1);
9314 processSync(mapper);
9315
9316 // start at (100,100), cursor should be at (0,0) * scale
9317 NotifyMotionArgs args;
9318 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9319 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
9320 ASSERT_NO_FATAL_FAILURE(
9321 assertPointerCoords(args.pointerCoords[0], 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
9322
9323 // FINGER 0 MOVE
9324 processPosition(mapper, 200, 200);
9325 processSync(mapper);
9326
9327 // compute scaling to help with touch position checking
9328 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
9329 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
9330 float scale =
9331 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
9332
9333 // translate from (100,100) -> (200,200), cursor should have changed to (100,100) * scale)
9334 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9335 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
9336 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 100 * scale, 100 * scale, 0,
9337 0, 0, 0, 0, 0, 0, 0));
9338}
9339
9340TEST_F(MultiTouchInputMapperTest, WhenCapturedAndNotCaptured_GetSources) {
9341 std::shared_ptr<FakePointerController> fakePointerController =
9342 std::make_shared<FakePointerController>();
9343
9344 prepareDisplay(DISPLAY_ORIENTATION_0);
9345 prepareAxes(POSITION | ID | SLOT);
9346 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009347 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009348 mFakePolicy->setPointerCapture(false);
9349 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9350
9351 // uncaptured touchpad should be a pointer device
9352 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
9353
9354 // captured touchpad should be a touchpad device
9355 mFakePolicy->setPointerCapture(true);
9356 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
9357 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
9358}
9359
Arthur Hung6d5b4b22022-01-21 07:21:10 +00009360// --- JoystickInputMapperTest ---
9361
9362class JoystickInputMapperTest : public InputMapperTest {
9363protected:
9364 static const int32_t RAW_X_MIN;
9365 static const int32_t RAW_X_MAX;
9366 static const int32_t RAW_Y_MIN;
9367 static const int32_t RAW_Y_MAX;
9368
9369 void SetUp() override {
9370 InputMapperTest::SetUp(InputDeviceClass::JOYSTICK | InputDeviceClass::EXTERNAL);
9371 }
9372 void prepareAxes() {
9373 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
9374 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
9375 }
9376
9377 void processAxis(JoystickInputMapper& mapper, int32_t axis, int32_t value) {
9378 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, axis, value);
9379 }
9380
9381 void processSync(JoystickInputMapper& mapper) {
9382 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
9383 }
9384
9385 void prepareVirtualDisplay(int32_t orientation) {
9386 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH,
9387 VIRTUAL_DISPLAY_HEIGHT, orientation, VIRTUAL_DISPLAY_UNIQUE_ID,
9388 NO_PORT, ViewportType::VIRTUAL);
9389 }
9390};
9391
9392const int32_t JoystickInputMapperTest::RAW_X_MIN = -32767;
9393const int32_t JoystickInputMapperTest::RAW_X_MAX = 32767;
9394const int32_t JoystickInputMapperTest::RAW_Y_MIN = -32767;
9395const int32_t JoystickInputMapperTest::RAW_Y_MAX = 32767;
9396
9397TEST_F(JoystickInputMapperTest, Configure_AssignsDisplayUniqueId) {
9398 prepareAxes();
9399 JoystickInputMapper& mapper = addMapperAndConfigure<JoystickInputMapper>();
9400
9401 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
9402
9403 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
9404
9405 // Send an axis event
9406 processAxis(mapper, ABS_X, 100);
9407 processSync(mapper);
9408
9409 NotifyMotionArgs args;
9410 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9411 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
9412
9413 // Send another axis event
9414 processAxis(mapper, ABS_Y, 100);
9415 processSync(mapper);
9416
9417 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9418 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
9419}
9420
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009421// --- PeripheralControllerTest ---
Chris Yee2b1e5c2021-03-10 22:45:12 -08009422
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009423class PeripheralControllerTest : public testing::Test {
Chris Yee2b1e5c2021-03-10 22:45:12 -08009424protected:
9425 static const char* DEVICE_NAME;
9426 static const char* DEVICE_LOCATION;
9427 static const int32_t DEVICE_ID;
9428 static const int32_t DEVICE_GENERATION;
9429 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07009430 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Chris Yee2b1e5c2021-03-10 22:45:12 -08009431 static const int32_t EVENTHUB_ID;
9432
9433 std::shared_ptr<FakeEventHub> mFakeEventHub;
9434 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07009435 std::unique_ptr<TestInputListener> mFakeListener;
Chris Yee2b1e5c2021-03-10 22:45:12 -08009436 std::unique_ptr<InstrumentedInputReader> mReader;
9437 std::shared_ptr<InputDevice> mDevice;
9438
Dominik Laskowski2f01d772022-03-23 16:01:29 -07009439 virtual void SetUp(ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -08009440 mFakeEventHub = std::make_unique<FakeEventHub>();
9441 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07009442 mFakeListener = std::make_unique<TestInputListener>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009443 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07009444 *mFakeListener);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009445 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
9446 }
9447
9448 void SetUp() override { SetUp(DEVICE_CLASSES); }
9449
9450 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07009451 mFakeListener.reset();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009452 mFakePolicy.clear();
9453 }
9454
9455 void configureDevice(uint32_t changes) {
9456 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
9457 mReader->requestRefreshConfiguration(changes);
9458 mReader->loopOnce();
9459 }
9460 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
9461 }
9462
9463 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
9464 const std::string& location, int32_t eventHubId,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07009465 ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -08009466 InputDeviceIdentifier identifier;
9467 identifier.name = name;
9468 identifier.location = location;
9469 std::shared_ptr<InputDevice> device =
9470 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
9471 identifier);
9472 mReader->pushNextDevice(device);
9473 mFakeEventHub->addDevice(eventHubId, name, classes);
9474 mReader->loopOnce();
9475 return device;
9476 }
9477
9478 template <class T, typename... Args>
9479 T& addControllerAndConfigure(Args... args) {
9480 T& controller = mDevice->addController<T>(EVENTHUB_ID, args...);
9481
9482 return controller;
9483 }
9484};
9485
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009486const char* PeripheralControllerTest::DEVICE_NAME = "device";
9487const char* PeripheralControllerTest::DEVICE_LOCATION = "BLUETOOTH";
9488const int32_t PeripheralControllerTest::DEVICE_ID = END_RESERVED_ID + 1000;
9489const int32_t PeripheralControllerTest::DEVICE_GENERATION = 2;
9490const int32_t PeripheralControllerTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07009491const ftl::Flags<InputDeviceClass> PeripheralControllerTest::DEVICE_CLASSES =
9492 ftl::Flags<InputDeviceClass>(0); // not needed for current tests
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009493const int32_t PeripheralControllerTest::EVENTHUB_ID = 1;
Chris Yee2b1e5c2021-03-10 22:45:12 -08009494
9495// --- BatteryControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009496class BatteryControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -08009497protected:
9498 void SetUp() override {
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009499 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::BATTERY);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009500 }
9501};
9502
9503TEST_F(BatteryControllerTest, GetBatteryCapacity) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009504 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009505
9506 ASSERT_TRUE(controller.getBatteryCapacity(DEFAULT_BATTERY));
9507 ASSERT_EQ(controller.getBatteryCapacity(DEFAULT_BATTERY).value_or(-1), BATTERY_CAPACITY);
9508}
9509
9510TEST_F(BatteryControllerTest, GetBatteryStatus) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009511 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009512
9513 ASSERT_TRUE(controller.getBatteryStatus(DEFAULT_BATTERY));
9514 ASSERT_EQ(controller.getBatteryStatus(DEFAULT_BATTERY).value_or(-1), BATTERY_STATUS);
9515}
9516
9517// --- LightControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009518class LightControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -08009519protected:
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009520 void SetUp() override {
9521 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::LIGHT);
9522 }
Chris Yee2b1e5c2021-03-10 22:45:12 -08009523};
9524
Chris Ye85758332021-05-16 23:05:17 -07009525TEST_F(LightControllerTest, MonoLight) {
9526 RawLightInfo infoMono = {.id = 1,
9527 .name = "Mono",
9528 .maxBrightness = 255,
9529 .flags = InputLightClass::BRIGHTNESS,
9530 .path = ""};
9531 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
Chris Yee2b1e5c2021-03-10 22:45:12 -08009532
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009533 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009534 InputDeviceInfo info;
9535 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009536 std::vector<InputDeviceLightInfo> lights = info.getLights();
9537 ASSERT_EQ(1U, lights.size());
9538 ASSERT_EQ(InputDeviceLightType::MONO, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009539
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009540 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
9541 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009542}
9543
9544TEST_F(LightControllerTest, RGBLight) {
9545 RawLightInfo infoRed = {.id = 1,
9546 .name = "red",
9547 .maxBrightness = 255,
9548 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
9549 .path = ""};
9550 RawLightInfo infoGreen = {.id = 2,
9551 .name = "green",
9552 .maxBrightness = 255,
9553 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
9554 .path = ""};
9555 RawLightInfo infoBlue = {.id = 3,
9556 .name = "blue",
9557 .maxBrightness = 255,
9558 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
9559 .path = ""};
9560 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
9561 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
9562 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
9563
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009564 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009565 InputDeviceInfo info;
9566 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009567 std::vector<InputDeviceLightInfo> lights = info.getLights();
9568 ASSERT_EQ(1U, lights.size());
9569 ASSERT_EQ(InputDeviceLightType::RGB, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009570
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009571 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
9572 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009573}
9574
9575TEST_F(LightControllerTest, MultiColorRGBLight) {
9576 RawLightInfo infoColor = {.id = 1,
9577 .name = "red",
9578 .maxBrightness = 255,
9579 .flags = InputLightClass::BRIGHTNESS |
9580 InputLightClass::MULTI_INTENSITY |
9581 InputLightClass::MULTI_INDEX,
9582 .path = ""};
9583
9584 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
9585
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009586 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009587 InputDeviceInfo info;
9588 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009589 std::vector<InputDeviceLightInfo> lights = info.getLights();
9590 ASSERT_EQ(1U, lights.size());
9591 ASSERT_EQ(InputDeviceLightType::MULTI_COLOR, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009592
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009593 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
9594 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009595}
9596
9597TEST_F(LightControllerTest, PlayerIdLight) {
9598 RawLightInfo info1 = {.id = 1,
9599 .name = "player1",
9600 .maxBrightness = 255,
9601 .flags = InputLightClass::BRIGHTNESS,
9602 .path = ""};
9603 RawLightInfo info2 = {.id = 2,
9604 .name = "player2",
9605 .maxBrightness = 255,
9606 .flags = InputLightClass::BRIGHTNESS,
9607 .path = ""};
9608 RawLightInfo info3 = {.id = 3,
9609 .name = "player3",
9610 .maxBrightness = 255,
9611 .flags = InputLightClass::BRIGHTNESS,
9612 .path = ""};
9613 RawLightInfo info4 = {.id = 4,
9614 .name = "player4",
9615 .maxBrightness = 255,
9616 .flags = InputLightClass::BRIGHTNESS,
9617 .path = ""};
9618 mFakeEventHub->addRawLightInfo(info1.id, std::move(info1));
9619 mFakeEventHub->addRawLightInfo(info2.id, std::move(info2));
9620 mFakeEventHub->addRawLightInfo(info3.id, std::move(info3));
9621 mFakeEventHub->addRawLightInfo(info4.id, std::move(info4));
9622
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009623 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009624 InputDeviceInfo info;
9625 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009626 std::vector<InputDeviceLightInfo> lights = info.getLights();
9627 ASSERT_EQ(1U, lights.size());
9628 ASSERT_EQ(InputDeviceLightType::PLAYER_ID, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009629
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009630 ASSERT_FALSE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
9631 ASSERT_TRUE(controller.setLightPlayerId(lights[0].id, LIGHT_PLAYER_ID));
9632 ASSERT_EQ(controller.getLightPlayerId(lights[0].id).value_or(-1), LIGHT_PLAYER_ID);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009633}
9634
Michael Wrightd02c5b62014-02-10 15:10:22 -08009635} // namespace android