blob: aa188f2ffb117847ee14bbee36256362d9806fde [file] [log] [blame]
Michael Wrightd02c5b62014-02-10 15:10:22 -08001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Dominik Laskowski2f01d772022-03-23 16:01:29 -070017#include <cinttypes>
18#include <memory>
19
Prabir Pradhan2770d242019-09-02 18:07:11 -070020#include <CursorInputMapper.h>
21#include <InputDevice.h>
22#include <InputMapper.h>
23#include <InputReader.h>
Prabir Pradhan1aed8582019-12-30 11:46:51 -080024#include <InputReaderBase.h>
25#include <InputReaderFactory.h>
Arthur Hung6d5b4b22022-01-21 07:21:10 +000026#include <JoystickInputMapper.h>
Prabir Pradhan2770d242019-09-02 18:07:11 -070027#include <KeyboardInputMapper.h>
28#include <MultiTouchInputMapper.h>
Chris Ye1dd2e5c2021-04-04 23:12:41 -070029#include <PeripheralController.h>
Chris Yef59a2f42020-10-16 12:55:26 -070030#include <SensorInputMapper.h>
Prabir Pradhan2770d242019-09-02 18:07:11 -070031#include <SingleTouchInputMapper.h>
32#include <SwitchInputMapper.h>
33#include <TestInputListener.h>
34#include <TouchInputMapper.h>
Prabir Pradhan1aed8582019-12-30 11:46:51 -080035#include <UinputDevice.h>
Chris Ye87143712020-11-10 05:05:58 +000036#include <VibratorInputMapper.h>
Prabir Pradhan2574dfa2019-10-16 16:35:07 -070037#include <android-base/thread_annotations.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080038#include <gtest/gtest.h>
chaviw3277faf2021-05-19 16:45:23 -050039#include <gui/constants.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080040
Michael Wrightdde67b82020-10-27 16:09:22 +000041#include "input/DisplayViewport.h"
42#include "input/Input.h"
Michael Wright17db18e2020-06-26 20:51:44 +010043
Michael Wrightd02c5b62014-02-10 15:10:22 -080044namespace android {
45
Dominik Laskowski2f01d772022-03-23 16:01:29 -070046using namespace ftl::flag_operators;
47
Prabir Pradhan2574dfa2019-10-16 16:35:07 -070048using std::chrono_literals::operator""ms;
49
50// Timeout for waiting for an expected event
51static constexpr std::chrono::duration WAIT_TIMEOUT = 100ms;
52
Michael Wrightd02c5b62014-02-10 15:10:22 -080053// An arbitrary time value.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +000054static constexpr nsecs_t ARBITRARY_TIME = 1234;
55static constexpr nsecs_t READ_TIME = 4321;
Michael Wrightd02c5b62014-02-10 15:10:22 -080056
57// Arbitrary display properties.
arthurhungcc7f9802020-04-30 17:55:40 +080058static constexpr int32_t DISPLAY_ID = 0;
59static constexpr int32_t SECONDARY_DISPLAY_ID = DISPLAY_ID + 1;
60static constexpr int32_t DISPLAY_WIDTH = 480;
61static constexpr int32_t DISPLAY_HEIGHT = 800;
62static constexpr int32_t VIRTUAL_DISPLAY_ID = 1;
63static constexpr int32_t VIRTUAL_DISPLAY_WIDTH = 400;
64static constexpr int32_t VIRTUAL_DISPLAY_HEIGHT = 500;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -070065static const char* VIRTUAL_DISPLAY_UNIQUE_ID = "virtual:1";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -070066static constexpr std::optional<uint8_t> NO_PORT = std::nullopt; // no physical port is specified
Michael Wrightd02c5b62014-02-10 15:10:22 -080067
arthurhungcc7f9802020-04-30 17:55:40 +080068static constexpr int32_t FIRST_SLOT = 0;
69static constexpr int32_t SECOND_SLOT = 1;
70static constexpr int32_t THIRD_SLOT = 2;
71static constexpr int32_t INVALID_TRACKING_ID = -1;
72static constexpr int32_t FIRST_TRACKING_ID = 0;
73static constexpr int32_t SECOND_TRACKING_ID = 1;
74static constexpr int32_t THIRD_TRACKING_ID = 2;
Chris Yee2b1e5c2021-03-10 22:45:12 -080075static constexpr int32_t DEFAULT_BATTERY = 1;
Kim Low03ea0352020-11-06 12:45:07 -080076static constexpr int32_t BATTERY_STATUS = 4;
77static constexpr int32_t BATTERY_CAPACITY = 66;
Chris Ye3fdbfef2021-01-06 18:45:18 -080078static constexpr int32_t LIGHT_BRIGHTNESS = 0x55000000;
79static constexpr int32_t LIGHT_COLOR = 0x7F448866;
80static constexpr int32_t LIGHT_PLAYER_ID = 2;
arthurhungcc7f9802020-04-30 17:55:40 +080081
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080082static constexpr int32_t ACTION_POINTER_0_DOWN =
83 AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
84static constexpr int32_t ACTION_POINTER_0_UP =
85 AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
86static constexpr int32_t ACTION_POINTER_1_DOWN =
87 AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
88static constexpr int32_t ACTION_POINTER_1_UP =
89 AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
90
Michael Wrightd02c5b62014-02-10 15:10:22 -080091// Error tolerance for floating point assertions.
92static const float EPSILON = 0.001f;
93
94template<typename T>
95static inline T min(T a, T b) {
96 return a < b ? a : b;
97}
98
99static inline float avg(float x, float y) {
100 return (x + y) / 2;
101}
102
Chris Ye3fdbfef2021-01-06 18:45:18 -0800103// Mapping for light color name and the light color
104const std::unordered_map<std::string, LightColor> LIGHT_COLORS = {{"red", LightColor::RED},
105 {"green", LightColor::GREEN},
106 {"blue", LightColor::BLUE}};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800107
Prabir Pradhanc14266f2021-05-12 15:56:24 -0700108static int32_t getInverseRotation(int32_t orientation) {
109 switch (orientation) {
110 case DISPLAY_ORIENTATION_90:
111 return DISPLAY_ORIENTATION_270;
112 case DISPLAY_ORIENTATION_270:
113 return DISPLAY_ORIENTATION_90;
114 default:
115 return orientation;
116 }
117}
118
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800119static void assertAxisResolution(MultiTouchInputMapper& mapper, int axis, float resolution) {
120 InputDeviceInfo info;
121 mapper.populateDeviceInfo(&info);
122
123 const InputDeviceInfo::MotionRange* motionRange =
124 info.getMotionRange(axis, AINPUT_SOURCE_TOUCHSCREEN);
125 ASSERT_NEAR(motionRange->resolution, resolution, EPSILON);
126}
127
128static void assertAxisNotPresent(MultiTouchInputMapper& mapper, int axis) {
129 InputDeviceInfo info;
130 mapper.populateDeviceInfo(&info);
131
132 const InputDeviceInfo::MotionRange* motionRange =
133 info.getMotionRange(axis, AINPUT_SOURCE_TOUCHSCREEN);
134 ASSERT_EQ(nullptr, motionRange);
135}
136
Michael Wrightd02c5b62014-02-10 15:10:22 -0800137// --- FakePointerController ---
138
139class FakePointerController : public PointerControllerInterface {
140 bool mHaveBounds;
141 float mMinX, mMinY, mMaxX, mMaxY;
142 float mX, mY;
143 int32_t mButtonState;
Arthur Hungc7ad2d02018-12-18 17:41:29 +0800144 int32_t mDisplayId;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800145
Michael Wrightd02c5b62014-02-10 15:10:22 -0800146public:
147 FakePointerController() :
148 mHaveBounds(false), mMinX(0), mMinY(0), mMaxX(0), mMaxY(0), mX(0), mY(0),
Arthur Hungc7ad2d02018-12-18 17:41:29 +0800149 mButtonState(0), mDisplayId(ADISPLAY_ID_DEFAULT) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800150 }
151
Michael Wright17db18e2020-06-26 20:51:44 +0100152 virtual ~FakePointerController() {}
153
Michael Wrightd02c5b62014-02-10 15:10:22 -0800154 void setBounds(float minX, float minY, float maxX, float maxY) {
155 mHaveBounds = true;
156 mMinX = minX;
157 mMinY = minY;
158 mMaxX = maxX;
159 mMaxY = maxY;
160 }
161
Chris Yea52ade12020-08-27 16:49:20 -0700162 void setPosition(float x, float y) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800163 mX = x;
164 mY = y;
165 }
166
Chris Yea52ade12020-08-27 16:49:20 -0700167 void setButtonState(int32_t buttonState) override { mButtonState = buttonState; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800168
Chris Yea52ade12020-08-27 16:49:20 -0700169 int32_t getButtonState() const override { return mButtonState; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800170
Chris Yea52ade12020-08-27 16:49:20 -0700171 void getPosition(float* outX, float* outY) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800172 *outX = mX;
173 *outY = mY;
174 }
175
Chris Yea52ade12020-08-27 16:49:20 -0700176 int32_t getDisplayId() const override { return mDisplayId; }
Arthur Hungc7ad2d02018-12-18 17:41:29 +0800177
Chris Yea52ade12020-08-27 16:49:20 -0700178 void setDisplayViewport(const DisplayViewport& viewport) override {
Garfield Tan888a6a42020-01-09 11:39:16 -0800179 mDisplayId = viewport.displayId;
180 }
181
Arthur Hung7c645402019-01-25 17:45:42 +0800182 const std::map<int32_t, std::vector<int32_t>>& getSpots() {
183 return mSpotsByDisplay;
184 }
185
Michael Wrightd02c5b62014-02-10 15:10:22 -0800186private:
Chris Yea52ade12020-08-27 16:49:20 -0700187 bool getBounds(float* outMinX, float* outMinY, float* outMaxX, float* outMaxY) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800188 *outMinX = mMinX;
189 *outMinY = mMinY;
190 *outMaxX = mMaxX;
191 *outMaxY = mMaxY;
192 return mHaveBounds;
193 }
194
Chris Yea52ade12020-08-27 16:49:20 -0700195 void move(float deltaX, float deltaY) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800196 mX += deltaX;
197 if (mX < mMinX) mX = mMinX;
198 if (mX > mMaxX) mX = mMaxX;
199 mY += deltaY;
200 if (mY < mMinY) mY = mMinY;
201 if (mY > mMaxY) mY = mMaxY;
202 }
203
Chris Yea52ade12020-08-27 16:49:20 -0700204 void fade(Transition) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800205
Chris Yea52ade12020-08-27 16:49:20 -0700206 void unfade(Transition) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800207
Chris Yea52ade12020-08-27 16:49:20 -0700208 void setPresentation(Presentation) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800209
Chris Yea52ade12020-08-27 16:49:20 -0700210 void setSpots(const PointerCoords*, const uint32_t*, BitSet32 spotIdBits,
211 int32_t displayId) override {
Arthur Hung7c645402019-01-25 17:45:42 +0800212 std::vector<int32_t> newSpots;
213 // Add spots for fingers that are down.
214 for (BitSet32 idBits(spotIdBits); !idBits.isEmpty(); ) {
215 uint32_t id = idBits.clearFirstMarkedBit();
216 newSpots.push_back(id);
217 }
218
219 mSpotsByDisplay[displayId] = newSpots;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800220 }
221
Chris Yea52ade12020-08-27 16:49:20 -0700222 void clearSpots() override {}
Arthur Hung7c645402019-01-25 17:45:42 +0800223
224 std::map<int32_t, std::vector<int32_t>> mSpotsByDisplay;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800225};
226
227
228// --- FakeInputReaderPolicy ---
229
230class FakeInputReaderPolicy : public InputReaderPolicyInterface {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700231 std::mutex mLock;
232 std::condition_variable mDevicesChangedCondition;
233
Michael Wrightd02c5b62014-02-10 15:10:22 -0800234 InputReaderConfiguration mConfig;
Prabir Pradhan2853b7a2021-08-23 14:08:51 +0000235 std::shared_ptr<FakePointerController> mPointerController;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700236 std::vector<InputDeviceInfo> mInputDevices GUARDED_BY(mLock);
237 bool mInputDevicesChanged GUARDED_BY(mLock){false};
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100238 std::vector<DisplayViewport> mViewports;
Jason Gerecke489fda82012-09-07 17:19:40 -0700239 TouchAffineTransformation transform;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800240
241protected:
Chris Yea52ade12020-08-27 16:49:20 -0700242 virtual ~FakeInputReaderPolicy() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800243
244public:
245 FakeInputReaderPolicy() {
246 }
247
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700248 void assertInputDevicesChanged() {
Prabir Pradhan1aed8582019-12-30 11:46:51 -0800249 waitForInputDevices([](bool devicesChanged) {
250 if (!devicesChanged) {
251 FAIL() << "Timed out waiting for notifyInputDevicesChanged() to be called.";
252 }
253 });
254 }
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700255
Prabir Pradhan1aed8582019-12-30 11:46:51 -0800256 void assertInputDevicesNotChanged() {
257 waitForInputDevices([](bool devicesChanged) {
258 if (devicesChanged) {
259 FAIL() << "Expected notifyInputDevicesChanged() to not be called.";
260 }
261 });
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700262 }
263
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700264 virtual void clearViewports() {
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100265 mViewports.clear();
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100266 mConfig.setDisplayViewports(mViewports);
Santos Cordonfa5cf462017-04-05 10:37:00 -0700267 }
268
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700269 std::optional<DisplayViewport> getDisplayViewportByUniqueId(const std::string& uniqueId) const {
270 return mConfig.getDisplayViewportByUniqueId(uniqueId);
271 }
272 std::optional<DisplayViewport> getDisplayViewportByType(ViewportType type) const {
273 return mConfig.getDisplayViewportByType(type);
274 }
275
276 std::optional<DisplayViewport> getDisplayViewportByPort(uint8_t displayPort) const {
277 return mConfig.getDisplayViewportByPort(displayPort);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700278 }
279
Prabir Pradhan5632d622021-09-06 07:57:20 -0700280 void addDisplayViewport(DisplayViewport viewport) {
281 mViewports.push_back(std::move(viewport));
282 mConfig.setDisplayViewports(mViewports);
283 }
284
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700285 void addDisplayViewport(int32_t displayId, int32_t width, int32_t height, int32_t orientation,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +0000286 bool isActive, const std::string& uniqueId,
Prabir Pradhan5632d622021-09-06 07:57:20 -0700287 std::optional<uint8_t> physicalPort, ViewportType type) {
288 const bool isRotated =
289 (orientation == DISPLAY_ORIENTATION_90 || orientation == DISPLAY_ORIENTATION_270);
290 DisplayViewport v;
291 v.displayId = displayId;
292 v.orientation = orientation;
293 v.logicalLeft = 0;
294 v.logicalTop = 0;
295 v.logicalRight = isRotated ? height : width;
296 v.logicalBottom = isRotated ? width : height;
297 v.physicalLeft = 0;
298 v.physicalTop = 0;
299 v.physicalRight = isRotated ? height : width;
300 v.physicalBottom = isRotated ? width : height;
301 v.deviceWidth = isRotated ? height : width;
302 v.deviceHeight = isRotated ? width : height;
303 v.isActive = isActive;
304 v.uniqueId = uniqueId;
305 v.physicalPort = physicalPort;
306 v.type = type;
307
308 addDisplayViewport(v);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800309 }
310
Arthur Hung6cd19a42019-08-30 19:04:12 +0800311 bool updateViewport(const DisplayViewport& viewport) {
312 size_t count = mViewports.size();
313 for (size_t i = 0; i < count; i++) {
314 const DisplayViewport& currentViewport = mViewports[i];
315 if (currentViewport.displayId == viewport.displayId) {
316 mViewports[i] = viewport;
317 mConfig.setDisplayViewports(mViewports);
318 return true;
319 }
320 }
321 // no viewport found.
322 return false;
323 }
324
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100325 void addExcludedDeviceName(const std::string& deviceName) {
326 mConfig.excludedDeviceNames.push_back(deviceName);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800327 }
328
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700329 void addInputPortAssociation(const std::string& inputPort, uint8_t displayPort) {
330 mConfig.portAssociations.insert({inputPort, displayPort});
331 }
332
Christine Franks1ba71cc2021-04-07 14:37:42 -0700333 void addInputUniqueIdAssociation(const std::string& inputUniqueId,
334 const std::string& displayUniqueId) {
335 mConfig.uniqueIdAssociations.insert({inputUniqueId, displayUniqueId});
336 }
337
Siarhei Vishniakouc6f61192019-07-23 18:12:31 +0000338 void addDisabledDevice(int32_t deviceId) { mConfig.disabledDevices.insert(deviceId); }
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700339
Siarhei Vishniakouc6f61192019-07-23 18:12:31 +0000340 void removeDisabledDevice(int32_t deviceId) { mConfig.disabledDevices.erase(deviceId); }
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700341
Prabir Pradhan2853b7a2021-08-23 14:08:51 +0000342 void setPointerController(std::shared_ptr<FakePointerController> controller) {
343 mPointerController = std::move(controller);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800344 }
345
346 const InputReaderConfiguration* getReaderConfiguration() const {
347 return &mConfig;
348 }
349
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800350 const std::vector<InputDeviceInfo>& getInputDevices() const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800351 return mInputDevices;
352 }
353
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100354 TouchAffineTransformation getTouchAffineTransformation(const std::string& inputDeviceDescriptor,
Jason Gerecke71b16e82014-03-10 09:47:59 -0700355 int32_t surfaceRotation) {
Jason Gerecke489fda82012-09-07 17:19:40 -0700356 return transform;
357 }
358
359 void setTouchAffineTransformation(const TouchAffineTransformation t) {
360 transform = t;
Jason Gerecke12d6baa2014-01-27 18:34:20 -0800361 }
362
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000363 PointerCaptureRequest setPointerCapture(bool enabled) {
364 mConfig.pointerCaptureRequest = {enabled, mNextPointerCaptureSequenceNumber++};
365 return mConfig.pointerCaptureRequest;
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -0800366 }
367
Arthur Hung7c645402019-01-25 17:45:42 +0800368 void setShowTouches(bool enabled) {
369 mConfig.showTouches = enabled;
370 }
371
Garfield Tan888a6a42020-01-09 11:39:16 -0800372 void setDefaultPointerDisplayId(int32_t pointerDisplayId) {
373 mConfig.defaultPointerDisplayId = pointerDisplayId;
374 }
375
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -0800376 float getPointerGestureMovementSpeedRatio() { return mConfig.pointerGestureMovementSpeedRatio; }
377
Prabir Pradhanf99d6e72022-04-21 15:28:35 +0000378 void setVelocityControlParams(const VelocityControlParameters& params) {
379 mConfig.pointerVelocityControlParameters = params;
380 mConfig.wheelVelocityControlParameters = params;
381 }
382
Michael Wrightd02c5b62014-02-10 15:10:22 -0800383private:
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000384 uint32_t mNextPointerCaptureSequenceNumber = 0;
385
Chris Yea52ade12020-08-27 16:49:20 -0700386 void getReaderConfiguration(InputReaderConfiguration* outConfig) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800387 *outConfig = mConfig;
388 }
389
Prabir Pradhan2853b7a2021-08-23 14:08:51 +0000390 std::shared_ptr<PointerControllerInterface> obtainPointerController(
391 int32_t /*deviceId*/) override {
392 return mPointerController;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800393 }
394
Chris Yea52ade12020-08-27 16:49:20 -0700395 void notifyInputDevicesChanged(const std::vector<InputDeviceInfo>& inputDevices) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700396 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800397 mInputDevices = inputDevices;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700398 mInputDevicesChanged = true;
399 mDevicesChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800400 }
401
Chris Yea52ade12020-08-27 16:49:20 -0700402 std::shared_ptr<KeyCharacterMap> getKeyboardLayoutOverlay(
403 const InputDeviceIdentifier&) override {
Yi Kong9b14ac62018-07-17 13:48:38 -0700404 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800405 }
406
Chris Yea52ade12020-08-27 16:49:20 -0700407 std::string getDeviceAlias(const InputDeviceIdentifier&) override { return ""; }
Prabir Pradhan1aed8582019-12-30 11:46:51 -0800408
409 void waitForInputDevices(std::function<void(bool)> processDevicesChanged) {
410 std::unique_lock<std::mutex> lock(mLock);
411 base::ScopedLockAssertion assumeLocked(mLock);
412
413 const bool devicesChanged =
414 mDevicesChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
415 return mInputDevicesChanged;
416 });
417 ASSERT_NO_FATAL_FAILURE(processDevicesChanged(devicesChanged));
418 mInputDevicesChanged = false;
419 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800420};
421
Michael Wrightd02c5b62014-02-10 15:10:22 -0800422// --- FakeEventHub ---
423
424class FakeEventHub : public EventHubInterface {
425 struct KeyInfo {
426 int32_t keyCode;
427 uint32_t flags;
428 };
429
Chris Yef59a2f42020-10-16 12:55:26 -0700430 struct SensorInfo {
431 InputDeviceSensorType sensorType;
432 int32_t sensorDataIndex;
433 };
434
Michael Wrightd02c5b62014-02-10 15:10:22 -0800435 struct Device {
436 InputDeviceIdentifier identifier;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700437 ftl::Flags<InputDeviceClass> classes;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800438 PropertyMap configuration;
439 KeyedVector<int, RawAbsoluteAxisInfo> absoluteAxes;
440 KeyedVector<int, bool> relativeAxes;
441 KeyedVector<int32_t, int32_t> keyCodeStates;
442 KeyedVector<int32_t, int32_t> scanCodeStates;
443 KeyedVector<int32_t, int32_t> switchStates;
444 KeyedVector<int32_t, int32_t> absoluteAxisValue;
445 KeyedVector<int32_t, KeyInfo> keysByScanCode;
446 KeyedVector<int32_t, KeyInfo> keysByUsageCode;
447 KeyedVector<int32_t, bool> leds;
Philip Junker4af3b3d2021-12-14 10:36:55 +0100448 // fake mapping which would normally come from keyCharacterMap
449 std::unordered_map<int32_t, int32_t> keyCodeMapping;
Chris Yef59a2f42020-10-16 12:55:26 -0700450 std::unordered_map<int32_t, SensorInfo> sensorsByAbsCode;
451 BitArray<MSC_MAX> mscBitmask;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800452 std::vector<VirtualKeyDefinition> virtualKeys;
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700453 bool enabled;
454
455 status_t enable() {
456 enabled = true;
457 return OK;
458 }
459
460 status_t disable() {
461 enabled = false;
462 return OK;
463 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800464
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700465 explicit Device(ftl::Flags<InputDeviceClass> classes) : classes(classes), enabled(true) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800466 };
467
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700468 std::mutex mLock;
469 std::condition_variable mEventsCondition;
470
Michael Wrightd02c5b62014-02-10 15:10:22 -0800471 KeyedVector<int32_t, Device*> mDevices;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100472 std::vector<std::string> mExcludedDevices;
Siarhei Vishniakou370039c2021-02-04 22:09:01 +0000473 std::vector<RawEvent> mEvents GUARDED_BY(mLock);
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600474 std::unordered_map<int32_t /*deviceId*/, std::vector<TouchVideoFrame>> mVideoFrames;
Chris Ye87143712020-11-10 05:05:58 +0000475 std::vector<int32_t> mVibrators = {0, 1};
Chris Ye3fdbfef2021-01-06 18:45:18 -0800476 std::unordered_map<int32_t, RawLightInfo> mRawLightInfos;
477 // Simulates a device light brightness, from light id to light brightness.
478 std::unordered_map<int32_t /* lightId */, int32_t /* brightness*/> mLightBrightness;
479 // Simulates a device light intensities, from light id to light intensities map.
480 std::unordered_map<int32_t /* lightId */, std::unordered_map<LightColor, int32_t>>
481 mLightIntensities;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800482
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700483public:
Michael Wrightd02c5b62014-02-10 15:10:22 -0800484 virtual ~FakeEventHub() {
485 for (size_t i = 0; i < mDevices.size(); i++) {
486 delete mDevices.valueAt(i);
487 }
488 }
489
Michael Wrightd02c5b62014-02-10 15:10:22 -0800490 FakeEventHub() { }
491
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700492 void addDevice(int32_t deviceId, const std::string& name,
493 ftl::Flags<InputDeviceClass> classes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800494 Device* device = new Device(classes);
495 device->identifier.name = name;
496 mDevices.add(deviceId, device);
497
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000498 enqueueEvent(ARBITRARY_TIME, READ_TIME, deviceId, EventHubInterface::DEVICE_ADDED, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800499 }
500
501 void removeDevice(int32_t deviceId) {
502 delete mDevices.valueFor(deviceId);
503 mDevices.removeItem(deviceId);
504
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000505 enqueueEvent(ARBITRARY_TIME, READ_TIME, deviceId, EventHubInterface::DEVICE_REMOVED, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800506 }
507
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700508 bool isDeviceEnabled(int32_t deviceId) {
509 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700510 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700511 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
512 return false;
513 }
514 return device->enabled;
515 }
516
517 status_t enableDevice(int32_t deviceId) {
518 status_t result;
519 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700520 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700521 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
522 return BAD_VALUE;
523 }
524 if (device->enabled) {
525 ALOGW("Duplicate call to %s, device %" PRId32 " already enabled", __func__, deviceId);
526 return OK;
527 }
528 result = device->enable();
529 return result;
530 }
531
532 status_t disableDevice(int32_t deviceId) {
533 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700534 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700535 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
536 return BAD_VALUE;
537 }
538 if (!device->enabled) {
539 ALOGW("Duplicate call to %s, device %" PRId32 " already disabled", __func__, deviceId);
540 return OK;
541 }
542 return device->disable();
543 }
544
Michael Wrightd02c5b62014-02-10 15:10:22 -0800545 void finishDeviceScan() {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000546 enqueueEvent(ARBITRARY_TIME, READ_TIME, 0, EventHubInterface::FINISHED_DEVICE_SCAN, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800547 }
548
549 void addConfigurationProperty(int32_t deviceId, const String8& key, const String8& value) {
550 Device* device = getDevice(deviceId);
551 device->configuration.addProperty(key, value);
552 }
553
554 void addConfigurationMap(int32_t deviceId, const PropertyMap* configuration) {
555 Device* device = getDevice(deviceId);
556 device->configuration.addAll(configuration);
557 }
558
559 void addAbsoluteAxis(int32_t deviceId, int axis,
560 int32_t minValue, int32_t maxValue, int flat, int fuzz, int resolution = 0) {
561 Device* device = getDevice(deviceId);
562
563 RawAbsoluteAxisInfo info;
564 info.valid = true;
565 info.minValue = minValue;
566 info.maxValue = maxValue;
567 info.flat = flat;
568 info.fuzz = fuzz;
569 info.resolution = resolution;
570 device->absoluteAxes.add(axis, info);
571 }
572
573 void addRelativeAxis(int32_t deviceId, int32_t axis) {
574 Device* device = getDevice(deviceId);
575 device->relativeAxes.add(axis, true);
576 }
577
578 void setKeyCodeState(int32_t deviceId, int32_t keyCode, int32_t state) {
579 Device* device = getDevice(deviceId);
580 device->keyCodeStates.replaceValueFor(keyCode, state);
581 }
582
583 void setScanCodeState(int32_t deviceId, int32_t scanCode, int32_t state) {
584 Device* device = getDevice(deviceId);
585 device->scanCodeStates.replaceValueFor(scanCode, state);
586 }
587
588 void setSwitchState(int32_t deviceId, int32_t switchCode, int32_t state) {
589 Device* device = getDevice(deviceId);
590 device->switchStates.replaceValueFor(switchCode, state);
591 }
592
593 void setAbsoluteAxisValue(int32_t deviceId, int32_t axis, int32_t value) {
594 Device* device = getDevice(deviceId);
595 device->absoluteAxisValue.replaceValueFor(axis, value);
596 }
597
598 void addKey(int32_t deviceId, int32_t scanCode, int32_t usageCode,
599 int32_t keyCode, uint32_t flags) {
600 Device* device = getDevice(deviceId);
601 KeyInfo info;
602 info.keyCode = keyCode;
603 info.flags = flags;
604 if (scanCode) {
605 device->keysByScanCode.add(scanCode, info);
606 }
607 if (usageCode) {
608 device->keysByUsageCode.add(usageCode, info);
609 }
610 }
611
Philip Junker4af3b3d2021-12-14 10:36:55 +0100612 void addKeyCodeMapping(int32_t deviceId, int32_t fromKeyCode, int32_t toKeyCode) {
613 Device* device = getDevice(deviceId);
614 device->keyCodeMapping.insert_or_assign(fromKeyCode, toKeyCode);
615 }
616
Michael Wrightd02c5b62014-02-10 15:10:22 -0800617 void addLed(int32_t deviceId, int32_t led, bool initialState) {
618 Device* device = getDevice(deviceId);
619 device->leds.add(led, initialState);
620 }
621
Chris Yef59a2f42020-10-16 12:55:26 -0700622 void addSensorAxis(int32_t deviceId, int32_t absCode, InputDeviceSensorType sensorType,
623 int32_t sensorDataIndex) {
624 Device* device = getDevice(deviceId);
625 SensorInfo info;
626 info.sensorType = sensorType;
627 info.sensorDataIndex = sensorDataIndex;
628 device->sensorsByAbsCode.emplace(absCode, info);
629 }
630
631 void setMscEvent(int32_t deviceId, int32_t mscEvent) {
632 Device* device = getDevice(deviceId);
633 typename BitArray<MSC_MAX>::Buffer buffer;
634 buffer[mscEvent / 32] = 1 << mscEvent % 32;
635 device->mscBitmask.loadFromBuffer(buffer);
636 }
637
Chris Ye3fdbfef2021-01-06 18:45:18 -0800638 void addRawLightInfo(int32_t rawId, RawLightInfo&& info) {
639 mRawLightInfos.emplace(rawId, std::move(info));
640 }
641
642 void fakeLightBrightness(int32_t rawId, int32_t brightness) {
643 mLightBrightness.emplace(rawId, brightness);
644 }
645
646 void fakeLightIntensities(int32_t rawId,
647 const std::unordered_map<LightColor, int32_t> intensities) {
648 mLightIntensities.emplace(rawId, std::move(intensities));
649 }
650
Michael Wrightd02c5b62014-02-10 15:10:22 -0800651 bool getLedState(int32_t deviceId, int32_t led) {
652 Device* device = getDevice(deviceId);
653 return device->leds.valueFor(led);
654 }
655
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100656 std::vector<std::string>& getExcludedDevices() {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800657 return mExcludedDevices;
658 }
659
660 void addVirtualKeyDefinition(int32_t deviceId, const VirtualKeyDefinition& definition) {
661 Device* device = getDevice(deviceId);
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800662 device->virtualKeys.push_back(definition);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800663 }
664
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000665 void enqueueEvent(nsecs_t when, nsecs_t readTime, int32_t deviceId, int32_t type, int32_t code,
666 int32_t value) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700667 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800668 RawEvent event;
669 event.when = when;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000670 event.readTime = readTime;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800671 event.deviceId = deviceId;
672 event.type = type;
673 event.code = code;
674 event.value = value;
675 mEvents.push_back(event);
676
677 if (type == EV_ABS) {
678 setAbsoluteAxisValue(deviceId, code, value);
679 }
680 }
681
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600682 void setVideoFrames(std::unordered_map<int32_t /*deviceId*/,
683 std::vector<TouchVideoFrame>> videoFrames) {
684 mVideoFrames = std::move(videoFrames);
685 }
686
Michael Wrightd02c5b62014-02-10 15:10:22 -0800687 void assertQueueIsEmpty() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700688 std::unique_lock<std::mutex> lock(mLock);
689 base::ScopedLockAssertion assumeLocked(mLock);
690 const bool queueIsEmpty =
691 mEventsCondition.wait_for(lock, WAIT_TIMEOUT,
692 [this]() REQUIRES(mLock) { return mEvents.size() == 0; });
693 if (!queueIsEmpty) {
694 FAIL() << "Timed out waiting for EventHub queue to be emptied.";
695 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800696 }
697
698private:
699 Device* getDevice(int32_t deviceId) const {
700 ssize_t index = mDevices.indexOfKey(deviceId);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100701 return index >= 0 ? mDevices.valueAt(index) : nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800702 }
703
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700704 ftl::Flags<InputDeviceClass> getDeviceClasses(int32_t deviceId) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800705 Device* device = getDevice(deviceId);
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700706 return device ? device->classes : ftl::Flags<InputDeviceClass>(0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800707 }
708
Chris Yea52ade12020-08-27 16:49:20 -0700709 InputDeviceIdentifier getDeviceIdentifier(int32_t deviceId) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800710 Device* device = getDevice(deviceId);
711 return device ? device->identifier : InputDeviceIdentifier();
712 }
713
Chris Yea52ade12020-08-27 16:49:20 -0700714 int32_t getDeviceControllerNumber(int32_t) const override { return 0; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800715
Chris Yea52ade12020-08-27 16:49:20 -0700716 void getConfiguration(int32_t deviceId, PropertyMap* outConfiguration) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800717 Device* device = getDevice(deviceId);
718 if (device) {
719 *outConfiguration = device->configuration;
720 }
721 }
722
Chris Yea52ade12020-08-27 16:49:20 -0700723 status_t getAbsoluteAxisInfo(int32_t deviceId, int axis,
724 RawAbsoluteAxisInfo* outAxisInfo) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800725 Device* device = getDevice(deviceId);
Arthur Hung9da14732019-09-02 16:16:58 +0800726 if (device && device->enabled) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800727 ssize_t index = device->absoluteAxes.indexOfKey(axis);
728 if (index >= 0) {
729 *outAxisInfo = device->absoluteAxes.valueAt(index);
730 return OK;
731 }
732 }
733 outAxisInfo->clear();
734 return -1;
735 }
736
Chris Yea52ade12020-08-27 16:49:20 -0700737 bool hasRelativeAxis(int32_t deviceId, int axis) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800738 Device* device = getDevice(deviceId);
739 if (device) {
740 return device->relativeAxes.indexOfKey(axis) >= 0;
741 }
742 return false;
743 }
744
Chris Yea52ade12020-08-27 16:49:20 -0700745 bool hasInputProperty(int32_t, int) const override { return false; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800746
Chris Yef59a2f42020-10-16 12:55:26 -0700747 bool hasMscEvent(int32_t deviceId, int mscEvent) const override final {
748 Device* device = getDevice(deviceId);
749 if (device) {
750 return mscEvent >= 0 && mscEvent <= MSC_MAX ? device->mscBitmask.test(mscEvent) : false;
751 }
752 return false;
753 }
754
Chris Yea52ade12020-08-27 16:49:20 -0700755 status_t mapKey(int32_t deviceId, int32_t scanCode, int32_t usageCode, int32_t metaState,
756 int32_t* outKeycode, int32_t* outMetaState, uint32_t* outFlags) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800757 Device* device = getDevice(deviceId);
758 if (device) {
759 const KeyInfo* key = getKey(device, scanCode, usageCode);
760 if (key) {
761 if (outKeycode) {
762 *outKeycode = key->keyCode;
763 }
764 if (outFlags) {
765 *outFlags = key->flags;
766 }
Dmitry Torokhov0faaa0b2015-09-24 13:13:55 -0700767 if (outMetaState) {
768 *outMetaState = metaState;
769 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800770 return OK;
771 }
772 }
773 return NAME_NOT_FOUND;
774 }
775
776 const KeyInfo* getKey(Device* device, int32_t scanCode, int32_t usageCode) const {
777 if (usageCode) {
778 ssize_t index = device->keysByUsageCode.indexOfKey(usageCode);
779 if (index >= 0) {
780 return &device->keysByUsageCode.valueAt(index);
781 }
782 }
783 if (scanCode) {
784 ssize_t index = device->keysByScanCode.indexOfKey(scanCode);
785 if (index >= 0) {
786 return &device->keysByScanCode.valueAt(index);
787 }
788 }
Yi Kong9b14ac62018-07-17 13:48:38 -0700789 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800790 }
791
Chris Yea52ade12020-08-27 16:49:20 -0700792 status_t mapAxis(int32_t, int32_t, AxisInfo*) const override { return NAME_NOT_FOUND; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800793
Chris Yef59a2f42020-10-16 12:55:26 -0700794 base::Result<std::pair<InputDeviceSensorType, int32_t>> mapSensor(int32_t deviceId,
795 int32_t absCode) {
796 Device* device = getDevice(deviceId);
797 if (!device) {
798 return Errorf("Sensor device not found.");
799 }
800 auto it = device->sensorsByAbsCode.find(absCode);
801 if (it == device->sensorsByAbsCode.end()) {
802 return Errorf("Sensor map not found.");
803 }
804 const SensorInfo& info = it->second;
805 return std::make_pair(info.sensorType, info.sensorDataIndex);
806 }
807
Chris Yea52ade12020-08-27 16:49:20 -0700808 void setExcludedDevices(const std::vector<std::string>& devices) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800809 mExcludedDevices = devices;
810 }
811
Siarhei Vishniakou370039c2021-02-04 22:09:01 +0000812 size_t getEvents(int, RawEvent* buffer, size_t bufferSize) override {
813 std::scoped_lock lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800814
Siarhei Vishniakou370039c2021-02-04 22:09:01 +0000815 const size_t filledSize = std::min(mEvents.size(), bufferSize);
816 std::copy(mEvents.begin(), mEvents.begin() + filledSize, buffer);
817
818 mEvents.erase(mEvents.begin(), mEvents.begin() + filledSize);
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700819 mEventsCondition.notify_all();
Siarhei Vishniakou370039c2021-02-04 22:09:01 +0000820 return filledSize;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800821 }
822
Chris Yea52ade12020-08-27 16:49:20 -0700823 std::vector<TouchVideoFrame> getVideoFrames(int32_t deviceId) override {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600824 auto it = mVideoFrames.find(deviceId);
825 if (it != mVideoFrames.end()) {
826 std::vector<TouchVideoFrame> frames = std::move(it->second);
827 mVideoFrames.erase(deviceId);
828 return frames;
829 }
Siarhei Vishniakouadd89292018-12-13 19:23:36 -0800830 return {};
831 }
832
Chris Yea52ade12020-08-27 16:49:20 -0700833 int32_t getScanCodeState(int32_t deviceId, int32_t scanCode) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800834 Device* device = getDevice(deviceId);
835 if (device) {
836 ssize_t index = device->scanCodeStates.indexOfKey(scanCode);
837 if (index >= 0) {
838 return device->scanCodeStates.valueAt(index);
839 }
840 }
841 return AKEY_STATE_UNKNOWN;
842 }
843
Chris Yea52ade12020-08-27 16:49:20 -0700844 int32_t getKeyCodeState(int32_t deviceId, int32_t keyCode) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800845 Device* device = getDevice(deviceId);
846 if (device) {
847 ssize_t index = device->keyCodeStates.indexOfKey(keyCode);
848 if (index >= 0) {
849 return device->keyCodeStates.valueAt(index);
850 }
851 }
852 return AKEY_STATE_UNKNOWN;
853 }
854
Chris Yea52ade12020-08-27 16:49:20 -0700855 int32_t getSwitchState(int32_t deviceId, int32_t sw) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800856 Device* device = getDevice(deviceId);
857 if (device) {
858 ssize_t index = device->switchStates.indexOfKey(sw);
859 if (index >= 0) {
860 return device->switchStates.valueAt(index);
861 }
862 }
863 return AKEY_STATE_UNKNOWN;
864 }
865
Chris Yea52ade12020-08-27 16:49:20 -0700866 status_t getAbsoluteAxisValue(int32_t deviceId, int32_t axis,
867 int32_t* outValue) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800868 Device* device = getDevice(deviceId);
869 if (device) {
870 ssize_t index = device->absoluteAxisValue.indexOfKey(axis);
871 if (index >= 0) {
872 *outValue = device->absoluteAxisValue.valueAt(index);
873 return OK;
874 }
875 }
876 *outValue = 0;
877 return -1;
878 }
879
Philip Junker4af3b3d2021-12-14 10:36:55 +0100880 int32_t getKeyCodeForKeyLocation(int32_t deviceId, int32_t locationKeyCode) const override {
881 Device* device = getDevice(deviceId);
882 if (!device) {
883 return AKEYCODE_UNKNOWN;
884 }
885 auto it = device->keyCodeMapping.find(locationKeyCode);
886 return it != device->keyCodeMapping.end() ? it->second : locationKeyCode;
887 }
888
Chris Yea52ade12020-08-27 16:49:20 -0700889 // Return true if the device has non-empty key layout.
890 bool markSupportedKeyCodes(int32_t deviceId, size_t numCodes, const int32_t* keyCodes,
891 uint8_t* outFlags) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800892 bool result = false;
893 Device* device = getDevice(deviceId);
894 if (device) {
Chris Yea52ade12020-08-27 16:49:20 -0700895 result = device->keysByScanCode.size() > 0 || device->keysByUsageCode.size() > 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800896 for (size_t i = 0; i < numCodes; i++) {
897 for (size_t j = 0; j < device->keysByScanCode.size(); j++) {
898 if (keyCodes[i] == device->keysByScanCode.valueAt(j).keyCode) {
899 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800900 }
901 }
902 for (size_t j = 0; j < device->keysByUsageCode.size(); j++) {
903 if (keyCodes[i] == device->keysByUsageCode.valueAt(j).keyCode) {
904 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800905 }
906 }
907 }
908 }
909 return result;
910 }
911
Chris Yea52ade12020-08-27 16:49:20 -0700912 bool hasScanCode(int32_t deviceId, int32_t scanCode) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800913 Device* device = getDevice(deviceId);
914 if (device) {
915 ssize_t index = device->keysByScanCode.indexOfKey(scanCode);
916 return index >= 0;
917 }
918 return false;
919 }
920
Arthur Hungcb40a002021-08-03 14:31:01 +0000921 bool hasKeyCode(int32_t deviceId, int32_t keyCode) const override {
922 Device* device = getDevice(deviceId);
923 if (!device) {
924 return false;
925 }
926 for (size_t i = 0; i < device->keysByScanCode.size(); i++) {
927 if (keyCode == device->keysByScanCode.valueAt(i).keyCode) {
928 return true;
929 }
930 }
931 for (size_t j = 0; j < device->keysByUsageCode.size(); j++) {
932 if (keyCode == device->keysByUsageCode.valueAt(j).keyCode) {
933 return true;
934 }
935 }
936 return false;
937 }
938
Chris Yea52ade12020-08-27 16:49:20 -0700939 bool hasLed(int32_t deviceId, int32_t led) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800940 Device* device = getDevice(deviceId);
941 return device && device->leds.indexOfKey(led) >= 0;
942 }
943
Chris Yea52ade12020-08-27 16:49:20 -0700944 void setLedState(int32_t deviceId, int32_t led, bool on) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800945 Device* device = getDevice(deviceId);
946 if (device) {
947 ssize_t index = device->leds.indexOfKey(led);
948 if (index >= 0) {
949 device->leds.replaceValueAt(led, on);
950 } else {
951 ADD_FAILURE()
952 << "Attempted to set the state of an LED that the EventHub declared "
953 "was not present. led=" << led;
954 }
955 }
956 }
957
Chris Yea52ade12020-08-27 16:49:20 -0700958 void getVirtualKeyDefinitions(
959 int32_t deviceId, std::vector<VirtualKeyDefinition>& outVirtualKeys) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800960 outVirtualKeys.clear();
961
962 Device* device = getDevice(deviceId);
963 if (device) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800964 outVirtualKeys = device->virtualKeys;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800965 }
966 }
967
Chris Yea52ade12020-08-27 16:49:20 -0700968 const std::shared_ptr<KeyCharacterMap> getKeyCharacterMap(int32_t) const override {
Yi Kong9b14ac62018-07-17 13:48:38 -0700969 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800970 }
971
Chris Yea52ade12020-08-27 16:49:20 -0700972 bool setKeyboardLayoutOverlay(int32_t, std::shared_ptr<KeyCharacterMap>) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800973 return false;
974 }
975
Chris Yea52ade12020-08-27 16:49:20 -0700976 void vibrate(int32_t, const VibrationElement&) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800977
Chris Yea52ade12020-08-27 16:49:20 -0700978 void cancelVibrate(int32_t) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800979
Chris Ye87143712020-11-10 05:05:58 +0000980 std::vector<int32_t> getVibratorIds(int32_t deviceId) override { return mVibrators; };
981
Chris Yee2b1e5c2021-03-10 22:45:12 -0800982 std::optional<int32_t> getBatteryCapacity(int32_t, int32_t) const override {
983 return BATTERY_CAPACITY;
984 }
Kim Low03ea0352020-11-06 12:45:07 -0800985
Chris Yee2b1e5c2021-03-10 22:45:12 -0800986 std::optional<int32_t> getBatteryStatus(int32_t, int32_t) const override {
987 return BATTERY_STATUS;
988 }
989
990 const std::vector<int32_t> getRawBatteryIds(int32_t deviceId) { return {}; }
991
992 std::optional<RawBatteryInfo> getRawBatteryInfo(int32_t deviceId, int32_t batteryId) {
993 return std::nullopt;
994 }
Kim Low03ea0352020-11-06 12:45:07 -0800995
Chris Ye3fdbfef2021-01-06 18:45:18 -0800996 const std::vector<int32_t> getRawLightIds(int32_t deviceId) override {
997 std::vector<int32_t> ids;
998 for (const auto& [rawId, info] : mRawLightInfos) {
999 ids.push_back(rawId);
1000 }
1001 return ids;
1002 }
1003
1004 std::optional<RawLightInfo> getRawLightInfo(int32_t deviceId, int32_t lightId) override {
1005 auto it = mRawLightInfos.find(lightId);
1006 if (it == mRawLightInfos.end()) {
1007 return std::nullopt;
1008 }
1009 return it->second;
1010 }
1011
1012 void setLightBrightness(int32_t deviceId, int32_t lightId, int32_t brightness) override {
1013 mLightBrightness.emplace(lightId, brightness);
1014 }
1015
1016 void setLightIntensities(int32_t deviceId, int32_t lightId,
1017 std::unordered_map<LightColor, int32_t> intensities) override {
1018 mLightIntensities.emplace(lightId, intensities);
1019 };
1020
1021 std::optional<int32_t> getLightBrightness(int32_t deviceId, int32_t lightId) override {
1022 auto lightIt = mLightBrightness.find(lightId);
1023 if (lightIt == mLightBrightness.end()) {
1024 return std::nullopt;
1025 }
1026 return lightIt->second;
1027 }
1028
1029 std::optional<std::unordered_map<LightColor, int32_t>> getLightIntensities(
1030 int32_t deviceId, int32_t lightId) override {
1031 auto lightIt = mLightIntensities.find(lightId);
1032 if (lightIt == mLightIntensities.end()) {
1033 return std::nullopt;
1034 }
1035 return lightIt->second;
1036 };
1037
Narayan Kamath39efe3e2014-10-17 10:37:08 +01001038 virtual bool isExternal(int32_t) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001039 return false;
1040 }
1041
Chris Yea52ade12020-08-27 16:49:20 -07001042 void dump(std::string&) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001043
Chris Yea52ade12020-08-27 16:49:20 -07001044 void monitor() override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001045
Chris Yea52ade12020-08-27 16:49:20 -07001046 void requestReopenDevices() override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001047
Chris Yea52ade12020-08-27 16:49:20 -07001048 void wake() override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001049};
1050
Michael Wrightd02c5b62014-02-10 15:10:22 -08001051// --- FakeInputMapper ---
1052
1053class FakeInputMapper : public InputMapper {
1054 uint32_t mSources;
1055 int32_t mKeyboardType;
1056 int32_t mMetaState;
1057 KeyedVector<int32_t, int32_t> mKeyCodeStates;
1058 KeyedVector<int32_t, int32_t> mScanCodeStates;
1059 KeyedVector<int32_t, int32_t> mSwitchStates;
Philip Junker4af3b3d2021-12-14 10:36:55 +01001060 // fake mapping which would normally come from keyCharacterMap
1061 std::unordered_map<int32_t, int32_t> mKeyCodeMapping;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001062 std::vector<int32_t> mSupportedKeyCodes;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001063
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001064 std::mutex mLock;
1065 std::condition_variable mStateChangedCondition;
1066 bool mConfigureWasCalled GUARDED_BY(mLock);
1067 bool mResetWasCalled GUARDED_BY(mLock);
1068 bool mProcessWasCalled GUARDED_BY(mLock);
1069 RawEvent mLastEvent GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001070
Arthur Hungc23540e2018-11-29 20:42:11 +08001071 std::optional<DisplayViewport> mViewport;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001072public:
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001073 FakeInputMapper(InputDeviceContext& deviceContext, uint32_t sources)
1074 : InputMapper(deviceContext),
1075 mSources(sources),
1076 mKeyboardType(AINPUT_KEYBOARD_TYPE_NONE),
Michael Wrightd02c5b62014-02-10 15:10:22 -08001077 mMetaState(0),
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001078 mConfigureWasCalled(false),
1079 mResetWasCalled(false),
1080 mProcessWasCalled(false) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001081
Chris Yea52ade12020-08-27 16:49:20 -07001082 virtual ~FakeInputMapper() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001083
1084 void setKeyboardType(int32_t keyboardType) {
1085 mKeyboardType = keyboardType;
1086 }
1087
1088 void setMetaState(int32_t metaState) {
1089 mMetaState = metaState;
1090 }
1091
1092 void assertConfigureWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001093 std::unique_lock<std::mutex> lock(mLock);
1094 base::ScopedLockAssertion assumeLocked(mLock);
1095 const bool configureCalled =
1096 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1097 return mConfigureWasCalled;
1098 });
1099 if (!configureCalled) {
1100 FAIL() << "Expected configure() to have been called.";
1101 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001102 mConfigureWasCalled = false;
1103 }
1104
1105 void assertResetWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001106 std::unique_lock<std::mutex> lock(mLock);
1107 base::ScopedLockAssertion assumeLocked(mLock);
1108 const bool resetCalled =
1109 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1110 return mResetWasCalled;
1111 });
1112 if (!resetCalled) {
1113 FAIL() << "Expected reset() to have been called.";
1114 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001115 mResetWasCalled = false;
1116 }
1117
Yi Kong9b14ac62018-07-17 13:48:38 -07001118 void assertProcessWasCalled(RawEvent* outLastEvent = nullptr) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001119 std::unique_lock<std::mutex> lock(mLock);
1120 base::ScopedLockAssertion assumeLocked(mLock);
1121 const bool processCalled =
1122 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1123 return mProcessWasCalled;
1124 });
1125 if (!processCalled) {
1126 FAIL() << "Expected process() to have been called.";
1127 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001128 if (outLastEvent) {
1129 *outLastEvent = mLastEvent;
1130 }
1131 mProcessWasCalled = false;
1132 }
1133
1134 void setKeyCodeState(int32_t keyCode, int32_t state) {
1135 mKeyCodeStates.replaceValueFor(keyCode, state);
1136 }
1137
1138 void setScanCodeState(int32_t scanCode, int32_t state) {
1139 mScanCodeStates.replaceValueFor(scanCode, state);
1140 }
1141
1142 void setSwitchState(int32_t switchCode, int32_t state) {
1143 mSwitchStates.replaceValueFor(switchCode, state);
1144 }
1145
1146 void addSupportedKeyCode(int32_t keyCode) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001147 mSupportedKeyCodes.push_back(keyCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001148 }
1149
Philip Junker4af3b3d2021-12-14 10:36:55 +01001150 void addKeyCodeMapping(int32_t fromKeyCode, int32_t toKeyCode) {
1151 mKeyCodeMapping.insert_or_assign(fromKeyCode, toKeyCode);
1152 }
1153
Michael Wrightd02c5b62014-02-10 15:10:22 -08001154private:
Philip Junker4af3b3d2021-12-14 10:36:55 +01001155 uint32_t getSources() const override { return mSources; }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001156
Chris Yea52ade12020-08-27 16:49:20 -07001157 void populateDeviceInfo(InputDeviceInfo* deviceInfo) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001158 InputMapper::populateDeviceInfo(deviceInfo);
1159
1160 if (mKeyboardType != AINPUT_KEYBOARD_TYPE_NONE) {
1161 deviceInfo->setKeyboardType(mKeyboardType);
1162 }
1163 }
1164
Chris Yea52ade12020-08-27 16:49:20 -07001165 void configure(nsecs_t, const InputReaderConfiguration* config, uint32_t changes) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001166 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001167 mConfigureWasCalled = true;
Arthur Hungc23540e2018-11-29 20:42:11 +08001168
1169 // Find the associated viewport if exist.
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001170 const std::optional<uint8_t> displayPort = getDeviceContext().getAssociatedDisplayPort();
Arthur Hungc23540e2018-11-29 20:42:11 +08001171 if (displayPort && (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
1172 mViewport = config->getDisplayViewportByPort(*displayPort);
1173 }
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001174
1175 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001176 }
1177
Chris Yea52ade12020-08-27 16:49:20 -07001178 void reset(nsecs_t) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001179 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001180 mResetWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001181 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001182 }
1183
Chris Yea52ade12020-08-27 16:49:20 -07001184 void process(const RawEvent* rawEvent) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001185 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001186 mLastEvent = *rawEvent;
1187 mProcessWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001188 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001189 }
1190
Chris Yea52ade12020-08-27 16:49:20 -07001191 int32_t getKeyCodeState(uint32_t, int32_t keyCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001192 ssize_t index = mKeyCodeStates.indexOfKey(keyCode);
1193 return index >= 0 ? mKeyCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1194 }
1195
Philip Junker4af3b3d2021-12-14 10:36:55 +01001196 int32_t getKeyCodeForKeyLocation(int32_t locationKeyCode) const override {
1197 auto it = mKeyCodeMapping.find(locationKeyCode);
1198 return it != mKeyCodeMapping.end() ? it->second : locationKeyCode;
1199 }
1200
Chris Yea52ade12020-08-27 16:49:20 -07001201 int32_t getScanCodeState(uint32_t, int32_t scanCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001202 ssize_t index = mScanCodeStates.indexOfKey(scanCode);
1203 return index >= 0 ? mScanCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1204 }
1205
Chris Yea52ade12020-08-27 16:49:20 -07001206 int32_t getSwitchState(uint32_t, int32_t switchCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001207 ssize_t index = mSwitchStates.indexOfKey(switchCode);
1208 return index >= 0 ? mSwitchStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1209 }
1210
Chris Yea52ade12020-08-27 16:49:20 -07001211 // Return true if the device has non-empty key layout.
1212 bool markSupportedKeyCodes(uint32_t, size_t numCodes, const int32_t* keyCodes,
1213 uint8_t* outFlags) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001214 for (size_t i = 0; i < numCodes; i++) {
1215 for (size_t j = 0; j < mSupportedKeyCodes.size(); j++) {
1216 if (keyCodes[i] == mSupportedKeyCodes[j]) {
1217 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001218 }
1219 }
1220 }
Chris Yea52ade12020-08-27 16:49:20 -07001221 bool result = mSupportedKeyCodes.size() > 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001222 return result;
1223 }
1224
1225 virtual int32_t getMetaState() {
1226 return mMetaState;
1227 }
1228
1229 virtual void fadePointer() {
1230 }
Arthur Hungc23540e2018-11-29 20:42:11 +08001231
1232 virtual std::optional<int32_t> getAssociatedDisplay() {
1233 if (mViewport) {
1234 return std::make_optional(mViewport->displayId);
1235 }
1236 return std::nullopt;
1237 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001238};
1239
1240
1241// --- InstrumentedInputReader ---
1242
1243class InstrumentedInputReader : public InputReader {
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001244 std::queue<std::shared_ptr<InputDevice>> mNextDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001245
1246public:
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001247 InstrumentedInputReader(std::shared_ptr<EventHubInterface> eventHub,
1248 const sp<InputReaderPolicyInterface>& policy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001249 InputListenerInterface& listener)
arthurhungdcef2dc2020-08-11 14:47:50 +08001250 : InputReader(eventHub, policy, listener), mFakeContext(this) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001251
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001252 virtual ~InstrumentedInputReader() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001253
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001254 void pushNextDevice(std::shared_ptr<InputDevice> device) { mNextDevices.push(device); }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001255
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001256 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001257 const std::string& location = "") {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001258 InputDeviceIdentifier identifier;
1259 identifier.name = name;
Arthur Hungc23540e2018-11-29 20:42:11 +08001260 identifier.location = location;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001261 int32_t generation = deviceId + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08001262 return std::make_shared<InputDevice>(&mFakeContext, deviceId, generation, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001263 }
1264
Prabir Pradhan28efc192019-11-05 01:10:04 +00001265 // Make the protected loopOnce method accessible to tests.
1266 using InputReader::loopOnce;
1267
Michael Wrightd02c5b62014-02-10 15:10:22 -08001268protected:
Chris Ye1c2e0892020-11-30 21:41:44 -08001269 virtual std::shared_ptr<InputDevice> createDeviceLocked(int32_t eventHubId,
1270 const InputDeviceIdentifier& identifier)
1271 REQUIRES(mLock) {
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001272 if (!mNextDevices.empty()) {
1273 std::shared_ptr<InputDevice> device(std::move(mNextDevices.front()));
1274 mNextDevices.pop();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001275 return device;
1276 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001277 return InputReader::createDeviceLocked(eventHubId, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001278 }
1279
arthurhungdcef2dc2020-08-11 14:47:50 +08001280 // --- FakeInputReaderContext ---
1281 class FakeInputReaderContext : public ContextImpl {
1282 int32_t mGlobalMetaState;
1283 bool mUpdateGlobalMetaStateWasCalled;
1284 int32_t mGeneration;
1285
1286 public:
1287 FakeInputReaderContext(InputReader* reader)
1288 : ContextImpl(reader),
1289 mGlobalMetaState(0),
1290 mUpdateGlobalMetaStateWasCalled(false),
1291 mGeneration(1) {}
1292
1293 virtual ~FakeInputReaderContext() {}
1294
1295 void assertUpdateGlobalMetaStateWasCalled() {
1296 ASSERT_TRUE(mUpdateGlobalMetaStateWasCalled)
1297 << "Expected updateGlobalMetaState() to have been called.";
1298 mUpdateGlobalMetaStateWasCalled = false;
1299 }
1300
1301 void setGlobalMetaState(int32_t state) { mGlobalMetaState = state; }
1302
1303 uint32_t getGeneration() { return mGeneration; }
1304
1305 void updateGlobalMetaState() override {
1306 mUpdateGlobalMetaStateWasCalled = true;
1307 ContextImpl::updateGlobalMetaState();
1308 }
1309
1310 int32_t getGlobalMetaState() override {
1311 return mGlobalMetaState | ContextImpl::getGlobalMetaState();
1312 }
1313
1314 int32_t bumpGeneration() override {
1315 mGeneration = ContextImpl::bumpGeneration();
1316 return mGeneration;
1317 }
1318 } mFakeContext;
1319
Michael Wrightd02c5b62014-02-10 15:10:22 -08001320 friend class InputReaderTest;
arthurhungdcef2dc2020-08-11 14:47:50 +08001321
1322public:
1323 FakeInputReaderContext* getContext() { return &mFakeContext; }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001324};
1325
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001326// --- InputReaderPolicyTest ---
1327class InputReaderPolicyTest : public testing::Test {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001328protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001329 sp<FakeInputReaderPolicy> mFakePolicy;
1330
Chris Yea52ade12020-08-27 16:49:20 -07001331 void SetUp() override { mFakePolicy = new FakeInputReaderPolicy(); }
1332 void TearDown() override { mFakePolicy.clear(); }
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001333};
1334
1335/**
1336 * Check that empty set of viewports is an acceptable configuration.
1337 * Also try to get internal viewport two different ways - by type and by uniqueId.
1338 *
1339 * There will be confusion if two viewports with empty uniqueId and identical type are present.
1340 * Such configuration is not currently allowed.
1341 */
1342TEST_F(InputReaderPolicyTest, Viewports_GetCleared) {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001343 static const std::string uniqueId = "local:0";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001344
1345 // We didn't add any viewports yet, so there shouldn't be any.
1346 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001347 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001348 ASSERT_FALSE(internalViewport);
1349
1350 // Add an internal viewport, then clear it
1351 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001352 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId, NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001353 ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001354
1355 // Check matching by uniqueId
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001356 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001357 ASSERT_TRUE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001358 ASSERT_EQ(ViewportType::INTERNAL, internalViewport->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001359
1360 // Check matching by viewport type
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001361 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001362 ASSERT_TRUE(internalViewport);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001363 ASSERT_EQ(uniqueId, internalViewport->uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001364
1365 mFakePolicy->clearViewports();
1366 // Make sure nothing is found after clear
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001367 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001368 ASSERT_FALSE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001369 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001370 ASSERT_FALSE(internalViewport);
1371}
1372
1373TEST_F(InputReaderPolicyTest, Viewports_GetByType) {
1374 const std::string internalUniqueId = "local:0";
1375 const std::string externalUniqueId = "local:1";
1376 const std::string virtualUniqueId1 = "virtual:2";
1377 const std::string virtualUniqueId2 = "virtual:3";
1378 constexpr int32_t virtualDisplayId1 = 2;
1379 constexpr int32_t virtualDisplayId2 = 3;
1380
1381 // Add an internal viewport
1382 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001383 DISPLAY_ORIENTATION_0, true /*isActive*/, internalUniqueId,
1384 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001385 // Add an external viewport
1386 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001387 DISPLAY_ORIENTATION_0, true /*isActive*/, externalUniqueId,
1388 NO_PORT, ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001389 // Add an virtual viewport
1390 mFakePolicy->addDisplayViewport(virtualDisplayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001391 DISPLAY_ORIENTATION_0, true /*isActive*/, virtualUniqueId1,
1392 NO_PORT, ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001393 // Add another virtual viewport
1394 mFakePolicy->addDisplayViewport(virtualDisplayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001395 DISPLAY_ORIENTATION_0, true /*isActive*/, virtualUniqueId2,
1396 NO_PORT, ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001397
1398 // Check matching by type for internal
1399 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001400 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001401 ASSERT_TRUE(internalViewport);
1402 ASSERT_EQ(internalUniqueId, internalViewport->uniqueId);
1403
1404 // Check matching by type for external
1405 std::optional<DisplayViewport> externalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001406 mFakePolicy->getDisplayViewportByType(ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001407 ASSERT_TRUE(externalViewport);
1408 ASSERT_EQ(externalUniqueId, externalViewport->uniqueId);
1409
1410 // Check matching by uniqueId for virtual viewport #1
1411 std::optional<DisplayViewport> virtualViewport1 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001412 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001413 ASSERT_TRUE(virtualViewport1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001414 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport1->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001415 ASSERT_EQ(virtualUniqueId1, virtualViewport1->uniqueId);
1416 ASSERT_EQ(virtualDisplayId1, virtualViewport1->displayId);
1417
1418 // Check matching by uniqueId for virtual viewport #2
1419 std::optional<DisplayViewport> virtualViewport2 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001420 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001421 ASSERT_TRUE(virtualViewport2);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001422 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport2->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001423 ASSERT_EQ(virtualUniqueId2, virtualViewport2->uniqueId);
1424 ASSERT_EQ(virtualDisplayId2, virtualViewport2->displayId);
1425}
1426
1427
1428/**
1429 * We can have 2 viewports of the same kind. We can distinguish them by uniqueId, and confirm
1430 * that lookup works by checking display id.
1431 * Check that 2 viewports of each kind is possible, for all existing viewport types.
1432 */
1433TEST_F(InputReaderPolicyTest, Viewports_TwoOfSameType) {
1434 const std::string uniqueId1 = "uniqueId1";
1435 const std::string uniqueId2 = "uniqueId2";
1436 constexpr int32_t displayId1 = 2;
1437 constexpr int32_t displayId2 = 3;
1438
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001439 std::vector<ViewportType> types = {ViewportType::INTERNAL, ViewportType::EXTERNAL,
1440 ViewportType::VIRTUAL};
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001441 for (const ViewportType& type : types) {
1442 mFakePolicy->clearViewports();
1443 // Add a viewport
1444 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001445 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1,
1446 NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001447 // Add another viewport
1448 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001449 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2,
1450 NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001451
1452 // Check that correct display viewport was returned by comparing the display IDs.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001453 std::optional<DisplayViewport> viewport1 =
1454 mFakePolicy->getDisplayViewportByUniqueId(uniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001455 ASSERT_TRUE(viewport1);
1456 ASSERT_EQ(displayId1, viewport1->displayId);
1457 ASSERT_EQ(type, viewport1->type);
1458
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001459 std::optional<DisplayViewport> viewport2 =
1460 mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001461 ASSERT_TRUE(viewport2);
1462 ASSERT_EQ(displayId2, viewport2->displayId);
1463 ASSERT_EQ(type, viewport2->type);
1464
1465 // When there are multiple viewports of the same kind, and uniqueId is not specified
1466 // in the call to getDisplayViewport, then that situation is not supported.
1467 // The viewports can be stored in any order, so we cannot rely on the order, since that
1468 // is just implementation detail.
1469 // However, we can check that it still returns *a* viewport, we just cannot assert
1470 // which one specifically is returned.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001471 std::optional<DisplayViewport> someViewport = mFakePolicy->getDisplayViewportByType(type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001472 ASSERT_TRUE(someViewport);
1473 }
1474}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001475
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001476/**
Michael Wrightdde67b82020-10-27 16:09:22 +00001477 * When we have multiple internal displays make sure we always return the default display when
1478 * querying by type.
1479 */
1480TEST_F(InputReaderPolicyTest, Viewports_ByTypeReturnsDefaultForInternal) {
1481 const std::string uniqueId1 = "uniqueId1";
1482 const std::string uniqueId2 = "uniqueId2";
1483 constexpr int32_t nonDefaultDisplayId = 2;
1484 static_assert(nonDefaultDisplayId != ADISPLAY_ID_DEFAULT,
1485 "Test display ID should not be ADISPLAY_ID_DEFAULT");
1486
1487 // Add the default display first and ensure it gets returned.
1488 mFakePolicy->clearViewports();
1489 mFakePolicy->addDisplayViewport(ADISPLAY_ID_DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001490 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001491 ViewportType::INTERNAL);
1492 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001493 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001494 ViewportType::INTERNAL);
1495
1496 std::optional<DisplayViewport> viewport =
1497 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
1498 ASSERT_TRUE(viewport);
1499 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
1500 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
1501
1502 // Add the default display second to make sure order doesn't matter.
1503 mFakePolicy->clearViewports();
1504 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001505 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001506 ViewportType::INTERNAL);
1507 mFakePolicy->addDisplayViewport(ADISPLAY_ID_DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001508 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001509 ViewportType::INTERNAL);
1510
1511 viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
1512 ASSERT_TRUE(viewport);
1513 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
1514 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
1515}
1516
1517/**
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001518 * Check getDisplayViewportByPort
1519 */
1520TEST_F(InputReaderPolicyTest, Viewports_GetByPort) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001521 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001522 const std::string uniqueId1 = "uniqueId1";
1523 const std::string uniqueId2 = "uniqueId2";
1524 constexpr int32_t displayId1 = 1;
1525 constexpr int32_t displayId2 = 2;
1526 const uint8_t hdmi1 = 0;
1527 const uint8_t hdmi2 = 1;
1528 const uint8_t hdmi3 = 2;
1529
1530 mFakePolicy->clearViewports();
1531 // Add a viewport that's associated with some display port that's not of interest.
1532 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001533 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, hdmi3,
1534 type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001535 // Add another viewport, connected to HDMI1 port
1536 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001537 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, hdmi1,
1538 type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001539
1540 // Check that correct display viewport was returned by comparing the display ports.
1541 std::optional<DisplayViewport> hdmi1Viewport = mFakePolicy->getDisplayViewportByPort(hdmi1);
1542 ASSERT_TRUE(hdmi1Viewport);
1543 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1544 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1545
1546 // Check that we can still get the same viewport using the uniqueId
1547 hdmi1Viewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
1548 ASSERT_TRUE(hdmi1Viewport);
1549 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1550 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1551 ASSERT_EQ(type, hdmi1Viewport->type);
1552
1553 // Check that we cannot find a port with "HDMI2", because we never added one
1554 std::optional<DisplayViewport> hdmi2Viewport = mFakePolicy->getDisplayViewportByPort(hdmi2);
1555 ASSERT_FALSE(hdmi2Viewport);
1556}
1557
Michael Wrightd02c5b62014-02-10 15:10:22 -08001558// --- InputReaderTest ---
1559
1560class InputReaderTest : public testing::Test {
1561protected:
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001562 std::unique_ptr<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001563 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001564 std::shared_ptr<FakeEventHub> mFakeEventHub;
Prabir Pradhan28efc192019-11-05 01:10:04 +00001565 std::unique_ptr<InstrumentedInputReader> mReader;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001566
Chris Yea52ade12020-08-27 16:49:20 -07001567 void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001568 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001569 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001570 mFakeListener = std::make_unique<TestInputListener>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001571
Prabir Pradhan28efc192019-11-05 01:10:04 +00001572 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001573 *mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001574 }
1575
Chris Yea52ade12020-08-27 16:49:20 -07001576 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001577 mFakeListener.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001578 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001579 }
1580
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001581 void addDevice(int32_t eventHubId, const std::string& name,
1582 ftl::Flags<InputDeviceClass> classes, const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001583 mFakeEventHub->addDevice(eventHubId, name, classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001584
1585 if (configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001586 mFakeEventHub->addConfigurationMap(eventHubId, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001587 }
1588 mFakeEventHub->finishDeviceScan();
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001589 mReader->loopOnce();
1590 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001591 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1592 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001593 }
1594
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001595 void disableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001596 mFakePolicy->addDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001597 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001598 }
1599
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001600 void enableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001601 mFakePolicy->removeDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001602 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001603 }
1604
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001605 FakeInputMapper& addDeviceWithFakeInputMapper(int32_t deviceId, int32_t eventHubId,
Chris Ye1b0c7342020-07-28 21:57:03 -07001606 const std::string& name,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001607 ftl::Flags<InputDeviceClass> classes,
1608 uint32_t sources,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001609 const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001610 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, name);
1611 FakeInputMapper& mapper = device->addMapper<FakeInputMapper>(eventHubId, sources);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001612 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001613 addDevice(eventHubId, name, classes, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001614 return mapper;
1615 }
1616};
1617
Chris Ye98d3f532020-10-01 21:48:59 -07001618TEST_F(InputReaderTest, PolicyGetInputDevices) {
1619 ASSERT_NO_FATAL_FAILURE(addDevice(1, "keyboard", InputDeviceClass::KEYBOARD, nullptr));
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001620 ASSERT_NO_FATAL_FAILURE(addDevice(2, "ignored", ftl::Flags<InputDeviceClass>(0),
Chris Ye98d3f532020-10-01 21:48:59 -07001621 nullptr)); // no classes so device will be ignored
Michael Wrightd02c5b62014-02-10 15:10:22 -08001622
1623 // Should also have received a notification describing the new input devices.
Chris Ye98d3f532020-10-01 21:48:59 -07001624 const std::vector<InputDeviceInfo>& inputDevices = mFakePolicy->getInputDevices();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001625 ASSERT_EQ(1U, inputDevices.size());
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001626 ASSERT_EQ(END_RESERVED_ID + 1, inputDevices[0].getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001627 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001628 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
1629 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00001630 ASSERT_EQ(0U, inputDevices[0].getMotionRanges().size());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001631}
1632
Chris Yee7310032020-09-22 15:36:28 -07001633TEST_F(InputReaderTest, GetMergedInputDevices) {
1634 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1635 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1636 // Add two subdevices to device
1637 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1638 // Must add at least one mapper or the device will be ignored!
1639 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1640 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1641
1642 // Push same device instance for next device to be added, so they'll have same identifier.
1643 mReader->pushNextDevice(device);
1644 mReader->pushNextDevice(device);
1645 ASSERT_NO_FATAL_FAILURE(
1646 addDevice(eventHubIds[0], "fake1", InputDeviceClass::KEYBOARD, nullptr));
1647 ASSERT_NO_FATAL_FAILURE(
1648 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
1649
1650 // Two devices will be merged to one input device as they have same identifier
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00001651 ASSERT_EQ(1U, mFakePolicy->getInputDevices().size());
Chris Yee7310032020-09-22 15:36:28 -07001652}
1653
Chris Yee14523a2020-12-19 13:46:00 -08001654TEST_F(InputReaderTest, GetMergedInputDevicesEnabled) {
1655 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1656 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1657 // Add two subdevices to device
1658 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1659 // Must add at least one mapper or the device will be ignored!
1660 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1661 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1662
1663 // Push same device instance for next device to be added, so they'll have same identifier.
1664 mReader->pushNextDevice(device);
1665 mReader->pushNextDevice(device);
1666 // Sensor device is initially disabled
1667 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1",
1668 InputDeviceClass::KEYBOARD | InputDeviceClass::SENSOR,
1669 nullptr));
1670 // Device is disabled because the only sub device is a sensor device and disabled initially.
1671 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1672 ASSERT_FALSE(device->isEnabled());
1673 ASSERT_NO_FATAL_FAILURE(
1674 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
1675 // The merged device is enabled if any sub device is enabled
1676 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1677 ASSERT_TRUE(device->isEnabled());
1678}
1679
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001680TEST_F(InputReaderTest, WhenEnabledChanges_SendsDeviceResetNotification) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001681 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001682 constexpr ftl::Flags<InputDeviceClass> deviceClass(InputDeviceClass::KEYBOARD);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001683 constexpr int32_t eventHubId = 1;
1684 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001685 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001686 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001687 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001688 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001689
Yi Kong9b14ac62018-07-17 13:48:38 -07001690 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001691
1692 NotifyDeviceResetArgs resetArgs;
1693 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001694 ASSERT_EQ(deviceId, resetArgs.deviceId);
1695
1696 ASSERT_EQ(device->isEnabled(), true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001697 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001698 mReader->loopOnce();
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001699
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001700 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001701 ASSERT_EQ(deviceId, resetArgs.deviceId);
1702 ASSERT_EQ(device->isEnabled(), false);
1703
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001704 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001705 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001706 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
1707 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasNotCalled());
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001708 ASSERT_EQ(device->isEnabled(), false);
1709
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001710 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001711 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001712 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001713 ASSERT_EQ(deviceId, resetArgs.deviceId);
1714 ASSERT_EQ(device->isEnabled(), true);
1715}
1716
Michael Wrightd02c5b62014-02-10 15:10:22 -08001717TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001718 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001719 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001720 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001721 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001722 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001723 AINPUT_SOURCE_KEYBOARD, nullptr);
1724 mapper.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001725
1726 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(0,
1727 AINPUT_SOURCE_ANY, AKEYCODE_A))
1728 << "Should return unknown when the device id is >= 0 but unknown.";
1729
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001730 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1731 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1732 << "Should return unknown when the device id is valid but the sources are not "
1733 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001734
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001735 ASSERT_EQ(AKEY_STATE_DOWN,
1736 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1737 AKEYCODE_A))
1738 << "Should return value provided by mapper when device id is valid and the device "
1739 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001740
1741 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(-1,
1742 AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1743 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1744
1745 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getKeyCodeState(-1,
1746 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1747 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1748}
1749
Philip Junker4af3b3d2021-12-14 10:36:55 +01001750TEST_F(InputReaderTest, GetKeyCodeForKeyLocation_ForwardsRequestsToMappers) {
1751 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1752 constexpr int32_t eventHubId = 1;
1753 FakeInputMapper& mapper = addDeviceWithFakeInputMapper(deviceId, eventHubId, "keyboard",
1754 InputDeviceClass::KEYBOARD,
1755 AINPUT_SOURCE_KEYBOARD, nullptr);
1756 mapper.addKeyCodeMapping(AKEYCODE_Y, AKEYCODE_Z);
1757
1758 ASSERT_EQ(AKEYCODE_UNKNOWN, mReader->getKeyCodeForKeyLocation(0, AKEYCODE_Y))
1759 << "Should return unknown when the device with the specified id is not found.";
1760
1761 ASSERT_EQ(AKEYCODE_Z, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_Y))
1762 << "Should return correct mapping when device id is valid and mapping exists.";
1763
1764 ASSERT_EQ(AKEYCODE_A, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_A))
1765 << "Should return the location key code when device id is valid and there's no "
1766 "mapping.";
1767}
1768
1769TEST_F(InputReaderTest, GetKeyCodeForKeyLocation_NoKeyboardMapper) {
1770 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1771 constexpr int32_t eventHubId = 1;
1772 FakeInputMapper& mapper = addDeviceWithFakeInputMapper(deviceId, eventHubId, "joystick",
1773 InputDeviceClass::JOYSTICK,
1774 AINPUT_SOURCE_GAMEPAD, nullptr);
1775 mapper.addKeyCodeMapping(AKEYCODE_Y, AKEYCODE_Z);
1776
1777 ASSERT_EQ(AKEYCODE_UNKNOWN, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_Y))
1778 << "Should return unknown when the device id is valid but there is no keyboard mapper";
1779}
1780
Michael Wrightd02c5b62014-02-10 15:10:22 -08001781TEST_F(InputReaderTest, GetScanCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001782 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001783 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001784 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001785 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001786 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001787 AINPUT_SOURCE_KEYBOARD, nullptr);
1788 mapper.setScanCodeState(KEY_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001789
1790 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(0,
1791 AINPUT_SOURCE_ANY, KEY_A))
1792 << "Should return unknown when the device id is >= 0 but unknown.";
1793
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001794 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1795 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, KEY_A))
1796 << "Should return unknown when the device id is valid but the sources are not "
1797 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001798
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001799 ASSERT_EQ(AKEY_STATE_DOWN,
1800 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1801 KEY_A))
1802 << "Should return value provided by mapper when device id is valid and the device "
1803 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001804
1805 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(-1,
1806 AINPUT_SOURCE_TRACKBALL, KEY_A))
1807 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1808
1809 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getScanCodeState(-1,
1810 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, KEY_A))
1811 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1812}
1813
1814TEST_F(InputReaderTest, GetSwitchState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001815 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001816 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001817 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001818 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001819 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001820 AINPUT_SOURCE_KEYBOARD, nullptr);
1821 mapper.setSwitchState(SW_LID, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001822
1823 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(0,
1824 AINPUT_SOURCE_ANY, SW_LID))
1825 << "Should return unknown when the device id is >= 0 but unknown.";
1826
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001827 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1828 mReader->getSwitchState(deviceId, AINPUT_SOURCE_TRACKBALL, SW_LID))
1829 << "Should return unknown when the device id is valid but the sources are not "
1830 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001831
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001832 ASSERT_EQ(AKEY_STATE_DOWN,
1833 mReader->getSwitchState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1834 SW_LID))
1835 << "Should return value provided by mapper when device id is valid and the device "
1836 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001837
1838 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(-1,
1839 AINPUT_SOURCE_TRACKBALL, SW_LID))
1840 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1841
1842 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getSwitchState(-1,
1843 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, SW_LID))
1844 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1845}
1846
1847TEST_F(InputReaderTest, MarkSupportedKeyCodes_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001848 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001849 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001850 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001851 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001852 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001853 AINPUT_SOURCE_KEYBOARD, nullptr);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001854
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001855 mapper.addSupportedKeyCode(AKEYCODE_A);
1856 mapper.addSupportedKeyCode(AKEYCODE_B);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001857
1858 const int32_t keyCodes[4] = { AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2 };
1859 uint8_t flags[4] = { 0, 0, 0, 1 };
1860
1861 ASSERT_FALSE(mReader->hasKeys(0, AINPUT_SOURCE_ANY, 4, keyCodes, flags))
1862 << "Should return false when device id is >= 0 but unknown.";
1863 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1864
1865 flags[3] = 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001866 ASSERT_FALSE(mReader->hasKeys(deviceId, AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1867 << "Should return false when device id is valid but the sources are not supported by "
1868 "the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001869 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1870
1871 flags[3] = 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001872 ASSERT_TRUE(mReader->hasKeys(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, 4,
1873 keyCodes, flags))
1874 << "Should return value provided by mapper when device id is valid and the device "
1875 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001876 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1877
1878 flags[3] = 1;
1879 ASSERT_FALSE(mReader->hasKeys(-1, AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1880 << "Should return false when the device id is < 0 but the sources are not supported by any device.";
1881 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1882
1883 flags[3] = 1;
1884 ASSERT_TRUE(mReader->hasKeys(-1, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1885 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1886 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1887}
1888
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001889TEST_F(InputReaderTest, LoopOnce_WhenDeviceScanFinished_SendsConfigurationChanged) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001890 constexpr int32_t eventHubId = 1;
Chris Ye1b0c7342020-07-28 21:57:03 -07001891 addDevice(eventHubId, "ignored", InputDeviceClass::KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001892
1893 NotifyConfigurationChangedArgs args;
1894
1895 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(&args));
1896 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
1897}
1898
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001899TEST_F(InputReaderTest, LoopOnce_ForwardsRawEventsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001900 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001901 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001902 constexpr nsecs_t when = 0;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001903 constexpr int32_t eventHubId = 1;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001904 constexpr nsecs_t readTime = 2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001905 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001906 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001907 AINPUT_SOURCE_KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001908
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001909 mFakeEventHub->enqueueEvent(when, readTime, eventHubId, EV_KEY, KEY_A, 1);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001910 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001911 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
1912
1913 RawEvent event;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001914 ASSERT_NO_FATAL_FAILURE(mapper.assertProcessWasCalled(&event));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001915 ASSERT_EQ(when, event.when);
1916 ASSERT_EQ(readTime, event.readTime);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001917 ASSERT_EQ(eventHubId, event.deviceId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001918 ASSERT_EQ(EV_KEY, event.type);
1919 ASSERT_EQ(KEY_A, event.code);
1920 ASSERT_EQ(1, event.value);
1921}
1922
Garfield Tan1c7bc862020-01-28 13:24:04 -08001923TEST_F(InputReaderTest, DeviceReset_RandomId) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001924 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001925 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001926 constexpr int32_t eventHubId = 1;
1927 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Prabir Pradhan42611e02018-11-27 14:04:02 -08001928 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001929 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001930 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001931 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan42611e02018-11-27 14:04:02 -08001932
1933 NotifyDeviceResetArgs resetArgs;
1934 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001935 int32_t prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001936
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001937 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001938 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001939 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001940 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001941 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001942
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001943 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001944 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001945 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001946 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001947 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001948
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001949 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001950 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001951 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001952 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001953 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001954}
1955
Garfield Tan1c7bc862020-01-28 13:24:04 -08001956TEST_F(InputReaderTest, DeviceReset_GenerateIdWithInputReaderSource) {
1957 constexpr int32_t deviceId = 1;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001958 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Garfield Tan1c7bc862020-01-28 13:24:04 -08001959 constexpr int32_t eventHubId = 1;
1960 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1961 // Must add at least one mapper or the device will be ignored!
1962 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001963 mReader->pushNextDevice(device);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001964 ASSERT_NO_FATAL_FAILURE(addDevice(deviceId, "fake", deviceClass, nullptr));
1965
1966 NotifyDeviceResetArgs resetArgs;
1967 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1968 ASSERT_EQ(IdGenerator::Source::INPUT_READER, IdGenerator::getSource(resetArgs.id));
1969}
1970
Arthur Hungc23540e2018-11-29 20:42:11 +08001971TEST_F(InputReaderTest, Device_CanDispatchToDisplay) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001972 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001973 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001974 constexpr int32_t eventHubId = 1;
Arthur Hungc23540e2018-11-29 20:42:11 +08001975 const char* DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001976 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1977 FakeInputMapper& mapper =
1978 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001979 mReader->pushNextDevice(device);
Arthur Hungc23540e2018-11-29 20:42:11 +08001980
1981 const uint8_t hdmi1 = 1;
1982
1983 // Associated touch screen with second display.
1984 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
1985
1986 // Add default and second display.
Prabir Pradhan28efc192019-11-05 01:10:04 +00001987 mFakePolicy->clearViewports();
Arthur Hungc23540e2018-11-29 20:42:11 +08001988 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001989 DISPLAY_ORIENTATION_0, true /*isActive*/, "local:0", NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001990 ViewportType::INTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08001991 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001992 DISPLAY_ORIENTATION_0, true /*isActive*/, "local:1", hdmi1,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001993 ViewportType::EXTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08001994 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001995 mReader->loopOnce();
Prabir Pradhan28efc192019-11-05 01:10:04 +00001996
1997 // Add the device, and make sure all of the callbacks are triggered.
1998 // The device is added after the input port associations are processed since
1999 // we do not yet support dynamic device-to-display associations.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002000 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07002001 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled());
Prabir Pradhan28efc192019-11-05 01:10:04 +00002002 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002003 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
Arthur Hungc23540e2018-11-29 20:42:11 +08002004
Arthur Hung2c9a3342019-07-23 14:18:59 +08002005 // Device should only dispatch to the specified display.
Arthur Hungc23540e2018-11-29 20:42:11 +08002006 ASSERT_EQ(deviceId, device->getId());
2007 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, DISPLAY_ID));
2008 ASSERT_TRUE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hung2c9a3342019-07-23 14:18:59 +08002009
2010 // Can't dispatch event from a disabled device.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002011 disableDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00002012 mReader->loopOnce();
Arthur Hung2c9a3342019-07-23 14:18:59 +08002013 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hungc23540e2018-11-29 20:42:11 +08002014}
2015
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08002016TEST_F(InputReaderTest, WhenEnabledChanges_AllSubdevicesAreUpdated) {
2017 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002018 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08002019 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
2020 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
2021 // Must add at least one mapper or the device will be ignored!
2022 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
2023 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
2024 mReader->pushNextDevice(device);
2025 mReader->pushNextDevice(device);
2026 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
2027 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
2028
2029 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
2030
2031 NotifyDeviceResetArgs resetArgs;
2032 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2033 ASSERT_EQ(deviceId, resetArgs.deviceId);
2034 ASSERT_TRUE(device->isEnabled());
2035 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
2036 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
2037
2038 disableDevice(deviceId);
2039 mReader->loopOnce();
2040
2041 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2042 ASSERT_EQ(deviceId, resetArgs.deviceId);
2043 ASSERT_FALSE(device->isEnabled());
2044 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
2045 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
2046
2047 enableDevice(deviceId);
2048 mReader->loopOnce();
2049
2050 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2051 ASSERT_EQ(deviceId, resetArgs.deviceId);
2052 ASSERT_TRUE(device->isEnabled());
2053 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
2054 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
2055}
2056
2057TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToSubdeviceMappers) {
2058 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002059 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08002060 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
2061 // Add two subdevices to device
2062 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
2063 FakeInputMapper& mapperDevice1 =
2064 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
2065 FakeInputMapper& mapperDevice2 =
2066 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
2067 mReader->pushNextDevice(device);
2068 mReader->pushNextDevice(device);
2069 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
2070 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
2071
2072 mapperDevice1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
2073 mapperDevice2.setKeyCodeState(AKEYCODE_B, AKEY_STATE_DOWN);
2074
2075 ASSERT_EQ(AKEY_STATE_DOWN,
2076 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_A));
2077 ASSERT_EQ(AKEY_STATE_DOWN,
2078 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_B));
2079 ASSERT_EQ(AKEY_STATE_UNKNOWN,
2080 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_C));
2081}
2082
Prabir Pradhan7e186182020-11-10 13:56:45 -08002083TEST_F(InputReaderTest, ChangingPointerCaptureNotifiesInputListener) {
2084 NotifyPointerCaptureChangedArgs args;
2085
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002086 auto request = mFakePolicy->setPointerCapture(true);
Prabir Pradhan7e186182020-11-10 13:56:45 -08002087 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
2088 mReader->loopOnce();
2089 mFakeListener->assertNotifyCaptureWasCalled(&args);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002090 ASSERT_TRUE(args.request.enable) << "Pointer Capture should be enabled.";
2091 ASSERT_EQ(args.request, request) << "Pointer Capture sequence number should match.";
Prabir Pradhan7e186182020-11-10 13:56:45 -08002092
2093 mFakePolicy->setPointerCapture(false);
2094 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
2095 mReader->loopOnce();
2096 mFakeListener->assertNotifyCaptureWasCalled(&args);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002097 ASSERT_FALSE(args.request.enable) << "Pointer Capture should be disabled.";
Prabir Pradhan7e186182020-11-10 13:56:45 -08002098
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002099 // Verify that the Pointer Capture state is not updated when the configuration value
Prabir Pradhan7e186182020-11-10 13:56:45 -08002100 // does not change.
2101 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
2102 mReader->loopOnce();
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002103 mFakeListener->assertNotifyCaptureWasNotCalled();
Prabir Pradhan7e186182020-11-10 13:56:45 -08002104}
2105
Chris Ye87143712020-11-10 05:05:58 +00002106class FakeVibratorInputMapper : public FakeInputMapper {
2107public:
2108 FakeVibratorInputMapper(InputDeviceContext& deviceContext, uint32_t sources)
2109 : FakeInputMapper(deviceContext, sources) {}
2110
2111 std::vector<int32_t> getVibratorIds() override { return getDeviceContext().getVibratorIds(); }
2112};
2113
2114TEST_F(InputReaderTest, VibratorGetVibratorIds) {
2115 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002116 ftl::Flags<InputDeviceClass> deviceClass =
2117 InputDeviceClass::KEYBOARD | InputDeviceClass::VIBRATOR;
Chris Ye87143712020-11-10 05:05:58 +00002118 constexpr int32_t eventHubId = 1;
2119 const char* DEVICE_LOCATION = "BLUETOOTH";
2120 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
2121 FakeVibratorInputMapper& mapper =
2122 device->addMapper<FakeVibratorInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
2123 mReader->pushNextDevice(device);
2124
2125 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2126 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
2127
2128 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
2129 ASSERT_EQ(mReader->getVibratorIds(deviceId).size(), 2U);
2130}
2131
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002132// --- FakePeripheralController ---
Kim Low03ea0352020-11-06 12:45:07 -08002133
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002134class FakePeripheralController : public PeripheralControllerInterface {
Chris Yee2b1e5c2021-03-10 22:45:12 -08002135public:
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002136 FakePeripheralController(InputDeviceContext& deviceContext) : mDeviceContext(deviceContext) {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08002137
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002138 ~FakePeripheralController() override {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08002139
2140 void populateDeviceInfo(InputDeviceInfo* deviceInfo) override {}
2141
2142 void dump(std::string& dump) override {}
2143
2144 std::optional<int32_t> getBatteryCapacity(int32_t batteryId) override {
2145 return getDeviceContext().getBatteryCapacity(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08002146 }
2147
Chris Yee2b1e5c2021-03-10 22:45:12 -08002148 std::optional<int32_t> getBatteryStatus(int32_t batteryId) override {
2149 return getDeviceContext().getBatteryStatus(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08002150 }
Chris Ye3fdbfef2021-01-06 18:45:18 -08002151
2152 bool setLightColor(int32_t lightId, int32_t color) override {
2153 getDeviceContext().setLightBrightness(lightId, color >> 24);
2154 return true;
2155 }
2156
2157 std::optional<int32_t> getLightColor(int32_t lightId) override {
2158 std::optional<int32_t> result = getDeviceContext().getLightBrightness(lightId);
2159 if (!result.has_value()) {
2160 return std::nullopt;
2161 }
2162 return result.value() << 24;
2163 }
Chris Yee2b1e5c2021-03-10 22:45:12 -08002164
2165 bool setLightPlayerId(int32_t lightId, int32_t playerId) override { return true; }
2166
2167 std::optional<int32_t> getLightPlayerId(int32_t lightId) override { return std::nullopt; }
2168
2169private:
2170 InputDeviceContext& mDeviceContext;
2171 inline int32_t getDeviceId() { return mDeviceContext.getId(); }
2172 inline InputDeviceContext& getDeviceContext() { return mDeviceContext; }
Chris Ye3fdbfef2021-01-06 18:45:18 -08002173};
2174
Chris Yee2b1e5c2021-03-10 22:45:12 -08002175TEST_F(InputReaderTest, BatteryGetCapacity) {
2176 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002177 ftl::Flags<InputDeviceClass> deviceClass =
2178 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
Chris Yee2b1e5c2021-03-10 22:45:12 -08002179 constexpr int32_t eventHubId = 1;
2180 const char* DEVICE_LOCATION = "BLUETOOTH";
2181 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002182 FakePeripheralController& controller =
2183 device->addController<FakePeripheralController>(eventHubId);
Chris Yee2b1e5c2021-03-10 22:45:12 -08002184 mReader->pushNextDevice(device);
2185
2186 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2187
2188 ASSERT_EQ(controller.getBatteryCapacity(DEFAULT_BATTERY), BATTERY_CAPACITY);
2189 ASSERT_EQ(mReader->getBatteryCapacity(deviceId), BATTERY_CAPACITY);
2190}
2191
2192TEST_F(InputReaderTest, BatteryGetStatus) {
2193 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002194 ftl::Flags<InputDeviceClass> deviceClass =
2195 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
Chris Yee2b1e5c2021-03-10 22:45:12 -08002196 constexpr int32_t eventHubId = 1;
2197 const char* DEVICE_LOCATION = "BLUETOOTH";
2198 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002199 FakePeripheralController& controller =
2200 device->addController<FakePeripheralController>(eventHubId);
Chris Yee2b1e5c2021-03-10 22:45:12 -08002201 mReader->pushNextDevice(device);
2202
2203 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2204
2205 ASSERT_EQ(controller.getBatteryStatus(DEFAULT_BATTERY), BATTERY_STATUS);
2206 ASSERT_EQ(mReader->getBatteryStatus(deviceId), BATTERY_STATUS);
2207}
2208
Chris Ye3fdbfef2021-01-06 18:45:18 -08002209TEST_F(InputReaderTest, LightGetColor) {
2210 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002211 ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD | InputDeviceClass::LIGHT;
Chris Ye3fdbfef2021-01-06 18:45:18 -08002212 constexpr int32_t eventHubId = 1;
2213 const char* DEVICE_LOCATION = "BLUETOOTH";
2214 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002215 FakePeripheralController& controller =
2216 device->addController<FakePeripheralController>(eventHubId);
Chris Ye3fdbfef2021-01-06 18:45:18 -08002217 mReader->pushNextDevice(device);
2218 RawLightInfo info = {.id = 1,
2219 .name = "Mono",
2220 .maxBrightness = 255,
2221 .flags = InputLightClass::BRIGHTNESS,
2222 .path = ""};
2223 mFakeEventHub->addRawLightInfo(1 /* rawId */, std::move(info));
2224 mFakeEventHub->fakeLightBrightness(1 /* rawId */, 0x55);
2225
2226 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Chris Ye3fdbfef2021-01-06 18:45:18 -08002227
Chris Yee2b1e5c2021-03-10 22:45:12 -08002228 ASSERT_TRUE(controller.setLightColor(1 /* lightId */, LIGHT_BRIGHTNESS));
2229 ASSERT_EQ(controller.getLightColor(1 /* lightId */), LIGHT_BRIGHTNESS);
Chris Ye3fdbfef2021-01-06 18:45:18 -08002230 ASSERT_TRUE(mReader->setLightColor(deviceId, 1 /* lightId */, LIGHT_BRIGHTNESS));
2231 ASSERT_EQ(mReader->getLightColor(deviceId, 1 /* lightId */), LIGHT_BRIGHTNESS);
2232}
2233
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002234// --- InputReaderIntegrationTest ---
2235
2236// These tests create and interact with the InputReader only through its interface.
2237// The InputReader is started during SetUp(), which starts its processing in its own
2238// thread. The tests use linux uinput to emulate input devices.
2239// NOTE: Interacting with the physical device while these tests are running may cause
2240// the tests to fail.
2241class InputReaderIntegrationTest : public testing::Test {
2242protected:
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002243 std::unique_ptr<TestInputListener> mTestListener;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002244 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002245 std::unique_ptr<InputReaderInterface> mReader;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002246
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00002247 std::shared_ptr<FakePointerController> mFakePointerController;
2248
Chris Yea52ade12020-08-27 16:49:20 -07002249 void SetUp() override {
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002250 mFakePolicy = new FakeInputReaderPolicy();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00002251 mFakePointerController = std::make_shared<FakePointerController>();
2252 mFakePolicy->setPointerController(mFakePointerController);
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002253 mTestListener = std::make_unique<TestInputListener>(2000ms /*eventHappenedTimeout*/,
2254 30ms /*eventDidNotHappenTimeout*/);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002255
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002256 mReader = std::make_unique<InputReader>(std::make_shared<EventHub>(), mFakePolicy,
2257 *mTestListener);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002258 ASSERT_EQ(mReader->start(), OK);
2259
2260 // Since this test is run on a real device, all the input devices connected
2261 // to the test device will show up in mReader. We wait for those input devices to
2262 // show up before beginning the tests.
2263 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2264 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2265 }
2266
Chris Yea52ade12020-08-27 16:49:20 -07002267 void TearDown() override {
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002268 ASSERT_EQ(mReader->stop(), OK);
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002269 mReader.reset();
2270 mTestListener.reset();
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002271 mFakePolicy.clear();
2272 }
2273};
2274
2275TEST_F(InputReaderIntegrationTest, TestInvalidDevice) {
2276 // An invalid input device that is only used for this test.
2277 class InvalidUinputDevice : public UinputDevice {
2278 public:
2279 InvalidUinputDevice() : UinputDevice("Invalid Device") {}
2280
2281 private:
2282 void configureDevice(int fd, uinput_user_dev* device) override {}
2283 };
2284
2285 const size_t numDevices = mFakePolicy->getInputDevices().size();
2286
2287 // UinputDevice does not set any event or key bits, so InputReader should not
2288 // consider it as a valid device.
2289 std::unique_ptr<UinputDevice> invalidDevice = createUinputDevice<InvalidUinputDevice>();
2290 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
2291 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
2292 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
2293
2294 invalidDevice.reset();
2295 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
2296 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
2297 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
2298}
2299
2300TEST_F(InputReaderIntegrationTest, AddNewDevice) {
2301 const size_t initialNumDevices = mFakePolicy->getInputDevices().size();
2302
2303 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
2304 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2305 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2306 ASSERT_EQ(initialNumDevices + 1, mFakePolicy->getInputDevices().size());
2307
2308 // Find the test device by its name.
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002309 const std::vector<InputDeviceInfo> inputDevices = mFakePolicy->getInputDevices();
Chris Ye98d3f532020-10-01 21:48:59 -07002310 const auto& it =
2311 std::find_if(inputDevices.begin(), inputDevices.end(),
2312 [&keyboard](const InputDeviceInfo& info) {
2313 return info.getIdentifier().name == keyboard->getName();
2314 });
2315
2316 ASSERT_NE(it, inputDevices.end());
2317 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, it->getKeyboardType());
2318 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, it->getSources());
2319 ASSERT_EQ(0U, it->getMotionRanges().size());
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002320
2321 keyboard.reset();
2322 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2323 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2324 ASSERT_EQ(initialNumDevices, mFakePolicy->getInputDevices().size());
2325}
2326
2327TEST_F(InputReaderIntegrationTest, SendsEventsToInputListener) {
2328 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
2329 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2330
2331 NotifyConfigurationChangedArgs configChangedArgs;
2332 ASSERT_NO_FATAL_FAILURE(
2333 mTestListener->assertNotifyConfigurationChangedWasCalled(&configChangedArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002334 int32_t prevId = configChangedArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002335 nsecs_t prevTimestamp = configChangedArgs.eventTime;
2336
2337 NotifyKeyArgs keyArgs;
2338 keyboard->pressAndReleaseHomeKey();
2339 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
2340 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002341 ASSERT_NE(prevId, keyArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002342 prevId = keyArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002343 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002344 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002345 prevTimestamp = keyArgs.eventTime;
2346
2347 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
2348 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002349 ASSERT_NE(prevId, keyArgs.id);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002350 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002351 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002352}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002353
Siarhei Vishniakoua0d2b802020-05-13 14:00:31 -07002354/**
2355 * The Steam controller sends BTN_GEAR_DOWN and BTN_GEAR_UP for the two "paddle" buttons
2356 * on the back. In this test, we make sure that BTN_GEAR_DOWN / BTN_WHEEL and BTN_GEAR_UP
2357 * are passed to the listener.
2358 */
2359static_assert(BTN_GEAR_DOWN == BTN_WHEEL);
2360TEST_F(InputReaderIntegrationTest, SendsGearDownAndUpToInputListener) {
2361 std::unique_ptr<UinputSteamController> controller = createUinputDevice<UinputSteamController>();
2362 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2363 NotifyKeyArgs keyArgs;
2364
2365 controller->pressAndReleaseKey(BTN_GEAR_DOWN);
2366 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
2367 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
2368 ASSERT_EQ(BTN_GEAR_DOWN, keyArgs.scanCode);
2369
2370 controller->pressAndReleaseKey(BTN_GEAR_UP);
2371 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
2372 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
2373 ASSERT_EQ(BTN_GEAR_UP, keyArgs.scanCode);
2374}
2375
Arthur Hungaab25622020-01-16 11:22:11 +08002376// --- TouchProcessTest ---
2377class TouchIntegrationTest : public InputReaderIntegrationTest {
2378protected:
Arthur Hungaab25622020-01-16 11:22:11 +08002379 const std::string UNIQUE_ID = "local:0";
2380
Chris Yea52ade12020-08-27 16:49:20 -07002381 void SetUp() override {
Arthur Hungaab25622020-01-16 11:22:11 +08002382 InputReaderIntegrationTest::SetUp();
2383 // At least add an internal display.
2384 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2385 DISPLAY_ORIENTATION_0, UNIQUE_ID, NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002386 ViewportType::INTERNAL);
Arthur Hungaab25622020-01-16 11:22:11 +08002387
2388 mDevice = createUinputDevice<UinputTouchScreen>(Rect(0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT));
2389 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2390 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2391 }
2392
2393 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
2394 int32_t orientation, const std::string& uniqueId,
2395 std::optional<uint8_t> physicalPort,
2396 ViewportType viewportType) {
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002397 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, true /*isActive*/,
2398 uniqueId, physicalPort, viewportType);
Arthur Hungaab25622020-01-16 11:22:11 +08002399 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2400 }
2401
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002402 void assertReceivedMotion(int32_t action, const std::vector<Point>& points) {
2403 NotifyMotionArgs args;
2404 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2405 EXPECT_EQ(action, args.action);
2406 ASSERT_EQ(points.size(), args.pointerCount);
2407 for (size_t i = 0; i < args.pointerCount; i++) {
2408 EXPECT_EQ(points[i].x, args.pointerCoords[i].getX());
2409 EXPECT_EQ(points[i].y, args.pointerCoords[i].getY());
2410 }
2411 }
2412
Arthur Hungaab25622020-01-16 11:22:11 +08002413 std::unique_ptr<UinputTouchScreen> mDevice;
2414};
2415
2416TEST_F(TouchIntegrationTest, InputEvent_ProcessSingleTouch) {
2417 NotifyMotionArgs args;
2418 const Point centerPoint = mDevice->getCenterPoint();
2419
2420 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00002421 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002422 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002423 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002424 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2425 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2426
2427 // ACTION_MOVE
2428 mDevice->sendMove(centerPoint + Point(1, 1));
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002429 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002430 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2431 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2432
2433 // ACTION_UP
2434 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002435 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002436 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2437 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2438}
2439
2440TEST_F(TouchIntegrationTest, InputEvent_ProcessMultiTouch) {
2441 NotifyMotionArgs args;
2442 const Point centerPoint = mDevice->getCenterPoint();
2443
2444 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00002445 mDevice->sendSlot(FIRST_SLOT);
2446 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002447 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002448 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002449 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2450 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2451
2452 // ACTION_POINTER_DOWN (Second slot)
2453 const Point secondPoint = centerPoint + Point(100, 100);
2454 mDevice->sendSlot(SECOND_SLOT);
2455 mDevice->sendTrackingId(SECOND_TRACKING_ID);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002456 mDevice->sendDown(secondPoint);
2457 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002458 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08002459 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002460
2461 // ACTION_MOVE (Second slot)
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002462 mDevice->sendMove(secondPoint + Point(1, 1));
2463 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002464 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2465 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2466
2467 // ACTION_POINTER_UP (Second slot)
arthurhungcc7f9802020-04-30 17:55:40 +08002468 mDevice->sendPointerUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002469 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002470 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08002471 ASSERT_EQ(ACTION_POINTER_1_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002472
2473 // ACTION_UP
2474 mDevice->sendSlot(FIRST_SLOT);
2475 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002476 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002477 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2478 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2479}
2480
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002481/**
2482 * What happens when a pointer goes up while another pointer moves in the same frame? Are POINTER_UP
2483 * events guaranteed to contain the same data as a preceding MOVE, or can they contain different
2484 * data?
2485 * In this test, we try to send a change in coordinates in Pointer 0 in the same frame as the
2486 * liftoff of Pointer 1. We check that POINTER_UP event is generated first, and the MOVE event
2487 * for Pointer 0 only is generated after.
2488 * Suppose we are only interested in learning the movement of Pointer 0. If we only observe MOVE
2489 * events, we will not miss any information.
2490 * Even though the Pointer 1 up event contains updated Pointer 0 coordinates, there is another MOVE
2491 * event generated afterwards that contains the newest movement of pointer 0.
2492 * This is important for palm rejection. If there is a subsequent InputListener stage that detects
2493 * palms, and wants to cancel Pointer 1, then it is safe to simply drop POINTER_1_UP event without
2494 * losing information about non-palm pointers.
2495 */
2496TEST_F(TouchIntegrationTest, MultiTouch_PointerMoveAndSecondPointerUp) {
2497 NotifyMotionArgs args;
2498 const Point centerPoint = mDevice->getCenterPoint();
2499
2500 // ACTION_DOWN
2501 mDevice->sendSlot(FIRST_SLOT);
2502 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2503 mDevice->sendDown(centerPoint);
2504 mDevice->sendSync();
2505 assertReceivedMotion(AMOTION_EVENT_ACTION_DOWN, {centerPoint});
2506
2507 // ACTION_POINTER_DOWN (Second slot)
2508 const Point secondPoint = centerPoint + Point(100, 100);
2509 mDevice->sendSlot(SECOND_SLOT);
2510 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2511 mDevice->sendDown(secondPoint);
2512 mDevice->sendSync();
2513 assertReceivedMotion(ACTION_POINTER_1_DOWN, {centerPoint, secondPoint});
2514
2515 // ACTION_MOVE (First slot)
2516 mDevice->sendSlot(FIRST_SLOT);
2517 mDevice->sendMove(centerPoint + Point(5, 5));
2518 // ACTION_POINTER_UP (Second slot)
2519 mDevice->sendSlot(SECOND_SLOT);
2520 mDevice->sendPointerUp();
2521 // Send a single sync for the above 2 pointer updates
2522 mDevice->sendSync();
2523
2524 // First, we should get POINTER_UP for the second pointer
2525 assertReceivedMotion(ACTION_POINTER_1_UP,
2526 {/*first pointer */ centerPoint + Point(5, 5),
2527 /*second pointer*/ secondPoint});
2528
2529 // Next, the MOVE event for the first pointer
2530 assertReceivedMotion(AMOTION_EVENT_ACTION_MOVE, {centerPoint + Point(5, 5)});
2531}
2532
2533/**
2534 * Similar scenario as above. The difference is that when the second pointer goes up, it will first
2535 * move, and then it will go up, all in the same frame.
2536 * In this scenario, the movement of the second pointer just prior to liftoff is ignored, and never
2537 * gets sent to the listener.
2538 */
2539TEST_F(TouchIntegrationTest, MultiTouch_PointerMoveAndSecondPointerMoveAndUp) {
2540 NotifyMotionArgs args;
2541 const Point centerPoint = mDevice->getCenterPoint();
2542
2543 // ACTION_DOWN
2544 mDevice->sendSlot(FIRST_SLOT);
2545 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2546 mDevice->sendDown(centerPoint);
2547 mDevice->sendSync();
2548 assertReceivedMotion(AMOTION_EVENT_ACTION_DOWN, {centerPoint});
2549
2550 // ACTION_POINTER_DOWN (Second slot)
2551 const Point secondPoint = centerPoint + Point(100, 100);
2552 mDevice->sendSlot(SECOND_SLOT);
2553 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2554 mDevice->sendDown(secondPoint);
2555 mDevice->sendSync();
2556 assertReceivedMotion(ACTION_POINTER_1_DOWN, {centerPoint, secondPoint});
2557
2558 // ACTION_MOVE (First slot)
2559 mDevice->sendSlot(FIRST_SLOT);
2560 mDevice->sendMove(centerPoint + Point(5, 5));
2561 // ACTION_POINTER_UP (Second slot)
2562 mDevice->sendSlot(SECOND_SLOT);
2563 mDevice->sendMove(secondPoint + Point(6, 6));
2564 mDevice->sendPointerUp();
2565 // Send a single sync for the above 2 pointer updates
2566 mDevice->sendSync();
2567
2568 // First, we should get POINTER_UP for the second pointer
2569 // The movement of the second pointer during the liftoff frame is ignored.
2570 // The coordinates 'secondPoint + Point(6, 6)' are never sent to the listener.
2571 assertReceivedMotion(ACTION_POINTER_1_UP,
2572 {/*first pointer */ centerPoint + Point(5, 5),
2573 /*second pointer*/ secondPoint});
2574
2575 // Next, the MOVE event for the first pointer
2576 assertReceivedMotion(AMOTION_EVENT_ACTION_MOVE, {centerPoint + Point(5, 5)});
2577}
2578
Arthur Hungaab25622020-01-16 11:22:11 +08002579TEST_F(TouchIntegrationTest, InputEvent_ProcessPalm) {
2580 NotifyMotionArgs args;
2581 const Point centerPoint = mDevice->getCenterPoint();
2582
2583 // ACTION_DOWN
arthurhungcc7f9802020-04-30 17:55:40 +08002584 mDevice->sendSlot(FIRST_SLOT);
2585 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002586 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002587 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002588 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2589 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2590
arthurhungcc7f9802020-04-30 17:55:40 +08002591 // ACTION_POINTER_DOWN (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002592 const Point secondPoint = centerPoint + Point(100, 100);
2593 mDevice->sendSlot(SECOND_SLOT);
2594 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2595 mDevice->sendDown(secondPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002596 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002597 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08002598 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002599
arthurhungcc7f9802020-04-30 17:55:40 +08002600 // ACTION_MOVE (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002601 mDevice->sendMove(secondPoint + Point(1, 1));
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002602 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002603 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2604 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2605
arthurhungcc7f9802020-04-30 17:55:40 +08002606 // Send MT_TOOL_PALM (second slot), which indicates that the touch IC has determined this to be
2607 // a palm event.
2608 // Expect to receive the ACTION_POINTER_UP with cancel flag.
Arthur Hungaab25622020-01-16 11:22:11 +08002609 mDevice->sendToolType(MT_TOOL_PALM);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002610 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002611 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08002612 ASSERT_EQ(ACTION_POINTER_1_UP, args.action);
arthurhungcc7f9802020-04-30 17:55:40 +08002613 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, args.flags);
Arthur Hungaab25622020-01-16 11:22:11 +08002614
arthurhungcc7f9802020-04-30 17:55:40 +08002615 // Send up to second slot, expect first slot send moving.
2616 mDevice->sendPointerUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002617 mDevice->sendSync();
arthurhungcc7f9802020-04-30 17:55:40 +08002618 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2619 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002620
arthurhungcc7f9802020-04-30 17:55:40 +08002621 // Send ACTION_UP (first slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002622 mDevice->sendSlot(FIRST_SLOT);
2623 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002624 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002625
arthurhungcc7f9802020-04-30 17:55:40 +08002626 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2627 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002628}
2629
Michael Wrightd02c5b62014-02-10 15:10:22 -08002630// --- InputDeviceTest ---
Michael Wrightd02c5b62014-02-10 15:10:22 -08002631class InputDeviceTest : public testing::Test {
2632protected:
2633 static const char* DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002634 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002635 static const int32_t DEVICE_ID;
2636 static const int32_t DEVICE_GENERATION;
2637 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002638 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002639 static const int32_t EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002640
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002641 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002642 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002643 std::unique_ptr<TestInputListener> mFakeListener;
arthurhungdcef2dc2020-08-11 14:47:50 +08002644 std::unique_ptr<InstrumentedInputReader> mReader;
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00002645 std::shared_ptr<InputDevice> mDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002646
Chris Yea52ade12020-08-27 16:49:20 -07002647 void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002648 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002649 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002650 mFakeListener = std::make_unique<TestInputListener>();
arthurhungdcef2dc2020-08-11 14:47:50 +08002651 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002652 *mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002653 InputDeviceIdentifier identifier;
2654 identifier.name = DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002655 identifier.location = DEVICE_LOCATION;
arthurhungdcef2dc2020-08-11 14:47:50 +08002656 mDevice = std::make_shared<InputDevice>(mReader->getContext(), DEVICE_ID, DEVICE_GENERATION,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002657 identifier);
arthurhungdcef2dc2020-08-11 14:47:50 +08002658 mReader->pushNextDevice(mDevice);
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002659 mFakeEventHub->addDevice(EVENTHUB_ID, DEVICE_NAME, ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08002660 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002661 }
2662
Chris Yea52ade12020-08-27 16:49:20 -07002663 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002664 mFakeListener.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002665 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002666 }
2667};
2668
2669const char* InputDeviceTest::DEVICE_NAME = "device";
Arthur Hung2c9a3342019-07-23 14:18:59 +08002670const char* InputDeviceTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002671const int32_t InputDeviceTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002672const int32_t InputDeviceTest::DEVICE_GENERATION = 2;
2673const int32_t InputDeviceTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002674const ftl::Flags<InputDeviceClass> InputDeviceTest::DEVICE_CLASSES =
Chris Ye1b0c7342020-07-28 21:57:03 -07002675 InputDeviceClass::KEYBOARD | InputDeviceClass::TOUCH | InputDeviceClass::JOYSTICK;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002676const int32_t InputDeviceTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002677
2678TEST_F(InputDeviceTest, ImmutableProperties) {
2679 ASSERT_EQ(DEVICE_ID, mDevice->getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002680 ASSERT_STREQ(DEVICE_NAME, mDevice->getName().c_str());
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002681 ASSERT_EQ(ftl::Flags<InputDeviceClass>(0), mDevice->getClasses());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002682}
2683
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002684TEST_F(InputDeviceTest, WhenDeviceCreated_EnabledIsFalse) {
2685 ASSERT_EQ(mDevice->isEnabled(), false);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07002686}
2687
Michael Wrightd02c5b62014-02-10 15:10:22 -08002688TEST_F(InputDeviceTest, WhenNoMappersAreRegistered_DeviceIsIgnored) {
2689 // Configuration.
2690 InputReaderConfiguration config;
2691 mDevice->configure(ARBITRARY_TIME, &config, 0);
2692
2693 // Reset.
2694 mDevice->reset(ARBITRARY_TIME);
2695
2696 NotifyDeviceResetArgs resetArgs;
2697 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2698 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2699 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2700
2701 // Metadata.
2702 ASSERT_TRUE(mDevice->isIgnored());
2703 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mDevice->getSources());
2704
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002705 InputDeviceInfo info = mDevice->getDeviceInfo();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002706 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002707 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002708 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NONE, info.getKeyboardType());
2709 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, info.getSources());
2710
2711 // State queries.
2712 ASSERT_EQ(0, mDevice->getMetaState());
2713
2714 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2715 << "Ignored device should return unknown key code state.";
2716 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2717 << "Ignored device should return unknown scan code state.";
2718 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 0))
2719 << "Ignored device should return unknown switch state.";
2720
2721 const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
2722 uint8_t flags[2] = { 0, 1 };
2723 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, 2, keyCodes, flags))
2724 << "Ignored device should never mark any key codes.";
2725 ASSERT_EQ(0, flags[0]) << "Flag for unsupported key should be unchanged.";
2726 ASSERT_EQ(1, flags[1]) << "Flag for unsupported key should be unchanged.";
2727}
2728
2729TEST_F(InputDeviceTest, WhenMappersAreRegistered_DeviceIsNotIgnoredAndForwardsRequestsToMappers) {
2730 // Configuration.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002731 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, String8("key"), String8("value"));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002732
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002733 FakeInputMapper& mapper1 =
2734 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002735 mapper1.setKeyboardType(AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2736 mapper1.setMetaState(AMETA_ALT_ON);
2737 mapper1.addSupportedKeyCode(AKEYCODE_A);
2738 mapper1.addSupportedKeyCode(AKEYCODE_B);
2739 mapper1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
2740 mapper1.setKeyCodeState(AKEYCODE_B, AKEY_STATE_UP);
2741 mapper1.setScanCodeState(2, AKEY_STATE_DOWN);
2742 mapper1.setScanCodeState(3, AKEY_STATE_UP);
2743 mapper1.setSwitchState(4, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002744
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002745 FakeInputMapper& mapper2 =
2746 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002747 mapper2.setMetaState(AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002748
2749 InputReaderConfiguration config;
2750 mDevice->configure(ARBITRARY_TIME, &config, 0);
2751
2752 String8 propertyValue;
2753 ASSERT_TRUE(mDevice->getConfiguration().tryGetProperty(String8("key"), propertyValue))
2754 << "Device should have read configuration during configuration phase.";
2755 ASSERT_STREQ("value", propertyValue.string());
2756
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002757 ASSERT_NO_FATAL_FAILURE(mapper1.assertConfigureWasCalled());
2758 ASSERT_NO_FATAL_FAILURE(mapper2.assertConfigureWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002759
2760 // Reset
2761 mDevice->reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002762 ASSERT_NO_FATAL_FAILURE(mapper1.assertResetWasCalled());
2763 ASSERT_NO_FATAL_FAILURE(mapper2.assertResetWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002764
2765 NotifyDeviceResetArgs resetArgs;
2766 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2767 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2768 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2769
2770 // Metadata.
2771 ASSERT_FALSE(mDevice->isIgnored());
2772 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), mDevice->getSources());
2773
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002774 InputDeviceInfo info = mDevice->getDeviceInfo();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002775 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002776 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002777 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_ALPHABETIC, info.getKeyboardType());
2778 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), info.getSources());
2779
2780 // State queries.
2781 ASSERT_EQ(AMETA_ALT_ON | AMETA_SHIFT_ON, mDevice->getMetaState())
2782 << "Should query mappers and combine meta states.";
2783
2784 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2785 << "Should return unknown key code state when source not supported.";
2786 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2787 << "Should return unknown scan code state when source not supported.";
2788 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2789 << "Should return unknown switch state when source not supported.";
2790
2791 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, AKEYCODE_A))
2792 << "Should query mapper when source is supported.";
2793 ASSERT_EQ(AKEY_STATE_UP, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 3))
2794 << "Should query mapper when source is supported.";
2795 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 4))
2796 << "Should query mapper when source is supported.";
2797
2798 const int32_t keyCodes[4] = { AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2 };
2799 uint8_t flags[4] = { 0, 0, 0, 1 };
2800 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
2801 << "Should do nothing when source is unsupported.";
2802 ASSERT_EQ(0, flags[0]) << "Flag should be unchanged when source is unsupported.";
2803 ASSERT_EQ(0, flags[1]) << "Flag should be unchanged when source is unsupported.";
2804 ASSERT_EQ(0, flags[2]) << "Flag should be unchanged when source is unsupported.";
2805 ASSERT_EQ(1, flags[3]) << "Flag should be unchanged when source is unsupported.";
2806
2807 ASSERT_TRUE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, 4, keyCodes, flags))
2808 << "Should query mapper when source is supported.";
2809 ASSERT_EQ(1, flags[0]) << "Flag for supported key should be set.";
2810 ASSERT_EQ(1, flags[1]) << "Flag for supported key should be set.";
2811 ASSERT_EQ(0, flags[2]) << "Flag for unsupported key should be unchanged.";
2812 ASSERT_EQ(1, flags[3]) << "Flag for unsupported key should be unchanged.";
2813
2814 // Event handling.
2815 RawEvent event;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002816 event.deviceId = EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002817 mDevice->process(&event, 1);
2818
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002819 ASSERT_NO_FATAL_FAILURE(mapper1.assertProcessWasCalled());
2820 ASSERT_NO_FATAL_FAILURE(mapper2.assertProcessWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002821}
2822
Arthur Hung2c9a3342019-07-23 14:18:59 +08002823// A single input device is associated with a specific display. Check that:
2824// 1. Device is disabled if the viewport corresponding to the associated display is not found
2825// 2. Device is disabled when setEnabled API is called
2826TEST_F(InputDeviceTest, Configure_AssignsDisplayPort) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002827 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002828
2829 // First Configuration.
2830 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
2831
2832 // Device should be enabled by default.
2833 ASSERT_TRUE(mDevice->isEnabled());
2834
2835 // Prepare associated info.
2836 constexpr uint8_t hdmi = 1;
2837 const std::string UNIQUE_ID = "local:1";
2838
2839 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi);
2840 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2841 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2842 // Device should be disabled because it is associated with a specific display via
2843 // input port <-> display port association, but the corresponding display is not found
2844 ASSERT_FALSE(mDevice->isEnabled());
2845
2846 // Prepare displays.
2847 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002848 DISPLAY_ORIENTATION_0, true /*isActive*/, UNIQUE_ID, hdmi,
2849 ViewportType::INTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002850 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2851 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2852 ASSERT_TRUE(mDevice->isEnabled());
2853
2854 // Device should be disabled after set disable.
2855 mFakePolicy->addDisabledDevice(mDevice->getId());
2856 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2857 InputReaderConfiguration::CHANGE_ENABLED_STATE);
2858 ASSERT_FALSE(mDevice->isEnabled());
2859
2860 // Device should still be disabled even found the associated display.
2861 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2862 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2863 ASSERT_FALSE(mDevice->isEnabled());
2864}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002865
Christine Franks1ba71cc2021-04-07 14:37:42 -07002866TEST_F(InputDeviceTest, Configure_AssignsDisplayUniqueId) {
2867 // Device should be enabled by default.
2868 mFakePolicy->clearViewports();
2869 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
2870 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
2871 ASSERT_TRUE(mDevice->isEnabled());
2872
2873 // Device should be disabled because it is associated with a specific display, but the
2874 // corresponding display is not found.
2875 const std::string DISPLAY_UNIQUE_ID = "displayUniqueId";
Christine Franks2a2293c2022-01-18 11:51:16 -08002876 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002877 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2878 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2879 ASSERT_FALSE(mDevice->isEnabled());
2880
2881 // Device should be enabled when a display is found.
2882 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2883 DISPLAY_ORIENTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
2884 NO_PORT, ViewportType::INTERNAL);
2885 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2886 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2887 ASSERT_TRUE(mDevice->isEnabled());
2888
2889 // Device should be disabled after set disable.
2890 mFakePolicy->addDisabledDevice(mDevice->getId());
2891 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2892 InputReaderConfiguration::CHANGE_ENABLED_STATE);
2893 ASSERT_FALSE(mDevice->isEnabled());
2894
2895 // Device should still be disabled even found the associated display.
2896 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2897 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2898 ASSERT_FALSE(mDevice->isEnabled());
2899}
2900
Christine Franks2a2293c2022-01-18 11:51:16 -08002901TEST_F(InputDeviceTest, Configure_UniqueId_CorrectlyMatches) {
2902 mFakePolicy->clearViewports();
2903 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
2904 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
2905
2906 const std::string DISPLAY_UNIQUE_ID = "displayUniqueId";
2907 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
2908 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2909 DISPLAY_ORIENTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
2910 NO_PORT, ViewportType::INTERNAL);
2911 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2912 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2913 ASSERT_EQ(DISPLAY_UNIQUE_ID, mDevice->getAssociatedDisplayUniqueId());
2914}
2915
Michael Wrightd02c5b62014-02-10 15:10:22 -08002916// --- InputMapperTest ---
2917
2918class InputMapperTest : public testing::Test {
2919protected:
2920 static const char* DEVICE_NAME;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002921 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002922 static const int32_t DEVICE_ID;
2923 static const int32_t DEVICE_GENERATION;
2924 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002925 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002926 static const int32_t EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002927
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002928 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002929 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002930 std::unique_ptr<TestInputListener> mFakeListener;
arthurhungdcef2dc2020-08-11 14:47:50 +08002931 std::unique_ptr<InstrumentedInputReader> mReader;
2932 std::shared_ptr<InputDevice> mDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002933
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002934 virtual void SetUp(ftl::Flags<InputDeviceClass> classes) {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002935 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002936 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002937 mFakeListener = std::make_unique<TestInputListener>();
arthurhungdcef2dc2020-08-11 14:47:50 +08002938 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002939 *mFakeListener);
arthurhungdcef2dc2020-08-11 14:47:50 +08002940 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002941 }
2942
Prabir Pradhanc14266f2021-05-12 15:56:24 -07002943 void SetUp() override {
Prabir Pradhanc14266f2021-05-12 15:56:24 -07002944 SetUp(DEVICE_CLASSES);
2945 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002946
Chris Yea52ade12020-08-27 16:49:20 -07002947 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002948 mFakeListener.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002949 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002950 }
2951
2952 void addConfigurationProperty(const char* key, const char* value) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002953 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, String8(key), String8(value));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002954 }
2955
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002956 void configureDevice(uint32_t changes) {
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00002957 if (!changes ||
2958 (changes &
2959 (InputReaderConfiguration::CHANGE_DISPLAY_INFO |
2960 InputReaderConfiguration::CHANGE_POINTER_CAPTURE))) {
arthurhungdcef2dc2020-08-11 14:47:50 +08002961 mReader->requestRefreshConfiguration(changes);
2962 mReader->loopOnce();
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -08002963 }
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002964 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
2965 }
2966
arthurhungdcef2dc2020-08-11 14:47:50 +08002967 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
2968 const std::string& location, int32_t eventHubId,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002969 ftl::Flags<InputDeviceClass> classes) {
arthurhungdcef2dc2020-08-11 14:47:50 +08002970 InputDeviceIdentifier identifier;
2971 identifier.name = name;
2972 identifier.location = location;
2973 std::shared_ptr<InputDevice> device =
2974 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
2975 identifier);
2976 mReader->pushNextDevice(device);
2977 mFakeEventHub->addDevice(eventHubId, name, classes);
2978 mReader->loopOnce();
2979 return device;
2980 }
2981
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002982 template <class T, typename... Args>
2983 T& addMapperAndConfigure(Args... args) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002984 T& mapper = mDevice->addMapper<T>(EVENTHUB_ID, args...);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002985 configureDevice(0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002986 mDevice->reset(ARBITRARY_TIME);
Chris Ye42b06822020-08-07 11:39:33 -07002987 mapper.reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002988 return mapper;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002989 }
2990
2991 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002992 int32_t orientation, const std::string& uniqueId,
2993 std::optional<uint8_t> physicalPort, ViewportType viewportType) {
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002994 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, true /*isActive*/,
2995 uniqueId, physicalPort, viewportType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07002996 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2997 }
2998
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002999 void clearViewports() {
3000 mFakePolicy->clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003001 }
3002
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003003 void process(InputMapper& mapper, nsecs_t when, nsecs_t readTime, int32_t type, int32_t code,
3004 int32_t value) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003005 RawEvent event;
3006 event.when = when;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003007 event.readTime = readTime;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003008 event.deviceId = mapper.getDeviceContext().getEventHubId();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003009 event.type = type;
3010 event.code = code;
3011 event.value = value;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003012 mapper.process(&event);
arthurhungdcef2dc2020-08-11 14:47:50 +08003013 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003014 }
3015
3016 static void assertMotionRange(const InputDeviceInfo& info,
3017 int32_t axis, uint32_t source, float min, float max, float flat, float fuzz) {
3018 const InputDeviceInfo::MotionRange* range = info.getMotionRange(axis, source);
Yi Kong9b14ac62018-07-17 13:48:38 -07003019 ASSERT_TRUE(range != nullptr) << "Axis: " << axis << " Source: " << source;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003020 ASSERT_EQ(axis, range->axis) << "Axis: " << axis << " Source: " << source;
3021 ASSERT_EQ(source, range->source) << "Axis: " << axis << " Source: " << source;
3022 ASSERT_NEAR(min, range->min, EPSILON) << "Axis: " << axis << " Source: " << source;
3023 ASSERT_NEAR(max, range->max, EPSILON) << "Axis: " << axis << " Source: " << source;
3024 ASSERT_NEAR(flat, range->flat, EPSILON) << "Axis: " << axis << " Source: " << source;
3025 ASSERT_NEAR(fuzz, range->fuzz, EPSILON) << "Axis: " << axis << " Source: " << source;
3026 }
3027
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003028 static void assertPointerCoords(const PointerCoords& coords, float x, float y, float pressure,
3029 float size, float touchMajor, float touchMinor, float toolMajor,
3030 float toolMinor, float orientation, float distance,
3031 float scaledAxisEpsilon = 1.f) {
3032 ASSERT_NEAR(x, coords.getAxisValue(AMOTION_EVENT_AXIS_X), scaledAxisEpsilon);
3033 ASSERT_NEAR(y, coords.getAxisValue(AMOTION_EVENT_AXIS_Y), scaledAxisEpsilon);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003034 ASSERT_NEAR(pressure, coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), EPSILON);
3035 ASSERT_NEAR(size, coords.getAxisValue(AMOTION_EVENT_AXIS_SIZE), EPSILON);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003036 ASSERT_NEAR(touchMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR),
3037 scaledAxisEpsilon);
3038 ASSERT_NEAR(touchMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR),
3039 scaledAxisEpsilon);
3040 ASSERT_NEAR(toolMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR),
3041 scaledAxisEpsilon);
3042 ASSERT_NEAR(toolMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR),
3043 scaledAxisEpsilon);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003044 ASSERT_NEAR(orientation, coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION), EPSILON);
3045 ASSERT_NEAR(distance, coords.getAxisValue(AMOTION_EVENT_AXIS_DISTANCE), EPSILON);
3046 }
3047
Michael Wright17db18e2020-06-26 20:51:44 +01003048 static void assertPosition(const FakePointerController& controller, float x, float y) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003049 float actualX, actualY;
Michael Wright17db18e2020-06-26 20:51:44 +01003050 controller.getPosition(&actualX, &actualY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003051 ASSERT_NEAR(x, actualX, 1);
3052 ASSERT_NEAR(y, actualY, 1);
3053 }
3054};
3055
3056const char* InputMapperTest::DEVICE_NAME = "device";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003057const char* InputMapperTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003058const int32_t InputMapperTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003059const int32_t InputMapperTest::DEVICE_GENERATION = 2;
3060const int32_t InputMapperTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003061const ftl::Flags<InputDeviceClass> InputMapperTest::DEVICE_CLASSES =
3062 ftl::Flags<InputDeviceClass>(0); // not needed for current tests
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003063const int32_t InputMapperTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003064
3065// --- SwitchInputMapperTest ---
3066
3067class SwitchInputMapperTest : public InputMapperTest {
3068protected:
3069};
3070
3071TEST_F(SwitchInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003072 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003073
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003074 ASSERT_EQ(uint32_t(AINPUT_SOURCE_SWITCH), mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003075}
3076
3077TEST_F(SwitchInputMapperTest, GetSwitchState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003078 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003079
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003080 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003081 ASSERT_EQ(1, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003082
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003083 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003084 ASSERT_EQ(0, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003085}
3086
3087TEST_F(SwitchInputMapperTest, Process) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003088 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003089
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003090 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_LID, 1);
3091 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_JACK_PHYSICAL_INSERT, 1);
3092 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_HEADPHONE_INSERT, 0);
3093 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003094
3095 NotifySwitchArgs args;
3096 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySwitchWasCalled(&args));
3097 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
Dan Albert1bd2fc02016-02-02 15:11:57 -08003098 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT), args.switchValues);
3099 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT) | (1 << SW_HEADPHONE_INSERT),
Michael Wrightd02c5b62014-02-10 15:10:22 -08003100 args.switchMask);
3101 ASSERT_EQ(uint32_t(0), args.policyFlags);
3102}
3103
Chris Ye87143712020-11-10 05:05:58 +00003104// --- VibratorInputMapperTest ---
3105class VibratorInputMapperTest : public InputMapperTest {
3106protected:
3107 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::VIBRATOR); }
3108};
3109
3110TEST_F(VibratorInputMapperTest, GetSources) {
3111 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
3112
3113 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mapper.getSources());
3114}
3115
3116TEST_F(VibratorInputMapperTest, GetVibratorIds) {
3117 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
3118
3119 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
3120}
3121
3122TEST_F(VibratorInputMapperTest, Vibrate) {
3123 constexpr uint8_t DEFAULT_AMPLITUDE = 192;
Chris Yefb552902021-02-03 17:18:37 -08003124 constexpr int32_t VIBRATION_TOKEN = 100;
Chris Ye87143712020-11-10 05:05:58 +00003125 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
3126
3127 VibrationElement pattern(2);
3128 VibrationSequence sequence(2);
3129 pattern.duration = std::chrono::milliseconds(200);
3130 pattern.channels = {{0 /* vibratorId */, DEFAULT_AMPLITUDE / 2},
3131 {1 /* vibratorId */, DEFAULT_AMPLITUDE}};
3132 sequence.addElement(pattern);
3133 pattern.duration = std::chrono::milliseconds(500);
3134 pattern.channels = {{0 /* vibratorId */, DEFAULT_AMPLITUDE / 4},
3135 {1 /* vibratorId */, DEFAULT_AMPLITUDE}};
3136 sequence.addElement(pattern);
3137
3138 std::vector<int64_t> timings = {0, 1};
3139 std::vector<uint8_t> amplitudes = {DEFAULT_AMPLITUDE, DEFAULT_AMPLITUDE / 2};
3140
3141 ASSERT_FALSE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08003142 // Start vibrating
3143 mapper.vibrate(sequence, -1 /* repeat */, VIBRATION_TOKEN);
Chris Ye87143712020-11-10 05:05:58 +00003144 ASSERT_TRUE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08003145 // Verify vibrator state listener was notified.
3146 mReader->loopOnce();
3147 NotifyVibratorStateArgs args;
3148 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyVibratorStateWasCalled(&args));
3149 ASSERT_EQ(DEVICE_ID, args.deviceId);
3150 ASSERT_TRUE(args.isOn);
3151 // Stop vibrating
3152 mapper.cancelVibrate(VIBRATION_TOKEN);
3153 ASSERT_FALSE(mapper.isVibrating());
3154 // Verify vibrator state listener was notified.
3155 mReader->loopOnce();
3156 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyVibratorStateWasCalled(&args));
3157 ASSERT_EQ(DEVICE_ID, args.deviceId);
3158 ASSERT_FALSE(args.isOn);
Chris Ye87143712020-11-10 05:05:58 +00003159}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003160
Chris Yef59a2f42020-10-16 12:55:26 -07003161// --- SensorInputMapperTest ---
3162
3163class SensorInputMapperTest : public InputMapperTest {
3164protected:
3165 static const int32_t ACCEL_RAW_MIN;
3166 static const int32_t ACCEL_RAW_MAX;
3167 static const int32_t ACCEL_RAW_FUZZ;
3168 static const int32_t ACCEL_RAW_FLAT;
3169 static const int32_t ACCEL_RAW_RESOLUTION;
3170
3171 static const int32_t GYRO_RAW_MIN;
3172 static const int32_t GYRO_RAW_MAX;
3173 static const int32_t GYRO_RAW_FUZZ;
3174 static const int32_t GYRO_RAW_FLAT;
3175 static const int32_t GYRO_RAW_RESOLUTION;
3176
3177 static const float GRAVITY_MS2_UNIT;
3178 static const float DEGREE_RADIAN_UNIT;
3179
3180 void prepareAccelAxes();
3181 void prepareGyroAxes();
3182 void setAccelProperties();
3183 void setGyroProperties();
3184 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::SENSOR); }
3185};
3186
3187const int32_t SensorInputMapperTest::ACCEL_RAW_MIN = -32768;
3188const int32_t SensorInputMapperTest::ACCEL_RAW_MAX = 32768;
3189const int32_t SensorInputMapperTest::ACCEL_RAW_FUZZ = 16;
3190const int32_t SensorInputMapperTest::ACCEL_RAW_FLAT = 0;
3191const int32_t SensorInputMapperTest::ACCEL_RAW_RESOLUTION = 8192;
3192
3193const int32_t SensorInputMapperTest::GYRO_RAW_MIN = -2097152;
3194const int32_t SensorInputMapperTest::GYRO_RAW_MAX = 2097152;
3195const int32_t SensorInputMapperTest::GYRO_RAW_FUZZ = 16;
3196const int32_t SensorInputMapperTest::GYRO_RAW_FLAT = 0;
3197const int32_t SensorInputMapperTest::GYRO_RAW_RESOLUTION = 1024;
3198
3199const float SensorInputMapperTest::GRAVITY_MS2_UNIT = 9.80665f;
3200const float SensorInputMapperTest::DEGREE_RADIAN_UNIT = 0.0174533f;
3201
3202void SensorInputMapperTest::prepareAccelAxes() {
3203 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3204 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3205 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3206 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3207 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Z, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3208 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3209}
3210
3211void SensorInputMapperTest::prepareGyroAxes() {
3212 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RX, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3213 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3214 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RY, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3215 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3216 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RZ, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3217 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3218}
3219
3220void SensorInputMapperTest::setAccelProperties() {
3221 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 0, InputDeviceSensorType::ACCELEROMETER,
3222 /* sensorDataIndex */ 0);
3223 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 1, InputDeviceSensorType::ACCELEROMETER,
3224 /* sensorDataIndex */ 1);
3225 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 2, InputDeviceSensorType::ACCELEROMETER,
3226 /* sensorDataIndex */ 2);
3227 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
3228 addConfigurationProperty("sensor.accelerometer.reportingMode", "0");
3229 addConfigurationProperty("sensor.accelerometer.maxDelay", "100000");
3230 addConfigurationProperty("sensor.accelerometer.minDelay", "5000");
3231 addConfigurationProperty("sensor.accelerometer.power", "1.5");
3232}
3233
3234void SensorInputMapperTest::setGyroProperties() {
3235 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 3, InputDeviceSensorType::GYROSCOPE,
3236 /* sensorDataIndex */ 0);
3237 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 4, InputDeviceSensorType::GYROSCOPE,
3238 /* sensorDataIndex */ 1);
3239 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 5, InputDeviceSensorType::GYROSCOPE,
3240 /* sensorDataIndex */ 2);
3241 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
3242 addConfigurationProperty("sensor.gyroscope.reportingMode", "0");
3243 addConfigurationProperty("sensor.gyroscope.maxDelay", "100000");
3244 addConfigurationProperty("sensor.gyroscope.minDelay", "5000");
3245 addConfigurationProperty("sensor.gyroscope.power", "0.8");
3246}
3247
3248TEST_F(SensorInputMapperTest, GetSources) {
3249 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3250
3251 ASSERT_EQ(static_cast<uint32_t>(AINPUT_SOURCE_SENSOR), mapper.getSources());
3252}
3253
3254TEST_F(SensorInputMapperTest, ProcessAccelerometerSensor) {
3255 setAccelProperties();
3256 prepareAccelAxes();
3257 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3258
3259 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::ACCELEROMETER,
3260 std::chrono::microseconds(10000),
3261 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08003262 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003263 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, 20000);
3264 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, -20000);
3265 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Z, 40000);
3266 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
3267 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07003268
3269 NotifySensorArgs args;
3270 std::vector<float> values = {20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
3271 -20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
3272 40000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT};
3273
3274 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
3275 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
3276 ASSERT_EQ(args.deviceId, DEVICE_ID);
3277 ASSERT_EQ(args.sensorType, InputDeviceSensorType::ACCELEROMETER);
3278 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
3279 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
3280 ASSERT_EQ(args.values, values);
3281 mapper.flushSensor(InputDeviceSensorType::ACCELEROMETER);
3282}
3283
3284TEST_F(SensorInputMapperTest, ProcessGyroscopeSensor) {
3285 setGyroProperties();
3286 prepareGyroAxes();
3287 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3288
3289 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::GYROSCOPE,
3290 std::chrono::microseconds(10000),
3291 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08003292 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003293 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RX, 20000);
3294 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RY, -20000);
3295 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RZ, 40000);
3296 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
3297 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07003298
3299 NotifySensorArgs args;
3300 std::vector<float> values = {20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
3301 -20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
3302 40000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT};
3303
3304 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
3305 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
3306 ASSERT_EQ(args.deviceId, DEVICE_ID);
3307 ASSERT_EQ(args.sensorType, InputDeviceSensorType::GYROSCOPE);
3308 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
3309 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
3310 ASSERT_EQ(args.values, values);
3311 mapper.flushSensor(InputDeviceSensorType::GYROSCOPE);
3312}
3313
Michael Wrightd02c5b62014-02-10 15:10:22 -08003314// --- KeyboardInputMapperTest ---
3315
3316class KeyboardInputMapperTest : public InputMapperTest {
3317protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003318 const std::string UNIQUE_ID = "local:0";
3319
3320 void prepareDisplay(int32_t orientation);
3321
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003322 void testDPadKeyRotation(KeyboardInputMapper& mapper, int32_t originalScanCode,
Arthur Hung2c9a3342019-07-23 14:18:59 +08003323 int32_t originalKeyCode, int32_t rotatedKeyCode,
3324 int32_t displayId = ADISPLAY_ID_NONE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003325};
3326
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003327/* Similar to setDisplayInfoAndReconfigure, but pre-populates all parameters except for the
3328 * orientation.
3329 */
3330void KeyboardInputMapperTest::prepareDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003331 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
3332 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003333}
3334
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003335void KeyboardInputMapperTest::testDPadKeyRotation(KeyboardInputMapper& mapper,
Arthur Hung2c9a3342019-07-23 14:18:59 +08003336 int32_t originalScanCode, int32_t originalKeyCode,
3337 int32_t rotatedKeyCode, int32_t displayId) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003338 NotifyKeyArgs args;
3339
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003340 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003341 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3342 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3343 ASSERT_EQ(originalScanCode, args.scanCode);
3344 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003345 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003346
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003347 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003348 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3349 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3350 ASSERT_EQ(originalScanCode, args.scanCode);
3351 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003352 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003353}
3354
Michael Wrightd02c5b62014-02-10 15:10:22 -08003355TEST_F(KeyboardInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003356 KeyboardInputMapper& mapper =
3357 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3358 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003359
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003360 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003361}
3362
3363TEST_F(KeyboardInputMapperTest, Process_SimpleKeyPress) {
3364 const int32_t USAGE_A = 0x070004;
3365 const int32_t USAGE_UNKNOWN = 0x07ffff;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003366 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3367 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
Chris Yea52ade12020-08-27 16:49:20 -07003368 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, POLICY_FLAG_WAKE);
3369 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, POLICY_FLAG_WAKE);
3370 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003371
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003372 KeyboardInputMapper& mapper =
3373 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3374 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
arthurhungc903df12020-08-11 15:08:42 +08003375 // Initial metastate to AMETA_NONE.
3376 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
3377 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003378
3379 // Key down by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003380 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003381 NotifyKeyArgs args;
3382 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3383 ASSERT_EQ(DEVICE_ID, args.deviceId);
3384 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3385 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3386 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3387 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3388 ASSERT_EQ(KEY_HOME, args.scanCode);
3389 ASSERT_EQ(AMETA_NONE, args.metaState);
3390 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3391 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3392 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3393
3394 // Key up by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003395 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003396 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3397 ASSERT_EQ(DEVICE_ID, args.deviceId);
3398 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3399 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3400 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3401 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3402 ASSERT_EQ(KEY_HOME, args.scanCode);
3403 ASSERT_EQ(AMETA_NONE, args.metaState);
3404 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3405 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3406 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3407
3408 // Key down by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003409 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3410 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, 0, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003411 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3412 ASSERT_EQ(DEVICE_ID, args.deviceId);
3413 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3414 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3415 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3416 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3417 ASSERT_EQ(0, args.scanCode);
3418 ASSERT_EQ(AMETA_NONE, args.metaState);
3419 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3420 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3421 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3422
3423 // Key up by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003424 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3425 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003426 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3427 ASSERT_EQ(DEVICE_ID, args.deviceId);
3428 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3429 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3430 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3431 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3432 ASSERT_EQ(0, args.scanCode);
3433 ASSERT_EQ(AMETA_NONE, args.metaState);
3434 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3435 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3436 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3437
3438 // Key down with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003439 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3440 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UNKNOWN, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003441 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3442 ASSERT_EQ(DEVICE_ID, args.deviceId);
3443 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3444 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3445 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3446 ASSERT_EQ(0, args.keyCode);
3447 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3448 ASSERT_EQ(AMETA_NONE, args.metaState);
3449 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3450 ASSERT_EQ(0U, args.policyFlags);
3451 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3452
3453 // Key up with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003454 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3455 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003456 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3457 ASSERT_EQ(DEVICE_ID, args.deviceId);
3458 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3459 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3460 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3461 ASSERT_EQ(0, args.keyCode);
3462 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3463 ASSERT_EQ(AMETA_NONE, args.metaState);
3464 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3465 ASSERT_EQ(0U, args.policyFlags);
3466 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3467}
3468
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003469/**
3470 * Ensure that the readTime is set to the time when the EV_KEY is received.
3471 */
3472TEST_F(KeyboardInputMapperTest, Process_SendsReadTime) {
3473 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3474
3475 KeyboardInputMapper& mapper =
3476 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3477 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3478 NotifyKeyArgs args;
3479
3480 // Key down
3481 process(mapper, ARBITRARY_TIME, 12 /*readTime*/, EV_KEY, KEY_HOME, 1);
3482 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3483 ASSERT_EQ(12, args.readTime);
3484
3485 // Key up
3486 process(mapper, ARBITRARY_TIME, 15 /*readTime*/, EV_KEY, KEY_HOME, 1);
3487 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3488 ASSERT_EQ(15, args.readTime);
3489}
3490
Michael Wrightd02c5b62014-02-10 15:10:22 -08003491TEST_F(KeyboardInputMapperTest, Process_ShouldUpdateMetaState) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003492 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFTSHIFT, 0, AKEYCODE_SHIFT_LEFT, 0);
3493 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003494 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, 0);
3495 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, 0);
3496 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003497
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003498 KeyboardInputMapper& mapper =
3499 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3500 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003501
arthurhungc903df12020-08-11 15:08:42 +08003502 // Initial metastate to AMETA_NONE.
3503 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
3504 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003505
3506 // Metakey down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003507 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003508 NotifyKeyArgs args;
3509 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3510 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003511 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003512 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003513
3514 // Key down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003515 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_A, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003516 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3517 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003518 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003519
3520 // Key up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003521 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, KEY_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003522 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3523 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003524 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003525
3526 // Metakey up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003527 process(mapper, ARBITRARY_TIME + 3, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003528 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3529 ASSERT_EQ(AMETA_NONE, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003530 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003531 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003532}
3533
3534TEST_F(KeyboardInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003535 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3536 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3537 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3538 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003539
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003540 KeyboardInputMapper& mapper =
3541 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3542 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003543
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003544 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003545 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3546 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
3547 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3548 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
3549 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3550 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
3551 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3552 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
3553}
3554
3555TEST_F(KeyboardInputMapperTest, Process_WhenOrientationAware_ShouldRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003556 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3557 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3558 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3559 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003560
Michael Wrightd02c5b62014-02-10 15:10:22 -08003561 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003562 KeyboardInputMapper& mapper =
3563 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3564 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003565
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003566 prepareDisplay(DISPLAY_ORIENTATION_0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003567 ASSERT_NO_FATAL_FAILURE(
3568 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3569 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3570 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3571 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3572 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3573 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3574 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003575
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003576 clearViewports();
3577 prepareDisplay(DISPLAY_ORIENTATION_90);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003578 ASSERT_NO_FATAL_FAILURE(
3579 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3580 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3581 AKEYCODE_DPAD_UP, DISPLAY_ID));
3582 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3583 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3584 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3585 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003586
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003587 clearViewports();
3588 prepareDisplay(DISPLAY_ORIENTATION_180);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003589 ASSERT_NO_FATAL_FAILURE(
3590 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3591 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3592 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3593 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3594 AKEYCODE_DPAD_UP, DISPLAY_ID));
3595 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3596 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003597
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003598 clearViewports();
3599 prepareDisplay(DISPLAY_ORIENTATION_270);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003600 ASSERT_NO_FATAL_FAILURE(
3601 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3602 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3603 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3604 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3605 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3606 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3607 AKEYCODE_DPAD_UP, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003608
3609 // Special case: if orientation changes while key is down, we still emit the same keycode
3610 // in the key up as we did in the key down.
3611 NotifyKeyArgs args;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003612 clearViewports();
3613 prepareDisplay(DISPLAY_ORIENTATION_270);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003614 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003615 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3616 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3617 ASSERT_EQ(KEY_UP, args.scanCode);
3618 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3619
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003620 clearViewports();
3621 prepareDisplay(DISPLAY_ORIENTATION_180);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003622 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003623 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3624 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3625 ASSERT_EQ(KEY_UP, args.scanCode);
3626 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3627}
3628
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003629TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_NotOrientationAware) {
3630 // If the keyboard is not orientation aware,
3631 // key events should not be associated with a specific display id
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003632 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003633
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003634 KeyboardInputMapper& mapper =
3635 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3636 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003637 NotifyKeyArgs args;
3638
3639 // Display id should be ADISPLAY_ID_NONE without any display configuration.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003640 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003641 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003642 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003643 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3644 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3645
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003646 prepareDisplay(DISPLAY_ORIENTATION_0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003647 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003648 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003649 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003650 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3651 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3652}
3653
3654TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_OrientationAware) {
3655 // If the keyboard is orientation aware,
3656 // key events should be associated with the internal viewport
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003657 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003658
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003659 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003660 KeyboardInputMapper& mapper =
3661 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3662 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003663 NotifyKeyArgs args;
3664
3665 // Display id should be ADISPLAY_ID_NONE without any display configuration.
3666 // ^--- already checked by the previous test
3667
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003668 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003669 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003670 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003671 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003672 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003673 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3674 ASSERT_EQ(DISPLAY_ID, args.displayId);
3675
3676 constexpr int32_t newDisplayId = 2;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003677 clearViewports();
3678 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003679 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003680 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003681 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003682 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003683 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3684 ASSERT_EQ(newDisplayId, args.displayId);
3685}
3686
Michael Wrightd02c5b62014-02-10 15:10:22 -08003687TEST_F(KeyboardInputMapperTest, GetKeyCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003688 KeyboardInputMapper& mapper =
3689 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3690 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003691
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003692 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003693 ASSERT_EQ(1, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003694
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003695 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003696 ASSERT_EQ(0, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003697}
3698
Philip Junker4af3b3d2021-12-14 10:36:55 +01003699TEST_F(KeyboardInputMapperTest, GetKeyCodeForKeyLocation) {
3700 KeyboardInputMapper& mapper =
3701 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3702 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3703
3704 mFakeEventHub->addKeyCodeMapping(EVENTHUB_ID, AKEYCODE_Y, AKEYCODE_Z);
3705 ASSERT_EQ(AKEYCODE_Z, mapper.getKeyCodeForKeyLocation(AKEYCODE_Y))
3706 << "If a mapping is available, the result is equal to the mapping";
3707
3708 ASSERT_EQ(AKEYCODE_A, mapper.getKeyCodeForKeyLocation(AKEYCODE_A))
3709 << "If no mapping is available, the result is the key location";
3710}
3711
Michael Wrightd02c5b62014-02-10 15:10:22 -08003712TEST_F(KeyboardInputMapperTest, GetScanCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003713 KeyboardInputMapper& mapper =
3714 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3715 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003716
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003717 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003718 ASSERT_EQ(1, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003719
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003720 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003721 ASSERT_EQ(0, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003722}
3723
3724TEST_F(KeyboardInputMapperTest, MarkSupportedKeyCodes) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003725 KeyboardInputMapper& mapper =
3726 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3727 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003728
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003729 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003730
3731 const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
3732 uint8_t flags[2] = { 0, 0 };
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003733 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, 1, keyCodes, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003734 ASSERT_TRUE(flags[0]);
3735 ASSERT_FALSE(flags[1]);
3736}
3737
3738TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleMetaStateAndLeds) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003739 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3740 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3741 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3742 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3743 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3744 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003745
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003746 KeyboardInputMapper& mapper =
3747 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3748 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Chris Yea52ade12020-08-27 16:49:20 -07003749 // Initialize metastate to AMETA_NUM_LOCK_ON.
arthurhungc903df12020-08-11 15:08:42 +08003750 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
3751 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003752
3753 // Initialization should have turned all of the lights off.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003754 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3755 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3756 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003757
3758 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003759 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3760 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003761 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3762 ASSERT_FALSE(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, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003765
3766 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003767 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3768 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003769 ASSERT_TRUE(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_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003773
3774 // Toggle caps lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003775 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3776 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 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_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003780 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003781
3782 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003783 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3784 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003785 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3786 ASSERT_TRUE(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_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003789
3790 // Toggle num lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003791 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3792 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 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_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003796 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003797
3798 // Toggle scroll lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003799 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3800 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003801 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3802 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3803 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003804 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003805}
3806
Chris Yea52ade12020-08-27 16:49:20 -07003807TEST_F(KeyboardInputMapperTest, NoMetaStateWhenMetaKeysNotPresent) {
3808 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_BUTTON_A, 0);
3809 mFakeEventHub->addKey(EVENTHUB_ID, BTN_B, 0, AKEYCODE_BUTTON_B, 0);
3810 mFakeEventHub->addKey(EVENTHUB_ID, BTN_X, 0, AKEYCODE_BUTTON_X, 0);
3811 mFakeEventHub->addKey(EVENTHUB_ID, BTN_Y, 0, AKEYCODE_BUTTON_Y, 0);
3812
3813 KeyboardInputMapper& mapper =
3814 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3815 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
3816
3817 // Initial metastate should be AMETA_NONE as no meta keys added.
3818 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3819 // Meta state should be AMETA_NONE after reset
3820 mapper.reset(ARBITRARY_TIME);
3821 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3822 // Meta state should be AMETA_NONE with update, as device doesn't have the keys.
3823 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
3824 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3825
3826 NotifyKeyArgs args;
3827 // Press button "A"
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003828 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_A, 1);
Chris Yea52ade12020-08-27 16:49:20 -07003829 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3830 ASSERT_EQ(AMETA_NONE, args.metaState);
3831 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3832 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3833 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3834
3835 // Button up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003836 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003837 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3838 ASSERT_EQ(AMETA_NONE, args.metaState);
3839 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3840 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3841 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3842}
3843
Arthur Hung2c9a3342019-07-23 14:18:59 +08003844TEST_F(KeyboardInputMapperTest, Configure_AssignsDisplayPort) {
3845 // keyboard 1.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003846 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3847 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3848 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3849 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003850
3851 // keyboard 2.
3852 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08003853 const std::string DEVICE_NAME2 = "KEYBOARD2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08003854 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003855 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08003856 std::shared_ptr<InputDevice> device2 =
3857 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003858 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08003859
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003860 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3861 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3862 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3863 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003864
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003865 KeyboardInputMapper& mapper =
3866 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3867 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003868
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003869 KeyboardInputMapper& mapper2 =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003870 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003871 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003872 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
3873 device2->reset(ARBITRARY_TIME);
3874
3875 // Prepared displays and associated info.
3876 constexpr uint8_t hdmi1 = 0;
3877 constexpr uint8_t hdmi2 = 1;
3878 const std::string SECONDARY_UNIQUE_ID = "local:1";
3879
3880 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
3881 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
3882
3883 // No associated display viewport found, should disable the device.
3884 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3885 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3886 ASSERT_FALSE(device2->isEnabled());
3887
3888 // Prepare second display.
3889 constexpr int32_t newDisplayId = 2;
3890 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003891 UNIQUE_ID, hdmi1, ViewportType::INTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003892 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003893 SECONDARY_UNIQUE_ID, hdmi2, ViewportType::EXTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003894 // Default device will reconfigure above, need additional reconfiguration for another device.
3895 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3896 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3897
3898 // Device should be enabled after the associated display is found.
3899 ASSERT_TRUE(mDevice->isEnabled());
3900 ASSERT_TRUE(device2->isEnabled());
3901
3902 // Test pad key events
3903 ASSERT_NO_FATAL_FAILURE(
3904 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3905 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3906 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3907 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3908 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3909 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3910 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3911
3912 ASSERT_NO_FATAL_FAILURE(
3913 testDPadKeyRotation(mapper2, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, newDisplayId));
3914 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3915 AKEYCODE_DPAD_RIGHT, newDisplayId));
3916 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3917 AKEYCODE_DPAD_DOWN, newDisplayId));
3918 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3919 AKEYCODE_DPAD_LEFT, newDisplayId));
3920}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003921
arthurhungc903df12020-08-11 15:08:42 +08003922TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleAfterReattach) {
3923 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3924 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3925 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3926 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3927 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3928 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3929
3930 KeyboardInputMapper& mapper =
3931 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3932 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3933 // Initial metastate to AMETA_NONE.
3934 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
3935 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
3936
3937 // Initialization should have turned all of the lights off.
3938 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3939 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3940 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3941
3942 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003943 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3944 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003945 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3946 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
3947
3948 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003949 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3950 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003951 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3952 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
3953
3954 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003955 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3956 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003957 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3958 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
3959
3960 mFakeEventHub->removeDevice(EVENTHUB_ID);
3961 mReader->loopOnce();
3962
3963 // keyboard 2 should default toggle keys.
3964 const std::string USB2 = "USB2";
3965 const std::string DEVICE_NAME2 = "KEYBOARD2";
3966 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
3967 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
3968 std::shared_ptr<InputDevice> device2 =
3969 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003970 ftl::Flags<InputDeviceClass>(0));
arthurhungc903df12020-08-11 15:08:42 +08003971 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3972 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
3973 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3974 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3975 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3976 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3977
arthurhung6fe95782020-10-05 22:41:16 +08003978 KeyboardInputMapper& mapper2 =
3979 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
3980 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
arthurhungc903df12020-08-11 15:08:42 +08003981 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
3982 device2->reset(ARBITRARY_TIME);
3983
3984 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_CAPSL));
3985 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_NUML));
3986 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_SCROLLL));
arthurhung6fe95782020-10-05 22:41:16 +08003987 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON,
3988 mapper2.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08003989}
3990
Arthur Hungcb40a002021-08-03 14:31:01 +00003991TEST_F(KeyboardInputMapperTest, Process_toggleCapsLockState) {
3992 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3993 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3994 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3995
3996 // Suppose we have two mappers. (DPAD + KEYBOARD)
3997 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_DPAD,
3998 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
3999 KeyboardInputMapper& mapper =
4000 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4001 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
4002 // Initialize metastate to AMETA_NUM_LOCK_ON.
4003 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
4004 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
4005
4006 mReader->toggleCapsLockState(DEVICE_ID);
4007 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
4008}
4009
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004010// --- KeyboardInputMapperTest_ExternalDevice ---
4011
4012class KeyboardInputMapperTest_ExternalDevice : public InputMapperTest {
4013protected:
Chris Yea52ade12020-08-27 16:49:20 -07004014 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004015};
4016
4017TEST_F(KeyboardInputMapperTest_ExternalDevice, WakeBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07004018 // For external devices, non-media keys will trigger wake on key down. Media keys need to be
4019 // marked as WAKE in the keylayout file to trigger wake.
Powei Fengd041c5d2019-05-03 17:11:33 -07004020
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004021 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, 0);
4022 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, 0);
4023 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAYPAUSE, 0, AKEYCODE_MEDIA_PLAY_PAUSE,
4024 POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07004025
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004026 KeyboardInputMapper& mapper =
4027 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4028 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07004029
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004030 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004031 NotifyKeyArgs args;
4032 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4033 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4034
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004035 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004036 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4037 ASSERT_EQ(uint32_t(0), args.policyFlags);
4038
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004039 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004040 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4041 ASSERT_EQ(uint32_t(0), args.policyFlags);
4042
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004043 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004044 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4045 ASSERT_EQ(uint32_t(0), args.policyFlags);
4046
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004047 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004048 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4049 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4050
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004051 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004052 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4053 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4054}
4055
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004056TEST_F(KeyboardInputMapperTest_ExternalDevice, DoNotWakeByDefaultBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07004057 // Tv Remote key's wake behavior is prescribed by the keylayout file.
Powei Fengd041c5d2019-05-03 17:11:33 -07004058
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004059 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4060 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
4061 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07004062
Powei Fengd041c5d2019-05-03 17:11:33 -07004063 addConfigurationProperty("keyboard.doNotWakeByDefault", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004064 KeyboardInputMapper& mapper =
4065 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4066 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07004067
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004068 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004069 NotifyKeyArgs args;
4070 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4071 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4072
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004073 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004074 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4075 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4076
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004077 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_DOWN, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004078 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4079 ASSERT_EQ(uint32_t(0), args.policyFlags);
4080
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004081 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_DOWN, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004082 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4083 ASSERT_EQ(uint32_t(0), args.policyFlags);
4084
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004085 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004086 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4087 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4088
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004089 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004090 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4091 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4092}
4093
Michael Wrightd02c5b62014-02-10 15:10:22 -08004094// --- CursorInputMapperTest ---
4095
4096class CursorInputMapperTest : public InputMapperTest {
4097protected:
4098 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD;
4099
Michael Wright17db18e2020-06-26 20:51:44 +01004100 std::shared_ptr<FakePointerController> mFakePointerController;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004101
Chris Yea52ade12020-08-27 16:49:20 -07004102 void SetUp() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004103 InputMapperTest::SetUp();
4104
Michael Wright17db18e2020-06-26 20:51:44 +01004105 mFakePointerController = std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00004106 mFakePolicy->setPointerController(mFakePointerController);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004107 }
4108
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004109 void testMotionRotation(CursorInputMapper& mapper, int32_t originalX, int32_t originalY,
4110 int32_t rotatedX, int32_t rotatedY);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004111
4112 void prepareDisplay(int32_t orientation) {
4113 const std::string uniqueId = "local:0";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004114 const ViewportType viewportType = ViewportType::INTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004115 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
4116 orientation, uniqueId, NO_PORT, viewportType);
4117 }
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004118
4119 static void assertCursorPointerCoords(const PointerCoords& coords, float x, float y,
4120 float pressure) {
4121 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(coords, x, y, pressure, 0.0f, 0.0f, 0.0f, 0.0f,
4122 0.0f, 0.0f, 0.0f, EPSILON));
4123 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08004124};
4125
4126const int32_t CursorInputMapperTest::TRACKBALL_MOVEMENT_THRESHOLD = 6;
4127
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004128void CursorInputMapperTest::testMotionRotation(CursorInputMapper& mapper, int32_t originalX,
4129 int32_t originalY, int32_t rotatedX,
4130 int32_t rotatedY) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004131 NotifyMotionArgs args;
4132
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004133 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, originalX);
4134 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, originalY);
4135 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004136 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4137 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004138 ASSERT_NO_FATAL_FAILURE(
4139 assertCursorPointerCoords(args.pointerCoords[0],
4140 float(rotatedX) / TRACKBALL_MOVEMENT_THRESHOLD,
4141 float(rotatedY) / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004142}
4143
4144TEST_F(CursorInputMapperTest, WhenModeIsPointer_GetSources_ReturnsMouse) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004145 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004146 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004147
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004148 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004149}
4150
4151TEST_F(CursorInputMapperTest, WhenModeIsNavigation_GetSources_ReturnsTrackball) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004152 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004153 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004154
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004155 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004156}
4157
4158TEST_F(CursorInputMapperTest, WhenModeIsPointer_PopulateDeviceInfo_ReturnsRangeFromPointerController) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004159 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004160 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004161
4162 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004163 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004164
4165 // Initially there may not be a valid motion range.
Yi Kong9b14ac62018-07-17 13:48:38 -07004166 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE));
4167 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004168 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4169 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE, 0.0f, 1.0f, 0.0f, 0.0f));
4170
4171 // When the bounds are set, then there should be a valid motion range.
4172 mFakePointerController->setBounds(1, 2, 800 - 1, 480 - 1);
4173
4174 InputDeviceInfo info2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004175 mapper.populateDeviceInfo(&info2);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004176
4177 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
4178 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE,
4179 1, 800 - 1, 0.0f, 0.0f));
4180 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
4181 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE,
4182 2, 480 - 1, 0.0f, 0.0f));
4183 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
4184 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE,
4185 0.0f, 1.0f, 0.0f, 0.0f));
4186}
4187
4188TEST_F(CursorInputMapperTest, WhenModeIsNavigation_PopulateDeviceInfo_ReturnsScaledRange) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004189 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004190 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004191
4192 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004193 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004194
4195 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4196 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_TRACKBALL,
4197 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
4198 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4199 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_TRACKBALL,
4200 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
4201 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4202 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TRACKBALL,
4203 0.0f, 1.0f, 0.0f, 0.0f));
4204}
4205
4206TEST_F(CursorInputMapperTest, Process_ShouldSetAllFieldsAndIncludeGlobalMetaState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004207 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004208 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004209
arthurhungdcef2dc2020-08-11 14:47:50 +08004210 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004211
4212 NotifyMotionArgs args;
4213
4214 // Button press.
4215 // Mostly testing non x/y behavior here so we don't need to check again elsewhere.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004216 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4217 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004218 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4219 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4220 ASSERT_EQ(DEVICE_ID, args.deviceId);
4221 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4222 ASSERT_EQ(uint32_t(0), args.policyFlags);
4223 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
4224 ASSERT_EQ(0, args.flags);
4225 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4226 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
4227 ASSERT_EQ(0, args.edgeFlags);
4228 ASSERT_EQ(uint32_t(1), args.pointerCount);
4229 ASSERT_EQ(0, args.pointerProperties[0].id);
4230 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004231 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004232 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4233 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4234 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4235
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004236 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4237 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4238 ASSERT_EQ(DEVICE_ID, args.deviceId);
4239 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4240 ASSERT_EQ(uint32_t(0), args.policyFlags);
4241 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
4242 ASSERT_EQ(0, args.flags);
4243 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4244 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
4245 ASSERT_EQ(0, args.edgeFlags);
4246 ASSERT_EQ(uint32_t(1), args.pointerCount);
4247 ASSERT_EQ(0, args.pointerProperties[0].id);
4248 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004249 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004250 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4251 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4252 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4253
Michael Wrightd02c5b62014-02-10 15:10:22 -08004254 // Button release. Should have same down time.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004255 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4256 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004257 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4258 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
4259 ASSERT_EQ(DEVICE_ID, args.deviceId);
4260 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4261 ASSERT_EQ(uint32_t(0), args.policyFlags);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004262 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
4263 ASSERT_EQ(0, args.flags);
4264 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4265 ASSERT_EQ(0, args.buttonState);
4266 ASSERT_EQ(0, args.edgeFlags);
4267 ASSERT_EQ(uint32_t(1), args.pointerCount);
4268 ASSERT_EQ(0, args.pointerProperties[0].id);
4269 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004270 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004271 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4272 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4273 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4274
4275 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4276 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
4277 ASSERT_EQ(DEVICE_ID, args.deviceId);
4278 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4279 ASSERT_EQ(uint32_t(0), args.policyFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004280 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
4281 ASSERT_EQ(0, args.flags);
4282 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4283 ASSERT_EQ(0, args.buttonState);
4284 ASSERT_EQ(0, args.edgeFlags);
4285 ASSERT_EQ(uint32_t(1), args.pointerCount);
4286 ASSERT_EQ(0, args.pointerProperties[0].id);
4287 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004288 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004289 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4290 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4291 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4292}
4293
4294TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentXYUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004295 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004296 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004297
4298 NotifyMotionArgs args;
4299
4300 // Motion in X but not Y.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004301 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4302 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004303 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4304 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004305 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4306 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f,
4307 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004308
4309 // Motion in Y but not X.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004310 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
4311 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004312 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4313 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004314 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f,
4315 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004316}
4317
4318TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004319 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004320 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004321
4322 NotifyMotionArgs args;
4323
4324 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004325 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4326 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004327 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4328 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004329 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004330
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004331 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4332 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004333 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004334
Michael Wrightd02c5b62014-02-10 15:10:22 -08004335 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004336 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4337 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004338 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004339 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004340 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004341
4342 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004343 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004344 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004345}
4346
4347TEST_F(CursorInputMapperTest, Process_ShouldHandleCombinedXYAndButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004348 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004349 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004350
4351 NotifyMotionArgs args;
4352
4353 // Combined X, Y and Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004354 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4355 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
4356 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4357 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004358 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4359 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004360 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4361 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4362 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004363
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004364 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4365 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004366 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4367 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4368 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004369
Michael Wrightd02c5b62014-02-10 15:10:22 -08004370 // Move X, Y a bit while pressed.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004371 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 2);
4372 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 1);
4373 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004374 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4375 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004376 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4377 2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4378 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004379
4380 // Release Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004381 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4382 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004383 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004384 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004385 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004386
4387 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004388 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004389 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004390}
4391
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004392TEST_F(CursorInputMapperTest, Process_WhenOrientationAware_ShouldNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004393 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004394 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
4395 // need to be rotated.
4396 addConfigurationProperty("cursor.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004397 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004398
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004399 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004400 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4401 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4402 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4403 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4404 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4405 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4406 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4407 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4408}
4409
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004410TEST_F(CursorInputMapperTest, Process_WhenNotOrientationAware_ShouldRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004411 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004412 // Since InputReader works in the un-rotated coordinate space, only devices that are not
4413 // orientation-aware are affected by display rotation.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004414 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004415
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004416 prepareDisplay(DISPLAY_ORIENTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004417 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4418 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4419 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4420 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4421 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4422 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4423 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4424 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4425
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004426 prepareDisplay(DISPLAY_ORIENTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004427 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, -1, 0));
4428 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, 1));
4429 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, 1));
4430 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, 1));
4431 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 1, 0));
4432 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, -1));
4433 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, -1));
4434 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, -1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004435
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004436 prepareDisplay(DISPLAY_ORIENTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004437 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, -1));
4438 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, -1));
4439 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, -1, 0));
4440 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, 1));
4441 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, 1));
4442 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, 1));
4443 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 1, 0));
4444 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, -1));
4445
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004446 prepareDisplay(DISPLAY_ORIENTATION_270);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004447 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 1, 0));
4448 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, -1));
4449 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, -1));
4450 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, -1));
4451 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, -1, 0));
4452 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, 1));
4453 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, 1));
4454 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, 1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004455}
4456
4457TEST_F(CursorInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004458 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004459 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004460
4461 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4462 mFakePointerController->setPosition(100, 200);
4463 mFakePointerController->setButtonState(0);
4464
4465 NotifyMotionArgs motionArgs;
4466 NotifyKeyArgs keyArgs;
4467
4468 // press BTN_LEFT, release BTN_LEFT
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004469 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 1);
4470 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004471 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4472 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4473 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4474 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004475 ASSERT_NO_FATAL_FAILURE(
4476 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004477
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004478 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4479 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4480 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4481 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004482 ASSERT_NO_FATAL_FAILURE(
4483 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004484
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004485 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 0);
4486 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004487 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004488 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004489 ASSERT_EQ(0, motionArgs.buttonState);
4490 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004491 ASSERT_NO_FATAL_FAILURE(
4492 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004493
4494 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004495 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004496 ASSERT_EQ(0, motionArgs.buttonState);
4497 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004498 ASSERT_NO_FATAL_FAILURE(
4499 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004500
4501 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004502 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004503 ASSERT_EQ(0, motionArgs.buttonState);
4504 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004505 ASSERT_NO_FATAL_FAILURE(
4506 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004507
4508 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004509 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 1);
4510 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 1);
4511 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004512 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4513 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4514 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4515 motionArgs.buttonState);
4516 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4517 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004518 ASSERT_NO_FATAL_FAILURE(
4519 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004520
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004521 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4522 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4523 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4524 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4525 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004526 ASSERT_NO_FATAL_FAILURE(
4527 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004528
4529 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4530 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4531 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4532 motionArgs.buttonState);
4533 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4534 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004535 ASSERT_NO_FATAL_FAILURE(
4536 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004537
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004538 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 0);
4539 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004540 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004541 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004542 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4543 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004544 ASSERT_NO_FATAL_FAILURE(
4545 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004546
4547 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004548 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004549 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4550 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004551 ASSERT_NO_FATAL_FAILURE(
4552 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004553
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004554 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4555 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004556 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004557 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
4558 ASSERT_EQ(0, motionArgs.buttonState);
4559 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004560 ASSERT_NO_FATAL_FAILURE(
4561 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004562 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4563 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
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(0, motionArgs.buttonState);
4567 ASSERT_EQ(0, mFakePointerController->getButtonState());
4568 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004569 ASSERT_NO_FATAL_FAILURE(
4570 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004571
Michael Wrightd02c5b62014-02-10 15:10:22 -08004572 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4573 ASSERT_EQ(0, motionArgs.buttonState);
4574 ASSERT_EQ(0, mFakePointerController->getButtonState());
4575 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004576 ASSERT_NO_FATAL_FAILURE(
4577 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004578
4579 // press BTN_BACK, release BTN_BACK
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004580 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 1);
4581 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004582 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4583 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4584 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004585
Michael Wrightd02c5b62014-02-10 15:10:22 -08004586 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004587 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004588 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4589 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004590 ASSERT_NO_FATAL_FAILURE(
4591 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.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_BACK, motionArgs.buttonState);
4596 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004597 ASSERT_NO_FATAL_FAILURE(
4598 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004599
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004600 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 0);
4601 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004602 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004603 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004604 ASSERT_EQ(0, motionArgs.buttonState);
4605 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004606 ASSERT_NO_FATAL_FAILURE(
4607 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004608
4609 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004610 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004611 ASSERT_EQ(0, motionArgs.buttonState);
4612 ASSERT_EQ(0, mFakePointerController->getButtonState());
4613
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004614 ASSERT_NO_FATAL_FAILURE(
4615 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004616 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4617 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4618 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4619
4620 // press BTN_SIDE, release BTN_SIDE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004621 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 1);
4622 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004623 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4624 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4625 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004626
Michael Wrightd02c5b62014-02-10 15:10:22 -08004627 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004628 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004629 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4630 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004631 ASSERT_NO_FATAL_FAILURE(
4632 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004633
4634 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4635 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4636 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4637 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004638 ASSERT_NO_FATAL_FAILURE(
4639 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004640
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004641 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 0);
4642 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004643 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004644 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004645 ASSERT_EQ(0, motionArgs.buttonState);
4646 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004647 ASSERT_NO_FATAL_FAILURE(
4648 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004649
4650 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4651 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4652 ASSERT_EQ(0, motionArgs.buttonState);
4653 ASSERT_EQ(0, 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
Michael Wrightd02c5b62014-02-10 15:10:22 -08004657 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4658 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4659 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4660
4661 // press BTN_FORWARD, release BTN_FORWARD
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004662 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 1);
4663 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004664 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4665 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4666 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004667
Michael Wrightd02c5b62014-02-10 15:10:22 -08004668 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004669 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004670 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4671 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004672 ASSERT_NO_FATAL_FAILURE(
4673 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004674
4675 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4676 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4677 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4678 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004679 ASSERT_NO_FATAL_FAILURE(
4680 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004681
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004682 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 0);
4683 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004684 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004685 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004686 ASSERT_EQ(0, motionArgs.buttonState);
4687 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004688 ASSERT_NO_FATAL_FAILURE(
4689 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004690
4691 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4692 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4693 ASSERT_EQ(0, motionArgs.buttonState);
4694 ASSERT_EQ(0, 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
Michael Wrightd02c5b62014-02-10 15:10:22 -08004698 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4699 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4700 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4701
4702 // press BTN_EXTRA, release BTN_EXTRA
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004703 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 1);
4704 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004705 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4706 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4707 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004708
Michael Wrightd02c5b62014-02-10 15:10:22 -08004709 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004710 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004711 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4712 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004713 ASSERT_NO_FATAL_FAILURE(
4714 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004715
4716 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4717 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4718 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4719 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004720 ASSERT_NO_FATAL_FAILURE(
4721 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004722
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004723 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 0);
4724 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004725 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004726 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004727 ASSERT_EQ(0, motionArgs.buttonState);
4728 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004729 ASSERT_NO_FATAL_FAILURE(
4730 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004731
4732 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4733 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4734 ASSERT_EQ(0, motionArgs.buttonState);
4735 ASSERT_EQ(0, 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
Michael Wrightd02c5b62014-02-10 15:10:22 -08004739 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4740 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4741 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4742}
4743
4744TEST_F(CursorInputMapperTest, Process_WhenModeIsPointer_ShouldMoveThePointerAround) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004745 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004746 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004747
4748 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4749 mFakePointerController->setPosition(100, 200);
4750 mFakePointerController->setButtonState(0);
4751
4752 NotifyMotionArgs args;
4753
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004754 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4755 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4756 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004757 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004758 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4759 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4760 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4761 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 +01004762 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004763}
4764
4765TEST_F(CursorInputMapperTest, Process_PointerCapture) {
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004766 addConfigurationProperty("cursor.mode", "pointer");
4767 mFakePolicy->setPointerCapture(true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004768 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004769
4770 NotifyDeviceResetArgs resetArgs;
4771 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4772 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4773 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4774
4775 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4776 mFakePointerController->setPosition(100, 200);
4777 mFakePointerController->setButtonState(0);
4778
4779 NotifyMotionArgs args;
4780
4781 // Move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004782 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4783 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4784 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004785 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4786 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4787 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4788 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4789 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 +01004790 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004791
4792 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004793 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4794 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004795 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4796 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4797 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
4798 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4799 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4800 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4801 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4802 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
4803 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4804 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4805
4806 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004807 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4808 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004809 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4810 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4811 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
4812 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4813 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4814 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4815 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4816 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
4817 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4818 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4819
4820 // Another move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004821 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 30);
4822 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 40);
4823 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004824 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4825 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4826 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4827 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4828 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 +01004829 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004830
4831 // Disable pointer capture and check that the device generation got bumped
4832 // and events are generated the usual way.
arthurhungdcef2dc2020-08-11 14:47:50 +08004833 const uint32_t generation = mReader->getContext()->getGeneration();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004834 mFakePolicy->setPointerCapture(false);
4835 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
arthurhungdcef2dc2020-08-11 14:47:50 +08004836 ASSERT_TRUE(mReader->getContext()->getGeneration() != generation);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004837
4838 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4839 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4840 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4841
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004842 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4843 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4844 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004845 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4846 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004847 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4848 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4849 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 +01004850 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004851}
4852
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00004853/**
4854 * When Pointer Capture is enabled, we expect to report unprocessed relative movements, so any
4855 * pointer acceleration or speed processing should not be applied.
4856 */
4857TEST_F(CursorInputMapperTest, PointerCaptureDisablesVelocityProcessing) {
4858 addConfigurationProperty("cursor.mode", "pointer");
4859 const VelocityControlParameters testParams(5.f /*scale*/, 0.f /*low threshold*/,
4860 100.f /*high threshold*/, 10.f /*acceleration*/);
4861 mFakePolicy->setVelocityControlParams(testParams);
4862 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
4863
4864 NotifyDeviceResetArgs resetArgs;
4865 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4866 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4867 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4868
4869 NotifyMotionArgs args;
4870
4871 // Move and verify scale is applied.
4872 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4873 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4874 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4875 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4876 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4877 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4878 const float relX = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X);
4879 const float relY = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y);
4880 ASSERT_GT(relX, 10);
4881 ASSERT_GT(relY, 20);
4882
4883 // Enable Pointer Capture
4884 mFakePolicy->setPointerCapture(true);
4885 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
4886 NotifyPointerCaptureChangedArgs captureArgs;
4887 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyCaptureWasCalled(&captureArgs));
4888 ASSERT_TRUE(captureArgs.request.enable);
4889
4890 // Move and verify scale is not applied.
4891 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4892 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4893 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4894 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4895 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4896 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4897 ASSERT_EQ(10, args.pointerCoords[0].getX());
4898 ASSERT_EQ(20, args.pointerCoords[0].getY());
4899}
4900
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004901TEST_F(CursorInputMapperTest, Process_ShouldHandleDisplayId) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004902 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004903
Garfield Tan888a6a42020-01-09 11:39:16 -08004904 // Setup for second display.
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004905 constexpr int32_t SECOND_DISPLAY_ID = 1;
Garfield Tan888a6a42020-01-09 11:39:16 -08004906 const std::string SECOND_DISPLAY_UNIQUE_ID = "local:1";
4907 mFakePolicy->addDisplayViewport(SECOND_DISPLAY_ID, 800, 480, DISPLAY_ORIENTATION_0,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00004908 true /*isActive*/, SECOND_DISPLAY_UNIQUE_ID, NO_PORT,
4909 ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08004910 mFakePolicy->setDefaultPointerDisplayId(SECOND_DISPLAY_ID);
4911 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
4912
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004913 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4914 mFakePointerController->setPosition(100, 200);
4915 mFakePointerController->setButtonState(0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004916
4917 NotifyMotionArgs args;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004918 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4919 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4920 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004921 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4922 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4923 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4924 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4925 110.0f, 220.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
Michael Wright17db18e2020-06-26 20:51:44 +01004926 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004927 ASSERT_EQ(SECOND_DISPLAY_ID, args.displayId);
4928}
4929
Michael Wrightd02c5b62014-02-10 15:10:22 -08004930// --- TouchInputMapperTest ---
4931
4932class TouchInputMapperTest : public InputMapperTest {
4933protected:
4934 static const int32_t RAW_X_MIN;
4935 static const int32_t RAW_X_MAX;
4936 static const int32_t RAW_Y_MIN;
4937 static const int32_t RAW_Y_MAX;
4938 static const int32_t RAW_TOUCH_MIN;
4939 static const int32_t RAW_TOUCH_MAX;
4940 static const int32_t RAW_TOOL_MIN;
4941 static const int32_t RAW_TOOL_MAX;
4942 static const int32_t RAW_PRESSURE_MIN;
4943 static const int32_t RAW_PRESSURE_MAX;
4944 static const int32_t RAW_ORIENTATION_MIN;
4945 static const int32_t RAW_ORIENTATION_MAX;
4946 static const int32_t RAW_DISTANCE_MIN;
4947 static const int32_t RAW_DISTANCE_MAX;
4948 static const int32_t RAW_TILT_MIN;
4949 static const int32_t RAW_TILT_MAX;
4950 static const int32_t RAW_ID_MIN;
4951 static const int32_t RAW_ID_MAX;
4952 static const int32_t RAW_SLOT_MIN;
4953 static const int32_t RAW_SLOT_MAX;
4954 static const float X_PRECISION;
4955 static const float Y_PRECISION;
Santos Cordonfa5cf462017-04-05 10:37:00 -07004956 static const float X_PRECISION_VIRTUAL;
4957 static const float Y_PRECISION_VIRTUAL;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004958
4959 static const float GEOMETRIC_SCALE;
Jason Gerecke489fda82012-09-07 17:19:40 -07004960 static const TouchAffineTransformation AFFINE_TRANSFORM;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004961
4962 static const VirtualKeyDefinition VIRTUAL_KEYS[2];
4963
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004964 const std::string UNIQUE_ID = "local:0";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004965 const std::string SECONDARY_UNIQUE_ID = "local:1";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004966
Michael Wrightd02c5b62014-02-10 15:10:22 -08004967 enum Axes {
4968 POSITION = 1 << 0,
4969 TOUCH = 1 << 1,
4970 TOOL = 1 << 2,
4971 PRESSURE = 1 << 3,
4972 ORIENTATION = 1 << 4,
4973 MINOR = 1 << 5,
4974 ID = 1 << 6,
4975 DISTANCE = 1 << 7,
4976 TILT = 1 << 8,
4977 SLOT = 1 << 9,
4978 TOOL_TYPE = 1 << 10,
4979 };
4980
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004981 void prepareDisplay(int32_t orientation, std::optional<uint8_t> port = NO_PORT);
4982 void prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port = NO_PORT);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004983 void prepareVirtualDisplay(int32_t orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004984 void prepareVirtualKeys();
Jason Gerecke489fda82012-09-07 17:19:40 -07004985 void prepareLocationCalibration();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004986 int32_t toRawX(float displayX);
4987 int32_t toRawY(float displayY);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07004988 int32_t toRotatedRawX(float displayX);
4989 int32_t toRotatedRawY(float displayY);
Jason Gerecke489fda82012-09-07 17:19:40 -07004990 float toCookedX(float rawX, float rawY);
4991 float toCookedY(float rawX, float rawY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004992 float toDisplayX(int32_t rawX);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004993 float toDisplayX(int32_t rawX, int32_t displayWidth);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004994 float toDisplayY(int32_t rawY);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004995 float toDisplayY(int32_t rawY, int32_t displayHeight);
4996
Michael Wrightd02c5b62014-02-10 15:10:22 -08004997};
4998
4999const int32_t TouchInputMapperTest::RAW_X_MIN = 25;
5000const int32_t TouchInputMapperTest::RAW_X_MAX = 1019;
5001const int32_t TouchInputMapperTest::RAW_Y_MIN = 30;
5002const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009;
5003const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0;
5004const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31;
5005const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0;
5006const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15;
Michael Wrightaa449c92017-12-13 21:21:43 +00005007const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = 0;
5008const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = 255;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005009const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7;
5010const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
5011const int32_t TouchInputMapperTest::RAW_DISTANCE_MIN = 0;
5012const int32_t TouchInputMapperTest::RAW_DISTANCE_MAX = 7;
5013const int32_t TouchInputMapperTest::RAW_TILT_MIN = 0;
5014const int32_t TouchInputMapperTest::RAW_TILT_MAX = 150;
5015const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
5016const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
5017const int32_t TouchInputMapperTest::RAW_SLOT_MIN = 0;
5018const int32_t TouchInputMapperTest::RAW_SLOT_MAX = 9;
5019const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH;
5020const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT;
Santos Cordonfa5cf462017-04-05 10:37:00 -07005021const float TouchInputMapperTest::X_PRECISION_VIRTUAL =
5022 float(RAW_X_MAX - RAW_X_MIN + 1) / VIRTUAL_DISPLAY_WIDTH;
5023const float TouchInputMapperTest::Y_PRECISION_VIRTUAL =
5024 float(RAW_Y_MAX - RAW_Y_MIN + 1) / VIRTUAL_DISPLAY_HEIGHT;
Jason Gerecke489fda82012-09-07 17:19:40 -07005025const TouchAffineTransformation TouchInputMapperTest::AFFINE_TRANSFORM =
5026 TouchAffineTransformation(1, -2, 3, -4, 5, -6);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005027
5028const float TouchInputMapperTest::GEOMETRIC_SCALE =
5029 avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1),
5030 float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1));
5031
5032const VirtualKeyDefinition TouchInputMapperTest::VIRTUAL_KEYS[2] = {
5033 { KEY_HOME, 60, DISPLAY_HEIGHT + 15, 20, 20 },
5034 { KEY_MENU, DISPLAY_HEIGHT - 60, DISPLAY_WIDTH + 15, 20, 20 },
5035};
5036
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005037void TouchInputMapperTest::prepareDisplay(int32_t orientation, std::optional<uint8_t> port) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01005038 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
5039 port, ViewportType::INTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005040}
5041
5042void TouchInputMapperTest::prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port) {
5043 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
5044 DISPLAY_ORIENTATION_0, SECONDARY_UNIQUE_ID, port, type);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005045}
5046
Santos Cordonfa5cf462017-04-05 10:37:00 -07005047void TouchInputMapperTest::prepareVirtualDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01005048 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH, VIRTUAL_DISPLAY_HEIGHT,
5049 orientation, VIRTUAL_DISPLAY_UNIQUE_ID, NO_PORT,
5050 ViewportType::VIRTUAL);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005051}
5052
Michael Wrightd02c5b62014-02-10 15:10:22 -08005053void TouchInputMapperTest::prepareVirtualKeys() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005054 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[0]);
5055 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[1]);
5056 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
5057 mFakeEventHub->addKey(EVENTHUB_ID, KEY_MENU, 0, AKEYCODE_MENU, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005058}
5059
Jason Gerecke489fda82012-09-07 17:19:40 -07005060void TouchInputMapperTest::prepareLocationCalibration() {
5061 mFakePolicy->setTouchAffineTransformation(AFFINE_TRANSFORM);
5062}
5063
Michael Wrightd02c5b62014-02-10 15:10:22 -08005064int32_t TouchInputMapperTest::toRawX(float displayX) {
5065 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN);
5066}
5067
5068int32_t TouchInputMapperTest::toRawY(float displayY) {
5069 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN);
5070}
5071
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005072int32_t TouchInputMapperTest::toRotatedRawX(float displayX) {
5073 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_HEIGHT + RAW_X_MIN);
5074}
5075
5076int32_t TouchInputMapperTest::toRotatedRawY(float displayY) {
5077 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_WIDTH + RAW_Y_MIN);
5078}
5079
Jason Gerecke489fda82012-09-07 17:19:40 -07005080float TouchInputMapperTest::toCookedX(float rawX, float rawY) {
5081 AFFINE_TRANSFORM.applyTo(rawX, rawY);
5082 return rawX;
5083}
5084
5085float TouchInputMapperTest::toCookedY(float rawX, float rawY) {
5086 AFFINE_TRANSFORM.applyTo(rawX, rawY);
5087 return rawY;
5088}
5089
Michael Wrightd02c5b62014-02-10 15:10:22 -08005090float TouchInputMapperTest::toDisplayX(int32_t rawX) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005091 return toDisplayX(rawX, DISPLAY_WIDTH);
5092}
5093
5094float TouchInputMapperTest::toDisplayX(int32_t rawX, int32_t displayWidth) {
5095 return float(rawX - RAW_X_MIN) * displayWidth / (RAW_X_MAX - RAW_X_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005096}
5097
5098float TouchInputMapperTest::toDisplayY(int32_t rawY) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005099 return toDisplayY(rawY, DISPLAY_HEIGHT);
5100}
5101
5102float TouchInputMapperTest::toDisplayY(int32_t rawY, int32_t displayHeight) {
5103 return float(rawY - RAW_Y_MIN) * displayHeight / (RAW_Y_MAX - RAW_Y_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005104}
5105
5106
5107// --- SingleTouchInputMapperTest ---
5108
5109class SingleTouchInputMapperTest : public TouchInputMapperTest {
5110protected:
5111 void prepareButtons();
5112 void prepareAxes(int axes);
5113
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005114 void processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
5115 void processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
5116 void processUp(SingleTouchInputMapper& mappery);
5117 void processPressure(SingleTouchInputMapper& mapper, int32_t pressure);
5118 void processToolMajor(SingleTouchInputMapper& mapper, int32_t toolMajor);
5119 void processDistance(SingleTouchInputMapper& mapper, int32_t distance);
5120 void processTilt(SingleTouchInputMapper& mapper, int32_t tiltX, int32_t tiltY);
5121 void processKey(SingleTouchInputMapper& mapper, int32_t code, int32_t value);
5122 void processSync(SingleTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005123};
5124
5125void SingleTouchInputMapperTest::prepareButtons() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005126 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005127}
5128
5129void SingleTouchInputMapperTest::prepareAxes(int axes) {
5130 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005131 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
5132 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005133 }
5134 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005135 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MIN,
5136 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005137 }
5138 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005139 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TOOL_WIDTH, RAW_TOOL_MIN, RAW_TOOL_MAX, 0,
5140 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005141 }
5142 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005143 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_DISTANCE, RAW_DISTANCE_MIN,
5144 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005145 }
5146 if (axes & TILT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005147 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_X, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
5148 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_Y, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005149 }
5150}
5151
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005152void SingleTouchInputMapperTest::processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005153 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
5154 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
5155 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005156}
5157
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005158void SingleTouchInputMapperTest::processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005159 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
5160 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005161}
5162
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005163void SingleTouchInputMapperTest::processUp(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005164 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005165}
5166
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005167void SingleTouchInputMapperTest::processPressure(SingleTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005168 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005169}
5170
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005171void SingleTouchInputMapperTest::processToolMajor(SingleTouchInputMapper& mapper,
5172 int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005173 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TOOL_WIDTH, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005174}
5175
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005176void SingleTouchInputMapperTest::processDistance(SingleTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005177 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005178}
5179
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005180void SingleTouchInputMapperTest::processTilt(SingleTouchInputMapper& mapper, int32_t tiltX,
5181 int32_t tiltY) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005182 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_X, tiltX);
5183 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_Y, tiltY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005184}
5185
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005186void SingleTouchInputMapperTest::processKey(SingleTouchInputMapper& mapper, int32_t code,
5187 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005188 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005189}
5190
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005191void SingleTouchInputMapperTest::processSync(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005192 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005193}
5194
Michael Wrightd02c5b62014-02-10 15:10:22 -08005195TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005196 prepareButtons();
5197 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005198 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005199
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005200 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005201}
5202
5203TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndIsACursor_ReturnsTouchPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005204 mFakeEventHub->addRelativeAxis(EVENTHUB_ID, REL_X);
5205 mFakeEventHub->addRelativeAxis(EVENTHUB_ID, REL_Y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005206 prepareButtons();
5207 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005208 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005209
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005210 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005211}
5212
5213TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchPad_ReturnsTouchPad) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005214 prepareButtons();
5215 prepareAxes(POSITION);
5216 addConfigurationProperty("touch.deviceType", "touchPad");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005217 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005218
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005219 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005220}
5221
5222TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005223 prepareButtons();
5224 prepareAxes(POSITION);
5225 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005226 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005227
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005228 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005229}
5230
5231TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005232 addConfigurationProperty("touch.deviceType", "touchScreen");
5233 prepareDisplay(DISPLAY_ORIENTATION_0);
5234 prepareButtons();
5235 prepareAxes(POSITION);
5236 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005237 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005238
5239 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005240 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005241
5242 // Virtual key is down.
5243 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5244 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5245 processDown(mapper, x, y);
5246 processSync(mapper);
5247 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5248
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005249 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005250
5251 // Virtual key is up.
5252 processUp(mapper);
5253 processSync(mapper);
5254 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5255
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005256 ASSERT_EQ(AKEY_STATE_UP, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005257}
5258
5259TEST_F(SingleTouchInputMapperTest, GetScanCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005260 addConfigurationProperty("touch.deviceType", "touchScreen");
5261 prepareDisplay(DISPLAY_ORIENTATION_0);
5262 prepareButtons();
5263 prepareAxes(POSITION);
5264 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005265 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005266
5267 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005268 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005269
5270 // Virtual key is down.
5271 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5272 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5273 processDown(mapper, x, y);
5274 processSync(mapper);
5275 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5276
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005277 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005278
5279 // Virtual key is up.
5280 processUp(mapper);
5281 processSync(mapper);
5282 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5283
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005284 ASSERT_EQ(AKEY_STATE_UP, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005285}
5286
5287TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005288 addConfigurationProperty("touch.deviceType", "touchScreen");
5289 prepareDisplay(DISPLAY_ORIENTATION_0);
5290 prepareButtons();
5291 prepareAxes(POSITION);
5292 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005293 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005294
5295 const int32_t keys[2] = { AKEYCODE_HOME, AKEYCODE_A };
5296 uint8_t flags[2] = { 0, 0 };
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005297 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, 2, keys, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005298 ASSERT_TRUE(flags[0]);
5299 ASSERT_FALSE(flags[1]);
5300}
5301
5302TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005303 addConfigurationProperty("touch.deviceType", "touchScreen");
5304 prepareDisplay(DISPLAY_ORIENTATION_0);
5305 prepareButtons();
5306 prepareAxes(POSITION);
5307 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005308 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005309
arthurhungdcef2dc2020-08-11 14:47:50 +08005310 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005311
5312 NotifyKeyArgs args;
5313
5314 // Press virtual key.
5315 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5316 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5317 processDown(mapper, x, y);
5318 processSync(mapper);
5319
5320 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
5321 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5322 ASSERT_EQ(DEVICE_ID, args.deviceId);
5323 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
5324 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
5325 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
5326 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5327 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5328 ASSERT_EQ(KEY_HOME, args.scanCode);
5329 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5330 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5331
5332 // Release virtual key.
5333 processUp(mapper);
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_UP, 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 // Should not have sent any motions.
5349 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5350}
5351
5352TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005353 addConfigurationProperty("touch.deviceType", "touchScreen");
5354 prepareDisplay(DISPLAY_ORIENTATION_0);
5355 prepareButtons();
5356 prepareAxes(POSITION);
5357 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005358 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005359
arthurhungdcef2dc2020-08-11 14:47:50 +08005360 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005361
5362 NotifyKeyArgs keyArgs;
5363
5364 // Press virtual key.
5365 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5366 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5367 processDown(mapper, x, y);
5368 processSync(mapper);
5369
5370 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5371 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5372 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5373 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5374 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5375 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5376 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags);
5377 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5378 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5379 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5380 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5381
5382 // Move out of bounds. This should generate a cancel and a pointer down since we moved
5383 // into the display area.
5384 y -= 100;
5385 processMove(mapper, x, y);
5386 processSync(mapper);
5387
5388 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5389 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5390 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5391 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5392 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5393 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5394 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
5395 | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags);
5396 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5397 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5398 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5399 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5400
5401 NotifyMotionArgs motionArgs;
5402 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5403 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5404 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5405 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5406 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5407 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5408 ASSERT_EQ(0, motionArgs.flags);
5409 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5410 ASSERT_EQ(0, motionArgs.buttonState);
5411 ASSERT_EQ(0, motionArgs.edgeFlags);
5412 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5413 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5414 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5415 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5416 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5417 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5418 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5419 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5420
5421 // Keep moving out of bounds. Should generate a pointer move.
5422 y -= 50;
5423 processMove(mapper, x, y);
5424 processSync(mapper);
5425
5426 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5427 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5428 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5429 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5430 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5431 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5432 ASSERT_EQ(0, motionArgs.flags);
5433 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5434 ASSERT_EQ(0, motionArgs.buttonState);
5435 ASSERT_EQ(0, motionArgs.edgeFlags);
5436 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5437 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5438 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5439 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5440 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5441 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5442 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5443 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5444
5445 // Release out of bounds. Should generate a pointer up.
5446 processUp(mapper);
5447 processSync(mapper);
5448
5449 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5450 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5451 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5452 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5453 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5454 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5455 ASSERT_EQ(0, motionArgs.flags);
5456 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5457 ASSERT_EQ(0, motionArgs.buttonState);
5458 ASSERT_EQ(0, motionArgs.edgeFlags);
5459 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5460 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5461 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5462 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5463 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5464 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5465 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5466 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5467
5468 // Should not have sent any more keys or motions.
5469 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5470 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5471}
5472
5473TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005474 addConfigurationProperty("touch.deviceType", "touchScreen");
5475 prepareDisplay(DISPLAY_ORIENTATION_0);
5476 prepareButtons();
5477 prepareAxes(POSITION);
5478 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005479 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005480
arthurhungdcef2dc2020-08-11 14:47:50 +08005481 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005482
5483 NotifyMotionArgs motionArgs;
5484
5485 // Initially go down out of bounds.
5486 int32_t x = -10;
5487 int32_t y = -10;
5488 processDown(mapper, x, y);
5489 processSync(mapper);
5490
5491 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5492
5493 // Move into the display area. Should generate a pointer down.
5494 x = 50;
5495 y = 75;
5496 processMove(mapper, x, y);
5497 processSync(mapper);
5498
5499 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5500 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5501 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5502 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5503 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5504 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5505 ASSERT_EQ(0, motionArgs.flags);
5506 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5507 ASSERT_EQ(0, motionArgs.buttonState);
5508 ASSERT_EQ(0, motionArgs.edgeFlags);
5509 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5510 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5511 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5512 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5513 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5514 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5515 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5516 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5517
5518 // Release. Should generate a pointer up.
5519 processUp(mapper);
5520 processSync(mapper);
5521
5522 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5523 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5524 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5525 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5526 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5527 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5528 ASSERT_EQ(0, motionArgs.flags);
5529 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5530 ASSERT_EQ(0, motionArgs.buttonState);
5531 ASSERT_EQ(0, motionArgs.edgeFlags);
5532 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5533 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5534 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5535 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5536 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5537 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5538 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5539 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5540
5541 // Should not have sent any more keys or motions.
5542 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5543 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5544}
5545
Santos Cordonfa5cf462017-04-05 10:37:00 -07005546TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture_VirtualDisplay) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005547 addConfigurationProperty("touch.deviceType", "touchScreen");
5548 addConfigurationProperty("touch.displayId", VIRTUAL_DISPLAY_UNIQUE_ID);
5549
5550 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
5551 prepareButtons();
5552 prepareAxes(POSITION);
5553 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005554 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Santos Cordonfa5cf462017-04-05 10:37:00 -07005555
arthurhungdcef2dc2020-08-11 14:47:50 +08005556 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005557
5558 NotifyMotionArgs motionArgs;
5559
5560 // Down.
5561 int32_t x = 100;
5562 int32_t y = 125;
5563 processDown(mapper, x, y);
5564 processSync(mapper);
5565
5566 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5567 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5568 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5569 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5570 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5571 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5572 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5573 ASSERT_EQ(0, motionArgs.flags);
5574 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5575 ASSERT_EQ(0, motionArgs.buttonState);
5576 ASSERT_EQ(0, motionArgs.edgeFlags);
5577 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5578 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5579 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5580 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5581 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5582 1, 0, 0, 0, 0, 0, 0, 0));
5583 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5584 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5585 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5586
5587 // Move.
5588 x += 50;
5589 y += 75;
5590 processMove(mapper, x, y);
5591 processSync(mapper);
5592
5593 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5594 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5595 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5596 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5597 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5598 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5599 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5600 ASSERT_EQ(0, motionArgs.flags);
5601 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5602 ASSERT_EQ(0, motionArgs.buttonState);
5603 ASSERT_EQ(0, motionArgs.edgeFlags);
5604 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5605 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5606 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5607 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5608 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5609 1, 0, 0, 0, 0, 0, 0, 0));
5610 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5611 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5612 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5613
5614 // Up.
5615 processUp(mapper);
5616 processSync(mapper);
5617
5618 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5619 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5620 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5621 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5622 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5623 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5624 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5625 ASSERT_EQ(0, motionArgs.flags);
5626 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5627 ASSERT_EQ(0, motionArgs.buttonState);
5628 ASSERT_EQ(0, motionArgs.edgeFlags);
5629 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5630 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5631 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5632 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5633 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5634 1, 0, 0, 0, 0, 0, 0, 0));
5635 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5636 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5637 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5638
5639 // Should not have sent any more keys or motions.
5640 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5641 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5642}
5643
Michael Wrightd02c5b62014-02-10 15:10:22 -08005644TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005645 addConfigurationProperty("touch.deviceType", "touchScreen");
5646 prepareDisplay(DISPLAY_ORIENTATION_0);
5647 prepareButtons();
5648 prepareAxes(POSITION);
5649 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005650 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005651
arthurhungdcef2dc2020-08-11 14:47:50 +08005652 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005653
5654 NotifyMotionArgs motionArgs;
5655
5656 // Down.
5657 int32_t x = 100;
5658 int32_t y = 125;
5659 processDown(mapper, x, y);
5660 processSync(mapper);
5661
5662 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5663 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5664 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5665 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5666 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5667 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5668 ASSERT_EQ(0, motionArgs.flags);
5669 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5670 ASSERT_EQ(0, motionArgs.buttonState);
5671 ASSERT_EQ(0, motionArgs.edgeFlags);
5672 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5673 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5674 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5675 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5676 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5677 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5678 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5679 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5680
5681 // Move.
5682 x += 50;
5683 y += 75;
5684 processMove(mapper, x, y);
5685 processSync(mapper);
5686
5687 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5688 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5689 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5690 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5691 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5692 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5693 ASSERT_EQ(0, motionArgs.flags);
5694 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5695 ASSERT_EQ(0, motionArgs.buttonState);
5696 ASSERT_EQ(0, motionArgs.edgeFlags);
5697 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5698 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5699 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5700 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5701 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5702 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5703 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5704 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5705
5706 // Up.
5707 processUp(mapper);
5708 processSync(mapper);
5709
5710 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5711 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5712 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5713 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5714 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5715 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5716 ASSERT_EQ(0, motionArgs.flags);
5717 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5718 ASSERT_EQ(0, motionArgs.buttonState);
5719 ASSERT_EQ(0, motionArgs.edgeFlags);
5720 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5721 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5722 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5723 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5724 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5725 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5726 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5727 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5728
5729 // Should not have sent any more keys or motions.
5730 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5731 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5732}
5733
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005734TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_DoesNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005735 addConfigurationProperty("touch.deviceType", "touchScreen");
5736 prepareButtons();
5737 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005738 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
5739 // need to be rotated. Touchscreens are orientation-aware by default.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005740 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005741
5742 NotifyMotionArgs args;
5743
5744 // Rotation 90.
5745 prepareDisplay(DISPLAY_ORIENTATION_90);
5746 processDown(mapper, toRawX(50), toRawY(75));
5747 processSync(mapper);
5748
5749 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5750 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5751 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5752
5753 processUp(mapper);
5754 processSync(mapper);
5755 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5756}
5757
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005758TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_RotatesMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005759 addConfigurationProperty("touch.deviceType", "touchScreen");
5760 prepareButtons();
5761 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005762 // Since InputReader works in the un-rotated coordinate space, only devices that are not
5763 // orientation-aware are affected by display rotation.
5764 addConfigurationProperty("touch.orientationAware", "0");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005765 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005766
5767 NotifyMotionArgs args;
5768
5769 // Rotation 0.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005770 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005771 prepareDisplay(DISPLAY_ORIENTATION_0);
5772 processDown(mapper, toRawX(50), toRawY(75));
5773 processSync(mapper);
5774
5775 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5776 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5777 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5778
5779 processUp(mapper);
5780 processSync(mapper);
5781 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5782
5783 // Rotation 90.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005784 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005785 prepareDisplay(DISPLAY_ORIENTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005786 processDown(mapper, toRawX(75), RAW_Y_MAX - toRawY(50) + RAW_Y_MIN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005787 processSync(mapper);
5788
5789 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5790 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5791 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5792
5793 processUp(mapper);
5794 processSync(mapper);
5795 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5796
5797 // Rotation 180.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005798 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005799 prepareDisplay(DISPLAY_ORIENTATION_180);
5800 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
5801 processSync(mapper);
5802
5803 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5804 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5805 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5806
5807 processUp(mapper);
5808 processSync(mapper);
5809 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5810
5811 // Rotation 270.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005812 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005813 prepareDisplay(DISPLAY_ORIENTATION_270);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005814 processDown(mapper, RAW_X_MAX - toRawX(75) + RAW_X_MIN, toRawY(50));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005815 processSync(mapper);
5816
5817 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5818 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5819 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5820
5821 processUp(mapper);
5822 processSync(mapper);
5823 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5824}
5825
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005826TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation0_RotatesMotions) {
5827 addConfigurationProperty("touch.deviceType", "touchScreen");
5828 prepareButtons();
5829 prepareAxes(POSITION);
5830 addConfigurationProperty("touch.orientationAware", "1");
5831 addConfigurationProperty("touch.orientation", "ORIENTATION_0");
5832 clearViewports();
5833 prepareDisplay(DISPLAY_ORIENTATION_0);
5834 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5835 NotifyMotionArgs args;
5836
5837 // Orientation 0.
5838 processDown(mapper, toRawX(50), toRawY(75));
5839 processSync(mapper);
5840
5841 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5842 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5843 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5844
5845 processUp(mapper);
5846 processSync(mapper);
5847 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5848}
5849
5850TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation90_RotatesMotions) {
5851 addConfigurationProperty("touch.deviceType", "touchScreen");
5852 prepareButtons();
5853 prepareAxes(POSITION);
5854 addConfigurationProperty("touch.orientationAware", "1");
5855 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
5856 clearViewports();
5857 prepareDisplay(DISPLAY_ORIENTATION_0);
5858 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5859 NotifyMotionArgs args;
5860
5861 // Orientation 90.
5862 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
5863 processSync(mapper);
5864
5865 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5866 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5867 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5868
5869 processUp(mapper);
5870 processSync(mapper);
5871 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5872}
5873
5874TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation180_RotatesMotions) {
5875 addConfigurationProperty("touch.deviceType", "touchScreen");
5876 prepareButtons();
5877 prepareAxes(POSITION);
5878 addConfigurationProperty("touch.orientationAware", "1");
5879 addConfigurationProperty("touch.orientation", "ORIENTATION_180");
5880 clearViewports();
5881 prepareDisplay(DISPLAY_ORIENTATION_0);
5882 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5883 NotifyMotionArgs args;
5884
5885 // Orientation 180.
5886 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
5887 processSync(mapper);
5888
5889 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5890 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5891 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5892
5893 processUp(mapper);
5894 processSync(mapper);
5895 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5896}
5897
5898TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation270_RotatesMotions) {
5899 addConfigurationProperty("touch.deviceType", "touchScreen");
5900 prepareButtons();
5901 prepareAxes(POSITION);
5902 addConfigurationProperty("touch.orientationAware", "1");
5903 addConfigurationProperty("touch.orientation", "ORIENTATION_270");
5904 clearViewports();
5905 prepareDisplay(DISPLAY_ORIENTATION_0);
5906 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5907 NotifyMotionArgs args;
5908
5909 // Orientation 270.
5910 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
5911 processSync(mapper);
5912
5913 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5914 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5915 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5916
5917 processUp(mapper);
5918 processSync(mapper);
5919 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5920}
5921
5922TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationSpecified_RotatesMotionWithDisplay) {
5923 addConfigurationProperty("touch.deviceType", "touchScreen");
5924 prepareButtons();
5925 prepareAxes(POSITION);
5926 // Since InputReader works in the un-rotated coordinate space, only devices that are not
5927 // orientation-aware are affected by display rotation.
5928 addConfigurationProperty("touch.orientationAware", "0");
5929 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
5930 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5931
5932 NotifyMotionArgs args;
5933
5934 // Orientation 90, Rotation 0.
5935 clearViewports();
5936 prepareDisplay(DISPLAY_ORIENTATION_0);
5937 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
5938 processSync(mapper);
5939
5940 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5941 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5942 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5943
5944 processUp(mapper);
5945 processSync(mapper);
5946 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5947
5948 // Orientation 90, Rotation 90.
5949 clearViewports();
5950 prepareDisplay(DISPLAY_ORIENTATION_90);
5951 processDown(mapper, toRotatedRawX(50), toRotatedRawY(75));
5952 processSync(mapper);
5953
5954 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5955 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5956 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5957
5958 processUp(mapper);
5959 processSync(mapper);
5960 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5961
5962 // Orientation 90, Rotation 180.
5963 clearViewports();
5964 prepareDisplay(DISPLAY_ORIENTATION_180);
5965 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
5966 processSync(mapper);
5967
5968 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5969 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5970 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5971
5972 processUp(mapper);
5973 processSync(mapper);
5974 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5975
5976 // Orientation 90, Rotation 270.
5977 clearViewports();
5978 prepareDisplay(DISPLAY_ORIENTATION_270);
5979 processDown(mapper, RAW_X_MAX - toRotatedRawX(50) + RAW_X_MIN,
5980 RAW_Y_MAX - toRotatedRawY(75) + RAW_Y_MIN);
5981 processSync(mapper);
5982
5983 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5984 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5985 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5986
5987 processUp(mapper);
5988 processSync(mapper);
5989 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5990}
5991
Michael Wrightd02c5b62014-02-10 15:10:22 -08005992TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005993 addConfigurationProperty("touch.deviceType", "touchScreen");
5994 prepareDisplay(DISPLAY_ORIENTATION_0);
5995 prepareButtons();
5996 prepareAxes(POSITION | PRESSURE | TOOL | DISTANCE | TILT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005997 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005998
5999 // These calculations are based on the input device calibration documentation.
6000 int32_t rawX = 100;
6001 int32_t rawY = 200;
6002 int32_t rawPressure = 10;
6003 int32_t rawToolMajor = 12;
6004 int32_t rawDistance = 2;
6005 int32_t rawTiltX = 30;
6006 int32_t rawTiltY = 110;
6007
6008 float x = toDisplayX(rawX);
6009 float y = toDisplayY(rawY);
6010 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
6011 float size = float(rawToolMajor) / RAW_TOOL_MAX;
6012 float tool = float(rawToolMajor) * GEOMETRIC_SCALE;
6013 float distance = float(rawDistance);
6014
6015 float tiltCenter = (RAW_TILT_MAX + RAW_TILT_MIN) * 0.5f;
6016 float tiltScale = M_PI / 180;
6017 float tiltXAngle = (rawTiltX - tiltCenter) * tiltScale;
6018 float tiltYAngle = (rawTiltY - tiltCenter) * tiltScale;
6019 float orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
6020 float tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
6021
6022 processDown(mapper, rawX, rawY);
6023 processPressure(mapper, rawPressure);
6024 processToolMajor(mapper, rawToolMajor);
6025 processDistance(mapper, rawDistance);
6026 processTilt(mapper, rawTiltX, rawTiltY);
6027 processSync(mapper);
6028
6029 NotifyMotionArgs args;
6030 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6031 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6032 x, y, pressure, size, tool, tool, tool, tool, orientation, distance));
6033 ASSERT_EQ(tilt, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TILT));
6034}
6035
Jason Gerecke489fda82012-09-07 17:19:40 -07006036TEST_F(SingleTouchInputMapperTest, Process_XYAxes_AffineCalibration) {
Jason Gerecke489fda82012-09-07 17:19:40 -07006037 addConfigurationProperty("touch.deviceType", "touchScreen");
6038 prepareDisplay(DISPLAY_ORIENTATION_0);
6039 prepareLocationCalibration();
6040 prepareButtons();
6041 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006042 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Jason Gerecke489fda82012-09-07 17:19:40 -07006043
6044 int32_t rawX = 100;
6045 int32_t rawY = 200;
6046
6047 float x = toDisplayX(toCookedX(rawX, rawY));
6048 float y = toDisplayY(toCookedY(rawX, rawY));
6049
6050 processDown(mapper, rawX, rawY);
6051 processSync(mapper);
6052
6053 NotifyMotionArgs args;
6054 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6055 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6056 x, y, 1, 0, 0, 0, 0, 0, 0, 0));
6057}
6058
Michael Wrightd02c5b62014-02-10 15:10:22 -08006059TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006060 addConfigurationProperty("touch.deviceType", "touchScreen");
6061 prepareDisplay(DISPLAY_ORIENTATION_0);
6062 prepareButtons();
6063 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006064 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006065
6066 NotifyMotionArgs motionArgs;
6067 NotifyKeyArgs keyArgs;
6068
6069 processDown(mapper, 100, 200);
6070 processSync(mapper);
6071 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6072 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6073 ASSERT_EQ(0, motionArgs.buttonState);
6074
6075 // press BTN_LEFT, release BTN_LEFT
6076 processKey(mapper, BTN_LEFT, 1);
6077 processSync(mapper);
6078 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6079 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6080 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6081
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006082 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6083 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6084 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6085
Michael Wrightd02c5b62014-02-10 15:10:22 -08006086 processKey(mapper, BTN_LEFT, 0);
6087 processSync(mapper);
6088 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006089 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006090 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006091
6092 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006093 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006094 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006095
6096 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
6097 processKey(mapper, BTN_RIGHT, 1);
6098 processKey(mapper, BTN_MIDDLE, 1);
6099 processSync(mapper);
6100 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6101 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6102 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6103 motionArgs.buttonState);
6104
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006105 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6106 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6107 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
6108
6109 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6110 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6111 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6112 motionArgs.buttonState);
6113
Michael Wrightd02c5b62014-02-10 15:10:22 -08006114 processKey(mapper, BTN_RIGHT, 0);
6115 processSync(mapper);
6116 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006117 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006118 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006119
6120 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006121 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006122 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006123
6124 processKey(mapper, BTN_MIDDLE, 0);
6125 processSync(mapper);
6126 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006127 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006128 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006129
6130 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006131 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006132 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006133
6134 // press BTN_BACK, release BTN_BACK
6135 processKey(mapper, BTN_BACK, 1);
6136 processSync(mapper);
6137 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6138 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6139 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006140
Michael Wrightd02c5b62014-02-10 15:10:22 -08006141 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006142 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006143 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6144
6145 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6146 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6147 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006148
6149 processKey(mapper, BTN_BACK, 0);
6150 processSync(mapper);
6151 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006152 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006153 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006154
6155 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006156 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006157 ASSERT_EQ(0, motionArgs.buttonState);
6158
Michael Wrightd02c5b62014-02-10 15:10:22 -08006159 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6160 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6161 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6162
6163 // press BTN_SIDE, release BTN_SIDE
6164 processKey(mapper, BTN_SIDE, 1);
6165 processSync(mapper);
6166 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6167 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6168 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006169
Michael Wrightd02c5b62014-02-10 15:10:22 -08006170 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006171 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006172 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6173
6174 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6175 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6176 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006177
6178 processKey(mapper, BTN_SIDE, 0);
6179 processSync(mapper);
6180 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006181 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006182 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006183
6184 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006185 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006186 ASSERT_EQ(0, motionArgs.buttonState);
6187
Michael Wrightd02c5b62014-02-10 15:10:22 -08006188 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6189 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6190 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6191
6192 // press BTN_FORWARD, release BTN_FORWARD
6193 processKey(mapper, BTN_FORWARD, 1);
6194 processSync(mapper);
6195 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6196 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6197 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006198
Michael Wrightd02c5b62014-02-10 15:10:22 -08006199 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006200 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006201 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6202
6203 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6204 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6205 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006206
6207 processKey(mapper, BTN_FORWARD, 0);
6208 processSync(mapper);
6209 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006210 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006211 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006212
6213 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006214 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006215 ASSERT_EQ(0, motionArgs.buttonState);
6216
Michael Wrightd02c5b62014-02-10 15:10:22 -08006217 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6218 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6219 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6220
6221 // press BTN_EXTRA, release BTN_EXTRA
6222 processKey(mapper, BTN_EXTRA, 1);
6223 processSync(mapper);
6224 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6225 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6226 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006227
Michael Wrightd02c5b62014-02-10 15:10:22 -08006228 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006229 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006230 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6231
6232 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6233 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6234 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006235
6236 processKey(mapper, BTN_EXTRA, 0);
6237 processSync(mapper);
6238 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006239 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006240 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006241
6242 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006243 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006244 ASSERT_EQ(0, motionArgs.buttonState);
6245
Michael Wrightd02c5b62014-02-10 15:10:22 -08006246 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6247 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6248 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6249
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006250 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6251
Michael Wrightd02c5b62014-02-10 15:10:22 -08006252 // press BTN_STYLUS, release BTN_STYLUS
6253 processKey(mapper, BTN_STYLUS, 1);
6254 processSync(mapper);
6255 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6256 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006257 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
6258
6259 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6260 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6261 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006262
6263 processKey(mapper, BTN_STYLUS, 0);
6264 processSync(mapper);
6265 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006266 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006267 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006268
6269 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006270 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006271 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006272
6273 // press BTN_STYLUS2, release BTN_STYLUS2
6274 processKey(mapper, BTN_STYLUS2, 1);
6275 processSync(mapper);
6276 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6277 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006278 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
6279
6280 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6281 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6282 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006283
6284 processKey(mapper, BTN_STYLUS2, 0);
6285 processSync(mapper);
6286 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006287 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006288 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006289
6290 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006291 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006292 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006293
6294 // release touch
6295 processUp(mapper);
6296 processSync(mapper);
6297 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6298 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6299 ASSERT_EQ(0, motionArgs.buttonState);
6300}
6301
6302TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006303 addConfigurationProperty("touch.deviceType", "touchScreen");
6304 prepareDisplay(DISPLAY_ORIENTATION_0);
6305 prepareButtons();
6306 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006307 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006308
6309 NotifyMotionArgs motionArgs;
6310
6311 // default tool type is finger
6312 processDown(mapper, 100, 200);
6313 processSync(mapper);
6314 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6315 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6316 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6317
6318 // eraser
6319 processKey(mapper, BTN_TOOL_RUBBER, 1);
6320 processSync(mapper);
6321 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6322 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6323 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6324
6325 // stylus
6326 processKey(mapper, BTN_TOOL_RUBBER, 0);
6327 processKey(mapper, BTN_TOOL_PEN, 1);
6328 processSync(mapper);
6329 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6330 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6331 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6332
6333 // brush
6334 processKey(mapper, BTN_TOOL_PEN, 0);
6335 processKey(mapper, BTN_TOOL_BRUSH, 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_STYLUS, motionArgs.pointerProperties[0].toolType);
6340
6341 // pencil
6342 processKey(mapper, BTN_TOOL_BRUSH, 0);
6343 processKey(mapper, BTN_TOOL_PENCIL, 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
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08006349 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08006350 processKey(mapper, BTN_TOOL_PENCIL, 0);
6351 processKey(mapper, BTN_TOOL_AIRBRUSH, 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 // mouse
6358 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
6359 processKey(mapper, BTN_TOOL_MOUSE, 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_MOUSE, motionArgs.pointerProperties[0].toolType);
6364
6365 // lens
6366 processKey(mapper, BTN_TOOL_MOUSE, 0);
6367 processKey(mapper, BTN_TOOL_LENS, 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_MOUSE, motionArgs.pointerProperties[0].toolType);
6372
6373 // double-tap
6374 processKey(mapper, BTN_TOOL_LENS, 0);
6375 processKey(mapper, BTN_TOOL_DOUBLETAP, 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_FINGER, motionArgs.pointerProperties[0].toolType);
6380
6381 // triple-tap
6382 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
6383 processKey(mapper, BTN_TOOL_TRIPLETAP, 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_FINGER, motionArgs.pointerProperties[0].toolType);
6388
6389 // quad-tap
6390 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
6391 processKey(mapper, BTN_TOOL_QUADTAP, 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 // finger
6398 processKey(mapper, BTN_TOOL_QUADTAP, 0);
6399 processKey(mapper, BTN_TOOL_FINGER, 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 // stylus trumps finger
6406 processKey(mapper, BTN_TOOL_PEN, 1);
6407 processSync(mapper);
6408 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6409 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6410 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6411
6412 // eraser trumps stylus
6413 processKey(mapper, BTN_TOOL_RUBBER, 1);
6414 processSync(mapper);
6415 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6416 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6417 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6418
6419 // mouse trumps eraser
6420 processKey(mapper, BTN_TOOL_MOUSE, 1);
6421 processSync(mapper);
6422 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6423 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6424 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6425
6426 // back to default tool type
6427 processKey(mapper, BTN_TOOL_MOUSE, 0);
6428 processKey(mapper, BTN_TOOL_RUBBER, 0);
6429 processKey(mapper, BTN_TOOL_PEN, 0);
6430 processKey(mapper, BTN_TOOL_FINGER, 0);
6431 processSync(mapper);
6432 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6433 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6434 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6435}
6436
6437TEST_F(SingleTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006438 addConfigurationProperty("touch.deviceType", "touchScreen");
6439 prepareDisplay(DISPLAY_ORIENTATION_0);
6440 prepareButtons();
6441 prepareAxes(POSITION);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006442 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_FINGER, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006443 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006444
6445 NotifyMotionArgs motionArgs;
6446
6447 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
6448 processKey(mapper, BTN_TOOL_FINGER, 1);
6449 processMove(mapper, 100, 200);
6450 processSync(mapper);
6451 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6452 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6453 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6454 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6455
6456 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6457 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6458 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6459 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6460
6461 // move a little
6462 processMove(mapper, 150, 250);
6463 processSync(mapper);
6464 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6465 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6466 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6467 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6468
6469 // down when BTN_TOUCH is pressed, pressure defaults to 1
6470 processKey(mapper, BTN_TOUCH, 1);
6471 processSync(mapper);
6472 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6473 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6474 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6475 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6476
6477 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6478 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6479 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6480 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6481
6482 // up when BTN_TOUCH is released, hover restored
6483 processKey(mapper, BTN_TOUCH, 0);
6484 processSync(mapper);
6485 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6486 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6487 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6488 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6489
6490 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6491 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6492 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6493 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6494
6495 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6496 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6497 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6498 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6499
6500 // exit hover when pointer goes away
6501 processKey(mapper, BTN_TOOL_FINGER, 0);
6502 processSync(mapper);
6503 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6504 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6505 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6506 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6507}
6508
6509TEST_F(SingleTouchInputMapperTest, Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006510 addConfigurationProperty("touch.deviceType", "touchScreen");
6511 prepareDisplay(DISPLAY_ORIENTATION_0);
6512 prepareButtons();
6513 prepareAxes(POSITION | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006514 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006515
6516 NotifyMotionArgs motionArgs;
6517
6518 // initially hovering because pressure is 0
6519 processDown(mapper, 100, 200);
6520 processPressure(mapper, 0);
6521 processSync(mapper);
6522 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6523 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6524 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6525 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6526
6527 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6528 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6529 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6530 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6531
6532 // move a little
6533 processMove(mapper, 150, 250);
6534 processSync(mapper);
6535 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6536 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6537 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6538 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6539
6540 // down when pressure is non-zero
6541 processPressure(mapper, RAW_PRESSURE_MAX);
6542 processSync(mapper);
6543 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6544 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6545 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6546 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6547
6548 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6549 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6550 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6551 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6552
6553 // up when pressure becomes 0, hover restored
6554 processPressure(mapper, 0);
6555 processSync(mapper);
6556 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6557 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6558 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6559 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6560
6561 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6562 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6563 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6564 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6565
6566 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6567 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6568 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6569 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6570
6571 // exit hover when pointer goes away
6572 processUp(mapper);
6573 processSync(mapper);
6574 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6575 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6576 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6577 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6578}
6579
Prabir Pradhan5632d622021-09-06 07:57:20 -07006580// --- TouchDisplayProjectionTest ---
6581
6582class TouchDisplayProjectionTest : public SingleTouchInputMapperTest {
6583public:
6584 // The values inside DisplayViewport are expected to be pre-rotated. This updates the current
6585 // DisplayViewport to pre-rotate the values. The viewport's physical display will be set to the
6586 // rotated equivalent of the given un-rotated physical display bounds.
6587 void configurePhysicalDisplay(int32_t orientation, Rect naturalPhysicalDisplay) {
6588 uint32_t inverseRotationFlags;
6589 auto width = DISPLAY_WIDTH;
6590 auto height = DISPLAY_HEIGHT;
6591 switch (orientation) {
6592 case DISPLAY_ORIENTATION_90:
6593 inverseRotationFlags = ui::Transform::ROT_270;
6594 std::swap(width, height);
6595 break;
6596 case DISPLAY_ORIENTATION_180:
6597 inverseRotationFlags = ui::Transform::ROT_180;
6598 break;
6599 case DISPLAY_ORIENTATION_270:
6600 inverseRotationFlags = ui::Transform::ROT_90;
6601 std::swap(width, height);
6602 break;
6603 case DISPLAY_ORIENTATION_0:
6604 inverseRotationFlags = ui::Transform::ROT_0;
6605 break;
6606 default:
6607 FAIL() << "Invalid orientation: " << orientation;
6608 }
6609
6610 const ui::Transform rotation(inverseRotationFlags, width, height);
6611 const Rect rotatedPhysicalDisplay = rotation.transform(naturalPhysicalDisplay);
6612
6613 std::optional<DisplayViewport> internalViewport =
6614 *mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6615 DisplayViewport& v = *internalViewport;
6616 v.displayId = DISPLAY_ID;
6617 v.orientation = orientation;
6618
6619 v.logicalLeft = 0;
6620 v.logicalTop = 0;
6621 v.logicalRight = 100;
6622 v.logicalBottom = 100;
6623
6624 v.physicalLeft = rotatedPhysicalDisplay.left;
6625 v.physicalTop = rotatedPhysicalDisplay.top;
6626 v.physicalRight = rotatedPhysicalDisplay.right;
6627 v.physicalBottom = rotatedPhysicalDisplay.bottom;
6628
6629 v.deviceWidth = width;
6630 v.deviceHeight = height;
6631
6632 v.isActive = true;
6633 v.uniqueId = UNIQUE_ID;
6634 v.type = ViewportType::INTERNAL;
6635 mFakePolicy->updateViewport(v);
6636 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
6637 }
6638
6639 void assertReceivedMove(const Point& point) {
6640 NotifyMotionArgs motionArgs;
6641 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6642 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6643 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6644 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], point.x, point.y,
6645 1, 0, 0, 0, 0, 0, 0, 0));
6646 }
6647};
6648
6649TEST_F(TouchDisplayProjectionTest, IgnoresTouchesOutsidePhysicalDisplay) {
6650 addConfigurationProperty("touch.deviceType", "touchScreen");
6651 prepareDisplay(DISPLAY_ORIENTATION_0);
6652
6653 prepareButtons();
6654 prepareAxes(POSITION);
6655 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6656
6657 NotifyMotionArgs motionArgs;
6658
6659 // Configure the DisplayViewport such that the logical display maps to a subsection of
6660 // the display panel called the physical display. Here, the physical display is bounded by the
6661 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
6662 static const Rect kPhysicalDisplay{10, 20, 70, 160};
6663 static const std::array<Point, 6> kPointsOutsidePhysicalDisplay{
6664 {{-10, -10}, {0, 0}, {5, 100}, {50, 15}, {75, 100}, {50, 165}}};
6665
6666 for (auto orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90, DISPLAY_ORIENTATION_180,
6667 DISPLAY_ORIENTATION_270}) {
6668 configurePhysicalDisplay(orientation, kPhysicalDisplay);
6669
6670 // Touches outside the physical display should be ignored, and should not generate any
6671 // events. Ensure touches at the following points that lie outside of the physical display
6672 // area do not generate any events.
6673 for (const auto& point : kPointsOutsidePhysicalDisplay) {
6674 processDown(mapper, toRawX(point.x), toRawY(point.y));
6675 processSync(mapper);
6676 processUp(mapper);
6677 processSync(mapper);
6678 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled())
6679 << "Unexpected event generated for touch outside physical display at point: "
6680 << point.x << ", " << point.y;
6681 }
6682 }
6683}
6684
6685TEST_F(TouchDisplayProjectionTest, EmitsTouchDownAfterEnteringPhysicalDisplay) {
6686 addConfigurationProperty("touch.deviceType", "touchScreen");
6687 prepareDisplay(DISPLAY_ORIENTATION_0);
6688
6689 prepareButtons();
6690 prepareAxes(POSITION);
6691 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6692
6693 NotifyMotionArgs motionArgs;
6694
6695 // Configure the DisplayViewport such that the logical display maps to a subsection of
6696 // the display panel called the physical display. Here, the physical display is bounded by the
6697 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
6698 static const Rect kPhysicalDisplay{10, 20, 70, 160};
6699
6700 for (auto orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90, DISPLAY_ORIENTATION_180,
6701 DISPLAY_ORIENTATION_270}) {
6702 configurePhysicalDisplay(orientation, kPhysicalDisplay);
6703
6704 // Touches that start outside the physical display should be ignored until it enters the
6705 // physical display bounds, at which point it should generate a down event. Start a touch at
6706 // the point (5, 100), which is outside the physical display bounds.
6707 static const Point kOutsidePoint{5, 100};
6708 processDown(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
6709 processSync(mapper);
6710 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6711
6712 // Move the touch into the physical display area. This should generate a pointer down.
6713 processMove(mapper, toRawX(11), toRawY(21));
6714 processSync(mapper);
6715 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6716 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6717 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6718 ASSERT_NO_FATAL_FAILURE(
6719 assertPointerCoords(motionArgs.pointerCoords[0], 11, 21, 1, 0, 0, 0, 0, 0, 0, 0));
6720
6721 // Move the touch inside the physical display area. This should generate a pointer move.
6722 processMove(mapper, toRawX(69), toRawY(159));
6723 processSync(mapper);
6724 assertReceivedMove({69, 159});
6725
6726 // Move outside the physical display area. Since the pointer is already down, this should
6727 // now continue generating events.
6728 processMove(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
6729 processSync(mapper);
6730 assertReceivedMove(kOutsidePoint);
6731
6732 // Release. This should generate a pointer up.
6733 processUp(mapper);
6734 processSync(mapper);
6735 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6736 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6737 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], kOutsidePoint.x,
6738 kOutsidePoint.y, 1, 0, 0, 0, 0, 0, 0, 0));
6739
6740 // Ensure no more events were generated.
6741 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6742 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6743 }
6744}
6745
Michael Wrightd02c5b62014-02-10 15:10:22 -08006746// --- MultiTouchInputMapperTest ---
6747
6748class MultiTouchInputMapperTest : public TouchInputMapperTest {
6749protected:
6750 void prepareAxes(int axes);
6751
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006752 void processPosition(MultiTouchInputMapper& mapper, int32_t x, int32_t y);
6753 void processTouchMajor(MultiTouchInputMapper& mapper, int32_t touchMajor);
6754 void processTouchMinor(MultiTouchInputMapper& mapper, int32_t touchMinor);
6755 void processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor);
6756 void processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor);
6757 void processOrientation(MultiTouchInputMapper& mapper, int32_t orientation);
6758 void processPressure(MultiTouchInputMapper& mapper, int32_t pressure);
6759 void processDistance(MultiTouchInputMapper& mapper, int32_t distance);
6760 void processId(MultiTouchInputMapper& mapper, int32_t id);
6761 void processSlot(MultiTouchInputMapper& mapper, int32_t slot);
6762 void processToolType(MultiTouchInputMapper& mapper, int32_t toolType);
6763 void processKey(MultiTouchInputMapper& mapper, int32_t code, int32_t value);
6764 void processMTSync(MultiTouchInputMapper& mapper);
6765 void processSync(MultiTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006766};
6767
6768void MultiTouchInputMapperTest::prepareAxes(int axes) {
6769 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006770 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
6771 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006772 }
6773 if (axes & TOUCH) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006774 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN,
6775 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006776 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006777 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN,
6778 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006779 }
6780 }
6781 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006782 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
6783 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006784 if (axes & MINOR) {
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08006785 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006786 RAW_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006787 }
6788 }
6789 if (axes & ORIENTATION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006790 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_ORIENTATION, RAW_ORIENTATION_MIN,
6791 RAW_ORIENTATION_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006792 }
6793 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006794 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_PRESSURE, RAW_PRESSURE_MIN,
6795 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006796 }
6797 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006798 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_DISTANCE, RAW_DISTANCE_MIN,
6799 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006800 }
6801 if (axes & ID) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006802 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX, 0,
6803 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006804 }
6805 if (axes & SLOT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006806 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
6807 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_MT_SLOT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006808 }
6809 if (axes & TOOL_TYPE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006810 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOOL_TYPE, 0, MT_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006811 }
6812}
6813
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006814void MultiTouchInputMapperTest::processPosition(MultiTouchInputMapper& mapper, int32_t x,
6815 int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006816 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_X, x);
6817 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006818}
6819
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006820void MultiTouchInputMapperTest::processTouchMajor(MultiTouchInputMapper& mapper,
6821 int32_t touchMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006822 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006823}
6824
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006825void MultiTouchInputMapperTest::processTouchMinor(MultiTouchInputMapper& mapper,
6826 int32_t touchMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006827 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006828}
6829
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006830void MultiTouchInputMapperTest::processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006831 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006832}
6833
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006834void MultiTouchInputMapperTest::processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006835 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006836}
6837
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006838void MultiTouchInputMapperTest::processOrientation(MultiTouchInputMapper& mapper,
6839 int32_t orientation) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006840 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_ORIENTATION, orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006841}
6842
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006843void MultiTouchInputMapperTest::processPressure(MultiTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006844 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006845}
6846
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006847void MultiTouchInputMapperTest::processDistance(MultiTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006848 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006849}
6850
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006851void MultiTouchInputMapperTest::processId(MultiTouchInputMapper& mapper, int32_t id) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006852 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TRACKING_ID, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006853}
6854
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006855void MultiTouchInputMapperTest::processSlot(MultiTouchInputMapper& mapper, int32_t slot) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006856 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_SLOT, slot);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006857}
6858
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006859void MultiTouchInputMapperTest::processToolType(MultiTouchInputMapper& mapper, int32_t toolType) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006860 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006861}
6862
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006863void MultiTouchInputMapperTest::processKey(MultiTouchInputMapper& mapper, int32_t code,
6864 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006865 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006866}
6867
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006868void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006869 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_MT_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006870}
6871
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006872void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006873 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006874}
6875
Michael Wrightd02c5b62014-02-10 15:10:22 -08006876TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006877 addConfigurationProperty("touch.deviceType", "touchScreen");
6878 prepareDisplay(DISPLAY_ORIENTATION_0);
6879 prepareAxes(POSITION);
6880 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006881 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006882
arthurhungdcef2dc2020-08-11 14:47:50 +08006883 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006884
6885 NotifyMotionArgs motionArgs;
6886
6887 // Two fingers down at once.
6888 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
6889 processPosition(mapper, x1, y1);
6890 processMTSync(mapper);
6891 processPosition(mapper, x2, y2);
6892 processMTSync(mapper);
6893 processSync(mapper);
6894
6895 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6896 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6897 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6898 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6899 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6900 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6901 ASSERT_EQ(0, motionArgs.flags);
6902 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6903 ASSERT_EQ(0, motionArgs.buttonState);
6904 ASSERT_EQ(0, motionArgs.edgeFlags);
6905 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6906 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6907 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6908 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6909 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6910 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6911 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6912 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6913
6914 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6915 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6916 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6917 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6918 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08006919 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006920 ASSERT_EQ(0, motionArgs.flags);
6921 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6922 ASSERT_EQ(0, motionArgs.buttonState);
6923 ASSERT_EQ(0, motionArgs.edgeFlags);
6924 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6925 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6926 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6927 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6928 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6929 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6930 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6931 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6932 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6933 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6934 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6935 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6936
6937 // Move.
6938 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
6939 processPosition(mapper, x1, y1);
6940 processMTSync(mapper);
6941 processPosition(mapper, x2, y2);
6942 processMTSync(mapper);
6943 processSync(mapper);
6944
6945 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6946 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6947 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6948 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6949 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6950 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6951 ASSERT_EQ(0, motionArgs.flags);
6952 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6953 ASSERT_EQ(0, motionArgs.buttonState);
6954 ASSERT_EQ(0, motionArgs.edgeFlags);
6955 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6956 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6957 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6958 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6959 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6960 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6961 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6962 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6963 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6964 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6965 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6966 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6967
6968 // First finger up.
6969 x2 += 15; y2 -= 20;
6970 processPosition(mapper, x2, y2);
6971 processMTSync(mapper);
6972 processSync(mapper);
6973
6974 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6975 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6976 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6977 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6978 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08006979 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006980 ASSERT_EQ(0, motionArgs.flags);
6981 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6982 ASSERT_EQ(0, motionArgs.buttonState);
6983 ASSERT_EQ(0, motionArgs.edgeFlags);
6984 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6985 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6986 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6987 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6988 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6989 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6990 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6991 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6992 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6993 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6994 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6995 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6996
6997 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6998 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6999 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7000 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7001 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7002 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7003 ASSERT_EQ(0, motionArgs.flags);
7004 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7005 ASSERT_EQ(0, motionArgs.buttonState);
7006 ASSERT_EQ(0, motionArgs.edgeFlags);
7007 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7008 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7009 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7010 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7011 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7012 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7013 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7014 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7015
7016 // Move.
7017 x2 += 20; y2 -= 25;
7018 processPosition(mapper, x2, y2);
7019 processMTSync(mapper);
7020 processSync(mapper);
7021
7022 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7023 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7024 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7025 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7026 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7027 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7028 ASSERT_EQ(0, motionArgs.flags);
7029 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7030 ASSERT_EQ(0, motionArgs.buttonState);
7031 ASSERT_EQ(0, motionArgs.edgeFlags);
7032 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7033 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7034 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7035 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7036 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7037 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7038 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7039 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7040
7041 // New finger down.
7042 int32_t x3 = 700, y3 = 300;
7043 processPosition(mapper, x2, y2);
7044 processMTSync(mapper);
7045 processPosition(mapper, x3, y3);
7046 processMTSync(mapper);
7047 processSync(mapper);
7048
7049 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7050 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7051 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7052 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7053 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007054 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007055 ASSERT_EQ(0, motionArgs.flags);
7056 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7057 ASSERT_EQ(0, motionArgs.buttonState);
7058 ASSERT_EQ(0, motionArgs.edgeFlags);
7059 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7060 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7061 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7062 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7063 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7064 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7065 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7066 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7067 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7068 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7069 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7070 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7071
7072 // Second finger up.
7073 x3 += 30; y3 -= 20;
7074 processPosition(mapper, x3, y3);
7075 processMTSync(mapper);
7076 processSync(mapper);
7077
7078 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7079 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7080 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7081 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7082 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007083 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007084 ASSERT_EQ(0, motionArgs.flags);
7085 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7086 ASSERT_EQ(0, motionArgs.buttonState);
7087 ASSERT_EQ(0, motionArgs.edgeFlags);
7088 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7089 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7090 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7091 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7092 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7093 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7094 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7095 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7096 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7097 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7098 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7099 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7100
7101 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7102 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7103 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7104 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7105 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7106 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7107 ASSERT_EQ(0, motionArgs.flags);
7108 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7109 ASSERT_EQ(0, motionArgs.buttonState);
7110 ASSERT_EQ(0, motionArgs.edgeFlags);
7111 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7112 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7113 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7114 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7115 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7116 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7117 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7118 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7119
7120 // Last finger up.
7121 processMTSync(mapper);
7122 processSync(mapper);
7123
7124 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7125 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7126 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7127 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7128 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7129 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7130 ASSERT_EQ(0, motionArgs.flags);
7131 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7132 ASSERT_EQ(0, motionArgs.buttonState);
7133 ASSERT_EQ(0, motionArgs.edgeFlags);
7134 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7135 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7136 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7137 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7138 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7139 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7140 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7141 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7142
7143 // Should not have sent any more keys or motions.
7144 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7145 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7146}
7147
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007148TEST_F(MultiTouchInputMapperTest, AxisResolution_IsPopulated) {
7149 addConfigurationProperty("touch.deviceType", "touchScreen");
7150 prepareDisplay(DISPLAY_ORIENTATION_0);
7151
7152 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
7153 /*fuzz*/ 0, /*resolution*/ 10);
7154 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
7155 /*fuzz*/ 0, /*resolution*/ 11);
7156 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
7157 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 12);
7158 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
7159 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 13);
7160 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7161 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 14);
7162 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7163 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 15);
7164
7165 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7166
7167 // X and Y axes
7168 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_X, 10 / X_PRECISION);
7169 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_Y, 11 / Y_PRECISION);
7170 // Touch major and minor
7171 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR, 12 * GEOMETRIC_SCALE);
7172 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR, 13 * GEOMETRIC_SCALE);
7173 // Tool major and minor
7174 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR, 14 * GEOMETRIC_SCALE);
7175 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR, 15 * GEOMETRIC_SCALE);
7176}
7177
7178TEST_F(MultiTouchInputMapperTest, TouchMajorAndMinorAxes_DoNotAppearIfNotSupported) {
7179 addConfigurationProperty("touch.deviceType", "touchScreen");
7180 prepareDisplay(DISPLAY_ORIENTATION_0);
7181
7182 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
7183 /*fuzz*/ 0, /*resolution*/ 10);
7184 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
7185 /*fuzz*/ 0, /*resolution*/ 11);
7186
7187 // We do not add ABS_MT_TOUCH_MAJOR / MINOR or ABS_MT_WIDTH_MAJOR / MINOR axes
7188
7189 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7190
7191 // Touch major and minor
7192 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR);
7193 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR);
7194 // Tool major and minor
7195 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR);
7196 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR);
7197}
7198
Michael Wrightd02c5b62014-02-10 15:10:22 -08007199TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007200 addConfigurationProperty("touch.deviceType", "touchScreen");
7201 prepareDisplay(DISPLAY_ORIENTATION_0);
7202 prepareAxes(POSITION | ID);
7203 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007204 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007205
arthurhungdcef2dc2020-08-11 14:47:50 +08007206 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007207
7208 NotifyMotionArgs motionArgs;
7209
7210 // Two fingers down at once.
7211 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7212 processPosition(mapper, x1, y1);
7213 processId(mapper, 1);
7214 processMTSync(mapper);
7215 processPosition(mapper, x2, y2);
7216 processId(mapper, 2);
7217 processMTSync(mapper);
7218 processSync(mapper);
7219
7220 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7221 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7222 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7223 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7224 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7225 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7226 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7227
7228 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007229 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007230 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7231 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7232 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7233 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7234 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7235 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7236 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7237 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7238 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7239
7240 // Move.
7241 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7242 processPosition(mapper, x1, y1);
7243 processId(mapper, 1);
7244 processMTSync(mapper);
7245 processPosition(mapper, x2, y2);
7246 processId(mapper, 2);
7247 processMTSync(mapper);
7248 processSync(mapper);
7249
7250 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7251 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7252 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7253 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7254 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7255 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7256 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7257 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7258 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7259 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7260 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7261
7262 // First finger up.
7263 x2 += 15; y2 -= 20;
7264 processPosition(mapper, x2, y2);
7265 processId(mapper, 2);
7266 processMTSync(mapper);
7267 processSync(mapper);
7268
7269 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007270 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007271 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7272 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7273 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7274 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7275 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7276 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7277 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7278 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7279 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7280
7281 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7282 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7283 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7284 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7285 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7286 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7287 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7288
7289 // Move.
7290 x2 += 20; y2 -= 25;
7291 processPosition(mapper, x2, y2);
7292 processId(mapper, 2);
7293 processMTSync(mapper);
7294 processSync(mapper);
7295
7296 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7297 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7298 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7299 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7300 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7301 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7302 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7303
7304 // New finger down.
7305 int32_t x3 = 700, y3 = 300;
7306 processPosition(mapper, x2, y2);
7307 processId(mapper, 2);
7308 processMTSync(mapper);
7309 processPosition(mapper, x3, y3);
7310 processId(mapper, 3);
7311 processMTSync(mapper);
7312 processSync(mapper);
7313
7314 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007315 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007316 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7317 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7318 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7319 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7320 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7321 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7322 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7323 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7324 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7325
7326 // Second finger up.
7327 x3 += 30; y3 -= 20;
7328 processPosition(mapper, x3, y3);
7329 processId(mapper, 3);
7330 processMTSync(mapper);
7331 processSync(mapper);
7332
7333 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007334 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007335 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7336 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7337 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7338 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7339 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7340 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7341 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7342 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7343 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7344
7345 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7346 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7347 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7348 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7349 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7350 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7351 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7352
7353 // Last finger up.
7354 processMTSync(mapper);
7355 processSync(mapper);
7356
7357 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7358 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7359 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7360 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7361 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7362 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7363 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7364
7365 // Should not have sent any more keys or motions.
7366 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7367 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7368}
7369
7370TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007371 addConfigurationProperty("touch.deviceType", "touchScreen");
7372 prepareDisplay(DISPLAY_ORIENTATION_0);
7373 prepareAxes(POSITION | ID | SLOT);
7374 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007375 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007376
arthurhungdcef2dc2020-08-11 14:47:50 +08007377 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007378
7379 NotifyMotionArgs motionArgs;
7380
7381 // Two fingers down at once.
7382 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7383 processPosition(mapper, x1, y1);
7384 processId(mapper, 1);
7385 processSlot(mapper, 1);
7386 processPosition(mapper, x2, y2);
7387 processId(mapper, 2);
7388 processSync(mapper);
7389
7390 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7391 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7392 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7393 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7394 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7395 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7396 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7397
7398 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007399 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007400 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7401 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7402 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7403 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7404 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7405 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7406 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7407 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7408 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7409
7410 // Move.
7411 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7412 processSlot(mapper, 0);
7413 processPosition(mapper, x1, y1);
7414 processSlot(mapper, 1);
7415 processPosition(mapper, x2, y2);
7416 processSync(mapper);
7417
7418 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7419 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7420 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7421 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7422 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7423 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7424 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7425 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7426 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7427 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7428 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7429
7430 // First finger up.
7431 x2 += 15; y2 -= 20;
7432 processSlot(mapper, 0);
7433 processId(mapper, -1);
7434 processSlot(mapper, 1);
7435 processPosition(mapper, x2, y2);
7436 processSync(mapper);
7437
7438 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007439 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007440 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7441 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7442 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7443 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7444 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7445 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7446 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7447 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7448 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7449
7450 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7451 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7452 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7453 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7454 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7455 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7456 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7457
7458 // Move.
7459 x2 += 20; y2 -= 25;
7460 processPosition(mapper, x2, y2);
7461 processSync(mapper);
7462
7463 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7464 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7465 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7466 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7467 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7468 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7469 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7470
7471 // New finger down.
7472 int32_t x3 = 700, y3 = 300;
7473 processPosition(mapper, x2, y2);
7474 processSlot(mapper, 0);
7475 processId(mapper, 3);
7476 processPosition(mapper, x3, y3);
7477 processSync(mapper);
7478
7479 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007480 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007481 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7482 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7483 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7484 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7485 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7486 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7487 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7488 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7489 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7490
7491 // Second finger up.
7492 x3 += 30; y3 -= 20;
7493 processSlot(mapper, 1);
7494 processId(mapper, -1);
7495 processSlot(mapper, 0);
7496 processPosition(mapper, x3, y3);
7497 processSync(mapper);
7498
7499 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007500 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007501 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7502 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7503 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7504 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7505 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7506 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7507 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7508 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7509 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7510
7511 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7512 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7513 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7514 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7515 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7516 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7517 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7518
7519 // Last finger up.
7520 processId(mapper, -1);
7521 processSync(mapper);
7522
7523 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7524 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7525 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7526 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7527 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7528 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7529 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7530
7531 // Should not have sent any more keys or motions.
7532 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7533 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7534}
7535
7536TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007537 addConfigurationProperty("touch.deviceType", "touchScreen");
7538 prepareDisplay(DISPLAY_ORIENTATION_0);
7539 prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007540 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007541
7542 // These calculations are based on the input device calibration documentation.
7543 int32_t rawX = 100;
7544 int32_t rawY = 200;
7545 int32_t rawTouchMajor = 7;
7546 int32_t rawTouchMinor = 6;
7547 int32_t rawToolMajor = 9;
7548 int32_t rawToolMinor = 8;
7549 int32_t rawPressure = 11;
7550 int32_t rawDistance = 0;
7551 int32_t rawOrientation = 3;
7552 int32_t id = 5;
7553
7554 float x = toDisplayX(rawX);
7555 float y = toDisplayY(rawY);
7556 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
7557 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
7558 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
7559 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
7560 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
7561 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
7562 float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
7563 float distance = float(rawDistance);
7564
7565 processPosition(mapper, rawX, rawY);
7566 processTouchMajor(mapper, rawTouchMajor);
7567 processTouchMinor(mapper, rawTouchMinor);
7568 processToolMajor(mapper, rawToolMajor);
7569 processToolMinor(mapper, rawToolMinor);
7570 processPressure(mapper, rawPressure);
7571 processOrientation(mapper, rawOrientation);
7572 processDistance(mapper, rawDistance);
7573 processId(mapper, id);
7574 processMTSync(mapper);
7575 processSync(mapper);
7576
7577 NotifyMotionArgs args;
7578 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7579 ASSERT_EQ(0, args.pointerProperties[0].id);
7580 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7581 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
7582 orientation, distance));
7583}
7584
7585TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007586 addConfigurationProperty("touch.deviceType", "touchScreen");
7587 prepareDisplay(DISPLAY_ORIENTATION_0);
7588 prepareAxes(POSITION | TOUCH | TOOL | MINOR);
7589 addConfigurationProperty("touch.size.calibration", "geometric");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007590 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007591
7592 // These calculations are based on the input device calibration documentation.
7593 int32_t rawX = 100;
7594 int32_t rawY = 200;
7595 int32_t rawTouchMajor = 140;
7596 int32_t rawTouchMinor = 120;
7597 int32_t rawToolMajor = 180;
7598 int32_t rawToolMinor = 160;
7599
7600 float x = toDisplayX(rawX);
7601 float y = toDisplayY(rawY);
7602 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
7603 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
7604 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
7605 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
7606 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
7607
7608 processPosition(mapper, rawX, rawY);
7609 processTouchMajor(mapper, rawTouchMajor);
7610 processTouchMinor(mapper, rawTouchMinor);
7611 processToolMajor(mapper, rawToolMajor);
7612 processToolMinor(mapper, rawToolMinor);
7613 processMTSync(mapper);
7614 processSync(mapper);
7615
7616 NotifyMotionArgs args;
7617 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7618 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7619 x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
7620}
7621
7622TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007623 addConfigurationProperty("touch.deviceType", "touchScreen");
7624 prepareDisplay(DISPLAY_ORIENTATION_0);
7625 prepareAxes(POSITION | TOUCH | TOOL);
7626 addConfigurationProperty("touch.size.calibration", "diameter");
7627 addConfigurationProperty("touch.size.scale", "10");
7628 addConfigurationProperty("touch.size.bias", "160");
7629 addConfigurationProperty("touch.size.isSummed", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007630 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007631
7632 // These calculations are based on the input device calibration documentation.
7633 // Note: We only provide a single common touch/tool value because the device is assumed
7634 // not to emit separate values for each pointer (isSummed = 1).
7635 int32_t rawX = 100;
7636 int32_t rawY = 200;
7637 int32_t rawX2 = 150;
7638 int32_t rawY2 = 250;
7639 int32_t rawTouchMajor = 5;
7640 int32_t rawToolMajor = 8;
7641
7642 float x = toDisplayX(rawX);
7643 float y = toDisplayY(rawY);
7644 float x2 = toDisplayX(rawX2);
7645 float y2 = toDisplayY(rawY2);
7646 float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
7647 float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
7648 float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
7649
7650 processPosition(mapper, rawX, rawY);
7651 processTouchMajor(mapper, rawTouchMajor);
7652 processToolMajor(mapper, rawToolMajor);
7653 processMTSync(mapper);
7654 processPosition(mapper, rawX2, rawY2);
7655 processTouchMajor(mapper, rawTouchMajor);
7656 processToolMajor(mapper, rawToolMajor);
7657 processMTSync(mapper);
7658 processSync(mapper);
7659
7660 NotifyMotionArgs args;
7661 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7662 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
7663
7664 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007665 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007666 ASSERT_EQ(size_t(2), args.pointerCount);
7667 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7668 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
7669 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
7670 x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
7671}
7672
7673TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007674 addConfigurationProperty("touch.deviceType", "touchScreen");
7675 prepareDisplay(DISPLAY_ORIENTATION_0);
7676 prepareAxes(POSITION | TOUCH | TOOL);
7677 addConfigurationProperty("touch.size.calibration", "area");
7678 addConfigurationProperty("touch.size.scale", "43");
7679 addConfigurationProperty("touch.size.bias", "3");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007680 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007681
7682 // These calculations are based on the input device calibration documentation.
7683 int32_t rawX = 100;
7684 int32_t rawY = 200;
7685 int32_t rawTouchMajor = 5;
7686 int32_t rawToolMajor = 8;
7687
7688 float x = toDisplayX(rawX);
7689 float y = toDisplayY(rawY);
7690 float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
7691 float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
7692 float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
7693
7694 processPosition(mapper, rawX, rawY);
7695 processTouchMajor(mapper, rawTouchMajor);
7696 processToolMajor(mapper, rawToolMajor);
7697 processMTSync(mapper);
7698 processSync(mapper);
7699
7700 NotifyMotionArgs args;
7701 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7702 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7703 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
7704}
7705
7706TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007707 addConfigurationProperty("touch.deviceType", "touchScreen");
7708 prepareDisplay(DISPLAY_ORIENTATION_0);
7709 prepareAxes(POSITION | PRESSURE);
7710 addConfigurationProperty("touch.pressure.calibration", "amplitude");
7711 addConfigurationProperty("touch.pressure.scale", "0.01");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007712 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007713
Michael Wrightaa449c92017-12-13 21:21:43 +00007714 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007715 mapper.populateDeviceInfo(&info);
Michael Wrightaa449c92017-12-13 21:21:43 +00007716 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
7717 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TOUCHSCREEN,
7718 0.0f, RAW_PRESSURE_MAX * 0.01, 0.0f, 0.0f));
7719
Michael Wrightd02c5b62014-02-10 15:10:22 -08007720 // These calculations are based on the input device calibration documentation.
7721 int32_t rawX = 100;
7722 int32_t rawY = 200;
7723 int32_t rawPressure = 60;
7724
7725 float x = toDisplayX(rawX);
7726 float y = toDisplayY(rawY);
7727 float pressure = float(rawPressure) * 0.01f;
7728
7729 processPosition(mapper, rawX, rawY);
7730 processPressure(mapper, rawPressure);
7731 processMTSync(mapper);
7732 processSync(mapper);
7733
7734 NotifyMotionArgs args;
7735 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7736 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7737 x, y, pressure, 0, 0, 0, 0, 0, 0, 0));
7738}
7739
7740TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007741 addConfigurationProperty("touch.deviceType", "touchScreen");
7742 prepareDisplay(DISPLAY_ORIENTATION_0);
7743 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007744 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007745
7746 NotifyMotionArgs motionArgs;
7747 NotifyKeyArgs keyArgs;
7748
7749 processId(mapper, 1);
7750 processPosition(mapper, 100, 200);
7751 processSync(mapper);
7752 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7753 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7754 ASSERT_EQ(0, motionArgs.buttonState);
7755
7756 // press BTN_LEFT, release BTN_LEFT
7757 processKey(mapper, BTN_LEFT, 1);
7758 processSync(mapper);
7759 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7760 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7761 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
7762
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007763 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7764 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7765 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
7766
Michael Wrightd02c5b62014-02-10 15:10:22 -08007767 processKey(mapper, BTN_LEFT, 0);
7768 processSync(mapper);
7769 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007770 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007771 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007772
7773 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007774 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007775 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007776
7777 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
7778 processKey(mapper, BTN_RIGHT, 1);
7779 processKey(mapper, BTN_MIDDLE, 1);
7780 processSync(mapper);
7781 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7782 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7783 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
7784 motionArgs.buttonState);
7785
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007786 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7787 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7788 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
7789
7790 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7791 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7792 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
7793 motionArgs.buttonState);
7794
Michael Wrightd02c5b62014-02-10 15:10:22 -08007795 processKey(mapper, BTN_RIGHT, 0);
7796 processSync(mapper);
7797 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007798 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007799 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007800
7801 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007802 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007803 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007804
7805 processKey(mapper, BTN_MIDDLE, 0);
7806 processSync(mapper);
7807 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007808 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007809 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007810
7811 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007812 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007813 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007814
7815 // press BTN_BACK, release BTN_BACK
7816 processKey(mapper, BTN_BACK, 1);
7817 processSync(mapper);
7818 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7819 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
7820 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007821
Michael Wrightd02c5b62014-02-10 15:10:22 -08007822 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007823 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007824 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
7825
7826 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7827 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7828 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007829
7830 processKey(mapper, BTN_BACK, 0);
7831 processSync(mapper);
7832 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007833 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007834 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007835
7836 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007837 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007838 ASSERT_EQ(0, motionArgs.buttonState);
7839
Michael Wrightd02c5b62014-02-10 15:10:22 -08007840 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7841 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
7842 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
7843
7844 // press BTN_SIDE, release BTN_SIDE
7845 processKey(mapper, BTN_SIDE, 1);
7846 processSync(mapper);
7847 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7848 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
7849 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007850
Michael Wrightd02c5b62014-02-10 15:10:22 -08007851 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007852 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007853 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
7854
7855 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7856 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7857 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007858
7859 processKey(mapper, BTN_SIDE, 0);
7860 processSync(mapper);
7861 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007862 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007863 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007864
7865 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007866 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007867 ASSERT_EQ(0, motionArgs.buttonState);
7868
Michael Wrightd02c5b62014-02-10 15:10:22 -08007869 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7870 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
7871 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
7872
7873 // press BTN_FORWARD, release BTN_FORWARD
7874 processKey(mapper, BTN_FORWARD, 1);
7875 processSync(mapper);
7876 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7877 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
7878 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007879
Michael Wrightd02c5b62014-02-10 15:10:22 -08007880 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007881 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007882 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
7883
7884 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7885 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7886 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007887
7888 processKey(mapper, BTN_FORWARD, 0);
7889 processSync(mapper);
7890 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007891 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007892 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007893
7894 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007895 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007896 ASSERT_EQ(0, motionArgs.buttonState);
7897
Michael Wrightd02c5b62014-02-10 15:10:22 -08007898 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7899 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
7900 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
7901
7902 // press BTN_EXTRA, release BTN_EXTRA
7903 processKey(mapper, BTN_EXTRA, 1);
7904 processSync(mapper);
7905 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7906 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
7907 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007908
Michael Wrightd02c5b62014-02-10 15:10:22 -08007909 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007910 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007911 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
7912
7913 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7914 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7915 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007916
7917 processKey(mapper, BTN_EXTRA, 0);
7918 processSync(mapper);
7919 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007920 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007921 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007922
7923 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007924 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007925 ASSERT_EQ(0, motionArgs.buttonState);
7926
Michael Wrightd02c5b62014-02-10 15:10:22 -08007927 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7928 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
7929 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
7930
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007931 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7932
Michael Wrightd02c5b62014-02-10 15:10:22 -08007933 // press BTN_STYLUS, release BTN_STYLUS
7934 processKey(mapper, BTN_STYLUS, 1);
7935 processSync(mapper);
7936 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7937 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007938 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
7939
7940 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7941 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7942 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007943
7944 processKey(mapper, BTN_STYLUS, 0);
7945 processSync(mapper);
7946 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007947 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007948 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007949
7950 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007951 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007952 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007953
7954 // press BTN_STYLUS2, release BTN_STYLUS2
7955 processKey(mapper, BTN_STYLUS2, 1);
7956 processSync(mapper);
7957 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7958 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007959 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
7960
7961 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7962 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7963 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007964
7965 processKey(mapper, BTN_STYLUS2, 0);
7966 processSync(mapper);
7967 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007968 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007969 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007970
7971 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007972 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007973 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007974
7975 // release touch
7976 processId(mapper, -1);
7977 processSync(mapper);
7978 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7979 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7980 ASSERT_EQ(0, motionArgs.buttonState);
7981}
7982
7983TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007984 addConfigurationProperty("touch.deviceType", "touchScreen");
7985 prepareDisplay(DISPLAY_ORIENTATION_0);
7986 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007987 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007988
7989 NotifyMotionArgs motionArgs;
7990
7991 // default tool type is finger
7992 processId(mapper, 1);
7993 processPosition(mapper, 100, 200);
7994 processSync(mapper);
7995 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7996 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7997 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7998
7999 // eraser
8000 processKey(mapper, BTN_TOOL_RUBBER, 1);
8001 processSync(mapper);
8002 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8003 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8004 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
8005
8006 // stylus
8007 processKey(mapper, BTN_TOOL_RUBBER, 0);
8008 processKey(mapper, BTN_TOOL_PEN, 1);
8009 processSync(mapper);
8010 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8011 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8012 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8013
8014 // brush
8015 processKey(mapper, BTN_TOOL_PEN, 0);
8016 processKey(mapper, BTN_TOOL_BRUSH, 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_STYLUS, motionArgs.pointerProperties[0].toolType);
8021
8022 // pencil
8023 processKey(mapper, BTN_TOOL_BRUSH, 0);
8024 processKey(mapper, BTN_TOOL_PENCIL, 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
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08008030 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08008031 processKey(mapper, BTN_TOOL_PENCIL, 0);
8032 processKey(mapper, BTN_TOOL_AIRBRUSH, 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 // mouse
8039 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
8040 processKey(mapper, BTN_TOOL_MOUSE, 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_MOUSE, motionArgs.pointerProperties[0].toolType);
8045
8046 // lens
8047 processKey(mapper, BTN_TOOL_MOUSE, 0);
8048 processKey(mapper, BTN_TOOL_LENS, 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_MOUSE, motionArgs.pointerProperties[0].toolType);
8053
8054 // double-tap
8055 processKey(mapper, BTN_TOOL_LENS, 0);
8056 processKey(mapper, BTN_TOOL_DOUBLETAP, 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_FINGER, motionArgs.pointerProperties[0].toolType);
8061
8062 // triple-tap
8063 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
8064 processKey(mapper, BTN_TOOL_TRIPLETAP, 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_FINGER, motionArgs.pointerProperties[0].toolType);
8069
8070 // quad-tap
8071 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
8072 processKey(mapper, BTN_TOOL_QUADTAP, 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 // finger
8079 processKey(mapper, BTN_TOOL_QUADTAP, 0);
8080 processKey(mapper, BTN_TOOL_FINGER, 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 // stylus trumps finger
8087 processKey(mapper, BTN_TOOL_PEN, 1);
8088 processSync(mapper);
8089 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8090 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8091 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8092
8093 // eraser trumps stylus
8094 processKey(mapper, BTN_TOOL_RUBBER, 1);
8095 processSync(mapper);
8096 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8097 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8098 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
8099
8100 // mouse trumps eraser
8101 processKey(mapper, BTN_TOOL_MOUSE, 1);
8102 processSync(mapper);
8103 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8104 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8105 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
8106
8107 // MT tool type trumps BTN tool types: MT_TOOL_FINGER
8108 processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
8109 processSync(mapper);
8110 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8111 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8112 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8113
8114 // MT tool type trumps BTN tool types: MT_TOOL_PEN
8115 processToolType(mapper, MT_TOOL_PEN);
8116 processSync(mapper);
8117 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8118 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8119 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8120
8121 // back to default tool type
8122 processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
8123 processKey(mapper, BTN_TOOL_MOUSE, 0);
8124 processKey(mapper, BTN_TOOL_RUBBER, 0);
8125 processKey(mapper, BTN_TOOL_PEN, 0);
8126 processKey(mapper, BTN_TOOL_FINGER, 0);
8127 processSync(mapper);
8128 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8129 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8130 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8131}
8132
8133TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008134 addConfigurationProperty("touch.deviceType", "touchScreen");
8135 prepareDisplay(DISPLAY_ORIENTATION_0);
8136 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008137 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008138 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008139
8140 NotifyMotionArgs motionArgs;
8141
8142 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
8143 processId(mapper, 1);
8144 processPosition(mapper, 100, 200);
8145 processSync(mapper);
8146 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8147 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8148 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8149 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8150
8151 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8152 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8153 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8154 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8155
8156 // move a little
8157 processPosition(mapper, 150, 250);
8158 processSync(mapper);
8159 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8160 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8161 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8162 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8163
8164 // down when BTN_TOUCH is pressed, pressure defaults to 1
8165 processKey(mapper, BTN_TOUCH, 1);
8166 processSync(mapper);
8167 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8168 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8169 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8170 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8171
8172 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8173 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8174 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8175 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8176
8177 // up when BTN_TOUCH is released, hover restored
8178 processKey(mapper, BTN_TOUCH, 0);
8179 processSync(mapper);
8180 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8181 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8182 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8183 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8184
8185 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8186 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8187 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8188 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8189
8190 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8191 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8192 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8193 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8194
8195 // exit hover when pointer goes away
8196 processId(mapper, -1);
8197 processSync(mapper);
8198 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8199 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8200 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8201 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8202}
8203
8204TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008205 addConfigurationProperty("touch.deviceType", "touchScreen");
8206 prepareDisplay(DISPLAY_ORIENTATION_0);
8207 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008208 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008209
8210 NotifyMotionArgs motionArgs;
8211
8212 // initially hovering because pressure is 0
8213 processId(mapper, 1);
8214 processPosition(mapper, 100, 200);
8215 processPressure(mapper, 0);
8216 processSync(mapper);
8217 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8218 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8219 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8220 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8221
8222 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8223 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8224 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8225 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8226
8227 // move a little
8228 processPosition(mapper, 150, 250);
8229 processSync(mapper);
8230 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8231 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8232 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8233 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8234
8235 // down when pressure becomes non-zero
8236 processPressure(mapper, RAW_PRESSURE_MAX);
8237 processSync(mapper);
8238 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8239 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8240 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8241 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8242
8243 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8244 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8245 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8246 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8247
8248 // up when pressure becomes 0, hover restored
8249 processPressure(mapper, 0);
8250 processSync(mapper);
8251 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8252 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8253 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8254 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8255
8256 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8257 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8258 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8259 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8260
8261 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8262 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8263 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8264 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8265
8266 // exit hover when pointer goes away
8267 processId(mapper, -1);
8268 processSync(mapper);
8269 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8270 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8271 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8272 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8273}
8274
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008275/**
8276 * Set the input device port <--> display port associations, and check that the
8277 * events are routed to the display that matches the display port.
8278 * This can be checked by looking at the displayId of the resulting NotifyMotionArgs.
8279 */
8280TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayPort) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008281 const std::string usb2 = "USB2";
8282 const uint8_t hdmi1 = 0;
8283 const uint8_t hdmi2 = 1;
8284 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008285 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008286
8287 addConfigurationProperty("touch.deviceType", "touchScreen");
8288 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008289 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008290
8291 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
8292 mFakePolicy->addInputPortAssociation(usb2, hdmi2);
8293
8294 // We are intentionally not adding the viewport for display 1 yet. Since the port association
8295 // for this input device is specified, and the matching viewport is not present,
8296 // the input device should be disabled (at the mapper level).
8297
8298 // Add viewport for display 2 on hdmi2
8299 prepareSecondaryDisplay(type, hdmi2);
8300 // Send a touch event
8301 processPosition(mapper, 100, 100);
8302 processSync(mapper);
8303 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8304
8305 // Add viewport for display 1 on hdmi1
8306 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
8307 // Send a touch event again
8308 processPosition(mapper, 100, 100);
8309 processSync(mapper);
8310
8311 NotifyMotionArgs args;
8312 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8313 ASSERT_EQ(DISPLAY_ID, args.displayId);
8314}
Michael Wrightd02c5b62014-02-10 15:10:22 -08008315
Arthur Hung6d5b4b22022-01-21 07:21:10 +00008316TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayUniqueId) {
8317 addConfigurationProperty("touch.deviceType", "touchScreen");
8318 prepareAxes(POSITION);
8319 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8320
8321 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
8322
8323 prepareDisplay(DISPLAY_ORIENTATION_0);
8324 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
8325
8326 // Send a touch event
8327 processPosition(mapper, 100, 100);
8328 processSync(mapper);
8329
8330 NotifyMotionArgs args;
8331 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8332 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
8333}
8334
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008335TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShouldHandleDisplayId) {
Garfield Tan888a6a42020-01-09 11:39:16 -08008336 // Setup for second display.
Michael Wright17db18e2020-06-26 20:51:44 +01008337 std::shared_ptr<FakePointerController> fakePointerController =
8338 std::make_shared<FakePointerController>();
Garfield Tan888a6a42020-01-09 11:39:16 -08008339 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008340 fakePointerController->setPosition(100, 200);
8341 fakePointerController->setButtonState(0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00008342 mFakePolicy->setPointerController(fakePointerController);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008343
Garfield Tan888a6a42020-01-09 11:39:16 -08008344 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008345 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08008346
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008347 prepareDisplay(DISPLAY_ORIENTATION_0);
8348 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008349 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008350
8351 // Check source is mouse that would obtain the PointerController.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008352 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008353
8354 NotifyMotionArgs motionArgs;
8355 processPosition(mapper, 100, 100);
8356 processSync(mapper);
8357
8358 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8359 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8360 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
8361}
8362
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00008363/**
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008364 * Ensure that the readTime is set to the SYN_REPORT value when processing touch events.
8365 */
8366TEST_F(MultiTouchInputMapperTest, Process_SendsReadTime) {
8367 addConfigurationProperty("touch.deviceType", "touchScreen");
8368 prepareAxes(POSITION);
8369 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8370
8371 prepareDisplay(DISPLAY_ORIENTATION_0);
8372 process(mapper, 10, 11 /*readTime*/, EV_ABS, ABS_MT_TRACKING_ID, 1);
8373 process(mapper, 15, 16 /*readTime*/, EV_ABS, ABS_MT_POSITION_X, 100);
8374 process(mapper, 20, 21 /*readTime*/, EV_ABS, ABS_MT_POSITION_Y, 100);
8375 process(mapper, 25, 26 /*readTime*/, EV_SYN, SYN_REPORT, 0);
8376
8377 NotifyMotionArgs args;
8378 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8379 ASSERT_EQ(26, args.readTime);
8380
8381 process(mapper, 30, 31 /*readTime*/, EV_ABS, ABS_MT_POSITION_X, 110);
8382 process(mapper, 30, 32 /*readTime*/, EV_ABS, ABS_MT_POSITION_Y, 220);
8383 process(mapper, 30, 33 /*readTime*/, EV_SYN, SYN_REPORT, 0);
8384
8385 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8386 ASSERT_EQ(33, args.readTime);
8387}
8388
8389/**
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00008390 * When the viewport is not active (isActive=false), the touch mapper should be disabled and the
8391 * events should not be delivered to the listener.
8392 */
8393TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreDropped) {
8394 addConfigurationProperty("touch.deviceType", "touchScreen");
8395 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
8396 DISPLAY_ORIENTATION_0, false /*isActive*/, UNIQUE_ID, NO_PORT,
8397 ViewportType::INTERNAL);
8398 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8399 prepareAxes(POSITION);
8400 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8401
8402 NotifyMotionArgs motionArgs;
8403 processPosition(mapper, 100, 100);
8404 processSync(mapper);
8405
8406 mFakeListener->assertNotifyMotionWasNotCalled();
8407}
8408
Garfield Tanc734e4f2021-01-15 20:01:39 -08008409TEST_F(MultiTouchInputMapperTest, Process_DeactivateViewport_AbortTouches) {
8410 addConfigurationProperty("touch.deviceType", "touchScreen");
8411 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
8412 DISPLAY_ORIENTATION_0, true /*isActive*/, UNIQUE_ID, NO_PORT,
8413 ViewportType::INTERNAL);
8414 std::optional<DisplayViewport> optionalDisplayViewport =
8415 mFakePolicy->getDisplayViewportByUniqueId(UNIQUE_ID);
8416 ASSERT_TRUE(optionalDisplayViewport.has_value());
8417 DisplayViewport displayViewport = *optionalDisplayViewport;
8418
8419 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8420 prepareAxes(POSITION);
8421 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8422
8423 // Finger down
8424 int32_t x = 100, y = 100;
8425 processPosition(mapper, x, y);
8426 processSync(mapper);
8427
8428 NotifyMotionArgs motionArgs;
8429 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8430 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8431
8432 // Deactivate display viewport
8433 displayViewport.isActive = false;
8434 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
8435 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8436
8437 // Finger move
8438 x += 10, y += 10;
8439 processPosition(mapper, x, y);
8440 processSync(mapper);
8441
8442 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8443 EXPECT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
8444
8445 // Reactivate display viewport
8446 displayViewport.isActive = true;
8447 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
8448 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8449
8450 // Finger move again
8451 x += 10, y += 10;
8452 processPosition(mapper, x, y);
8453 processSync(mapper);
8454
8455 // Gesture is aborted, so events after display is activated won't be dispatched until there is
8456 // no pointer on the touch device.
8457 mFakeListener->assertNotifyMotionWasNotCalled();
8458}
8459
Arthur Hung7c645402019-01-25 17:45:42 +08008460TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShowTouches) {
8461 // Setup the first touch screen device.
Arthur Hung7c645402019-01-25 17:45:42 +08008462 prepareAxes(POSITION | ID | SLOT);
8463 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008464 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung7c645402019-01-25 17:45:42 +08008465
8466 // Create the second touch screen device, and enable multi fingers.
8467 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08008468 const std::string DEVICE_NAME2 = "TOUCHSCREEN2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08008469 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008470 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08008471 std::shared_ptr<InputDevice> device2 =
8472 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07008473 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08008474
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008475 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
8476 0 /*flat*/, 0 /*fuzz*/);
8477 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
8478 0 /*flat*/, 0 /*fuzz*/);
8479 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX,
8480 0 /*flat*/, 0 /*fuzz*/);
8481 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX,
8482 0 /*flat*/, 0 /*fuzz*/);
8483 mFakeEventHub->setAbsoluteAxisValue(SECOND_EVENTHUB_ID, ABS_MT_SLOT, 0 /*value*/);
8484 mFakeEventHub->addConfigurationProperty(SECOND_EVENTHUB_ID, String8("touch.deviceType"),
8485 String8("touchScreen"));
Arthur Hung7c645402019-01-25 17:45:42 +08008486
8487 // Setup the second touch screen device.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008488 MultiTouchInputMapper& mapper2 = device2->addMapper<MultiTouchInputMapper>(SECOND_EVENTHUB_ID);
Arthur Hung7c645402019-01-25 17:45:42 +08008489 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
8490 device2->reset(ARBITRARY_TIME);
8491
8492 // Setup PointerController.
Michael Wright17db18e2020-06-26 20:51:44 +01008493 std::shared_ptr<FakePointerController> fakePointerController =
8494 std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00008495 mFakePolicy->setPointerController(fakePointerController);
Arthur Hung7c645402019-01-25 17:45:42 +08008496
8497 // Setup policy for associated displays and show touches.
8498 const uint8_t hdmi1 = 0;
8499 const uint8_t hdmi2 = 1;
8500 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
8501 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
8502 mFakePolicy->setShowTouches(true);
8503
8504 // Create displays.
8505 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008506 prepareSecondaryDisplay(ViewportType::EXTERNAL, hdmi2);
Arthur Hung7c645402019-01-25 17:45:42 +08008507
8508 // Default device will reconfigure above, need additional reconfiguration for another device.
8509 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008510 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung7c645402019-01-25 17:45:42 +08008511
8512 // Two fingers down at default display.
8513 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
8514 processPosition(mapper, x1, y1);
8515 processId(mapper, 1);
8516 processSlot(mapper, 1);
8517 processPosition(mapper, x2, y2);
8518 processId(mapper, 2);
8519 processSync(mapper);
8520
8521 std::map<int32_t, std::vector<int32_t>>::const_iterator iter =
8522 fakePointerController->getSpots().find(DISPLAY_ID);
8523 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
8524 ASSERT_EQ(size_t(2), iter->second.size());
8525
8526 // Two fingers down at second display.
8527 processPosition(mapper2, x1, y1);
8528 processId(mapper2, 1);
8529 processSlot(mapper2, 1);
8530 processPosition(mapper2, x2, y2);
8531 processId(mapper2, 2);
8532 processSync(mapper2);
8533
8534 iter = fakePointerController->getSpots().find(SECONDARY_DISPLAY_ID);
8535 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
8536 ASSERT_EQ(size_t(2), iter->second.size());
8537}
8538
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008539TEST_F(MultiTouchInputMapperTest, VideoFrames_ReceivedByListener) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008540 prepareAxes(POSITION);
8541 addConfigurationProperty("touch.deviceType", "touchScreen");
8542 prepareDisplay(DISPLAY_ORIENTATION_0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008543 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008544
8545 NotifyMotionArgs motionArgs;
8546 // Unrotated video frame
8547 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8548 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008549 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008550 processPosition(mapper, 100, 200);
8551 processSync(mapper);
8552 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8553 ASSERT_EQ(frames, motionArgs.videoFrames);
8554
8555 // Subsequent touch events should not have any videoframes
8556 // This is implemented separately in FakeEventHub,
8557 // but that should match the behaviour of TouchVideoDevice.
8558 processPosition(mapper, 200, 200);
8559 processSync(mapper);
8560 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8561 ASSERT_EQ(std::vector<TouchVideoFrame>(), motionArgs.videoFrames);
8562}
8563
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008564TEST_F(MultiTouchInputMapperTest, VideoFrames_AreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008565 prepareAxes(POSITION);
8566 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008567 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008568 // Unrotated video frame
8569 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8570 NotifyMotionArgs motionArgs;
8571
8572 // Test all 4 orientations
8573 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008574 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
8575 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
8576 clearViewports();
8577 prepareDisplay(orientation);
8578 std::vector<TouchVideoFrame> frames{frame};
8579 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
8580 processPosition(mapper, 100, 200);
8581 processSync(mapper);
8582 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8583 ASSERT_EQ(frames, motionArgs.videoFrames);
8584 }
8585}
8586
8587TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_AreRotated) {
8588 prepareAxes(POSITION);
8589 addConfigurationProperty("touch.deviceType", "touchScreen");
8590 // Since InputReader works in the un-rotated coordinate space, only devices that are not
8591 // orientation-aware are affected by display rotation.
8592 addConfigurationProperty("touch.orientationAware", "0");
8593 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8594 // Unrotated video frame
8595 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8596 NotifyMotionArgs motionArgs;
8597
8598 // Test all 4 orientations
8599 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008600 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
8601 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
8602 clearViewports();
8603 prepareDisplay(orientation);
8604 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008605 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008606 processPosition(mapper, 100, 200);
8607 processSync(mapper);
8608 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008609 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
8610 // compared to the display. This is so that when the window transform (which contains the
8611 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
8612 // window's coordinate space.
8613 frames[0].rotate(getInverseRotation(orientation));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008614 ASSERT_EQ(frames, motionArgs.videoFrames);
8615 }
8616}
8617
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008618TEST_F(MultiTouchInputMapperTest, VideoFrames_MultipleFramesAreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008619 prepareAxes(POSITION);
8620 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008621 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008622 // Unrotated video frames. There's no rule that they must all have the same dimensions,
8623 // so mix these.
8624 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8625 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
8626 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
8627 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
8628 NotifyMotionArgs motionArgs;
8629
8630 prepareDisplay(DISPLAY_ORIENTATION_90);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008631 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008632 processPosition(mapper, 100, 200);
8633 processSync(mapper);
8634 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008635 ASSERT_EQ(frames, motionArgs.videoFrames);
8636}
8637
8638TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_MultipleFramesAreRotated) {
8639 prepareAxes(POSITION);
8640 addConfigurationProperty("touch.deviceType", "touchScreen");
8641 // Since InputReader works in the un-rotated coordinate space, only devices that are not
8642 // orientation-aware are affected by display rotation.
8643 addConfigurationProperty("touch.orientationAware", "0");
8644 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8645 // Unrotated video frames. There's no rule that they must all have the same dimensions,
8646 // so mix these.
8647 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8648 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
8649 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
8650 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
8651 NotifyMotionArgs motionArgs;
8652
8653 prepareDisplay(DISPLAY_ORIENTATION_90);
8654 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
8655 processPosition(mapper, 100, 200);
8656 processSync(mapper);
8657 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8658 std::for_each(frames.begin(), frames.end(), [](TouchVideoFrame& frame) {
8659 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
8660 // compared to the display. This is so that when the window transform (which contains the
8661 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
8662 // window's coordinate space.
8663 frame.rotate(getInverseRotation(DISPLAY_ORIENTATION_90));
8664 });
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008665 ASSERT_EQ(frames, motionArgs.videoFrames);
8666}
8667
Arthur Hung9da14732019-09-02 16:16:58 +08008668/**
8669 * If we had defined port associations, but the viewport is not ready, the touch device would be
8670 * expected to be disabled, and it should be enabled after the viewport has found.
8671 */
8672TEST_F(MultiTouchInputMapperTest, Configure_EnabledForAssociatedDisplay) {
Arthur Hung9da14732019-09-02 16:16:58 +08008673 constexpr uint8_t hdmi2 = 1;
8674 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008675 constexpr ViewportType type = ViewportType::EXTERNAL;
Arthur Hung9da14732019-09-02 16:16:58 +08008676
8677 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi2);
8678
8679 addConfigurationProperty("touch.deviceType", "touchScreen");
8680 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008681 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung9da14732019-09-02 16:16:58 +08008682
8683 ASSERT_EQ(mDevice->isEnabled(), false);
8684
8685 // Add display on hdmi2, the device should be enabled and can receive touch event.
8686 prepareSecondaryDisplay(type, hdmi2);
8687 ASSERT_EQ(mDevice->isEnabled(), true);
8688
8689 // Send a touch event.
8690 processPosition(mapper, 100, 100);
8691 processSync(mapper);
8692
8693 NotifyMotionArgs args;
8694 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8695 ASSERT_EQ(SECONDARY_DISPLAY_ID, args.displayId);
8696}
8697
Arthur Hung421eb1c2020-01-16 00:09:42 +08008698TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleSingleTouch) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08008699 addConfigurationProperty("touch.deviceType", "touchScreen");
8700 prepareDisplay(DISPLAY_ORIENTATION_0);
8701 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008702 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08008703
8704 NotifyMotionArgs motionArgs;
8705
8706 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
8707 // finger down
8708 processId(mapper, 1);
8709 processPosition(mapper, x1, y1);
8710 processSync(mapper);
8711 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8712 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8713 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8714
8715 // finger move
8716 processId(mapper, 1);
8717 processPosition(mapper, x2, y2);
8718 processSync(mapper);
8719 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8720 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8721 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8722
8723 // finger up.
8724 processId(mapper, -1);
8725 processSync(mapper);
8726 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8727 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8728 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8729
8730 // new finger down
8731 processId(mapper, 1);
8732 processPosition(mapper, x3, y3);
8733 processSync(mapper);
8734 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8735 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8736 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8737}
8738
8739/**
arthurhungcc7f9802020-04-30 17:55:40 +08008740 * Test single touch should be canceled when received the MT_TOOL_PALM event, and the following
8741 * MOVE and UP events should be ignored.
Arthur Hung421eb1c2020-01-16 00:09:42 +08008742 */
arthurhungcc7f9802020-04-30 17:55:40 +08008743TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_SinglePointer) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08008744 addConfigurationProperty("touch.deviceType", "touchScreen");
8745 prepareDisplay(DISPLAY_ORIENTATION_0);
8746 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008747 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08008748
8749 NotifyMotionArgs motionArgs;
8750
8751 // default tool type is finger
8752 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
arthurhungcc7f9802020-04-30 17:55:40 +08008753 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008754 processPosition(mapper, x1, y1);
8755 processSync(mapper);
8756 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8757 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8758 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8759
8760 // Tool changed to MT_TOOL_PALM expect sending the cancel event.
8761 processToolType(mapper, MT_TOOL_PALM);
8762 processSync(mapper);
8763 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8764 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
8765
8766 // Ignore the following MOVE and UP events if had detect a palm event.
arthurhungcc7f9802020-04-30 17:55:40 +08008767 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008768 processPosition(mapper, x2, y2);
8769 processSync(mapper);
8770 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8771
8772 // finger up.
arthurhungcc7f9802020-04-30 17:55:40 +08008773 processId(mapper, INVALID_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008774 processSync(mapper);
8775 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8776
8777 // new finger down
arthurhungcc7f9802020-04-30 17:55:40 +08008778 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008779 processToolType(mapper, MT_TOOL_FINGER);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008780 processPosition(mapper, x3, y3);
8781 processSync(mapper);
8782 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8783 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8784 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8785}
8786
arthurhungbf89a482020-04-17 17:37:55 +08008787/**
arthurhungcc7f9802020-04-30 17:55:40 +08008788 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
8789 * and the rest active fingers could still be allowed to receive the events
arthurhungbf89a482020-04-17 17:37:55 +08008790 */
arthurhungcc7f9802020-04-30 17:55:40 +08008791TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_TwoPointers) {
arthurhungbf89a482020-04-17 17:37:55 +08008792 addConfigurationProperty("touch.deviceType", "touchScreen");
8793 prepareDisplay(DISPLAY_ORIENTATION_0);
8794 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
8795 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8796
8797 NotifyMotionArgs motionArgs;
8798
8799 // default tool type is finger
arthurhungcc7f9802020-04-30 17:55:40 +08008800 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
8801 processId(mapper, FIRST_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08008802 processPosition(mapper, x1, y1);
8803 processSync(mapper);
8804 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8805 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8806 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8807
8808 // Second finger down.
arthurhungcc7f9802020-04-30 17:55:40 +08008809 processSlot(mapper, SECOND_SLOT);
8810 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08008811 processPosition(mapper, x2, y2);
arthurhungcc7f9802020-04-30 17:55:40 +08008812 processSync(mapper);
8813 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008814 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08008815 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8816
8817 // If the tool type of the first finger changes to MT_TOOL_PALM,
8818 // we expect to receive ACTION_POINTER_UP with cancel flag.
8819 processSlot(mapper, FIRST_SLOT);
8820 processId(mapper, FIRST_TRACKING_ID);
8821 processToolType(mapper, MT_TOOL_PALM);
8822 processSync(mapper);
8823 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008824 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08008825 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
8826
8827 // The following MOVE events of second finger should be processed.
8828 processSlot(mapper, SECOND_SLOT);
8829 processId(mapper, SECOND_TRACKING_ID);
8830 processPosition(mapper, x2 + 1, y2 + 1);
8831 processSync(mapper);
8832 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8833 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8834 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8835
8836 // First finger up. It used to be in palm mode, and we already generated ACTION_POINTER_UP for
8837 // it. Second finger receive move.
8838 processSlot(mapper, FIRST_SLOT);
8839 processId(mapper, INVALID_TRACKING_ID);
8840 processSync(mapper);
8841 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8842 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8843 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8844
8845 // Second finger keeps moving.
8846 processSlot(mapper, SECOND_SLOT);
8847 processId(mapper, SECOND_TRACKING_ID);
8848 processPosition(mapper, x2 + 2, y2 + 2);
8849 processSync(mapper);
8850 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8851 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8852 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8853
8854 // Second finger up.
8855 processId(mapper, INVALID_TRACKING_ID);
8856 processSync(mapper);
8857 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8858 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8859 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
8860}
8861
8862/**
8863 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event, if only 1 finger
8864 * is active, it should send CANCEL after receiving the MT_TOOL_PALM event.
8865 */
8866TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_ShouldCancelWhenAllTouchIsPalm) {
8867 addConfigurationProperty("touch.deviceType", "touchScreen");
8868 prepareDisplay(DISPLAY_ORIENTATION_0);
8869 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
8870 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8871
8872 NotifyMotionArgs motionArgs;
8873
8874 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
8875 // First finger down.
8876 processId(mapper, FIRST_TRACKING_ID);
8877 processPosition(mapper, x1, y1);
8878 processSync(mapper);
8879 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8880 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8881 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8882
8883 // Second finger down.
8884 processSlot(mapper, SECOND_SLOT);
8885 processId(mapper, SECOND_TRACKING_ID);
8886 processPosition(mapper, x2, y2);
arthurhungbf89a482020-04-17 17:37:55 +08008887 processSync(mapper);
8888 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008889 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungbf89a482020-04-17 17:37:55 +08008890 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8891
arthurhungcc7f9802020-04-30 17:55:40 +08008892 // If the tool type of the first finger changes to MT_TOOL_PALM,
8893 // we expect to receive ACTION_POINTER_UP with cancel flag.
8894 processSlot(mapper, FIRST_SLOT);
8895 processId(mapper, FIRST_TRACKING_ID);
8896 processToolType(mapper, MT_TOOL_PALM);
8897 processSync(mapper);
8898 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008899 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08008900 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
8901
8902 // Second finger keeps moving.
8903 processSlot(mapper, SECOND_SLOT);
8904 processId(mapper, SECOND_TRACKING_ID);
8905 processPosition(mapper, x2 + 1, y2 + 1);
8906 processSync(mapper);
8907 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8908 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8909
8910 // second finger becomes palm, receive cancel due to only 1 finger is active.
8911 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08008912 processToolType(mapper, MT_TOOL_PALM);
8913 processSync(mapper);
8914 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8915 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
8916
arthurhungcc7f9802020-04-30 17:55:40 +08008917 // third finger down.
8918 processSlot(mapper, THIRD_SLOT);
8919 processId(mapper, THIRD_TRACKING_ID);
8920 processToolType(mapper, MT_TOOL_FINGER);
arthurhungbf89a482020-04-17 17:37:55 +08008921 processPosition(mapper, x3, y3);
8922 processSync(mapper);
arthurhungbf89a482020-04-17 17:37:55 +08008923 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8924 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8925 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08008926 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8927
8928 // third finger move
8929 processId(mapper, THIRD_TRACKING_ID);
8930 processPosition(mapper, x3 + 1, y3 + 1);
8931 processSync(mapper);
8932 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8933 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8934
8935 // first finger up, third finger receive move.
8936 processSlot(mapper, FIRST_SLOT);
8937 processId(mapper, INVALID_TRACKING_ID);
8938 processSync(mapper);
8939 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8940 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8941 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8942
8943 // second finger up, third finger receive move.
8944 processSlot(mapper, SECOND_SLOT);
8945 processId(mapper, INVALID_TRACKING_ID);
8946 processSync(mapper);
8947 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8948 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8949 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8950
8951 // third finger up.
8952 processSlot(mapper, THIRD_SLOT);
8953 processId(mapper, INVALID_TRACKING_ID);
8954 processSync(mapper);
8955 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8956 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8957 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
8958}
8959
8960/**
8961 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
8962 * and the active finger could still be allowed to receive the events
8963 */
8964TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_KeepFirstPointer) {
8965 addConfigurationProperty("touch.deviceType", "touchScreen");
8966 prepareDisplay(DISPLAY_ORIENTATION_0);
8967 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
8968 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8969
8970 NotifyMotionArgs motionArgs;
8971
8972 // default tool type is finger
8973 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
8974 processId(mapper, FIRST_TRACKING_ID);
8975 processPosition(mapper, x1, y1);
8976 processSync(mapper);
8977 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8978 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8979 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8980
8981 // Second finger down.
8982 processSlot(mapper, SECOND_SLOT);
8983 processId(mapper, SECOND_TRACKING_ID);
8984 processPosition(mapper, x2, y2);
8985 processSync(mapper);
8986 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008987 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08008988 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8989
8990 // If the tool type of the second finger changes to MT_TOOL_PALM,
8991 // we expect to receive ACTION_POINTER_UP with cancel flag.
8992 processId(mapper, SECOND_TRACKING_ID);
8993 processToolType(mapper, MT_TOOL_PALM);
8994 processSync(mapper);
8995 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008996 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08008997 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
8998
8999 // The following MOVE event should be processed.
9000 processSlot(mapper, FIRST_SLOT);
9001 processId(mapper, FIRST_TRACKING_ID);
9002 processPosition(mapper, x1 + 1, y1 + 1);
9003 processSync(mapper);
9004 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9005 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9006 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9007
9008 // second finger up.
9009 processSlot(mapper, SECOND_SLOT);
9010 processId(mapper, INVALID_TRACKING_ID);
9011 processSync(mapper);
9012 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9013 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9014
9015 // first finger keep moving
9016 processSlot(mapper, FIRST_SLOT);
9017 processId(mapper, FIRST_TRACKING_ID);
9018 processPosition(mapper, x1 + 2, y1 + 2);
9019 processSync(mapper);
9020 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9021 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9022
9023 // first finger up.
9024 processId(mapper, INVALID_TRACKING_ID);
9025 processSync(mapper);
9026 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9027 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9028 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
arthurhungbf89a482020-04-17 17:37:55 +08009029}
9030
Arthur Hung9ad18942021-06-19 02:04:46 +00009031/**
9032 * Test multi-touch should sent ACTION_POINTER_UP/ACTION_UP when received the INVALID_TRACKING_ID,
9033 * to prevent the driver side may send unexpected data after set tracking id as INVALID_TRACKING_ID
9034 * cause slot be valid again.
9035 */
9036TEST_F(MultiTouchInputMapperTest, Process_MultiTouch_WithInvalidTrackingId) {
9037 addConfigurationProperty("touch.deviceType", "touchScreen");
9038 prepareDisplay(DISPLAY_ORIENTATION_0);
9039 prepareAxes(POSITION | ID | SLOT | PRESSURE);
9040 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9041
9042 NotifyMotionArgs motionArgs;
9043
9044 constexpr int32_t x1 = 100, y1 = 200, x2 = 0, y2 = 0;
9045 // First finger down.
9046 processId(mapper, FIRST_TRACKING_ID);
9047 processPosition(mapper, x1, y1);
9048 processPressure(mapper, RAW_PRESSURE_MAX);
9049 processSync(mapper);
9050 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9051 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9052 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9053
9054 // First finger move.
9055 processId(mapper, FIRST_TRACKING_ID);
9056 processPosition(mapper, x1 + 1, y1 + 1);
9057 processPressure(mapper, RAW_PRESSURE_MAX);
9058 processSync(mapper);
9059 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9060 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9061 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9062
9063 // Second finger down.
9064 processSlot(mapper, SECOND_SLOT);
9065 processId(mapper, SECOND_TRACKING_ID);
9066 processPosition(mapper, x2, y2);
9067 processPressure(mapper, RAW_PRESSURE_MAX);
9068 processSync(mapper);
9069 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009070 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Arthur Hung9ad18942021-06-19 02:04:46 +00009071 ASSERT_EQ(uint32_t(2), motionArgs.pointerCount);
9072
9073 // second finger up with some unexpected data.
9074 processSlot(mapper, SECOND_SLOT);
9075 processId(mapper, INVALID_TRACKING_ID);
9076 processPosition(mapper, x2, y2);
9077 processSync(mapper);
9078 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009079 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Arthur Hung9ad18942021-06-19 02:04:46 +00009080 ASSERT_EQ(uint32_t(2), motionArgs.pointerCount);
9081
9082 // first finger up with some unexpected data.
9083 processSlot(mapper, FIRST_SLOT);
9084 processId(mapper, INVALID_TRACKING_ID);
9085 processPosition(mapper, x2, y2);
9086 processPressure(mapper, RAW_PRESSURE_MAX);
9087 processSync(mapper);
9088 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9089 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9090 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9091}
9092
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009093// --- MultiTouchInputMapperTest_ExternalDevice ---
9094
9095class MultiTouchInputMapperTest_ExternalDevice : public MultiTouchInputMapperTest {
9096protected:
Chris Yea52ade12020-08-27 16:49:20 -07009097 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009098};
9099
9100/**
9101 * Expect fallback to internal viewport if device is external and external viewport is not present.
9102 */
9103TEST_F(MultiTouchInputMapperTest_ExternalDevice, Viewports_Fallback) {
9104 prepareAxes(POSITION);
9105 addConfigurationProperty("touch.deviceType", "touchScreen");
9106 prepareDisplay(DISPLAY_ORIENTATION_0);
9107 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9108
9109 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
9110
9111 NotifyMotionArgs motionArgs;
9112
9113 // Expect the event to be sent to the internal viewport,
9114 // because an external viewport is not present.
9115 processPosition(mapper, 100, 100);
9116 processSync(mapper);
9117 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9118 ASSERT_EQ(ADISPLAY_ID_DEFAULT, motionArgs.displayId);
9119
9120 // Expect the event to be sent to the external viewport if it is present.
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009121 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009122 processPosition(mapper, 100, 100);
9123 processSync(mapper);
9124 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9125 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
9126}
Arthur Hung4197f6b2020-03-16 15:39:59 +08009127
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009128TEST_F(MultiTouchInputMapperTest, Process_TouchpadCapture) {
9129 // we need a pointer controller for mouse mode of touchpad (start pointer at 0,0)
9130 std::shared_ptr<FakePointerController> fakePointerController =
9131 std::make_shared<FakePointerController>();
9132 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
9133 fakePointerController->setPosition(0, 0);
9134 fakePointerController->setButtonState(0);
9135
9136 // prepare device and capture
9137 prepareDisplay(DISPLAY_ORIENTATION_0);
9138 prepareAxes(POSITION | ID | SLOT);
9139 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
9140 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
9141 mFakePolicy->setPointerCapture(true);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009142 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009143 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9144
9145 // captured touchpad should be a touchpad source
9146 NotifyDeviceResetArgs resetArgs;
9147 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
9148 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
9149
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009150 InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
Chris Yef74dc422020-09-02 22:41:50 -07009151
9152 const InputDeviceInfo::MotionRange* relRangeX =
9153 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_X, AINPUT_SOURCE_TOUCHPAD);
9154 ASSERT_NE(relRangeX, nullptr);
9155 ASSERT_EQ(relRangeX->min, -(RAW_X_MAX - RAW_X_MIN));
9156 ASSERT_EQ(relRangeX->max, RAW_X_MAX - RAW_X_MIN);
9157 const InputDeviceInfo::MotionRange* relRangeY =
9158 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_Y, AINPUT_SOURCE_TOUCHPAD);
9159 ASSERT_NE(relRangeY, nullptr);
9160 ASSERT_EQ(relRangeY->min, -(RAW_Y_MAX - RAW_Y_MIN));
9161 ASSERT_EQ(relRangeY->max, RAW_Y_MAX - RAW_Y_MIN);
9162
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009163 // run captured pointer tests - note that this is unscaled, so input listener events should be
9164 // identical to what the hardware sends (accounting for any
9165 // calibration).
9166 // FINGER 0 DOWN
Chris Ye364fdb52020-08-05 15:07:56 -07009167 processSlot(mapper, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009168 processId(mapper, 1);
9169 processPosition(mapper, 100 + RAW_X_MIN, 100 + RAW_Y_MIN);
9170 processKey(mapper, BTN_TOUCH, 1);
9171 processSync(mapper);
9172
9173 // expect coord[0] to contain initial location of touch 0
9174 NotifyMotionArgs args;
9175 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9176 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
9177 ASSERT_EQ(1U, args.pointerCount);
9178 ASSERT_EQ(0, args.pointerProperties[0].id);
9179 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, args.source);
9180 ASSERT_NO_FATAL_FAILURE(
9181 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
9182
9183 // FINGER 1 DOWN
9184 processSlot(mapper, 1);
9185 processId(mapper, 2);
9186 processPosition(mapper, 560 + RAW_X_MIN, 154 + RAW_Y_MIN);
9187 processSync(mapper);
9188
9189 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
9190 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009191 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009192 ASSERT_EQ(2U, args.pointerCount);
9193 ASSERT_EQ(0, args.pointerProperties[0].id);
9194 ASSERT_EQ(1, args.pointerProperties[1].id);
9195 ASSERT_NO_FATAL_FAILURE(
9196 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
9197 ASSERT_NO_FATAL_FAILURE(
9198 assertPointerCoords(args.pointerCoords[1], 560, 154, 1, 0, 0, 0, 0, 0, 0, 0));
9199
9200 // FINGER 1 MOVE
9201 processPosition(mapper, 540 + RAW_X_MIN, 690 + RAW_Y_MIN);
9202 processSync(mapper);
9203
9204 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
9205 // from move
9206 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9207 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9208 ASSERT_NO_FATAL_FAILURE(
9209 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
9210 ASSERT_NO_FATAL_FAILURE(
9211 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
9212
9213 // FINGER 0 MOVE
9214 processSlot(mapper, 0);
9215 processPosition(mapper, 50 + RAW_X_MIN, 800 + RAW_Y_MIN);
9216 processSync(mapper);
9217
9218 // expect coord[0] to contain new touch 0 location, coord[1] to contain previous location
9219 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9220 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9221 ASSERT_NO_FATAL_FAILURE(
9222 assertPointerCoords(args.pointerCoords[0], 50, 800, 1, 0, 0, 0, 0, 0, 0, 0));
9223 ASSERT_NO_FATAL_FAILURE(
9224 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
9225
9226 // BUTTON DOWN
9227 processKey(mapper, BTN_LEFT, 1);
9228 processSync(mapper);
9229
9230 // touchinputmapper design sends a move before button press
9231 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9232 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9233 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9234 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
9235
9236 // BUTTON UP
9237 processKey(mapper, BTN_LEFT, 0);
9238 processSync(mapper);
9239
9240 // touchinputmapper design sends a move after button release
9241 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9242 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
9243 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9244 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9245
9246 // FINGER 0 UP
9247 processId(mapper, -1);
9248 processSync(mapper);
9249 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9250 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | 0x0000, args.action);
9251
9252 // FINGER 1 MOVE
9253 processSlot(mapper, 1);
9254 processPosition(mapper, 320 + RAW_X_MIN, 900 + RAW_Y_MIN);
9255 processSync(mapper);
9256
9257 // expect coord[0] to contain new location of touch 1, and properties[0].id to contain 1
9258 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9259 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9260 ASSERT_EQ(1U, args.pointerCount);
9261 ASSERT_EQ(1, args.pointerProperties[0].id);
9262 ASSERT_NO_FATAL_FAILURE(
9263 assertPointerCoords(args.pointerCoords[0], 320, 900, 1, 0, 0, 0, 0, 0, 0, 0));
9264
9265 // FINGER 1 UP
9266 processId(mapper, -1);
9267 processKey(mapper, BTN_TOUCH, 0);
9268 processSync(mapper);
9269 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9270 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
9271
9272 // non captured touchpad should be a mouse source
9273 mFakePolicy->setPointerCapture(false);
9274 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
9275 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
9276 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
9277}
9278
9279TEST_F(MultiTouchInputMapperTest, Process_UnCapturedTouchpadPointer) {
9280 std::shared_ptr<FakePointerController> fakePointerController =
9281 std::make_shared<FakePointerController>();
9282 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
9283 fakePointerController->setPosition(0, 0);
9284 fakePointerController->setButtonState(0);
9285
9286 // prepare device and capture
9287 prepareDisplay(DISPLAY_ORIENTATION_0);
9288 prepareAxes(POSITION | ID | SLOT);
9289 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
9290 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009291 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009292 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9293 // run uncaptured pointer tests - pushes out generic events
9294 // FINGER 0 DOWN
9295 processId(mapper, 3);
9296 processPosition(mapper, 100, 100);
9297 processKey(mapper, BTN_TOUCH, 1);
9298 processSync(mapper);
9299
9300 // start at (100,100), cursor should be at (0,0) * scale
9301 NotifyMotionArgs args;
9302 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9303 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
9304 ASSERT_NO_FATAL_FAILURE(
9305 assertPointerCoords(args.pointerCoords[0], 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
9306
9307 // FINGER 0 MOVE
9308 processPosition(mapper, 200, 200);
9309 processSync(mapper);
9310
9311 // compute scaling to help with touch position checking
9312 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
9313 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
9314 float scale =
9315 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
9316
9317 // translate from (100,100) -> (200,200), cursor should have changed to (100,100) * scale)
9318 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9319 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
9320 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 100 * scale, 100 * scale, 0,
9321 0, 0, 0, 0, 0, 0, 0));
9322}
9323
9324TEST_F(MultiTouchInputMapperTest, WhenCapturedAndNotCaptured_GetSources) {
9325 std::shared_ptr<FakePointerController> fakePointerController =
9326 std::make_shared<FakePointerController>();
9327
9328 prepareDisplay(DISPLAY_ORIENTATION_0);
9329 prepareAxes(POSITION | ID | SLOT);
9330 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009331 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009332 mFakePolicy->setPointerCapture(false);
9333 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9334
9335 // uncaptured touchpad should be a pointer device
9336 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
9337
9338 // captured touchpad should be a touchpad device
9339 mFakePolicy->setPointerCapture(true);
9340 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
9341 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
9342}
9343
Arthur Hung6d5b4b22022-01-21 07:21:10 +00009344// --- JoystickInputMapperTest ---
9345
9346class JoystickInputMapperTest : public InputMapperTest {
9347protected:
9348 static const int32_t RAW_X_MIN;
9349 static const int32_t RAW_X_MAX;
9350 static const int32_t RAW_Y_MIN;
9351 static const int32_t RAW_Y_MAX;
9352
9353 void SetUp() override {
9354 InputMapperTest::SetUp(InputDeviceClass::JOYSTICK | InputDeviceClass::EXTERNAL);
9355 }
9356 void prepareAxes() {
9357 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
9358 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
9359 }
9360
9361 void processAxis(JoystickInputMapper& mapper, int32_t axis, int32_t value) {
9362 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, axis, value);
9363 }
9364
9365 void processSync(JoystickInputMapper& mapper) {
9366 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
9367 }
9368
9369 void prepareVirtualDisplay(int32_t orientation) {
9370 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH,
9371 VIRTUAL_DISPLAY_HEIGHT, orientation, VIRTUAL_DISPLAY_UNIQUE_ID,
9372 NO_PORT, ViewportType::VIRTUAL);
9373 }
9374};
9375
9376const int32_t JoystickInputMapperTest::RAW_X_MIN = -32767;
9377const int32_t JoystickInputMapperTest::RAW_X_MAX = 32767;
9378const int32_t JoystickInputMapperTest::RAW_Y_MIN = -32767;
9379const int32_t JoystickInputMapperTest::RAW_Y_MAX = 32767;
9380
9381TEST_F(JoystickInputMapperTest, Configure_AssignsDisplayUniqueId) {
9382 prepareAxes();
9383 JoystickInputMapper& mapper = addMapperAndConfigure<JoystickInputMapper>();
9384
9385 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
9386
9387 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
9388
9389 // Send an axis event
9390 processAxis(mapper, ABS_X, 100);
9391 processSync(mapper);
9392
9393 NotifyMotionArgs args;
9394 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9395 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
9396
9397 // Send another axis event
9398 processAxis(mapper, ABS_Y, 100);
9399 processSync(mapper);
9400
9401 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9402 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
9403}
9404
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009405// --- PeripheralControllerTest ---
Chris Yee2b1e5c2021-03-10 22:45:12 -08009406
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009407class PeripheralControllerTest : public testing::Test {
Chris Yee2b1e5c2021-03-10 22:45:12 -08009408protected:
9409 static const char* DEVICE_NAME;
9410 static const char* DEVICE_LOCATION;
9411 static const int32_t DEVICE_ID;
9412 static const int32_t DEVICE_GENERATION;
9413 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07009414 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Chris Yee2b1e5c2021-03-10 22:45:12 -08009415 static const int32_t EVENTHUB_ID;
9416
9417 std::shared_ptr<FakeEventHub> mFakeEventHub;
9418 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07009419 std::unique_ptr<TestInputListener> mFakeListener;
Chris Yee2b1e5c2021-03-10 22:45:12 -08009420 std::unique_ptr<InstrumentedInputReader> mReader;
9421 std::shared_ptr<InputDevice> mDevice;
9422
Dominik Laskowski2f01d772022-03-23 16:01:29 -07009423 virtual void SetUp(ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -08009424 mFakeEventHub = std::make_unique<FakeEventHub>();
9425 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07009426 mFakeListener = std::make_unique<TestInputListener>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009427 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07009428 *mFakeListener);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009429 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
9430 }
9431
9432 void SetUp() override { SetUp(DEVICE_CLASSES); }
9433
9434 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07009435 mFakeListener.reset();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009436 mFakePolicy.clear();
9437 }
9438
9439 void configureDevice(uint32_t changes) {
9440 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
9441 mReader->requestRefreshConfiguration(changes);
9442 mReader->loopOnce();
9443 }
9444 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
9445 }
9446
9447 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
9448 const std::string& location, int32_t eventHubId,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07009449 ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -08009450 InputDeviceIdentifier identifier;
9451 identifier.name = name;
9452 identifier.location = location;
9453 std::shared_ptr<InputDevice> device =
9454 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
9455 identifier);
9456 mReader->pushNextDevice(device);
9457 mFakeEventHub->addDevice(eventHubId, name, classes);
9458 mReader->loopOnce();
9459 return device;
9460 }
9461
9462 template <class T, typename... Args>
9463 T& addControllerAndConfigure(Args... args) {
9464 T& controller = mDevice->addController<T>(EVENTHUB_ID, args...);
9465
9466 return controller;
9467 }
9468};
9469
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009470const char* PeripheralControllerTest::DEVICE_NAME = "device";
9471const char* PeripheralControllerTest::DEVICE_LOCATION = "BLUETOOTH";
9472const int32_t PeripheralControllerTest::DEVICE_ID = END_RESERVED_ID + 1000;
9473const int32_t PeripheralControllerTest::DEVICE_GENERATION = 2;
9474const int32_t PeripheralControllerTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07009475const ftl::Flags<InputDeviceClass> PeripheralControllerTest::DEVICE_CLASSES =
9476 ftl::Flags<InputDeviceClass>(0); // not needed for current tests
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009477const int32_t PeripheralControllerTest::EVENTHUB_ID = 1;
Chris Yee2b1e5c2021-03-10 22:45:12 -08009478
9479// --- BatteryControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009480class BatteryControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -08009481protected:
9482 void SetUp() override {
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009483 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::BATTERY);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009484 }
9485};
9486
9487TEST_F(BatteryControllerTest, GetBatteryCapacity) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009488 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009489
9490 ASSERT_TRUE(controller.getBatteryCapacity(DEFAULT_BATTERY));
9491 ASSERT_EQ(controller.getBatteryCapacity(DEFAULT_BATTERY).value_or(-1), BATTERY_CAPACITY);
9492}
9493
9494TEST_F(BatteryControllerTest, GetBatteryStatus) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009495 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009496
9497 ASSERT_TRUE(controller.getBatteryStatus(DEFAULT_BATTERY));
9498 ASSERT_EQ(controller.getBatteryStatus(DEFAULT_BATTERY).value_or(-1), BATTERY_STATUS);
9499}
9500
9501// --- LightControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009502class LightControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -08009503protected:
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009504 void SetUp() override {
9505 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::LIGHT);
9506 }
Chris Yee2b1e5c2021-03-10 22:45:12 -08009507};
9508
Chris Ye85758332021-05-16 23:05:17 -07009509TEST_F(LightControllerTest, MonoLight) {
9510 RawLightInfo infoMono = {.id = 1,
9511 .name = "Mono",
9512 .maxBrightness = 255,
9513 .flags = InputLightClass::BRIGHTNESS,
9514 .path = ""};
9515 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
Chris Yee2b1e5c2021-03-10 22:45:12 -08009516
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009517 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009518 InputDeviceInfo info;
9519 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009520 std::vector<InputDeviceLightInfo> lights = info.getLights();
9521 ASSERT_EQ(1U, lights.size());
9522 ASSERT_EQ(InputDeviceLightType::MONO, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009523
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009524 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
9525 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009526}
9527
9528TEST_F(LightControllerTest, RGBLight) {
9529 RawLightInfo infoRed = {.id = 1,
9530 .name = "red",
9531 .maxBrightness = 255,
9532 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
9533 .path = ""};
9534 RawLightInfo infoGreen = {.id = 2,
9535 .name = "green",
9536 .maxBrightness = 255,
9537 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
9538 .path = ""};
9539 RawLightInfo infoBlue = {.id = 3,
9540 .name = "blue",
9541 .maxBrightness = 255,
9542 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
9543 .path = ""};
9544 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
9545 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
9546 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
9547
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009548 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009549 InputDeviceInfo info;
9550 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009551 std::vector<InputDeviceLightInfo> lights = info.getLights();
9552 ASSERT_EQ(1U, lights.size());
9553 ASSERT_EQ(InputDeviceLightType::RGB, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009554
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009555 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
9556 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009557}
9558
9559TEST_F(LightControllerTest, MultiColorRGBLight) {
9560 RawLightInfo infoColor = {.id = 1,
9561 .name = "red",
9562 .maxBrightness = 255,
9563 .flags = InputLightClass::BRIGHTNESS |
9564 InputLightClass::MULTI_INTENSITY |
9565 InputLightClass::MULTI_INDEX,
9566 .path = ""};
9567
9568 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
9569
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009570 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009571 InputDeviceInfo info;
9572 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009573 std::vector<InputDeviceLightInfo> lights = info.getLights();
9574 ASSERT_EQ(1U, lights.size());
9575 ASSERT_EQ(InputDeviceLightType::MULTI_COLOR, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009576
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009577 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
9578 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009579}
9580
9581TEST_F(LightControllerTest, PlayerIdLight) {
9582 RawLightInfo info1 = {.id = 1,
9583 .name = "player1",
9584 .maxBrightness = 255,
9585 .flags = InputLightClass::BRIGHTNESS,
9586 .path = ""};
9587 RawLightInfo info2 = {.id = 2,
9588 .name = "player2",
9589 .maxBrightness = 255,
9590 .flags = InputLightClass::BRIGHTNESS,
9591 .path = ""};
9592 RawLightInfo info3 = {.id = 3,
9593 .name = "player3",
9594 .maxBrightness = 255,
9595 .flags = InputLightClass::BRIGHTNESS,
9596 .path = ""};
9597 RawLightInfo info4 = {.id = 4,
9598 .name = "player4",
9599 .maxBrightness = 255,
9600 .flags = InputLightClass::BRIGHTNESS,
9601 .path = ""};
9602 mFakeEventHub->addRawLightInfo(info1.id, std::move(info1));
9603 mFakeEventHub->addRawLightInfo(info2.id, std::move(info2));
9604 mFakeEventHub->addRawLightInfo(info3.id, std::move(info3));
9605 mFakeEventHub->addRawLightInfo(info4.id, std::move(info4));
9606
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009607 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009608 InputDeviceInfo info;
9609 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009610 std::vector<InputDeviceLightInfo> lights = info.getLights();
9611 ASSERT_EQ(1U, lights.size());
9612 ASSERT_EQ(InputDeviceLightType::PLAYER_ID, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009613
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009614 ASSERT_FALSE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
9615 ASSERT_TRUE(controller.setLightPlayerId(lights[0].id, LIGHT_PLAYER_ID));
9616 ASSERT_EQ(controller.getLightPlayerId(lights[0].id).value_or(-1), LIGHT_PLAYER_ID);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009617}
9618
Michael Wrightd02c5b62014-02-10 15:10:22 -08009619} // namespace android