blob: 53b03ada3a29fd231fe1a7b7268a22ad3dba7fdd [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
Prabir Pradhan2770d242019-09-02 18:07:11 -070017#include <CursorInputMapper.h>
18#include <InputDevice.h>
Prabir Pradhanc14266f2021-05-12 15:56:24 -070019#include <InputFlingerProperties.sysprop.h>
Prabir Pradhan2770d242019-09-02 18:07:11 -070020#include <InputMapper.h>
21#include <InputReader.h>
Prabir Pradhan1aed8582019-12-30 11:46:51 -080022#include <InputReaderBase.h>
23#include <InputReaderFactory.h>
Prabir Pradhan2770d242019-09-02 18:07:11 -070024#include <KeyboardInputMapper.h>
25#include <MultiTouchInputMapper.h>
Chris Ye1dd2e5c2021-04-04 23:12:41 -070026#include <PeripheralController.h>
Chris Yef59a2f42020-10-16 12:55:26 -070027#include <SensorInputMapper.h>
Prabir Pradhan2770d242019-09-02 18:07:11 -070028#include <SingleTouchInputMapper.h>
29#include <SwitchInputMapper.h>
30#include <TestInputListener.h>
31#include <TouchInputMapper.h>
Prabir Pradhan1aed8582019-12-30 11:46:51 -080032#include <UinputDevice.h>
Chris Ye87143712020-11-10 05:05:58 +000033#include <VibratorInputMapper.h>
Prabir Pradhan2574dfa2019-10-16 16:35:07 -070034#include <android-base/thread_annotations.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080035#include <gtest/gtest.h>
chaviw3277faf2021-05-19 16:45:23 -050036#include <gui/constants.h>
Siarhei Vishniakou473174e2017-12-27 16:44:42 -080037#include <inttypes.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080038#include <math.h>
39
Michael Wright17db18e2020-06-26 20:51:44 +010040#include <memory>
Chris Ye3fdbfef2021-01-06 18:45:18 -080041#include <regex>
Michael Wrightdde67b82020-10-27 16:09:22 +000042#include "input/DisplayViewport.h"
43#include "input/Input.h"
Michael Wright17db18e2020-06-26 20:51:44 +010044
Michael Wrightd02c5b62014-02-10 15:10:22 -080045namespace android {
46
Prabir Pradhan2574dfa2019-10-16 16:35:07 -070047using std::chrono_literals::operator""ms;
Chris Ye1b0c7342020-07-28 21:57:03 -070048using namespace android::flag_operators;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -070049
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
Michael Wrightd02c5b62014-02-10 15:10:22 -080082// Error tolerance for floating point assertions.
83static const float EPSILON = 0.001f;
84
85template<typename T>
86static inline T min(T a, T b) {
87 return a < b ? a : b;
88}
89
90static inline float avg(float x, float y) {
91 return (x + y) / 2;
92}
93
Chris Ye3fdbfef2021-01-06 18:45:18 -080094// Mapping for light color name and the light color
95const std::unordered_map<std::string, LightColor> LIGHT_COLORS = {{"red", LightColor::RED},
96 {"green", LightColor::GREEN},
97 {"blue", LightColor::BLUE}};
Michael Wrightd02c5b62014-02-10 15:10:22 -080098
Prabir Pradhanc14266f2021-05-12 15:56:24 -070099static int32_t getInverseRotation(int32_t orientation) {
100 switch (orientation) {
101 case DISPLAY_ORIENTATION_90:
102 return DISPLAY_ORIENTATION_270;
103 case DISPLAY_ORIENTATION_270:
104 return DISPLAY_ORIENTATION_90;
105 default:
106 return orientation;
107 }
108}
109
Michael Wrightd02c5b62014-02-10 15:10:22 -0800110// --- FakePointerController ---
111
112class FakePointerController : public PointerControllerInterface {
113 bool mHaveBounds;
114 float mMinX, mMinY, mMaxX, mMaxY;
115 float mX, mY;
116 int32_t mButtonState;
Arthur Hungc7ad2d02018-12-18 17:41:29 +0800117 int32_t mDisplayId;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800118
Michael Wrightd02c5b62014-02-10 15:10:22 -0800119public:
120 FakePointerController() :
121 mHaveBounds(false), mMinX(0), mMinY(0), mMaxX(0), mMaxY(0), mX(0), mY(0),
Arthur Hungc7ad2d02018-12-18 17:41:29 +0800122 mButtonState(0), mDisplayId(ADISPLAY_ID_DEFAULT) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800123 }
124
Michael Wright17db18e2020-06-26 20:51:44 +0100125 virtual ~FakePointerController() {}
126
Michael Wrightd02c5b62014-02-10 15:10:22 -0800127 void setBounds(float minX, float minY, float maxX, float maxY) {
128 mHaveBounds = true;
129 mMinX = minX;
130 mMinY = minY;
131 mMaxX = maxX;
132 mMaxY = maxY;
133 }
134
Chris Yea52ade12020-08-27 16:49:20 -0700135 void setPosition(float x, float y) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800136 mX = x;
137 mY = y;
138 }
139
Chris Yea52ade12020-08-27 16:49:20 -0700140 void setButtonState(int32_t buttonState) override { mButtonState = buttonState; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800141
Chris Yea52ade12020-08-27 16:49:20 -0700142 int32_t getButtonState() const override { return mButtonState; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800143
Chris Yea52ade12020-08-27 16:49:20 -0700144 void getPosition(float* outX, float* outY) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800145 *outX = mX;
146 *outY = mY;
147 }
148
Chris Yea52ade12020-08-27 16:49:20 -0700149 int32_t getDisplayId() const override { return mDisplayId; }
Arthur Hungc7ad2d02018-12-18 17:41:29 +0800150
Chris Yea52ade12020-08-27 16:49:20 -0700151 void setDisplayViewport(const DisplayViewport& viewport) override {
Garfield Tan888a6a42020-01-09 11:39:16 -0800152 mDisplayId = viewport.displayId;
153 }
154
Arthur Hung7c645402019-01-25 17:45:42 +0800155 const std::map<int32_t, std::vector<int32_t>>& getSpots() {
156 return mSpotsByDisplay;
157 }
158
Michael Wrightd02c5b62014-02-10 15:10:22 -0800159private:
Chris Yea52ade12020-08-27 16:49:20 -0700160 bool getBounds(float* outMinX, float* outMinY, float* outMaxX, float* outMaxY) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800161 *outMinX = mMinX;
162 *outMinY = mMinY;
163 *outMaxX = mMaxX;
164 *outMaxY = mMaxY;
165 return mHaveBounds;
166 }
167
Chris Yea52ade12020-08-27 16:49:20 -0700168 void move(float deltaX, float deltaY) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800169 mX += deltaX;
170 if (mX < mMinX) mX = mMinX;
171 if (mX > mMaxX) mX = mMaxX;
172 mY += deltaY;
173 if (mY < mMinY) mY = mMinY;
174 if (mY > mMaxY) mY = mMaxY;
175 }
176
Chris Yea52ade12020-08-27 16:49:20 -0700177 void fade(Transition) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800178
Chris Yea52ade12020-08-27 16:49:20 -0700179 void unfade(Transition) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800180
Chris Yea52ade12020-08-27 16:49:20 -0700181 void setPresentation(Presentation) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800182
Chris Yea52ade12020-08-27 16:49:20 -0700183 void setSpots(const PointerCoords*, const uint32_t*, BitSet32 spotIdBits,
184 int32_t displayId) override {
Arthur Hung7c645402019-01-25 17:45:42 +0800185 std::vector<int32_t> newSpots;
186 // Add spots for fingers that are down.
187 for (BitSet32 idBits(spotIdBits); !idBits.isEmpty(); ) {
188 uint32_t id = idBits.clearFirstMarkedBit();
189 newSpots.push_back(id);
190 }
191
192 mSpotsByDisplay[displayId] = newSpots;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800193 }
194
Chris Yea52ade12020-08-27 16:49:20 -0700195 void clearSpots() override {}
Arthur Hung7c645402019-01-25 17:45:42 +0800196
197 std::map<int32_t, std::vector<int32_t>> mSpotsByDisplay;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800198};
199
200
201// --- FakeInputReaderPolicy ---
202
203class FakeInputReaderPolicy : public InputReaderPolicyInterface {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700204 std::mutex mLock;
205 std::condition_variable mDevicesChangedCondition;
206
Michael Wrightd02c5b62014-02-10 15:10:22 -0800207 InputReaderConfiguration mConfig;
Prabir Pradhan2853b7a2021-08-23 14:08:51 +0000208 std::shared_ptr<FakePointerController> mPointerController;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700209 std::vector<InputDeviceInfo> mInputDevices GUARDED_BY(mLock);
210 bool mInputDevicesChanged GUARDED_BY(mLock){false};
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100211 std::vector<DisplayViewport> mViewports;
Jason Gerecke489fda82012-09-07 17:19:40 -0700212 TouchAffineTransformation transform;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800213
214protected:
Chris Yea52ade12020-08-27 16:49:20 -0700215 virtual ~FakeInputReaderPolicy() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800216
217public:
218 FakeInputReaderPolicy() {
219 }
220
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700221 void assertInputDevicesChanged() {
Prabir Pradhan1aed8582019-12-30 11:46:51 -0800222 waitForInputDevices([](bool devicesChanged) {
223 if (!devicesChanged) {
224 FAIL() << "Timed out waiting for notifyInputDevicesChanged() to be called.";
225 }
226 });
227 }
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700228
Prabir Pradhan1aed8582019-12-30 11:46:51 -0800229 void assertInputDevicesNotChanged() {
230 waitForInputDevices([](bool devicesChanged) {
231 if (devicesChanged) {
232 FAIL() << "Expected notifyInputDevicesChanged() to not be called.";
233 }
234 });
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700235 }
236
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700237 virtual void clearViewports() {
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100238 mViewports.clear();
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100239 mConfig.setDisplayViewports(mViewports);
Santos Cordonfa5cf462017-04-05 10:37:00 -0700240 }
241
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700242 std::optional<DisplayViewport> getDisplayViewportByUniqueId(const std::string& uniqueId) const {
243 return mConfig.getDisplayViewportByUniqueId(uniqueId);
244 }
245 std::optional<DisplayViewport> getDisplayViewportByType(ViewportType type) const {
246 return mConfig.getDisplayViewportByType(type);
247 }
248
249 std::optional<DisplayViewport> getDisplayViewportByPort(uint8_t displayPort) const {
250 return mConfig.getDisplayViewportByPort(displayPort);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700251 }
252
Prabir Pradhan5632d622021-09-06 07:57:20 -0700253 void addDisplayViewport(DisplayViewport viewport) {
254 mViewports.push_back(std::move(viewport));
255 mConfig.setDisplayViewports(mViewports);
256 }
257
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700258 void addDisplayViewport(int32_t displayId, int32_t width, int32_t height, int32_t orientation,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +0000259 bool isActive, const std::string& uniqueId,
Prabir Pradhan5632d622021-09-06 07:57:20 -0700260 std::optional<uint8_t> physicalPort, ViewportType type) {
261 const bool isRotated =
262 (orientation == DISPLAY_ORIENTATION_90 || orientation == DISPLAY_ORIENTATION_270);
263 DisplayViewport v;
264 v.displayId = displayId;
265 v.orientation = orientation;
266 v.logicalLeft = 0;
267 v.logicalTop = 0;
268 v.logicalRight = isRotated ? height : width;
269 v.logicalBottom = isRotated ? width : height;
270 v.physicalLeft = 0;
271 v.physicalTop = 0;
272 v.physicalRight = isRotated ? height : width;
273 v.physicalBottom = isRotated ? width : height;
274 v.deviceWidth = isRotated ? height : width;
275 v.deviceHeight = isRotated ? width : height;
276 v.isActive = isActive;
277 v.uniqueId = uniqueId;
278 v.physicalPort = physicalPort;
279 v.type = type;
280
281 addDisplayViewport(v);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800282 }
283
Arthur Hung6cd19a42019-08-30 19:04:12 +0800284 bool updateViewport(const DisplayViewport& viewport) {
285 size_t count = mViewports.size();
286 for (size_t i = 0; i < count; i++) {
287 const DisplayViewport& currentViewport = mViewports[i];
288 if (currentViewport.displayId == viewport.displayId) {
289 mViewports[i] = viewport;
290 mConfig.setDisplayViewports(mViewports);
291 return true;
292 }
293 }
294 // no viewport found.
295 return false;
296 }
297
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100298 void addExcludedDeviceName(const std::string& deviceName) {
299 mConfig.excludedDeviceNames.push_back(deviceName);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800300 }
301
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700302 void addInputPortAssociation(const std::string& inputPort, uint8_t displayPort) {
303 mConfig.portAssociations.insert({inputPort, displayPort});
304 }
305
Christine Franks1ba71cc2021-04-07 14:37:42 -0700306 void addInputUniqueIdAssociation(const std::string& inputUniqueId,
307 const std::string& displayUniqueId) {
308 mConfig.uniqueIdAssociations.insert({inputUniqueId, displayUniqueId});
309 }
310
Siarhei Vishniakouc6f61192019-07-23 18:12:31 +0000311 void addDisabledDevice(int32_t deviceId) { mConfig.disabledDevices.insert(deviceId); }
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700312
Siarhei Vishniakouc6f61192019-07-23 18:12:31 +0000313 void removeDisabledDevice(int32_t deviceId) { mConfig.disabledDevices.erase(deviceId); }
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700314
Prabir Pradhan2853b7a2021-08-23 14:08:51 +0000315 void setPointerController(std::shared_ptr<FakePointerController> controller) {
316 mPointerController = std::move(controller);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800317 }
318
319 const InputReaderConfiguration* getReaderConfiguration() const {
320 return &mConfig;
321 }
322
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800323 const std::vector<InputDeviceInfo>& getInputDevices() const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800324 return mInputDevices;
325 }
326
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100327 TouchAffineTransformation getTouchAffineTransformation(const std::string& inputDeviceDescriptor,
Jason Gerecke71b16e82014-03-10 09:47:59 -0700328 int32_t surfaceRotation) {
Jason Gerecke489fda82012-09-07 17:19:40 -0700329 return transform;
330 }
331
332 void setTouchAffineTransformation(const TouchAffineTransformation t) {
333 transform = t;
Jason Gerecke12d6baa2014-01-27 18:34:20 -0800334 }
335
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000336 PointerCaptureRequest setPointerCapture(bool enabled) {
337 mConfig.pointerCaptureRequest = {enabled, mNextPointerCaptureSequenceNumber++};
338 return mConfig.pointerCaptureRequest;
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -0800339 }
340
Arthur Hung7c645402019-01-25 17:45:42 +0800341 void setShowTouches(bool enabled) {
342 mConfig.showTouches = enabled;
343 }
344
Garfield Tan888a6a42020-01-09 11:39:16 -0800345 void setDefaultPointerDisplayId(int32_t pointerDisplayId) {
346 mConfig.defaultPointerDisplayId = pointerDisplayId;
347 }
348
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -0800349 float getPointerGestureMovementSpeedRatio() { return mConfig.pointerGestureMovementSpeedRatio; }
350
Michael Wrightd02c5b62014-02-10 15:10:22 -0800351private:
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000352 uint32_t mNextPointerCaptureSequenceNumber = 0;
353
Chris Yea52ade12020-08-27 16:49:20 -0700354 void getReaderConfiguration(InputReaderConfiguration* outConfig) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800355 *outConfig = mConfig;
356 }
357
Prabir Pradhan2853b7a2021-08-23 14:08:51 +0000358 std::shared_ptr<PointerControllerInterface> obtainPointerController(
359 int32_t /*deviceId*/) override {
360 return mPointerController;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800361 }
362
Chris Yea52ade12020-08-27 16:49:20 -0700363 void notifyInputDevicesChanged(const std::vector<InputDeviceInfo>& inputDevices) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700364 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800365 mInputDevices = inputDevices;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700366 mInputDevicesChanged = true;
367 mDevicesChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800368 }
369
Chris Yea52ade12020-08-27 16:49:20 -0700370 std::shared_ptr<KeyCharacterMap> getKeyboardLayoutOverlay(
371 const InputDeviceIdentifier&) override {
Yi Kong9b14ac62018-07-17 13:48:38 -0700372 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800373 }
374
Chris Yea52ade12020-08-27 16:49:20 -0700375 std::string getDeviceAlias(const InputDeviceIdentifier&) override { return ""; }
Prabir Pradhan1aed8582019-12-30 11:46:51 -0800376
377 void waitForInputDevices(std::function<void(bool)> processDevicesChanged) {
378 std::unique_lock<std::mutex> lock(mLock);
379 base::ScopedLockAssertion assumeLocked(mLock);
380
381 const bool devicesChanged =
382 mDevicesChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
383 return mInputDevicesChanged;
384 });
385 ASSERT_NO_FATAL_FAILURE(processDevicesChanged(devicesChanged));
386 mInputDevicesChanged = false;
387 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800388};
389
Michael Wrightd02c5b62014-02-10 15:10:22 -0800390// --- FakeEventHub ---
391
392class FakeEventHub : public EventHubInterface {
393 struct KeyInfo {
394 int32_t keyCode;
395 uint32_t flags;
396 };
397
Chris Yef59a2f42020-10-16 12:55:26 -0700398 struct SensorInfo {
399 InputDeviceSensorType sensorType;
400 int32_t sensorDataIndex;
401 };
402
Michael Wrightd02c5b62014-02-10 15:10:22 -0800403 struct Device {
404 InputDeviceIdentifier identifier;
Chris Ye1b0c7342020-07-28 21:57:03 -0700405 Flags<InputDeviceClass> classes;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800406 PropertyMap configuration;
407 KeyedVector<int, RawAbsoluteAxisInfo> absoluteAxes;
408 KeyedVector<int, bool> relativeAxes;
409 KeyedVector<int32_t, int32_t> keyCodeStates;
410 KeyedVector<int32_t, int32_t> scanCodeStates;
411 KeyedVector<int32_t, int32_t> switchStates;
412 KeyedVector<int32_t, int32_t> absoluteAxisValue;
413 KeyedVector<int32_t, KeyInfo> keysByScanCode;
414 KeyedVector<int32_t, KeyInfo> keysByUsageCode;
415 KeyedVector<int32_t, bool> leds;
Chris Yef59a2f42020-10-16 12:55:26 -0700416 std::unordered_map<int32_t, SensorInfo> sensorsByAbsCode;
417 BitArray<MSC_MAX> mscBitmask;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800418 std::vector<VirtualKeyDefinition> virtualKeys;
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700419 bool enabled;
420
421 status_t enable() {
422 enabled = true;
423 return OK;
424 }
425
426 status_t disable() {
427 enabled = false;
428 return OK;
429 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800430
Chris Ye1b0c7342020-07-28 21:57:03 -0700431 explicit Device(Flags<InputDeviceClass> classes) : classes(classes), enabled(true) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800432 };
433
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700434 std::mutex mLock;
435 std::condition_variable mEventsCondition;
436
Michael Wrightd02c5b62014-02-10 15:10:22 -0800437 KeyedVector<int32_t, Device*> mDevices;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100438 std::vector<std::string> mExcludedDevices;
Siarhei Vishniakou370039c2021-02-04 22:09:01 +0000439 std::vector<RawEvent> mEvents GUARDED_BY(mLock);
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600440 std::unordered_map<int32_t /*deviceId*/, std::vector<TouchVideoFrame>> mVideoFrames;
Chris Ye87143712020-11-10 05:05:58 +0000441 std::vector<int32_t> mVibrators = {0, 1};
Chris Ye3fdbfef2021-01-06 18:45:18 -0800442 std::unordered_map<int32_t, RawLightInfo> mRawLightInfos;
443 // Simulates a device light brightness, from light id to light brightness.
444 std::unordered_map<int32_t /* lightId */, int32_t /* brightness*/> mLightBrightness;
445 // Simulates a device light intensities, from light id to light intensities map.
446 std::unordered_map<int32_t /* lightId */, std::unordered_map<LightColor, int32_t>>
447 mLightIntensities;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800448
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700449public:
Michael Wrightd02c5b62014-02-10 15:10:22 -0800450 virtual ~FakeEventHub() {
451 for (size_t i = 0; i < mDevices.size(); i++) {
452 delete mDevices.valueAt(i);
453 }
454 }
455
Michael Wrightd02c5b62014-02-10 15:10:22 -0800456 FakeEventHub() { }
457
Chris Ye1b0c7342020-07-28 21:57:03 -0700458 void addDevice(int32_t deviceId, const std::string& name, Flags<InputDeviceClass> classes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800459 Device* device = new Device(classes);
460 device->identifier.name = name;
461 mDevices.add(deviceId, device);
462
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000463 enqueueEvent(ARBITRARY_TIME, READ_TIME, deviceId, EventHubInterface::DEVICE_ADDED, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800464 }
465
466 void removeDevice(int32_t deviceId) {
467 delete mDevices.valueFor(deviceId);
468 mDevices.removeItem(deviceId);
469
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000470 enqueueEvent(ARBITRARY_TIME, READ_TIME, deviceId, EventHubInterface::DEVICE_REMOVED, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800471 }
472
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700473 bool isDeviceEnabled(int32_t deviceId) {
474 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700475 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700476 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
477 return false;
478 }
479 return device->enabled;
480 }
481
482 status_t enableDevice(int32_t deviceId) {
483 status_t result;
484 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700485 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700486 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
487 return BAD_VALUE;
488 }
489 if (device->enabled) {
490 ALOGW("Duplicate call to %s, device %" PRId32 " already enabled", __func__, deviceId);
491 return OK;
492 }
493 result = device->enable();
494 return result;
495 }
496
497 status_t disableDevice(int32_t deviceId) {
498 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700499 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700500 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
501 return BAD_VALUE;
502 }
503 if (!device->enabled) {
504 ALOGW("Duplicate call to %s, device %" PRId32 " already disabled", __func__, deviceId);
505 return OK;
506 }
507 return device->disable();
508 }
509
Michael Wrightd02c5b62014-02-10 15:10:22 -0800510 void finishDeviceScan() {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000511 enqueueEvent(ARBITRARY_TIME, READ_TIME, 0, EventHubInterface::FINISHED_DEVICE_SCAN, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800512 }
513
514 void addConfigurationProperty(int32_t deviceId, const String8& key, const String8& value) {
515 Device* device = getDevice(deviceId);
516 device->configuration.addProperty(key, value);
517 }
518
519 void addConfigurationMap(int32_t deviceId, const PropertyMap* configuration) {
520 Device* device = getDevice(deviceId);
521 device->configuration.addAll(configuration);
522 }
523
524 void addAbsoluteAxis(int32_t deviceId, int axis,
525 int32_t minValue, int32_t maxValue, int flat, int fuzz, int resolution = 0) {
526 Device* device = getDevice(deviceId);
527
528 RawAbsoluteAxisInfo info;
529 info.valid = true;
530 info.minValue = minValue;
531 info.maxValue = maxValue;
532 info.flat = flat;
533 info.fuzz = fuzz;
534 info.resolution = resolution;
535 device->absoluteAxes.add(axis, info);
536 }
537
538 void addRelativeAxis(int32_t deviceId, int32_t axis) {
539 Device* device = getDevice(deviceId);
540 device->relativeAxes.add(axis, true);
541 }
542
543 void setKeyCodeState(int32_t deviceId, int32_t keyCode, int32_t state) {
544 Device* device = getDevice(deviceId);
545 device->keyCodeStates.replaceValueFor(keyCode, state);
546 }
547
548 void setScanCodeState(int32_t deviceId, int32_t scanCode, int32_t state) {
549 Device* device = getDevice(deviceId);
550 device->scanCodeStates.replaceValueFor(scanCode, state);
551 }
552
553 void setSwitchState(int32_t deviceId, int32_t switchCode, int32_t state) {
554 Device* device = getDevice(deviceId);
555 device->switchStates.replaceValueFor(switchCode, state);
556 }
557
558 void setAbsoluteAxisValue(int32_t deviceId, int32_t axis, int32_t value) {
559 Device* device = getDevice(deviceId);
560 device->absoluteAxisValue.replaceValueFor(axis, value);
561 }
562
563 void addKey(int32_t deviceId, int32_t scanCode, int32_t usageCode,
564 int32_t keyCode, uint32_t flags) {
565 Device* device = getDevice(deviceId);
566 KeyInfo info;
567 info.keyCode = keyCode;
568 info.flags = flags;
569 if (scanCode) {
570 device->keysByScanCode.add(scanCode, info);
571 }
572 if (usageCode) {
573 device->keysByUsageCode.add(usageCode, info);
574 }
575 }
576
577 void addLed(int32_t deviceId, int32_t led, bool initialState) {
578 Device* device = getDevice(deviceId);
579 device->leds.add(led, initialState);
580 }
581
Chris Yef59a2f42020-10-16 12:55:26 -0700582 void addSensorAxis(int32_t deviceId, int32_t absCode, InputDeviceSensorType sensorType,
583 int32_t sensorDataIndex) {
584 Device* device = getDevice(deviceId);
585 SensorInfo info;
586 info.sensorType = sensorType;
587 info.sensorDataIndex = sensorDataIndex;
588 device->sensorsByAbsCode.emplace(absCode, info);
589 }
590
591 void setMscEvent(int32_t deviceId, int32_t mscEvent) {
592 Device* device = getDevice(deviceId);
593 typename BitArray<MSC_MAX>::Buffer buffer;
594 buffer[mscEvent / 32] = 1 << mscEvent % 32;
595 device->mscBitmask.loadFromBuffer(buffer);
596 }
597
Chris Ye3fdbfef2021-01-06 18:45:18 -0800598 void addRawLightInfo(int32_t rawId, RawLightInfo&& info) {
599 mRawLightInfos.emplace(rawId, std::move(info));
600 }
601
602 void fakeLightBrightness(int32_t rawId, int32_t brightness) {
603 mLightBrightness.emplace(rawId, brightness);
604 }
605
606 void fakeLightIntensities(int32_t rawId,
607 const std::unordered_map<LightColor, int32_t> intensities) {
608 mLightIntensities.emplace(rawId, std::move(intensities));
609 }
610
Michael Wrightd02c5b62014-02-10 15:10:22 -0800611 bool getLedState(int32_t deviceId, int32_t led) {
612 Device* device = getDevice(deviceId);
613 return device->leds.valueFor(led);
614 }
615
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100616 std::vector<std::string>& getExcludedDevices() {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800617 return mExcludedDevices;
618 }
619
620 void addVirtualKeyDefinition(int32_t deviceId, const VirtualKeyDefinition& definition) {
621 Device* device = getDevice(deviceId);
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800622 device->virtualKeys.push_back(definition);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800623 }
624
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000625 void enqueueEvent(nsecs_t when, nsecs_t readTime, int32_t deviceId, int32_t type, int32_t code,
626 int32_t value) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700627 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800628 RawEvent event;
629 event.when = when;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000630 event.readTime = readTime;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800631 event.deviceId = deviceId;
632 event.type = type;
633 event.code = code;
634 event.value = value;
635 mEvents.push_back(event);
636
637 if (type == EV_ABS) {
638 setAbsoluteAxisValue(deviceId, code, value);
639 }
640 }
641
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600642 void setVideoFrames(std::unordered_map<int32_t /*deviceId*/,
643 std::vector<TouchVideoFrame>> videoFrames) {
644 mVideoFrames = std::move(videoFrames);
645 }
646
Michael Wrightd02c5b62014-02-10 15:10:22 -0800647 void assertQueueIsEmpty() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700648 std::unique_lock<std::mutex> lock(mLock);
649 base::ScopedLockAssertion assumeLocked(mLock);
650 const bool queueIsEmpty =
651 mEventsCondition.wait_for(lock, WAIT_TIMEOUT,
652 [this]() REQUIRES(mLock) { return mEvents.size() == 0; });
653 if (!queueIsEmpty) {
654 FAIL() << "Timed out waiting for EventHub queue to be emptied.";
655 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800656 }
657
658private:
659 Device* getDevice(int32_t deviceId) const {
660 ssize_t index = mDevices.indexOfKey(deviceId);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100661 return index >= 0 ? mDevices.valueAt(index) : nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800662 }
663
Chris Yea52ade12020-08-27 16:49:20 -0700664 Flags<InputDeviceClass> getDeviceClasses(int32_t deviceId) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800665 Device* device = getDevice(deviceId);
Chris Ye1b0c7342020-07-28 21:57:03 -0700666 return device ? device->classes : Flags<InputDeviceClass>(0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800667 }
668
Chris Yea52ade12020-08-27 16:49:20 -0700669 InputDeviceIdentifier getDeviceIdentifier(int32_t deviceId) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800670 Device* device = getDevice(deviceId);
671 return device ? device->identifier : InputDeviceIdentifier();
672 }
673
Chris Yea52ade12020-08-27 16:49:20 -0700674 int32_t getDeviceControllerNumber(int32_t) const override { return 0; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800675
Chris Yea52ade12020-08-27 16:49:20 -0700676 void getConfiguration(int32_t deviceId, PropertyMap* outConfiguration) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800677 Device* device = getDevice(deviceId);
678 if (device) {
679 *outConfiguration = device->configuration;
680 }
681 }
682
Chris Yea52ade12020-08-27 16:49:20 -0700683 status_t getAbsoluteAxisInfo(int32_t deviceId, int axis,
684 RawAbsoluteAxisInfo* outAxisInfo) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800685 Device* device = getDevice(deviceId);
Arthur Hung9da14732019-09-02 16:16:58 +0800686 if (device && device->enabled) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800687 ssize_t index = device->absoluteAxes.indexOfKey(axis);
688 if (index >= 0) {
689 *outAxisInfo = device->absoluteAxes.valueAt(index);
690 return OK;
691 }
692 }
693 outAxisInfo->clear();
694 return -1;
695 }
696
Chris Yea52ade12020-08-27 16:49:20 -0700697 bool hasRelativeAxis(int32_t deviceId, int axis) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800698 Device* device = getDevice(deviceId);
699 if (device) {
700 return device->relativeAxes.indexOfKey(axis) >= 0;
701 }
702 return false;
703 }
704
Chris Yea52ade12020-08-27 16:49:20 -0700705 bool hasInputProperty(int32_t, int) const override { return false; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800706
Chris Yef59a2f42020-10-16 12:55:26 -0700707 bool hasMscEvent(int32_t deviceId, int mscEvent) const override final {
708 Device* device = getDevice(deviceId);
709 if (device) {
710 return mscEvent >= 0 && mscEvent <= MSC_MAX ? device->mscBitmask.test(mscEvent) : false;
711 }
712 return false;
713 }
714
Chris Yea52ade12020-08-27 16:49:20 -0700715 status_t mapKey(int32_t deviceId, int32_t scanCode, int32_t usageCode, int32_t metaState,
716 int32_t* outKeycode, int32_t* outMetaState, uint32_t* outFlags) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800717 Device* device = getDevice(deviceId);
718 if (device) {
719 const KeyInfo* key = getKey(device, scanCode, usageCode);
720 if (key) {
721 if (outKeycode) {
722 *outKeycode = key->keyCode;
723 }
724 if (outFlags) {
725 *outFlags = key->flags;
726 }
Dmitry Torokhov0faaa0b2015-09-24 13:13:55 -0700727 if (outMetaState) {
728 *outMetaState = metaState;
729 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800730 return OK;
731 }
732 }
733 return NAME_NOT_FOUND;
734 }
735
736 const KeyInfo* getKey(Device* device, int32_t scanCode, int32_t usageCode) const {
737 if (usageCode) {
738 ssize_t index = device->keysByUsageCode.indexOfKey(usageCode);
739 if (index >= 0) {
740 return &device->keysByUsageCode.valueAt(index);
741 }
742 }
743 if (scanCode) {
744 ssize_t index = device->keysByScanCode.indexOfKey(scanCode);
745 if (index >= 0) {
746 return &device->keysByScanCode.valueAt(index);
747 }
748 }
Yi Kong9b14ac62018-07-17 13:48:38 -0700749 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800750 }
751
Chris Yea52ade12020-08-27 16:49:20 -0700752 status_t mapAxis(int32_t, int32_t, AxisInfo*) const override { return NAME_NOT_FOUND; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800753
Chris Yef59a2f42020-10-16 12:55:26 -0700754 base::Result<std::pair<InputDeviceSensorType, int32_t>> mapSensor(int32_t deviceId,
755 int32_t absCode) {
756 Device* device = getDevice(deviceId);
757 if (!device) {
758 return Errorf("Sensor device not found.");
759 }
760 auto it = device->sensorsByAbsCode.find(absCode);
761 if (it == device->sensorsByAbsCode.end()) {
762 return Errorf("Sensor map not found.");
763 }
764 const SensorInfo& info = it->second;
765 return std::make_pair(info.sensorType, info.sensorDataIndex);
766 }
767
Chris Yea52ade12020-08-27 16:49:20 -0700768 void setExcludedDevices(const std::vector<std::string>& devices) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800769 mExcludedDevices = devices;
770 }
771
Siarhei Vishniakou370039c2021-02-04 22:09:01 +0000772 size_t getEvents(int, RawEvent* buffer, size_t bufferSize) override {
773 std::scoped_lock lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800774
Siarhei Vishniakou370039c2021-02-04 22:09:01 +0000775 const size_t filledSize = std::min(mEvents.size(), bufferSize);
776 std::copy(mEvents.begin(), mEvents.begin() + filledSize, buffer);
777
778 mEvents.erase(mEvents.begin(), mEvents.begin() + filledSize);
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700779 mEventsCondition.notify_all();
Siarhei Vishniakou370039c2021-02-04 22:09:01 +0000780 return filledSize;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800781 }
782
Chris Yea52ade12020-08-27 16:49:20 -0700783 std::vector<TouchVideoFrame> getVideoFrames(int32_t deviceId) override {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600784 auto it = mVideoFrames.find(deviceId);
785 if (it != mVideoFrames.end()) {
786 std::vector<TouchVideoFrame> frames = std::move(it->second);
787 mVideoFrames.erase(deviceId);
788 return frames;
789 }
Siarhei Vishniakouadd89292018-12-13 19:23:36 -0800790 return {};
791 }
792
Chris Yea52ade12020-08-27 16:49:20 -0700793 int32_t getScanCodeState(int32_t deviceId, int32_t scanCode) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800794 Device* device = getDevice(deviceId);
795 if (device) {
796 ssize_t index = device->scanCodeStates.indexOfKey(scanCode);
797 if (index >= 0) {
798 return device->scanCodeStates.valueAt(index);
799 }
800 }
801 return AKEY_STATE_UNKNOWN;
802 }
803
Chris Yea52ade12020-08-27 16:49:20 -0700804 int32_t getKeyCodeState(int32_t deviceId, int32_t keyCode) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800805 Device* device = getDevice(deviceId);
806 if (device) {
807 ssize_t index = device->keyCodeStates.indexOfKey(keyCode);
808 if (index >= 0) {
809 return device->keyCodeStates.valueAt(index);
810 }
811 }
812 return AKEY_STATE_UNKNOWN;
813 }
814
Chris Yea52ade12020-08-27 16:49:20 -0700815 int32_t getSwitchState(int32_t deviceId, int32_t sw) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800816 Device* device = getDevice(deviceId);
817 if (device) {
818 ssize_t index = device->switchStates.indexOfKey(sw);
819 if (index >= 0) {
820 return device->switchStates.valueAt(index);
821 }
822 }
823 return AKEY_STATE_UNKNOWN;
824 }
825
Chris Yea52ade12020-08-27 16:49:20 -0700826 status_t getAbsoluteAxisValue(int32_t deviceId, int32_t axis,
827 int32_t* outValue) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800828 Device* device = getDevice(deviceId);
829 if (device) {
830 ssize_t index = device->absoluteAxisValue.indexOfKey(axis);
831 if (index >= 0) {
832 *outValue = device->absoluteAxisValue.valueAt(index);
833 return OK;
834 }
835 }
836 *outValue = 0;
837 return -1;
838 }
839
Chris Yea52ade12020-08-27 16:49:20 -0700840 // Return true if the device has non-empty key layout.
841 bool markSupportedKeyCodes(int32_t deviceId, size_t numCodes, const int32_t* keyCodes,
842 uint8_t* outFlags) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800843 bool result = false;
844 Device* device = getDevice(deviceId);
845 if (device) {
Chris Yea52ade12020-08-27 16:49:20 -0700846 result = device->keysByScanCode.size() > 0 || device->keysByUsageCode.size() > 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800847 for (size_t i = 0; i < numCodes; i++) {
848 for (size_t j = 0; j < device->keysByScanCode.size(); j++) {
849 if (keyCodes[i] == device->keysByScanCode.valueAt(j).keyCode) {
850 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800851 }
852 }
853 for (size_t j = 0; j < device->keysByUsageCode.size(); j++) {
854 if (keyCodes[i] == device->keysByUsageCode.valueAt(j).keyCode) {
855 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800856 }
857 }
858 }
859 }
860 return result;
861 }
862
Chris Yea52ade12020-08-27 16:49:20 -0700863 bool hasScanCode(int32_t deviceId, int32_t scanCode) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800864 Device* device = getDevice(deviceId);
865 if (device) {
866 ssize_t index = device->keysByScanCode.indexOfKey(scanCode);
867 return index >= 0;
868 }
869 return false;
870 }
871
Arthur Hungcb40a002021-08-03 14:31:01 +0000872 bool hasKeyCode(int32_t deviceId, int32_t keyCode) const override {
873 Device* device = getDevice(deviceId);
874 if (!device) {
875 return false;
876 }
877 for (size_t i = 0; i < device->keysByScanCode.size(); i++) {
878 if (keyCode == device->keysByScanCode.valueAt(i).keyCode) {
879 return true;
880 }
881 }
882 for (size_t j = 0; j < device->keysByUsageCode.size(); j++) {
883 if (keyCode == device->keysByUsageCode.valueAt(j).keyCode) {
884 return true;
885 }
886 }
887 return false;
888 }
889
Chris Yea52ade12020-08-27 16:49:20 -0700890 bool hasLed(int32_t deviceId, int32_t led) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800891 Device* device = getDevice(deviceId);
892 return device && device->leds.indexOfKey(led) >= 0;
893 }
894
Chris Yea52ade12020-08-27 16:49:20 -0700895 void setLedState(int32_t deviceId, int32_t led, bool on) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800896 Device* device = getDevice(deviceId);
897 if (device) {
898 ssize_t index = device->leds.indexOfKey(led);
899 if (index >= 0) {
900 device->leds.replaceValueAt(led, on);
901 } else {
902 ADD_FAILURE()
903 << "Attempted to set the state of an LED that the EventHub declared "
904 "was not present. led=" << led;
905 }
906 }
907 }
908
Chris Yea52ade12020-08-27 16:49:20 -0700909 void getVirtualKeyDefinitions(
910 int32_t deviceId, std::vector<VirtualKeyDefinition>& outVirtualKeys) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800911 outVirtualKeys.clear();
912
913 Device* device = getDevice(deviceId);
914 if (device) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800915 outVirtualKeys = device->virtualKeys;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800916 }
917 }
918
Chris Yea52ade12020-08-27 16:49:20 -0700919 const std::shared_ptr<KeyCharacterMap> getKeyCharacterMap(int32_t) const override {
Yi Kong9b14ac62018-07-17 13:48:38 -0700920 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800921 }
922
Chris Yea52ade12020-08-27 16:49:20 -0700923 bool setKeyboardLayoutOverlay(int32_t, std::shared_ptr<KeyCharacterMap>) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800924 return false;
925 }
926
Chris Yea52ade12020-08-27 16:49:20 -0700927 void vibrate(int32_t, const VibrationElement&) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800928
Chris Yea52ade12020-08-27 16:49:20 -0700929 void cancelVibrate(int32_t) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800930
Chris Ye87143712020-11-10 05:05:58 +0000931 std::vector<int32_t> getVibratorIds(int32_t deviceId) override { return mVibrators; };
932
Chris Yee2b1e5c2021-03-10 22:45:12 -0800933 std::optional<int32_t> getBatteryCapacity(int32_t, int32_t) const override {
934 return BATTERY_CAPACITY;
935 }
Kim Low03ea0352020-11-06 12:45:07 -0800936
Chris Yee2b1e5c2021-03-10 22:45:12 -0800937 std::optional<int32_t> getBatteryStatus(int32_t, int32_t) const override {
938 return BATTERY_STATUS;
939 }
940
941 const std::vector<int32_t> getRawBatteryIds(int32_t deviceId) { return {}; }
942
943 std::optional<RawBatteryInfo> getRawBatteryInfo(int32_t deviceId, int32_t batteryId) {
944 return std::nullopt;
945 }
Kim Low03ea0352020-11-06 12:45:07 -0800946
Chris Ye3fdbfef2021-01-06 18:45:18 -0800947 const std::vector<int32_t> getRawLightIds(int32_t deviceId) override {
948 std::vector<int32_t> ids;
949 for (const auto& [rawId, info] : mRawLightInfos) {
950 ids.push_back(rawId);
951 }
952 return ids;
953 }
954
955 std::optional<RawLightInfo> getRawLightInfo(int32_t deviceId, int32_t lightId) override {
956 auto it = mRawLightInfos.find(lightId);
957 if (it == mRawLightInfos.end()) {
958 return std::nullopt;
959 }
960 return it->second;
961 }
962
963 void setLightBrightness(int32_t deviceId, int32_t lightId, int32_t brightness) override {
964 mLightBrightness.emplace(lightId, brightness);
965 }
966
967 void setLightIntensities(int32_t deviceId, int32_t lightId,
968 std::unordered_map<LightColor, int32_t> intensities) override {
969 mLightIntensities.emplace(lightId, intensities);
970 };
971
972 std::optional<int32_t> getLightBrightness(int32_t deviceId, int32_t lightId) override {
973 auto lightIt = mLightBrightness.find(lightId);
974 if (lightIt == mLightBrightness.end()) {
975 return std::nullopt;
976 }
977 return lightIt->second;
978 }
979
980 std::optional<std::unordered_map<LightColor, int32_t>> getLightIntensities(
981 int32_t deviceId, int32_t lightId) override {
982 auto lightIt = mLightIntensities.find(lightId);
983 if (lightIt == mLightIntensities.end()) {
984 return std::nullopt;
985 }
986 return lightIt->second;
987 };
988
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100989 virtual bool isExternal(int32_t) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800990 return false;
991 }
992
Chris Yea52ade12020-08-27 16:49:20 -0700993 void dump(std::string&) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800994
Chris Yea52ade12020-08-27 16:49:20 -0700995 void monitor() override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800996
Chris Yea52ade12020-08-27 16:49:20 -0700997 void requestReopenDevices() override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800998
Chris Yea52ade12020-08-27 16:49:20 -0700999 void wake() override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001000};
1001
Michael Wrightd02c5b62014-02-10 15:10:22 -08001002// --- FakeInputMapper ---
1003
1004class FakeInputMapper : public InputMapper {
1005 uint32_t mSources;
1006 int32_t mKeyboardType;
1007 int32_t mMetaState;
1008 KeyedVector<int32_t, int32_t> mKeyCodeStates;
1009 KeyedVector<int32_t, int32_t> mScanCodeStates;
1010 KeyedVector<int32_t, int32_t> mSwitchStates;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001011 std::vector<int32_t> mSupportedKeyCodes;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001012
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001013 std::mutex mLock;
1014 std::condition_variable mStateChangedCondition;
1015 bool mConfigureWasCalled GUARDED_BY(mLock);
1016 bool mResetWasCalled GUARDED_BY(mLock);
1017 bool mProcessWasCalled GUARDED_BY(mLock);
1018 RawEvent mLastEvent GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001019
Arthur Hungc23540e2018-11-29 20:42:11 +08001020 std::optional<DisplayViewport> mViewport;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001021public:
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001022 FakeInputMapper(InputDeviceContext& deviceContext, uint32_t sources)
1023 : InputMapper(deviceContext),
1024 mSources(sources),
1025 mKeyboardType(AINPUT_KEYBOARD_TYPE_NONE),
Michael Wrightd02c5b62014-02-10 15:10:22 -08001026 mMetaState(0),
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001027 mConfigureWasCalled(false),
1028 mResetWasCalled(false),
1029 mProcessWasCalled(false) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001030
Chris Yea52ade12020-08-27 16:49:20 -07001031 virtual ~FakeInputMapper() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001032
1033 void setKeyboardType(int32_t keyboardType) {
1034 mKeyboardType = keyboardType;
1035 }
1036
1037 void setMetaState(int32_t metaState) {
1038 mMetaState = metaState;
1039 }
1040
1041 void assertConfigureWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001042 std::unique_lock<std::mutex> lock(mLock);
1043 base::ScopedLockAssertion assumeLocked(mLock);
1044 const bool configureCalled =
1045 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1046 return mConfigureWasCalled;
1047 });
1048 if (!configureCalled) {
1049 FAIL() << "Expected configure() to have been called.";
1050 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001051 mConfigureWasCalled = false;
1052 }
1053
1054 void assertResetWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001055 std::unique_lock<std::mutex> lock(mLock);
1056 base::ScopedLockAssertion assumeLocked(mLock);
1057 const bool resetCalled =
1058 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1059 return mResetWasCalled;
1060 });
1061 if (!resetCalled) {
1062 FAIL() << "Expected reset() to have been called.";
1063 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001064 mResetWasCalled = false;
1065 }
1066
Yi Kong9b14ac62018-07-17 13:48:38 -07001067 void assertProcessWasCalled(RawEvent* outLastEvent = nullptr) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001068 std::unique_lock<std::mutex> lock(mLock);
1069 base::ScopedLockAssertion assumeLocked(mLock);
1070 const bool processCalled =
1071 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1072 return mProcessWasCalled;
1073 });
1074 if (!processCalled) {
1075 FAIL() << "Expected process() to have been called.";
1076 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001077 if (outLastEvent) {
1078 *outLastEvent = mLastEvent;
1079 }
1080 mProcessWasCalled = false;
1081 }
1082
1083 void setKeyCodeState(int32_t keyCode, int32_t state) {
1084 mKeyCodeStates.replaceValueFor(keyCode, state);
1085 }
1086
1087 void setScanCodeState(int32_t scanCode, int32_t state) {
1088 mScanCodeStates.replaceValueFor(scanCode, state);
1089 }
1090
1091 void setSwitchState(int32_t switchCode, int32_t state) {
1092 mSwitchStates.replaceValueFor(switchCode, state);
1093 }
1094
1095 void addSupportedKeyCode(int32_t keyCode) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001096 mSupportedKeyCodes.push_back(keyCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001097 }
1098
1099private:
Chris Yea52ade12020-08-27 16:49:20 -07001100 uint32_t getSources() override { return mSources; }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001101
Chris Yea52ade12020-08-27 16:49:20 -07001102 void populateDeviceInfo(InputDeviceInfo* deviceInfo) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001103 InputMapper::populateDeviceInfo(deviceInfo);
1104
1105 if (mKeyboardType != AINPUT_KEYBOARD_TYPE_NONE) {
1106 deviceInfo->setKeyboardType(mKeyboardType);
1107 }
1108 }
1109
Chris Yea52ade12020-08-27 16:49:20 -07001110 void configure(nsecs_t, const InputReaderConfiguration* config, uint32_t changes) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001111 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001112 mConfigureWasCalled = true;
Arthur Hungc23540e2018-11-29 20:42:11 +08001113
1114 // Find the associated viewport if exist.
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001115 const std::optional<uint8_t> displayPort = getDeviceContext().getAssociatedDisplayPort();
Arthur Hungc23540e2018-11-29 20:42:11 +08001116 if (displayPort && (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
1117 mViewport = config->getDisplayViewportByPort(*displayPort);
1118 }
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001119
1120 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001121 }
1122
Chris Yea52ade12020-08-27 16:49:20 -07001123 void reset(nsecs_t) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001124 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001125 mResetWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001126 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001127 }
1128
Chris Yea52ade12020-08-27 16:49:20 -07001129 void process(const RawEvent* rawEvent) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001130 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001131 mLastEvent = *rawEvent;
1132 mProcessWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001133 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001134 }
1135
Chris Yea52ade12020-08-27 16:49:20 -07001136 int32_t getKeyCodeState(uint32_t, int32_t keyCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001137 ssize_t index = mKeyCodeStates.indexOfKey(keyCode);
1138 return index >= 0 ? mKeyCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1139 }
1140
Chris Yea52ade12020-08-27 16:49:20 -07001141 int32_t getScanCodeState(uint32_t, int32_t scanCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001142 ssize_t index = mScanCodeStates.indexOfKey(scanCode);
1143 return index >= 0 ? mScanCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1144 }
1145
Chris Yea52ade12020-08-27 16:49:20 -07001146 int32_t getSwitchState(uint32_t, int32_t switchCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001147 ssize_t index = mSwitchStates.indexOfKey(switchCode);
1148 return index >= 0 ? mSwitchStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1149 }
1150
Chris Yea52ade12020-08-27 16:49:20 -07001151 // Return true if the device has non-empty key layout.
1152 bool markSupportedKeyCodes(uint32_t, size_t numCodes, const int32_t* keyCodes,
1153 uint8_t* outFlags) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001154 for (size_t i = 0; i < numCodes; i++) {
1155 for (size_t j = 0; j < mSupportedKeyCodes.size(); j++) {
1156 if (keyCodes[i] == mSupportedKeyCodes[j]) {
1157 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001158 }
1159 }
1160 }
Chris Yea52ade12020-08-27 16:49:20 -07001161 bool result = mSupportedKeyCodes.size() > 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001162 return result;
1163 }
1164
1165 virtual int32_t getMetaState() {
1166 return mMetaState;
1167 }
1168
1169 virtual void fadePointer() {
1170 }
Arthur Hungc23540e2018-11-29 20:42:11 +08001171
1172 virtual std::optional<int32_t> getAssociatedDisplay() {
1173 if (mViewport) {
1174 return std::make_optional(mViewport->displayId);
1175 }
1176 return std::nullopt;
1177 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001178};
1179
1180
1181// --- InstrumentedInputReader ---
1182
1183class InstrumentedInputReader : public InputReader {
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001184 std::queue<std::shared_ptr<InputDevice>> mNextDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001185
1186public:
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001187 InstrumentedInputReader(std::shared_ptr<EventHubInterface> eventHub,
1188 const sp<InputReaderPolicyInterface>& policy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001189 InputListenerInterface& listener)
arthurhungdcef2dc2020-08-11 14:47:50 +08001190 : InputReader(eventHub, policy, listener), mFakeContext(this) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001191
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001192 virtual ~InstrumentedInputReader() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001193
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001194 void pushNextDevice(std::shared_ptr<InputDevice> device) { mNextDevices.push(device); }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001195
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001196 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001197 const std::string& location = "") {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001198 InputDeviceIdentifier identifier;
1199 identifier.name = name;
Arthur Hungc23540e2018-11-29 20:42:11 +08001200 identifier.location = location;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001201 int32_t generation = deviceId + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08001202 return std::make_shared<InputDevice>(&mFakeContext, deviceId, generation, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001203 }
1204
Prabir Pradhan28efc192019-11-05 01:10:04 +00001205 // Make the protected loopOnce method accessible to tests.
1206 using InputReader::loopOnce;
1207
Michael Wrightd02c5b62014-02-10 15:10:22 -08001208protected:
Chris Ye1c2e0892020-11-30 21:41:44 -08001209 virtual std::shared_ptr<InputDevice> createDeviceLocked(int32_t eventHubId,
1210 const InputDeviceIdentifier& identifier)
1211 REQUIRES(mLock) {
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001212 if (!mNextDevices.empty()) {
1213 std::shared_ptr<InputDevice> device(std::move(mNextDevices.front()));
1214 mNextDevices.pop();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001215 return device;
1216 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001217 return InputReader::createDeviceLocked(eventHubId, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001218 }
1219
arthurhungdcef2dc2020-08-11 14:47:50 +08001220 // --- FakeInputReaderContext ---
1221 class FakeInputReaderContext : public ContextImpl {
1222 int32_t mGlobalMetaState;
1223 bool mUpdateGlobalMetaStateWasCalled;
1224 int32_t mGeneration;
1225
1226 public:
1227 FakeInputReaderContext(InputReader* reader)
1228 : ContextImpl(reader),
1229 mGlobalMetaState(0),
1230 mUpdateGlobalMetaStateWasCalled(false),
1231 mGeneration(1) {}
1232
1233 virtual ~FakeInputReaderContext() {}
1234
1235 void assertUpdateGlobalMetaStateWasCalled() {
1236 ASSERT_TRUE(mUpdateGlobalMetaStateWasCalled)
1237 << "Expected updateGlobalMetaState() to have been called.";
1238 mUpdateGlobalMetaStateWasCalled = false;
1239 }
1240
1241 void setGlobalMetaState(int32_t state) { mGlobalMetaState = state; }
1242
1243 uint32_t getGeneration() { return mGeneration; }
1244
1245 void updateGlobalMetaState() override {
1246 mUpdateGlobalMetaStateWasCalled = true;
1247 ContextImpl::updateGlobalMetaState();
1248 }
1249
1250 int32_t getGlobalMetaState() override {
1251 return mGlobalMetaState | ContextImpl::getGlobalMetaState();
1252 }
1253
1254 int32_t bumpGeneration() override {
1255 mGeneration = ContextImpl::bumpGeneration();
1256 return mGeneration;
1257 }
1258 } mFakeContext;
1259
Michael Wrightd02c5b62014-02-10 15:10:22 -08001260 friend class InputReaderTest;
arthurhungdcef2dc2020-08-11 14:47:50 +08001261
1262public:
1263 FakeInputReaderContext* getContext() { return &mFakeContext; }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001264};
1265
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001266// --- InputReaderPolicyTest ---
1267class InputReaderPolicyTest : public testing::Test {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001268protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001269 sp<FakeInputReaderPolicy> mFakePolicy;
1270
Chris Yea52ade12020-08-27 16:49:20 -07001271 void SetUp() override { mFakePolicy = new FakeInputReaderPolicy(); }
1272 void TearDown() override { mFakePolicy.clear(); }
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001273};
1274
1275/**
1276 * Check that empty set of viewports is an acceptable configuration.
1277 * Also try to get internal viewport two different ways - by type and by uniqueId.
1278 *
1279 * There will be confusion if two viewports with empty uniqueId and identical type are present.
1280 * Such configuration is not currently allowed.
1281 */
1282TEST_F(InputReaderPolicyTest, Viewports_GetCleared) {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001283 static const std::string uniqueId = "local:0";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001284
1285 // We didn't add any viewports yet, so there shouldn't be any.
1286 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001287 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001288 ASSERT_FALSE(internalViewport);
1289
1290 // Add an internal viewport, then clear it
1291 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001292 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId, NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001293 ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001294
1295 // Check matching by uniqueId
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001296 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001297 ASSERT_TRUE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001298 ASSERT_EQ(ViewportType::INTERNAL, internalViewport->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001299
1300 // Check matching by viewport type
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001301 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001302 ASSERT_TRUE(internalViewport);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001303 ASSERT_EQ(uniqueId, internalViewport->uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001304
1305 mFakePolicy->clearViewports();
1306 // Make sure nothing is found after clear
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001307 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001308 ASSERT_FALSE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001309 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001310 ASSERT_FALSE(internalViewport);
1311}
1312
1313TEST_F(InputReaderPolicyTest, Viewports_GetByType) {
1314 const std::string internalUniqueId = "local:0";
1315 const std::string externalUniqueId = "local:1";
1316 const std::string virtualUniqueId1 = "virtual:2";
1317 const std::string virtualUniqueId2 = "virtual:3";
1318 constexpr int32_t virtualDisplayId1 = 2;
1319 constexpr int32_t virtualDisplayId2 = 3;
1320
1321 // Add an internal viewport
1322 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001323 DISPLAY_ORIENTATION_0, true /*isActive*/, internalUniqueId,
1324 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001325 // Add an external viewport
1326 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001327 DISPLAY_ORIENTATION_0, true /*isActive*/, externalUniqueId,
1328 NO_PORT, ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001329 // Add an virtual viewport
1330 mFakePolicy->addDisplayViewport(virtualDisplayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001331 DISPLAY_ORIENTATION_0, true /*isActive*/, virtualUniqueId1,
1332 NO_PORT, ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001333 // Add another virtual viewport
1334 mFakePolicy->addDisplayViewport(virtualDisplayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001335 DISPLAY_ORIENTATION_0, true /*isActive*/, virtualUniqueId2,
1336 NO_PORT, ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001337
1338 // Check matching by type for internal
1339 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001340 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001341 ASSERT_TRUE(internalViewport);
1342 ASSERT_EQ(internalUniqueId, internalViewport->uniqueId);
1343
1344 // Check matching by type for external
1345 std::optional<DisplayViewport> externalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001346 mFakePolicy->getDisplayViewportByType(ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001347 ASSERT_TRUE(externalViewport);
1348 ASSERT_EQ(externalUniqueId, externalViewport->uniqueId);
1349
1350 // Check matching by uniqueId for virtual viewport #1
1351 std::optional<DisplayViewport> virtualViewport1 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001352 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001353 ASSERT_TRUE(virtualViewport1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001354 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport1->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001355 ASSERT_EQ(virtualUniqueId1, virtualViewport1->uniqueId);
1356 ASSERT_EQ(virtualDisplayId1, virtualViewport1->displayId);
1357
1358 // Check matching by uniqueId for virtual viewport #2
1359 std::optional<DisplayViewport> virtualViewport2 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001360 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001361 ASSERT_TRUE(virtualViewport2);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001362 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport2->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001363 ASSERT_EQ(virtualUniqueId2, virtualViewport2->uniqueId);
1364 ASSERT_EQ(virtualDisplayId2, virtualViewport2->displayId);
1365}
1366
1367
1368/**
1369 * We can have 2 viewports of the same kind. We can distinguish them by uniqueId, and confirm
1370 * that lookup works by checking display id.
1371 * Check that 2 viewports of each kind is possible, for all existing viewport types.
1372 */
1373TEST_F(InputReaderPolicyTest, Viewports_TwoOfSameType) {
1374 const std::string uniqueId1 = "uniqueId1";
1375 const std::string uniqueId2 = "uniqueId2";
1376 constexpr int32_t displayId1 = 2;
1377 constexpr int32_t displayId2 = 3;
1378
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001379 std::vector<ViewportType> types = {ViewportType::INTERNAL, ViewportType::EXTERNAL,
1380 ViewportType::VIRTUAL};
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001381 for (const ViewportType& type : types) {
1382 mFakePolicy->clearViewports();
1383 // Add a viewport
1384 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001385 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1,
1386 NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001387 // Add another viewport
1388 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001389 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2,
1390 NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001391
1392 // Check that correct display viewport was returned by comparing the display IDs.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001393 std::optional<DisplayViewport> viewport1 =
1394 mFakePolicy->getDisplayViewportByUniqueId(uniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001395 ASSERT_TRUE(viewport1);
1396 ASSERT_EQ(displayId1, viewport1->displayId);
1397 ASSERT_EQ(type, viewport1->type);
1398
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001399 std::optional<DisplayViewport> viewport2 =
1400 mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001401 ASSERT_TRUE(viewport2);
1402 ASSERT_EQ(displayId2, viewport2->displayId);
1403 ASSERT_EQ(type, viewport2->type);
1404
1405 // When there are multiple viewports of the same kind, and uniqueId is not specified
1406 // in the call to getDisplayViewport, then that situation is not supported.
1407 // The viewports can be stored in any order, so we cannot rely on the order, since that
1408 // is just implementation detail.
1409 // However, we can check that it still returns *a* viewport, we just cannot assert
1410 // which one specifically is returned.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001411 std::optional<DisplayViewport> someViewport = mFakePolicy->getDisplayViewportByType(type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001412 ASSERT_TRUE(someViewport);
1413 }
1414}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001415
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001416/**
Michael Wrightdde67b82020-10-27 16:09:22 +00001417 * When we have multiple internal displays make sure we always return the default display when
1418 * querying by type.
1419 */
1420TEST_F(InputReaderPolicyTest, Viewports_ByTypeReturnsDefaultForInternal) {
1421 const std::string uniqueId1 = "uniqueId1";
1422 const std::string uniqueId2 = "uniqueId2";
1423 constexpr int32_t nonDefaultDisplayId = 2;
1424 static_assert(nonDefaultDisplayId != ADISPLAY_ID_DEFAULT,
1425 "Test display ID should not be ADISPLAY_ID_DEFAULT");
1426
1427 // Add the default display first and ensure it gets returned.
1428 mFakePolicy->clearViewports();
1429 mFakePolicy->addDisplayViewport(ADISPLAY_ID_DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001430 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001431 ViewportType::INTERNAL);
1432 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001433 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001434 ViewportType::INTERNAL);
1435
1436 std::optional<DisplayViewport> viewport =
1437 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
1438 ASSERT_TRUE(viewport);
1439 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
1440 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
1441
1442 // Add the default display second to make sure order doesn't matter.
1443 mFakePolicy->clearViewports();
1444 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001445 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001446 ViewportType::INTERNAL);
1447 mFakePolicy->addDisplayViewport(ADISPLAY_ID_DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001448 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001449 ViewportType::INTERNAL);
1450
1451 viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
1452 ASSERT_TRUE(viewport);
1453 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
1454 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
1455}
1456
1457/**
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001458 * Check getDisplayViewportByPort
1459 */
1460TEST_F(InputReaderPolicyTest, Viewports_GetByPort) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001461 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001462 const std::string uniqueId1 = "uniqueId1";
1463 const std::string uniqueId2 = "uniqueId2";
1464 constexpr int32_t displayId1 = 1;
1465 constexpr int32_t displayId2 = 2;
1466 const uint8_t hdmi1 = 0;
1467 const uint8_t hdmi2 = 1;
1468 const uint8_t hdmi3 = 2;
1469
1470 mFakePolicy->clearViewports();
1471 // Add a viewport that's associated with some display port that's not of interest.
1472 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001473 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, hdmi3,
1474 type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001475 // Add another viewport, connected to HDMI1 port
1476 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001477 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, hdmi1,
1478 type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001479
1480 // Check that correct display viewport was returned by comparing the display ports.
1481 std::optional<DisplayViewport> hdmi1Viewport = mFakePolicy->getDisplayViewportByPort(hdmi1);
1482 ASSERT_TRUE(hdmi1Viewport);
1483 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1484 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1485
1486 // Check that we can still get the same viewport using the uniqueId
1487 hdmi1Viewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
1488 ASSERT_TRUE(hdmi1Viewport);
1489 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1490 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1491 ASSERT_EQ(type, hdmi1Viewport->type);
1492
1493 // Check that we cannot find a port with "HDMI2", because we never added one
1494 std::optional<DisplayViewport> hdmi2Viewport = mFakePolicy->getDisplayViewportByPort(hdmi2);
1495 ASSERT_FALSE(hdmi2Viewport);
1496}
1497
Michael Wrightd02c5b62014-02-10 15:10:22 -08001498// --- InputReaderTest ---
1499
1500class InputReaderTest : public testing::Test {
1501protected:
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001502 std::unique_ptr<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001503 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001504 std::shared_ptr<FakeEventHub> mFakeEventHub;
Prabir Pradhan28efc192019-11-05 01:10:04 +00001505 std::unique_ptr<InstrumentedInputReader> mReader;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001506
Chris Yea52ade12020-08-27 16:49:20 -07001507 void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001508 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001509 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001510 mFakeListener = std::make_unique<TestInputListener>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001511
Prabir Pradhan28efc192019-11-05 01:10:04 +00001512 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001513 *mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001514 }
1515
Chris Yea52ade12020-08-27 16:49:20 -07001516 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001517 mFakeListener.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001518 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001519 }
1520
Chris Ye1b0c7342020-07-28 21:57:03 -07001521 void addDevice(int32_t eventHubId, const std::string& name, Flags<InputDeviceClass> classes,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001522 const PropertyMap* configuration) {
1523 mFakeEventHub->addDevice(eventHubId, name, classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001524
1525 if (configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001526 mFakeEventHub->addConfigurationMap(eventHubId, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001527 }
1528 mFakeEventHub->finishDeviceScan();
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001529 mReader->loopOnce();
1530 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001531 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1532 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001533 }
1534
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001535 void disableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001536 mFakePolicy->addDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001537 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001538 }
1539
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001540 void enableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001541 mFakePolicy->removeDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001542 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001543 }
1544
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001545 FakeInputMapper& addDeviceWithFakeInputMapper(int32_t deviceId, int32_t eventHubId,
Chris Ye1b0c7342020-07-28 21:57:03 -07001546 const std::string& name,
1547 Flags<InputDeviceClass> classes, uint32_t sources,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001548 const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001549 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, name);
1550 FakeInputMapper& mapper = device->addMapper<FakeInputMapper>(eventHubId, sources);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001551 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001552 addDevice(eventHubId, name, classes, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001553 return mapper;
1554 }
1555};
1556
Chris Ye98d3f532020-10-01 21:48:59 -07001557TEST_F(InputReaderTest, PolicyGetInputDevices) {
1558 ASSERT_NO_FATAL_FAILURE(addDevice(1, "keyboard", InputDeviceClass::KEYBOARD, nullptr));
1559 ASSERT_NO_FATAL_FAILURE(addDevice(2, "ignored", Flags<InputDeviceClass>(0),
1560 nullptr)); // no classes so device will be ignored
Michael Wrightd02c5b62014-02-10 15:10:22 -08001561
1562 // Should also have received a notification describing the new input devices.
Chris Ye98d3f532020-10-01 21:48:59 -07001563 const std::vector<InputDeviceInfo>& inputDevices = mFakePolicy->getInputDevices();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001564 ASSERT_EQ(1U, inputDevices.size());
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001565 ASSERT_EQ(END_RESERVED_ID + 1, inputDevices[0].getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001566 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001567 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
1568 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00001569 ASSERT_EQ(0U, inputDevices[0].getMotionRanges().size());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001570}
1571
Chris Yee7310032020-09-22 15:36:28 -07001572TEST_F(InputReaderTest, GetMergedInputDevices) {
1573 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1574 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1575 // Add two subdevices to device
1576 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1577 // Must add at least one mapper or the device will be ignored!
1578 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1579 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1580
1581 // Push same device instance for next device to be added, so they'll have same identifier.
1582 mReader->pushNextDevice(device);
1583 mReader->pushNextDevice(device);
1584 ASSERT_NO_FATAL_FAILURE(
1585 addDevice(eventHubIds[0], "fake1", InputDeviceClass::KEYBOARD, nullptr));
1586 ASSERT_NO_FATAL_FAILURE(
1587 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
1588
1589 // Two devices will be merged to one input device as they have same identifier
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00001590 ASSERT_EQ(1U, mFakePolicy->getInputDevices().size());
Chris Yee7310032020-09-22 15:36:28 -07001591}
1592
Chris Yee14523a2020-12-19 13:46:00 -08001593TEST_F(InputReaderTest, GetMergedInputDevicesEnabled) {
1594 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1595 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1596 // Add two subdevices to device
1597 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1598 // Must add at least one mapper or the device will be ignored!
1599 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1600 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1601
1602 // Push same device instance for next device to be added, so they'll have same identifier.
1603 mReader->pushNextDevice(device);
1604 mReader->pushNextDevice(device);
1605 // Sensor device is initially disabled
1606 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1",
1607 InputDeviceClass::KEYBOARD | InputDeviceClass::SENSOR,
1608 nullptr));
1609 // Device is disabled because the only sub device is a sensor device and disabled initially.
1610 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1611 ASSERT_FALSE(device->isEnabled());
1612 ASSERT_NO_FATAL_FAILURE(
1613 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
1614 // The merged device is enabled if any sub device is enabled
1615 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1616 ASSERT_TRUE(device->isEnabled());
1617}
1618
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001619TEST_F(InputReaderTest, WhenEnabledChanges_SendsDeviceResetNotification) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001620 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001621 constexpr Flags<InputDeviceClass> deviceClass(InputDeviceClass::KEYBOARD);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001622 constexpr int32_t eventHubId = 1;
1623 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001624 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001625 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001626 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001627 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001628
Yi Kong9b14ac62018-07-17 13:48:38 -07001629 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001630
1631 NotifyDeviceResetArgs resetArgs;
1632 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001633 ASSERT_EQ(deviceId, resetArgs.deviceId);
1634
1635 ASSERT_EQ(device->isEnabled(), true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001636 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001637 mReader->loopOnce();
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001638
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001639 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001640 ASSERT_EQ(deviceId, resetArgs.deviceId);
1641 ASSERT_EQ(device->isEnabled(), false);
1642
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001643 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001644 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001645 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
1646 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasNotCalled());
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001647 ASSERT_EQ(device->isEnabled(), false);
1648
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001649 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001650 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001651 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001652 ASSERT_EQ(deviceId, resetArgs.deviceId);
1653 ASSERT_EQ(device->isEnabled(), true);
1654}
1655
Michael Wrightd02c5b62014-02-10 15:10:22 -08001656TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001657 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001658 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001659 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001660 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001661 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001662 AINPUT_SOURCE_KEYBOARD, nullptr);
1663 mapper.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001664
1665 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(0,
1666 AINPUT_SOURCE_ANY, AKEYCODE_A))
1667 << "Should return unknown when the device id is >= 0 but unknown.";
1668
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001669 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1670 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1671 << "Should return unknown when the device id is valid but the sources are not "
1672 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001673
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001674 ASSERT_EQ(AKEY_STATE_DOWN,
1675 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1676 AKEYCODE_A))
1677 << "Should return value provided by mapper when device id is valid and the device "
1678 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001679
1680 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(-1,
1681 AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1682 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1683
1684 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getKeyCodeState(-1,
1685 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1686 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1687}
1688
1689TEST_F(InputReaderTest, GetScanCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001690 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001691 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001692 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001693 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001694 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001695 AINPUT_SOURCE_KEYBOARD, nullptr);
1696 mapper.setScanCodeState(KEY_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001697
1698 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(0,
1699 AINPUT_SOURCE_ANY, KEY_A))
1700 << "Should return unknown when the device id is >= 0 but unknown.";
1701
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001702 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1703 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, KEY_A))
1704 << "Should return unknown when the device id is valid but the sources are not "
1705 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001706
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001707 ASSERT_EQ(AKEY_STATE_DOWN,
1708 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1709 KEY_A))
1710 << "Should return value provided by mapper when device id is valid and the device "
1711 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001712
1713 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(-1,
1714 AINPUT_SOURCE_TRACKBALL, KEY_A))
1715 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1716
1717 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getScanCodeState(-1,
1718 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, KEY_A))
1719 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1720}
1721
1722TEST_F(InputReaderTest, GetSwitchState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001723 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001724 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001725 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001726 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001727 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001728 AINPUT_SOURCE_KEYBOARD, nullptr);
1729 mapper.setSwitchState(SW_LID, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001730
1731 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(0,
1732 AINPUT_SOURCE_ANY, SW_LID))
1733 << "Should return unknown when the device id is >= 0 but unknown.";
1734
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001735 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1736 mReader->getSwitchState(deviceId, AINPUT_SOURCE_TRACKBALL, SW_LID))
1737 << "Should return unknown when the device id is valid but the sources are not "
1738 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001739
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001740 ASSERT_EQ(AKEY_STATE_DOWN,
1741 mReader->getSwitchState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1742 SW_LID))
1743 << "Should return value provided by mapper when device id is valid and the device "
1744 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001745
1746 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(-1,
1747 AINPUT_SOURCE_TRACKBALL, SW_LID))
1748 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1749
1750 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getSwitchState(-1,
1751 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, SW_LID))
1752 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1753}
1754
1755TEST_F(InputReaderTest, MarkSupportedKeyCodes_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001756 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001757 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001758 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001759 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001760 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001761 AINPUT_SOURCE_KEYBOARD, nullptr);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001762
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001763 mapper.addSupportedKeyCode(AKEYCODE_A);
1764 mapper.addSupportedKeyCode(AKEYCODE_B);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001765
1766 const int32_t keyCodes[4] = { AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2 };
1767 uint8_t flags[4] = { 0, 0, 0, 1 };
1768
1769 ASSERT_FALSE(mReader->hasKeys(0, AINPUT_SOURCE_ANY, 4, keyCodes, flags))
1770 << "Should return false when device id is >= 0 but unknown.";
1771 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1772
1773 flags[3] = 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001774 ASSERT_FALSE(mReader->hasKeys(deviceId, AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1775 << "Should return false when device id is valid but the sources are not supported by "
1776 "the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001777 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1778
1779 flags[3] = 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001780 ASSERT_TRUE(mReader->hasKeys(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, 4,
1781 keyCodes, flags))
1782 << "Should return value provided by mapper when device id is valid and the device "
1783 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001784 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1785
1786 flags[3] = 1;
1787 ASSERT_FALSE(mReader->hasKeys(-1, AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1788 << "Should return false when the device id is < 0 but the sources are not supported by any device.";
1789 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1790
1791 flags[3] = 1;
1792 ASSERT_TRUE(mReader->hasKeys(-1, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1793 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1794 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1795}
1796
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001797TEST_F(InputReaderTest, LoopOnce_WhenDeviceScanFinished_SendsConfigurationChanged) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001798 constexpr int32_t eventHubId = 1;
Chris Ye1b0c7342020-07-28 21:57:03 -07001799 addDevice(eventHubId, "ignored", InputDeviceClass::KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001800
1801 NotifyConfigurationChangedArgs args;
1802
1803 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(&args));
1804 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
1805}
1806
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001807TEST_F(InputReaderTest, LoopOnce_ForwardsRawEventsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001808 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001809 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001810 constexpr nsecs_t when = 0;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001811 constexpr int32_t eventHubId = 1;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001812 constexpr nsecs_t readTime = 2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001813 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001814 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001815 AINPUT_SOURCE_KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001816
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001817 mFakeEventHub->enqueueEvent(when, readTime, eventHubId, EV_KEY, KEY_A, 1);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001818 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001819 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
1820
1821 RawEvent event;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001822 ASSERT_NO_FATAL_FAILURE(mapper.assertProcessWasCalled(&event));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001823 ASSERT_EQ(when, event.when);
1824 ASSERT_EQ(readTime, event.readTime);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001825 ASSERT_EQ(eventHubId, event.deviceId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001826 ASSERT_EQ(EV_KEY, event.type);
1827 ASSERT_EQ(KEY_A, event.code);
1828 ASSERT_EQ(1, event.value);
1829}
1830
Garfield Tan1c7bc862020-01-28 13:24:04 -08001831TEST_F(InputReaderTest, DeviceReset_RandomId) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001832 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001833 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001834 constexpr int32_t eventHubId = 1;
1835 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Prabir Pradhan42611e02018-11-27 14:04:02 -08001836 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001837 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001838 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001839 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan42611e02018-11-27 14:04:02 -08001840
1841 NotifyDeviceResetArgs resetArgs;
1842 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001843 int32_t prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001844
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001845 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001846 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001847 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001848 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001849 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001850
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001851 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001852 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001853 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001854 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001855 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001856
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001857 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001858 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001859 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001860 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001861 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001862}
1863
Garfield Tan1c7bc862020-01-28 13:24:04 -08001864TEST_F(InputReaderTest, DeviceReset_GenerateIdWithInputReaderSource) {
1865 constexpr int32_t deviceId = 1;
Chris Ye1b0c7342020-07-28 21:57:03 -07001866 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Garfield Tan1c7bc862020-01-28 13:24:04 -08001867 constexpr int32_t eventHubId = 1;
1868 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1869 // Must add at least one mapper or the device will be ignored!
1870 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001871 mReader->pushNextDevice(device);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001872 ASSERT_NO_FATAL_FAILURE(addDevice(deviceId, "fake", deviceClass, nullptr));
1873
1874 NotifyDeviceResetArgs resetArgs;
1875 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1876 ASSERT_EQ(IdGenerator::Source::INPUT_READER, IdGenerator::getSource(resetArgs.id));
1877}
1878
Arthur Hungc23540e2018-11-29 20:42:11 +08001879TEST_F(InputReaderTest, Device_CanDispatchToDisplay) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001880 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001881 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001882 constexpr int32_t eventHubId = 1;
Arthur Hungc23540e2018-11-29 20:42:11 +08001883 const char* DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001884 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1885 FakeInputMapper& mapper =
1886 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001887 mReader->pushNextDevice(device);
Arthur Hungc23540e2018-11-29 20:42:11 +08001888
1889 const uint8_t hdmi1 = 1;
1890
1891 // Associated touch screen with second display.
1892 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
1893
1894 // Add default and second display.
Prabir Pradhan28efc192019-11-05 01:10:04 +00001895 mFakePolicy->clearViewports();
Arthur Hungc23540e2018-11-29 20:42:11 +08001896 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001897 DISPLAY_ORIENTATION_0, true /*isActive*/, "local:0", NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001898 ViewportType::INTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08001899 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001900 DISPLAY_ORIENTATION_0, true /*isActive*/, "local:1", hdmi1,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001901 ViewportType::EXTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08001902 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001903 mReader->loopOnce();
Prabir Pradhan28efc192019-11-05 01:10:04 +00001904
1905 // Add the device, and make sure all of the callbacks are triggered.
1906 // The device is added after the input port associations are processed since
1907 // we do not yet support dynamic device-to-display associations.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001908 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001909 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled());
Prabir Pradhan28efc192019-11-05 01:10:04 +00001910 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001911 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
Arthur Hungc23540e2018-11-29 20:42:11 +08001912
Arthur Hung2c9a3342019-07-23 14:18:59 +08001913 // Device should only dispatch to the specified display.
Arthur Hungc23540e2018-11-29 20:42:11 +08001914 ASSERT_EQ(deviceId, device->getId());
1915 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, DISPLAY_ID));
1916 ASSERT_TRUE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hung2c9a3342019-07-23 14:18:59 +08001917
1918 // Can't dispatch event from a disabled device.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001919 disableDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001920 mReader->loopOnce();
Arthur Hung2c9a3342019-07-23 14:18:59 +08001921 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hungc23540e2018-11-29 20:42:11 +08001922}
1923
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001924TEST_F(InputReaderTest, WhenEnabledChanges_AllSubdevicesAreUpdated) {
1925 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1926 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
1927 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1928 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1929 // Must add at least one mapper or the device will be ignored!
1930 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1931 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1932 mReader->pushNextDevice(device);
1933 mReader->pushNextDevice(device);
1934 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
1935 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
1936
1937 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
1938
1939 NotifyDeviceResetArgs resetArgs;
1940 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1941 ASSERT_EQ(deviceId, resetArgs.deviceId);
1942 ASSERT_TRUE(device->isEnabled());
1943 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1944 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1945
1946 disableDevice(deviceId);
1947 mReader->loopOnce();
1948
1949 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1950 ASSERT_EQ(deviceId, resetArgs.deviceId);
1951 ASSERT_FALSE(device->isEnabled());
1952 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1953 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1954
1955 enableDevice(deviceId);
1956 mReader->loopOnce();
1957
1958 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1959 ASSERT_EQ(deviceId, resetArgs.deviceId);
1960 ASSERT_TRUE(device->isEnabled());
1961 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1962 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1963}
1964
1965TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToSubdeviceMappers) {
1966 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1967 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
1968 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1969 // Add two subdevices to device
1970 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1971 FakeInputMapper& mapperDevice1 =
1972 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1973 FakeInputMapper& mapperDevice2 =
1974 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1975 mReader->pushNextDevice(device);
1976 mReader->pushNextDevice(device);
1977 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
1978 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
1979
1980 mapperDevice1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
1981 mapperDevice2.setKeyCodeState(AKEYCODE_B, AKEY_STATE_DOWN);
1982
1983 ASSERT_EQ(AKEY_STATE_DOWN,
1984 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_A));
1985 ASSERT_EQ(AKEY_STATE_DOWN,
1986 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_B));
1987 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1988 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_C));
1989}
1990
Prabir Pradhan7e186182020-11-10 13:56:45 -08001991TEST_F(InputReaderTest, ChangingPointerCaptureNotifiesInputListener) {
1992 NotifyPointerCaptureChangedArgs args;
1993
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001994 auto request = mFakePolicy->setPointerCapture(true);
Prabir Pradhan7e186182020-11-10 13:56:45 -08001995 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
1996 mReader->loopOnce();
1997 mFakeListener->assertNotifyCaptureWasCalled(&args);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001998 ASSERT_TRUE(args.request.enable) << "Pointer Capture should be enabled.";
1999 ASSERT_EQ(args.request, request) << "Pointer Capture sequence number should match.";
Prabir Pradhan7e186182020-11-10 13:56:45 -08002000
2001 mFakePolicy->setPointerCapture(false);
2002 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
2003 mReader->loopOnce();
2004 mFakeListener->assertNotifyCaptureWasCalled(&args);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002005 ASSERT_FALSE(args.request.enable) << "Pointer Capture should be disabled.";
Prabir Pradhan7e186182020-11-10 13:56:45 -08002006
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002007 // Verify that the Pointer Capture state is not updated when the configuration value
Prabir Pradhan7e186182020-11-10 13:56:45 -08002008 // does not change.
2009 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
2010 mReader->loopOnce();
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002011 mFakeListener->assertNotifyCaptureWasNotCalled();
Prabir Pradhan7e186182020-11-10 13:56:45 -08002012}
2013
Chris Ye87143712020-11-10 05:05:58 +00002014class FakeVibratorInputMapper : public FakeInputMapper {
2015public:
2016 FakeVibratorInputMapper(InputDeviceContext& deviceContext, uint32_t sources)
2017 : FakeInputMapper(deviceContext, sources) {}
2018
2019 std::vector<int32_t> getVibratorIds() override { return getDeviceContext().getVibratorIds(); }
2020};
2021
2022TEST_F(InputReaderTest, VibratorGetVibratorIds) {
2023 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
2024 Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD | InputDeviceClass::VIBRATOR;
2025 constexpr int32_t eventHubId = 1;
2026 const char* DEVICE_LOCATION = "BLUETOOTH";
2027 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
2028 FakeVibratorInputMapper& mapper =
2029 device->addMapper<FakeVibratorInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
2030 mReader->pushNextDevice(device);
2031
2032 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2033 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
2034
2035 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
2036 ASSERT_EQ(mReader->getVibratorIds(deviceId).size(), 2U);
2037}
2038
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002039// --- FakePeripheralController ---
Kim Low03ea0352020-11-06 12:45:07 -08002040
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002041class FakePeripheralController : public PeripheralControllerInterface {
Chris Yee2b1e5c2021-03-10 22:45:12 -08002042public:
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002043 FakePeripheralController(InputDeviceContext& deviceContext) : mDeviceContext(deviceContext) {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08002044
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002045 ~FakePeripheralController() override {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08002046
2047 void populateDeviceInfo(InputDeviceInfo* deviceInfo) override {}
2048
2049 void dump(std::string& dump) override {}
2050
2051 std::optional<int32_t> getBatteryCapacity(int32_t batteryId) override {
2052 return getDeviceContext().getBatteryCapacity(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08002053 }
2054
Chris Yee2b1e5c2021-03-10 22:45:12 -08002055 std::optional<int32_t> getBatteryStatus(int32_t batteryId) override {
2056 return getDeviceContext().getBatteryStatus(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08002057 }
Chris Ye3fdbfef2021-01-06 18:45:18 -08002058
2059 bool setLightColor(int32_t lightId, int32_t color) override {
2060 getDeviceContext().setLightBrightness(lightId, color >> 24);
2061 return true;
2062 }
2063
2064 std::optional<int32_t> getLightColor(int32_t lightId) override {
2065 std::optional<int32_t> result = getDeviceContext().getLightBrightness(lightId);
2066 if (!result.has_value()) {
2067 return std::nullopt;
2068 }
2069 return result.value() << 24;
2070 }
Chris Yee2b1e5c2021-03-10 22:45:12 -08002071
2072 bool setLightPlayerId(int32_t lightId, int32_t playerId) override { return true; }
2073
2074 std::optional<int32_t> getLightPlayerId(int32_t lightId) override { return std::nullopt; }
2075
2076private:
2077 InputDeviceContext& mDeviceContext;
2078 inline int32_t getDeviceId() { return mDeviceContext.getId(); }
2079 inline InputDeviceContext& getDeviceContext() { return mDeviceContext; }
Chris Ye3fdbfef2021-01-06 18:45:18 -08002080};
2081
Chris Yee2b1e5c2021-03-10 22:45:12 -08002082TEST_F(InputReaderTest, BatteryGetCapacity) {
2083 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
2084 Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
2085 constexpr int32_t eventHubId = 1;
2086 const char* DEVICE_LOCATION = "BLUETOOTH";
2087 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002088 FakePeripheralController& controller =
2089 device->addController<FakePeripheralController>(eventHubId);
Chris Yee2b1e5c2021-03-10 22:45:12 -08002090 mReader->pushNextDevice(device);
2091
2092 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2093
2094 ASSERT_EQ(controller.getBatteryCapacity(DEFAULT_BATTERY), BATTERY_CAPACITY);
2095 ASSERT_EQ(mReader->getBatteryCapacity(deviceId), BATTERY_CAPACITY);
2096}
2097
2098TEST_F(InputReaderTest, BatteryGetStatus) {
2099 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
2100 Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
2101 constexpr int32_t eventHubId = 1;
2102 const char* DEVICE_LOCATION = "BLUETOOTH";
2103 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002104 FakePeripheralController& controller =
2105 device->addController<FakePeripheralController>(eventHubId);
Chris Yee2b1e5c2021-03-10 22:45:12 -08002106 mReader->pushNextDevice(device);
2107
2108 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2109
2110 ASSERT_EQ(controller.getBatteryStatus(DEFAULT_BATTERY), BATTERY_STATUS);
2111 ASSERT_EQ(mReader->getBatteryStatus(deviceId), BATTERY_STATUS);
2112}
2113
Chris Ye3fdbfef2021-01-06 18:45:18 -08002114TEST_F(InputReaderTest, LightGetColor) {
2115 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
2116 Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD | InputDeviceClass::LIGHT;
2117 constexpr int32_t eventHubId = 1;
2118 const char* DEVICE_LOCATION = "BLUETOOTH";
2119 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002120 FakePeripheralController& controller =
2121 device->addController<FakePeripheralController>(eventHubId);
Chris Ye3fdbfef2021-01-06 18:45:18 -08002122 mReader->pushNextDevice(device);
2123 RawLightInfo info = {.id = 1,
2124 .name = "Mono",
2125 .maxBrightness = 255,
2126 .flags = InputLightClass::BRIGHTNESS,
2127 .path = ""};
2128 mFakeEventHub->addRawLightInfo(1 /* rawId */, std::move(info));
2129 mFakeEventHub->fakeLightBrightness(1 /* rawId */, 0x55);
2130
2131 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Chris Ye3fdbfef2021-01-06 18:45:18 -08002132
Chris Yee2b1e5c2021-03-10 22:45:12 -08002133 ASSERT_TRUE(controller.setLightColor(1 /* lightId */, LIGHT_BRIGHTNESS));
2134 ASSERT_EQ(controller.getLightColor(1 /* lightId */), LIGHT_BRIGHTNESS);
Chris Ye3fdbfef2021-01-06 18:45:18 -08002135 ASSERT_TRUE(mReader->setLightColor(deviceId, 1 /* lightId */, LIGHT_BRIGHTNESS));
2136 ASSERT_EQ(mReader->getLightColor(deviceId, 1 /* lightId */), LIGHT_BRIGHTNESS);
2137}
2138
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002139// --- InputReaderIntegrationTest ---
2140
2141// These tests create and interact with the InputReader only through its interface.
2142// The InputReader is started during SetUp(), which starts its processing in its own
2143// thread. The tests use linux uinput to emulate input devices.
2144// NOTE: Interacting with the physical device while these tests are running may cause
2145// the tests to fail.
2146class InputReaderIntegrationTest : public testing::Test {
2147protected:
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002148 std::unique_ptr<TestInputListener> mTestListener;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002149 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002150 std::unique_ptr<InputReaderInterface> mReader;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002151
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00002152 std::shared_ptr<FakePointerController> mFakePointerController;
2153
Chris Yea52ade12020-08-27 16:49:20 -07002154 void SetUp() override {
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002155 mFakePolicy = new FakeInputReaderPolicy();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00002156 mFakePointerController = std::make_shared<FakePointerController>();
2157 mFakePolicy->setPointerController(mFakePointerController);
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002158 mTestListener = std::make_unique<TestInputListener>(2000ms /*eventHappenedTimeout*/,
2159 30ms /*eventDidNotHappenTimeout*/);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002160
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002161 mReader = std::make_unique<InputReader>(std::make_shared<EventHub>(), mFakePolicy,
2162 *mTestListener);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002163 ASSERT_EQ(mReader->start(), OK);
2164
2165 // Since this test is run on a real device, all the input devices connected
2166 // to the test device will show up in mReader. We wait for those input devices to
2167 // show up before beginning the tests.
2168 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2169 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2170 }
2171
Chris Yea52ade12020-08-27 16:49:20 -07002172 void TearDown() override {
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002173 ASSERT_EQ(mReader->stop(), OK);
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002174 mReader.reset();
2175 mTestListener.reset();
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002176 mFakePolicy.clear();
2177 }
2178};
2179
2180TEST_F(InputReaderIntegrationTest, TestInvalidDevice) {
2181 // An invalid input device that is only used for this test.
2182 class InvalidUinputDevice : public UinputDevice {
2183 public:
2184 InvalidUinputDevice() : UinputDevice("Invalid Device") {}
2185
2186 private:
2187 void configureDevice(int fd, uinput_user_dev* device) override {}
2188 };
2189
2190 const size_t numDevices = mFakePolicy->getInputDevices().size();
2191
2192 // UinputDevice does not set any event or key bits, so InputReader should not
2193 // consider it as a valid device.
2194 std::unique_ptr<UinputDevice> invalidDevice = createUinputDevice<InvalidUinputDevice>();
2195 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
2196 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
2197 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
2198
2199 invalidDevice.reset();
2200 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
2201 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
2202 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
2203}
2204
2205TEST_F(InputReaderIntegrationTest, AddNewDevice) {
2206 const size_t initialNumDevices = mFakePolicy->getInputDevices().size();
2207
2208 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
2209 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2210 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2211 ASSERT_EQ(initialNumDevices + 1, mFakePolicy->getInputDevices().size());
2212
2213 // Find the test device by its name.
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002214 const std::vector<InputDeviceInfo> inputDevices = mFakePolicy->getInputDevices();
Chris Ye98d3f532020-10-01 21:48:59 -07002215 const auto& it =
2216 std::find_if(inputDevices.begin(), inputDevices.end(),
2217 [&keyboard](const InputDeviceInfo& info) {
2218 return info.getIdentifier().name == keyboard->getName();
2219 });
2220
2221 ASSERT_NE(it, inputDevices.end());
2222 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, it->getKeyboardType());
2223 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, it->getSources());
2224 ASSERT_EQ(0U, it->getMotionRanges().size());
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002225
2226 keyboard.reset();
2227 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2228 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2229 ASSERT_EQ(initialNumDevices, mFakePolicy->getInputDevices().size());
2230}
2231
2232TEST_F(InputReaderIntegrationTest, SendsEventsToInputListener) {
2233 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
2234 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2235
2236 NotifyConfigurationChangedArgs configChangedArgs;
2237 ASSERT_NO_FATAL_FAILURE(
2238 mTestListener->assertNotifyConfigurationChangedWasCalled(&configChangedArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002239 int32_t prevId = configChangedArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002240 nsecs_t prevTimestamp = configChangedArgs.eventTime;
2241
2242 NotifyKeyArgs keyArgs;
2243 keyboard->pressAndReleaseHomeKey();
2244 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
2245 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002246 ASSERT_NE(prevId, keyArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002247 prevId = keyArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002248 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002249 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002250 prevTimestamp = keyArgs.eventTime;
2251
2252 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
2253 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002254 ASSERT_NE(prevId, keyArgs.id);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002255 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002256 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002257}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002258
Siarhei Vishniakoua0d2b802020-05-13 14:00:31 -07002259/**
2260 * The Steam controller sends BTN_GEAR_DOWN and BTN_GEAR_UP for the two "paddle" buttons
2261 * on the back. In this test, we make sure that BTN_GEAR_DOWN / BTN_WHEEL and BTN_GEAR_UP
2262 * are passed to the listener.
2263 */
2264static_assert(BTN_GEAR_DOWN == BTN_WHEEL);
2265TEST_F(InputReaderIntegrationTest, SendsGearDownAndUpToInputListener) {
2266 std::unique_ptr<UinputSteamController> controller = createUinputDevice<UinputSteamController>();
2267 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2268 NotifyKeyArgs keyArgs;
2269
2270 controller->pressAndReleaseKey(BTN_GEAR_DOWN);
2271 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
2272 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
2273 ASSERT_EQ(BTN_GEAR_DOWN, keyArgs.scanCode);
2274
2275 controller->pressAndReleaseKey(BTN_GEAR_UP);
2276 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
2277 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
2278 ASSERT_EQ(BTN_GEAR_UP, keyArgs.scanCode);
2279}
2280
Arthur Hungaab25622020-01-16 11:22:11 +08002281// --- TouchProcessTest ---
2282class TouchIntegrationTest : public InputReaderIntegrationTest {
2283protected:
Arthur Hungaab25622020-01-16 11:22:11 +08002284 const std::string UNIQUE_ID = "local:0";
2285
Chris Yea52ade12020-08-27 16:49:20 -07002286 void SetUp() override {
Arthur Hungaab25622020-01-16 11:22:11 +08002287 InputReaderIntegrationTest::SetUp();
2288 // At least add an internal display.
2289 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2290 DISPLAY_ORIENTATION_0, UNIQUE_ID, NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002291 ViewportType::INTERNAL);
Arthur Hungaab25622020-01-16 11:22:11 +08002292
2293 mDevice = createUinputDevice<UinputTouchScreen>(Rect(0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT));
2294 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2295 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2296 }
2297
2298 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
2299 int32_t orientation, const std::string& uniqueId,
2300 std::optional<uint8_t> physicalPort,
2301 ViewportType viewportType) {
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002302 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, true /*isActive*/,
2303 uniqueId, physicalPort, viewportType);
Arthur Hungaab25622020-01-16 11:22:11 +08002304 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2305 }
2306
2307 std::unique_ptr<UinputTouchScreen> mDevice;
2308};
2309
2310TEST_F(TouchIntegrationTest, InputEvent_ProcessSingleTouch) {
2311 NotifyMotionArgs args;
2312 const Point centerPoint = mDevice->getCenterPoint();
2313
2314 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00002315 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002316 mDevice->sendDown(centerPoint);
2317 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2318 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2319
2320 // ACTION_MOVE
2321 mDevice->sendMove(centerPoint + Point(1, 1));
2322 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2323 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2324
2325 // ACTION_UP
2326 mDevice->sendUp();
2327 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2328 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2329}
2330
2331TEST_F(TouchIntegrationTest, InputEvent_ProcessMultiTouch) {
2332 NotifyMotionArgs args;
2333 const Point centerPoint = mDevice->getCenterPoint();
2334
2335 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00002336 mDevice->sendSlot(FIRST_SLOT);
2337 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002338 mDevice->sendDown(centerPoint);
2339 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2340 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2341
2342 // ACTION_POINTER_DOWN (Second slot)
2343 const Point secondPoint = centerPoint + Point(100, 100);
2344 mDevice->sendSlot(SECOND_SLOT);
2345 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2346 mDevice->sendDown(secondPoint + Point(1, 1));
2347 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2348 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
2349 args.action);
2350
2351 // ACTION_MOVE (Second slot)
2352 mDevice->sendMove(secondPoint);
2353 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2354 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2355
2356 // ACTION_POINTER_UP (Second slot)
arthurhungcc7f9802020-04-30 17:55:40 +08002357 mDevice->sendPointerUp();
Arthur Hungaab25622020-01-16 11:22:11 +08002358 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
arthurhungcc7f9802020-04-30 17:55:40 +08002359 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
Arthur Hungaab25622020-01-16 11:22:11 +08002360 args.action);
2361
2362 // ACTION_UP
2363 mDevice->sendSlot(FIRST_SLOT);
2364 mDevice->sendUp();
2365 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2366 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2367}
2368
2369TEST_F(TouchIntegrationTest, InputEvent_ProcessPalm) {
2370 NotifyMotionArgs args;
2371 const Point centerPoint = mDevice->getCenterPoint();
2372
2373 // ACTION_DOWN
arthurhungcc7f9802020-04-30 17:55:40 +08002374 mDevice->sendSlot(FIRST_SLOT);
2375 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002376 mDevice->sendDown(centerPoint);
2377 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2378 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2379
arthurhungcc7f9802020-04-30 17:55:40 +08002380 // ACTION_POINTER_DOWN (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002381 const Point secondPoint = centerPoint + Point(100, 100);
2382 mDevice->sendSlot(SECOND_SLOT);
2383 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2384 mDevice->sendDown(secondPoint);
2385 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2386 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
2387 args.action);
2388
arthurhungcc7f9802020-04-30 17:55:40 +08002389 // ACTION_MOVE (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002390 mDevice->sendMove(secondPoint + Point(1, 1));
2391 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2392 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2393
arthurhungcc7f9802020-04-30 17:55:40 +08002394 // Send MT_TOOL_PALM (second slot), which indicates that the touch IC has determined this to be
2395 // a palm event.
2396 // Expect to receive the ACTION_POINTER_UP with cancel flag.
Arthur Hungaab25622020-01-16 11:22:11 +08002397 mDevice->sendToolType(MT_TOOL_PALM);
2398 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
arthurhungcc7f9802020-04-30 17:55:40 +08002399 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
2400 args.action);
2401 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, args.flags);
Arthur Hungaab25622020-01-16 11:22:11 +08002402
arthurhungcc7f9802020-04-30 17:55:40 +08002403 // Send up to second slot, expect first slot send moving.
2404 mDevice->sendPointerUp();
2405 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2406 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002407
arthurhungcc7f9802020-04-30 17:55:40 +08002408 // Send ACTION_UP (first slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002409 mDevice->sendSlot(FIRST_SLOT);
2410 mDevice->sendUp();
2411
arthurhungcc7f9802020-04-30 17:55:40 +08002412 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2413 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002414}
2415
Michael Wrightd02c5b62014-02-10 15:10:22 -08002416// --- InputDeviceTest ---
Michael Wrightd02c5b62014-02-10 15:10:22 -08002417class InputDeviceTest : public testing::Test {
2418protected:
2419 static const char* DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002420 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002421 static const int32_t DEVICE_ID;
2422 static const int32_t DEVICE_GENERATION;
2423 static const int32_t DEVICE_CONTROLLER_NUMBER;
Chris Ye1b0c7342020-07-28 21:57:03 -07002424 static const Flags<InputDeviceClass> DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002425 static const int32_t EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002426
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002427 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002428 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002429 std::unique_ptr<TestInputListener> mFakeListener;
arthurhungdcef2dc2020-08-11 14:47:50 +08002430 std::unique_ptr<InstrumentedInputReader> mReader;
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00002431 std::shared_ptr<InputDevice> mDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002432
Chris Yea52ade12020-08-27 16:49:20 -07002433 void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002434 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002435 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002436 mFakeListener = std::make_unique<TestInputListener>();
arthurhungdcef2dc2020-08-11 14:47:50 +08002437 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002438 *mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002439 InputDeviceIdentifier identifier;
2440 identifier.name = DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002441 identifier.location = DEVICE_LOCATION;
arthurhungdcef2dc2020-08-11 14:47:50 +08002442 mDevice = std::make_shared<InputDevice>(mReader->getContext(), DEVICE_ID, DEVICE_GENERATION,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002443 identifier);
arthurhungdcef2dc2020-08-11 14:47:50 +08002444 mReader->pushNextDevice(mDevice);
2445 mFakeEventHub->addDevice(EVENTHUB_ID, DEVICE_NAME, Flags<InputDeviceClass>(0));
2446 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002447 }
2448
Chris Yea52ade12020-08-27 16:49:20 -07002449 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002450 mFakeListener.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002451 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002452 }
2453};
2454
2455const char* InputDeviceTest::DEVICE_NAME = "device";
Arthur Hung2c9a3342019-07-23 14:18:59 +08002456const char* InputDeviceTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002457const int32_t InputDeviceTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002458const int32_t InputDeviceTest::DEVICE_GENERATION = 2;
2459const int32_t InputDeviceTest::DEVICE_CONTROLLER_NUMBER = 0;
Chris Ye1b0c7342020-07-28 21:57:03 -07002460const Flags<InputDeviceClass> InputDeviceTest::DEVICE_CLASSES =
2461 InputDeviceClass::KEYBOARD | InputDeviceClass::TOUCH | InputDeviceClass::JOYSTICK;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002462const int32_t InputDeviceTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002463
2464TEST_F(InputDeviceTest, ImmutableProperties) {
2465 ASSERT_EQ(DEVICE_ID, mDevice->getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002466 ASSERT_STREQ(DEVICE_NAME, mDevice->getName().c_str());
Chris Ye1b0c7342020-07-28 21:57:03 -07002467 ASSERT_EQ(Flags<InputDeviceClass>(0), mDevice->getClasses());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002468}
2469
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002470TEST_F(InputDeviceTest, WhenDeviceCreated_EnabledIsFalse) {
2471 ASSERT_EQ(mDevice->isEnabled(), false);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07002472}
2473
Michael Wrightd02c5b62014-02-10 15:10:22 -08002474TEST_F(InputDeviceTest, WhenNoMappersAreRegistered_DeviceIsIgnored) {
2475 // Configuration.
2476 InputReaderConfiguration config;
2477 mDevice->configure(ARBITRARY_TIME, &config, 0);
2478
2479 // Reset.
2480 mDevice->reset(ARBITRARY_TIME);
2481
2482 NotifyDeviceResetArgs resetArgs;
2483 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2484 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2485 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2486
2487 // Metadata.
2488 ASSERT_TRUE(mDevice->isIgnored());
2489 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mDevice->getSources());
2490
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002491 InputDeviceInfo info = mDevice->getDeviceInfo();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002492 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002493 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002494 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NONE, info.getKeyboardType());
2495 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, info.getSources());
2496
2497 // State queries.
2498 ASSERT_EQ(0, mDevice->getMetaState());
2499
2500 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2501 << "Ignored device should return unknown key code state.";
2502 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2503 << "Ignored device should return unknown scan code state.";
2504 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 0))
2505 << "Ignored device should return unknown switch state.";
2506
2507 const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
2508 uint8_t flags[2] = { 0, 1 };
2509 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, 2, keyCodes, flags))
2510 << "Ignored device should never mark any key codes.";
2511 ASSERT_EQ(0, flags[0]) << "Flag for unsupported key should be unchanged.";
2512 ASSERT_EQ(1, flags[1]) << "Flag for unsupported key should be unchanged.";
2513}
2514
2515TEST_F(InputDeviceTest, WhenMappersAreRegistered_DeviceIsNotIgnoredAndForwardsRequestsToMappers) {
2516 // Configuration.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002517 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, String8("key"), String8("value"));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002518
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002519 FakeInputMapper& mapper1 =
2520 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002521 mapper1.setKeyboardType(AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2522 mapper1.setMetaState(AMETA_ALT_ON);
2523 mapper1.addSupportedKeyCode(AKEYCODE_A);
2524 mapper1.addSupportedKeyCode(AKEYCODE_B);
2525 mapper1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
2526 mapper1.setKeyCodeState(AKEYCODE_B, AKEY_STATE_UP);
2527 mapper1.setScanCodeState(2, AKEY_STATE_DOWN);
2528 mapper1.setScanCodeState(3, AKEY_STATE_UP);
2529 mapper1.setSwitchState(4, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002530
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002531 FakeInputMapper& mapper2 =
2532 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002533 mapper2.setMetaState(AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002534
2535 InputReaderConfiguration config;
2536 mDevice->configure(ARBITRARY_TIME, &config, 0);
2537
2538 String8 propertyValue;
2539 ASSERT_TRUE(mDevice->getConfiguration().tryGetProperty(String8("key"), propertyValue))
2540 << "Device should have read configuration during configuration phase.";
2541 ASSERT_STREQ("value", propertyValue.string());
2542
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002543 ASSERT_NO_FATAL_FAILURE(mapper1.assertConfigureWasCalled());
2544 ASSERT_NO_FATAL_FAILURE(mapper2.assertConfigureWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002545
2546 // Reset
2547 mDevice->reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002548 ASSERT_NO_FATAL_FAILURE(mapper1.assertResetWasCalled());
2549 ASSERT_NO_FATAL_FAILURE(mapper2.assertResetWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002550
2551 NotifyDeviceResetArgs resetArgs;
2552 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2553 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2554 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2555
2556 // Metadata.
2557 ASSERT_FALSE(mDevice->isIgnored());
2558 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), mDevice->getSources());
2559
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002560 InputDeviceInfo info = mDevice->getDeviceInfo();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002561 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002562 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002563 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_ALPHABETIC, info.getKeyboardType());
2564 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), info.getSources());
2565
2566 // State queries.
2567 ASSERT_EQ(AMETA_ALT_ON | AMETA_SHIFT_ON, mDevice->getMetaState())
2568 << "Should query mappers and combine meta states.";
2569
2570 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2571 << "Should return unknown key code state when source not supported.";
2572 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2573 << "Should return unknown scan code state when source not supported.";
2574 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2575 << "Should return unknown switch state when source not supported.";
2576
2577 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, AKEYCODE_A))
2578 << "Should query mapper when source is supported.";
2579 ASSERT_EQ(AKEY_STATE_UP, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 3))
2580 << "Should query mapper when source is supported.";
2581 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 4))
2582 << "Should query mapper when source is supported.";
2583
2584 const int32_t keyCodes[4] = { AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2 };
2585 uint8_t flags[4] = { 0, 0, 0, 1 };
2586 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
2587 << "Should do nothing when source is unsupported.";
2588 ASSERT_EQ(0, flags[0]) << "Flag should be unchanged when source is unsupported.";
2589 ASSERT_EQ(0, flags[1]) << "Flag should be unchanged when source is unsupported.";
2590 ASSERT_EQ(0, flags[2]) << "Flag should be unchanged when source is unsupported.";
2591 ASSERT_EQ(1, flags[3]) << "Flag should be unchanged when source is unsupported.";
2592
2593 ASSERT_TRUE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, 4, keyCodes, flags))
2594 << "Should query mapper when source is supported.";
2595 ASSERT_EQ(1, flags[0]) << "Flag for supported key should be set.";
2596 ASSERT_EQ(1, flags[1]) << "Flag for supported key should be set.";
2597 ASSERT_EQ(0, flags[2]) << "Flag for unsupported key should be unchanged.";
2598 ASSERT_EQ(1, flags[3]) << "Flag for unsupported key should be unchanged.";
2599
2600 // Event handling.
2601 RawEvent event;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002602 event.deviceId = EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002603 mDevice->process(&event, 1);
2604
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002605 ASSERT_NO_FATAL_FAILURE(mapper1.assertProcessWasCalled());
2606 ASSERT_NO_FATAL_FAILURE(mapper2.assertProcessWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002607}
2608
Arthur Hung2c9a3342019-07-23 14:18:59 +08002609// A single input device is associated with a specific display. Check that:
2610// 1. Device is disabled if the viewport corresponding to the associated display is not found
2611// 2. Device is disabled when setEnabled API is called
2612TEST_F(InputDeviceTest, Configure_AssignsDisplayPort) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002613 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002614
2615 // First Configuration.
2616 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
2617
2618 // Device should be enabled by default.
2619 ASSERT_TRUE(mDevice->isEnabled());
2620
2621 // Prepare associated info.
2622 constexpr uint8_t hdmi = 1;
2623 const std::string UNIQUE_ID = "local:1";
2624
2625 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi);
2626 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2627 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2628 // Device should be disabled because it is associated with a specific display via
2629 // input port <-> display port association, but the corresponding display is not found
2630 ASSERT_FALSE(mDevice->isEnabled());
2631
2632 // Prepare displays.
2633 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002634 DISPLAY_ORIENTATION_0, true /*isActive*/, UNIQUE_ID, hdmi,
2635 ViewportType::INTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002636 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2637 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2638 ASSERT_TRUE(mDevice->isEnabled());
2639
2640 // Device should be disabled after set disable.
2641 mFakePolicy->addDisabledDevice(mDevice->getId());
2642 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2643 InputReaderConfiguration::CHANGE_ENABLED_STATE);
2644 ASSERT_FALSE(mDevice->isEnabled());
2645
2646 // Device should still be disabled even found the associated display.
2647 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2648 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2649 ASSERT_FALSE(mDevice->isEnabled());
2650}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002651
Christine Franks1ba71cc2021-04-07 14:37:42 -07002652TEST_F(InputDeviceTest, Configure_AssignsDisplayUniqueId) {
2653 // Device should be enabled by default.
2654 mFakePolicy->clearViewports();
2655 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
2656 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
2657 ASSERT_TRUE(mDevice->isEnabled());
2658
2659 // Device should be disabled because it is associated with a specific display, but the
2660 // corresponding display is not found.
2661 const std::string DISPLAY_UNIQUE_ID = "displayUniqueId";
2662 mFakePolicy->addInputUniqueIdAssociation(DEVICE_NAME, DISPLAY_UNIQUE_ID);
2663 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2664 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2665 ASSERT_FALSE(mDevice->isEnabled());
2666
2667 // Device should be enabled when a display is found.
2668 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2669 DISPLAY_ORIENTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
2670 NO_PORT, ViewportType::INTERNAL);
2671 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2672 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2673 ASSERT_TRUE(mDevice->isEnabled());
2674
2675 // Device should be disabled after set disable.
2676 mFakePolicy->addDisabledDevice(mDevice->getId());
2677 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2678 InputReaderConfiguration::CHANGE_ENABLED_STATE);
2679 ASSERT_FALSE(mDevice->isEnabled());
2680
2681 // Device should still be disabled even found the associated display.
2682 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2683 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2684 ASSERT_FALSE(mDevice->isEnabled());
2685}
2686
Michael Wrightd02c5b62014-02-10 15:10:22 -08002687// --- InputMapperTest ---
2688
2689class InputMapperTest : public testing::Test {
2690protected:
2691 static const char* DEVICE_NAME;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002692 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002693 static const int32_t DEVICE_ID;
2694 static const int32_t DEVICE_GENERATION;
2695 static const int32_t DEVICE_CONTROLLER_NUMBER;
Chris Ye1b0c7342020-07-28 21:57:03 -07002696 static const Flags<InputDeviceClass> DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002697 static const int32_t EVENTHUB_ID;
Prabir Pradhanc14266f2021-05-12 15:56:24 -07002698 static const std::optional<bool> INITIAL_PER_WINDOW_INPUT_ROTATION_FLAG_VALUE;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002699
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002700 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002701 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002702 std::unique_ptr<TestInputListener> mFakeListener;
arthurhungdcef2dc2020-08-11 14:47:50 +08002703 std::unique_ptr<InstrumentedInputReader> mReader;
2704 std::shared_ptr<InputDevice> mDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002705
Chris Ye1b0c7342020-07-28 21:57:03 -07002706 virtual void SetUp(Flags<InputDeviceClass> classes) {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002707 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002708 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002709 mFakeListener = std::make_unique<TestInputListener>();
arthurhungdcef2dc2020-08-11 14:47:50 +08002710 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002711 *mFakeListener);
arthurhungdcef2dc2020-08-11 14:47:50 +08002712 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002713 }
2714
Prabir Pradhanc14266f2021-05-12 15:56:24 -07002715 void SetUp() override {
2716 // Ensure per_window_input_rotation is enabled.
2717 sysprop::InputFlingerProperties::per_window_input_rotation(true);
2718
2719 SetUp(DEVICE_CLASSES);
2720 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002721
Chris Yea52ade12020-08-27 16:49:20 -07002722 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002723 mFakeListener.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002724 mFakePolicy.clear();
Prabir Pradhanc14266f2021-05-12 15:56:24 -07002725
2726 sysprop::InputFlingerProperties::per_window_input_rotation(
2727 INITIAL_PER_WINDOW_INPUT_ROTATION_FLAG_VALUE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002728 }
2729
2730 void addConfigurationProperty(const char* key, const char* value) {
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 }
2733
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002734 void configureDevice(uint32_t changes) {
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -08002735 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
arthurhungdcef2dc2020-08-11 14:47:50 +08002736 mReader->requestRefreshConfiguration(changes);
2737 mReader->loopOnce();
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -08002738 }
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002739 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
2740 }
2741
arthurhungdcef2dc2020-08-11 14:47:50 +08002742 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
2743 const std::string& location, int32_t eventHubId,
2744 Flags<InputDeviceClass> classes) {
2745 InputDeviceIdentifier identifier;
2746 identifier.name = name;
2747 identifier.location = location;
2748 std::shared_ptr<InputDevice> device =
2749 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
2750 identifier);
2751 mReader->pushNextDevice(device);
2752 mFakeEventHub->addDevice(eventHubId, name, classes);
2753 mReader->loopOnce();
2754 return device;
2755 }
2756
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002757 template <class T, typename... Args>
2758 T& addMapperAndConfigure(Args... args) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002759 T& mapper = mDevice->addMapper<T>(EVENTHUB_ID, args...);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002760 configureDevice(0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002761 mDevice->reset(ARBITRARY_TIME);
Chris Ye42b06822020-08-07 11:39:33 -07002762 mapper.reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002763 return mapper;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002764 }
2765
2766 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002767 int32_t orientation, const std::string& uniqueId,
2768 std::optional<uint8_t> physicalPort, ViewportType viewportType) {
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002769 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, true /*isActive*/,
2770 uniqueId, physicalPort, viewportType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07002771 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2772 }
2773
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002774 void clearViewports() {
2775 mFakePolicy->clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002776 }
2777
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002778 void process(InputMapper& mapper, nsecs_t when, nsecs_t readTime, int32_t type, int32_t code,
2779 int32_t value) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002780 RawEvent event;
2781 event.when = when;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002782 event.readTime = readTime;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002783 event.deviceId = mapper.getDeviceContext().getEventHubId();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002784 event.type = type;
2785 event.code = code;
2786 event.value = value;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002787 mapper.process(&event);
arthurhungdcef2dc2020-08-11 14:47:50 +08002788 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002789 }
2790
2791 static void assertMotionRange(const InputDeviceInfo& info,
2792 int32_t axis, uint32_t source, float min, float max, float flat, float fuzz) {
2793 const InputDeviceInfo::MotionRange* range = info.getMotionRange(axis, source);
Yi Kong9b14ac62018-07-17 13:48:38 -07002794 ASSERT_TRUE(range != nullptr) << "Axis: " << axis << " Source: " << source;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002795 ASSERT_EQ(axis, range->axis) << "Axis: " << axis << " Source: " << source;
2796 ASSERT_EQ(source, range->source) << "Axis: " << axis << " Source: " << source;
2797 ASSERT_NEAR(min, range->min, EPSILON) << "Axis: " << axis << " Source: " << source;
2798 ASSERT_NEAR(max, range->max, EPSILON) << "Axis: " << axis << " Source: " << source;
2799 ASSERT_NEAR(flat, range->flat, EPSILON) << "Axis: " << axis << " Source: " << source;
2800 ASSERT_NEAR(fuzz, range->fuzz, EPSILON) << "Axis: " << axis << " Source: " << source;
2801 }
2802
Prabir Pradhanf5334b82021-05-13 14:00:39 -07002803 static void assertPointerCoords(const PointerCoords& coords, float x, float y, float pressure,
2804 float size, float touchMajor, float touchMinor, float toolMajor,
2805 float toolMinor, float orientation, float distance,
2806 float scaledAxisEpsilon = 1.f) {
2807 ASSERT_NEAR(x, coords.getAxisValue(AMOTION_EVENT_AXIS_X), scaledAxisEpsilon);
2808 ASSERT_NEAR(y, coords.getAxisValue(AMOTION_EVENT_AXIS_Y), scaledAxisEpsilon);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002809 ASSERT_NEAR(pressure, coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), EPSILON);
2810 ASSERT_NEAR(size, coords.getAxisValue(AMOTION_EVENT_AXIS_SIZE), EPSILON);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07002811 ASSERT_NEAR(touchMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR),
2812 scaledAxisEpsilon);
2813 ASSERT_NEAR(touchMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR),
2814 scaledAxisEpsilon);
2815 ASSERT_NEAR(toolMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR),
2816 scaledAxisEpsilon);
2817 ASSERT_NEAR(toolMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR),
2818 scaledAxisEpsilon);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002819 ASSERT_NEAR(orientation, coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION), EPSILON);
2820 ASSERT_NEAR(distance, coords.getAxisValue(AMOTION_EVENT_AXIS_DISTANCE), EPSILON);
2821 }
2822
Michael Wright17db18e2020-06-26 20:51:44 +01002823 static void assertPosition(const FakePointerController& controller, float x, float y) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002824 float actualX, actualY;
Michael Wright17db18e2020-06-26 20:51:44 +01002825 controller.getPosition(&actualX, &actualY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002826 ASSERT_NEAR(x, actualX, 1);
2827 ASSERT_NEAR(y, actualY, 1);
2828 }
2829};
2830
2831const char* InputMapperTest::DEVICE_NAME = "device";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002832const char* InputMapperTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002833const int32_t InputMapperTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002834const int32_t InputMapperTest::DEVICE_GENERATION = 2;
2835const int32_t InputMapperTest::DEVICE_CONTROLLER_NUMBER = 0;
Chris Ye1b0c7342020-07-28 21:57:03 -07002836const Flags<InputDeviceClass> InputMapperTest::DEVICE_CLASSES =
2837 Flags<InputDeviceClass>(0); // not needed for current tests
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002838const int32_t InputMapperTest::EVENTHUB_ID = 1;
Prabir Pradhanc14266f2021-05-12 15:56:24 -07002839const std::optional<bool> InputMapperTest::INITIAL_PER_WINDOW_INPUT_ROTATION_FLAG_VALUE =
2840 sysprop::InputFlingerProperties::per_window_input_rotation();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002841
2842// --- SwitchInputMapperTest ---
2843
2844class SwitchInputMapperTest : public InputMapperTest {
2845protected:
2846};
2847
2848TEST_F(SwitchInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002849 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002850
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002851 ASSERT_EQ(uint32_t(AINPUT_SOURCE_SWITCH), mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002852}
2853
2854TEST_F(SwitchInputMapperTest, GetSwitchState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002855 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002856
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002857 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002858 ASSERT_EQ(1, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002859
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002860 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002861 ASSERT_EQ(0, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002862}
2863
2864TEST_F(SwitchInputMapperTest, Process) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002865 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002866
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002867 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_LID, 1);
2868 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_JACK_PHYSICAL_INSERT, 1);
2869 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_HEADPHONE_INSERT, 0);
2870 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002871
2872 NotifySwitchArgs args;
2873 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySwitchWasCalled(&args));
2874 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
Dan Albert1bd2fc02016-02-02 15:11:57 -08002875 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT), args.switchValues);
2876 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT) | (1 << SW_HEADPHONE_INSERT),
Michael Wrightd02c5b62014-02-10 15:10:22 -08002877 args.switchMask);
2878 ASSERT_EQ(uint32_t(0), args.policyFlags);
2879}
2880
Chris Ye87143712020-11-10 05:05:58 +00002881// --- VibratorInputMapperTest ---
2882class VibratorInputMapperTest : public InputMapperTest {
2883protected:
2884 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::VIBRATOR); }
2885};
2886
2887TEST_F(VibratorInputMapperTest, GetSources) {
2888 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
2889
2890 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mapper.getSources());
2891}
2892
2893TEST_F(VibratorInputMapperTest, GetVibratorIds) {
2894 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
2895
2896 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
2897}
2898
2899TEST_F(VibratorInputMapperTest, Vibrate) {
2900 constexpr uint8_t DEFAULT_AMPLITUDE = 192;
Chris Yefb552902021-02-03 17:18:37 -08002901 constexpr int32_t VIBRATION_TOKEN = 100;
Chris Ye87143712020-11-10 05:05:58 +00002902 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
2903
2904 VibrationElement pattern(2);
2905 VibrationSequence sequence(2);
2906 pattern.duration = std::chrono::milliseconds(200);
2907 pattern.channels = {{0 /* vibratorId */, DEFAULT_AMPLITUDE / 2},
2908 {1 /* vibratorId */, DEFAULT_AMPLITUDE}};
2909 sequence.addElement(pattern);
2910 pattern.duration = std::chrono::milliseconds(500);
2911 pattern.channels = {{0 /* vibratorId */, DEFAULT_AMPLITUDE / 4},
2912 {1 /* vibratorId */, DEFAULT_AMPLITUDE}};
2913 sequence.addElement(pattern);
2914
2915 std::vector<int64_t> timings = {0, 1};
2916 std::vector<uint8_t> amplitudes = {DEFAULT_AMPLITUDE, DEFAULT_AMPLITUDE / 2};
2917
2918 ASSERT_FALSE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08002919 // Start vibrating
2920 mapper.vibrate(sequence, -1 /* repeat */, VIBRATION_TOKEN);
Chris Ye87143712020-11-10 05:05:58 +00002921 ASSERT_TRUE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08002922 // Verify vibrator state listener was notified.
2923 mReader->loopOnce();
2924 NotifyVibratorStateArgs args;
2925 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyVibratorStateWasCalled(&args));
2926 ASSERT_EQ(DEVICE_ID, args.deviceId);
2927 ASSERT_TRUE(args.isOn);
2928 // Stop vibrating
2929 mapper.cancelVibrate(VIBRATION_TOKEN);
2930 ASSERT_FALSE(mapper.isVibrating());
2931 // Verify vibrator state listener was notified.
2932 mReader->loopOnce();
2933 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyVibratorStateWasCalled(&args));
2934 ASSERT_EQ(DEVICE_ID, args.deviceId);
2935 ASSERT_FALSE(args.isOn);
Chris Ye87143712020-11-10 05:05:58 +00002936}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002937
Chris Yef59a2f42020-10-16 12:55:26 -07002938// --- SensorInputMapperTest ---
2939
2940class SensorInputMapperTest : public InputMapperTest {
2941protected:
2942 static const int32_t ACCEL_RAW_MIN;
2943 static const int32_t ACCEL_RAW_MAX;
2944 static const int32_t ACCEL_RAW_FUZZ;
2945 static const int32_t ACCEL_RAW_FLAT;
2946 static const int32_t ACCEL_RAW_RESOLUTION;
2947
2948 static const int32_t GYRO_RAW_MIN;
2949 static const int32_t GYRO_RAW_MAX;
2950 static const int32_t GYRO_RAW_FUZZ;
2951 static const int32_t GYRO_RAW_FLAT;
2952 static const int32_t GYRO_RAW_RESOLUTION;
2953
2954 static const float GRAVITY_MS2_UNIT;
2955 static const float DEGREE_RADIAN_UNIT;
2956
2957 void prepareAccelAxes();
2958 void prepareGyroAxes();
2959 void setAccelProperties();
2960 void setGyroProperties();
2961 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::SENSOR); }
2962};
2963
2964const int32_t SensorInputMapperTest::ACCEL_RAW_MIN = -32768;
2965const int32_t SensorInputMapperTest::ACCEL_RAW_MAX = 32768;
2966const int32_t SensorInputMapperTest::ACCEL_RAW_FUZZ = 16;
2967const int32_t SensorInputMapperTest::ACCEL_RAW_FLAT = 0;
2968const int32_t SensorInputMapperTest::ACCEL_RAW_RESOLUTION = 8192;
2969
2970const int32_t SensorInputMapperTest::GYRO_RAW_MIN = -2097152;
2971const int32_t SensorInputMapperTest::GYRO_RAW_MAX = 2097152;
2972const int32_t SensorInputMapperTest::GYRO_RAW_FUZZ = 16;
2973const int32_t SensorInputMapperTest::GYRO_RAW_FLAT = 0;
2974const int32_t SensorInputMapperTest::GYRO_RAW_RESOLUTION = 1024;
2975
2976const float SensorInputMapperTest::GRAVITY_MS2_UNIT = 9.80665f;
2977const float SensorInputMapperTest::DEGREE_RADIAN_UNIT = 0.0174533f;
2978
2979void SensorInputMapperTest::prepareAccelAxes() {
2980 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
2981 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
2982 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
2983 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
2984 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Z, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
2985 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
2986}
2987
2988void SensorInputMapperTest::prepareGyroAxes() {
2989 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RX, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
2990 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
2991 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RY, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
2992 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
2993 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RZ, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
2994 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
2995}
2996
2997void SensorInputMapperTest::setAccelProperties() {
2998 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 0, InputDeviceSensorType::ACCELEROMETER,
2999 /* sensorDataIndex */ 0);
3000 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 1, InputDeviceSensorType::ACCELEROMETER,
3001 /* sensorDataIndex */ 1);
3002 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 2, InputDeviceSensorType::ACCELEROMETER,
3003 /* sensorDataIndex */ 2);
3004 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
3005 addConfigurationProperty("sensor.accelerometer.reportingMode", "0");
3006 addConfigurationProperty("sensor.accelerometer.maxDelay", "100000");
3007 addConfigurationProperty("sensor.accelerometer.minDelay", "5000");
3008 addConfigurationProperty("sensor.accelerometer.power", "1.5");
3009}
3010
3011void SensorInputMapperTest::setGyroProperties() {
3012 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 3, InputDeviceSensorType::GYROSCOPE,
3013 /* sensorDataIndex */ 0);
3014 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 4, InputDeviceSensorType::GYROSCOPE,
3015 /* sensorDataIndex */ 1);
3016 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 5, InputDeviceSensorType::GYROSCOPE,
3017 /* sensorDataIndex */ 2);
3018 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
3019 addConfigurationProperty("sensor.gyroscope.reportingMode", "0");
3020 addConfigurationProperty("sensor.gyroscope.maxDelay", "100000");
3021 addConfigurationProperty("sensor.gyroscope.minDelay", "5000");
3022 addConfigurationProperty("sensor.gyroscope.power", "0.8");
3023}
3024
3025TEST_F(SensorInputMapperTest, GetSources) {
3026 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3027
3028 ASSERT_EQ(static_cast<uint32_t>(AINPUT_SOURCE_SENSOR), mapper.getSources());
3029}
3030
3031TEST_F(SensorInputMapperTest, ProcessAccelerometerSensor) {
3032 setAccelProperties();
3033 prepareAccelAxes();
3034 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3035
3036 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::ACCELEROMETER,
3037 std::chrono::microseconds(10000),
3038 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08003039 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003040 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, 20000);
3041 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, -20000);
3042 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Z, 40000);
3043 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
3044 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07003045
3046 NotifySensorArgs args;
3047 std::vector<float> values = {20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
3048 -20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
3049 40000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT};
3050
3051 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
3052 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
3053 ASSERT_EQ(args.deviceId, DEVICE_ID);
3054 ASSERT_EQ(args.sensorType, InputDeviceSensorType::ACCELEROMETER);
3055 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
3056 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
3057 ASSERT_EQ(args.values, values);
3058 mapper.flushSensor(InputDeviceSensorType::ACCELEROMETER);
3059}
3060
3061TEST_F(SensorInputMapperTest, ProcessGyroscopeSensor) {
3062 setGyroProperties();
3063 prepareGyroAxes();
3064 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3065
3066 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::GYROSCOPE,
3067 std::chrono::microseconds(10000),
3068 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08003069 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003070 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RX, 20000);
3071 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RY, -20000);
3072 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RZ, 40000);
3073 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
3074 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07003075
3076 NotifySensorArgs args;
3077 std::vector<float> values = {20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
3078 -20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
3079 40000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT};
3080
3081 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
3082 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
3083 ASSERT_EQ(args.deviceId, DEVICE_ID);
3084 ASSERT_EQ(args.sensorType, InputDeviceSensorType::GYROSCOPE);
3085 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
3086 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
3087 ASSERT_EQ(args.values, values);
3088 mapper.flushSensor(InputDeviceSensorType::GYROSCOPE);
3089}
3090
Michael Wrightd02c5b62014-02-10 15:10:22 -08003091// --- KeyboardInputMapperTest ---
3092
3093class KeyboardInputMapperTest : public InputMapperTest {
3094protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003095 const std::string UNIQUE_ID = "local:0";
3096
3097 void prepareDisplay(int32_t orientation);
3098
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003099 void testDPadKeyRotation(KeyboardInputMapper& mapper, int32_t originalScanCode,
Arthur Hung2c9a3342019-07-23 14:18:59 +08003100 int32_t originalKeyCode, int32_t rotatedKeyCode,
3101 int32_t displayId = ADISPLAY_ID_NONE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003102};
3103
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003104/* Similar to setDisplayInfoAndReconfigure, but pre-populates all parameters except for the
3105 * orientation.
3106 */
3107void KeyboardInputMapperTest::prepareDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003108 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
3109 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003110}
3111
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003112void KeyboardInputMapperTest::testDPadKeyRotation(KeyboardInputMapper& mapper,
Arthur Hung2c9a3342019-07-23 14:18:59 +08003113 int32_t originalScanCode, int32_t originalKeyCode,
3114 int32_t rotatedKeyCode, int32_t displayId) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003115 NotifyKeyArgs args;
3116
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003117 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003118 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3119 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3120 ASSERT_EQ(originalScanCode, args.scanCode);
3121 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003122 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003123
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003124 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003125 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3126 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3127 ASSERT_EQ(originalScanCode, args.scanCode);
3128 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003129 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003130}
3131
Michael Wrightd02c5b62014-02-10 15:10:22 -08003132TEST_F(KeyboardInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003133 KeyboardInputMapper& mapper =
3134 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3135 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003136
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003137 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003138}
3139
3140TEST_F(KeyboardInputMapperTest, Process_SimpleKeyPress) {
3141 const int32_t USAGE_A = 0x070004;
3142 const int32_t USAGE_UNKNOWN = 0x07ffff;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003143 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3144 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
Chris Yea52ade12020-08-27 16:49:20 -07003145 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, POLICY_FLAG_WAKE);
3146 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, POLICY_FLAG_WAKE);
3147 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003148
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003149 KeyboardInputMapper& mapper =
3150 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3151 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
arthurhungc903df12020-08-11 15:08:42 +08003152 // Initial metastate to AMETA_NONE.
3153 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
3154 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003155
3156 // Key down by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003157 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003158 NotifyKeyArgs args;
3159 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3160 ASSERT_EQ(DEVICE_ID, args.deviceId);
3161 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3162 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3163 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3164 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3165 ASSERT_EQ(KEY_HOME, args.scanCode);
3166 ASSERT_EQ(AMETA_NONE, args.metaState);
3167 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3168 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3169 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3170
3171 // Key up by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003172 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003173 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3174 ASSERT_EQ(DEVICE_ID, args.deviceId);
3175 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3176 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3177 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3178 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3179 ASSERT_EQ(KEY_HOME, args.scanCode);
3180 ASSERT_EQ(AMETA_NONE, args.metaState);
3181 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3182 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3183 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3184
3185 // Key down by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003186 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3187 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, 0, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003188 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3189 ASSERT_EQ(DEVICE_ID, args.deviceId);
3190 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3191 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3192 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3193 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3194 ASSERT_EQ(0, args.scanCode);
3195 ASSERT_EQ(AMETA_NONE, args.metaState);
3196 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3197 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3198 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3199
3200 // Key up by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003201 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3202 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003203 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3204 ASSERT_EQ(DEVICE_ID, args.deviceId);
3205 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3206 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3207 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3208 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3209 ASSERT_EQ(0, args.scanCode);
3210 ASSERT_EQ(AMETA_NONE, args.metaState);
3211 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3212 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3213 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3214
3215 // Key down with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003216 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3217 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UNKNOWN, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003218 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3219 ASSERT_EQ(DEVICE_ID, args.deviceId);
3220 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3221 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3222 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3223 ASSERT_EQ(0, args.keyCode);
3224 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3225 ASSERT_EQ(AMETA_NONE, args.metaState);
3226 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3227 ASSERT_EQ(0U, args.policyFlags);
3228 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3229
3230 // Key up with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003231 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3232 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003233 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3234 ASSERT_EQ(DEVICE_ID, args.deviceId);
3235 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3236 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3237 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3238 ASSERT_EQ(0, args.keyCode);
3239 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3240 ASSERT_EQ(AMETA_NONE, args.metaState);
3241 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3242 ASSERT_EQ(0U, args.policyFlags);
3243 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3244}
3245
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003246/**
3247 * Ensure that the readTime is set to the time when the EV_KEY is received.
3248 */
3249TEST_F(KeyboardInputMapperTest, Process_SendsReadTime) {
3250 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3251
3252 KeyboardInputMapper& mapper =
3253 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3254 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3255 NotifyKeyArgs args;
3256
3257 // Key down
3258 process(mapper, ARBITRARY_TIME, 12 /*readTime*/, EV_KEY, KEY_HOME, 1);
3259 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3260 ASSERT_EQ(12, args.readTime);
3261
3262 // Key up
3263 process(mapper, ARBITRARY_TIME, 15 /*readTime*/, EV_KEY, KEY_HOME, 1);
3264 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3265 ASSERT_EQ(15, args.readTime);
3266}
3267
Michael Wrightd02c5b62014-02-10 15:10:22 -08003268TEST_F(KeyboardInputMapperTest, Process_ShouldUpdateMetaState) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003269 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFTSHIFT, 0, AKEYCODE_SHIFT_LEFT, 0);
3270 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003271 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, 0);
3272 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, 0);
3273 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003274
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003275 KeyboardInputMapper& mapper =
3276 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3277 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003278
arthurhungc903df12020-08-11 15:08:42 +08003279 // Initial metastate to AMETA_NONE.
3280 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
3281 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003282
3283 // Metakey down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003284 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003285 NotifyKeyArgs args;
3286 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3287 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003288 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003289 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003290
3291 // Key down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003292 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_A, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003293 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3294 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003295 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003296
3297 // Key up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003298 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, KEY_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003299 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3300 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003301 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003302
3303 // Metakey up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003304 process(mapper, ARBITRARY_TIME + 3, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003305 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3306 ASSERT_EQ(AMETA_NONE, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003307 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003308 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003309}
3310
3311TEST_F(KeyboardInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003312 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3313 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3314 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3315 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003316
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003317 KeyboardInputMapper& mapper =
3318 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3319 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003320
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003321 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003322 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3323 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
3324 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3325 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
3326 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3327 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
3328 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3329 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
3330}
3331
3332TEST_F(KeyboardInputMapperTest, Process_WhenOrientationAware_ShouldRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003333 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3334 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3335 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3336 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003337
Michael Wrightd02c5b62014-02-10 15:10:22 -08003338 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003339 KeyboardInputMapper& mapper =
3340 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3341 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003342
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003343 prepareDisplay(DISPLAY_ORIENTATION_0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003344 ASSERT_NO_FATAL_FAILURE(
3345 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3346 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3347 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3348 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3349 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3350 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3351 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003352
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003353 clearViewports();
3354 prepareDisplay(DISPLAY_ORIENTATION_90);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003355 ASSERT_NO_FATAL_FAILURE(
3356 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3357 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3358 AKEYCODE_DPAD_UP, DISPLAY_ID));
3359 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3360 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3361 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3362 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003363
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003364 clearViewports();
3365 prepareDisplay(DISPLAY_ORIENTATION_180);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003366 ASSERT_NO_FATAL_FAILURE(
3367 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3368 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3369 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3370 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3371 AKEYCODE_DPAD_UP, DISPLAY_ID));
3372 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3373 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003374
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003375 clearViewports();
3376 prepareDisplay(DISPLAY_ORIENTATION_270);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003377 ASSERT_NO_FATAL_FAILURE(
3378 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3379 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3380 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3381 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3382 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3383 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3384 AKEYCODE_DPAD_UP, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003385
3386 // Special case: if orientation changes while key is down, we still emit the same keycode
3387 // in the key up as we did in the key down.
3388 NotifyKeyArgs args;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003389 clearViewports();
3390 prepareDisplay(DISPLAY_ORIENTATION_270);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003391 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003392 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3393 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3394 ASSERT_EQ(KEY_UP, args.scanCode);
3395 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3396
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003397 clearViewports();
3398 prepareDisplay(DISPLAY_ORIENTATION_180);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003399 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003400 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3401 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3402 ASSERT_EQ(KEY_UP, args.scanCode);
3403 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3404}
3405
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003406TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_NotOrientationAware) {
3407 // If the keyboard is not orientation aware,
3408 // key events should not be associated with a specific display id
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003409 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003410
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003411 KeyboardInputMapper& mapper =
3412 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3413 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003414 NotifyKeyArgs args;
3415
3416 // Display id should be ADISPLAY_ID_NONE without any display configuration.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003417 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003418 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003419 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003420 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3421 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3422
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003423 prepareDisplay(DISPLAY_ORIENTATION_0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003424 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003425 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003426 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003427 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3428 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3429}
3430
3431TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_OrientationAware) {
3432 // If the keyboard is orientation aware,
3433 // key events should be associated with the internal viewport
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003434 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003435
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003436 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003437 KeyboardInputMapper& mapper =
3438 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3439 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003440 NotifyKeyArgs args;
3441
3442 // Display id should be ADISPLAY_ID_NONE without any display configuration.
3443 // ^--- already checked by the previous test
3444
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003445 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003446 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003447 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003448 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003449 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003450 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3451 ASSERT_EQ(DISPLAY_ID, args.displayId);
3452
3453 constexpr int32_t newDisplayId = 2;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003454 clearViewports();
3455 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003456 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003457 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003458 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003459 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003460 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3461 ASSERT_EQ(newDisplayId, args.displayId);
3462}
3463
Michael Wrightd02c5b62014-02-10 15:10:22 -08003464TEST_F(KeyboardInputMapperTest, GetKeyCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003465 KeyboardInputMapper& mapper =
3466 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3467 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003468
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003469 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003470 ASSERT_EQ(1, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003471
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003472 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003473 ASSERT_EQ(0, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003474}
3475
3476TEST_F(KeyboardInputMapperTest, GetScanCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003477 KeyboardInputMapper& mapper =
3478 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3479 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003480
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003481 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003482 ASSERT_EQ(1, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003483
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003484 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003485 ASSERT_EQ(0, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003486}
3487
3488TEST_F(KeyboardInputMapperTest, MarkSupportedKeyCodes) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003489 KeyboardInputMapper& mapper =
3490 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3491 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003492
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003493 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003494
3495 const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
3496 uint8_t flags[2] = { 0, 0 };
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003497 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, 1, keyCodes, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003498 ASSERT_TRUE(flags[0]);
3499 ASSERT_FALSE(flags[1]);
3500}
3501
3502TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleMetaStateAndLeds) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003503 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3504 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3505 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3506 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3507 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3508 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003509
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003510 KeyboardInputMapper& mapper =
3511 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3512 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Chris Yea52ade12020-08-27 16:49:20 -07003513 // Initialize metastate to AMETA_NUM_LOCK_ON.
arthurhungc903df12020-08-11 15:08:42 +08003514 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
3515 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003516
3517 // Initialization should have turned all of the lights off.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003518 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3519 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3520 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003521
3522 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003523 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3524 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003525 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3526 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3527 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003528 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003529
3530 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003531 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3532 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003533 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3534 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3535 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003536 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003537
3538 // Toggle caps lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003539 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3540 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003541 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3542 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3543 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003544 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003545
3546 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003547 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3548 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003549 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3550 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3551 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003552 ASSERT_EQ(AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003553
3554 // Toggle num lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003555 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3556 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003557 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3558 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3559 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003560 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003561
3562 // Toggle scroll lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003563 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3564 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003565 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3566 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3567 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003568 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003569}
3570
Chris Yea52ade12020-08-27 16:49:20 -07003571TEST_F(KeyboardInputMapperTest, NoMetaStateWhenMetaKeysNotPresent) {
3572 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_BUTTON_A, 0);
3573 mFakeEventHub->addKey(EVENTHUB_ID, BTN_B, 0, AKEYCODE_BUTTON_B, 0);
3574 mFakeEventHub->addKey(EVENTHUB_ID, BTN_X, 0, AKEYCODE_BUTTON_X, 0);
3575 mFakeEventHub->addKey(EVENTHUB_ID, BTN_Y, 0, AKEYCODE_BUTTON_Y, 0);
3576
3577 KeyboardInputMapper& mapper =
3578 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3579 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
3580
3581 // Initial metastate should be AMETA_NONE as no meta keys added.
3582 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3583 // Meta state should be AMETA_NONE after reset
3584 mapper.reset(ARBITRARY_TIME);
3585 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3586 // Meta state should be AMETA_NONE with update, as device doesn't have the keys.
3587 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
3588 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3589
3590 NotifyKeyArgs args;
3591 // Press button "A"
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003592 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_A, 1);
Chris Yea52ade12020-08-27 16:49:20 -07003593 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3594 ASSERT_EQ(AMETA_NONE, args.metaState);
3595 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3596 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3597 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3598
3599 // Button up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003600 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003601 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3602 ASSERT_EQ(AMETA_NONE, args.metaState);
3603 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3604 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3605 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3606}
3607
Arthur Hung2c9a3342019-07-23 14:18:59 +08003608TEST_F(KeyboardInputMapperTest, Configure_AssignsDisplayPort) {
3609 // keyboard 1.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003610 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3611 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3612 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3613 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003614
3615 // keyboard 2.
3616 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08003617 const std::string DEVICE_NAME2 = "KEYBOARD2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08003618 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003619 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08003620 std::shared_ptr<InputDevice> device2 =
3621 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
3622 Flags<InputDeviceClass>(0));
3623
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003624 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3625 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3626 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3627 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003628
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003629 KeyboardInputMapper& mapper =
3630 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3631 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003632
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003633 KeyboardInputMapper& mapper2 =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003634 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003635 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003636 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
3637 device2->reset(ARBITRARY_TIME);
3638
3639 // Prepared displays and associated info.
3640 constexpr uint8_t hdmi1 = 0;
3641 constexpr uint8_t hdmi2 = 1;
3642 const std::string SECONDARY_UNIQUE_ID = "local:1";
3643
3644 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
3645 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
3646
3647 // No associated display viewport found, should disable the device.
3648 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3649 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3650 ASSERT_FALSE(device2->isEnabled());
3651
3652 // Prepare second display.
3653 constexpr int32_t newDisplayId = 2;
3654 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003655 UNIQUE_ID, hdmi1, ViewportType::INTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003656 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003657 SECONDARY_UNIQUE_ID, hdmi2, ViewportType::EXTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003658 // Default device will reconfigure above, need additional reconfiguration for another device.
3659 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3660 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3661
3662 // Device should be enabled after the associated display is found.
3663 ASSERT_TRUE(mDevice->isEnabled());
3664 ASSERT_TRUE(device2->isEnabled());
3665
3666 // Test pad key events
3667 ASSERT_NO_FATAL_FAILURE(
3668 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3669 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3670 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3671 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3672 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3673 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3674 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3675
3676 ASSERT_NO_FATAL_FAILURE(
3677 testDPadKeyRotation(mapper2, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, newDisplayId));
3678 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3679 AKEYCODE_DPAD_RIGHT, newDisplayId));
3680 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3681 AKEYCODE_DPAD_DOWN, newDisplayId));
3682 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3683 AKEYCODE_DPAD_LEFT, newDisplayId));
3684}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003685
arthurhungc903df12020-08-11 15:08:42 +08003686TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleAfterReattach) {
3687 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3688 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3689 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3690 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3691 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3692 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3693
3694 KeyboardInputMapper& mapper =
3695 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3696 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3697 // Initial metastate to AMETA_NONE.
3698 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
3699 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
3700
3701 // Initialization should have turned all of the lights off.
3702 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3703 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3704 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3705
3706 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003707 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3708 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003709 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3710 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
3711
3712 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003713 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3714 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003715 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3716 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
3717
3718 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003719 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3720 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003721 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3722 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
3723
3724 mFakeEventHub->removeDevice(EVENTHUB_ID);
3725 mReader->loopOnce();
3726
3727 // keyboard 2 should default toggle keys.
3728 const std::string USB2 = "USB2";
3729 const std::string DEVICE_NAME2 = "KEYBOARD2";
3730 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
3731 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
3732 std::shared_ptr<InputDevice> device2 =
3733 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
3734 Flags<InputDeviceClass>(0));
3735 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3736 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
3737 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3738 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3739 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3740 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3741
arthurhung6fe95782020-10-05 22:41:16 +08003742 KeyboardInputMapper& mapper2 =
3743 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
3744 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
arthurhungc903df12020-08-11 15:08:42 +08003745 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
3746 device2->reset(ARBITRARY_TIME);
3747
3748 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_CAPSL));
3749 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_NUML));
3750 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_SCROLLL));
arthurhung6fe95782020-10-05 22:41:16 +08003751 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON,
3752 mapper2.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08003753}
3754
Arthur Hungcb40a002021-08-03 14:31:01 +00003755TEST_F(KeyboardInputMapperTest, Process_toggleCapsLockState) {
3756 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3757 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3758 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3759
3760 // Suppose we have two mappers. (DPAD + KEYBOARD)
3761 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_DPAD,
3762 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
3763 KeyboardInputMapper& mapper =
3764 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3765 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3766 // Initialize metastate to AMETA_NUM_LOCK_ON.
3767 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
3768 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
3769
3770 mReader->toggleCapsLockState(DEVICE_ID);
3771 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
3772}
3773
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003774// --- KeyboardInputMapperTest_ExternalDevice ---
3775
3776class KeyboardInputMapperTest_ExternalDevice : public InputMapperTest {
3777protected:
Chris Yea52ade12020-08-27 16:49:20 -07003778 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003779};
3780
3781TEST_F(KeyboardInputMapperTest_ExternalDevice, WakeBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07003782 // For external devices, non-media keys will trigger wake on key down. Media keys need to be
3783 // marked as WAKE in the keylayout file to trigger wake.
Powei Fengd041c5d2019-05-03 17:11:33 -07003784
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003785 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, 0);
3786 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, 0);
3787 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAYPAUSE, 0, AKEYCODE_MEDIA_PLAY_PAUSE,
3788 POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07003789
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003790 KeyboardInputMapper& mapper =
3791 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3792 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07003793
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003794 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003795 NotifyKeyArgs args;
3796 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3797 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3798
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003799 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003800 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3801 ASSERT_EQ(uint32_t(0), args.policyFlags);
3802
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003803 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003804 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3805 ASSERT_EQ(uint32_t(0), args.policyFlags);
3806
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003807 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003808 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3809 ASSERT_EQ(uint32_t(0), args.policyFlags);
3810
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003811 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003812 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3813 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3814
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003815 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003816 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3817 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3818}
3819
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003820TEST_F(KeyboardInputMapperTest_ExternalDevice, DoNotWakeByDefaultBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07003821 // Tv Remote key's wake behavior is prescribed by the keylayout file.
Powei Fengd041c5d2019-05-03 17:11:33 -07003822
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003823 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3824 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3825 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07003826
Powei Fengd041c5d2019-05-03 17:11:33 -07003827 addConfigurationProperty("keyboard.doNotWakeByDefault", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003828 KeyboardInputMapper& mapper =
3829 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3830 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07003831
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003832 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003833 NotifyKeyArgs args;
3834 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3835 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3836
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003837 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003838 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3839 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3840
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003841 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_DOWN, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003842 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3843 ASSERT_EQ(uint32_t(0), args.policyFlags);
3844
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003845 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_DOWN, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003846 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3847 ASSERT_EQ(uint32_t(0), args.policyFlags);
3848
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003849 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003850 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3851 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3852
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003853 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003854 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3855 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3856}
3857
Michael Wrightd02c5b62014-02-10 15:10:22 -08003858// --- CursorInputMapperTest ---
3859
3860class CursorInputMapperTest : public InputMapperTest {
3861protected:
3862 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD;
3863
Michael Wright17db18e2020-06-26 20:51:44 +01003864 std::shared_ptr<FakePointerController> mFakePointerController;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003865
Chris Yea52ade12020-08-27 16:49:20 -07003866 void SetUp() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003867 InputMapperTest::SetUp();
3868
Michael Wright17db18e2020-06-26 20:51:44 +01003869 mFakePointerController = std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00003870 mFakePolicy->setPointerController(mFakePointerController);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003871 }
3872
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003873 void testMotionRotation(CursorInputMapper& mapper, int32_t originalX, int32_t originalY,
3874 int32_t rotatedX, int32_t rotatedY);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003875
3876 void prepareDisplay(int32_t orientation) {
3877 const std::string uniqueId = "local:0";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003878 const ViewportType viewportType = ViewportType::INTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003879 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
3880 orientation, uniqueId, NO_PORT, viewportType);
3881 }
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003882
3883 static void assertCursorPointerCoords(const PointerCoords& coords, float x, float y,
3884 float pressure) {
3885 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(coords, x, y, pressure, 0.0f, 0.0f, 0.0f, 0.0f,
3886 0.0f, 0.0f, 0.0f, EPSILON));
3887 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08003888};
3889
3890const int32_t CursorInputMapperTest::TRACKBALL_MOVEMENT_THRESHOLD = 6;
3891
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003892void CursorInputMapperTest::testMotionRotation(CursorInputMapper& mapper, int32_t originalX,
3893 int32_t originalY, int32_t rotatedX,
3894 int32_t rotatedY) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003895 NotifyMotionArgs args;
3896
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003897 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, originalX);
3898 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, originalY);
3899 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003900 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3901 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003902 ASSERT_NO_FATAL_FAILURE(
3903 assertCursorPointerCoords(args.pointerCoords[0],
3904 float(rotatedX) / TRACKBALL_MOVEMENT_THRESHOLD,
3905 float(rotatedY) / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003906}
3907
3908TEST_F(CursorInputMapperTest, WhenModeIsPointer_GetSources_ReturnsMouse) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003909 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003910 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003911
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003912 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003913}
3914
3915TEST_F(CursorInputMapperTest, WhenModeIsNavigation_GetSources_ReturnsTrackball) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003916 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003917 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003918
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003919 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003920}
3921
3922TEST_F(CursorInputMapperTest, WhenModeIsPointer_PopulateDeviceInfo_ReturnsRangeFromPointerController) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003923 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003924 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003925
3926 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003927 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003928
3929 // Initially there may not be a valid motion range.
Yi Kong9b14ac62018-07-17 13:48:38 -07003930 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE));
3931 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003932 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3933 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE, 0.0f, 1.0f, 0.0f, 0.0f));
3934
3935 // When the bounds are set, then there should be a valid motion range.
3936 mFakePointerController->setBounds(1, 2, 800 - 1, 480 - 1);
3937
3938 InputDeviceInfo info2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003939 mapper.populateDeviceInfo(&info2);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003940
3941 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3942 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE,
3943 1, 800 - 1, 0.0f, 0.0f));
3944 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3945 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE,
3946 2, 480 - 1, 0.0f, 0.0f));
3947 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3948 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE,
3949 0.0f, 1.0f, 0.0f, 0.0f));
3950}
3951
3952TEST_F(CursorInputMapperTest, WhenModeIsNavigation_PopulateDeviceInfo_ReturnsScaledRange) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003953 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003954 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003955
3956 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003957 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003958
3959 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3960 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_TRACKBALL,
3961 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
3962 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3963 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_TRACKBALL,
3964 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
3965 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3966 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TRACKBALL,
3967 0.0f, 1.0f, 0.0f, 0.0f));
3968}
3969
3970TEST_F(CursorInputMapperTest, Process_ShouldSetAllFieldsAndIncludeGlobalMetaState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003971 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003972 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003973
arthurhungdcef2dc2020-08-11 14:47:50 +08003974 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003975
3976 NotifyMotionArgs args;
3977
3978 // Button press.
3979 // Mostly testing non x/y behavior here so we don't need to check again elsewhere.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003980 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
3981 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003982 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3983 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3984 ASSERT_EQ(DEVICE_ID, args.deviceId);
3985 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3986 ASSERT_EQ(uint32_t(0), args.policyFlags);
3987 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3988 ASSERT_EQ(0, args.flags);
3989 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3990 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
3991 ASSERT_EQ(0, args.edgeFlags);
3992 ASSERT_EQ(uint32_t(1), args.pointerCount);
3993 ASSERT_EQ(0, args.pointerProperties[0].id);
3994 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003995 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003996 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3997 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3998 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3999
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004000 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4001 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4002 ASSERT_EQ(DEVICE_ID, args.deviceId);
4003 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4004 ASSERT_EQ(uint32_t(0), args.policyFlags);
4005 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
4006 ASSERT_EQ(0, args.flags);
4007 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4008 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
4009 ASSERT_EQ(0, args.edgeFlags);
4010 ASSERT_EQ(uint32_t(1), args.pointerCount);
4011 ASSERT_EQ(0, args.pointerProperties[0].id);
4012 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004013 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004014 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4015 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4016 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4017
Michael Wrightd02c5b62014-02-10 15:10:22 -08004018 // Button release. Should have same down time.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004019 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4020 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004021 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4022 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
4023 ASSERT_EQ(DEVICE_ID, args.deviceId);
4024 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4025 ASSERT_EQ(uint32_t(0), args.policyFlags);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004026 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
4027 ASSERT_EQ(0, args.flags);
4028 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4029 ASSERT_EQ(0, args.buttonState);
4030 ASSERT_EQ(0, args.edgeFlags);
4031 ASSERT_EQ(uint32_t(1), args.pointerCount);
4032 ASSERT_EQ(0, args.pointerProperties[0].id);
4033 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004034 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004035 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4036 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4037 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4038
4039 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4040 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
4041 ASSERT_EQ(DEVICE_ID, args.deviceId);
4042 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4043 ASSERT_EQ(uint32_t(0), args.policyFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004044 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
4045 ASSERT_EQ(0, args.flags);
4046 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4047 ASSERT_EQ(0, args.buttonState);
4048 ASSERT_EQ(0, args.edgeFlags);
4049 ASSERT_EQ(uint32_t(1), args.pointerCount);
4050 ASSERT_EQ(0, args.pointerProperties[0].id);
4051 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004052 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004053 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4054 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4055 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4056}
4057
4058TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentXYUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004059 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004060 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004061
4062 NotifyMotionArgs args;
4063
4064 // Motion in X but not Y.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004065 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4066 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004067 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4068 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004069 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4070 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f,
4071 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004072
4073 // Motion in Y but not X.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004074 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
4075 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004076 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4077 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004078 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f,
4079 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004080}
4081
4082TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004083 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004084 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004085
4086 NotifyMotionArgs args;
4087
4088 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004089 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4090 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004091 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4092 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004093 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004094
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004095 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4096 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004097 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004098
Michael Wrightd02c5b62014-02-10 15:10:22 -08004099 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004100 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4101 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004102 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004103 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004104 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004105
4106 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004107 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004108 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004109}
4110
4111TEST_F(CursorInputMapperTest, Process_ShouldHandleCombinedXYAndButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004112 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004113 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004114
4115 NotifyMotionArgs args;
4116
4117 // Combined X, Y and Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004118 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4119 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
4120 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4121 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004122 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4123 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004124 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4125 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4126 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004127
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004128 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4129 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004130 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4131 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4132 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004133
Michael Wrightd02c5b62014-02-10 15:10:22 -08004134 // Move X, Y a bit while pressed.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004135 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 2);
4136 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 1);
4137 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004138 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4139 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004140 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4141 2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4142 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004143
4144 // Release Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004145 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4146 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004147 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004148 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004149 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004150
4151 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004152 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004153 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004154}
4155
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004156TEST_F(CursorInputMapperTest, Process_WhenOrientationAware_ShouldNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004157 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004158 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
4159 // need to be rotated.
4160 addConfigurationProperty("cursor.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004161 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004162
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004163 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004164 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4165 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4166 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4167 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4168 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4169 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4170 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4171 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4172}
4173
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004174TEST_F(CursorInputMapperTest, Process_WhenNotOrientationAware_ShouldRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004175 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004176 // Since InputReader works in the un-rotated coordinate space, only devices that are not
4177 // orientation-aware are affected by display rotation.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004178 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004179
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004180 prepareDisplay(DISPLAY_ORIENTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004181 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4182 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4183 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4184 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4185 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4186 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4187 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4188 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4189
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004190 prepareDisplay(DISPLAY_ORIENTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004191 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, -1, 0));
4192 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, 1));
4193 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, 1));
4194 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, 1));
4195 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 1, 0));
4196 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, -1));
4197 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, -1));
4198 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, -1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004199
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004200 prepareDisplay(DISPLAY_ORIENTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004201 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, -1));
4202 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, -1));
4203 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, -1, 0));
4204 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, 1));
4205 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, 1));
4206 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, 1));
4207 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 1, 0));
4208 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, -1));
4209
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004210 prepareDisplay(DISPLAY_ORIENTATION_270);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004211 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 1, 0));
4212 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, -1));
4213 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, -1));
4214 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, -1));
4215 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, -1, 0));
4216 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, 1));
4217 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, 1));
4218 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, 1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004219}
4220
4221TEST_F(CursorInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004222 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004223 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004224
4225 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4226 mFakePointerController->setPosition(100, 200);
4227 mFakePointerController->setButtonState(0);
4228
4229 NotifyMotionArgs motionArgs;
4230 NotifyKeyArgs keyArgs;
4231
4232 // press BTN_LEFT, release BTN_LEFT
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004233 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 1);
4234 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004235 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4236 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4237 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4238 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004239 ASSERT_NO_FATAL_FAILURE(
4240 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004241
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004242 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4243 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4244 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4245 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004246 ASSERT_NO_FATAL_FAILURE(
4247 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004248
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004249 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 0);
4250 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004251 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004252 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004253 ASSERT_EQ(0, motionArgs.buttonState);
4254 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004255 ASSERT_NO_FATAL_FAILURE(
4256 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004257
4258 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004259 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004260 ASSERT_EQ(0, motionArgs.buttonState);
4261 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004262 ASSERT_NO_FATAL_FAILURE(
4263 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004264
4265 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004266 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004267 ASSERT_EQ(0, motionArgs.buttonState);
4268 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004269 ASSERT_NO_FATAL_FAILURE(
4270 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004271
4272 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004273 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 1);
4274 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 1);
4275 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004276 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4277 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4278 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4279 motionArgs.buttonState);
4280 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4281 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004282 ASSERT_NO_FATAL_FAILURE(
4283 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004284
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004285 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4286 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4287 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4288 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4289 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004290 ASSERT_NO_FATAL_FAILURE(
4291 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004292
4293 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4294 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4295 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4296 motionArgs.buttonState);
4297 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4298 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004299 ASSERT_NO_FATAL_FAILURE(
4300 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004301
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004302 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 0);
4303 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004304 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004305 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004306 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4307 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004308 ASSERT_NO_FATAL_FAILURE(
4309 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004310
4311 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004312 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004313 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4314 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004315 ASSERT_NO_FATAL_FAILURE(
4316 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004317
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004318 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4319 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004320 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004321 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
4322 ASSERT_EQ(0, motionArgs.buttonState);
4323 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004324 ASSERT_NO_FATAL_FAILURE(
4325 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004326 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4327 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004328
4329 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004330 ASSERT_EQ(0, motionArgs.buttonState);
4331 ASSERT_EQ(0, mFakePointerController->getButtonState());
4332 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004333 ASSERT_NO_FATAL_FAILURE(
4334 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004335
Michael Wrightd02c5b62014-02-10 15:10:22 -08004336 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4337 ASSERT_EQ(0, motionArgs.buttonState);
4338 ASSERT_EQ(0, mFakePointerController->getButtonState());
4339 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004340 ASSERT_NO_FATAL_FAILURE(
4341 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004342
4343 // press BTN_BACK, release BTN_BACK
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004344 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 1);
4345 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004346 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4347 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4348 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004349
Michael Wrightd02c5b62014-02-10 15:10:22 -08004350 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004351 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004352 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4353 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004354 ASSERT_NO_FATAL_FAILURE(
4355 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004356
4357 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4358 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4359 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4360 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004361 ASSERT_NO_FATAL_FAILURE(
4362 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004363
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004364 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 0);
4365 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004366 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004367 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004368 ASSERT_EQ(0, motionArgs.buttonState);
4369 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004370 ASSERT_NO_FATAL_FAILURE(
4371 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004372
4373 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004374 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004375 ASSERT_EQ(0, motionArgs.buttonState);
4376 ASSERT_EQ(0, mFakePointerController->getButtonState());
4377
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004378 ASSERT_NO_FATAL_FAILURE(
4379 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004380 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4381 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4382 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4383
4384 // press BTN_SIDE, release BTN_SIDE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004385 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 1);
4386 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004387 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4388 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4389 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004390
Michael Wrightd02c5b62014-02-10 15:10:22 -08004391 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004392 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004393 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4394 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004395 ASSERT_NO_FATAL_FAILURE(
4396 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004397
4398 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4399 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4400 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4401 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004402 ASSERT_NO_FATAL_FAILURE(
4403 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004404
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004405 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 0);
4406 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004407 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004408 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004409 ASSERT_EQ(0, motionArgs.buttonState);
4410 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004411 ASSERT_NO_FATAL_FAILURE(
4412 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004413
4414 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4415 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4416 ASSERT_EQ(0, motionArgs.buttonState);
4417 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004418 ASSERT_NO_FATAL_FAILURE(
4419 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004420
Michael Wrightd02c5b62014-02-10 15:10:22 -08004421 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4422 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4423 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4424
4425 // press BTN_FORWARD, release BTN_FORWARD
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004426 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 1);
4427 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004428 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4429 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4430 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004431
Michael Wrightd02c5b62014-02-10 15:10:22 -08004432 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004433 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004434 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4435 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004436 ASSERT_NO_FATAL_FAILURE(
4437 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004438
4439 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4440 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4441 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4442 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004443 ASSERT_NO_FATAL_FAILURE(
4444 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004445
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004446 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 0);
4447 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004448 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004449 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004450 ASSERT_EQ(0, motionArgs.buttonState);
4451 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004452 ASSERT_NO_FATAL_FAILURE(
4453 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004454
4455 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4456 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4457 ASSERT_EQ(0, motionArgs.buttonState);
4458 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004459 ASSERT_NO_FATAL_FAILURE(
4460 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004461
Michael Wrightd02c5b62014-02-10 15:10:22 -08004462 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4463 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4464 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4465
4466 // press BTN_EXTRA, release BTN_EXTRA
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004467 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 1);
4468 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004469 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4470 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4471 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004472
Michael Wrightd02c5b62014-02-10 15:10:22 -08004473 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004474 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004475 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4476 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004477 ASSERT_NO_FATAL_FAILURE(
4478 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004479
4480 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4481 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4482 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4483 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004484 ASSERT_NO_FATAL_FAILURE(
4485 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004486
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004487 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 0);
4488 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004489 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004490 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004491 ASSERT_EQ(0, motionArgs.buttonState);
4492 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004493 ASSERT_NO_FATAL_FAILURE(
4494 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004495
4496 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4497 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4498 ASSERT_EQ(0, motionArgs.buttonState);
4499 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004500 ASSERT_NO_FATAL_FAILURE(
4501 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004502
Michael Wrightd02c5b62014-02-10 15:10:22 -08004503 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4504 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4505 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4506}
4507
4508TEST_F(CursorInputMapperTest, Process_WhenModeIsPointer_ShouldMoveThePointerAround) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004509 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004510 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004511
4512 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4513 mFakePointerController->setPosition(100, 200);
4514 mFakePointerController->setButtonState(0);
4515
4516 NotifyMotionArgs args;
4517
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004518 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4519 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4520 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004521 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004522 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4523 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4524 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4525 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 +01004526 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004527}
4528
4529TEST_F(CursorInputMapperTest, Process_PointerCapture) {
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004530 addConfigurationProperty("cursor.mode", "pointer");
4531 mFakePolicy->setPointerCapture(true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004532 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004533
4534 NotifyDeviceResetArgs resetArgs;
4535 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4536 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4537 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4538
4539 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4540 mFakePointerController->setPosition(100, 200);
4541 mFakePointerController->setButtonState(0);
4542
4543 NotifyMotionArgs args;
4544
4545 // Move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004546 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4547 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4548 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004549 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4550 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4551 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4552 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4553 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 +01004554 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004555
4556 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004557 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4558 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004559 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4560 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4561 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
4562 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4563 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4564 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4565 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4566 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
4567 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4568 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4569
4570 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004571 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4572 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004573 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4574 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4575 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
4576 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4577 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4578 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4579 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4580 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
4581 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4582 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4583
4584 // Another move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004585 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 30);
4586 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 40);
4587 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004588 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4589 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4590 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4591 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4592 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 +01004593 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004594
4595 // Disable pointer capture and check that the device generation got bumped
4596 // and events are generated the usual way.
arthurhungdcef2dc2020-08-11 14:47:50 +08004597 const uint32_t generation = mReader->getContext()->getGeneration();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004598 mFakePolicy->setPointerCapture(false);
4599 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
arthurhungdcef2dc2020-08-11 14:47:50 +08004600 ASSERT_TRUE(mReader->getContext()->getGeneration() != generation);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004601
4602 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4603 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4604 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4605
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004606 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4607 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4608 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004609 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4610 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004611 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4612 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4613 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 +01004614 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004615}
4616
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004617TEST_F(CursorInputMapperTest, Process_ShouldHandleDisplayId) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004618 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004619
Garfield Tan888a6a42020-01-09 11:39:16 -08004620 // Setup for second display.
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004621 constexpr int32_t SECOND_DISPLAY_ID = 1;
Garfield Tan888a6a42020-01-09 11:39:16 -08004622 const std::string SECOND_DISPLAY_UNIQUE_ID = "local:1";
4623 mFakePolicy->addDisplayViewport(SECOND_DISPLAY_ID, 800, 480, DISPLAY_ORIENTATION_0,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00004624 true /*isActive*/, SECOND_DISPLAY_UNIQUE_ID, NO_PORT,
4625 ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08004626 mFakePolicy->setDefaultPointerDisplayId(SECOND_DISPLAY_ID);
4627 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
4628
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004629 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4630 mFakePointerController->setPosition(100, 200);
4631 mFakePointerController->setButtonState(0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004632
4633 NotifyMotionArgs args;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004634 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4635 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4636 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004637 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4638 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4639 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4640 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4641 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 +01004642 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004643 ASSERT_EQ(SECOND_DISPLAY_ID, args.displayId);
4644}
4645
Michael Wrightd02c5b62014-02-10 15:10:22 -08004646// --- TouchInputMapperTest ---
4647
4648class TouchInputMapperTest : public InputMapperTest {
4649protected:
4650 static const int32_t RAW_X_MIN;
4651 static const int32_t RAW_X_MAX;
4652 static const int32_t RAW_Y_MIN;
4653 static const int32_t RAW_Y_MAX;
4654 static const int32_t RAW_TOUCH_MIN;
4655 static const int32_t RAW_TOUCH_MAX;
4656 static const int32_t RAW_TOOL_MIN;
4657 static const int32_t RAW_TOOL_MAX;
4658 static const int32_t RAW_PRESSURE_MIN;
4659 static const int32_t RAW_PRESSURE_MAX;
4660 static const int32_t RAW_ORIENTATION_MIN;
4661 static const int32_t RAW_ORIENTATION_MAX;
4662 static const int32_t RAW_DISTANCE_MIN;
4663 static const int32_t RAW_DISTANCE_MAX;
4664 static const int32_t RAW_TILT_MIN;
4665 static const int32_t RAW_TILT_MAX;
4666 static const int32_t RAW_ID_MIN;
4667 static const int32_t RAW_ID_MAX;
4668 static const int32_t RAW_SLOT_MIN;
4669 static const int32_t RAW_SLOT_MAX;
4670 static const float X_PRECISION;
4671 static const float Y_PRECISION;
Santos Cordonfa5cf462017-04-05 10:37:00 -07004672 static const float X_PRECISION_VIRTUAL;
4673 static const float Y_PRECISION_VIRTUAL;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004674
4675 static const float GEOMETRIC_SCALE;
Jason Gerecke489fda82012-09-07 17:19:40 -07004676 static const TouchAffineTransformation AFFINE_TRANSFORM;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004677
4678 static const VirtualKeyDefinition VIRTUAL_KEYS[2];
4679
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004680 const std::string UNIQUE_ID = "local:0";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004681 const std::string SECONDARY_UNIQUE_ID = "local:1";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004682
Michael Wrightd02c5b62014-02-10 15:10:22 -08004683 enum Axes {
4684 POSITION = 1 << 0,
4685 TOUCH = 1 << 1,
4686 TOOL = 1 << 2,
4687 PRESSURE = 1 << 3,
4688 ORIENTATION = 1 << 4,
4689 MINOR = 1 << 5,
4690 ID = 1 << 6,
4691 DISTANCE = 1 << 7,
4692 TILT = 1 << 8,
4693 SLOT = 1 << 9,
4694 TOOL_TYPE = 1 << 10,
4695 };
4696
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004697 void prepareDisplay(int32_t orientation, std::optional<uint8_t> port = NO_PORT);
4698 void prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port = NO_PORT);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004699 void prepareVirtualDisplay(int32_t orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004700 void prepareVirtualKeys();
Jason Gerecke489fda82012-09-07 17:19:40 -07004701 void prepareLocationCalibration();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004702 int32_t toRawX(float displayX);
4703 int32_t toRawY(float displayY);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07004704 int32_t toRotatedRawX(float displayX);
4705 int32_t toRotatedRawY(float displayY);
Jason Gerecke489fda82012-09-07 17:19:40 -07004706 float toCookedX(float rawX, float rawY);
4707 float toCookedY(float rawX, float rawY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004708 float toDisplayX(int32_t rawX);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004709 float toDisplayX(int32_t rawX, int32_t displayWidth);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004710 float toDisplayY(int32_t rawY);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004711 float toDisplayY(int32_t rawY, int32_t displayHeight);
4712
Michael Wrightd02c5b62014-02-10 15:10:22 -08004713};
4714
4715const int32_t TouchInputMapperTest::RAW_X_MIN = 25;
4716const int32_t TouchInputMapperTest::RAW_X_MAX = 1019;
4717const int32_t TouchInputMapperTest::RAW_Y_MIN = 30;
4718const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009;
4719const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0;
4720const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31;
4721const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0;
4722const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15;
Michael Wrightaa449c92017-12-13 21:21:43 +00004723const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = 0;
4724const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = 255;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004725const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7;
4726const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
4727const int32_t TouchInputMapperTest::RAW_DISTANCE_MIN = 0;
4728const int32_t TouchInputMapperTest::RAW_DISTANCE_MAX = 7;
4729const int32_t TouchInputMapperTest::RAW_TILT_MIN = 0;
4730const int32_t TouchInputMapperTest::RAW_TILT_MAX = 150;
4731const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
4732const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
4733const int32_t TouchInputMapperTest::RAW_SLOT_MIN = 0;
4734const int32_t TouchInputMapperTest::RAW_SLOT_MAX = 9;
4735const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH;
4736const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT;
Santos Cordonfa5cf462017-04-05 10:37:00 -07004737const float TouchInputMapperTest::X_PRECISION_VIRTUAL =
4738 float(RAW_X_MAX - RAW_X_MIN + 1) / VIRTUAL_DISPLAY_WIDTH;
4739const float TouchInputMapperTest::Y_PRECISION_VIRTUAL =
4740 float(RAW_Y_MAX - RAW_Y_MIN + 1) / VIRTUAL_DISPLAY_HEIGHT;
Jason Gerecke489fda82012-09-07 17:19:40 -07004741const TouchAffineTransformation TouchInputMapperTest::AFFINE_TRANSFORM =
4742 TouchAffineTransformation(1, -2, 3, -4, 5, -6);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004743
4744const float TouchInputMapperTest::GEOMETRIC_SCALE =
4745 avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1),
4746 float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1));
4747
4748const VirtualKeyDefinition TouchInputMapperTest::VIRTUAL_KEYS[2] = {
4749 { KEY_HOME, 60, DISPLAY_HEIGHT + 15, 20, 20 },
4750 { KEY_MENU, DISPLAY_HEIGHT - 60, DISPLAY_WIDTH + 15, 20, 20 },
4751};
4752
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004753void TouchInputMapperTest::prepareDisplay(int32_t orientation, std::optional<uint8_t> port) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004754 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
4755 port, ViewportType::INTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004756}
4757
4758void TouchInputMapperTest::prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port) {
4759 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
4760 DISPLAY_ORIENTATION_0, SECONDARY_UNIQUE_ID, port, type);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004761}
4762
Santos Cordonfa5cf462017-04-05 10:37:00 -07004763void TouchInputMapperTest::prepareVirtualDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004764 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH, VIRTUAL_DISPLAY_HEIGHT,
4765 orientation, VIRTUAL_DISPLAY_UNIQUE_ID, NO_PORT,
4766 ViewportType::VIRTUAL);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004767}
4768
Michael Wrightd02c5b62014-02-10 15:10:22 -08004769void TouchInputMapperTest::prepareVirtualKeys() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004770 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[0]);
4771 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[1]);
4772 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4773 mFakeEventHub->addKey(EVENTHUB_ID, KEY_MENU, 0, AKEYCODE_MENU, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004774}
4775
Jason Gerecke489fda82012-09-07 17:19:40 -07004776void TouchInputMapperTest::prepareLocationCalibration() {
4777 mFakePolicy->setTouchAffineTransformation(AFFINE_TRANSFORM);
4778}
4779
Michael Wrightd02c5b62014-02-10 15:10:22 -08004780int32_t TouchInputMapperTest::toRawX(float displayX) {
4781 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN);
4782}
4783
4784int32_t TouchInputMapperTest::toRawY(float displayY) {
4785 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN);
4786}
4787
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07004788int32_t TouchInputMapperTest::toRotatedRawX(float displayX) {
4789 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_HEIGHT + RAW_X_MIN);
4790}
4791
4792int32_t TouchInputMapperTest::toRotatedRawY(float displayY) {
4793 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_WIDTH + RAW_Y_MIN);
4794}
4795
Jason Gerecke489fda82012-09-07 17:19:40 -07004796float TouchInputMapperTest::toCookedX(float rawX, float rawY) {
4797 AFFINE_TRANSFORM.applyTo(rawX, rawY);
4798 return rawX;
4799}
4800
4801float TouchInputMapperTest::toCookedY(float rawX, float rawY) {
4802 AFFINE_TRANSFORM.applyTo(rawX, rawY);
4803 return rawY;
4804}
4805
Michael Wrightd02c5b62014-02-10 15:10:22 -08004806float TouchInputMapperTest::toDisplayX(int32_t rawX) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004807 return toDisplayX(rawX, DISPLAY_WIDTH);
4808}
4809
4810float TouchInputMapperTest::toDisplayX(int32_t rawX, int32_t displayWidth) {
4811 return float(rawX - RAW_X_MIN) * displayWidth / (RAW_X_MAX - RAW_X_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004812}
4813
4814float TouchInputMapperTest::toDisplayY(int32_t rawY) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004815 return toDisplayY(rawY, DISPLAY_HEIGHT);
4816}
4817
4818float TouchInputMapperTest::toDisplayY(int32_t rawY, int32_t displayHeight) {
4819 return float(rawY - RAW_Y_MIN) * displayHeight / (RAW_Y_MAX - RAW_Y_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004820}
4821
4822
4823// --- SingleTouchInputMapperTest ---
4824
4825class SingleTouchInputMapperTest : public TouchInputMapperTest {
4826protected:
4827 void prepareButtons();
4828 void prepareAxes(int axes);
4829
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004830 void processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
4831 void processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
4832 void processUp(SingleTouchInputMapper& mappery);
4833 void processPressure(SingleTouchInputMapper& mapper, int32_t pressure);
4834 void processToolMajor(SingleTouchInputMapper& mapper, int32_t toolMajor);
4835 void processDistance(SingleTouchInputMapper& mapper, int32_t distance);
4836 void processTilt(SingleTouchInputMapper& mapper, int32_t tiltX, int32_t tiltY);
4837 void processKey(SingleTouchInputMapper& mapper, int32_t code, int32_t value);
4838 void processSync(SingleTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004839};
4840
4841void SingleTouchInputMapperTest::prepareButtons() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004842 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004843}
4844
4845void SingleTouchInputMapperTest::prepareAxes(int axes) {
4846 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004847 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
4848 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004849 }
4850 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004851 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MIN,
4852 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004853 }
4854 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004855 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TOOL_WIDTH, RAW_TOOL_MIN, RAW_TOOL_MAX, 0,
4856 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004857 }
4858 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004859 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_DISTANCE, RAW_DISTANCE_MIN,
4860 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004861 }
4862 if (axes & TILT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004863 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_X, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
4864 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_Y, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004865 }
4866}
4867
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004868void SingleTouchInputMapperTest::processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004869 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
4870 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
4871 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004872}
4873
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004874void SingleTouchInputMapperTest::processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004875 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
4876 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004877}
4878
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004879void SingleTouchInputMapperTest::processUp(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004880 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004881}
4882
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004883void SingleTouchInputMapperTest::processPressure(SingleTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004884 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004885}
4886
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004887void SingleTouchInputMapperTest::processToolMajor(SingleTouchInputMapper& mapper,
4888 int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004889 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TOOL_WIDTH, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004890}
4891
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004892void SingleTouchInputMapperTest::processDistance(SingleTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004893 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004894}
4895
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004896void SingleTouchInputMapperTest::processTilt(SingleTouchInputMapper& mapper, int32_t tiltX,
4897 int32_t tiltY) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004898 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_X, tiltX);
4899 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_Y, tiltY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004900}
4901
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004902void SingleTouchInputMapperTest::processKey(SingleTouchInputMapper& mapper, int32_t code,
4903 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004904 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004905}
4906
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004907void SingleTouchInputMapperTest::processSync(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004908 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004909}
4910
Michael Wrightd02c5b62014-02-10 15:10:22 -08004911TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004912 prepareButtons();
4913 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004914 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004915
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004916 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004917}
4918
4919TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndIsACursor_ReturnsTouchPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004920 mFakeEventHub->addRelativeAxis(EVENTHUB_ID, REL_X);
4921 mFakeEventHub->addRelativeAxis(EVENTHUB_ID, REL_Y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004922 prepareButtons();
4923 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004924 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004925
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004926 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004927}
4928
4929TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchPad_ReturnsTouchPad) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004930 prepareButtons();
4931 prepareAxes(POSITION);
4932 addConfigurationProperty("touch.deviceType", "touchPad");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004933 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004934
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004935 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004936}
4937
4938TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004939 prepareButtons();
4940 prepareAxes(POSITION);
4941 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004942 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004943
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004944 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004945}
4946
4947TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004948 addConfigurationProperty("touch.deviceType", "touchScreen");
4949 prepareDisplay(DISPLAY_ORIENTATION_0);
4950 prepareButtons();
4951 prepareAxes(POSITION);
4952 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004953 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004954
4955 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004956 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004957
4958 // Virtual key is down.
4959 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4960 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4961 processDown(mapper, x, y);
4962 processSync(mapper);
4963 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4964
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004965 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004966
4967 // Virtual key is up.
4968 processUp(mapper);
4969 processSync(mapper);
4970 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4971
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004972 ASSERT_EQ(AKEY_STATE_UP, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004973}
4974
4975TEST_F(SingleTouchInputMapperTest, GetScanCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004976 addConfigurationProperty("touch.deviceType", "touchScreen");
4977 prepareDisplay(DISPLAY_ORIENTATION_0);
4978 prepareButtons();
4979 prepareAxes(POSITION);
4980 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004981 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004982
4983 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004984 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004985
4986 // Virtual key is down.
4987 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4988 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4989 processDown(mapper, x, y);
4990 processSync(mapper);
4991 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4992
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004993 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004994
4995 // Virtual key is up.
4996 processUp(mapper);
4997 processSync(mapper);
4998 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4999
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005000 ASSERT_EQ(AKEY_STATE_UP, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005001}
5002
5003TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005004 addConfigurationProperty("touch.deviceType", "touchScreen");
5005 prepareDisplay(DISPLAY_ORIENTATION_0);
5006 prepareButtons();
5007 prepareAxes(POSITION);
5008 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005009 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005010
5011 const int32_t keys[2] = { AKEYCODE_HOME, AKEYCODE_A };
5012 uint8_t flags[2] = { 0, 0 };
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005013 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, 2, keys, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005014 ASSERT_TRUE(flags[0]);
5015 ASSERT_FALSE(flags[1]);
5016}
5017
5018TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005019 addConfigurationProperty("touch.deviceType", "touchScreen");
5020 prepareDisplay(DISPLAY_ORIENTATION_0);
5021 prepareButtons();
5022 prepareAxes(POSITION);
5023 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005024 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005025
arthurhungdcef2dc2020-08-11 14:47:50 +08005026 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005027
5028 NotifyKeyArgs args;
5029
5030 // Press virtual key.
5031 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5032 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5033 processDown(mapper, x, y);
5034 processSync(mapper);
5035
5036 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
5037 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5038 ASSERT_EQ(DEVICE_ID, args.deviceId);
5039 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
5040 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
5041 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
5042 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5043 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5044 ASSERT_EQ(KEY_HOME, args.scanCode);
5045 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5046 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5047
5048 // Release virtual key.
5049 processUp(mapper);
5050 processSync(mapper);
5051
5052 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
5053 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5054 ASSERT_EQ(DEVICE_ID, args.deviceId);
5055 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
5056 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
5057 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
5058 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5059 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5060 ASSERT_EQ(KEY_HOME, args.scanCode);
5061 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5062 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5063
5064 // Should not have sent any motions.
5065 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5066}
5067
5068TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005069 addConfigurationProperty("touch.deviceType", "touchScreen");
5070 prepareDisplay(DISPLAY_ORIENTATION_0);
5071 prepareButtons();
5072 prepareAxes(POSITION);
5073 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005074 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005075
arthurhungdcef2dc2020-08-11 14:47:50 +08005076 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005077
5078 NotifyKeyArgs keyArgs;
5079
5080 // Press virtual key.
5081 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5082 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5083 processDown(mapper, x, y);
5084 processSync(mapper);
5085
5086 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5087 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5088 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5089 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5090 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5091 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5092 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags);
5093 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5094 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5095 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5096 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5097
5098 // Move out of bounds. This should generate a cancel and a pointer down since we moved
5099 // into the display area.
5100 y -= 100;
5101 processMove(mapper, x, y);
5102 processSync(mapper);
5103
5104 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5105 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5106 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5107 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5108 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5109 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5110 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
5111 | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags);
5112 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5113 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5114 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5115 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5116
5117 NotifyMotionArgs motionArgs;
5118 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5119 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5120 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5121 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5122 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5123 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5124 ASSERT_EQ(0, motionArgs.flags);
5125 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5126 ASSERT_EQ(0, motionArgs.buttonState);
5127 ASSERT_EQ(0, motionArgs.edgeFlags);
5128 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5129 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5130 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5131 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5132 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5133 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5134 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5135 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5136
5137 // Keep moving out of bounds. Should generate a pointer move.
5138 y -= 50;
5139 processMove(mapper, x, y);
5140 processSync(mapper);
5141
5142 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5143 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5144 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5145 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5146 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5147 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5148 ASSERT_EQ(0, motionArgs.flags);
5149 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5150 ASSERT_EQ(0, motionArgs.buttonState);
5151 ASSERT_EQ(0, motionArgs.edgeFlags);
5152 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5153 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5154 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5155 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5156 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5157 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5158 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5159 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5160
5161 // Release out of bounds. Should generate a pointer up.
5162 processUp(mapper);
5163 processSync(mapper);
5164
5165 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5166 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5167 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5168 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5169 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5170 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5171 ASSERT_EQ(0, motionArgs.flags);
5172 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5173 ASSERT_EQ(0, motionArgs.buttonState);
5174 ASSERT_EQ(0, motionArgs.edgeFlags);
5175 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5176 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5177 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5178 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5179 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5180 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5181 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5182 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5183
5184 // Should not have sent any more keys or motions.
5185 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5186 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5187}
5188
5189TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005190 addConfigurationProperty("touch.deviceType", "touchScreen");
5191 prepareDisplay(DISPLAY_ORIENTATION_0);
5192 prepareButtons();
5193 prepareAxes(POSITION);
5194 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005195 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005196
arthurhungdcef2dc2020-08-11 14:47:50 +08005197 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005198
5199 NotifyMotionArgs motionArgs;
5200
5201 // Initially go down out of bounds.
5202 int32_t x = -10;
5203 int32_t y = -10;
5204 processDown(mapper, x, y);
5205 processSync(mapper);
5206
5207 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5208
5209 // Move into the display area. Should generate a pointer down.
5210 x = 50;
5211 y = 75;
5212 processMove(mapper, x, y);
5213 processSync(mapper);
5214
5215 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5216 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5217 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5218 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5219 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5220 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5221 ASSERT_EQ(0, motionArgs.flags);
5222 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5223 ASSERT_EQ(0, motionArgs.buttonState);
5224 ASSERT_EQ(0, motionArgs.edgeFlags);
5225 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5226 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5227 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5228 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5229 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5230 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5231 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5232 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5233
5234 // Release. Should generate a pointer up.
5235 processUp(mapper);
5236 processSync(mapper);
5237
5238 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5239 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5240 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5241 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5242 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5243 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5244 ASSERT_EQ(0, motionArgs.flags);
5245 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5246 ASSERT_EQ(0, motionArgs.buttonState);
5247 ASSERT_EQ(0, motionArgs.edgeFlags);
5248 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5249 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5250 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5251 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5252 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5253 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5254 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5255 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5256
5257 // Should not have sent any more keys or motions.
5258 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5259 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5260}
5261
Santos Cordonfa5cf462017-04-05 10:37:00 -07005262TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture_VirtualDisplay) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005263 addConfigurationProperty("touch.deviceType", "touchScreen");
5264 addConfigurationProperty("touch.displayId", VIRTUAL_DISPLAY_UNIQUE_ID);
5265
5266 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
5267 prepareButtons();
5268 prepareAxes(POSITION);
5269 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005270 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Santos Cordonfa5cf462017-04-05 10:37:00 -07005271
arthurhungdcef2dc2020-08-11 14:47:50 +08005272 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005273
5274 NotifyMotionArgs motionArgs;
5275
5276 // Down.
5277 int32_t x = 100;
5278 int32_t y = 125;
5279 processDown(mapper, x, y);
5280 processSync(mapper);
5281
5282 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5283 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5284 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5285 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5286 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5287 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5288 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5289 ASSERT_EQ(0, motionArgs.flags);
5290 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5291 ASSERT_EQ(0, motionArgs.buttonState);
5292 ASSERT_EQ(0, motionArgs.edgeFlags);
5293 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5294 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5295 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5296 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5297 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5298 1, 0, 0, 0, 0, 0, 0, 0));
5299 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5300 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5301 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5302
5303 // Move.
5304 x += 50;
5305 y += 75;
5306 processMove(mapper, x, y);
5307 processSync(mapper);
5308
5309 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5310 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5311 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5312 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5313 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5314 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5315 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5316 ASSERT_EQ(0, motionArgs.flags);
5317 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5318 ASSERT_EQ(0, motionArgs.buttonState);
5319 ASSERT_EQ(0, motionArgs.edgeFlags);
5320 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5321 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5322 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5323 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5324 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5325 1, 0, 0, 0, 0, 0, 0, 0));
5326 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5327 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5328 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5329
5330 // Up.
5331 processUp(mapper);
5332 processSync(mapper);
5333
5334 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5335 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5336 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5337 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5338 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5339 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5340 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5341 ASSERT_EQ(0, motionArgs.flags);
5342 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5343 ASSERT_EQ(0, motionArgs.buttonState);
5344 ASSERT_EQ(0, motionArgs.edgeFlags);
5345 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5346 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5347 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5348 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5349 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5350 1, 0, 0, 0, 0, 0, 0, 0));
5351 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5352 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5353 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5354
5355 // Should not have sent any more keys or motions.
5356 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5357 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5358}
5359
Michael Wrightd02c5b62014-02-10 15:10:22 -08005360TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005361 addConfigurationProperty("touch.deviceType", "touchScreen");
5362 prepareDisplay(DISPLAY_ORIENTATION_0);
5363 prepareButtons();
5364 prepareAxes(POSITION);
5365 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005366 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005367
arthurhungdcef2dc2020-08-11 14:47:50 +08005368 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005369
5370 NotifyMotionArgs motionArgs;
5371
5372 // Down.
5373 int32_t x = 100;
5374 int32_t y = 125;
5375 processDown(mapper, x, y);
5376 processSync(mapper);
5377
5378 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5379 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5380 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5381 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5382 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5383 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5384 ASSERT_EQ(0, motionArgs.flags);
5385 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5386 ASSERT_EQ(0, motionArgs.buttonState);
5387 ASSERT_EQ(0, motionArgs.edgeFlags);
5388 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5389 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5390 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5391 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5392 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5393 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5394 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5395 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5396
5397 // Move.
5398 x += 50;
5399 y += 75;
5400 processMove(mapper, x, y);
5401 processSync(mapper);
5402
5403 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5404 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5405 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5406 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5407 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5408 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5409 ASSERT_EQ(0, motionArgs.flags);
5410 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5411 ASSERT_EQ(0, motionArgs.buttonState);
5412 ASSERT_EQ(0, motionArgs.edgeFlags);
5413 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5414 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5415 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5416 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5417 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5418 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5419 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5420 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5421
5422 // Up.
5423 processUp(mapper);
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_UP, 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 // Should not have sent any more keys or motions.
5446 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5447 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5448}
5449
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005450TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_DoesNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005451 addConfigurationProperty("touch.deviceType", "touchScreen");
5452 prepareButtons();
5453 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005454 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
5455 // need to be rotated. Touchscreens are orientation-aware by default.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005456 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005457
5458 NotifyMotionArgs args;
5459
5460 // Rotation 90.
5461 prepareDisplay(DISPLAY_ORIENTATION_90);
5462 processDown(mapper, toRawX(50), toRawY(75));
5463 processSync(mapper);
5464
5465 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5466 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5467 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5468
5469 processUp(mapper);
5470 processSync(mapper);
5471 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5472}
5473
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005474TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_RotatesMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005475 addConfigurationProperty("touch.deviceType", "touchScreen");
5476 prepareButtons();
5477 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005478 // Since InputReader works in the un-rotated coordinate space, only devices that are not
5479 // orientation-aware are affected by display rotation.
5480 addConfigurationProperty("touch.orientationAware", "0");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005481 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005482
5483 NotifyMotionArgs args;
5484
5485 // Rotation 0.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005486 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005487 prepareDisplay(DISPLAY_ORIENTATION_0);
5488 processDown(mapper, toRawX(50), toRawY(75));
5489 processSync(mapper);
5490
5491 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5492 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5493 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5494
5495 processUp(mapper);
5496 processSync(mapper);
5497 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5498
5499 // Rotation 90.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005500 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005501 prepareDisplay(DISPLAY_ORIENTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005502 processDown(mapper, toRawX(75), RAW_Y_MAX - toRawY(50) + RAW_Y_MIN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005503 processSync(mapper);
5504
5505 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5506 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5507 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5508
5509 processUp(mapper);
5510 processSync(mapper);
5511 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5512
5513 // Rotation 180.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005514 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005515 prepareDisplay(DISPLAY_ORIENTATION_180);
5516 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
5517 processSync(mapper);
5518
5519 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5520 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5521 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5522
5523 processUp(mapper);
5524 processSync(mapper);
5525 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5526
5527 // Rotation 270.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005528 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005529 prepareDisplay(DISPLAY_ORIENTATION_270);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005530 processDown(mapper, RAW_X_MAX - toRawX(75) + RAW_X_MIN, toRawY(50));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005531 processSync(mapper);
5532
5533 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5534 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5535 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5536
5537 processUp(mapper);
5538 processSync(mapper);
5539 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5540}
5541
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005542TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation0_RotatesMotions) {
5543 addConfigurationProperty("touch.deviceType", "touchScreen");
5544 prepareButtons();
5545 prepareAxes(POSITION);
5546 addConfigurationProperty("touch.orientationAware", "1");
5547 addConfigurationProperty("touch.orientation", "ORIENTATION_0");
5548 clearViewports();
5549 prepareDisplay(DISPLAY_ORIENTATION_0);
5550 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5551 NotifyMotionArgs args;
5552
5553 // Orientation 0.
5554 processDown(mapper, toRawX(50), toRawY(75));
5555 processSync(mapper);
5556
5557 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5558 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5559 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5560
5561 processUp(mapper);
5562 processSync(mapper);
5563 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5564}
5565
5566TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation90_RotatesMotions) {
5567 addConfigurationProperty("touch.deviceType", "touchScreen");
5568 prepareButtons();
5569 prepareAxes(POSITION);
5570 addConfigurationProperty("touch.orientationAware", "1");
5571 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
5572 clearViewports();
5573 prepareDisplay(DISPLAY_ORIENTATION_0);
5574 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5575 NotifyMotionArgs args;
5576
5577 // Orientation 90.
5578 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
5579 processSync(mapper);
5580
5581 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5582 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5583 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5584
5585 processUp(mapper);
5586 processSync(mapper);
5587 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5588}
5589
5590TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation180_RotatesMotions) {
5591 addConfigurationProperty("touch.deviceType", "touchScreen");
5592 prepareButtons();
5593 prepareAxes(POSITION);
5594 addConfigurationProperty("touch.orientationAware", "1");
5595 addConfigurationProperty("touch.orientation", "ORIENTATION_180");
5596 clearViewports();
5597 prepareDisplay(DISPLAY_ORIENTATION_0);
5598 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5599 NotifyMotionArgs args;
5600
5601 // Orientation 180.
5602 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
5603 processSync(mapper);
5604
5605 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5606 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5607 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5608
5609 processUp(mapper);
5610 processSync(mapper);
5611 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5612}
5613
5614TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation270_RotatesMotions) {
5615 addConfigurationProperty("touch.deviceType", "touchScreen");
5616 prepareButtons();
5617 prepareAxes(POSITION);
5618 addConfigurationProperty("touch.orientationAware", "1");
5619 addConfigurationProperty("touch.orientation", "ORIENTATION_270");
5620 clearViewports();
5621 prepareDisplay(DISPLAY_ORIENTATION_0);
5622 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5623 NotifyMotionArgs args;
5624
5625 // Orientation 270.
5626 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
5627 processSync(mapper);
5628
5629 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5630 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5631 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5632
5633 processUp(mapper);
5634 processSync(mapper);
5635 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5636}
5637
5638TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationSpecified_RotatesMotionWithDisplay) {
5639 addConfigurationProperty("touch.deviceType", "touchScreen");
5640 prepareButtons();
5641 prepareAxes(POSITION);
5642 // Since InputReader works in the un-rotated coordinate space, only devices that are not
5643 // orientation-aware are affected by display rotation.
5644 addConfigurationProperty("touch.orientationAware", "0");
5645 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
5646 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5647
5648 NotifyMotionArgs args;
5649
5650 // Orientation 90, Rotation 0.
5651 clearViewports();
5652 prepareDisplay(DISPLAY_ORIENTATION_0);
5653 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
5654 processSync(mapper);
5655
5656 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5657 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5658 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5659
5660 processUp(mapper);
5661 processSync(mapper);
5662 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5663
5664 // Orientation 90, Rotation 90.
5665 clearViewports();
5666 prepareDisplay(DISPLAY_ORIENTATION_90);
5667 processDown(mapper, toRotatedRawX(50), toRotatedRawY(75));
5668 processSync(mapper);
5669
5670 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5671 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5672 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5673
5674 processUp(mapper);
5675 processSync(mapper);
5676 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5677
5678 // Orientation 90, Rotation 180.
5679 clearViewports();
5680 prepareDisplay(DISPLAY_ORIENTATION_180);
5681 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
5682 processSync(mapper);
5683
5684 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5685 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5686 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5687
5688 processUp(mapper);
5689 processSync(mapper);
5690 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5691
5692 // Orientation 90, Rotation 270.
5693 clearViewports();
5694 prepareDisplay(DISPLAY_ORIENTATION_270);
5695 processDown(mapper, RAW_X_MAX - toRotatedRawX(50) + RAW_X_MIN,
5696 RAW_Y_MAX - toRotatedRawY(75) + RAW_Y_MIN);
5697 processSync(mapper);
5698
5699 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5700 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5701 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5702
5703 processUp(mapper);
5704 processSync(mapper);
5705 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5706}
5707
Michael Wrightd02c5b62014-02-10 15:10:22 -08005708TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005709 addConfigurationProperty("touch.deviceType", "touchScreen");
5710 prepareDisplay(DISPLAY_ORIENTATION_0);
5711 prepareButtons();
5712 prepareAxes(POSITION | PRESSURE | TOOL | DISTANCE | TILT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005713 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005714
5715 // These calculations are based on the input device calibration documentation.
5716 int32_t rawX = 100;
5717 int32_t rawY = 200;
5718 int32_t rawPressure = 10;
5719 int32_t rawToolMajor = 12;
5720 int32_t rawDistance = 2;
5721 int32_t rawTiltX = 30;
5722 int32_t rawTiltY = 110;
5723
5724 float x = toDisplayX(rawX);
5725 float y = toDisplayY(rawY);
5726 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
5727 float size = float(rawToolMajor) / RAW_TOOL_MAX;
5728 float tool = float(rawToolMajor) * GEOMETRIC_SCALE;
5729 float distance = float(rawDistance);
5730
5731 float tiltCenter = (RAW_TILT_MAX + RAW_TILT_MIN) * 0.5f;
5732 float tiltScale = M_PI / 180;
5733 float tiltXAngle = (rawTiltX - tiltCenter) * tiltScale;
5734 float tiltYAngle = (rawTiltY - tiltCenter) * tiltScale;
5735 float orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
5736 float tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
5737
5738 processDown(mapper, rawX, rawY);
5739 processPressure(mapper, rawPressure);
5740 processToolMajor(mapper, rawToolMajor);
5741 processDistance(mapper, rawDistance);
5742 processTilt(mapper, rawTiltX, rawTiltY);
5743 processSync(mapper);
5744
5745 NotifyMotionArgs args;
5746 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5747 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5748 x, y, pressure, size, tool, tool, tool, tool, orientation, distance));
5749 ASSERT_EQ(tilt, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TILT));
5750}
5751
Jason Gerecke489fda82012-09-07 17:19:40 -07005752TEST_F(SingleTouchInputMapperTest, Process_XYAxes_AffineCalibration) {
Jason Gerecke489fda82012-09-07 17:19:40 -07005753 addConfigurationProperty("touch.deviceType", "touchScreen");
5754 prepareDisplay(DISPLAY_ORIENTATION_0);
5755 prepareLocationCalibration();
5756 prepareButtons();
5757 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005758 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Jason Gerecke489fda82012-09-07 17:19:40 -07005759
5760 int32_t rawX = 100;
5761 int32_t rawY = 200;
5762
5763 float x = toDisplayX(toCookedX(rawX, rawY));
5764 float y = toDisplayY(toCookedY(rawX, rawY));
5765
5766 processDown(mapper, rawX, rawY);
5767 processSync(mapper);
5768
5769 NotifyMotionArgs args;
5770 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5771 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5772 x, y, 1, 0, 0, 0, 0, 0, 0, 0));
5773}
5774
Michael Wrightd02c5b62014-02-10 15:10:22 -08005775TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005776 addConfigurationProperty("touch.deviceType", "touchScreen");
5777 prepareDisplay(DISPLAY_ORIENTATION_0);
5778 prepareButtons();
5779 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005780 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005781
5782 NotifyMotionArgs motionArgs;
5783 NotifyKeyArgs keyArgs;
5784
5785 processDown(mapper, 100, 200);
5786 processSync(mapper);
5787 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5788 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5789 ASSERT_EQ(0, motionArgs.buttonState);
5790
5791 // press BTN_LEFT, release BTN_LEFT
5792 processKey(mapper, BTN_LEFT, 1);
5793 processSync(mapper);
5794 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5795 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5796 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
5797
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005798 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5799 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5800 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
5801
Michael Wrightd02c5b62014-02-10 15:10:22 -08005802 processKey(mapper, BTN_LEFT, 0);
5803 processSync(mapper);
5804 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005805 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005806 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005807
5808 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005809 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005810 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005811
5812 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
5813 processKey(mapper, BTN_RIGHT, 1);
5814 processKey(mapper, BTN_MIDDLE, 1);
5815 processSync(mapper);
5816 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5817 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5818 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5819 motionArgs.buttonState);
5820
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005821 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5822 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5823 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
5824
5825 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5826 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5827 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5828 motionArgs.buttonState);
5829
Michael Wrightd02c5b62014-02-10 15:10:22 -08005830 processKey(mapper, BTN_RIGHT, 0);
5831 processSync(mapper);
5832 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005833 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005834 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005835
5836 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005837 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005838 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005839
5840 processKey(mapper, BTN_MIDDLE, 0);
5841 processSync(mapper);
5842 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005843 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005844 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005845
5846 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005847 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005848 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005849
5850 // press BTN_BACK, release BTN_BACK
5851 processKey(mapper, BTN_BACK, 1);
5852 processSync(mapper);
5853 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5854 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5855 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005856
Michael Wrightd02c5b62014-02-10 15:10:22 -08005857 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005858 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005859 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5860
5861 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5862 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5863 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005864
5865 processKey(mapper, BTN_BACK, 0);
5866 processSync(mapper);
5867 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005868 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005869 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005870
5871 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005872 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005873 ASSERT_EQ(0, motionArgs.buttonState);
5874
Michael Wrightd02c5b62014-02-10 15:10:22 -08005875 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5876 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5877 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
5878
5879 // press BTN_SIDE, release BTN_SIDE
5880 processKey(mapper, BTN_SIDE, 1);
5881 processSync(mapper);
5882 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5883 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5884 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005885
Michael Wrightd02c5b62014-02-10 15:10:22 -08005886 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005887 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005888 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5889
5890 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5891 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5892 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005893
5894 processKey(mapper, BTN_SIDE, 0);
5895 processSync(mapper);
5896 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005897 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005898 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005899
5900 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005901 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005902 ASSERT_EQ(0, motionArgs.buttonState);
5903
Michael Wrightd02c5b62014-02-10 15:10:22 -08005904 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5905 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5906 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
5907
5908 // press BTN_FORWARD, release BTN_FORWARD
5909 processKey(mapper, BTN_FORWARD, 1);
5910 processSync(mapper);
5911 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5912 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5913 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005914
Michael Wrightd02c5b62014-02-10 15:10:22 -08005915 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005916 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005917 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5918
5919 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5920 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5921 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005922
5923 processKey(mapper, BTN_FORWARD, 0);
5924 processSync(mapper);
5925 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005926 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005927 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005928
5929 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005930 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005931 ASSERT_EQ(0, motionArgs.buttonState);
5932
Michael Wrightd02c5b62014-02-10 15:10:22 -08005933 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5934 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5935 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
5936
5937 // press BTN_EXTRA, release BTN_EXTRA
5938 processKey(mapper, BTN_EXTRA, 1);
5939 processSync(mapper);
5940 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5941 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5942 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005943
Michael Wrightd02c5b62014-02-10 15:10:22 -08005944 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005945 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005946 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5947
5948 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5949 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5950 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005951
5952 processKey(mapper, BTN_EXTRA, 0);
5953 processSync(mapper);
5954 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005955 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005956 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005957
5958 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005959 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005960 ASSERT_EQ(0, motionArgs.buttonState);
5961
Michael Wrightd02c5b62014-02-10 15:10:22 -08005962 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5963 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5964 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
5965
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005966 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5967
Michael Wrightd02c5b62014-02-10 15:10:22 -08005968 // press BTN_STYLUS, release BTN_STYLUS
5969 processKey(mapper, BTN_STYLUS, 1);
5970 processSync(mapper);
5971 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5972 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005973 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
5974
5975 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5976 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5977 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005978
5979 processKey(mapper, BTN_STYLUS, 0);
5980 processSync(mapper);
5981 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005982 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005983 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005984
5985 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005986 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005987 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005988
5989 // press BTN_STYLUS2, release BTN_STYLUS2
5990 processKey(mapper, BTN_STYLUS2, 1);
5991 processSync(mapper);
5992 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5993 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005994 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
5995
5996 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5997 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5998 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005999
6000 processKey(mapper, BTN_STYLUS2, 0);
6001 processSync(mapper);
6002 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006003 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006004 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006005
6006 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006007 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006008 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006009
6010 // release touch
6011 processUp(mapper);
6012 processSync(mapper);
6013 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6014 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6015 ASSERT_EQ(0, motionArgs.buttonState);
6016}
6017
6018TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006019 addConfigurationProperty("touch.deviceType", "touchScreen");
6020 prepareDisplay(DISPLAY_ORIENTATION_0);
6021 prepareButtons();
6022 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006023 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006024
6025 NotifyMotionArgs motionArgs;
6026
6027 // default tool type is finger
6028 processDown(mapper, 100, 200);
6029 processSync(mapper);
6030 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6031 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6032 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6033
6034 // eraser
6035 processKey(mapper, BTN_TOOL_RUBBER, 1);
6036 processSync(mapper);
6037 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6038 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6039 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6040
6041 // stylus
6042 processKey(mapper, BTN_TOOL_RUBBER, 0);
6043 processKey(mapper, BTN_TOOL_PEN, 1);
6044 processSync(mapper);
6045 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6046 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6047 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6048
6049 // brush
6050 processKey(mapper, BTN_TOOL_PEN, 0);
6051 processKey(mapper, BTN_TOOL_BRUSH, 1);
6052 processSync(mapper);
6053 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6054 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6055 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6056
6057 // pencil
6058 processKey(mapper, BTN_TOOL_BRUSH, 0);
6059 processKey(mapper, BTN_TOOL_PENCIL, 1);
6060 processSync(mapper);
6061 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6062 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6063 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6064
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08006065 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08006066 processKey(mapper, BTN_TOOL_PENCIL, 0);
6067 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
6068 processSync(mapper);
6069 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6070 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6071 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6072
6073 // mouse
6074 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
6075 processKey(mapper, BTN_TOOL_MOUSE, 1);
6076 processSync(mapper);
6077 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6078 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6079 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6080
6081 // lens
6082 processKey(mapper, BTN_TOOL_MOUSE, 0);
6083 processKey(mapper, BTN_TOOL_LENS, 1);
6084 processSync(mapper);
6085 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6086 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6087 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6088
6089 // double-tap
6090 processKey(mapper, BTN_TOOL_LENS, 0);
6091 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
6092 processSync(mapper);
6093 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6094 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6095 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6096
6097 // triple-tap
6098 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
6099 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
6100 processSync(mapper);
6101 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6102 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6103 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6104
6105 // quad-tap
6106 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
6107 processKey(mapper, BTN_TOOL_QUADTAP, 1);
6108 processSync(mapper);
6109 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6110 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6111 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6112
6113 // finger
6114 processKey(mapper, BTN_TOOL_QUADTAP, 0);
6115 processKey(mapper, BTN_TOOL_FINGER, 1);
6116 processSync(mapper);
6117 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6118 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6119 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6120
6121 // stylus trumps finger
6122 processKey(mapper, BTN_TOOL_PEN, 1);
6123 processSync(mapper);
6124 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6125 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6126 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6127
6128 // eraser trumps stylus
6129 processKey(mapper, BTN_TOOL_RUBBER, 1);
6130 processSync(mapper);
6131 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6132 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6133 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6134
6135 // mouse trumps eraser
6136 processKey(mapper, BTN_TOOL_MOUSE, 1);
6137 processSync(mapper);
6138 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6139 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6140 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6141
6142 // back to default tool type
6143 processKey(mapper, BTN_TOOL_MOUSE, 0);
6144 processKey(mapper, BTN_TOOL_RUBBER, 0);
6145 processKey(mapper, BTN_TOOL_PEN, 0);
6146 processKey(mapper, BTN_TOOL_FINGER, 0);
6147 processSync(mapper);
6148 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6149 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6150 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6151}
6152
6153TEST_F(SingleTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006154 addConfigurationProperty("touch.deviceType", "touchScreen");
6155 prepareDisplay(DISPLAY_ORIENTATION_0);
6156 prepareButtons();
6157 prepareAxes(POSITION);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006158 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_FINGER, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006159 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006160
6161 NotifyMotionArgs motionArgs;
6162
6163 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
6164 processKey(mapper, BTN_TOOL_FINGER, 1);
6165 processMove(mapper, 100, 200);
6166 processSync(mapper);
6167 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6168 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6169 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6170 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6171
6172 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6173 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6174 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6175 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6176
6177 // move a little
6178 processMove(mapper, 150, 250);
6179 processSync(mapper);
6180 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6181 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6182 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6183 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6184
6185 // down when BTN_TOUCH is pressed, pressure defaults to 1
6186 processKey(mapper, BTN_TOUCH, 1);
6187 processSync(mapper);
6188 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6189 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6190 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6191 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6192
6193 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6194 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6195 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6196 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6197
6198 // up when BTN_TOUCH is released, hover restored
6199 processKey(mapper, BTN_TOUCH, 0);
6200 processSync(mapper);
6201 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6202 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6203 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6204 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6205
6206 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6207 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6208 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6209 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6210
6211 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6212 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6213 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6214 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6215
6216 // exit hover when pointer goes away
6217 processKey(mapper, BTN_TOOL_FINGER, 0);
6218 processSync(mapper);
6219 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6220 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6221 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6222 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6223}
6224
6225TEST_F(SingleTouchInputMapperTest, Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006226 addConfigurationProperty("touch.deviceType", "touchScreen");
6227 prepareDisplay(DISPLAY_ORIENTATION_0);
6228 prepareButtons();
6229 prepareAxes(POSITION | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006230 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006231
6232 NotifyMotionArgs motionArgs;
6233
6234 // initially hovering because pressure is 0
6235 processDown(mapper, 100, 200);
6236 processPressure(mapper, 0);
6237 processSync(mapper);
6238 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6239 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6240 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6241 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6242
6243 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6244 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6245 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6246 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6247
6248 // move a little
6249 processMove(mapper, 150, 250);
6250 processSync(mapper);
6251 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6252 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6253 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6254 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6255
6256 // down when pressure is non-zero
6257 processPressure(mapper, RAW_PRESSURE_MAX);
6258 processSync(mapper);
6259 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6260 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6261 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6262 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6263
6264 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6265 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6266 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6267 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6268
6269 // up when pressure becomes 0, hover restored
6270 processPressure(mapper, 0);
6271 processSync(mapper);
6272 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6273 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6274 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6275 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6276
6277 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6278 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6279 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6280 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6281
6282 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6283 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6284 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6285 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6286
6287 // exit hover when pointer goes away
6288 processUp(mapper);
6289 processSync(mapper);
6290 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6291 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6292 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6293 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6294}
6295
Prabir Pradhan5632d622021-09-06 07:57:20 -07006296// --- TouchDisplayProjectionTest ---
6297
6298class TouchDisplayProjectionTest : public SingleTouchInputMapperTest {
6299public:
6300 // The values inside DisplayViewport are expected to be pre-rotated. This updates the current
6301 // DisplayViewport to pre-rotate the values. The viewport's physical display will be set to the
6302 // rotated equivalent of the given un-rotated physical display bounds.
6303 void configurePhysicalDisplay(int32_t orientation, Rect naturalPhysicalDisplay) {
6304 uint32_t inverseRotationFlags;
6305 auto width = DISPLAY_WIDTH;
6306 auto height = DISPLAY_HEIGHT;
6307 switch (orientation) {
6308 case DISPLAY_ORIENTATION_90:
6309 inverseRotationFlags = ui::Transform::ROT_270;
6310 std::swap(width, height);
6311 break;
6312 case DISPLAY_ORIENTATION_180:
6313 inverseRotationFlags = ui::Transform::ROT_180;
6314 break;
6315 case DISPLAY_ORIENTATION_270:
6316 inverseRotationFlags = ui::Transform::ROT_90;
6317 std::swap(width, height);
6318 break;
6319 case DISPLAY_ORIENTATION_0:
6320 inverseRotationFlags = ui::Transform::ROT_0;
6321 break;
6322 default:
6323 FAIL() << "Invalid orientation: " << orientation;
6324 }
6325
6326 const ui::Transform rotation(inverseRotationFlags, width, height);
6327 const Rect rotatedPhysicalDisplay = rotation.transform(naturalPhysicalDisplay);
6328
6329 std::optional<DisplayViewport> internalViewport =
6330 *mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6331 DisplayViewport& v = *internalViewport;
6332 v.displayId = DISPLAY_ID;
6333 v.orientation = orientation;
6334
6335 v.logicalLeft = 0;
6336 v.logicalTop = 0;
6337 v.logicalRight = 100;
6338 v.logicalBottom = 100;
6339
6340 v.physicalLeft = rotatedPhysicalDisplay.left;
6341 v.physicalTop = rotatedPhysicalDisplay.top;
6342 v.physicalRight = rotatedPhysicalDisplay.right;
6343 v.physicalBottom = rotatedPhysicalDisplay.bottom;
6344
6345 v.deviceWidth = width;
6346 v.deviceHeight = height;
6347
6348 v.isActive = true;
6349 v.uniqueId = UNIQUE_ID;
6350 v.type = ViewportType::INTERNAL;
6351 mFakePolicy->updateViewport(v);
6352 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
6353 }
6354
6355 void assertReceivedMove(const Point& point) {
6356 NotifyMotionArgs motionArgs;
6357 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6358 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6359 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6360 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], point.x, point.y,
6361 1, 0, 0, 0, 0, 0, 0, 0));
6362 }
6363};
6364
6365TEST_F(TouchDisplayProjectionTest, IgnoresTouchesOutsidePhysicalDisplay) {
6366 addConfigurationProperty("touch.deviceType", "touchScreen");
6367 prepareDisplay(DISPLAY_ORIENTATION_0);
6368
6369 prepareButtons();
6370 prepareAxes(POSITION);
6371 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6372
6373 NotifyMotionArgs motionArgs;
6374
6375 // Configure the DisplayViewport such that the logical display maps to a subsection of
6376 // the display panel called the physical display. Here, the physical display is bounded by the
6377 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
6378 static const Rect kPhysicalDisplay{10, 20, 70, 160};
6379 static const std::array<Point, 6> kPointsOutsidePhysicalDisplay{
6380 {{-10, -10}, {0, 0}, {5, 100}, {50, 15}, {75, 100}, {50, 165}}};
6381
6382 for (auto orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90, DISPLAY_ORIENTATION_180,
6383 DISPLAY_ORIENTATION_270}) {
6384 configurePhysicalDisplay(orientation, kPhysicalDisplay);
6385
6386 // Touches outside the physical display should be ignored, and should not generate any
6387 // events. Ensure touches at the following points that lie outside of the physical display
6388 // area do not generate any events.
6389 for (const auto& point : kPointsOutsidePhysicalDisplay) {
6390 processDown(mapper, toRawX(point.x), toRawY(point.y));
6391 processSync(mapper);
6392 processUp(mapper);
6393 processSync(mapper);
6394 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled())
6395 << "Unexpected event generated for touch outside physical display at point: "
6396 << point.x << ", " << point.y;
6397 }
6398 }
6399}
6400
6401TEST_F(TouchDisplayProjectionTest, EmitsTouchDownAfterEnteringPhysicalDisplay) {
6402 addConfigurationProperty("touch.deviceType", "touchScreen");
6403 prepareDisplay(DISPLAY_ORIENTATION_0);
6404
6405 prepareButtons();
6406 prepareAxes(POSITION);
6407 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6408
6409 NotifyMotionArgs motionArgs;
6410
6411 // Configure the DisplayViewport such that the logical display maps to a subsection of
6412 // the display panel called the physical display. Here, the physical display is bounded by the
6413 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
6414 static const Rect kPhysicalDisplay{10, 20, 70, 160};
6415
6416 for (auto orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90, DISPLAY_ORIENTATION_180,
6417 DISPLAY_ORIENTATION_270}) {
6418 configurePhysicalDisplay(orientation, kPhysicalDisplay);
6419
6420 // Touches that start outside the physical display should be ignored until it enters the
6421 // physical display bounds, at which point it should generate a down event. Start a touch at
6422 // the point (5, 100), which is outside the physical display bounds.
6423 static const Point kOutsidePoint{5, 100};
6424 processDown(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
6425 processSync(mapper);
6426 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6427
6428 // Move the touch into the physical display area. This should generate a pointer down.
6429 processMove(mapper, toRawX(11), toRawY(21));
6430 processSync(mapper);
6431 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6432 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6433 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6434 ASSERT_NO_FATAL_FAILURE(
6435 assertPointerCoords(motionArgs.pointerCoords[0], 11, 21, 1, 0, 0, 0, 0, 0, 0, 0));
6436
6437 // Move the touch inside the physical display area. This should generate a pointer move.
6438 processMove(mapper, toRawX(69), toRawY(159));
6439 processSync(mapper);
6440 assertReceivedMove({69, 159});
6441
6442 // Move outside the physical display area. Since the pointer is already down, this should
6443 // now continue generating events.
6444 processMove(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
6445 processSync(mapper);
6446 assertReceivedMove(kOutsidePoint);
6447
6448 // Release. This should generate a pointer up.
6449 processUp(mapper);
6450 processSync(mapper);
6451 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6452 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6453 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], kOutsidePoint.x,
6454 kOutsidePoint.y, 1, 0, 0, 0, 0, 0, 0, 0));
6455
6456 // Ensure no more events were generated.
6457 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6458 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6459 }
6460}
6461
Michael Wrightd02c5b62014-02-10 15:10:22 -08006462// --- MultiTouchInputMapperTest ---
6463
6464class MultiTouchInputMapperTest : public TouchInputMapperTest {
6465protected:
6466 void prepareAxes(int axes);
6467
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006468 void processPosition(MultiTouchInputMapper& mapper, int32_t x, int32_t y);
6469 void processTouchMajor(MultiTouchInputMapper& mapper, int32_t touchMajor);
6470 void processTouchMinor(MultiTouchInputMapper& mapper, int32_t touchMinor);
6471 void processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor);
6472 void processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor);
6473 void processOrientation(MultiTouchInputMapper& mapper, int32_t orientation);
6474 void processPressure(MultiTouchInputMapper& mapper, int32_t pressure);
6475 void processDistance(MultiTouchInputMapper& mapper, int32_t distance);
6476 void processId(MultiTouchInputMapper& mapper, int32_t id);
6477 void processSlot(MultiTouchInputMapper& mapper, int32_t slot);
6478 void processToolType(MultiTouchInputMapper& mapper, int32_t toolType);
6479 void processKey(MultiTouchInputMapper& mapper, int32_t code, int32_t value);
6480 void processMTSync(MultiTouchInputMapper& mapper);
6481 void processSync(MultiTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006482};
6483
6484void MultiTouchInputMapperTest::prepareAxes(int axes) {
6485 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006486 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
6487 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006488 }
6489 if (axes & TOUCH) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006490 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN,
6491 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006492 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006493 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN,
6494 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006495 }
6496 }
6497 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006498 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
6499 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006500 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006501 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MAX,
6502 RAW_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006503 }
6504 }
6505 if (axes & ORIENTATION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006506 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_ORIENTATION, RAW_ORIENTATION_MIN,
6507 RAW_ORIENTATION_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006508 }
6509 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006510 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_PRESSURE, RAW_PRESSURE_MIN,
6511 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006512 }
6513 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006514 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_DISTANCE, RAW_DISTANCE_MIN,
6515 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006516 }
6517 if (axes & ID) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006518 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX, 0,
6519 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006520 }
6521 if (axes & SLOT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006522 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
6523 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_MT_SLOT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006524 }
6525 if (axes & TOOL_TYPE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006526 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOOL_TYPE, 0, MT_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006527 }
6528}
6529
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006530void MultiTouchInputMapperTest::processPosition(MultiTouchInputMapper& mapper, int32_t x,
6531 int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006532 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_X, x);
6533 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006534}
6535
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006536void MultiTouchInputMapperTest::processTouchMajor(MultiTouchInputMapper& mapper,
6537 int32_t touchMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006538 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006539}
6540
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006541void MultiTouchInputMapperTest::processTouchMinor(MultiTouchInputMapper& mapper,
6542 int32_t touchMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006543 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006544}
6545
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006546void MultiTouchInputMapperTest::processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006547 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006548}
6549
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006550void MultiTouchInputMapperTest::processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006551 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006552}
6553
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006554void MultiTouchInputMapperTest::processOrientation(MultiTouchInputMapper& mapper,
6555 int32_t orientation) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006556 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_ORIENTATION, orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006557}
6558
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006559void MultiTouchInputMapperTest::processPressure(MultiTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006560 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006561}
6562
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006563void MultiTouchInputMapperTest::processDistance(MultiTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006564 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006565}
6566
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006567void MultiTouchInputMapperTest::processId(MultiTouchInputMapper& mapper, int32_t id) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006568 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TRACKING_ID, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006569}
6570
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006571void MultiTouchInputMapperTest::processSlot(MultiTouchInputMapper& mapper, int32_t slot) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006572 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_SLOT, slot);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006573}
6574
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006575void MultiTouchInputMapperTest::processToolType(MultiTouchInputMapper& mapper, int32_t toolType) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006576 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006577}
6578
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006579void MultiTouchInputMapperTest::processKey(MultiTouchInputMapper& mapper, int32_t code,
6580 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006581 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006582}
6583
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006584void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006585 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_MT_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006586}
6587
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006588void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006589 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006590}
6591
Michael Wrightd02c5b62014-02-10 15:10:22 -08006592TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006593 addConfigurationProperty("touch.deviceType", "touchScreen");
6594 prepareDisplay(DISPLAY_ORIENTATION_0);
6595 prepareAxes(POSITION);
6596 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006597 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006598
arthurhungdcef2dc2020-08-11 14:47:50 +08006599 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006600
6601 NotifyMotionArgs motionArgs;
6602
6603 // Two fingers down at once.
6604 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
6605 processPosition(mapper, x1, y1);
6606 processMTSync(mapper);
6607 processPosition(mapper, x2, y2);
6608 processMTSync(mapper);
6609 processSync(mapper);
6610
6611 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6612 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6613 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6614 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6615 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6616 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6617 ASSERT_EQ(0, motionArgs.flags);
6618 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6619 ASSERT_EQ(0, motionArgs.buttonState);
6620 ASSERT_EQ(0, motionArgs.edgeFlags);
6621 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6622 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6623 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6624 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6625 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6626 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6627 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6628 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6629
6630 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6631 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6632 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6633 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6634 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6635 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6636 motionArgs.action);
6637 ASSERT_EQ(0, motionArgs.flags);
6638 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6639 ASSERT_EQ(0, motionArgs.buttonState);
6640 ASSERT_EQ(0, motionArgs.edgeFlags);
6641 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6642 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6643 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6644 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6645 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6646 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6647 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6648 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6649 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6650 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6651 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6652 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6653
6654 // Move.
6655 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
6656 processPosition(mapper, x1, y1);
6657 processMTSync(mapper);
6658 processPosition(mapper, x2, y2);
6659 processMTSync(mapper);
6660 processSync(mapper);
6661
6662 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6663 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6664 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6665 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6666 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6667 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6668 ASSERT_EQ(0, motionArgs.flags);
6669 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6670 ASSERT_EQ(0, motionArgs.buttonState);
6671 ASSERT_EQ(0, motionArgs.edgeFlags);
6672 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6673 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6674 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6675 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6676 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6677 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6678 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6679 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6680 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6681 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6682 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6683 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6684
6685 // First finger up.
6686 x2 += 15; y2 -= 20;
6687 processPosition(mapper, x2, y2);
6688 processMTSync(mapper);
6689 processSync(mapper);
6690
6691 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6692 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6693 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6694 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6695 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6696 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6697 motionArgs.action);
6698 ASSERT_EQ(0, motionArgs.flags);
6699 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6700 ASSERT_EQ(0, motionArgs.buttonState);
6701 ASSERT_EQ(0, motionArgs.edgeFlags);
6702 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6703 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6704 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6705 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6706 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6707 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6708 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6709 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6710 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6711 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6712 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6713 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6714
6715 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6716 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6717 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6718 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6719 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6720 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6721 ASSERT_EQ(0, motionArgs.flags);
6722 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6723 ASSERT_EQ(0, motionArgs.buttonState);
6724 ASSERT_EQ(0, motionArgs.edgeFlags);
6725 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6726 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6727 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6728 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6729 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6730 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6731 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6732 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6733
6734 // Move.
6735 x2 += 20; y2 -= 25;
6736 processPosition(mapper, x2, y2);
6737 processMTSync(mapper);
6738 processSync(mapper);
6739
6740 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6741 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6742 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6743 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6744 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6745 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6746 ASSERT_EQ(0, motionArgs.flags);
6747 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6748 ASSERT_EQ(0, motionArgs.buttonState);
6749 ASSERT_EQ(0, motionArgs.edgeFlags);
6750 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6751 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6752 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6753 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6754 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6755 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6756 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6757 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6758
6759 // New finger down.
6760 int32_t x3 = 700, y3 = 300;
6761 processPosition(mapper, x2, y2);
6762 processMTSync(mapper);
6763 processPosition(mapper, x3, y3);
6764 processMTSync(mapper);
6765 processSync(mapper);
6766
6767 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6768 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6769 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6770 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6771 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6772 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6773 motionArgs.action);
6774 ASSERT_EQ(0, motionArgs.flags);
6775 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6776 ASSERT_EQ(0, motionArgs.buttonState);
6777 ASSERT_EQ(0, motionArgs.edgeFlags);
6778 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6779 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6780 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6781 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6782 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6783 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6784 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6785 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6786 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6787 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6788 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6789 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6790
6791 // Second finger up.
6792 x3 += 30; y3 -= 20;
6793 processPosition(mapper, x3, y3);
6794 processMTSync(mapper);
6795 processSync(mapper);
6796
6797 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6798 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6799 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6800 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6801 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6802 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6803 motionArgs.action);
6804 ASSERT_EQ(0, motionArgs.flags);
6805 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6806 ASSERT_EQ(0, motionArgs.buttonState);
6807 ASSERT_EQ(0, motionArgs.edgeFlags);
6808 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6809 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6810 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6811 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6812 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6813 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6814 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6815 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6816 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6817 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6818 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6819 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6820
6821 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6822 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6823 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6824 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6825 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6826 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6827 ASSERT_EQ(0, motionArgs.flags);
6828 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6829 ASSERT_EQ(0, motionArgs.buttonState);
6830 ASSERT_EQ(0, motionArgs.edgeFlags);
6831 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6832 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6833 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6834 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6835 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6836 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6837 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6838 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6839
6840 // Last finger up.
6841 processMTSync(mapper);
6842 processSync(mapper);
6843
6844 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6845 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6846 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6847 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6848 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6849 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6850 ASSERT_EQ(0, motionArgs.flags);
6851 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6852 ASSERT_EQ(0, motionArgs.buttonState);
6853 ASSERT_EQ(0, motionArgs.edgeFlags);
6854 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6855 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6856 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6857 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6858 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6859 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6860 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6861 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6862
6863 // Should not have sent any more keys or motions.
6864 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6865 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6866}
6867
6868TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006869 addConfigurationProperty("touch.deviceType", "touchScreen");
6870 prepareDisplay(DISPLAY_ORIENTATION_0);
6871 prepareAxes(POSITION | ID);
6872 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006873 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006874
arthurhungdcef2dc2020-08-11 14:47:50 +08006875 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006876
6877 NotifyMotionArgs motionArgs;
6878
6879 // Two fingers down at once.
6880 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
6881 processPosition(mapper, x1, y1);
6882 processId(mapper, 1);
6883 processMTSync(mapper);
6884 processPosition(mapper, x2, y2);
6885 processId(mapper, 2);
6886 processMTSync(mapper);
6887 processSync(mapper);
6888
6889 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6890 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6891 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6892 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6893 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6894 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6895 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6896
6897 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6898 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6899 motionArgs.action);
6900 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6901 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6902 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6903 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6904 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6905 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6906 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6907 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6908 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6909
6910 // Move.
6911 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
6912 processPosition(mapper, x1, y1);
6913 processId(mapper, 1);
6914 processMTSync(mapper);
6915 processPosition(mapper, x2, y2);
6916 processId(mapper, 2);
6917 processMTSync(mapper);
6918 processSync(mapper);
6919
6920 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6921 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6922 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6923 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6924 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6925 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6926 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6927 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6928 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6929 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6930 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6931
6932 // First finger up.
6933 x2 += 15; y2 -= 20;
6934 processPosition(mapper, x2, y2);
6935 processId(mapper, 2);
6936 processMTSync(mapper);
6937 processSync(mapper);
6938
6939 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6940 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6941 motionArgs.action);
6942 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6943 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6944 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6945 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6946 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6947 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6948 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6949 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6950 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6951
6952 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6953 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6954 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6955 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6956 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6957 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6958 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6959
6960 // Move.
6961 x2 += 20; y2 -= 25;
6962 processPosition(mapper, x2, y2);
6963 processId(mapper, 2);
6964 processMTSync(mapper);
6965 processSync(mapper);
6966
6967 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6968 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6969 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6970 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6971 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6972 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6973 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6974
6975 // New finger down.
6976 int32_t x3 = 700, y3 = 300;
6977 processPosition(mapper, x2, y2);
6978 processId(mapper, 2);
6979 processMTSync(mapper);
6980 processPosition(mapper, x3, y3);
6981 processId(mapper, 3);
6982 processMTSync(mapper);
6983 processSync(mapper);
6984
6985 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6986 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6987 motionArgs.action);
6988 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6989 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6990 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6991 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6992 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6993 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6994 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6995 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6996 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6997
6998 // Second finger up.
6999 x3 += 30; y3 -= 20;
7000 processPosition(mapper, x3, y3);
7001 processId(mapper, 3);
7002 processMTSync(mapper);
7003 processSync(mapper);
7004
7005 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7006 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7007 motionArgs.action);
7008 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7009 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7010 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7011 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7012 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7013 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7014 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7015 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7016 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7017
7018 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7019 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7020 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7021 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7022 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7023 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7024 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7025
7026 // Last finger up.
7027 processMTSync(mapper);
7028 processSync(mapper);
7029
7030 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7031 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7032 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7033 ASSERT_EQ(0, 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(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7037
7038 // Should not have sent any more keys or motions.
7039 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7040 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7041}
7042
7043TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007044 addConfigurationProperty("touch.deviceType", "touchScreen");
7045 prepareDisplay(DISPLAY_ORIENTATION_0);
7046 prepareAxes(POSITION | ID | SLOT);
7047 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007048 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007049
arthurhungdcef2dc2020-08-11 14:47:50 +08007050 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007051
7052 NotifyMotionArgs motionArgs;
7053
7054 // Two fingers down at once.
7055 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7056 processPosition(mapper, x1, y1);
7057 processId(mapper, 1);
7058 processSlot(mapper, 1);
7059 processPosition(mapper, x2, y2);
7060 processId(mapper, 2);
7061 processSync(mapper);
7062
7063 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7064 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7065 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7066 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7067 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7068 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7069 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7070
7071 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7072 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7073 motionArgs.action);
7074 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7075 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7076 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7077 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7078 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7079 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7080 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7081 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7082 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7083
7084 // Move.
7085 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7086 processSlot(mapper, 0);
7087 processPosition(mapper, x1, y1);
7088 processSlot(mapper, 1);
7089 processPosition(mapper, x2, y2);
7090 processSync(mapper);
7091
7092 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7093 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7094 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7095 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7096 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7097 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7098 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7099 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7100 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7101 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7102 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7103
7104 // First finger up.
7105 x2 += 15; y2 -= 20;
7106 processSlot(mapper, 0);
7107 processId(mapper, -1);
7108 processSlot(mapper, 1);
7109 processPosition(mapper, x2, y2);
7110 processSync(mapper);
7111
7112 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7113 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7114 motionArgs.action);
7115 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7116 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7117 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7118 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7119 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7120 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7121 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7122 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7123 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7124
7125 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7126 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7127 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7128 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7129 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7130 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7131 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7132
7133 // Move.
7134 x2 += 20; y2 -= 25;
7135 processPosition(mapper, x2, y2);
7136 processSync(mapper);
7137
7138 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7139 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7140 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7141 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7142 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7143 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7144 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7145
7146 // New finger down.
7147 int32_t x3 = 700, y3 = 300;
7148 processPosition(mapper, x2, y2);
7149 processSlot(mapper, 0);
7150 processId(mapper, 3);
7151 processPosition(mapper, x3, y3);
7152 processSync(mapper);
7153
7154 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7155 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7156 motionArgs.action);
7157 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7158 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7159 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7160 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7161 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7162 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7163 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7164 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7165 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7166
7167 // Second finger up.
7168 x3 += 30; y3 -= 20;
7169 processSlot(mapper, 1);
7170 processId(mapper, -1);
7171 processSlot(mapper, 0);
7172 processPosition(mapper, x3, y3);
7173 processSync(mapper);
7174
7175 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7176 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7177 motionArgs.action);
7178 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7179 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7180 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7181 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7182 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7183 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7184 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7185 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7186 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7187
7188 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7189 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7190 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7191 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7192 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7193 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7194 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7195
7196 // Last finger up.
7197 processId(mapper, -1);
7198 processSync(mapper);
7199
7200 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7201 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7202 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7203 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7204 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7205 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7206 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7207
7208 // Should not have sent any more keys or motions.
7209 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7210 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7211}
7212
7213TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007214 addConfigurationProperty("touch.deviceType", "touchScreen");
7215 prepareDisplay(DISPLAY_ORIENTATION_0);
7216 prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007217 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007218
7219 // These calculations are based on the input device calibration documentation.
7220 int32_t rawX = 100;
7221 int32_t rawY = 200;
7222 int32_t rawTouchMajor = 7;
7223 int32_t rawTouchMinor = 6;
7224 int32_t rawToolMajor = 9;
7225 int32_t rawToolMinor = 8;
7226 int32_t rawPressure = 11;
7227 int32_t rawDistance = 0;
7228 int32_t rawOrientation = 3;
7229 int32_t id = 5;
7230
7231 float x = toDisplayX(rawX);
7232 float y = toDisplayY(rawY);
7233 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
7234 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
7235 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
7236 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
7237 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
7238 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
7239 float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
7240 float distance = float(rawDistance);
7241
7242 processPosition(mapper, rawX, rawY);
7243 processTouchMajor(mapper, rawTouchMajor);
7244 processTouchMinor(mapper, rawTouchMinor);
7245 processToolMajor(mapper, rawToolMajor);
7246 processToolMinor(mapper, rawToolMinor);
7247 processPressure(mapper, rawPressure);
7248 processOrientation(mapper, rawOrientation);
7249 processDistance(mapper, rawDistance);
7250 processId(mapper, id);
7251 processMTSync(mapper);
7252 processSync(mapper);
7253
7254 NotifyMotionArgs args;
7255 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7256 ASSERT_EQ(0, args.pointerProperties[0].id);
7257 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7258 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
7259 orientation, distance));
7260}
7261
7262TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007263 addConfigurationProperty("touch.deviceType", "touchScreen");
7264 prepareDisplay(DISPLAY_ORIENTATION_0);
7265 prepareAxes(POSITION | TOUCH | TOOL | MINOR);
7266 addConfigurationProperty("touch.size.calibration", "geometric");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007267 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007268
7269 // These calculations are based on the input device calibration documentation.
7270 int32_t rawX = 100;
7271 int32_t rawY = 200;
7272 int32_t rawTouchMajor = 140;
7273 int32_t rawTouchMinor = 120;
7274 int32_t rawToolMajor = 180;
7275 int32_t rawToolMinor = 160;
7276
7277 float x = toDisplayX(rawX);
7278 float y = toDisplayY(rawY);
7279 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
7280 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
7281 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
7282 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
7283 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
7284
7285 processPosition(mapper, rawX, rawY);
7286 processTouchMajor(mapper, rawTouchMajor);
7287 processTouchMinor(mapper, rawTouchMinor);
7288 processToolMajor(mapper, rawToolMajor);
7289 processToolMinor(mapper, rawToolMinor);
7290 processMTSync(mapper);
7291 processSync(mapper);
7292
7293 NotifyMotionArgs args;
7294 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7295 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7296 x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
7297}
7298
7299TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007300 addConfigurationProperty("touch.deviceType", "touchScreen");
7301 prepareDisplay(DISPLAY_ORIENTATION_0);
7302 prepareAxes(POSITION | TOUCH | TOOL);
7303 addConfigurationProperty("touch.size.calibration", "diameter");
7304 addConfigurationProperty("touch.size.scale", "10");
7305 addConfigurationProperty("touch.size.bias", "160");
7306 addConfigurationProperty("touch.size.isSummed", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007307 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007308
7309 // These calculations are based on the input device calibration documentation.
7310 // Note: We only provide a single common touch/tool value because the device is assumed
7311 // not to emit separate values for each pointer (isSummed = 1).
7312 int32_t rawX = 100;
7313 int32_t rawY = 200;
7314 int32_t rawX2 = 150;
7315 int32_t rawY2 = 250;
7316 int32_t rawTouchMajor = 5;
7317 int32_t rawToolMajor = 8;
7318
7319 float x = toDisplayX(rawX);
7320 float y = toDisplayY(rawY);
7321 float x2 = toDisplayX(rawX2);
7322 float y2 = toDisplayY(rawY2);
7323 float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
7324 float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
7325 float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
7326
7327 processPosition(mapper, rawX, rawY);
7328 processTouchMajor(mapper, rawTouchMajor);
7329 processToolMajor(mapper, rawToolMajor);
7330 processMTSync(mapper);
7331 processPosition(mapper, rawX2, rawY2);
7332 processTouchMajor(mapper, rawTouchMajor);
7333 processToolMajor(mapper, rawToolMajor);
7334 processMTSync(mapper);
7335 processSync(mapper);
7336
7337 NotifyMotionArgs args;
7338 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7339 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
7340
7341 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7342 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7343 args.action);
7344 ASSERT_EQ(size_t(2), args.pointerCount);
7345 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7346 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
7347 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
7348 x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
7349}
7350
7351TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007352 addConfigurationProperty("touch.deviceType", "touchScreen");
7353 prepareDisplay(DISPLAY_ORIENTATION_0);
7354 prepareAxes(POSITION | TOUCH | TOOL);
7355 addConfigurationProperty("touch.size.calibration", "area");
7356 addConfigurationProperty("touch.size.scale", "43");
7357 addConfigurationProperty("touch.size.bias", "3");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007358 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007359
7360 // These calculations are based on the input device calibration documentation.
7361 int32_t rawX = 100;
7362 int32_t rawY = 200;
7363 int32_t rawTouchMajor = 5;
7364 int32_t rawToolMajor = 8;
7365
7366 float x = toDisplayX(rawX);
7367 float y = toDisplayY(rawY);
7368 float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
7369 float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
7370 float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
7371
7372 processPosition(mapper, rawX, rawY);
7373 processTouchMajor(mapper, rawTouchMajor);
7374 processToolMajor(mapper, rawToolMajor);
7375 processMTSync(mapper);
7376 processSync(mapper);
7377
7378 NotifyMotionArgs args;
7379 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7380 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7381 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
7382}
7383
7384TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007385 addConfigurationProperty("touch.deviceType", "touchScreen");
7386 prepareDisplay(DISPLAY_ORIENTATION_0);
7387 prepareAxes(POSITION | PRESSURE);
7388 addConfigurationProperty("touch.pressure.calibration", "amplitude");
7389 addConfigurationProperty("touch.pressure.scale", "0.01");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007390 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007391
Michael Wrightaa449c92017-12-13 21:21:43 +00007392 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007393 mapper.populateDeviceInfo(&info);
Michael Wrightaa449c92017-12-13 21:21:43 +00007394 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
7395 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TOUCHSCREEN,
7396 0.0f, RAW_PRESSURE_MAX * 0.01, 0.0f, 0.0f));
7397
Michael Wrightd02c5b62014-02-10 15:10:22 -08007398 // These calculations are based on the input device calibration documentation.
7399 int32_t rawX = 100;
7400 int32_t rawY = 200;
7401 int32_t rawPressure = 60;
7402
7403 float x = toDisplayX(rawX);
7404 float y = toDisplayY(rawY);
7405 float pressure = float(rawPressure) * 0.01f;
7406
7407 processPosition(mapper, rawX, rawY);
7408 processPressure(mapper, rawPressure);
7409 processMTSync(mapper);
7410 processSync(mapper);
7411
7412 NotifyMotionArgs args;
7413 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7414 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7415 x, y, pressure, 0, 0, 0, 0, 0, 0, 0));
7416}
7417
7418TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007419 addConfigurationProperty("touch.deviceType", "touchScreen");
7420 prepareDisplay(DISPLAY_ORIENTATION_0);
7421 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007422 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007423
7424 NotifyMotionArgs motionArgs;
7425 NotifyKeyArgs keyArgs;
7426
7427 processId(mapper, 1);
7428 processPosition(mapper, 100, 200);
7429 processSync(mapper);
7430 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7431 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7432 ASSERT_EQ(0, motionArgs.buttonState);
7433
7434 // press BTN_LEFT, release BTN_LEFT
7435 processKey(mapper, BTN_LEFT, 1);
7436 processSync(mapper);
7437 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7438 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7439 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
7440
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007441 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7442 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7443 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
7444
Michael Wrightd02c5b62014-02-10 15:10:22 -08007445 processKey(mapper, BTN_LEFT, 0);
7446 processSync(mapper);
7447 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007448 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007449 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007450
7451 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007452 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007453 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007454
7455 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
7456 processKey(mapper, BTN_RIGHT, 1);
7457 processKey(mapper, BTN_MIDDLE, 1);
7458 processSync(mapper);
7459 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7460 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7461 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
7462 motionArgs.buttonState);
7463
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007464 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7465 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7466 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
7467
7468 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7469 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7470 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
7471 motionArgs.buttonState);
7472
Michael Wrightd02c5b62014-02-10 15:10:22 -08007473 processKey(mapper, BTN_RIGHT, 0);
7474 processSync(mapper);
7475 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007476 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007477 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007478
7479 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007480 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007481 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007482
7483 processKey(mapper, BTN_MIDDLE, 0);
7484 processSync(mapper);
7485 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007486 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007487 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007488
7489 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007490 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007491 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007492
7493 // press BTN_BACK, release BTN_BACK
7494 processKey(mapper, BTN_BACK, 1);
7495 processSync(mapper);
7496 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7497 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
7498 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007499
Michael Wrightd02c5b62014-02-10 15:10:22 -08007500 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007501 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007502 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
7503
7504 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7505 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7506 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007507
7508 processKey(mapper, BTN_BACK, 0);
7509 processSync(mapper);
7510 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007511 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007512 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007513
7514 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007515 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007516 ASSERT_EQ(0, motionArgs.buttonState);
7517
Michael Wrightd02c5b62014-02-10 15:10:22 -08007518 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7519 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
7520 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
7521
7522 // press BTN_SIDE, release BTN_SIDE
7523 processKey(mapper, BTN_SIDE, 1);
7524 processSync(mapper);
7525 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7526 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
7527 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007528
Michael Wrightd02c5b62014-02-10 15:10:22 -08007529 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007530 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007531 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
7532
7533 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7534 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7535 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007536
7537 processKey(mapper, BTN_SIDE, 0);
7538 processSync(mapper);
7539 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007540 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007541 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007542
7543 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007544 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007545 ASSERT_EQ(0, motionArgs.buttonState);
7546
Michael Wrightd02c5b62014-02-10 15:10:22 -08007547 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7548 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
7549 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
7550
7551 // press BTN_FORWARD, release BTN_FORWARD
7552 processKey(mapper, BTN_FORWARD, 1);
7553 processSync(mapper);
7554 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7555 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
7556 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007557
Michael Wrightd02c5b62014-02-10 15:10:22 -08007558 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007559 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007560 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
7561
7562 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7563 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7564 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007565
7566 processKey(mapper, BTN_FORWARD, 0);
7567 processSync(mapper);
7568 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007569 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007570 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007571
7572 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007573 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007574 ASSERT_EQ(0, motionArgs.buttonState);
7575
Michael Wrightd02c5b62014-02-10 15:10:22 -08007576 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7577 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
7578 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
7579
7580 // press BTN_EXTRA, release BTN_EXTRA
7581 processKey(mapper, BTN_EXTRA, 1);
7582 processSync(mapper);
7583 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7584 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
7585 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007586
Michael Wrightd02c5b62014-02-10 15:10:22 -08007587 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007588 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007589 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
7590
7591 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7592 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7593 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007594
7595 processKey(mapper, BTN_EXTRA, 0);
7596 processSync(mapper);
7597 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007598 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007599 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007600
7601 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007602 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007603 ASSERT_EQ(0, motionArgs.buttonState);
7604
Michael Wrightd02c5b62014-02-10 15:10:22 -08007605 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7606 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
7607 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
7608
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007609 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7610
Michael Wrightd02c5b62014-02-10 15:10:22 -08007611 // press BTN_STYLUS, release BTN_STYLUS
7612 processKey(mapper, BTN_STYLUS, 1);
7613 processSync(mapper);
7614 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7615 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007616 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
7617
7618 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7619 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7620 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007621
7622 processKey(mapper, BTN_STYLUS, 0);
7623 processSync(mapper);
7624 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007625 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007626 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007627
7628 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007629 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007630 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007631
7632 // press BTN_STYLUS2, release BTN_STYLUS2
7633 processKey(mapper, BTN_STYLUS2, 1);
7634 processSync(mapper);
7635 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7636 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007637 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
7638
7639 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7640 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7641 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007642
7643 processKey(mapper, BTN_STYLUS2, 0);
7644 processSync(mapper);
7645 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007646 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007647 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007648
7649 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007650 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007651 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007652
7653 // release touch
7654 processId(mapper, -1);
7655 processSync(mapper);
7656 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7657 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7658 ASSERT_EQ(0, motionArgs.buttonState);
7659}
7660
7661TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007662 addConfigurationProperty("touch.deviceType", "touchScreen");
7663 prepareDisplay(DISPLAY_ORIENTATION_0);
7664 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007665 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007666
7667 NotifyMotionArgs motionArgs;
7668
7669 // default tool type is finger
7670 processId(mapper, 1);
7671 processPosition(mapper, 100, 200);
7672 processSync(mapper);
7673 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7674 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7675 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7676
7677 // eraser
7678 processKey(mapper, BTN_TOOL_RUBBER, 1);
7679 processSync(mapper);
7680 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7681 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7682 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
7683
7684 // stylus
7685 processKey(mapper, BTN_TOOL_RUBBER, 0);
7686 processKey(mapper, BTN_TOOL_PEN, 1);
7687 processSync(mapper);
7688 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7689 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7690 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7691
7692 // brush
7693 processKey(mapper, BTN_TOOL_PEN, 0);
7694 processKey(mapper, BTN_TOOL_BRUSH, 1);
7695 processSync(mapper);
7696 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7697 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7698 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7699
7700 // pencil
7701 processKey(mapper, BTN_TOOL_BRUSH, 0);
7702 processKey(mapper, BTN_TOOL_PENCIL, 1);
7703 processSync(mapper);
7704 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7705 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7706 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7707
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08007708 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08007709 processKey(mapper, BTN_TOOL_PENCIL, 0);
7710 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
7711 processSync(mapper);
7712 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7713 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7714 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7715
7716 // mouse
7717 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
7718 processKey(mapper, BTN_TOOL_MOUSE, 1);
7719 processSync(mapper);
7720 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7721 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7722 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
7723
7724 // lens
7725 processKey(mapper, BTN_TOOL_MOUSE, 0);
7726 processKey(mapper, BTN_TOOL_LENS, 1);
7727 processSync(mapper);
7728 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7729 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7730 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
7731
7732 // double-tap
7733 processKey(mapper, BTN_TOOL_LENS, 0);
7734 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
7735 processSync(mapper);
7736 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7737 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7738 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7739
7740 // triple-tap
7741 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
7742 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
7743 processSync(mapper);
7744 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7745 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7746 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7747
7748 // quad-tap
7749 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
7750 processKey(mapper, BTN_TOOL_QUADTAP, 1);
7751 processSync(mapper);
7752 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7753 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7754 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7755
7756 // finger
7757 processKey(mapper, BTN_TOOL_QUADTAP, 0);
7758 processKey(mapper, BTN_TOOL_FINGER, 1);
7759 processSync(mapper);
7760 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7761 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7762 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7763
7764 // stylus trumps finger
7765 processKey(mapper, BTN_TOOL_PEN, 1);
7766 processSync(mapper);
7767 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7768 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7769 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7770
7771 // eraser trumps stylus
7772 processKey(mapper, BTN_TOOL_RUBBER, 1);
7773 processSync(mapper);
7774 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7775 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7776 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
7777
7778 // mouse trumps eraser
7779 processKey(mapper, BTN_TOOL_MOUSE, 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_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
7784
7785 // MT tool type trumps BTN tool types: MT_TOOL_FINGER
7786 processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
7787 processSync(mapper);
7788 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7789 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7790 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7791
7792 // MT tool type trumps BTN tool types: MT_TOOL_PEN
7793 processToolType(mapper, MT_TOOL_PEN);
7794 processSync(mapper);
7795 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7796 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7797 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7798
7799 // back to default tool type
7800 processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
7801 processKey(mapper, BTN_TOOL_MOUSE, 0);
7802 processKey(mapper, BTN_TOOL_RUBBER, 0);
7803 processKey(mapper, BTN_TOOL_PEN, 0);
7804 processKey(mapper, BTN_TOOL_FINGER, 0);
7805 processSync(mapper);
7806 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7807 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7808 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7809}
7810
7811TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007812 addConfigurationProperty("touch.deviceType", "touchScreen");
7813 prepareDisplay(DISPLAY_ORIENTATION_0);
7814 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007815 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007816 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007817
7818 NotifyMotionArgs motionArgs;
7819
7820 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
7821 processId(mapper, 1);
7822 processPosition(mapper, 100, 200);
7823 processSync(mapper);
7824 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7825 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
7826 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7827 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
7828
7829 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7830 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7831 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7832 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
7833
7834 // move a little
7835 processPosition(mapper, 150, 250);
7836 processSync(mapper);
7837 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7838 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7839 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7840 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7841
7842 // down when BTN_TOUCH is pressed, pressure defaults to 1
7843 processKey(mapper, BTN_TOUCH, 1);
7844 processSync(mapper);
7845 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7846 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
7847 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7848 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7849
7850 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7851 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7852 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7853 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
7854
7855 // up when BTN_TOUCH is released, hover restored
7856 processKey(mapper, BTN_TOUCH, 0);
7857 processSync(mapper);
7858 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7859 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7860 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7861 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
7862
7863 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7864 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
7865 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7866 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7867
7868 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7869 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7870 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7871 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7872
7873 // exit hover when pointer goes away
7874 processId(mapper, -1);
7875 processSync(mapper);
7876 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7877 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
7878 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7879 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7880}
7881
7882TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007883 addConfigurationProperty("touch.deviceType", "touchScreen");
7884 prepareDisplay(DISPLAY_ORIENTATION_0);
7885 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007886 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007887
7888 NotifyMotionArgs motionArgs;
7889
7890 // initially hovering because pressure is 0
7891 processId(mapper, 1);
7892 processPosition(mapper, 100, 200);
7893 processPressure(mapper, 0);
7894 processSync(mapper);
7895 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7896 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
7897 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7898 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
7899
7900 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7901 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7902 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7903 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
7904
7905 // move a little
7906 processPosition(mapper, 150, 250);
7907 processSync(mapper);
7908 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7909 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7910 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7911 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7912
7913 // down when pressure becomes non-zero
7914 processPressure(mapper, RAW_PRESSURE_MAX);
7915 processSync(mapper);
7916 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7917 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
7918 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7919 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7920
7921 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7922 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7923 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7924 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
7925
7926 // up when pressure becomes 0, hover restored
7927 processPressure(mapper, 0);
7928 processSync(mapper);
7929 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7930 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7931 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7932 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
7933
7934 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7935 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
7936 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7937 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7938
7939 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7940 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7941 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7942 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7943
7944 // exit hover when pointer goes away
7945 processId(mapper, -1);
7946 processSync(mapper);
7947 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7948 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
7949 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7950 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7951}
7952
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07007953/**
7954 * Set the input device port <--> display port associations, and check that the
7955 * events are routed to the display that matches the display port.
7956 * This can be checked by looking at the displayId of the resulting NotifyMotionArgs.
7957 */
7958TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayPort) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07007959 const std::string usb2 = "USB2";
7960 const uint8_t hdmi1 = 0;
7961 const uint8_t hdmi2 = 1;
7962 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007963 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07007964
7965 addConfigurationProperty("touch.deviceType", "touchScreen");
7966 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007967 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07007968
7969 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
7970 mFakePolicy->addInputPortAssociation(usb2, hdmi2);
7971
7972 // We are intentionally not adding the viewport for display 1 yet. Since the port association
7973 // for this input device is specified, and the matching viewport is not present,
7974 // the input device should be disabled (at the mapper level).
7975
7976 // Add viewport for display 2 on hdmi2
7977 prepareSecondaryDisplay(type, hdmi2);
7978 // Send a touch event
7979 processPosition(mapper, 100, 100);
7980 processSync(mapper);
7981 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7982
7983 // Add viewport for display 1 on hdmi1
7984 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
7985 // Send a touch event again
7986 processPosition(mapper, 100, 100);
7987 processSync(mapper);
7988
7989 NotifyMotionArgs args;
7990 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7991 ASSERT_EQ(DISPLAY_ID, args.displayId);
7992}
Michael Wrightd02c5b62014-02-10 15:10:22 -08007993
Arthur Hungc7ad2d02018-12-18 17:41:29 +08007994TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShouldHandleDisplayId) {
Garfield Tan888a6a42020-01-09 11:39:16 -08007995 // Setup for second display.
Michael Wright17db18e2020-06-26 20:51:44 +01007996 std::shared_ptr<FakePointerController> fakePointerController =
7997 std::make_shared<FakePointerController>();
Garfield Tan888a6a42020-01-09 11:39:16 -08007998 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08007999 fakePointerController->setPosition(100, 200);
8000 fakePointerController->setButtonState(0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00008001 mFakePolicy->setPointerController(fakePointerController);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008002
Garfield Tan888a6a42020-01-09 11:39:16 -08008003 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008004 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08008005
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008006 prepareDisplay(DISPLAY_ORIENTATION_0);
8007 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008008 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008009
8010 // Check source is mouse that would obtain the PointerController.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008011 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008012
8013 NotifyMotionArgs motionArgs;
8014 processPosition(mapper, 100, 100);
8015 processSync(mapper);
8016
8017 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8018 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8019 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
8020}
8021
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00008022/**
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008023 * Ensure that the readTime is set to the SYN_REPORT value when processing touch events.
8024 */
8025TEST_F(MultiTouchInputMapperTest, Process_SendsReadTime) {
8026 addConfigurationProperty("touch.deviceType", "touchScreen");
8027 prepareAxes(POSITION);
8028 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8029
8030 prepareDisplay(DISPLAY_ORIENTATION_0);
8031 process(mapper, 10, 11 /*readTime*/, EV_ABS, ABS_MT_TRACKING_ID, 1);
8032 process(mapper, 15, 16 /*readTime*/, EV_ABS, ABS_MT_POSITION_X, 100);
8033 process(mapper, 20, 21 /*readTime*/, EV_ABS, ABS_MT_POSITION_Y, 100);
8034 process(mapper, 25, 26 /*readTime*/, EV_SYN, SYN_REPORT, 0);
8035
8036 NotifyMotionArgs args;
8037 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8038 ASSERT_EQ(26, args.readTime);
8039
8040 process(mapper, 30, 31 /*readTime*/, EV_ABS, ABS_MT_POSITION_X, 110);
8041 process(mapper, 30, 32 /*readTime*/, EV_ABS, ABS_MT_POSITION_Y, 220);
8042 process(mapper, 30, 33 /*readTime*/, EV_SYN, SYN_REPORT, 0);
8043
8044 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8045 ASSERT_EQ(33, args.readTime);
8046}
8047
8048/**
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00008049 * When the viewport is not active (isActive=false), the touch mapper should be disabled and the
8050 * events should not be delivered to the listener.
8051 */
8052TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreDropped) {
8053 addConfigurationProperty("touch.deviceType", "touchScreen");
8054 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
8055 DISPLAY_ORIENTATION_0, false /*isActive*/, UNIQUE_ID, NO_PORT,
8056 ViewportType::INTERNAL);
8057 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8058 prepareAxes(POSITION);
8059 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8060
8061 NotifyMotionArgs motionArgs;
8062 processPosition(mapper, 100, 100);
8063 processSync(mapper);
8064
8065 mFakeListener->assertNotifyMotionWasNotCalled();
8066}
8067
Garfield Tanc734e4f2021-01-15 20:01:39 -08008068TEST_F(MultiTouchInputMapperTest, Process_DeactivateViewport_AbortTouches) {
8069 addConfigurationProperty("touch.deviceType", "touchScreen");
8070 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
8071 DISPLAY_ORIENTATION_0, true /*isActive*/, UNIQUE_ID, NO_PORT,
8072 ViewportType::INTERNAL);
8073 std::optional<DisplayViewport> optionalDisplayViewport =
8074 mFakePolicy->getDisplayViewportByUniqueId(UNIQUE_ID);
8075 ASSERT_TRUE(optionalDisplayViewport.has_value());
8076 DisplayViewport displayViewport = *optionalDisplayViewport;
8077
8078 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8079 prepareAxes(POSITION);
8080 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8081
8082 // Finger down
8083 int32_t x = 100, y = 100;
8084 processPosition(mapper, x, y);
8085 processSync(mapper);
8086
8087 NotifyMotionArgs motionArgs;
8088 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8089 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8090
8091 // Deactivate display viewport
8092 displayViewport.isActive = false;
8093 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
8094 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8095
8096 // Finger move
8097 x += 10, y += 10;
8098 processPosition(mapper, x, y);
8099 processSync(mapper);
8100
8101 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8102 EXPECT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
8103
8104 // Reactivate display viewport
8105 displayViewport.isActive = true;
8106 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
8107 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8108
8109 // Finger move again
8110 x += 10, y += 10;
8111 processPosition(mapper, x, y);
8112 processSync(mapper);
8113
8114 // Gesture is aborted, so events after display is activated won't be dispatched until there is
8115 // no pointer on the touch device.
8116 mFakeListener->assertNotifyMotionWasNotCalled();
8117}
8118
Arthur Hung7c645402019-01-25 17:45:42 +08008119TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShowTouches) {
8120 // Setup the first touch screen device.
Arthur Hung7c645402019-01-25 17:45:42 +08008121 prepareAxes(POSITION | ID | SLOT);
8122 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008123 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung7c645402019-01-25 17:45:42 +08008124
8125 // Create the second touch screen device, and enable multi fingers.
8126 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08008127 const std::string DEVICE_NAME2 = "TOUCHSCREEN2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08008128 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008129 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08008130 std::shared_ptr<InputDevice> device2 =
8131 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
8132 Flags<InputDeviceClass>(0));
8133
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008134 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
8135 0 /*flat*/, 0 /*fuzz*/);
8136 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
8137 0 /*flat*/, 0 /*fuzz*/);
8138 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX,
8139 0 /*flat*/, 0 /*fuzz*/);
8140 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX,
8141 0 /*flat*/, 0 /*fuzz*/);
8142 mFakeEventHub->setAbsoluteAxisValue(SECOND_EVENTHUB_ID, ABS_MT_SLOT, 0 /*value*/);
8143 mFakeEventHub->addConfigurationProperty(SECOND_EVENTHUB_ID, String8("touch.deviceType"),
8144 String8("touchScreen"));
Arthur Hung7c645402019-01-25 17:45:42 +08008145
8146 // Setup the second touch screen device.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008147 MultiTouchInputMapper& mapper2 = device2->addMapper<MultiTouchInputMapper>(SECOND_EVENTHUB_ID);
Arthur Hung7c645402019-01-25 17:45:42 +08008148 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
8149 device2->reset(ARBITRARY_TIME);
8150
8151 // Setup PointerController.
Michael Wright17db18e2020-06-26 20:51:44 +01008152 std::shared_ptr<FakePointerController> fakePointerController =
8153 std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00008154 mFakePolicy->setPointerController(fakePointerController);
Arthur Hung7c645402019-01-25 17:45:42 +08008155
8156 // Setup policy for associated displays and show touches.
8157 const uint8_t hdmi1 = 0;
8158 const uint8_t hdmi2 = 1;
8159 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
8160 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
8161 mFakePolicy->setShowTouches(true);
8162
8163 // Create displays.
8164 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008165 prepareSecondaryDisplay(ViewportType::EXTERNAL, hdmi2);
Arthur Hung7c645402019-01-25 17:45:42 +08008166
8167 // Default device will reconfigure above, need additional reconfiguration for another device.
8168 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008169 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung7c645402019-01-25 17:45:42 +08008170
8171 // Two fingers down at default display.
8172 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
8173 processPosition(mapper, x1, y1);
8174 processId(mapper, 1);
8175 processSlot(mapper, 1);
8176 processPosition(mapper, x2, y2);
8177 processId(mapper, 2);
8178 processSync(mapper);
8179
8180 std::map<int32_t, std::vector<int32_t>>::const_iterator iter =
8181 fakePointerController->getSpots().find(DISPLAY_ID);
8182 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
8183 ASSERT_EQ(size_t(2), iter->second.size());
8184
8185 // Two fingers down at second display.
8186 processPosition(mapper2, x1, y1);
8187 processId(mapper2, 1);
8188 processSlot(mapper2, 1);
8189 processPosition(mapper2, x2, y2);
8190 processId(mapper2, 2);
8191 processSync(mapper2);
8192
8193 iter = fakePointerController->getSpots().find(SECONDARY_DISPLAY_ID);
8194 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
8195 ASSERT_EQ(size_t(2), iter->second.size());
8196}
8197
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008198TEST_F(MultiTouchInputMapperTest, VideoFrames_ReceivedByListener) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008199 prepareAxes(POSITION);
8200 addConfigurationProperty("touch.deviceType", "touchScreen");
8201 prepareDisplay(DISPLAY_ORIENTATION_0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008202 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008203
8204 NotifyMotionArgs motionArgs;
8205 // Unrotated video frame
8206 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8207 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008208 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008209 processPosition(mapper, 100, 200);
8210 processSync(mapper);
8211 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8212 ASSERT_EQ(frames, motionArgs.videoFrames);
8213
8214 // Subsequent touch events should not have any videoframes
8215 // This is implemented separately in FakeEventHub,
8216 // but that should match the behaviour of TouchVideoDevice.
8217 processPosition(mapper, 200, 200);
8218 processSync(mapper);
8219 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8220 ASSERT_EQ(std::vector<TouchVideoFrame>(), motionArgs.videoFrames);
8221}
8222
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008223TEST_F(MultiTouchInputMapperTest, VideoFrames_AreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008224 prepareAxes(POSITION);
8225 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008226 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008227 // Unrotated video frame
8228 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8229 NotifyMotionArgs motionArgs;
8230
8231 // Test all 4 orientations
8232 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008233 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
8234 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
8235 clearViewports();
8236 prepareDisplay(orientation);
8237 std::vector<TouchVideoFrame> frames{frame};
8238 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
8239 processPosition(mapper, 100, 200);
8240 processSync(mapper);
8241 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8242 ASSERT_EQ(frames, motionArgs.videoFrames);
8243 }
8244}
8245
8246TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_AreRotated) {
8247 prepareAxes(POSITION);
8248 addConfigurationProperty("touch.deviceType", "touchScreen");
8249 // Since InputReader works in the un-rotated coordinate space, only devices that are not
8250 // orientation-aware are affected by display rotation.
8251 addConfigurationProperty("touch.orientationAware", "0");
8252 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8253 // Unrotated video frame
8254 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8255 NotifyMotionArgs motionArgs;
8256
8257 // Test all 4 orientations
8258 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008259 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
8260 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
8261 clearViewports();
8262 prepareDisplay(orientation);
8263 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008264 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008265 processPosition(mapper, 100, 200);
8266 processSync(mapper);
8267 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008268 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
8269 // compared to the display. This is so that when the window transform (which contains the
8270 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
8271 // window's coordinate space.
8272 frames[0].rotate(getInverseRotation(orientation));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008273 ASSERT_EQ(frames, motionArgs.videoFrames);
8274 }
8275}
8276
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008277TEST_F(MultiTouchInputMapperTest, VideoFrames_MultipleFramesAreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008278 prepareAxes(POSITION);
8279 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008280 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008281 // Unrotated video frames. There's no rule that they must all have the same dimensions,
8282 // so mix these.
8283 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8284 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
8285 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
8286 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
8287 NotifyMotionArgs motionArgs;
8288
8289 prepareDisplay(DISPLAY_ORIENTATION_90);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008290 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008291 processPosition(mapper, 100, 200);
8292 processSync(mapper);
8293 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008294 ASSERT_EQ(frames, motionArgs.videoFrames);
8295}
8296
8297TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_MultipleFramesAreRotated) {
8298 prepareAxes(POSITION);
8299 addConfigurationProperty("touch.deviceType", "touchScreen");
8300 // Since InputReader works in the un-rotated coordinate space, only devices that are not
8301 // orientation-aware are affected by display rotation.
8302 addConfigurationProperty("touch.orientationAware", "0");
8303 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8304 // Unrotated video frames. There's no rule that they must all have the same dimensions,
8305 // so mix these.
8306 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8307 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
8308 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
8309 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
8310 NotifyMotionArgs motionArgs;
8311
8312 prepareDisplay(DISPLAY_ORIENTATION_90);
8313 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
8314 processPosition(mapper, 100, 200);
8315 processSync(mapper);
8316 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8317 std::for_each(frames.begin(), frames.end(), [](TouchVideoFrame& frame) {
8318 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
8319 // compared to the display. This is so that when the window transform (which contains the
8320 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
8321 // window's coordinate space.
8322 frame.rotate(getInverseRotation(DISPLAY_ORIENTATION_90));
8323 });
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008324 ASSERT_EQ(frames, motionArgs.videoFrames);
8325}
8326
Arthur Hung9da14732019-09-02 16:16:58 +08008327/**
8328 * If we had defined port associations, but the viewport is not ready, the touch device would be
8329 * expected to be disabled, and it should be enabled after the viewport has found.
8330 */
8331TEST_F(MultiTouchInputMapperTest, Configure_EnabledForAssociatedDisplay) {
Arthur Hung9da14732019-09-02 16:16:58 +08008332 constexpr uint8_t hdmi2 = 1;
8333 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008334 constexpr ViewportType type = ViewportType::EXTERNAL;
Arthur Hung9da14732019-09-02 16:16:58 +08008335
8336 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi2);
8337
8338 addConfigurationProperty("touch.deviceType", "touchScreen");
8339 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008340 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung9da14732019-09-02 16:16:58 +08008341
8342 ASSERT_EQ(mDevice->isEnabled(), false);
8343
8344 // Add display on hdmi2, the device should be enabled and can receive touch event.
8345 prepareSecondaryDisplay(type, hdmi2);
8346 ASSERT_EQ(mDevice->isEnabled(), true);
8347
8348 // Send a touch event.
8349 processPosition(mapper, 100, 100);
8350 processSync(mapper);
8351
8352 NotifyMotionArgs args;
8353 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8354 ASSERT_EQ(SECONDARY_DISPLAY_ID, args.displayId);
8355}
8356
Arthur Hung421eb1c2020-01-16 00:09:42 +08008357TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleSingleTouch) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08008358 addConfigurationProperty("touch.deviceType", "touchScreen");
8359 prepareDisplay(DISPLAY_ORIENTATION_0);
8360 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008361 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08008362
8363 NotifyMotionArgs motionArgs;
8364
8365 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
8366 // finger down
8367 processId(mapper, 1);
8368 processPosition(mapper, x1, y1);
8369 processSync(mapper);
8370 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8371 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8372 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8373
8374 // finger move
8375 processId(mapper, 1);
8376 processPosition(mapper, x2, y2);
8377 processSync(mapper);
8378 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8379 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8380 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8381
8382 // finger up.
8383 processId(mapper, -1);
8384 processSync(mapper);
8385 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8386 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8387 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8388
8389 // new finger down
8390 processId(mapper, 1);
8391 processPosition(mapper, x3, y3);
8392 processSync(mapper);
8393 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8394 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8395 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8396}
8397
8398/**
arthurhungcc7f9802020-04-30 17:55:40 +08008399 * Test single touch should be canceled when received the MT_TOOL_PALM event, and the following
8400 * MOVE and UP events should be ignored.
Arthur Hung421eb1c2020-01-16 00:09:42 +08008401 */
arthurhungcc7f9802020-04-30 17:55:40 +08008402TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_SinglePointer) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08008403 addConfigurationProperty("touch.deviceType", "touchScreen");
8404 prepareDisplay(DISPLAY_ORIENTATION_0);
8405 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008406 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08008407
8408 NotifyMotionArgs motionArgs;
8409
8410 // default tool type is finger
8411 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
arthurhungcc7f9802020-04-30 17:55:40 +08008412 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008413 processPosition(mapper, x1, y1);
8414 processSync(mapper);
8415 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8416 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8417 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8418
8419 // Tool changed to MT_TOOL_PALM expect sending the cancel event.
8420 processToolType(mapper, MT_TOOL_PALM);
8421 processSync(mapper);
8422 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8423 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
8424
8425 // Ignore the following MOVE and UP events if had detect a palm event.
arthurhungcc7f9802020-04-30 17:55:40 +08008426 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008427 processPosition(mapper, x2, y2);
8428 processSync(mapper);
8429 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8430
8431 // finger up.
arthurhungcc7f9802020-04-30 17:55:40 +08008432 processId(mapper, INVALID_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008433 processSync(mapper);
8434 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8435
8436 // new finger down
arthurhungcc7f9802020-04-30 17:55:40 +08008437 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008438 processToolType(mapper, MT_TOOL_FINGER);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008439 processPosition(mapper, x3, y3);
8440 processSync(mapper);
8441 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8442 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8443 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8444}
8445
arthurhungbf89a482020-04-17 17:37:55 +08008446/**
arthurhungcc7f9802020-04-30 17:55:40 +08008447 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
8448 * and the rest active fingers could still be allowed to receive the events
arthurhungbf89a482020-04-17 17:37:55 +08008449 */
arthurhungcc7f9802020-04-30 17:55:40 +08008450TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_TwoPointers) {
arthurhungbf89a482020-04-17 17:37:55 +08008451 addConfigurationProperty("touch.deviceType", "touchScreen");
8452 prepareDisplay(DISPLAY_ORIENTATION_0);
8453 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
8454 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8455
8456 NotifyMotionArgs motionArgs;
8457
8458 // default tool type is finger
arthurhungcc7f9802020-04-30 17:55:40 +08008459 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
8460 processId(mapper, FIRST_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08008461 processPosition(mapper, x1, y1);
8462 processSync(mapper);
8463 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8464 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8465 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8466
8467 // Second finger down.
arthurhungcc7f9802020-04-30 17:55:40 +08008468 processSlot(mapper, SECOND_SLOT);
8469 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08008470 processPosition(mapper, x2, y2);
arthurhungcc7f9802020-04-30 17:55:40 +08008471 processSync(mapper);
8472 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8473 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
8474 motionArgs.action);
8475 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8476
8477 // If the tool type of the first finger changes to MT_TOOL_PALM,
8478 // we expect to receive ACTION_POINTER_UP with cancel flag.
8479 processSlot(mapper, FIRST_SLOT);
8480 processId(mapper, FIRST_TRACKING_ID);
8481 processToolType(mapper, MT_TOOL_PALM);
8482 processSync(mapper);
8483 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8484 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
8485 motionArgs.action);
8486 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
8487
8488 // The following MOVE events of second finger should be processed.
8489 processSlot(mapper, SECOND_SLOT);
8490 processId(mapper, SECOND_TRACKING_ID);
8491 processPosition(mapper, x2 + 1, y2 + 1);
8492 processSync(mapper);
8493 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8494 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8495 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8496
8497 // First finger up. It used to be in palm mode, and we already generated ACTION_POINTER_UP for
8498 // it. Second finger receive move.
8499 processSlot(mapper, FIRST_SLOT);
8500 processId(mapper, INVALID_TRACKING_ID);
8501 processSync(mapper);
8502 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8503 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8504 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8505
8506 // Second finger keeps moving.
8507 processSlot(mapper, SECOND_SLOT);
8508 processId(mapper, SECOND_TRACKING_ID);
8509 processPosition(mapper, x2 + 2, y2 + 2);
8510 processSync(mapper);
8511 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8512 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8513 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8514
8515 // Second finger up.
8516 processId(mapper, INVALID_TRACKING_ID);
8517 processSync(mapper);
8518 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8519 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8520 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
8521}
8522
8523/**
8524 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event, if only 1 finger
8525 * is active, it should send CANCEL after receiving the MT_TOOL_PALM event.
8526 */
8527TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_ShouldCancelWhenAllTouchIsPalm) {
8528 addConfigurationProperty("touch.deviceType", "touchScreen");
8529 prepareDisplay(DISPLAY_ORIENTATION_0);
8530 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
8531 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8532
8533 NotifyMotionArgs motionArgs;
8534
8535 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
8536 // First finger down.
8537 processId(mapper, FIRST_TRACKING_ID);
8538 processPosition(mapper, x1, y1);
8539 processSync(mapper);
8540 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8541 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8542 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8543
8544 // Second finger down.
8545 processSlot(mapper, SECOND_SLOT);
8546 processId(mapper, SECOND_TRACKING_ID);
8547 processPosition(mapper, x2, y2);
arthurhungbf89a482020-04-17 17:37:55 +08008548 processSync(mapper);
8549 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8550 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
8551 motionArgs.action);
8552 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8553
arthurhungcc7f9802020-04-30 17:55:40 +08008554 // If the tool type of the first finger changes to MT_TOOL_PALM,
8555 // we expect to receive ACTION_POINTER_UP with cancel flag.
8556 processSlot(mapper, FIRST_SLOT);
8557 processId(mapper, FIRST_TRACKING_ID);
8558 processToolType(mapper, MT_TOOL_PALM);
8559 processSync(mapper);
8560 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8561 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
8562 motionArgs.action);
8563 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
8564
8565 // Second finger keeps moving.
8566 processSlot(mapper, SECOND_SLOT);
8567 processId(mapper, SECOND_TRACKING_ID);
8568 processPosition(mapper, x2 + 1, y2 + 1);
8569 processSync(mapper);
8570 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8571 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8572
8573 // second finger becomes palm, receive cancel due to only 1 finger is active.
8574 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08008575 processToolType(mapper, MT_TOOL_PALM);
8576 processSync(mapper);
8577 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8578 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
8579
arthurhungcc7f9802020-04-30 17:55:40 +08008580 // third finger down.
8581 processSlot(mapper, THIRD_SLOT);
8582 processId(mapper, THIRD_TRACKING_ID);
8583 processToolType(mapper, MT_TOOL_FINGER);
arthurhungbf89a482020-04-17 17:37:55 +08008584 processPosition(mapper, x3, y3);
8585 processSync(mapper);
arthurhungbf89a482020-04-17 17:37:55 +08008586 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8587 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8588 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08008589 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8590
8591 // third finger move
8592 processId(mapper, THIRD_TRACKING_ID);
8593 processPosition(mapper, x3 + 1, y3 + 1);
8594 processSync(mapper);
8595 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8596 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8597
8598 // first finger up, third finger receive move.
8599 processSlot(mapper, FIRST_SLOT);
8600 processId(mapper, INVALID_TRACKING_ID);
8601 processSync(mapper);
8602 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8603 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8604 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8605
8606 // second finger up, third finger receive move.
8607 processSlot(mapper, SECOND_SLOT);
8608 processId(mapper, INVALID_TRACKING_ID);
8609 processSync(mapper);
8610 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8611 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8612 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8613
8614 // third finger up.
8615 processSlot(mapper, THIRD_SLOT);
8616 processId(mapper, INVALID_TRACKING_ID);
8617 processSync(mapper);
8618 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8619 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8620 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
8621}
8622
8623/**
8624 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
8625 * and the active finger could still be allowed to receive the events
8626 */
8627TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_KeepFirstPointer) {
8628 addConfigurationProperty("touch.deviceType", "touchScreen");
8629 prepareDisplay(DISPLAY_ORIENTATION_0);
8630 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
8631 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8632
8633 NotifyMotionArgs motionArgs;
8634
8635 // default tool type is finger
8636 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
8637 processId(mapper, FIRST_TRACKING_ID);
8638 processPosition(mapper, x1, y1);
8639 processSync(mapper);
8640 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8641 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8642 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8643
8644 // Second finger down.
8645 processSlot(mapper, SECOND_SLOT);
8646 processId(mapper, SECOND_TRACKING_ID);
8647 processPosition(mapper, x2, y2);
8648 processSync(mapper);
8649 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8650 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
8651 motionArgs.action);
8652 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8653
8654 // If the tool type of the second finger changes to MT_TOOL_PALM,
8655 // we expect to receive ACTION_POINTER_UP with cancel flag.
8656 processId(mapper, SECOND_TRACKING_ID);
8657 processToolType(mapper, MT_TOOL_PALM);
8658 processSync(mapper);
8659 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8660 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
8661 motionArgs.action);
8662 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
8663
8664 // The following MOVE event should be processed.
8665 processSlot(mapper, FIRST_SLOT);
8666 processId(mapper, FIRST_TRACKING_ID);
8667 processPosition(mapper, x1 + 1, y1 + 1);
8668 processSync(mapper);
8669 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8670 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8671 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8672
8673 // second finger up.
8674 processSlot(mapper, SECOND_SLOT);
8675 processId(mapper, INVALID_TRACKING_ID);
8676 processSync(mapper);
8677 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8678 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8679
8680 // first finger keep moving
8681 processSlot(mapper, FIRST_SLOT);
8682 processId(mapper, FIRST_TRACKING_ID);
8683 processPosition(mapper, x1 + 2, y1 + 2);
8684 processSync(mapper);
8685 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8686 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8687
8688 // first finger up.
8689 processId(mapper, INVALID_TRACKING_ID);
8690 processSync(mapper);
8691 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8692 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8693 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
arthurhungbf89a482020-04-17 17:37:55 +08008694}
8695
Arthur Hung9ad18942021-06-19 02:04:46 +00008696/**
8697 * Test multi-touch should sent ACTION_POINTER_UP/ACTION_UP when received the INVALID_TRACKING_ID,
8698 * to prevent the driver side may send unexpected data after set tracking id as INVALID_TRACKING_ID
8699 * cause slot be valid again.
8700 */
8701TEST_F(MultiTouchInputMapperTest, Process_MultiTouch_WithInvalidTrackingId) {
8702 addConfigurationProperty("touch.deviceType", "touchScreen");
8703 prepareDisplay(DISPLAY_ORIENTATION_0);
8704 prepareAxes(POSITION | ID | SLOT | PRESSURE);
8705 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8706
8707 NotifyMotionArgs motionArgs;
8708
8709 constexpr int32_t x1 = 100, y1 = 200, x2 = 0, y2 = 0;
8710 // First finger down.
8711 processId(mapper, FIRST_TRACKING_ID);
8712 processPosition(mapper, x1, y1);
8713 processPressure(mapper, RAW_PRESSURE_MAX);
8714 processSync(mapper);
8715 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8716 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8717 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8718
8719 // First finger move.
8720 processId(mapper, FIRST_TRACKING_ID);
8721 processPosition(mapper, x1 + 1, y1 + 1);
8722 processPressure(mapper, RAW_PRESSURE_MAX);
8723 processSync(mapper);
8724 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8725 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8726 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8727
8728 // Second finger down.
8729 processSlot(mapper, SECOND_SLOT);
8730 processId(mapper, SECOND_TRACKING_ID);
8731 processPosition(mapper, x2, y2);
8732 processPressure(mapper, RAW_PRESSURE_MAX);
8733 processSync(mapper);
8734 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8735 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
8736 motionArgs.action);
8737 ASSERT_EQ(uint32_t(2), motionArgs.pointerCount);
8738
8739 // second finger up with some unexpected data.
8740 processSlot(mapper, SECOND_SLOT);
8741 processId(mapper, INVALID_TRACKING_ID);
8742 processPosition(mapper, x2, y2);
8743 processSync(mapper);
8744 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8745 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
8746 motionArgs.action);
8747 ASSERT_EQ(uint32_t(2), motionArgs.pointerCount);
8748
8749 // first finger up with some unexpected data.
8750 processSlot(mapper, FIRST_SLOT);
8751 processId(mapper, INVALID_TRACKING_ID);
8752 processPosition(mapper, x2, y2);
8753 processPressure(mapper, RAW_PRESSURE_MAX);
8754 processSync(mapper);
8755 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8756 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8757 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8758}
8759
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008760// --- MultiTouchInputMapperTest_ExternalDevice ---
8761
8762class MultiTouchInputMapperTest_ExternalDevice : public MultiTouchInputMapperTest {
8763protected:
Chris Yea52ade12020-08-27 16:49:20 -07008764 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008765};
8766
8767/**
8768 * Expect fallback to internal viewport if device is external and external viewport is not present.
8769 */
8770TEST_F(MultiTouchInputMapperTest_ExternalDevice, Viewports_Fallback) {
8771 prepareAxes(POSITION);
8772 addConfigurationProperty("touch.deviceType", "touchScreen");
8773 prepareDisplay(DISPLAY_ORIENTATION_0);
8774 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8775
8776 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
8777
8778 NotifyMotionArgs motionArgs;
8779
8780 // Expect the event to be sent to the internal viewport,
8781 // because an external viewport is not present.
8782 processPosition(mapper, 100, 100);
8783 processSync(mapper);
8784 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8785 ASSERT_EQ(ADISPLAY_ID_DEFAULT, motionArgs.displayId);
8786
8787 // Expect the event to be sent to the external viewport if it is present.
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008788 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008789 processPosition(mapper, 100, 100);
8790 processSync(mapper);
8791 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8792 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
8793}
Arthur Hung4197f6b2020-03-16 15:39:59 +08008794
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08008795TEST_F(MultiTouchInputMapperTest, Process_TouchpadCapture) {
8796 // we need a pointer controller for mouse mode of touchpad (start pointer at 0,0)
8797 std::shared_ptr<FakePointerController> fakePointerController =
8798 std::make_shared<FakePointerController>();
8799 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
8800 fakePointerController->setPosition(0, 0);
8801 fakePointerController->setButtonState(0);
8802
8803 // prepare device and capture
8804 prepareDisplay(DISPLAY_ORIENTATION_0);
8805 prepareAxes(POSITION | ID | SLOT);
8806 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
8807 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
8808 mFakePolicy->setPointerCapture(true);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00008809 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08008810 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8811
8812 // captured touchpad should be a touchpad source
8813 NotifyDeviceResetArgs resetArgs;
8814 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
8815 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
8816
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00008817 InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
Chris Yef74dc422020-09-02 22:41:50 -07008818
8819 const InputDeviceInfo::MotionRange* relRangeX =
8820 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_X, AINPUT_SOURCE_TOUCHPAD);
8821 ASSERT_NE(relRangeX, nullptr);
8822 ASSERT_EQ(relRangeX->min, -(RAW_X_MAX - RAW_X_MIN));
8823 ASSERT_EQ(relRangeX->max, RAW_X_MAX - RAW_X_MIN);
8824 const InputDeviceInfo::MotionRange* relRangeY =
8825 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_Y, AINPUT_SOURCE_TOUCHPAD);
8826 ASSERT_NE(relRangeY, nullptr);
8827 ASSERT_EQ(relRangeY->min, -(RAW_Y_MAX - RAW_Y_MIN));
8828 ASSERT_EQ(relRangeY->max, RAW_Y_MAX - RAW_Y_MIN);
8829
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08008830 // run captured pointer tests - note that this is unscaled, so input listener events should be
8831 // identical to what the hardware sends (accounting for any
8832 // calibration).
8833 // FINGER 0 DOWN
Chris Ye364fdb52020-08-05 15:07:56 -07008834 processSlot(mapper, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08008835 processId(mapper, 1);
8836 processPosition(mapper, 100 + RAW_X_MIN, 100 + RAW_Y_MIN);
8837 processKey(mapper, BTN_TOUCH, 1);
8838 processSync(mapper);
8839
8840 // expect coord[0] to contain initial location of touch 0
8841 NotifyMotionArgs args;
8842 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8843 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
8844 ASSERT_EQ(1U, args.pointerCount);
8845 ASSERT_EQ(0, args.pointerProperties[0].id);
8846 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, args.source);
8847 ASSERT_NO_FATAL_FAILURE(
8848 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
8849
8850 // FINGER 1 DOWN
8851 processSlot(mapper, 1);
8852 processId(mapper, 2);
8853 processPosition(mapper, 560 + RAW_X_MIN, 154 + RAW_Y_MIN);
8854 processSync(mapper);
8855
8856 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
8857 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Chris Ye364fdb52020-08-05 15:07:56 -07008858 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
8859 args.action);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08008860 ASSERT_EQ(2U, args.pointerCount);
8861 ASSERT_EQ(0, args.pointerProperties[0].id);
8862 ASSERT_EQ(1, args.pointerProperties[1].id);
8863 ASSERT_NO_FATAL_FAILURE(
8864 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
8865 ASSERT_NO_FATAL_FAILURE(
8866 assertPointerCoords(args.pointerCoords[1], 560, 154, 1, 0, 0, 0, 0, 0, 0, 0));
8867
8868 // FINGER 1 MOVE
8869 processPosition(mapper, 540 + RAW_X_MIN, 690 + RAW_Y_MIN);
8870 processSync(mapper);
8871
8872 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
8873 // from move
8874 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8875 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
8876 ASSERT_NO_FATAL_FAILURE(
8877 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
8878 ASSERT_NO_FATAL_FAILURE(
8879 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
8880
8881 // FINGER 0 MOVE
8882 processSlot(mapper, 0);
8883 processPosition(mapper, 50 + RAW_X_MIN, 800 + RAW_Y_MIN);
8884 processSync(mapper);
8885
8886 // expect coord[0] to contain new touch 0 location, coord[1] to contain previous location
8887 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8888 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
8889 ASSERT_NO_FATAL_FAILURE(
8890 assertPointerCoords(args.pointerCoords[0], 50, 800, 1, 0, 0, 0, 0, 0, 0, 0));
8891 ASSERT_NO_FATAL_FAILURE(
8892 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
8893
8894 // BUTTON DOWN
8895 processKey(mapper, BTN_LEFT, 1);
8896 processSync(mapper);
8897
8898 // touchinputmapper design sends a move before button press
8899 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8900 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
8901 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8902 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
8903
8904 // BUTTON UP
8905 processKey(mapper, BTN_LEFT, 0);
8906 processSync(mapper);
8907
8908 // touchinputmapper design sends a move after button release
8909 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8910 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
8911 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8912 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
8913
8914 // FINGER 0 UP
8915 processId(mapper, -1);
8916 processSync(mapper);
8917 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8918 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | 0x0000, args.action);
8919
8920 // FINGER 1 MOVE
8921 processSlot(mapper, 1);
8922 processPosition(mapper, 320 + RAW_X_MIN, 900 + RAW_Y_MIN);
8923 processSync(mapper);
8924
8925 // expect coord[0] to contain new location of touch 1, and properties[0].id to contain 1
8926 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8927 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
8928 ASSERT_EQ(1U, args.pointerCount);
8929 ASSERT_EQ(1, args.pointerProperties[0].id);
8930 ASSERT_NO_FATAL_FAILURE(
8931 assertPointerCoords(args.pointerCoords[0], 320, 900, 1, 0, 0, 0, 0, 0, 0, 0));
8932
8933 // FINGER 1 UP
8934 processId(mapper, -1);
8935 processKey(mapper, BTN_TOUCH, 0);
8936 processSync(mapper);
8937 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8938 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
8939
8940 // non captured touchpad should be a mouse source
8941 mFakePolicy->setPointerCapture(false);
8942 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
8943 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
8944 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
8945}
8946
8947TEST_F(MultiTouchInputMapperTest, Process_UnCapturedTouchpadPointer) {
8948 std::shared_ptr<FakePointerController> fakePointerController =
8949 std::make_shared<FakePointerController>();
8950 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
8951 fakePointerController->setPosition(0, 0);
8952 fakePointerController->setButtonState(0);
8953
8954 // prepare device and capture
8955 prepareDisplay(DISPLAY_ORIENTATION_0);
8956 prepareAxes(POSITION | ID | SLOT);
8957 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
8958 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00008959 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08008960 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8961 // run uncaptured pointer tests - pushes out generic events
8962 // FINGER 0 DOWN
8963 processId(mapper, 3);
8964 processPosition(mapper, 100, 100);
8965 processKey(mapper, BTN_TOUCH, 1);
8966 processSync(mapper);
8967
8968 // start at (100,100), cursor should be at (0,0) * scale
8969 NotifyMotionArgs args;
8970 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8971 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
8972 ASSERT_NO_FATAL_FAILURE(
8973 assertPointerCoords(args.pointerCoords[0], 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
8974
8975 // FINGER 0 MOVE
8976 processPosition(mapper, 200, 200);
8977 processSync(mapper);
8978
8979 // compute scaling to help with touch position checking
8980 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
8981 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
8982 float scale =
8983 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
8984
8985 // translate from (100,100) -> (200,200), cursor should have changed to (100,100) * scale)
8986 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8987 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
8988 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 100 * scale, 100 * scale, 0,
8989 0, 0, 0, 0, 0, 0, 0));
8990}
8991
8992TEST_F(MultiTouchInputMapperTest, WhenCapturedAndNotCaptured_GetSources) {
8993 std::shared_ptr<FakePointerController> fakePointerController =
8994 std::make_shared<FakePointerController>();
8995
8996 prepareDisplay(DISPLAY_ORIENTATION_0);
8997 prepareAxes(POSITION | ID | SLOT);
8998 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00008999 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009000 mFakePolicy->setPointerCapture(false);
9001 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9002
9003 // uncaptured touchpad should be a pointer device
9004 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
9005
9006 // captured touchpad should be a touchpad device
9007 mFakePolicy->setPointerCapture(true);
9008 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
9009 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
9010}
9011
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009012// --- PeripheralControllerTest ---
Chris Yee2b1e5c2021-03-10 22:45:12 -08009013
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009014class PeripheralControllerTest : public testing::Test {
Chris Yee2b1e5c2021-03-10 22:45:12 -08009015protected:
9016 static const char* DEVICE_NAME;
9017 static const char* DEVICE_LOCATION;
9018 static const int32_t DEVICE_ID;
9019 static const int32_t DEVICE_GENERATION;
9020 static const int32_t DEVICE_CONTROLLER_NUMBER;
9021 static const Flags<InputDeviceClass> DEVICE_CLASSES;
9022 static const int32_t EVENTHUB_ID;
9023
9024 std::shared_ptr<FakeEventHub> mFakeEventHub;
9025 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07009026 std::unique_ptr<TestInputListener> mFakeListener;
Chris Yee2b1e5c2021-03-10 22:45:12 -08009027 std::unique_ptr<InstrumentedInputReader> mReader;
9028 std::shared_ptr<InputDevice> mDevice;
9029
9030 virtual void SetUp(Flags<InputDeviceClass> classes) {
9031 mFakeEventHub = std::make_unique<FakeEventHub>();
9032 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07009033 mFakeListener = std::make_unique<TestInputListener>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009034 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07009035 *mFakeListener);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009036 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
9037 }
9038
9039 void SetUp() override { SetUp(DEVICE_CLASSES); }
9040
9041 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07009042 mFakeListener.reset();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009043 mFakePolicy.clear();
9044 }
9045
9046 void configureDevice(uint32_t changes) {
9047 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
9048 mReader->requestRefreshConfiguration(changes);
9049 mReader->loopOnce();
9050 }
9051 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
9052 }
9053
9054 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
9055 const std::string& location, int32_t eventHubId,
9056 Flags<InputDeviceClass> classes) {
9057 InputDeviceIdentifier identifier;
9058 identifier.name = name;
9059 identifier.location = location;
9060 std::shared_ptr<InputDevice> device =
9061 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
9062 identifier);
9063 mReader->pushNextDevice(device);
9064 mFakeEventHub->addDevice(eventHubId, name, classes);
9065 mReader->loopOnce();
9066 return device;
9067 }
9068
9069 template <class T, typename... Args>
9070 T& addControllerAndConfigure(Args... args) {
9071 T& controller = mDevice->addController<T>(EVENTHUB_ID, args...);
9072
9073 return controller;
9074 }
9075};
9076
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009077const char* PeripheralControllerTest::DEVICE_NAME = "device";
9078const char* PeripheralControllerTest::DEVICE_LOCATION = "BLUETOOTH";
9079const int32_t PeripheralControllerTest::DEVICE_ID = END_RESERVED_ID + 1000;
9080const int32_t PeripheralControllerTest::DEVICE_GENERATION = 2;
9081const int32_t PeripheralControllerTest::DEVICE_CONTROLLER_NUMBER = 0;
9082const Flags<InputDeviceClass> PeripheralControllerTest::DEVICE_CLASSES =
Chris Yee2b1e5c2021-03-10 22:45:12 -08009083 Flags<InputDeviceClass>(0); // not needed for current tests
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009084const int32_t PeripheralControllerTest::EVENTHUB_ID = 1;
Chris Yee2b1e5c2021-03-10 22:45:12 -08009085
9086// --- BatteryControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009087class BatteryControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -08009088protected:
9089 void SetUp() override {
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009090 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::BATTERY);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009091 }
9092};
9093
9094TEST_F(BatteryControllerTest, GetBatteryCapacity) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009095 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009096
9097 ASSERT_TRUE(controller.getBatteryCapacity(DEFAULT_BATTERY));
9098 ASSERT_EQ(controller.getBatteryCapacity(DEFAULT_BATTERY).value_or(-1), BATTERY_CAPACITY);
9099}
9100
9101TEST_F(BatteryControllerTest, GetBatteryStatus) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009102 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009103
9104 ASSERT_TRUE(controller.getBatteryStatus(DEFAULT_BATTERY));
9105 ASSERT_EQ(controller.getBatteryStatus(DEFAULT_BATTERY).value_or(-1), BATTERY_STATUS);
9106}
9107
9108// --- LightControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009109class LightControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -08009110protected:
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009111 void SetUp() override {
9112 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::LIGHT);
9113 }
Chris Yee2b1e5c2021-03-10 22:45:12 -08009114};
9115
Chris Ye85758332021-05-16 23:05:17 -07009116TEST_F(LightControllerTest, MonoLight) {
9117 RawLightInfo infoMono = {.id = 1,
9118 .name = "Mono",
9119 .maxBrightness = 255,
9120 .flags = InputLightClass::BRIGHTNESS,
9121 .path = ""};
9122 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
Chris Yee2b1e5c2021-03-10 22:45:12 -08009123
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009124 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009125 InputDeviceInfo info;
9126 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009127 std::vector<InputDeviceLightInfo> lights = info.getLights();
9128 ASSERT_EQ(1U, lights.size());
9129 ASSERT_EQ(InputDeviceLightType::MONO, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009130
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009131 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
9132 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009133}
9134
9135TEST_F(LightControllerTest, RGBLight) {
9136 RawLightInfo infoRed = {.id = 1,
9137 .name = "red",
9138 .maxBrightness = 255,
9139 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
9140 .path = ""};
9141 RawLightInfo infoGreen = {.id = 2,
9142 .name = "green",
9143 .maxBrightness = 255,
9144 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
9145 .path = ""};
9146 RawLightInfo infoBlue = {.id = 3,
9147 .name = "blue",
9148 .maxBrightness = 255,
9149 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
9150 .path = ""};
9151 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
9152 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
9153 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
9154
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009155 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009156 InputDeviceInfo info;
9157 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009158 std::vector<InputDeviceLightInfo> lights = info.getLights();
9159 ASSERT_EQ(1U, lights.size());
9160 ASSERT_EQ(InputDeviceLightType::RGB, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009161
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009162 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
9163 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009164}
9165
9166TEST_F(LightControllerTest, MultiColorRGBLight) {
9167 RawLightInfo infoColor = {.id = 1,
9168 .name = "red",
9169 .maxBrightness = 255,
9170 .flags = InputLightClass::BRIGHTNESS |
9171 InputLightClass::MULTI_INTENSITY |
9172 InputLightClass::MULTI_INDEX,
9173 .path = ""};
9174
9175 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
9176
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009177 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009178 InputDeviceInfo info;
9179 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009180 std::vector<InputDeviceLightInfo> lights = info.getLights();
9181 ASSERT_EQ(1U, lights.size());
9182 ASSERT_EQ(InputDeviceLightType::MULTI_COLOR, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009183
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009184 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
9185 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009186}
9187
9188TEST_F(LightControllerTest, PlayerIdLight) {
9189 RawLightInfo info1 = {.id = 1,
9190 .name = "player1",
9191 .maxBrightness = 255,
9192 .flags = InputLightClass::BRIGHTNESS,
9193 .path = ""};
9194 RawLightInfo info2 = {.id = 2,
9195 .name = "player2",
9196 .maxBrightness = 255,
9197 .flags = InputLightClass::BRIGHTNESS,
9198 .path = ""};
9199 RawLightInfo info3 = {.id = 3,
9200 .name = "player3",
9201 .maxBrightness = 255,
9202 .flags = InputLightClass::BRIGHTNESS,
9203 .path = ""};
9204 RawLightInfo info4 = {.id = 4,
9205 .name = "player4",
9206 .maxBrightness = 255,
9207 .flags = InputLightClass::BRIGHTNESS,
9208 .path = ""};
9209 mFakeEventHub->addRawLightInfo(info1.id, std::move(info1));
9210 mFakeEventHub->addRawLightInfo(info2.id, std::move(info2));
9211 mFakeEventHub->addRawLightInfo(info3.id, std::move(info3));
9212 mFakeEventHub->addRawLightInfo(info4.id, std::move(info4));
9213
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009214 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009215 InputDeviceInfo info;
9216 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009217 std::vector<InputDeviceLightInfo> lights = info.getLights();
9218 ASSERT_EQ(1U, lights.size());
9219 ASSERT_EQ(InputDeviceLightType::PLAYER_ID, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009220
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009221 ASSERT_FALSE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
9222 ASSERT_TRUE(controller.setLightPlayerId(lights[0].id, LIGHT_PLAYER_ID));
9223 ASSERT_EQ(controller.getLightPlayerId(lights[0].id).value_or(-1), LIGHT_PLAYER_ID);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009224}
9225
Michael Wrightd02c5b62014-02-10 15:10:22 -08009226} // namespace android