blob: b28c1e2401dec079d307731524cecb5a0d25944e [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,
1189 const sp<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 Vishniakou473174e2017-12-27 16:44:42 -08001502 sp<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 Vishniakou473174e2017-12-27 16:44:42 -08001510 mFakeListener = new TestInputListener();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001511
Prabir Pradhan28efc192019-11-05 01:10:04 +00001512 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
1513 mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001514 }
1515
Chris Yea52ade12020-08-27 16:49:20 -07001516 void TearDown() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001517 mFakeListener.clear();
1518 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:
2148 sp<TestInputListener> mTestListener;
2149 sp<FakeInputReaderPolicy> mFakePolicy;
2150 sp<InputReaderInterface> mReader;
2151
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 Vishniakouf0db5b82020-04-08 19:22:14 -07002158 mTestListener = new TestInputListener(2000ms /*eventHappenedTimeout*/,
2159 30ms /*eventDidNotHappenTimeout*/);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002160
Prabir Pradhan9244aea2020-02-05 20:31:40 -08002161 mReader = new InputReader(std::make_shared<EventHub>(), mFakePolicy, mTestListener);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002162 ASSERT_EQ(mReader->start(), OK);
2163
2164 // Since this test is run on a real device, all the input devices connected
2165 // to the test device will show up in mReader. We wait for those input devices to
2166 // show up before beginning the tests.
2167 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2168 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2169 }
2170
Chris Yea52ade12020-08-27 16:49:20 -07002171 void TearDown() override {
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002172 ASSERT_EQ(mReader->stop(), OK);
2173 mTestListener.clear();
2174 mFakePolicy.clear();
2175 }
2176};
2177
2178TEST_F(InputReaderIntegrationTest, TestInvalidDevice) {
2179 // An invalid input device that is only used for this test.
2180 class InvalidUinputDevice : public UinputDevice {
2181 public:
2182 InvalidUinputDevice() : UinputDevice("Invalid Device") {}
2183
2184 private:
2185 void configureDevice(int fd, uinput_user_dev* device) override {}
2186 };
2187
2188 const size_t numDevices = mFakePolicy->getInputDevices().size();
2189
2190 // UinputDevice does not set any event or key bits, so InputReader should not
2191 // consider it as a valid device.
2192 std::unique_ptr<UinputDevice> invalidDevice = createUinputDevice<InvalidUinputDevice>();
2193 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
2194 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
2195 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
2196
2197 invalidDevice.reset();
2198 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
2199 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
2200 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
2201}
2202
2203TEST_F(InputReaderIntegrationTest, AddNewDevice) {
2204 const size_t initialNumDevices = mFakePolicy->getInputDevices().size();
2205
2206 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
2207 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2208 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2209 ASSERT_EQ(initialNumDevices + 1, mFakePolicy->getInputDevices().size());
2210
2211 // Find the test device by its name.
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002212 const std::vector<InputDeviceInfo> inputDevices = mFakePolicy->getInputDevices();
Chris Ye98d3f532020-10-01 21:48:59 -07002213 const auto& it =
2214 std::find_if(inputDevices.begin(), inputDevices.end(),
2215 [&keyboard](const InputDeviceInfo& info) {
2216 return info.getIdentifier().name == keyboard->getName();
2217 });
2218
2219 ASSERT_NE(it, inputDevices.end());
2220 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, it->getKeyboardType());
2221 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, it->getSources());
2222 ASSERT_EQ(0U, it->getMotionRanges().size());
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002223
2224 keyboard.reset();
2225 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2226 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2227 ASSERT_EQ(initialNumDevices, mFakePolicy->getInputDevices().size());
2228}
2229
2230TEST_F(InputReaderIntegrationTest, SendsEventsToInputListener) {
2231 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
2232 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2233
2234 NotifyConfigurationChangedArgs configChangedArgs;
2235 ASSERT_NO_FATAL_FAILURE(
2236 mTestListener->assertNotifyConfigurationChangedWasCalled(&configChangedArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002237 int32_t prevId = configChangedArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002238 nsecs_t prevTimestamp = configChangedArgs.eventTime;
2239
2240 NotifyKeyArgs keyArgs;
2241 keyboard->pressAndReleaseHomeKey();
2242 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
2243 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002244 ASSERT_NE(prevId, keyArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002245 prevId = keyArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002246 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002247 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002248 prevTimestamp = keyArgs.eventTime;
2249
2250 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
2251 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002252 ASSERT_NE(prevId, keyArgs.id);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002253 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002254 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002255}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002256
Siarhei Vishniakoua0d2b802020-05-13 14:00:31 -07002257/**
2258 * The Steam controller sends BTN_GEAR_DOWN and BTN_GEAR_UP for the two "paddle" buttons
2259 * on the back. In this test, we make sure that BTN_GEAR_DOWN / BTN_WHEEL and BTN_GEAR_UP
2260 * are passed to the listener.
2261 */
2262static_assert(BTN_GEAR_DOWN == BTN_WHEEL);
2263TEST_F(InputReaderIntegrationTest, SendsGearDownAndUpToInputListener) {
2264 std::unique_ptr<UinputSteamController> controller = createUinputDevice<UinputSteamController>();
2265 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2266 NotifyKeyArgs keyArgs;
2267
2268 controller->pressAndReleaseKey(BTN_GEAR_DOWN);
2269 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
2270 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
2271 ASSERT_EQ(BTN_GEAR_DOWN, keyArgs.scanCode);
2272
2273 controller->pressAndReleaseKey(BTN_GEAR_UP);
2274 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
2275 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
2276 ASSERT_EQ(BTN_GEAR_UP, keyArgs.scanCode);
2277}
2278
Arthur Hungaab25622020-01-16 11:22:11 +08002279// --- TouchProcessTest ---
2280class TouchIntegrationTest : public InputReaderIntegrationTest {
2281protected:
Arthur Hungaab25622020-01-16 11:22:11 +08002282 const std::string UNIQUE_ID = "local:0";
2283
Chris Yea52ade12020-08-27 16:49:20 -07002284 void SetUp() override {
Arthur Hungaab25622020-01-16 11:22:11 +08002285 InputReaderIntegrationTest::SetUp();
2286 // At least add an internal display.
2287 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2288 DISPLAY_ORIENTATION_0, UNIQUE_ID, NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002289 ViewportType::INTERNAL);
Arthur Hungaab25622020-01-16 11:22:11 +08002290
2291 mDevice = createUinputDevice<UinputTouchScreen>(Rect(0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT));
2292 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2293 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2294 }
2295
2296 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
2297 int32_t orientation, const std::string& uniqueId,
2298 std::optional<uint8_t> physicalPort,
2299 ViewportType viewportType) {
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002300 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, true /*isActive*/,
2301 uniqueId, physicalPort, viewportType);
Arthur Hungaab25622020-01-16 11:22:11 +08002302 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2303 }
2304
2305 std::unique_ptr<UinputTouchScreen> mDevice;
2306};
2307
2308TEST_F(TouchIntegrationTest, InputEvent_ProcessSingleTouch) {
2309 NotifyMotionArgs args;
2310 const Point centerPoint = mDevice->getCenterPoint();
2311
2312 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00002313 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002314 mDevice->sendDown(centerPoint);
2315 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2316 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2317
2318 // ACTION_MOVE
2319 mDevice->sendMove(centerPoint + Point(1, 1));
2320 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2321 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2322
2323 // ACTION_UP
2324 mDevice->sendUp();
2325 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2326 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2327}
2328
2329TEST_F(TouchIntegrationTest, InputEvent_ProcessMultiTouch) {
2330 NotifyMotionArgs args;
2331 const Point centerPoint = mDevice->getCenterPoint();
2332
2333 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00002334 mDevice->sendSlot(FIRST_SLOT);
2335 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002336 mDevice->sendDown(centerPoint);
2337 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2338 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2339
2340 // ACTION_POINTER_DOWN (Second slot)
2341 const Point secondPoint = centerPoint + Point(100, 100);
2342 mDevice->sendSlot(SECOND_SLOT);
2343 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2344 mDevice->sendDown(secondPoint + Point(1, 1));
2345 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2346 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
2347 args.action);
2348
2349 // ACTION_MOVE (Second slot)
2350 mDevice->sendMove(secondPoint);
2351 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2352 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2353
2354 // ACTION_POINTER_UP (Second slot)
arthurhungcc7f9802020-04-30 17:55:40 +08002355 mDevice->sendPointerUp();
Arthur Hungaab25622020-01-16 11:22:11 +08002356 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
arthurhungcc7f9802020-04-30 17:55:40 +08002357 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
Arthur Hungaab25622020-01-16 11:22:11 +08002358 args.action);
2359
2360 // ACTION_UP
2361 mDevice->sendSlot(FIRST_SLOT);
2362 mDevice->sendUp();
2363 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2364 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2365}
2366
2367TEST_F(TouchIntegrationTest, InputEvent_ProcessPalm) {
2368 NotifyMotionArgs args;
2369 const Point centerPoint = mDevice->getCenterPoint();
2370
2371 // ACTION_DOWN
arthurhungcc7f9802020-04-30 17:55:40 +08002372 mDevice->sendSlot(FIRST_SLOT);
2373 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002374 mDevice->sendDown(centerPoint);
2375 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2376 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2377
arthurhungcc7f9802020-04-30 17:55:40 +08002378 // ACTION_POINTER_DOWN (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002379 const Point secondPoint = centerPoint + Point(100, 100);
2380 mDevice->sendSlot(SECOND_SLOT);
2381 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2382 mDevice->sendDown(secondPoint);
2383 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2384 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
2385 args.action);
2386
arthurhungcc7f9802020-04-30 17:55:40 +08002387 // ACTION_MOVE (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002388 mDevice->sendMove(secondPoint + Point(1, 1));
2389 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2390 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2391
arthurhungcc7f9802020-04-30 17:55:40 +08002392 // Send MT_TOOL_PALM (second slot), which indicates that the touch IC has determined this to be
2393 // a palm event.
2394 // Expect to receive the ACTION_POINTER_UP with cancel flag.
Arthur Hungaab25622020-01-16 11:22:11 +08002395 mDevice->sendToolType(MT_TOOL_PALM);
2396 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
arthurhungcc7f9802020-04-30 17:55:40 +08002397 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
2398 args.action);
2399 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, args.flags);
Arthur Hungaab25622020-01-16 11:22:11 +08002400
arthurhungcc7f9802020-04-30 17:55:40 +08002401 // Send up to second slot, expect first slot send moving.
2402 mDevice->sendPointerUp();
2403 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2404 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002405
arthurhungcc7f9802020-04-30 17:55:40 +08002406 // Send ACTION_UP (first slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002407 mDevice->sendSlot(FIRST_SLOT);
2408 mDevice->sendUp();
2409
arthurhungcc7f9802020-04-30 17:55:40 +08002410 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2411 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002412}
2413
Michael Wrightd02c5b62014-02-10 15:10:22 -08002414// --- InputDeviceTest ---
Michael Wrightd02c5b62014-02-10 15:10:22 -08002415class InputDeviceTest : public testing::Test {
2416protected:
2417 static const char* DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002418 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002419 static const int32_t DEVICE_ID;
2420 static const int32_t DEVICE_GENERATION;
2421 static const int32_t DEVICE_CONTROLLER_NUMBER;
Chris Ye1b0c7342020-07-28 21:57:03 -07002422 static const Flags<InputDeviceClass> DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002423 static const int32_t EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002424
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002425 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002426 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08002427 sp<TestInputListener> mFakeListener;
arthurhungdcef2dc2020-08-11 14:47:50 +08002428 std::unique_ptr<InstrumentedInputReader> mReader;
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00002429 std::shared_ptr<InputDevice> mDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002430
Chris Yea52ade12020-08-27 16:49:20 -07002431 void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002432 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002433 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08002434 mFakeListener = new TestInputListener();
arthurhungdcef2dc2020-08-11 14:47:50 +08002435 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
2436 mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002437 InputDeviceIdentifier identifier;
2438 identifier.name = DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002439 identifier.location = DEVICE_LOCATION;
arthurhungdcef2dc2020-08-11 14:47:50 +08002440 mDevice = std::make_shared<InputDevice>(mReader->getContext(), DEVICE_ID, DEVICE_GENERATION,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002441 identifier);
arthurhungdcef2dc2020-08-11 14:47:50 +08002442 mReader->pushNextDevice(mDevice);
2443 mFakeEventHub->addDevice(EVENTHUB_ID, DEVICE_NAME, Flags<InputDeviceClass>(0));
2444 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002445 }
2446
Chris Yea52ade12020-08-27 16:49:20 -07002447 void TearDown() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002448 mFakeListener.clear();
2449 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002450 }
2451};
2452
2453const char* InputDeviceTest::DEVICE_NAME = "device";
Arthur Hung2c9a3342019-07-23 14:18:59 +08002454const char* InputDeviceTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002455const int32_t InputDeviceTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002456const int32_t InputDeviceTest::DEVICE_GENERATION = 2;
2457const int32_t InputDeviceTest::DEVICE_CONTROLLER_NUMBER = 0;
Chris Ye1b0c7342020-07-28 21:57:03 -07002458const Flags<InputDeviceClass> InputDeviceTest::DEVICE_CLASSES =
2459 InputDeviceClass::KEYBOARD | InputDeviceClass::TOUCH | InputDeviceClass::JOYSTICK;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002460const int32_t InputDeviceTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002461
2462TEST_F(InputDeviceTest, ImmutableProperties) {
2463 ASSERT_EQ(DEVICE_ID, mDevice->getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002464 ASSERT_STREQ(DEVICE_NAME, mDevice->getName().c_str());
Chris Ye1b0c7342020-07-28 21:57:03 -07002465 ASSERT_EQ(Flags<InputDeviceClass>(0), mDevice->getClasses());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002466}
2467
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002468TEST_F(InputDeviceTest, WhenDeviceCreated_EnabledIsFalse) {
2469 ASSERT_EQ(mDevice->isEnabled(), false);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07002470}
2471
Michael Wrightd02c5b62014-02-10 15:10:22 -08002472TEST_F(InputDeviceTest, WhenNoMappersAreRegistered_DeviceIsIgnored) {
2473 // Configuration.
2474 InputReaderConfiguration config;
2475 mDevice->configure(ARBITRARY_TIME, &config, 0);
2476
2477 // Reset.
2478 mDevice->reset(ARBITRARY_TIME);
2479
2480 NotifyDeviceResetArgs resetArgs;
2481 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2482 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2483 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2484
2485 // Metadata.
2486 ASSERT_TRUE(mDevice->isIgnored());
2487 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mDevice->getSources());
2488
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002489 InputDeviceInfo info = mDevice->getDeviceInfo();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002490 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002491 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002492 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NONE, info.getKeyboardType());
2493 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, info.getSources());
2494
2495 // State queries.
2496 ASSERT_EQ(0, mDevice->getMetaState());
2497
2498 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2499 << "Ignored device should return unknown key code state.";
2500 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2501 << "Ignored device should return unknown scan code state.";
2502 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 0))
2503 << "Ignored device should return unknown switch state.";
2504
2505 const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
2506 uint8_t flags[2] = { 0, 1 };
2507 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, 2, keyCodes, flags))
2508 << "Ignored device should never mark any key codes.";
2509 ASSERT_EQ(0, flags[0]) << "Flag for unsupported key should be unchanged.";
2510 ASSERT_EQ(1, flags[1]) << "Flag for unsupported key should be unchanged.";
2511}
2512
2513TEST_F(InputDeviceTest, WhenMappersAreRegistered_DeviceIsNotIgnoredAndForwardsRequestsToMappers) {
2514 // Configuration.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002515 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, String8("key"), String8("value"));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002516
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002517 FakeInputMapper& mapper1 =
2518 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002519 mapper1.setKeyboardType(AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2520 mapper1.setMetaState(AMETA_ALT_ON);
2521 mapper1.addSupportedKeyCode(AKEYCODE_A);
2522 mapper1.addSupportedKeyCode(AKEYCODE_B);
2523 mapper1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
2524 mapper1.setKeyCodeState(AKEYCODE_B, AKEY_STATE_UP);
2525 mapper1.setScanCodeState(2, AKEY_STATE_DOWN);
2526 mapper1.setScanCodeState(3, AKEY_STATE_UP);
2527 mapper1.setSwitchState(4, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002528
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002529 FakeInputMapper& mapper2 =
2530 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002531 mapper2.setMetaState(AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002532
2533 InputReaderConfiguration config;
2534 mDevice->configure(ARBITRARY_TIME, &config, 0);
2535
2536 String8 propertyValue;
2537 ASSERT_TRUE(mDevice->getConfiguration().tryGetProperty(String8("key"), propertyValue))
2538 << "Device should have read configuration during configuration phase.";
2539 ASSERT_STREQ("value", propertyValue.string());
2540
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002541 ASSERT_NO_FATAL_FAILURE(mapper1.assertConfigureWasCalled());
2542 ASSERT_NO_FATAL_FAILURE(mapper2.assertConfigureWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002543
2544 // Reset
2545 mDevice->reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002546 ASSERT_NO_FATAL_FAILURE(mapper1.assertResetWasCalled());
2547 ASSERT_NO_FATAL_FAILURE(mapper2.assertResetWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002548
2549 NotifyDeviceResetArgs resetArgs;
2550 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2551 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2552 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2553
2554 // Metadata.
2555 ASSERT_FALSE(mDevice->isIgnored());
2556 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), mDevice->getSources());
2557
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002558 InputDeviceInfo info = mDevice->getDeviceInfo();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002559 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002560 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002561 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_ALPHABETIC, info.getKeyboardType());
2562 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), info.getSources());
2563
2564 // State queries.
2565 ASSERT_EQ(AMETA_ALT_ON | AMETA_SHIFT_ON, mDevice->getMetaState())
2566 << "Should query mappers and combine meta states.";
2567
2568 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2569 << "Should return unknown key code state when source not supported.";
2570 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2571 << "Should return unknown scan code state when source not supported.";
2572 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2573 << "Should return unknown switch state when source not supported.";
2574
2575 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, AKEYCODE_A))
2576 << "Should query mapper when source is supported.";
2577 ASSERT_EQ(AKEY_STATE_UP, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 3))
2578 << "Should query mapper when source is supported.";
2579 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 4))
2580 << "Should query mapper when source is supported.";
2581
2582 const int32_t keyCodes[4] = { AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2 };
2583 uint8_t flags[4] = { 0, 0, 0, 1 };
2584 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
2585 << "Should do nothing when source is unsupported.";
2586 ASSERT_EQ(0, flags[0]) << "Flag should be unchanged when source is unsupported.";
2587 ASSERT_EQ(0, flags[1]) << "Flag should be unchanged when source is unsupported.";
2588 ASSERT_EQ(0, flags[2]) << "Flag should be unchanged when source is unsupported.";
2589 ASSERT_EQ(1, flags[3]) << "Flag should be unchanged when source is unsupported.";
2590
2591 ASSERT_TRUE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, 4, keyCodes, flags))
2592 << "Should query mapper when source is supported.";
2593 ASSERT_EQ(1, flags[0]) << "Flag for supported key should be set.";
2594 ASSERT_EQ(1, flags[1]) << "Flag for supported key should be set.";
2595 ASSERT_EQ(0, flags[2]) << "Flag for unsupported key should be unchanged.";
2596 ASSERT_EQ(1, flags[3]) << "Flag for unsupported key should be unchanged.";
2597
2598 // Event handling.
2599 RawEvent event;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002600 event.deviceId = EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002601 mDevice->process(&event, 1);
2602
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002603 ASSERT_NO_FATAL_FAILURE(mapper1.assertProcessWasCalled());
2604 ASSERT_NO_FATAL_FAILURE(mapper2.assertProcessWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002605}
2606
Arthur Hung2c9a3342019-07-23 14:18:59 +08002607// A single input device is associated with a specific display. Check that:
2608// 1. Device is disabled if the viewport corresponding to the associated display is not found
2609// 2. Device is disabled when setEnabled API is called
2610TEST_F(InputDeviceTest, Configure_AssignsDisplayPort) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002611 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002612
2613 // First Configuration.
2614 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
2615
2616 // Device should be enabled by default.
2617 ASSERT_TRUE(mDevice->isEnabled());
2618
2619 // Prepare associated info.
2620 constexpr uint8_t hdmi = 1;
2621 const std::string UNIQUE_ID = "local:1";
2622
2623 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi);
2624 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2625 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2626 // Device should be disabled because it is associated with a specific display via
2627 // input port <-> display port association, but the corresponding display is not found
2628 ASSERT_FALSE(mDevice->isEnabled());
2629
2630 // Prepare displays.
2631 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002632 DISPLAY_ORIENTATION_0, true /*isActive*/, UNIQUE_ID, hdmi,
2633 ViewportType::INTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002634 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2635 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2636 ASSERT_TRUE(mDevice->isEnabled());
2637
2638 // Device should be disabled after set disable.
2639 mFakePolicy->addDisabledDevice(mDevice->getId());
2640 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2641 InputReaderConfiguration::CHANGE_ENABLED_STATE);
2642 ASSERT_FALSE(mDevice->isEnabled());
2643
2644 // Device should still be disabled even found the associated display.
2645 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2646 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2647 ASSERT_FALSE(mDevice->isEnabled());
2648}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002649
Christine Franks1ba71cc2021-04-07 14:37:42 -07002650TEST_F(InputDeviceTest, Configure_AssignsDisplayUniqueId) {
2651 // Device should be enabled by default.
2652 mFakePolicy->clearViewports();
2653 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
2654 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
2655 ASSERT_TRUE(mDevice->isEnabled());
2656
2657 // Device should be disabled because it is associated with a specific display, but the
2658 // corresponding display is not found.
2659 const std::string DISPLAY_UNIQUE_ID = "displayUniqueId";
2660 mFakePolicy->addInputUniqueIdAssociation(DEVICE_NAME, DISPLAY_UNIQUE_ID);
2661 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2662 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2663 ASSERT_FALSE(mDevice->isEnabled());
2664
2665 // Device should be enabled when a display is found.
2666 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2667 DISPLAY_ORIENTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
2668 NO_PORT, ViewportType::INTERNAL);
2669 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2670 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2671 ASSERT_TRUE(mDevice->isEnabled());
2672
2673 // Device should be disabled after set disable.
2674 mFakePolicy->addDisabledDevice(mDevice->getId());
2675 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2676 InputReaderConfiguration::CHANGE_ENABLED_STATE);
2677 ASSERT_FALSE(mDevice->isEnabled());
2678
2679 // Device should still be disabled even found the associated display.
2680 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2681 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2682 ASSERT_FALSE(mDevice->isEnabled());
2683}
2684
Michael Wrightd02c5b62014-02-10 15:10:22 -08002685// --- InputMapperTest ---
2686
2687class InputMapperTest : public testing::Test {
2688protected:
2689 static const char* DEVICE_NAME;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002690 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002691 static const int32_t DEVICE_ID;
2692 static const int32_t DEVICE_GENERATION;
2693 static const int32_t DEVICE_CONTROLLER_NUMBER;
Chris Ye1b0c7342020-07-28 21:57:03 -07002694 static const Flags<InputDeviceClass> DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002695 static const int32_t EVENTHUB_ID;
Prabir Pradhanc14266f2021-05-12 15:56:24 -07002696 static const std::optional<bool> INITIAL_PER_WINDOW_INPUT_ROTATION_FLAG_VALUE;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002697
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002698 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002699 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08002700 sp<TestInputListener> mFakeListener;
arthurhungdcef2dc2020-08-11 14:47:50 +08002701 std::unique_ptr<InstrumentedInputReader> mReader;
2702 std::shared_ptr<InputDevice> mDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002703
Chris Ye1b0c7342020-07-28 21:57:03 -07002704 virtual void SetUp(Flags<InputDeviceClass> classes) {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002705 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002706 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08002707 mFakeListener = new TestInputListener();
arthurhungdcef2dc2020-08-11 14:47:50 +08002708 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
2709 mFakeListener);
2710 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002711 }
2712
Prabir Pradhanc14266f2021-05-12 15:56:24 -07002713 void SetUp() override {
2714 // Ensure per_window_input_rotation is enabled.
2715 sysprop::InputFlingerProperties::per_window_input_rotation(true);
2716
2717 SetUp(DEVICE_CLASSES);
2718 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002719
Chris Yea52ade12020-08-27 16:49:20 -07002720 void TearDown() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002721 mFakeListener.clear();
2722 mFakePolicy.clear();
Prabir Pradhanc14266f2021-05-12 15:56:24 -07002723
2724 sysprop::InputFlingerProperties::per_window_input_rotation(
2725 INITIAL_PER_WINDOW_INPUT_ROTATION_FLAG_VALUE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002726 }
2727
2728 void addConfigurationProperty(const char* key, const char* value) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002729 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, String8(key), String8(value));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002730 }
2731
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002732 void configureDevice(uint32_t changes) {
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -08002733 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
arthurhungdcef2dc2020-08-11 14:47:50 +08002734 mReader->requestRefreshConfiguration(changes);
2735 mReader->loopOnce();
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -08002736 }
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002737 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
2738 }
2739
arthurhungdcef2dc2020-08-11 14:47:50 +08002740 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
2741 const std::string& location, int32_t eventHubId,
2742 Flags<InputDeviceClass> classes) {
2743 InputDeviceIdentifier identifier;
2744 identifier.name = name;
2745 identifier.location = location;
2746 std::shared_ptr<InputDevice> device =
2747 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
2748 identifier);
2749 mReader->pushNextDevice(device);
2750 mFakeEventHub->addDevice(eventHubId, name, classes);
2751 mReader->loopOnce();
2752 return device;
2753 }
2754
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002755 template <class T, typename... Args>
2756 T& addMapperAndConfigure(Args... args) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002757 T& mapper = mDevice->addMapper<T>(EVENTHUB_ID, args...);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002758 configureDevice(0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002759 mDevice->reset(ARBITRARY_TIME);
Chris Ye42b06822020-08-07 11:39:33 -07002760 mapper.reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002761 return mapper;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002762 }
2763
2764 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002765 int32_t orientation, const std::string& uniqueId,
2766 std::optional<uint8_t> physicalPort, ViewportType viewportType) {
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002767 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, true /*isActive*/,
2768 uniqueId, physicalPort, viewportType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07002769 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2770 }
2771
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002772 void clearViewports() {
2773 mFakePolicy->clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002774 }
2775
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002776 void process(InputMapper& mapper, nsecs_t when, nsecs_t readTime, int32_t type, int32_t code,
2777 int32_t value) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002778 RawEvent event;
2779 event.when = when;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002780 event.readTime = readTime;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002781 event.deviceId = mapper.getDeviceContext().getEventHubId();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002782 event.type = type;
2783 event.code = code;
2784 event.value = value;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002785 mapper.process(&event);
arthurhungdcef2dc2020-08-11 14:47:50 +08002786 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002787 }
2788
2789 static void assertMotionRange(const InputDeviceInfo& info,
2790 int32_t axis, uint32_t source, float min, float max, float flat, float fuzz) {
2791 const InputDeviceInfo::MotionRange* range = info.getMotionRange(axis, source);
Yi Kong9b14ac62018-07-17 13:48:38 -07002792 ASSERT_TRUE(range != nullptr) << "Axis: " << axis << " Source: " << source;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002793 ASSERT_EQ(axis, range->axis) << "Axis: " << axis << " Source: " << source;
2794 ASSERT_EQ(source, range->source) << "Axis: " << axis << " Source: " << source;
2795 ASSERT_NEAR(min, range->min, EPSILON) << "Axis: " << axis << " Source: " << source;
2796 ASSERT_NEAR(max, range->max, EPSILON) << "Axis: " << axis << " Source: " << source;
2797 ASSERT_NEAR(flat, range->flat, EPSILON) << "Axis: " << axis << " Source: " << source;
2798 ASSERT_NEAR(fuzz, range->fuzz, EPSILON) << "Axis: " << axis << " Source: " << source;
2799 }
2800
Prabir Pradhanf5334b82021-05-13 14:00:39 -07002801 static void assertPointerCoords(const PointerCoords& coords, float x, float y, float pressure,
2802 float size, float touchMajor, float touchMinor, float toolMajor,
2803 float toolMinor, float orientation, float distance,
2804 float scaledAxisEpsilon = 1.f) {
2805 ASSERT_NEAR(x, coords.getAxisValue(AMOTION_EVENT_AXIS_X), scaledAxisEpsilon);
2806 ASSERT_NEAR(y, coords.getAxisValue(AMOTION_EVENT_AXIS_Y), scaledAxisEpsilon);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002807 ASSERT_NEAR(pressure, coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), EPSILON);
2808 ASSERT_NEAR(size, coords.getAxisValue(AMOTION_EVENT_AXIS_SIZE), EPSILON);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07002809 ASSERT_NEAR(touchMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR),
2810 scaledAxisEpsilon);
2811 ASSERT_NEAR(touchMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR),
2812 scaledAxisEpsilon);
2813 ASSERT_NEAR(toolMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR),
2814 scaledAxisEpsilon);
2815 ASSERT_NEAR(toolMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR),
2816 scaledAxisEpsilon);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002817 ASSERT_NEAR(orientation, coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION), EPSILON);
2818 ASSERT_NEAR(distance, coords.getAxisValue(AMOTION_EVENT_AXIS_DISTANCE), EPSILON);
2819 }
2820
Michael Wright17db18e2020-06-26 20:51:44 +01002821 static void assertPosition(const FakePointerController& controller, float x, float y) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002822 float actualX, actualY;
Michael Wright17db18e2020-06-26 20:51:44 +01002823 controller.getPosition(&actualX, &actualY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002824 ASSERT_NEAR(x, actualX, 1);
2825 ASSERT_NEAR(y, actualY, 1);
2826 }
2827};
2828
2829const char* InputMapperTest::DEVICE_NAME = "device";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002830const char* InputMapperTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002831const int32_t InputMapperTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002832const int32_t InputMapperTest::DEVICE_GENERATION = 2;
2833const int32_t InputMapperTest::DEVICE_CONTROLLER_NUMBER = 0;
Chris Ye1b0c7342020-07-28 21:57:03 -07002834const Flags<InputDeviceClass> InputMapperTest::DEVICE_CLASSES =
2835 Flags<InputDeviceClass>(0); // not needed for current tests
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002836const int32_t InputMapperTest::EVENTHUB_ID = 1;
Prabir Pradhanc14266f2021-05-12 15:56:24 -07002837const std::optional<bool> InputMapperTest::INITIAL_PER_WINDOW_INPUT_ROTATION_FLAG_VALUE =
2838 sysprop::InputFlingerProperties::per_window_input_rotation();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002839
2840// --- SwitchInputMapperTest ---
2841
2842class SwitchInputMapperTest : public InputMapperTest {
2843protected:
2844};
2845
2846TEST_F(SwitchInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002847 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002848
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002849 ASSERT_EQ(uint32_t(AINPUT_SOURCE_SWITCH), mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002850}
2851
2852TEST_F(SwitchInputMapperTest, GetSwitchState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002853 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002854
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002855 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002856 ASSERT_EQ(1, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002857
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002858 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002859 ASSERT_EQ(0, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002860}
2861
2862TEST_F(SwitchInputMapperTest, Process) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002863 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002864
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002865 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_LID, 1);
2866 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_JACK_PHYSICAL_INSERT, 1);
2867 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_HEADPHONE_INSERT, 0);
2868 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002869
2870 NotifySwitchArgs args;
2871 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySwitchWasCalled(&args));
2872 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
Dan Albert1bd2fc02016-02-02 15:11:57 -08002873 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT), args.switchValues);
2874 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT) | (1 << SW_HEADPHONE_INSERT),
Michael Wrightd02c5b62014-02-10 15:10:22 -08002875 args.switchMask);
2876 ASSERT_EQ(uint32_t(0), args.policyFlags);
2877}
2878
Chris Ye87143712020-11-10 05:05:58 +00002879// --- VibratorInputMapperTest ---
2880class VibratorInputMapperTest : public InputMapperTest {
2881protected:
2882 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::VIBRATOR); }
2883};
2884
2885TEST_F(VibratorInputMapperTest, GetSources) {
2886 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
2887
2888 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mapper.getSources());
2889}
2890
2891TEST_F(VibratorInputMapperTest, GetVibratorIds) {
2892 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
2893
2894 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
2895}
2896
2897TEST_F(VibratorInputMapperTest, Vibrate) {
2898 constexpr uint8_t DEFAULT_AMPLITUDE = 192;
Chris Yefb552902021-02-03 17:18:37 -08002899 constexpr int32_t VIBRATION_TOKEN = 100;
Chris Ye87143712020-11-10 05:05:58 +00002900 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
2901
2902 VibrationElement pattern(2);
2903 VibrationSequence sequence(2);
2904 pattern.duration = std::chrono::milliseconds(200);
2905 pattern.channels = {{0 /* vibratorId */, DEFAULT_AMPLITUDE / 2},
2906 {1 /* vibratorId */, DEFAULT_AMPLITUDE}};
2907 sequence.addElement(pattern);
2908 pattern.duration = std::chrono::milliseconds(500);
2909 pattern.channels = {{0 /* vibratorId */, DEFAULT_AMPLITUDE / 4},
2910 {1 /* vibratorId */, DEFAULT_AMPLITUDE}};
2911 sequence.addElement(pattern);
2912
2913 std::vector<int64_t> timings = {0, 1};
2914 std::vector<uint8_t> amplitudes = {DEFAULT_AMPLITUDE, DEFAULT_AMPLITUDE / 2};
2915
2916 ASSERT_FALSE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08002917 // Start vibrating
2918 mapper.vibrate(sequence, -1 /* repeat */, VIBRATION_TOKEN);
Chris Ye87143712020-11-10 05:05:58 +00002919 ASSERT_TRUE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08002920 // Verify vibrator state listener was notified.
2921 mReader->loopOnce();
2922 NotifyVibratorStateArgs args;
2923 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyVibratorStateWasCalled(&args));
2924 ASSERT_EQ(DEVICE_ID, args.deviceId);
2925 ASSERT_TRUE(args.isOn);
2926 // Stop vibrating
2927 mapper.cancelVibrate(VIBRATION_TOKEN);
2928 ASSERT_FALSE(mapper.isVibrating());
2929 // Verify vibrator state listener was notified.
2930 mReader->loopOnce();
2931 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyVibratorStateWasCalled(&args));
2932 ASSERT_EQ(DEVICE_ID, args.deviceId);
2933 ASSERT_FALSE(args.isOn);
Chris Ye87143712020-11-10 05:05:58 +00002934}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002935
Chris Yef59a2f42020-10-16 12:55:26 -07002936// --- SensorInputMapperTest ---
2937
2938class SensorInputMapperTest : public InputMapperTest {
2939protected:
2940 static const int32_t ACCEL_RAW_MIN;
2941 static const int32_t ACCEL_RAW_MAX;
2942 static const int32_t ACCEL_RAW_FUZZ;
2943 static const int32_t ACCEL_RAW_FLAT;
2944 static const int32_t ACCEL_RAW_RESOLUTION;
2945
2946 static const int32_t GYRO_RAW_MIN;
2947 static const int32_t GYRO_RAW_MAX;
2948 static const int32_t GYRO_RAW_FUZZ;
2949 static const int32_t GYRO_RAW_FLAT;
2950 static const int32_t GYRO_RAW_RESOLUTION;
2951
2952 static const float GRAVITY_MS2_UNIT;
2953 static const float DEGREE_RADIAN_UNIT;
2954
2955 void prepareAccelAxes();
2956 void prepareGyroAxes();
2957 void setAccelProperties();
2958 void setGyroProperties();
2959 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::SENSOR); }
2960};
2961
2962const int32_t SensorInputMapperTest::ACCEL_RAW_MIN = -32768;
2963const int32_t SensorInputMapperTest::ACCEL_RAW_MAX = 32768;
2964const int32_t SensorInputMapperTest::ACCEL_RAW_FUZZ = 16;
2965const int32_t SensorInputMapperTest::ACCEL_RAW_FLAT = 0;
2966const int32_t SensorInputMapperTest::ACCEL_RAW_RESOLUTION = 8192;
2967
2968const int32_t SensorInputMapperTest::GYRO_RAW_MIN = -2097152;
2969const int32_t SensorInputMapperTest::GYRO_RAW_MAX = 2097152;
2970const int32_t SensorInputMapperTest::GYRO_RAW_FUZZ = 16;
2971const int32_t SensorInputMapperTest::GYRO_RAW_FLAT = 0;
2972const int32_t SensorInputMapperTest::GYRO_RAW_RESOLUTION = 1024;
2973
2974const float SensorInputMapperTest::GRAVITY_MS2_UNIT = 9.80665f;
2975const float SensorInputMapperTest::DEGREE_RADIAN_UNIT = 0.0174533f;
2976
2977void SensorInputMapperTest::prepareAccelAxes() {
2978 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
2979 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
2980 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
2981 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
2982 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Z, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
2983 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
2984}
2985
2986void SensorInputMapperTest::prepareGyroAxes() {
2987 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RX, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
2988 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
2989 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RY, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
2990 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
2991 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RZ, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
2992 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
2993}
2994
2995void SensorInputMapperTest::setAccelProperties() {
2996 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 0, InputDeviceSensorType::ACCELEROMETER,
2997 /* sensorDataIndex */ 0);
2998 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 1, InputDeviceSensorType::ACCELEROMETER,
2999 /* sensorDataIndex */ 1);
3000 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 2, InputDeviceSensorType::ACCELEROMETER,
3001 /* sensorDataIndex */ 2);
3002 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
3003 addConfigurationProperty("sensor.accelerometer.reportingMode", "0");
3004 addConfigurationProperty("sensor.accelerometer.maxDelay", "100000");
3005 addConfigurationProperty("sensor.accelerometer.minDelay", "5000");
3006 addConfigurationProperty("sensor.accelerometer.power", "1.5");
3007}
3008
3009void SensorInputMapperTest::setGyroProperties() {
3010 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 3, InputDeviceSensorType::GYROSCOPE,
3011 /* sensorDataIndex */ 0);
3012 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 4, InputDeviceSensorType::GYROSCOPE,
3013 /* sensorDataIndex */ 1);
3014 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 5, InputDeviceSensorType::GYROSCOPE,
3015 /* sensorDataIndex */ 2);
3016 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
3017 addConfigurationProperty("sensor.gyroscope.reportingMode", "0");
3018 addConfigurationProperty("sensor.gyroscope.maxDelay", "100000");
3019 addConfigurationProperty("sensor.gyroscope.minDelay", "5000");
3020 addConfigurationProperty("sensor.gyroscope.power", "0.8");
3021}
3022
3023TEST_F(SensorInputMapperTest, GetSources) {
3024 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3025
3026 ASSERT_EQ(static_cast<uint32_t>(AINPUT_SOURCE_SENSOR), mapper.getSources());
3027}
3028
3029TEST_F(SensorInputMapperTest, ProcessAccelerometerSensor) {
3030 setAccelProperties();
3031 prepareAccelAxes();
3032 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3033
3034 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::ACCELEROMETER,
3035 std::chrono::microseconds(10000),
3036 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08003037 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003038 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, 20000);
3039 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, -20000);
3040 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Z, 40000);
3041 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
3042 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07003043
3044 NotifySensorArgs args;
3045 std::vector<float> values = {20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
3046 -20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
3047 40000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT};
3048
3049 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
3050 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
3051 ASSERT_EQ(args.deviceId, DEVICE_ID);
3052 ASSERT_EQ(args.sensorType, InputDeviceSensorType::ACCELEROMETER);
3053 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
3054 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
3055 ASSERT_EQ(args.values, values);
3056 mapper.flushSensor(InputDeviceSensorType::ACCELEROMETER);
3057}
3058
3059TEST_F(SensorInputMapperTest, ProcessGyroscopeSensor) {
3060 setGyroProperties();
3061 prepareGyroAxes();
3062 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3063
3064 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::GYROSCOPE,
3065 std::chrono::microseconds(10000),
3066 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08003067 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003068 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RX, 20000);
3069 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RY, -20000);
3070 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RZ, 40000);
3071 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
3072 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07003073
3074 NotifySensorArgs args;
3075 std::vector<float> values = {20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
3076 -20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
3077 40000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT};
3078
3079 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
3080 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
3081 ASSERT_EQ(args.deviceId, DEVICE_ID);
3082 ASSERT_EQ(args.sensorType, InputDeviceSensorType::GYROSCOPE);
3083 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
3084 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
3085 ASSERT_EQ(args.values, values);
3086 mapper.flushSensor(InputDeviceSensorType::GYROSCOPE);
3087}
3088
Michael Wrightd02c5b62014-02-10 15:10:22 -08003089// --- KeyboardInputMapperTest ---
3090
3091class KeyboardInputMapperTest : public InputMapperTest {
3092protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003093 const std::string UNIQUE_ID = "local:0";
3094
3095 void prepareDisplay(int32_t orientation);
3096
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003097 void testDPadKeyRotation(KeyboardInputMapper& mapper, int32_t originalScanCode,
Arthur Hung2c9a3342019-07-23 14:18:59 +08003098 int32_t originalKeyCode, int32_t rotatedKeyCode,
3099 int32_t displayId = ADISPLAY_ID_NONE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003100};
3101
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003102/* Similar to setDisplayInfoAndReconfigure, but pre-populates all parameters except for the
3103 * orientation.
3104 */
3105void KeyboardInputMapperTest::prepareDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003106 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
3107 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003108}
3109
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003110void KeyboardInputMapperTest::testDPadKeyRotation(KeyboardInputMapper& mapper,
Arthur Hung2c9a3342019-07-23 14:18:59 +08003111 int32_t originalScanCode, int32_t originalKeyCode,
3112 int32_t rotatedKeyCode, int32_t displayId) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003113 NotifyKeyArgs args;
3114
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003115 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003116 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3117 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3118 ASSERT_EQ(originalScanCode, args.scanCode);
3119 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003120 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003121
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003122 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003123 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3124 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3125 ASSERT_EQ(originalScanCode, args.scanCode);
3126 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003127 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003128}
3129
Michael Wrightd02c5b62014-02-10 15:10:22 -08003130TEST_F(KeyboardInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003131 KeyboardInputMapper& mapper =
3132 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3133 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003134
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003135 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003136}
3137
3138TEST_F(KeyboardInputMapperTest, Process_SimpleKeyPress) {
3139 const int32_t USAGE_A = 0x070004;
3140 const int32_t USAGE_UNKNOWN = 0x07ffff;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003141 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3142 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
Chris Yea52ade12020-08-27 16:49:20 -07003143 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, POLICY_FLAG_WAKE);
3144 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, POLICY_FLAG_WAKE);
3145 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003146
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003147 KeyboardInputMapper& mapper =
3148 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3149 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
arthurhungc903df12020-08-11 15:08:42 +08003150 // Initial metastate to AMETA_NONE.
3151 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
3152 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003153
3154 // Key down by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003155 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003156 NotifyKeyArgs args;
3157 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3158 ASSERT_EQ(DEVICE_ID, args.deviceId);
3159 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3160 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3161 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3162 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3163 ASSERT_EQ(KEY_HOME, args.scanCode);
3164 ASSERT_EQ(AMETA_NONE, args.metaState);
3165 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3166 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3167 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3168
3169 // Key up by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003170 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003171 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3172 ASSERT_EQ(DEVICE_ID, args.deviceId);
3173 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3174 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3175 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3176 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3177 ASSERT_EQ(KEY_HOME, args.scanCode);
3178 ASSERT_EQ(AMETA_NONE, args.metaState);
3179 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3180 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3181 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3182
3183 // Key down by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003184 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3185 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, 0, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003186 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3187 ASSERT_EQ(DEVICE_ID, args.deviceId);
3188 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3189 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3190 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3191 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3192 ASSERT_EQ(0, args.scanCode);
3193 ASSERT_EQ(AMETA_NONE, args.metaState);
3194 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3195 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3196 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3197
3198 // Key up by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003199 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3200 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003201 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3202 ASSERT_EQ(DEVICE_ID, args.deviceId);
3203 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3204 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3205 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3206 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3207 ASSERT_EQ(0, args.scanCode);
3208 ASSERT_EQ(AMETA_NONE, args.metaState);
3209 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3210 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3211 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3212
3213 // Key down with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003214 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3215 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UNKNOWN, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003216 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3217 ASSERT_EQ(DEVICE_ID, args.deviceId);
3218 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3219 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3220 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3221 ASSERT_EQ(0, args.keyCode);
3222 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3223 ASSERT_EQ(AMETA_NONE, args.metaState);
3224 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3225 ASSERT_EQ(0U, args.policyFlags);
3226 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3227
3228 // Key up with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003229 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3230 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003231 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3232 ASSERT_EQ(DEVICE_ID, args.deviceId);
3233 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3234 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3235 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3236 ASSERT_EQ(0, args.keyCode);
3237 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3238 ASSERT_EQ(AMETA_NONE, args.metaState);
3239 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3240 ASSERT_EQ(0U, args.policyFlags);
3241 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3242}
3243
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003244/**
3245 * Ensure that the readTime is set to the time when the EV_KEY is received.
3246 */
3247TEST_F(KeyboardInputMapperTest, Process_SendsReadTime) {
3248 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3249
3250 KeyboardInputMapper& mapper =
3251 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3252 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3253 NotifyKeyArgs args;
3254
3255 // Key down
3256 process(mapper, ARBITRARY_TIME, 12 /*readTime*/, EV_KEY, KEY_HOME, 1);
3257 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3258 ASSERT_EQ(12, args.readTime);
3259
3260 // Key up
3261 process(mapper, ARBITRARY_TIME, 15 /*readTime*/, EV_KEY, KEY_HOME, 1);
3262 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3263 ASSERT_EQ(15, args.readTime);
3264}
3265
Michael Wrightd02c5b62014-02-10 15:10:22 -08003266TEST_F(KeyboardInputMapperTest, Process_ShouldUpdateMetaState) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003267 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFTSHIFT, 0, AKEYCODE_SHIFT_LEFT, 0);
3268 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003269 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, 0);
3270 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, 0);
3271 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003272
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003273 KeyboardInputMapper& mapper =
3274 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3275 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003276
arthurhungc903df12020-08-11 15:08:42 +08003277 // Initial metastate to AMETA_NONE.
3278 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
3279 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003280
3281 // Metakey down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003282 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003283 NotifyKeyArgs args;
3284 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3285 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003286 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003287 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003288
3289 // Key down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003290 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_A, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003291 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3292 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003293 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003294
3295 // Key up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003296 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, KEY_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003297 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3298 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003299 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003300
3301 // Metakey up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003302 process(mapper, ARBITRARY_TIME + 3, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003303 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3304 ASSERT_EQ(AMETA_NONE, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003305 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003306 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003307}
3308
3309TEST_F(KeyboardInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003310 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3311 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3312 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3313 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003314
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003315 KeyboardInputMapper& mapper =
3316 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3317 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003318
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003319 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003320 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3321 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
3322 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3323 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
3324 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3325 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
3326 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3327 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
3328}
3329
3330TEST_F(KeyboardInputMapperTest, Process_WhenOrientationAware_ShouldRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003331 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3332 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3333 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3334 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003335
Michael Wrightd02c5b62014-02-10 15:10:22 -08003336 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003337 KeyboardInputMapper& mapper =
3338 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3339 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003340
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003341 prepareDisplay(DISPLAY_ORIENTATION_0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003342 ASSERT_NO_FATAL_FAILURE(
3343 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3344 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3345 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3346 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3347 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3348 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3349 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003350
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003351 clearViewports();
3352 prepareDisplay(DISPLAY_ORIENTATION_90);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003353 ASSERT_NO_FATAL_FAILURE(
3354 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3355 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3356 AKEYCODE_DPAD_UP, DISPLAY_ID));
3357 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3358 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3359 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3360 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003361
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003362 clearViewports();
3363 prepareDisplay(DISPLAY_ORIENTATION_180);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003364 ASSERT_NO_FATAL_FAILURE(
3365 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3366 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3367 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3368 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3369 AKEYCODE_DPAD_UP, DISPLAY_ID));
3370 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3371 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003372
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003373 clearViewports();
3374 prepareDisplay(DISPLAY_ORIENTATION_270);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003375 ASSERT_NO_FATAL_FAILURE(
3376 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3377 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3378 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3379 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3380 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3381 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3382 AKEYCODE_DPAD_UP, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003383
3384 // Special case: if orientation changes while key is down, we still emit the same keycode
3385 // in the key up as we did in the key down.
3386 NotifyKeyArgs args;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003387 clearViewports();
3388 prepareDisplay(DISPLAY_ORIENTATION_270);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003389 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003390 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3391 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3392 ASSERT_EQ(KEY_UP, args.scanCode);
3393 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3394
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003395 clearViewports();
3396 prepareDisplay(DISPLAY_ORIENTATION_180);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003397 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003398 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3399 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3400 ASSERT_EQ(KEY_UP, args.scanCode);
3401 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3402}
3403
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003404TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_NotOrientationAware) {
3405 // If the keyboard is not orientation aware,
3406 // key events should not be associated with a specific display id
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003407 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003408
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003409 KeyboardInputMapper& mapper =
3410 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3411 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003412 NotifyKeyArgs args;
3413
3414 // Display id should be ADISPLAY_ID_NONE without any display configuration.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003415 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003416 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003417 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003418 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3419 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3420
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003421 prepareDisplay(DISPLAY_ORIENTATION_0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003422 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003423 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003424 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003425 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3426 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3427}
3428
3429TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_OrientationAware) {
3430 // If the keyboard is orientation aware,
3431 // key events should be associated with the internal viewport
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003432 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003433
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003434 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003435 KeyboardInputMapper& mapper =
3436 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3437 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003438 NotifyKeyArgs args;
3439
3440 // Display id should be ADISPLAY_ID_NONE without any display configuration.
3441 // ^--- already checked by the previous test
3442
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003443 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003444 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003445 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003446 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003447 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003448 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3449 ASSERT_EQ(DISPLAY_ID, args.displayId);
3450
3451 constexpr int32_t newDisplayId = 2;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003452 clearViewports();
3453 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003454 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003455 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003456 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003457 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003458 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3459 ASSERT_EQ(newDisplayId, args.displayId);
3460}
3461
Michael Wrightd02c5b62014-02-10 15:10:22 -08003462TEST_F(KeyboardInputMapperTest, GetKeyCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003463 KeyboardInputMapper& mapper =
3464 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3465 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003466
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003467 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003468 ASSERT_EQ(1, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003469
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003470 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003471 ASSERT_EQ(0, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003472}
3473
3474TEST_F(KeyboardInputMapperTest, GetScanCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003475 KeyboardInputMapper& mapper =
3476 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3477 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003478
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003479 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003480 ASSERT_EQ(1, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003481
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003482 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003483 ASSERT_EQ(0, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003484}
3485
3486TEST_F(KeyboardInputMapperTest, MarkSupportedKeyCodes) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003487 KeyboardInputMapper& mapper =
3488 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3489 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003490
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003491 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003492
3493 const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
3494 uint8_t flags[2] = { 0, 0 };
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003495 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, 1, keyCodes, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003496 ASSERT_TRUE(flags[0]);
3497 ASSERT_FALSE(flags[1]);
3498}
3499
3500TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleMetaStateAndLeds) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003501 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3502 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3503 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3504 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3505 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3506 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003507
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003508 KeyboardInputMapper& mapper =
3509 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3510 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Chris Yea52ade12020-08-27 16:49:20 -07003511 // Initialize metastate to AMETA_NUM_LOCK_ON.
arthurhungc903df12020-08-11 15:08:42 +08003512 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
3513 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003514
3515 // Initialization should have turned all of the lights off.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003516 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3517 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3518 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003519
3520 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003521 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3522 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003523 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3524 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3525 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003526 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003527
3528 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003529 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3530 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003531 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3532 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3533 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003534 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003535
3536 // Toggle caps lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003537 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3538 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003539 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3540 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3541 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003542 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003543
3544 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003545 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3546 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003547 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3548 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3549 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003550 ASSERT_EQ(AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003551
3552 // Toggle num lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003553 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3554 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003555 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3556 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3557 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003558 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003559
3560 // Toggle scroll lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003561 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3562 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003563 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3564 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3565 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003566 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003567}
3568
Chris Yea52ade12020-08-27 16:49:20 -07003569TEST_F(KeyboardInputMapperTest, NoMetaStateWhenMetaKeysNotPresent) {
3570 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_BUTTON_A, 0);
3571 mFakeEventHub->addKey(EVENTHUB_ID, BTN_B, 0, AKEYCODE_BUTTON_B, 0);
3572 mFakeEventHub->addKey(EVENTHUB_ID, BTN_X, 0, AKEYCODE_BUTTON_X, 0);
3573 mFakeEventHub->addKey(EVENTHUB_ID, BTN_Y, 0, AKEYCODE_BUTTON_Y, 0);
3574
3575 KeyboardInputMapper& mapper =
3576 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3577 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
3578
3579 // Initial metastate should be AMETA_NONE as no meta keys added.
3580 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3581 // Meta state should be AMETA_NONE after reset
3582 mapper.reset(ARBITRARY_TIME);
3583 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3584 // Meta state should be AMETA_NONE with update, as device doesn't have the keys.
3585 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
3586 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3587
3588 NotifyKeyArgs args;
3589 // Press button "A"
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003590 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_A, 1);
Chris Yea52ade12020-08-27 16:49:20 -07003591 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3592 ASSERT_EQ(AMETA_NONE, args.metaState);
3593 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3594 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3595 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3596
3597 // Button up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003598 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003599 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3600 ASSERT_EQ(AMETA_NONE, args.metaState);
3601 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3602 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3603 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3604}
3605
Arthur Hung2c9a3342019-07-23 14:18:59 +08003606TEST_F(KeyboardInputMapperTest, Configure_AssignsDisplayPort) {
3607 // keyboard 1.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003608 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3609 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3610 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3611 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003612
3613 // keyboard 2.
3614 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08003615 const std::string DEVICE_NAME2 = "KEYBOARD2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08003616 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003617 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08003618 std::shared_ptr<InputDevice> device2 =
3619 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
3620 Flags<InputDeviceClass>(0));
3621
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003622 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3623 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3624 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3625 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003626
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003627 KeyboardInputMapper& mapper =
3628 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3629 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003630
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003631 KeyboardInputMapper& mapper2 =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003632 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003633 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003634 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
3635 device2->reset(ARBITRARY_TIME);
3636
3637 // Prepared displays and associated info.
3638 constexpr uint8_t hdmi1 = 0;
3639 constexpr uint8_t hdmi2 = 1;
3640 const std::string SECONDARY_UNIQUE_ID = "local:1";
3641
3642 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
3643 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
3644
3645 // No associated display viewport found, should disable the device.
3646 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3647 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3648 ASSERT_FALSE(device2->isEnabled());
3649
3650 // Prepare second display.
3651 constexpr int32_t newDisplayId = 2;
3652 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003653 UNIQUE_ID, hdmi1, ViewportType::INTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003654 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003655 SECONDARY_UNIQUE_ID, hdmi2, ViewportType::EXTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003656 // Default device will reconfigure above, need additional reconfiguration for another device.
3657 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3658 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3659
3660 // Device should be enabled after the associated display is found.
3661 ASSERT_TRUE(mDevice->isEnabled());
3662 ASSERT_TRUE(device2->isEnabled());
3663
3664 // Test pad key events
3665 ASSERT_NO_FATAL_FAILURE(
3666 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3667 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3668 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3669 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3670 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3671 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3672 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3673
3674 ASSERT_NO_FATAL_FAILURE(
3675 testDPadKeyRotation(mapper2, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, newDisplayId));
3676 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3677 AKEYCODE_DPAD_RIGHT, newDisplayId));
3678 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3679 AKEYCODE_DPAD_DOWN, newDisplayId));
3680 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3681 AKEYCODE_DPAD_LEFT, newDisplayId));
3682}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003683
arthurhungc903df12020-08-11 15:08:42 +08003684TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleAfterReattach) {
3685 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3686 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3687 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3688 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3689 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3690 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3691
3692 KeyboardInputMapper& mapper =
3693 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3694 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3695 // Initial metastate to AMETA_NONE.
3696 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
3697 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
3698
3699 // Initialization should have turned all of the lights off.
3700 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3701 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3702 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3703
3704 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003705 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3706 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003707 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3708 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
3709
3710 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003711 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3712 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003713 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3714 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
3715
3716 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003717 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3718 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003719 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3720 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
3721
3722 mFakeEventHub->removeDevice(EVENTHUB_ID);
3723 mReader->loopOnce();
3724
3725 // keyboard 2 should default toggle keys.
3726 const std::string USB2 = "USB2";
3727 const std::string DEVICE_NAME2 = "KEYBOARD2";
3728 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
3729 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
3730 std::shared_ptr<InputDevice> device2 =
3731 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
3732 Flags<InputDeviceClass>(0));
3733 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3734 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
3735 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3736 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3737 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3738 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3739
arthurhung6fe95782020-10-05 22:41:16 +08003740 KeyboardInputMapper& mapper2 =
3741 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
3742 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
arthurhungc903df12020-08-11 15:08:42 +08003743 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
3744 device2->reset(ARBITRARY_TIME);
3745
3746 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_CAPSL));
3747 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_NUML));
3748 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_SCROLLL));
arthurhung6fe95782020-10-05 22:41:16 +08003749 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON,
3750 mapper2.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08003751}
3752
Arthur Hungcb40a002021-08-03 14:31:01 +00003753TEST_F(KeyboardInputMapperTest, Process_toggleCapsLockState) {
3754 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3755 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3756 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3757
3758 // Suppose we have two mappers. (DPAD + KEYBOARD)
3759 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_DPAD,
3760 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
3761 KeyboardInputMapper& mapper =
3762 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3763 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3764 // Initialize metastate to AMETA_NUM_LOCK_ON.
3765 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
3766 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
3767
3768 mReader->toggleCapsLockState(DEVICE_ID);
3769 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
3770}
3771
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003772// --- KeyboardInputMapperTest_ExternalDevice ---
3773
3774class KeyboardInputMapperTest_ExternalDevice : public InputMapperTest {
3775protected:
Chris Yea52ade12020-08-27 16:49:20 -07003776 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003777};
3778
3779TEST_F(KeyboardInputMapperTest_ExternalDevice, WakeBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07003780 // For external devices, non-media keys will trigger wake on key down. Media keys need to be
3781 // marked as WAKE in the keylayout file to trigger wake.
Powei Fengd041c5d2019-05-03 17:11:33 -07003782
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003783 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, 0);
3784 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, 0);
3785 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAYPAUSE, 0, AKEYCODE_MEDIA_PLAY_PAUSE,
3786 POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07003787
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003788 KeyboardInputMapper& mapper =
3789 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3790 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07003791
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003792 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003793 NotifyKeyArgs args;
3794 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3795 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3796
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003797 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003798 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3799 ASSERT_EQ(uint32_t(0), args.policyFlags);
3800
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003801 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003802 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3803 ASSERT_EQ(uint32_t(0), args.policyFlags);
3804
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003805 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003806 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3807 ASSERT_EQ(uint32_t(0), args.policyFlags);
3808
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003809 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003810 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3811 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3812
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003813 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003814 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3815 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3816}
3817
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003818TEST_F(KeyboardInputMapperTest_ExternalDevice, DoNotWakeByDefaultBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07003819 // Tv Remote key's wake behavior is prescribed by the keylayout file.
Powei Fengd041c5d2019-05-03 17:11:33 -07003820
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003821 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3822 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3823 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07003824
Powei Fengd041c5d2019-05-03 17:11:33 -07003825 addConfigurationProperty("keyboard.doNotWakeByDefault", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003826 KeyboardInputMapper& mapper =
3827 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3828 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07003829
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003830 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003831 NotifyKeyArgs args;
3832 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3833 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3834
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003835 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003836 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3837 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3838
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003839 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_DOWN, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003840 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3841 ASSERT_EQ(uint32_t(0), args.policyFlags);
3842
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003843 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_DOWN, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003844 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3845 ASSERT_EQ(uint32_t(0), args.policyFlags);
3846
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003847 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003848 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3849 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3850
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003851 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003852 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3853 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3854}
3855
Michael Wrightd02c5b62014-02-10 15:10:22 -08003856// --- CursorInputMapperTest ---
3857
3858class CursorInputMapperTest : public InputMapperTest {
3859protected:
3860 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD;
3861
Michael Wright17db18e2020-06-26 20:51:44 +01003862 std::shared_ptr<FakePointerController> mFakePointerController;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003863
Chris Yea52ade12020-08-27 16:49:20 -07003864 void SetUp() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003865 InputMapperTest::SetUp();
3866
Michael Wright17db18e2020-06-26 20:51:44 +01003867 mFakePointerController = std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00003868 mFakePolicy->setPointerController(mFakePointerController);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003869 }
3870
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003871 void testMotionRotation(CursorInputMapper& mapper, int32_t originalX, int32_t originalY,
3872 int32_t rotatedX, int32_t rotatedY);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003873
3874 void prepareDisplay(int32_t orientation) {
3875 const std::string uniqueId = "local:0";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003876 const ViewportType viewportType = ViewportType::INTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003877 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
3878 orientation, uniqueId, NO_PORT, viewportType);
3879 }
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003880
3881 static void assertCursorPointerCoords(const PointerCoords& coords, float x, float y,
3882 float pressure) {
3883 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(coords, x, y, pressure, 0.0f, 0.0f, 0.0f, 0.0f,
3884 0.0f, 0.0f, 0.0f, EPSILON));
3885 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08003886};
3887
3888const int32_t CursorInputMapperTest::TRACKBALL_MOVEMENT_THRESHOLD = 6;
3889
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003890void CursorInputMapperTest::testMotionRotation(CursorInputMapper& mapper, int32_t originalX,
3891 int32_t originalY, int32_t rotatedX,
3892 int32_t rotatedY) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003893 NotifyMotionArgs args;
3894
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003895 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, originalX);
3896 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, originalY);
3897 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003898 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3899 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003900 ASSERT_NO_FATAL_FAILURE(
3901 assertCursorPointerCoords(args.pointerCoords[0],
3902 float(rotatedX) / TRACKBALL_MOVEMENT_THRESHOLD,
3903 float(rotatedY) / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003904}
3905
3906TEST_F(CursorInputMapperTest, WhenModeIsPointer_GetSources_ReturnsMouse) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003907 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003908 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003909
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003910 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003911}
3912
3913TEST_F(CursorInputMapperTest, WhenModeIsNavigation_GetSources_ReturnsTrackball) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003914 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003915 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003916
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003917 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003918}
3919
3920TEST_F(CursorInputMapperTest, WhenModeIsPointer_PopulateDeviceInfo_ReturnsRangeFromPointerController) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003921 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003922 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003923
3924 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003925 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003926
3927 // Initially there may not be a valid motion range.
Yi Kong9b14ac62018-07-17 13:48:38 -07003928 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE));
3929 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003930 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3931 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE, 0.0f, 1.0f, 0.0f, 0.0f));
3932
3933 // When the bounds are set, then there should be a valid motion range.
3934 mFakePointerController->setBounds(1, 2, 800 - 1, 480 - 1);
3935
3936 InputDeviceInfo info2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003937 mapper.populateDeviceInfo(&info2);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003938
3939 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3940 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE,
3941 1, 800 - 1, 0.0f, 0.0f));
3942 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3943 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE,
3944 2, 480 - 1, 0.0f, 0.0f));
3945 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3946 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE,
3947 0.0f, 1.0f, 0.0f, 0.0f));
3948}
3949
3950TEST_F(CursorInputMapperTest, WhenModeIsNavigation_PopulateDeviceInfo_ReturnsScaledRange) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003951 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003952 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003953
3954 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003955 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003956
3957 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3958 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_TRACKBALL,
3959 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
3960 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3961 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_TRACKBALL,
3962 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
3963 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3964 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TRACKBALL,
3965 0.0f, 1.0f, 0.0f, 0.0f));
3966}
3967
3968TEST_F(CursorInputMapperTest, Process_ShouldSetAllFieldsAndIncludeGlobalMetaState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003969 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003970 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003971
arthurhungdcef2dc2020-08-11 14:47:50 +08003972 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003973
3974 NotifyMotionArgs args;
3975
3976 // Button press.
3977 // Mostly testing non x/y behavior here so we don't need to check again elsewhere.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003978 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
3979 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003980 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3981 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3982 ASSERT_EQ(DEVICE_ID, args.deviceId);
3983 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3984 ASSERT_EQ(uint32_t(0), args.policyFlags);
3985 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3986 ASSERT_EQ(0, args.flags);
3987 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3988 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
3989 ASSERT_EQ(0, args.edgeFlags);
3990 ASSERT_EQ(uint32_t(1), args.pointerCount);
3991 ASSERT_EQ(0, args.pointerProperties[0].id);
3992 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003993 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003994 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3995 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3996 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3997
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003998 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3999 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4000 ASSERT_EQ(DEVICE_ID, args.deviceId);
4001 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4002 ASSERT_EQ(uint32_t(0), args.policyFlags);
4003 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
4004 ASSERT_EQ(0, args.flags);
4005 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4006 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
4007 ASSERT_EQ(0, args.edgeFlags);
4008 ASSERT_EQ(uint32_t(1), args.pointerCount);
4009 ASSERT_EQ(0, args.pointerProperties[0].id);
4010 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004011 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004012 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4013 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4014 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4015
Michael Wrightd02c5b62014-02-10 15:10:22 -08004016 // Button release. Should have same down time.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004017 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4018 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004019 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4020 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
4021 ASSERT_EQ(DEVICE_ID, args.deviceId);
4022 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4023 ASSERT_EQ(uint32_t(0), args.policyFlags);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004024 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
4025 ASSERT_EQ(0, args.flags);
4026 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4027 ASSERT_EQ(0, args.buttonState);
4028 ASSERT_EQ(0, args.edgeFlags);
4029 ASSERT_EQ(uint32_t(1), args.pointerCount);
4030 ASSERT_EQ(0, args.pointerProperties[0].id);
4031 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004032 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004033 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4034 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4035 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4036
4037 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4038 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
4039 ASSERT_EQ(DEVICE_ID, args.deviceId);
4040 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4041 ASSERT_EQ(uint32_t(0), args.policyFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004042 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
4043 ASSERT_EQ(0, args.flags);
4044 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4045 ASSERT_EQ(0, args.buttonState);
4046 ASSERT_EQ(0, args.edgeFlags);
4047 ASSERT_EQ(uint32_t(1), args.pointerCount);
4048 ASSERT_EQ(0, args.pointerProperties[0].id);
4049 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004050 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004051 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4052 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4053 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4054}
4055
4056TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentXYUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004057 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004058 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004059
4060 NotifyMotionArgs args;
4061
4062 // Motion in X but not Y.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004063 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4064 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004065 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4066 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004067 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4068 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f,
4069 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004070
4071 // Motion in Y but not X.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004072 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
4073 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004074 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4075 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004076 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f,
4077 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004078}
4079
4080TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004081 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004082 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004083
4084 NotifyMotionArgs args;
4085
4086 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004087 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4088 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004089 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4090 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004091 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004092
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004093 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4094 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004095 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004096
Michael Wrightd02c5b62014-02-10 15:10:22 -08004097 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004098 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4099 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004100 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004101 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004102 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004103
4104 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004105 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004106 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004107}
4108
4109TEST_F(CursorInputMapperTest, Process_ShouldHandleCombinedXYAndButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004110 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004111 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004112
4113 NotifyMotionArgs args;
4114
4115 // Combined X, Y and Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004116 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4117 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
4118 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4119 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004120 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4121 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004122 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4123 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4124 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004125
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004126 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4127 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004128 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4129 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4130 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004131
Michael Wrightd02c5b62014-02-10 15:10:22 -08004132 // Move X, Y a bit while pressed.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004133 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 2);
4134 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 1);
4135 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004136 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4137 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004138 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4139 2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4140 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004141
4142 // Release Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004143 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4144 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004145 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004146 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004147 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004148
4149 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004150 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004151 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004152}
4153
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004154TEST_F(CursorInputMapperTest, Process_WhenOrientationAware_ShouldNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004155 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004156 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
4157 // need to be rotated.
4158 addConfigurationProperty("cursor.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004159 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004160
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004161 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004162 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4163 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4164 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4165 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4166 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4167 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4168 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4169 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4170}
4171
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004172TEST_F(CursorInputMapperTest, Process_WhenNotOrientationAware_ShouldRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004173 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004174 // Since InputReader works in the un-rotated coordinate space, only devices that are not
4175 // orientation-aware are affected by display rotation.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004176 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004177
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004178 prepareDisplay(DISPLAY_ORIENTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004179 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4180 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4181 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4182 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4183 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4184 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4185 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4186 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4187
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004188 prepareDisplay(DISPLAY_ORIENTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004189 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, -1, 0));
4190 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, 1));
4191 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, 1));
4192 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, 1));
4193 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 1, 0));
4194 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, -1));
4195 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, -1));
4196 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, -1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004197
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004198 prepareDisplay(DISPLAY_ORIENTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004199 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, -1));
4200 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, -1));
4201 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, -1, 0));
4202 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, 1));
4203 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, 1));
4204 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, 1));
4205 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 1, 0));
4206 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, -1));
4207
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004208 prepareDisplay(DISPLAY_ORIENTATION_270);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004209 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 1, 0));
4210 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, -1));
4211 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, -1));
4212 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, -1));
4213 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, -1, 0));
4214 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, 1));
4215 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, 1));
4216 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, 1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004217}
4218
4219TEST_F(CursorInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004220 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004221 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004222
4223 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4224 mFakePointerController->setPosition(100, 200);
4225 mFakePointerController->setButtonState(0);
4226
4227 NotifyMotionArgs motionArgs;
4228 NotifyKeyArgs keyArgs;
4229
4230 // press BTN_LEFT, release BTN_LEFT
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004231 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 1);
4232 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004233 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4234 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4235 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4236 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004237 ASSERT_NO_FATAL_FAILURE(
4238 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004239
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004240 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4241 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4242 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4243 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004244 ASSERT_NO_FATAL_FAILURE(
4245 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004246
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004247 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 0);
4248 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004249 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004250 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004251 ASSERT_EQ(0, motionArgs.buttonState);
4252 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004253 ASSERT_NO_FATAL_FAILURE(
4254 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004255
4256 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004257 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004258 ASSERT_EQ(0, motionArgs.buttonState);
4259 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004260 ASSERT_NO_FATAL_FAILURE(
4261 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004262
4263 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004264 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004265 ASSERT_EQ(0, motionArgs.buttonState);
4266 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004267 ASSERT_NO_FATAL_FAILURE(
4268 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004269
4270 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004271 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 1);
4272 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 1);
4273 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004274 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4275 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4276 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4277 motionArgs.buttonState);
4278 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4279 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004280 ASSERT_NO_FATAL_FAILURE(
4281 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004282
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004283 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4284 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4285 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4286 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4287 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004288 ASSERT_NO_FATAL_FAILURE(
4289 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004290
4291 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4292 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4293 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4294 motionArgs.buttonState);
4295 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4296 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004297 ASSERT_NO_FATAL_FAILURE(
4298 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004299
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004300 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 0);
4301 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004302 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004303 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004304 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4305 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004306 ASSERT_NO_FATAL_FAILURE(
4307 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004308
4309 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004310 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004311 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4312 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004313 ASSERT_NO_FATAL_FAILURE(
4314 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004315
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004316 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4317 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004318 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004319 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
4320 ASSERT_EQ(0, motionArgs.buttonState);
4321 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004322 ASSERT_NO_FATAL_FAILURE(
4323 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004324 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4325 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004326
4327 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004328 ASSERT_EQ(0, motionArgs.buttonState);
4329 ASSERT_EQ(0, mFakePointerController->getButtonState());
4330 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004331 ASSERT_NO_FATAL_FAILURE(
4332 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004333
Michael Wrightd02c5b62014-02-10 15:10:22 -08004334 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4335 ASSERT_EQ(0, motionArgs.buttonState);
4336 ASSERT_EQ(0, mFakePointerController->getButtonState());
4337 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004338 ASSERT_NO_FATAL_FAILURE(
4339 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004340
4341 // press BTN_BACK, release BTN_BACK
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004342 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 1);
4343 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004344 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4345 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4346 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004347
Michael Wrightd02c5b62014-02-10 15:10:22 -08004348 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004349 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004350 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4351 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004352 ASSERT_NO_FATAL_FAILURE(
4353 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004354
4355 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4356 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4357 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4358 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004359 ASSERT_NO_FATAL_FAILURE(
4360 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004361
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004362 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 0);
4363 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004364 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004365 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004366 ASSERT_EQ(0, motionArgs.buttonState);
4367 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004368 ASSERT_NO_FATAL_FAILURE(
4369 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004370
4371 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004372 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004373 ASSERT_EQ(0, motionArgs.buttonState);
4374 ASSERT_EQ(0, mFakePointerController->getButtonState());
4375
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004376 ASSERT_NO_FATAL_FAILURE(
4377 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004378 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4379 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4380 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4381
4382 // press BTN_SIDE, release BTN_SIDE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004383 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 1);
4384 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004385 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4386 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4387 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004388
Michael Wrightd02c5b62014-02-10 15:10:22 -08004389 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004390 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004391 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4392 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004393 ASSERT_NO_FATAL_FAILURE(
4394 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004395
4396 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4397 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4398 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4399 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004400 ASSERT_NO_FATAL_FAILURE(
4401 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004402
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004403 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 0);
4404 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004405 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004406 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004407 ASSERT_EQ(0, motionArgs.buttonState);
4408 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004409 ASSERT_NO_FATAL_FAILURE(
4410 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004411
4412 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4413 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4414 ASSERT_EQ(0, motionArgs.buttonState);
4415 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004416 ASSERT_NO_FATAL_FAILURE(
4417 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004418
Michael Wrightd02c5b62014-02-10 15:10:22 -08004419 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4420 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4421 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4422
4423 // press BTN_FORWARD, release BTN_FORWARD
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004424 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 1);
4425 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004426 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4427 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4428 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004429
Michael Wrightd02c5b62014-02-10 15:10:22 -08004430 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004431 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004432 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4433 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004434 ASSERT_NO_FATAL_FAILURE(
4435 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004436
4437 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4438 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4439 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4440 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004441 ASSERT_NO_FATAL_FAILURE(
4442 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004443
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004444 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 0);
4445 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004446 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004447 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004448 ASSERT_EQ(0, motionArgs.buttonState);
4449 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004450 ASSERT_NO_FATAL_FAILURE(
4451 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004452
4453 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4454 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4455 ASSERT_EQ(0, motionArgs.buttonState);
4456 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004457 ASSERT_NO_FATAL_FAILURE(
4458 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004459
Michael Wrightd02c5b62014-02-10 15:10:22 -08004460 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4461 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4462 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4463
4464 // press BTN_EXTRA, release BTN_EXTRA
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004465 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 1);
4466 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004467 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4468 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4469 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004470
Michael Wrightd02c5b62014-02-10 15:10:22 -08004471 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004472 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004473 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4474 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004475 ASSERT_NO_FATAL_FAILURE(
4476 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004477
4478 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4479 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4480 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4481 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004482 ASSERT_NO_FATAL_FAILURE(
4483 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004484
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004485 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 0);
4486 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004487 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004488 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004489 ASSERT_EQ(0, motionArgs.buttonState);
4490 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004491 ASSERT_NO_FATAL_FAILURE(
4492 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004493
4494 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4495 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4496 ASSERT_EQ(0, motionArgs.buttonState);
4497 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004498 ASSERT_NO_FATAL_FAILURE(
4499 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004500
Michael Wrightd02c5b62014-02-10 15:10:22 -08004501 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4502 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4503 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4504}
4505
4506TEST_F(CursorInputMapperTest, Process_WhenModeIsPointer_ShouldMoveThePointerAround) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004507 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004508 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004509
4510 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4511 mFakePointerController->setPosition(100, 200);
4512 mFakePointerController->setButtonState(0);
4513
4514 NotifyMotionArgs args;
4515
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004516 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4517 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4518 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004519 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004520 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4521 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4522 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4523 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 +01004524 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004525}
4526
4527TEST_F(CursorInputMapperTest, Process_PointerCapture) {
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004528 addConfigurationProperty("cursor.mode", "pointer");
4529 mFakePolicy->setPointerCapture(true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004530 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004531
4532 NotifyDeviceResetArgs resetArgs;
4533 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4534 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4535 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4536
4537 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4538 mFakePointerController->setPosition(100, 200);
4539 mFakePointerController->setButtonState(0);
4540
4541 NotifyMotionArgs args;
4542
4543 // Move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004544 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4545 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4546 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004547 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4548 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4549 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4550 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4551 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 +01004552 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004553
4554 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004555 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4556 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004557 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4558 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4559 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
4560 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4561 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4562 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4563 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4564 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
4565 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4566 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4567
4568 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004569 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4570 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004571 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4572 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4573 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
4574 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4575 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4576 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4577 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4578 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
4579 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4580 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4581
4582 // Another move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004583 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 30);
4584 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 40);
4585 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004586 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4587 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4588 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4589 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4590 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 +01004591 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004592
4593 // Disable pointer capture and check that the device generation got bumped
4594 // and events are generated the usual way.
arthurhungdcef2dc2020-08-11 14:47:50 +08004595 const uint32_t generation = mReader->getContext()->getGeneration();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004596 mFakePolicy->setPointerCapture(false);
4597 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
arthurhungdcef2dc2020-08-11 14:47:50 +08004598 ASSERT_TRUE(mReader->getContext()->getGeneration() != generation);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004599
4600 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4601 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4602 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4603
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004604 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4605 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4606 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004607 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4608 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004609 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4610 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4611 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 +01004612 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004613}
4614
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004615TEST_F(CursorInputMapperTest, Process_ShouldHandleDisplayId) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004616 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004617
Garfield Tan888a6a42020-01-09 11:39:16 -08004618 // Setup for second display.
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004619 constexpr int32_t SECOND_DISPLAY_ID = 1;
Garfield Tan888a6a42020-01-09 11:39:16 -08004620 const std::string SECOND_DISPLAY_UNIQUE_ID = "local:1";
4621 mFakePolicy->addDisplayViewport(SECOND_DISPLAY_ID, 800, 480, DISPLAY_ORIENTATION_0,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00004622 true /*isActive*/, SECOND_DISPLAY_UNIQUE_ID, NO_PORT,
4623 ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08004624 mFakePolicy->setDefaultPointerDisplayId(SECOND_DISPLAY_ID);
4625 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
4626
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004627 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4628 mFakePointerController->setPosition(100, 200);
4629 mFakePointerController->setButtonState(0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004630
4631 NotifyMotionArgs args;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004632 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4633 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4634 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004635 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4636 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4637 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4638 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4639 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 +01004640 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004641 ASSERT_EQ(SECOND_DISPLAY_ID, args.displayId);
4642}
4643
Michael Wrightd02c5b62014-02-10 15:10:22 -08004644// --- TouchInputMapperTest ---
4645
4646class TouchInputMapperTest : public InputMapperTest {
4647protected:
4648 static const int32_t RAW_X_MIN;
4649 static const int32_t RAW_X_MAX;
4650 static const int32_t RAW_Y_MIN;
4651 static const int32_t RAW_Y_MAX;
4652 static const int32_t RAW_TOUCH_MIN;
4653 static const int32_t RAW_TOUCH_MAX;
4654 static const int32_t RAW_TOOL_MIN;
4655 static const int32_t RAW_TOOL_MAX;
4656 static const int32_t RAW_PRESSURE_MIN;
4657 static const int32_t RAW_PRESSURE_MAX;
4658 static const int32_t RAW_ORIENTATION_MIN;
4659 static const int32_t RAW_ORIENTATION_MAX;
4660 static const int32_t RAW_DISTANCE_MIN;
4661 static const int32_t RAW_DISTANCE_MAX;
4662 static const int32_t RAW_TILT_MIN;
4663 static const int32_t RAW_TILT_MAX;
4664 static const int32_t RAW_ID_MIN;
4665 static const int32_t RAW_ID_MAX;
4666 static const int32_t RAW_SLOT_MIN;
4667 static const int32_t RAW_SLOT_MAX;
4668 static const float X_PRECISION;
4669 static const float Y_PRECISION;
Santos Cordonfa5cf462017-04-05 10:37:00 -07004670 static const float X_PRECISION_VIRTUAL;
4671 static const float Y_PRECISION_VIRTUAL;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004672
4673 static const float GEOMETRIC_SCALE;
Jason Gerecke489fda82012-09-07 17:19:40 -07004674 static const TouchAffineTransformation AFFINE_TRANSFORM;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004675
4676 static const VirtualKeyDefinition VIRTUAL_KEYS[2];
4677
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004678 const std::string UNIQUE_ID = "local:0";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004679 const std::string SECONDARY_UNIQUE_ID = "local:1";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004680
Michael Wrightd02c5b62014-02-10 15:10:22 -08004681 enum Axes {
4682 POSITION = 1 << 0,
4683 TOUCH = 1 << 1,
4684 TOOL = 1 << 2,
4685 PRESSURE = 1 << 3,
4686 ORIENTATION = 1 << 4,
4687 MINOR = 1 << 5,
4688 ID = 1 << 6,
4689 DISTANCE = 1 << 7,
4690 TILT = 1 << 8,
4691 SLOT = 1 << 9,
4692 TOOL_TYPE = 1 << 10,
4693 };
4694
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004695 void prepareDisplay(int32_t orientation, std::optional<uint8_t> port = NO_PORT);
4696 void prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port = NO_PORT);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004697 void prepareVirtualDisplay(int32_t orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004698 void prepareVirtualKeys();
Jason Gerecke489fda82012-09-07 17:19:40 -07004699 void prepareLocationCalibration();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004700 int32_t toRawX(float displayX);
4701 int32_t toRawY(float displayY);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07004702 int32_t toRotatedRawX(float displayX);
4703 int32_t toRotatedRawY(float displayY);
Jason Gerecke489fda82012-09-07 17:19:40 -07004704 float toCookedX(float rawX, float rawY);
4705 float toCookedY(float rawX, float rawY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004706 float toDisplayX(int32_t rawX);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004707 float toDisplayX(int32_t rawX, int32_t displayWidth);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004708 float toDisplayY(int32_t rawY);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004709 float toDisplayY(int32_t rawY, int32_t displayHeight);
4710
Michael Wrightd02c5b62014-02-10 15:10:22 -08004711};
4712
4713const int32_t TouchInputMapperTest::RAW_X_MIN = 25;
4714const int32_t TouchInputMapperTest::RAW_X_MAX = 1019;
4715const int32_t TouchInputMapperTest::RAW_Y_MIN = 30;
4716const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009;
4717const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0;
4718const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31;
4719const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0;
4720const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15;
Michael Wrightaa449c92017-12-13 21:21:43 +00004721const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = 0;
4722const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = 255;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004723const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7;
4724const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
4725const int32_t TouchInputMapperTest::RAW_DISTANCE_MIN = 0;
4726const int32_t TouchInputMapperTest::RAW_DISTANCE_MAX = 7;
4727const int32_t TouchInputMapperTest::RAW_TILT_MIN = 0;
4728const int32_t TouchInputMapperTest::RAW_TILT_MAX = 150;
4729const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
4730const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
4731const int32_t TouchInputMapperTest::RAW_SLOT_MIN = 0;
4732const int32_t TouchInputMapperTest::RAW_SLOT_MAX = 9;
4733const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH;
4734const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT;
Santos Cordonfa5cf462017-04-05 10:37:00 -07004735const float TouchInputMapperTest::X_PRECISION_VIRTUAL =
4736 float(RAW_X_MAX - RAW_X_MIN + 1) / VIRTUAL_DISPLAY_WIDTH;
4737const float TouchInputMapperTest::Y_PRECISION_VIRTUAL =
4738 float(RAW_Y_MAX - RAW_Y_MIN + 1) / VIRTUAL_DISPLAY_HEIGHT;
Jason Gerecke489fda82012-09-07 17:19:40 -07004739const TouchAffineTransformation TouchInputMapperTest::AFFINE_TRANSFORM =
4740 TouchAffineTransformation(1, -2, 3, -4, 5, -6);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004741
4742const float TouchInputMapperTest::GEOMETRIC_SCALE =
4743 avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1),
4744 float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1));
4745
4746const VirtualKeyDefinition TouchInputMapperTest::VIRTUAL_KEYS[2] = {
4747 { KEY_HOME, 60, DISPLAY_HEIGHT + 15, 20, 20 },
4748 { KEY_MENU, DISPLAY_HEIGHT - 60, DISPLAY_WIDTH + 15, 20, 20 },
4749};
4750
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004751void TouchInputMapperTest::prepareDisplay(int32_t orientation, std::optional<uint8_t> port) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004752 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
4753 port, ViewportType::INTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004754}
4755
4756void TouchInputMapperTest::prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port) {
4757 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
4758 DISPLAY_ORIENTATION_0, SECONDARY_UNIQUE_ID, port, type);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004759}
4760
Santos Cordonfa5cf462017-04-05 10:37:00 -07004761void TouchInputMapperTest::prepareVirtualDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004762 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH, VIRTUAL_DISPLAY_HEIGHT,
4763 orientation, VIRTUAL_DISPLAY_UNIQUE_ID, NO_PORT,
4764 ViewportType::VIRTUAL);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004765}
4766
Michael Wrightd02c5b62014-02-10 15:10:22 -08004767void TouchInputMapperTest::prepareVirtualKeys() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004768 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[0]);
4769 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[1]);
4770 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4771 mFakeEventHub->addKey(EVENTHUB_ID, KEY_MENU, 0, AKEYCODE_MENU, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004772}
4773
Jason Gerecke489fda82012-09-07 17:19:40 -07004774void TouchInputMapperTest::prepareLocationCalibration() {
4775 mFakePolicy->setTouchAffineTransformation(AFFINE_TRANSFORM);
4776}
4777
Michael Wrightd02c5b62014-02-10 15:10:22 -08004778int32_t TouchInputMapperTest::toRawX(float displayX) {
4779 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN);
4780}
4781
4782int32_t TouchInputMapperTest::toRawY(float displayY) {
4783 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN);
4784}
4785
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07004786int32_t TouchInputMapperTest::toRotatedRawX(float displayX) {
4787 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_HEIGHT + RAW_X_MIN);
4788}
4789
4790int32_t TouchInputMapperTest::toRotatedRawY(float displayY) {
4791 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_WIDTH + RAW_Y_MIN);
4792}
4793
Jason Gerecke489fda82012-09-07 17:19:40 -07004794float TouchInputMapperTest::toCookedX(float rawX, float rawY) {
4795 AFFINE_TRANSFORM.applyTo(rawX, rawY);
4796 return rawX;
4797}
4798
4799float TouchInputMapperTest::toCookedY(float rawX, float rawY) {
4800 AFFINE_TRANSFORM.applyTo(rawX, rawY);
4801 return rawY;
4802}
4803
Michael Wrightd02c5b62014-02-10 15:10:22 -08004804float TouchInputMapperTest::toDisplayX(int32_t rawX) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004805 return toDisplayX(rawX, DISPLAY_WIDTH);
4806}
4807
4808float TouchInputMapperTest::toDisplayX(int32_t rawX, int32_t displayWidth) {
4809 return float(rawX - RAW_X_MIN) * displayWidth / (RAW_X_MAX - RAW_X_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004810}
4811
4812float TouchInputMapperTest::toDisplayY(int32_t rawY) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004813 return toDisplayY(rawY, DISPLAY_HEIGHT);
4814}
4815
4816float TouchInputMapperTest::toDisplayY(int32_t rawY, int32_t displayHeight) {
4817 return float(rawY - RAW_Y_MIN) * displayHeight / (RAW_Y_MAX - RAW_Y_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004818}
4819
4820
4821// --- SingleTouchInputMapperTest ---
4822
4823class SingleTouchInputMapperTest : public TouchInputMapperTest {
4824protected:
4825 void prepareButtons();
4826 void prepareAxes(int axes);
4827
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004828 void processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
4829 void processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
4830 void processUp(SingleTouchInputMapper& mappery);
4831 void processPressure(SingleTouchInputMapper& mapper, int32_t pressure);
4832 void processToolMajor(SingleTouchInputMapper& mapper, int32_t toolMajor);
4833 void processDistance(SingleTouchInputMapper& mapper, int32_t distance);
4834 void processTilt(SingleTouchInputMapper& mapper, int32_t tiltX, int32_t tiltY);
4835 void processKey(SingleTouchInputMapper& mapper, int32_t code, int32_t value);
4836 void processSync(SingleTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004837};
4838
4839void SingleTouchInputMapperTest::prepareButtons() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004840 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004841}
4842
4843void SingleTouchInputMapperTest::prepareAxes(int axes) {
4844 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004845 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
4846 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004847 }
4848 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004849 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MIN,
4850 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004851 }
4852 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004853 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TOOL_WIDTH, RAW_TOOL_MIN, RAW_TOOL_MAX, 0,
4854 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004855 }
4856 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004857 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_DISTANCE, RAW_DISTANCE_MIN,
4858 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004859 }
4860 if (axes & TILT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004861 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_X, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
4862 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_Y, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004863 }
4864}
4865
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004866void SingleTouchInputMapperTest::processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004867 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
4868 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
4869 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004870}
4871
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004872void SingleTouchInputMapperTest::processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004873 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
4874 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004875}
4876
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004877void SingleTouchInputMapperTest::processUp(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004878 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004879}
4880
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004881void SingleTouchInputMapperTest::processPressure(SingleTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004882 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004883}
4884
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004885void SingleTouchInputMapperTest::processToolMajor(SingleTouchInputMapper& mapper,
4886 int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004887 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TOOL_WIDTH, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004888}
4889
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004890void SingleTouchInputMapperTest::processDistance(SingleTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004891 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004892}
4893
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004894void SingleTouchInputMapperTest::processTilt(SingleTouchInputMapper& mapper, int32_t tiltX,
4895 int32_t tiltY) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004896 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_X, tiltX);
4897 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_Y, tiltY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004898}
4899
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004900void SingleTouchInputMapperTest::processKey(SingleTouchInputMapper& mapper, int32_t code,
4901 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004902 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004903}
4904
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004905void SingleTouchInputMapperTest::processSync(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004906 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004907}
4908
Michael Wrightd02c5b62014-02-10 15:10:22 -08004909TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004910 prepareButtons();
4911 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004912 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004913
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004914 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004915}
4916
4917TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndIsACursor_ReturnsTouchPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004918 mFakeEventHub->addRelativeAxis(EVENTHUB_ID, REL_X);
4919 mFakeEventHub->addRelativeAxis(EVENTHUB_ID, REL_Y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004920 prepareButtons();
4921 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004922 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004923
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004924 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004925}
4926
4927TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchPad_ReturnsTouchPad) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004928 prepareButtons();
4929 prepareAxes(POSITION);
4930 addConfigurationProperty("touch.deviceType", "touchPad");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004931 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004932
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004933 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004934}
4935
4936TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004937 prepareButtons();
4938 prepareAxes(POSITION);
4939 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004940 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004941
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004942 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004943}
4944
4945TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004946 addConfigurationProperty("touch.deviceType", "touchScreen");
4947 prepareDisplay(DISPLAY_ORIENTATION_0);
4948 prepareButtons();
4949 prepareAxes(POSITION);
4950 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004951 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004952
4953 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004954 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004955
4956 // Virtual key is down.
4957 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4958 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4959 processDown(mapper, x, y);
4960 processSync(mapper);
4961 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4962
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004963 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004964
4965 // Virtual key is up.
4966 processUp(mapper);
4967 processSync(mapper);
4968 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4969
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004970 ASSERT_EQ(AKEY_STATE_UP, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004971}
4972
4973TEST_F(SingleTouchInputMapperTest, GetScanCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004974 addConfigurationProperty("touch.deviceType", "touchScreen");
4975 prepareDisplay(DISPLAY_ORIENTATION_0);
4976 prepareButtons();
4977 prepareAxes(POSITION);
4978 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004979 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004980
4981 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004982 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004983
4984 // Virtual key is down.
4985 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4986 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4987 processDown(mapper, x, y);
4988 processSync(mapper);
4989 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4990
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004991 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004992
4993 // Virtual key is up.
4994 processUp(mapper);
4995 processSync(mapper);
4996 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4997
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004998 ASSERT_EQ(AKEY_STATE_UP, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004999}
5000
5001TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005002 addConfigurationProperty("touch.deviceType", "touchScreen");
5003 prepareDisplay(DISPLAY_ORIENTATION_0);
5004 prepareButtons();
5005 prepareAxes(POSITION);
5006 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005007 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005008
5009 const int32_t keys[2] = { AKEYCODE_HOME, AKEYCODE_A };
5010 uint8_t flags[2] = { 0, 0 };
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005011 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, 2, keys, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005012 ASSERT_TRUE(flags[0]);
5013 ASSERT_FALSE(flags[1]);
5014}
5015
5016TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005017 addConfigurationProperty("touch.deviceType", "touchScreen");
5018 prepareDisplay(DISPLAY_ORIENTATION_0);
5019 prepareButtons();
5020 prepareAxes(POSITION);
5021 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005022 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005023
arthurhungdcef2dc2020-08-11 14:47:50 +08005024 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005025
5026 NotifyKeyArgs args;
5027
5028 // Press virtual key.
5029 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5030 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5031 processDown(mapper, x, y);
5032 processSync(mapper);
5033
5034 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
5035 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5036 ASSERT_EQ(DEVICE_ID, args.deviceId);
5037 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
5038 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
5039 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
5040 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5041 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5042 ASSERT_EQ(KEY_HOME, args.scanCode);
5043 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5044 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5045
5046 // Release virtual key.
5047 processUp(mapper);
5048 processSync(mapper);
5049
5050 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
5051 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5052 ASSERT_EQ(DEVICE_ID, args.deviceId);
5053 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
5054 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
5055 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
5056 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5057 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5058 ASSERT_EQ(KEY_HOME, args.scanCode);
5059 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5060 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5061
5062 // Should not have sent any motions.
5063 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5064}
5065
5066TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005067 addConfigurationProperty("touch.deviceType", "touchScreen");
5068 prepareDisplay(DISPLAY_ORIENTATION_0);
5069 prepareButtons();
5070 prepareAxes(POSITION);
5071 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005072 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005073
arthurhungdcef2dc2020-08-11 14:47:50 +08005074 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005075
5076 NotifyKeyArgs keyArgs;
5077
5078 // Press virtual key.
5079 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5080 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5081 processDown(mapper, x, y);
5082 processSync(mapper);
5083
5084 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5085 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5086 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5087 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5088 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5089 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5090 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags);
5091 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5092 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5093 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5094 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5095
5096 // Move out of bounds. This should generate a cancel and a pointer down since we moved
5097 // into the display area.
5098 y -= 100;
5099 processMove(mapper, x, y);
5100 processSync(mapper);
5101
5102 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5103 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5104 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5105 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5106 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5107 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5108 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
5109 | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags);
5110 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5111 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5112 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5113 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5114
5115 NotifyMotionArgs motionArgs;
5116 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5117 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5118 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5119 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5120 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5121 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5122 ASSERT_EQ(0, motionArgs.flags);
5123 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5124 ASSERT_EQ(0, motionArgs.buttonState);
5125 ASSERT_EQ(0, motionArgs.edgeFlags);
5126 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5127 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5128 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5129 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5130 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5131 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5132 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5133 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5134
5135 // Keep moving out of bounds. Should generate a pointer move.
5136 y -= 50;
5137 processMove(mapper, x, y);
5138 processSync(mapper);
5139
5140 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5141 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5142 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5143 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5144 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5145 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5146 ASSERT_EQ(0, motionArgs.flags);
5147 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5148 ASSERT_EQ(0, motionArgs.buttonState);
5149 ASSERT_EQ(0, motionArgs.edgeFlags);
5150 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5151 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5152 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5153 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5154 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5155 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5156 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5157 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5158
5159 // Release out of bounds. Should generate a pointer up.
5160 processUp(mapper);
5161 processSync(mapper);
5162
5163 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5164 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5165 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5166 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5167 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5168 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5169 ASSERT_EQ(0, motionArgs.flags);
5170 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5171 ASSERT_EQ(0, motionArgs.buttonState);
5172 ASSERT_EQ(0, motionArgs.edgeFlags);
5173 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5174 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5175 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5176 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5177 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5178 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5179 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5180 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5181
5182 // Should not have sent any more keys or motions.
5183 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5184 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5185}
5186
5187TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005188 addConfigurationProperty("touch.deviceType", "touchScreen");
5189 prepareDisplay(DISPLAY_ORIENTATION_0);
5190 prepareButtons();
5191 prepareAxes(POSITION);
5192 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005193 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005194
arthurhungdcef2dc2020-08-11 14:47:50 +08005195 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005196
5197 NotifyMotionArgs motionArgs;
5198
5199 // Initially go down out of bounds.
5200 int32_t x = -10;
5201 int32_t y = -10;
5202 processDown(mapper, x, y);
5203 processSync(mapper);
5204
5205 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5206
5207 // Move into the display area. Should generate a pointer down.
5208 x = 50;
5209 y = 75;
5210 processMove(mapper, x, y);
5211 processSync(mapper);
5212
5213 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5214 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5215 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5216 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5217 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5218 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5219 ASSERT_EQ(0, motionArgs.flags);
5220 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5221 ASSERT_EQ(0, motionArgs.buttonState);
5222 ASSERT_EQ(0, motionArgs.edgeFlags);
5223 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5224 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5225 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5226 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5227 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5228 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5229 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5230 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5231
5232 // Release. Should generate a pointer up.
5233 processUp(mapper);
5234 processSync(mapper);
5235
5236 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5237 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5238 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5239 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5240 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5241 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5242 ASSERT_EQ(0, motionArgs.flags);
5243 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5244 ASSERT_EQ(0, motionArgs.buttonState);
5245 ASSERT_EQ(0, motionArgs.edgeFlags);
5246 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5247 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5248 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5249 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5250 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5251 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5252 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5253 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5254
5255 // Should not have sent any more keys or motions.
5256 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5257 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5258}
5259
Santos Cordonfa5cf462017-04-05 10:37:00 -07005260TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture_VirtualDisplay) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005261 addConfigurationProperty("touch.deviceType", "touchScreen");
5262 addConfigurationProperty("touch.displayId", VIRTUAL_DISPLAY_UNIQUE_ID);
5263
5264 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
5265 prepareButtons();
5266 prepareAxes(POSITION);
5267 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005268 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Santos Cordonfa5cf462017-04-05 10:37:00 -07005269
arthurhungdcef2dc2020-08-11 14:47:50 +08005270 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005271
5272 NotifyMotionArgs motionArgs;
5273
5274 // Down.
5275 int32_t x = 100;
5276 int32_t y = 125;
5277 processDown(mapper, x, y);
5278 processSync(mapper);
5279
5280 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5281 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5282 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5283 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5284 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5285 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5286 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5287 ASSERT_EQ(0, motionArgs.flags);
5288 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5289 ASSERT_EQ(0, motionArgs.buttonState);
5290 ASSERT_EQ(0, motionArgs.edgeFlags);
5291 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5292 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5293 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5294 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5295 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5296 1, 0, 0, 0, 0, 0, 0, 0));
5297 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5298 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5299 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5300
5301 // Move.
5302 x += 50;
5303 y += 75;
5304 processMove(mapper, x, y);
5305 processSync(mapper);
5306
5307 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5308 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5309 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5310 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5311 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5312 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5313 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5314 ASSERT_EQ(0, motionArgs.flags);
5315 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5316 ASSERT_EQ(0, motionArgs.buttonState);
5317 ASSERT_EQ(0, motionArgs.edgeFlags);
5318 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5319 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5320 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5321 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5322 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5323 1, 0, 0, 0, 0, 0, 0, 0));
5324 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5325 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5326 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5327
5328 // Up.
5329 processUp(mapper);
5330 processSync(mapper);
5331
5332 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5333 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5334 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5335 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5336 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5337 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5338 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5339 ASSERT_EQ(0, motionArgs.flags);
5340 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5341 ASSERT_EQ(0, motionArgs.buttonState);
5342 ASSERT_EQ(0, motionArgs.edgeFlags);
5343 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5344 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5345 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5346 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5347 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5348 1, 0, 0, 0, 0, 0, 0, 0));
5349 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5350 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5351 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5352
5353 // Should not have sent any more keys or motions.
5354 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5355 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5356}
5357
Michael Wrightd02c5b62014-02-10 15:10:22 -08005358TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005359 addConfigurationProperty("touch.deviceType", "touchScreen");
5360 prepareDisplay(DISPLAY_ORIENTATION_0);
5361 prepareButtons();
5362 prepareAxes(POSITION);
5363 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005364 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005365
arthurhungdcef2dc2020-08-11 14:47:50 +08005366 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005367
5368 NotifyMotionArgs motionArgs;
5369
5370 // Down.
5371 int32_t x = 100;
5372 int32_t y = 125;
5373 processDown(mapper, x, y);
5374 processSync(mapper);
5375
5376 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5377 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5378 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5379 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5380 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5381 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5382 ASSERT_EQ(0, motionArgs.flags);
5383 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5384 ASSERT_EQ(0, motionArgs.buttonState);
5385 ASSERT_EQ(0, motionArgs.edgeFlags);
5386 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5387 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5388 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5389 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5390 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5391 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5392 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5393 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5394
5395 // Move.
5396 x += 50;
5397 y += 75;
5398 processMove(mapper, x, y);
5399 processSync(mapper);
5400
5401 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5402 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5403 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5404 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5405 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5406 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5407 ASSERT_EQ(0, motionArgs.flags);
5408 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5409 ASSERT_EQ(0, motionArgs.buttonState);
5410 ASSERT_EQ(0, motionArgs.edgeFlags);
5411 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5412 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5413 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5414 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5415 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5416 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5417 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5418 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5419
5420 // Up.
5421 processUp(mapper);
5422 processSync(mapper);
5423
5424 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5425 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5426 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5427 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5428 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5429 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5430 ASSERT_EQ(0, motionArgs.flags);
5431 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5432 ASSERT_EQ(0, motionArgs.buttonState);
5433 ASSERT_EQ(0, motionArgs.edgeFlags);
5434 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5435 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5436 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5437 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5438 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5439 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5440 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5441 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5442
5443 // Should not have sent any more keys or motions.
5444 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5445 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5446}
5447
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005448TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_DoesNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005449 addConfigurationProperty("touch.deviceType", "touchScreen");
5450 prepareButtons();
5451 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005452 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
5453 // need to be rotated. Touchscreens are orientation-aware by default.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005454 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005455
5456 NotifyMotionArgs args;
5457
5458 // Rotation 90.
5459 prepareDisplay(DISPLAY_ORIENTATION_90);
5460 processDown(mapper, toRawX(50), toRawY(75));
5461 processSync(mapper);
5462
5463 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5464 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5465 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5466
5467 processUp(mapper);
5468 processSync(mapper);
5469 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5470}
5471
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005472TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_RotatesMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005473 addConfigurationProperty("touch.deviceType", "touchScreen");
5474 prepareButtons();
5475 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005476 // Since InputReader works in the un-rotated coordinate space, only devices that are not
5477 // orientation-aware are affected by display rotation.
5478 addConfigurationProperty("touch.orientationAware", "0");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005479 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005480
5481 NotifyMotionArgs args;
5482
5483 // Rotation 0.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005484 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005485 prepareDisplay(DISPLAY_ORIENTATION_0);
5486 processDown(mapper, toRawX(50), toRawY(75));
5487 processSync(mapper);
5488
5489 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5490 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5491 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5492
5493 processUp(mapper);
5494 processSync(mapper);
5495 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5496
5497 // Rotation 90.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005498 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005499 prepareDisplay(DISPLAY_ORIENTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005500 processDown(mapper, toRawX(75), RAW_Y_MAX - toRawY(50) + RAW_Y_MIN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005501 processSync(mapper);
5502
5503 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5504 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5505 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5506
5507 processUp(mapper);
5508 processSync(mapper);
5509 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5510
5511 // Rotation 180.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005512 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005513 prepareDisplay(DISPLAY_ORIENTATION_180);
5514 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
5515 processSync(mapper);
5516
5517 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5518 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5519 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5520
5521 processUp(mapper);
5522 processSync(mapper);
5523 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5524
5525 // Rotation 270.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005526 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005527 prepareDisplay(DISPLAY_ORIENTATION_270);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005528 processDown(mapper, RAW_X_MAX - toRawX(75) + RAW_X_MIN, toRawY(50));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005529 processSync(mapper);
5530
5531 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5532 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5533 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5534
5535 processUp(mapper);
5536 processSync(mapper);
5537 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5538}
5539
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005540TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation0_RotatesMotions) {
5541 addConfigurationProperty("touch.deviceType", "touchScreen");
5542 prepareButtons();
5543 prepareAxes(POSITION);
5544 addConfigurationProperty("touch.orientationAware", "1");
5545 addConfigurationProperty("touch.orientation", "ORIENTATION_0");
5546 clearViewports();
5547 prepareDisplay(DISPLAY_ORIENTATION_0);
5548 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5549 NotifyMotionArgs args;
5550
5551 // Orientation 0.
5552 processDown(mapper, toRawX(50), toRawY(75));
5553 processSync(mapper);
5554
5555 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5556 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5557 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5558
5559 processUp(mapper);
5560 processSync(mapper);
5561 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5562}
5563
5564TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation90_RotatesMotions) {
5565 addConfigurationProperty("touch.deviceType", "touchScreen");
5566 prepareButtons();
5567 prepareAxes(POSITION);
5568 addConfigurationProperty("touch.orientationAware", "1");
5569 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
5570 clearViewports();
5571 prepareDisplay(DISPLAY_ORIENTATION_0);
5572 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5573 NotifyMotionArgs args;
5574
5575 // Orientation 90.
5576 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
5577 processSync(mapper);
5578
5579 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5580 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5581 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5582
5583 processUp(mapper);
5584 processSync(mapper);
5585 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5586}
5587
5588TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation180_RotatesMotions) {
5589 addConfigurationProperty("touch.deviceType", "touchScreen");
5590 prepareButtons();
5591 prepareAxes(POSITION);
5592 addConfigurationProperty("touch.orientationAware", "1");
5593 addConfigurationProperty("touch.orientation", "ORIENTATION_180");
5594 clearViewports();
5595 prepareDisplay(DISPLAY_ORIENTATION_0);
5596 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5597 NotifyMotionArgs args;
5598
5599 // Orientation 180.
5600 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
5601 processSync(mapper);
5602
5603 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5604 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5605 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5606
5607 processUp(mapper);
5608 processSync(mapper);
5609 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5610}
5611
5612TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation270_RotatesMotions) {
5613 addConfigurationProperty("touch.deviceType", "touchScreen");
5614 prepareButtons();
5615 prepareAxes(POSITION);
5616 addConfigurationProperty("touch.orientationAware", "1");
5617 addConfigurationProperty("touch.orientation", "ORIENTATION_270");
5618 clearViewports();
5619 prepareDisplay(DISPLAY_ORIENTATION_0);
5620 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5621 NotifyMotionArgs args;
5622
5623 // Orientation 270.
5624 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
5625 processSync(mapper);
5626
5627 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5628 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5629 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5630
5631 processUp(mapper);
5632 processSync(mapper);
5633 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5634}
5635
5636TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationSpecified_RotatesMotionWithDisplay) {
5637 addConfigurationProperty("touch.deviceType", "touchScreen");
5638 prepareButtons();
5639 prepareAxes(POSITION);
5640 // Since InputReader works in the un-rotated coordinate space, only devices that are not
5641 // orientation-aware are affected by display rotation.
5642 addConfigurationProperty("touch.orientationAware", "0");
5643 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
5644 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5645
5646 NotifyMotionArgs args;
5647
5648 // Orientation 90, Rotation 0.
5649 clearViewports();
5650 prepareDisplay(DISPLAY_ORIENTATION_0);
5651 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
5652 processSync(mapper);
5653
5654 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5655 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5656 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5657
5658 processUp(mapper);
5659 processSync(mapper);
5660 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5661
5662 // Orientation 90, Rotation 90.
5663 clearViewports();
5664 prepareDisplay(DISPLAY_ORIENTATION_90);
5665 processDown(mapper, toRotatedRawX(50), toRotatedRawY(75));
5666 processSync(mapper);
5667
5668 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5669 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5670 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5671
5672 processUp(mapper);
5673 processSync(mapper);
5674 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5675
5676 // Orientation 90, Rotation 180.
5677 clearViewports();
5678 prepareDisplay(DISPLAY_ORIENTATION_180);
5679 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
5680 processSync(mapper);
5681
5682 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5683 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5684 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5685
5686 processUp(mapper);
5687 processSync(mapper);
5688 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5689
5690 // Orientation 90, Rotation 270.
5691 clearViewports();
5692 prepareDisplay(DISPLAY_ORIENTATION_270);
5693 processDown(mapper, RAW_X_MAX - toRotatedRawX(50) + RAW_X_MIN,
5694 RAW_Y_MAX - toRotatedRawY(75) + RAW_Y_MIN);
5695 processSync(mapper);
5696
5697 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5698 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5699 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5700
5701 processUp(mapper);
5702 processSync(mapper);
5703 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5704}
5705
Michael Wrightd02c5b62014-02-10 15:10:22 -08005706TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005707 addConfigurationProperty("touch.deviceType", "touchScreen");
5708 prepareDisplay(DISPLAY_ORIENTATION_0);
5709 prepareButtons();
5710 prepareAxes(POSITION | PRESSURE | TOOL | DISTANCE | TILT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005711 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005712
5713 // These calculations are based on the input device calibration documentation.
5714 int32_t rawX = 100;
5715 int32_t rawY = 200;
5716 int32_t rawPressure = 10;
5717 int32_t rawToolMajor = 12;
5718 int32_t rawDistance = 2;
5719 int32_t rawTiltX = 30;
5720 int32_t rawTiltY = 110;
5721
5722 float x = toDisplayX(rawX);
5723 float y = toDisplayY(rawY);
5724 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
5725 float size = float(rawToolMajor) / RAW_TOOL_MAX;
5726 float tool = float(rawToolMajor) * GEOMETRIC_SCALE;
5727 float distance = float(rawDistance);
5728
5729 float tiltCenter = (RAW_TILT_MAX + RAW_TILT_MIN) * 0.5f;
5730 float tiltScale = M_PI / 180;
5731 float tiltXAngle = (rawTiltX - tiltCenter) * tiltScale;
5732 float tiltYAngle = (rawTiltY - tiltCenter) * tiltScale;
5733 float orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
5734 float tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
5735
5736 processDown(mapper, rawX, rawY);
5737 processPressure(mapper, rawPressure);
5738 processToolMajor(mapper, rawToolMajor);
5739 processDistance(mapper, rawDistance);
5740 processTilt(mapper, rawTiltX, rawTiltY);
5741 processSync(mapper);
5742
5743 NotifyMotionArgs args;
5744 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5745 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5746 x, y, pressure, size, tool, tool, tool, tool, orientation, distance));
5747 ASSERT_EQ(tilt, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TILT));
5748}
5749
Jason Gerecke489fda82012-09-07 17:19:40 -07005750TEST_F(SingleTouchInputMapperTest, Process_XYAxes_AffineCalibration) {
Jason Gerecke489fda82012-09-07 17:19:40 -07005751 addConfigurationProperty("touch.deviceType", "touchScreen");
5752 prepareDisplay(DISPLAY_ORIENTATION_0);
5753 prepareLocationCalibration();
5754 prepareButtons();
5755 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005756 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Jason Gerecke489fda82012-09-07 17:19:40 -07005757
5758 int32_t rawX = 100;
5759 int32_t rawY = 200;
5760
5761 float x = toDisplayX(toCookedX(rawX, rawY));
5762 float y = toDisplayY(toCookedY(rawX, rawY));
5763
5764 processDown(mapper, rawX, rawY);
5765 processSync(mapper);
5766
5767 NotifyMotionArgs args;
5768 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5769 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5770 x, y, 1, 0, 0, 0, 0, 0, 0, 0));
5771}
5772
Michael Wrightd02c5b62014-02-10 15:10:22 -08005773TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005774 addConfigurationProperty("touch.deviceType", "touchScreen");
5775 prepareDisplay(DISPLAY_ORIENTATION_0);
5776 prepareButtons();
5777 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005778 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005779
5780 NotifyMotionArgs motionArgs;
5781 NotifyKeyArgs keyArgs;
5782
5783 processDown(mapper, 100, 200);
5784 processSync(mapper);
5785 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5786 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5787 ASSERT_EQ(0, motionArgs.buttonState);
5788
5789 // press BTN_LEFT, release BTN_LEFT
5790 processKey(mapper, BTN_LEFT, 1);
5791 processSync(mapper);
5792 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5793 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5794 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
5795
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005796 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5797 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5798 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
5799
Michael Wrightd02c5b62014-02-10 15:10:22 -08005800 processKey(mapper, BTN_LEFT, 0);
5801 processSync(mapper);
5802 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005803 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005804 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005805
5806 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005807 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005808 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005809
5810 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
5811 processKey(mapper, BTN_RIGHT, 1);
5812 processKey(mapper, BTN_MIDDLE, 1);
5813 processSync(mapper);
5814 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5815 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5816 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5817 motionArgs.buttonState);
5818
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005819 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5820 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5821 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
5822
5823 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5824 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5825 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5826 motionArgs.buttonState);
5827
Michael Wrightd02c5b62014-02-10 15:10:22 -08005828 processKey(mapper, BTN_RIGHT, 0);
5829 processSync(mapper);
5830 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005831 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005832 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005833
5834 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005835 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005836 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005837
5838 processKey(mapper, BTN_MIDDLE, 0);
5839 processSync(mapper);
5840 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005841 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005842 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005843
5844 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005845 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005846 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005847
5848 // press BTN_BACK, release BTN_BACK
5849 processKey(mapper, BTN_BACK, 1);
5850 processSync(mapper);
5851 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5852 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5853 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005854
Michael Wrightd02c5b62014-02-10 15:10:22 -08005855 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005856 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005857 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5858
5859 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5860 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5861 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005862
5863 processKey(mapper, BTN_BACK, 0);
5864 processSync(mapper);
5865 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005866 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005867 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005868
5869 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005870 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005871 ASSERT_EQ(0, motionArgs.buttonState);
5872
Michael Wrightd02c5b62014-02-10 15:10:22 -08005873 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5874 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5875 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
5876
5877 // press BTN_SIDE, release BTN_SIDE
5878 processKey(mapper, BTN_SIDE, 1);
5879 processSync(mapper);
5880 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5881 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5882 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005883
Michael Wrightd02c5b62014-02-10 15:10:22 -08005884 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005885 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005886 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5887
5888 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5889 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5890 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005891
5892 processKey(mapper, BTN_SIDE, 0);
5893 processSync(mapper);
5894 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005895 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005896 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005897
5898 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005899 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005900 ASSERT_EQ(0, motionArgs.buttonState);
5901
Michael Wrightd02c5b62014-02-10 15:10:22 -08005902 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5903 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5904 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
5905
5906 // press BTN_FORWARD, release BTN_FORWARD
5907 processKey(mapper, BTN_FORWARD, 1);
5908 processSync(mapper);
5909 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5910 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5911 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005912
Michael Wrightd02c5b62014-02-10 15:10:22 -08005913 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005914 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005915 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5916
5917 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5918 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5919 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005920
5921 processKey(mapper, BTN_FORWARD, 0);
5922 processSync(mapper);
5923 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005924 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005925 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005926
5927 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005928 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005929 ASSERT_EQ(0, motionArgs.buttonState);
5930
Michael Wrightd02c5b62014-02-10 15:10:22 -08005931 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5932 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5933 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
5934
5935 // press BTN_EXTRA, release BTN_EXTRA
5936 processKey(mapper, BTN_EXTRA, 1);
5937 processSync(mapper);
5938 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5939 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5940 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005941
Michael Wrightd02c5b62014-02-10 15:10:22 -08005942 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005943 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005944 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5945
5946 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5947 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5948 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005949
5950 processKey(mapper, BTN_EXTRA, 0);
5951 processSync(mapper);
5952 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005953 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005954 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005955
5956 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005957 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005958 ASSERT_EQ(0, motionArgs.buttonState);
5959
Michael Wrightd02c5b62014-02-10 15:10:22 -08005960 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5961 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5962 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
5963
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005964 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5965
Michael Wrightd02c5b62014-02-10 15:10:22 -08005966 // press BTN_STYLUS, release BTN_STYLUS
5967 processKey(mapper, BTN_STYLUS, 1);
5968 processSync(mapper);
5969 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5970 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005971 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
5972
5973 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5974 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5975 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005976
5977 processKey(mapper, BTN_STYLUS, 0);
5978 processSync(mapper);
5979 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005980 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005981 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005982
5983 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005984 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005985 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005986
5987 // press BTN_STYLUS2, release BTN_STYLUS2
5988 processKey(mapper, BTN_STYLUS2, 1);
5989 processSync(mapper);
5990 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5991 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005992 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
5993
5994 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5995 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5996 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005997
5998 processKey(mapper, BTN_STYLUS2, 0);
5999 processSync(mapper);
6000 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006001 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006002 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006003
6004 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006005 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006006 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006007
6008 // release touch
6009 processUp(mapper);
6010 processSync(mapper);
6011 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6012 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6013 ASSERT_EQ(0, motionArgs.buttonState);
6014}
6015
6016TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006017 addConfigurationProperty("touch.deviceType", "touchScreen");
6018 prepareDisplay(DISPLAY_ORIENTATION_0);
6019 prepareButtons();
6020 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006021 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006022
6023 NotifyMotionArgs motionArgs;
6024
6025 // default tool type is finger
6026 processDown(mapper, 100, 200);
6027 processSync(mapper);
6028 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6029 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6030 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6031
6032 // eraser
6033 processKey(mapper, BTN_TOOL_RUBBER, 1);
6034 processSync(mapper);
6035 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6036 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6037 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6038
6039 // stylus
6040 processKey(mapper, BTN_TOOL_RUBBER, 0);
6041 processKey(mapper, BTN_TOOL_PEN, 1);
6042 processSync(mapper);
6043 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6044 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6045 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6046
6047 // brush
6048 processKey(mapper, BTN_TOOL_PEN, 0);
6049 processKey(mapper, BTN_TOOL_BRUSH, 1);
6050 processSync(mapper);
6051 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6052 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6053 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6054
6055 // pencil
6056 processKey(mapper, BTN_TOOL_BRUSH, 0);
6057 processKey(mapper, BTN_TOOL_PENCIL, 1);
6058 processSync(mapper);
6059 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6060 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6061 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6062
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08006063 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08006064 processKey(mapper, BTN_TOOL_PENCIL, 0);
6065 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
6066 processSync(mapper);
6067 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6068 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6069 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6070
6071 // mouse
6072 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
6073 processKey(mapper, BTN_TOOL_MOUSE, 1);
6074 processSync(mapper);
6075 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6076 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6077 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6078
6079 // lens
6080 processKey(mapper, BTN_TOOL_MOUSE, 0);
6081 processKey(mapper, BTN_TOOL_LENS, 1);
6082 processSync(mapper);
6083 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6084 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6085 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6086
6087 // double-tap
6088 processKey(mapper, BTN_TOOL_LENS, 0);
6089 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
6090 processSync(mapper);
6091 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6092 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6093 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6094
6095 // triple-tap
6096 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
6097 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
6098 processSync(mapper);
6099 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6100 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6101 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6102
6103 // quad-tap
6104 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
6105 processKey(mapper, BTN_TOOL_QUADTAP, 1);
6106 processSync(mapper);
6107 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6108 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6109 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6110
6111 // finger
6112 processKey(mapper, BTN_TOOL_QUADTAP, 0);
6113 processKey(mapper, BTN_TOOL_FINGER, 1);
6114 processSync(mapper);
6115 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6116 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6117 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6118
6119 // stylus trumps finger
6120 processKey(mapper, BTN_TOOL_PEN, 1);
6121 processSync(mapper);
6122 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6123 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6124 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6125
6126 // eraser trumps stylus
6127 processKey(mapper, BTN_TOOL_RUBBER, 1);
6128 processSync(mapper);
6129 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6130 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6131 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6132
6133 // mouse trumps eraser
6134 processKey(mapper, BTN_TOOL_MOUSE, 1);
6135 processSync(mapper);
6136 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6137 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6138 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6139
6140 // back to default tool type
6141 processKey(mapper, BTN_TOOL_MOUSE, 0);
6142 processKey(mapper, BTN_TOOL_RUBBER, 0);
6143 processKey(mapper, BTN_TOOL_PEN, 0);
6144 processKey(mapper, BTN_TOOL_FINGER, 0);
6145 processSync(mapper);
6146 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6147 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6148 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6149}
6150
6151TEST_F(SingleTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006152 addConfigurationProperty("touch.deviceType", "touchScreen");
6153 prepareDisplay(DISPLAY_ORIENTATION_0);
6154 prepareButtons();
6155 prepareAxes(POSITION);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006156 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_FINGER, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006157 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006158
6159 NotifyMotionArgs motionArgs;
6160
6161 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
6162 processKey(mapper, BTN_TOOL_FINGER, 1);
6163 processMove(mapper, 100, 200);
6164 processSync(mapper);
6165 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6166 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6167 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6168 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6169
6170 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6171 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6172 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6173 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6174
6175 // move a little
6176 processMove(mapper, 150, 250);
6177 processSync(mapper);
6178 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6179 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6180 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6181 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6182
6183 // down when BTN_TOUCH is pressed, pressure defaults to 1
6184 processKey(mapper, BTN_TOUCH, 1);
6185 processSync(mapper);
6186 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6187 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6188 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6189 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6190
6191 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6192 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6193 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6194 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6195
6196 // up when BTN_TOUCH is released, hover restored
6197 processKey(mapper, BTN_TOUCH, 0);
6198 processSync(mapper);
6199 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6200 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6201 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6202 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6203
6204 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6205 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6206 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6207 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6208
6209 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6210 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6211 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6212 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6213
6214 // exit hover when pointer goes away
6215 processKey(mapper, BTN_TOOL_FINGER, 0);
6216 processSync(mapper);
6217 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6218 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6219 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6220 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6221}
6222
6223TEST_F(SingleTouchInputMapperTest, Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006224 addConfigurationProperty("touch.deviceType", "touchScreen");
6225 prepareDisplay(DISPLAY_ORIENTATION_0);
6226 prepareButtons();
6227 prepareAxes(POSITION | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006228 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006229
6230 NotifyMotionArgs motionArgs;
6231
6232 // initially hovering because pressure is 0
6233 processDown(mapper, 100, 200);
6234 processPressure(mapper, 0);
6235 processSync(mapper);
6236 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6237 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6238 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6239 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6240
6241 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6242 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6243 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6244 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6245
6246 // move a little
6247 processMove(mapper, 150, 250);
6248 processSync(mapper);
6249 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6250 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6251 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6252 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6253
6254 // down when pressure is non-zero
6255 processPressure(mapper, RAW_PRESSURE_MAX);
6256 processSync(mapper);
6257 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6258 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6259 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6260 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6261
6262 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6263 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6264 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6265 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6266
6267 // up when pressure becomes 0, hover restored
6268 processPressure(mapper, 0);
6269 processSync(mapper);
6270 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6271 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6272 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6273 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6274
6275 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6276 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6277 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6278 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6279
6280 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6281 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6282 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6283 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6284
6285 // exit hover when pointer goes away
6286 processUp(mapper);
6287 processSync(mapper);
6288 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6289 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6290 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6291 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6292}
6293
Prabir Pradhan5632d622021-09-06 07:57:20 -07006294// --- TouchDisplayProjectionTest ---
6295
6296class TouchDisplayProjectionTest : public SingleTouchInputMapperTest {
6297public:
6298 // The values inside DisplayViewport are expected to be pre-rotated. This updates the current
6299 // DisplayViewport to pre-rotate the values. The viewport's physical display will be set to the
6300 // rotated equivalent of the given un-rotated physical display bounds.
6301 void configurePhysicalDisplay(int32_t orientation, Rect naturalPhysicalDisplay) {
6302 uint32_t inverseRotationFlags;
6303 auto width = DISPLAY_WIDTH;
6304 auto height = DISPLAY_HEIGHT;
6305 switch (orientation) {
6306 case DISPLAY_ORIENTATION_90:
6307 inverseRotationFlags = ui::Transform::ROT_270;
6308 std::swap(width, height);
6309 break;
6310 case DISPLAY_ORIENTATION_180:
6311 inverseRotationFlags = ui::Transform::ROT_180;
6312 break;
6313 case DISPLAY_ORIENTATION_270:
6314 inverseRotationFlags = ui::Transform::ROT_90;
6315 std::swap(width, height);
6316 break;
6317 case DISPLAY_ORIENTATION_0:
6318 inverseRotationFlags = ui::Transform::ROT_0;
6319 break;
6320 default:
6321 FAIL() << "Invalid orientation: " << orientation;
6322 }
6323
6324 const ui::Transform rotation(inverseRotationFlags, width, height);
6325 const Rect rotatedPhysicalDisplay = rotation.transform(naturalPhysicalDisplay);
6326
6327 std::optional<DisplayViewport> internalViewport =
6328 *mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6329 DisplayViewport& v = *internalViewport;
6330 v.displayId = DISPLAY_ID;
6331 v.orientation = orientation;
6332
6333 v.logicalLeft = 0;
6334 v.logicalTop = 0;
6335 v.logicalRight = 100;
6336 v.logicalBottom = 100;
6337
6338 v.physicalLeft = rotatedPhysicalDisplay.left;
6339 v.physicalTop = rotatedPhysicalDisplay.top;
6340 v.physicalRight = rotatedPhysicalDisplay.right;
6341 v.physicalBottom = rotatedPhysicalDisplay.bottom;
6342
6343 v.deviceWidth = width;
6344 v.deviceHeight = height;
6345
6346 v.isActive = true;
6347 v.uniqueId = UNIQUE_ID;
6348 v.type = ViewportType::INTERNAL;
6349 mFakePolicy->updateViewport(v);
6350 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
6351 }
6352
6353 void assertReceivedMove(const Point& point) {
6354 NotifyMotionArgs motionArgs;
6355 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6356 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6357 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6358 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], point.x, point.y,
6359 1, 0, 0, 0, 0, 0, 0, 0));
6360 }
6361};
6362
6363TEST_F(TouchDisplayProjectionTest, IgnoresTouchesOutsidePhysicalDisplay) {
6364 addConfigurationProperty("touch.deviceType", "touchScreen");
6365 prepareDisplay(DISPLAY_ORIENTATION_0);
6366
6367 prepareButtons();
6368 prepareAxes(POSITION);
6369 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6370
6371 NotifyMotionArgs motionArgs;
6372
6373 // Configure the DisplayViewport such that the logical display maps to a subsection of
6374 // the display panel called the physical display. Here, the physical display is bounded by the
6375 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
6376 static const Rect kPhysicalDisplay{10, 20, 70, 160};
6377 static const std::array<Point, 6> kPointsOutsidePhysicalDisplay{
6378 {{-10, -10}, {0, 0}, {5, 100}, {50, 15}, {75, 100}, {50, 165}}};
6379
6380 for (auto orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90, DISPLAY_ORIENTATION_180,
6381 DISPLAY_ORIENTATION_270}) {
6382 configurePhysicalDisplay(orientation, kPhysicalDisplay);
6383
6384 // Touches outside the physical display should be ignored, and should not generate any
6385 // events. Ensure touches at the following points that lie outside of the physical display
6386 // area do not generate any events.
6387 for (const auto& point : kPointsOutsidePhysicalDisplay) {
6388 processDown(mapper, toRawX(point.x), toRawY(point.y));
6389 processSync(mapper);
6390 processUp(mapper);
6391 processSync(mapper);
6392 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled())
6393 << "Unexpected event generated for touch outside physical display at point: "
6394 << point.x << ", " << point.y;
6395 }
6396 }
6397}
6398
6399TEST_F(TouchDisplayProjectionTest, EmitsTouchDownAfterEnteringPhysicalDisplay) {
6400 addConfigurationProperty("touch.deviceType", "touchScreen");
6401 prepareDisplay(DISPLAY_ORIENTATION_0);
6402
6403 prepareButtons();
6404 prepareAxes(POSITION);
6405 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6406
6407 NotifyMotionArgs motionArgs;
6408
6409 // Configure the DisplayViewport such that the logical display maps to a subsection of
6410 // the display panel called the physical display. Here, the physical display is bounded by the
6411 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
6412 static const Rect kPhysicalDisplay{10, 20, 70, 160};
6413
6414 for (auto orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90, DISPLAY_ORIENTATION_180,
6415 DISPLAY_ORIENTATION_270}) {
6416 configurePhysicalDisplay(orientation, kPhysicalDisplay);
6417
6418 // Touches that start outside the physical display should be ignored until it enters the
6419 // physical display bounds, at which point it should generate a down event. Start a touch at
6420 // the point (5, 100), which is outside the physical display bounds.
6421 static const Point kOutsidePoint{5, 100};
6422 processDown(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
6423 processSync(mapper);
6424 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6425
6426 // Move the touch into the physical display area. This should generate a pointer down.
6427 processMove(mapper, toRawX(11), toRawY(21));
6428 processSync(mapper);
6429 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6430 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6431 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6432 ASSERT_NO_FATAL_FAILURE(
6433 assertPointerCoords(motionArgs.pointerCoords[0], 11, 21, 1, 0, 0, 0, 0, 0, 0, 0));
6434
6435 // Move the touch inside the physical display area. This should generate a pointer move.
6436 processMove(mapper, toRawX(69), toRawY(159));
6437 processSync(mapper);
6438 assertReceivedMove({69, 159});
6439
6440 // Move outside the physical display area. Since the pointer is already down, this should
6441 // now continue generating events.
6442 processMove(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
6443 processSync(mapper);
6444 assertReceivedMove(kOutsidePoint);
6445
6446 // Release. This should generate a pointer up.
6447 processUp(mapper);
6448 processSync(mapper);
6449 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6450 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6451 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], kOutsidePoint.x,
6452 kOutsidePoint.y, 1, 0, 0, 0, 0, 0, 0, 0));
6453
6454 // Ensure no more events were generated.
6455 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6456 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6457 }
6458}
6459
Michael Wrightd02c5b62014-02-10 15:10:22 -08006460// --- MultiTouchInputMapperTest ---
6461
6462class MultiTouchInputMapperTest : public TouchInputMapperTest {
6463protected:
6464 void prepareAxes(int axes);
6465
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006466 void processPosition(MultiTouchInputMapper& mapper, int32_t x, int32_t y);
6467 void processTouchMajor(MultiTouchInputMapper& mapper, int32_t touchMajor);
6468 void processTouchMinor(MultiTouchInputMapper& mapper, int32_t touchMinor);
6469 void processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor);
6470 void processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor);
6471 void processOrientation(MultiTouchInputMapper& mapper, int32_t orientation);
6472 void processPressure(MultiTouchInputMapper& mapper, int32_t pressure);
6473 void processDistance(MultiTouchInputMapper& mapper, int32_t distance);
6474 void processId(MultiTouchInputMapper& mapper, int32_t id);
6475 void processSlot(MultiTouchInputMapper& mapper, int32_t slot);
6476 void processToolType(MultiTouchInputMapper& mapper, int32_t toolType);
6477 void processKey(MultiTouchInputMapper& mapper, int32_t code, int32_t value);
6478 void processMTSync(MultiTouchInputMapper& mapper);
6479 void processSync(MultiTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006480};
6481
6482void MultiTouchInputMapperTest::prepareAxes(int axes) {
6483 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006484 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
6485 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006486 }
6487 if (axes & TOUCH) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006488 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN,
6489 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006490 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006491 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN,
6492 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006493 }
6494 }
6495 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006496 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
6497 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006498 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006499 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MAX,
6500 RAW_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006501 }
6502 }
6503 if (axes & ORIENTATION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006504 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_ORIENTATION, RAW_ORIENTATION_MIN,
6505 RAW_ORIENTATION_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006506 }
6507 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006508 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_PRESSURE, RAW_PRESSURE_MIN,
6509 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006510 }
6511 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006512 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_DISTANCE, RAW_DISTANCE_MIN,
6513 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006514 }
6515 if (axes & ID) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006516 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX, 0,
6517 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006518 }
6519 if (axes & SLOT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006520 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
6521 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_MT_SLOT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006522 }
6523 if (axes & TOOL_TYPE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006524 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOOL_TYPE, 0, MT_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006525 }
6526}
6527
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006528void MultiTouchInputMapperTest::processPosition(MultiTouchInputMapper& mapper, int32_t x,
6529 int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006530 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_X, x);
6531 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006532}
6533
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006534void MultiTouchInputMapperTest::processTouchMajor(MultiTouchInputMapper& mapper,
6535 int32_t touchMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006536 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006537}
6538
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006539void MultiTouchInputMapperTest::processTouchMinor(MultiTouchInputMapper& mapper,
6540 int32_t touchMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006541 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006542}
6543
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006544void MultiTouchInputMapperTest::processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006545 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006546}
6547
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006548void MultiTouchInputMapperTest::processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006549 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006550}
6551
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006552void MultiTouchInputMapperTest::processOrientation(MultiTouchInputMapper& mapper,
6553 int32_t orientation) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006554 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_ORIENTATION, orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006555}
6556
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006557void MultiTouchInputMapperTest::processPressure(MultiTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006558 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006559}
6560
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006561void MultiTouchInputMapperTest::processDistance(MultiTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006562 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006563}
6564
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006565void MultiTouchInputMapperTest::processId(MultiTouchInputMapper& mapper, int32_t id) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006566 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TRACKING_ID, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006567}
6568
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006569void MultiTouchInputMapperTest::processSlot(MultiTouchInputMapper& mapper, int32_t slot) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006570 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_SLOT, slot);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006571}
6572
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006573void MultiTouchInputMapperTest::processToolType(MultiTouchInputMapper& mapper, int32_t toolType) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006574 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006575}
6576
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006577void MultiTouchInputMapperTest::processKey(MultiTouchInputMapper& mapper, int32_t code,
6578 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006579 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006580}
6581
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006582void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006583 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_MT_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006584}
6585
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006586void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006587 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006588}
6589
Michael Wrightd02c5b62014-02-10 15:10:22 -08006590TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006591 addConfigurationProperty("touch.deviceType", "touchScreen");
6592 prepareDisplay(DISPLAY_ORIENTATION_0);
6593 prepareAxes(POSITION);
6594 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006595 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006596
arthurhungdcef2dc2020-08-11 14:47:50 +08006597 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006598
6599 NotifyMotionArgs motionArgs;
6600
6601 // Two fingers down at once.
6602 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
6603 processPosition(mapper, x1, y1);
6604 processMTSync(mapper);
6605 processPosition(mapper, x2, y2);
6606 processMTSync(mapper);
6607 processSync(mapper);
6608
6609 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6610 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6611 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6612 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6613 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6614 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6615 ASSERT_EQ(0, motionArgs.flags);
6616 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6617 ASSERT_EQ(0, motionArgs.buttonState);
6618 ASSERT_EQ(0, motionArgs.edgeFlags);
6619 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6620 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6621 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6622 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6623 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6624 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6625 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6626 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6627
6628 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6629 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6630 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6631 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6632 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6633 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6634 motionArgs.action);
6635 ASSERT_EQ(0, motionArgs.flags);
6636 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6637 ASSERT_EQ(0, motionArgs.buttonState);
6638 ASSERT_EQ(0, motionArgs.edgeFlags);
6639 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6640 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6641 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6642 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6643 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6644 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6645 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6646 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6647 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6648 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6649 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6650 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6651
6652 // Move.
6653 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
6654 processPosition(mapper, x1, y1);
6655 processMTSync(mapper);
6656 processPosition(mapper, x2, y2);
6657 processMTSync(mapper);
6658 processSync(mapper);
6659
6660 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6661 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6662 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6663 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6664 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6665 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6666 ASSERT_EQ(0, motionArgs.flags);
6667 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6668 ASSERT_EQ(0, motionArgs.buttonState);
6669 ASSERT_EQ(0, motionArgs.edgeFlags);
6670 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6671 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6672 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6673 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6674 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6675 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6676 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6677 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6678 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6679 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6680 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6681 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6682
6683 // First finger up.
6684 x2 += 15; y2 -= 20;
6685 processPosition(mapper, x2, y2);
6686 processMTSync(mapper);
6687 processSync(mapper);
6688
6689 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6690 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6691 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6692 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6693 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6694 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6695 motionArgs.action);
6696 ASSERT_EQ(0, motionArgs.flags);
6697 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6698 ASSERT_EQ(0, motionArgs.buttonState);
6699 ASSERT_EQ(0, motionArgs.edgeFlags);
6700 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6701 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6702 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6703 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6704 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6705 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6706 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6707 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6708 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6709 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6710 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6711 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6712
6713 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6714 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6715 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6716 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6717 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6718 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6719 ASSERT_EQ(0, motionArgs.flags);
6720 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6721 ASSERT_EQ(0, motionArgs.buttonState);
6722 ASSERT_EQ(0, motionArgs.edgeFlags);
6723 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6724 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6725 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6726 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6727 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6728 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6729 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6730 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6731
6732 // Move.
6733 x2 += 20; y2 -= 25;
6734 processPosition(mapper, x2, y2);
6735 processMTSync(mapper);
6736 processSync(mapper);
6737
6738 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6739 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6740 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6741 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6742 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6743 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6744 ASSERT_EQ(0, motionArgs.flags);
6745 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6746 ASSERT_EQ(0, motionArgs.buttonState);
6747 ASSERT_EQ(0, motionArgs.edgeFlags);
6748 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6749 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6750 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6751 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6752 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6753 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6754 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6755 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6756
6757 // New finger down.
6758 int32_t x3 = 700, y3 = 300;
6759 processPosition(mapper, x2, y2);
6760 processMTSync(mapper);
6761 processPosition(mapper, x3, y3);
6762 processMTSync(mapper);
6763 processSync(mapper);
6764
6765 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6766 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6767 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6768 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6769 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6770 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6771 motionArgs.action);
6772 ASSERT_EQ(0, motionArgs.flags);
6773 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6774 ASSERT_EQ(0, motionArgs.buttonState);
6775 ASSERT_EQ(0, motionArgs.edgeFlags);
6776 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6777 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6778 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6779 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6780 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6781 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6782 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6783 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6784 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6785 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6786 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6787 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6788
6789 // Second finger up.
6790 x3 += 30; y3 -= 20;
6791 processPosition(mapper, x3, y3);
6792 processMTSync(mapper);
6793 processSync(mapper);
6794
6795 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6796 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6797 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6798 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6799 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6800 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6801 motionArgs.action);
6802 ASSERT_EQ(0, motionArgs.flags);
6803 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6804 ASSERT_EQ(0, motionArgs.buttonState);
6805 ASSERT_EQ(0, motionArgs.edgeFlags);
6806 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6807 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6808 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6809 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6810 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6811 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6812 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6813 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6814 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6815 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6816 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6817 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6818
6819 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6820 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6821 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6822 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6823 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6824 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6825 ASSERT_EQ(0, motionArgs.flags);
6826 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6827 ASSERT_EQ(0, motionArgs.buttonState);
6828 ASSERT_EQ(0, motionArgs.edgeFlags);
6829 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6830 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6831 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6832 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6833 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6834 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6835 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6836 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6837
6838 // Last finger up.
6839 processMTSync(mapper);
6840 processSync(mapper);
6841
6842 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6843 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6844 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6845 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6846 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6847 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6848 ASSERT_EQ(0, motionArgs.flags);
6849 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6850 ASSERT_EQ(0, motionArgs.buttonState);
6851 ASSERT_EQ(0, motionArgs.edgeFlags);
6852 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6853 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6854 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6855 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6856 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6857 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6858 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6859 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6860
6861 // Should not have sent any more keys or motions.
6862 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6863 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6864}
6865
6866TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006867 addConfigurationProperty("touch.deviceType", "touchScreen");
6868 prepareDisplay(DISPLAY_ORIENTATION_0);
6869 prepareAxes(POSITION | ID);
6870 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006871 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006872
arthurhungdcef2dc2020-08-11 14:47:50 +08006873 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006874
6875 NotifyMotionArgs motionArgs;
6876
6877 // Two fingers down at once.
6878 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
6879 processPosition(mapper, x1, y1);
6880 processId(mapper, 1);
6881 processMTSync(mapper);
6882 processPosition(mapper, x2, y2);
6883 processId(mapper, 2);
6884 processMTSync(mapper);
6885 processSync(mapper);
6886
6887 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6888 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6889 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6890 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6891 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6892 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6893 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6894
6895 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6896 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6897 motionArgs.action);
6898 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6899 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6900 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6901 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6902 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6903 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6904 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6905 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6906 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6907
6908 // Move.
6909 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
6910 processPosition(mapper, x1, y1);
6911 processId(mapper, 1);
6912 processMTSync(mapper);
6913 processPosition(mapper, x2, y2);
6914 processId(mapper, 2);
6915 processMTSync(mapper);
6916 processSync(mapper);
6917
6918 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6919 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6920 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6921 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6922 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6923 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6924 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6925 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6926 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6927 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6928 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6929
6930 // First finger up.
6931 x2 += 15; y2 -= 20;
6932 processPosition(mapper, x2, y2);
6933 processId(mapper, 2);
6934 processMTSync(mapper);
6935 processSync(mapper);
6936
6937 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6938 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6939 motionArgs.action);
6940 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6941 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6942 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6943 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6944 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6945 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6946 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6947 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6948 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6949
6950 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6951 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6952 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6953 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6954 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6955 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6956 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6957
6958 // Move.
6959 x2 += 20; y2 -= 25;
6960 processPosition(mapper, x2, y2);
6961 processId(mapper, 2);
6962 processMTSync(mapper);
6963 processSync(mapper);
6964
6965 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6966 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6967 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6968 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6969 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6970 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6971 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6972
6973 // New finger down.
6974 int32_t x3 = 700, y3 = 300;
6975 processPosition(mapper, x2, y2);
6976 processId(mapper, 2);
6977 processMTSync(mapper);
6978 processPosition(mapper, x3, y3);
6979 processId(mapper, 3);
6980 processMTSync(mapper);
6981 processSync(mapper);
6982
6983 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6984 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6985 motionArgs.action);
6986 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6987 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6988 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6989 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6990 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6991 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6992 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6993 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6994 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6995
6996 // Second finger up.
6997 x3 += 30; y3 -= 20;
6998 processPosition(mapper, x3, y3);
6999 processId(mapper, 3);
7000 processMTSync(mapper);
7001 processSync(mapper);
7002
7003 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7004 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7005 motionArgs.action);
7006 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7007 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7008 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7009 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7010 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7011 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7012 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7013 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7014 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7015
7016 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7017 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7018 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7019 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7020 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7021 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7022 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7023
7024 // Last finger up.
7025 processMTSync(mapper);
7026 processSync(mapper);
7027
7028 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7029 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7030 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7031 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7032 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7033 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7034 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7035
7036 // Should not have sent any more keys or motions.
7037 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7038 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7039}
7040
7041TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007042 addConfigurationProperty("touch.deviceType", "touchScreen");
7043 prepareDisplay(DISPLAY_ORIENTATION_0);
7044 prepareAxes(POSITION | ID | SLOT);
7045 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007046 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007047
arthurhungdcef2dc2020-08-11 14:47:50 +08007048 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007049
7050 NotifyMotionArgs motionArgs;
7051
7052 // Two fingers down at once.
7053 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7054 processPosition(mapper, x1, y1);
7055 processId(mapper, 1);
7056 processSlot(mapper, 1);
7057 processPosition(mapper, x2, y2);
7058 processId(mapper, 2);
7059 processSync(mapper);
7060
7061 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7062 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7063 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7064 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7065 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7066 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7067 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7068
7069 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7070 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7071 motionArgs.action);
7072 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7073 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7074 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7075 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7076 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7077 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7078 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7079 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7080 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7081
7082 // Move.
7083 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7084 processSlot(mapper, 0);
7085 processPosition(mapper, x1, y1);
7086 processSlot(mapper, 1);
7087 processPosition(mapper, x2, y2);
7088 processSync(mapper);
7089
7090 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7091 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7092 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7093 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7094 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7095 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7096 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7097 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7098 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7099 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7100 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7101
7102 // First finger up.
7103 x2 += 15; y2 -= 20;
7104 processSlot(mapper, 0);
7105 processId(mapper, -1);
7106 processSlot(mapper, 1);
7107 processPosition(mapper, x2, y2);
7108 processSync(mapper);
7109
7110 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7111 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7112 motionArgs.action);
7113 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7114 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7115 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7116 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7117 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7118 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7119 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7120 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7121 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7122
7123 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7124 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7125 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7126 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7127 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7128 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7129 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7130
7131 // Move.
7132 x2 += 20; y2 -= 25;
7133 processPosition(mapper, x2, y2);
7134 processSync(mapper);
7135
7136 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7137 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7138 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7139 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7140 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7141 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7142 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7143
7144 // New finger down.
7145 int32_t x3 = 700, y3 = 300;
7146 processPosition(mapper, x2, y2);
7147 processSlot(mapper, 0);
7148 processId(mapper, 3);
7149 processPosition(mapper, x3, y3);
7150 processSync(mapper);
7151
7152 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7153 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7154 motionArgs.action);
7155 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7156 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7157 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7158 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7159 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7160 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7161 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7162 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7163 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7164
7165 // Second finger up.
7166 x3 += 30; y3 -= 20;
7167 processSlot(mapper, 1);
7168 processId(mapper, -1);
7169 processSlot(mapper, 0);
7170 processPosition(mapper, x3, y3);
7171 processSync(mapper);
7172
7173 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7174 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7175 motionArgs.action);
7176 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7177 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7178 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7179 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7180 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7181 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7182 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7183 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7184 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7185
7186 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7187 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7188 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7189 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7190 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7191 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7192 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7193
7194 // Last finger up.
7195 processId(mapper, -1);
7196 processSync(mapper);
7197
7198 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7199 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7200 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7201 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7202 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7203 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7204 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7205
7206 // Should not have sent any more keys or motions.
7207 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7208 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7209}
7210
7211TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007212 addConfigurationProperty("touch.deviceType", "touchScreen");
7213 prepareDisplay(DISPLAY_ORIENTATION_0);
7214 prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007215 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007216
7217 // These calculations are based on the input device calibration documentation.
7218 int32_t rawX = 100;
7219 int32_t rawY = 200;
7220 int32_t rawTouchMajor = 7;
7221 int32_t rawTouchMinor = 6;
7222 int32_t rawToolMajor = 9;
7223 int32_t rawToolMinor = 8;
7224 int32_t rawPressure = 11;
7225 int32_t rawDistance = 0;
7226 int32_t rawOrientation = 3;
7227 int32_t id = 5;
7228
7229 float x = toDisplayX(rawX);
7230 float y = toDisplayY(rawY);
7231 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
7232 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
7233 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
7234 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
7235 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
7236 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
7237 float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
7238 float distance = float(rawDistance);
7239
7240 processPosition(mapper, rawX, rawY);
7241 processTouchMajor(mapper, rawTouchMajor);
7242 processTouchMinor(mapper, rawTouchMinor);
7243 processToolMajor(mapper, rawToolMajor);
7244 processToolMinor(mapper, rawToolMinor);
7245 processPressure(mapper, rawPressure);
7246 processOrientation(mapper, rawOrientation);
7247 processDistance(mapper, rawDistance);
7248 processId(mapper, id);
7249 processMTSync(mapper);
7250 processSync(mapper);
7251
7252 NotifyMotionArgs args;
7253 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7254 ASSERT_EQ(0, args.pointerProperties[0].id);
7255 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7256 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
7257 orientation, distance));
7258}
7259
7260TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007261 addConfigurationProperty("touch.deviceType", "touchScreen");
7262 prepareDisplay(DISPLAY_ORIENTATION_0);
7263 prepareAxes(POSITION | TOUCH | TOOL | MINOR);
7264 addConfigurationProperty("touch.size.calibration", "geometric");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007265 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007266
7267 // These calculations are based on the input device calibration documentation.
7268 int32_t rawX = 100;
7269 int32_t rawY = 200;
7270 int32_t rawTouchMajor = 140;
7271 int32_t rawTouchMinor = 120;
7272 int32_t rawToolMajor = 180;
7273 int32_t rawToolMinor = 160;
7274
7275 float x = toDisplayX(rawX);
7276 float y = toDisplayY(rawY);
7277 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
7278 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
7279 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
7280 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
7281 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
7282
7283 processPosition(mapper, rawX, rawY);
7284 processTouchMajor(mapper, rawTouchMajor);
7285 processTouchMinor(mapper, rawTouchMinor);
7286 processToolMajor(mapper, rawToolMajor);
7287 processToolMinor(mapper, rawToolMinor);
7288 processMTSync(mapper);
7289 processSync(mapper);
7290
7291 NotifyMotionArgs args;
7292 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7293 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7294 x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
7295}
7296
7297TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007298 addConfigurationProperty("touch.deviceType", "touchScreen");
7299 prepareDisplay(DISPLAY_ORIENTATION_0);
7300 prepareAxes(POSITION | TOUCH | TOOL);
7301 addConfigurationProperty("touch.size.calibration", "diameter");
7302 addConfigurationProperty("touch.size.scale", "10");
7303 addConfigurationProperty("touch.size.bias", "160");
7304 addConfigurationProperty("touch.size.isSummed", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007305 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007306
7307 // These calculations are based on the input device calibration documentation.
7308 // Note: We only provide a single common touch/tool value because the device is assumed
7309 // not to emit separate values for each pointer (isSummed = 1).
7310 int32_t rawX = 100;
7311 int32_t rawY = 200;
7312 int32_t rawX2 = 150;
7313 int32_t rawY2 = 250;
7314 int32_t rawTouchMajor = 5;
7315 int32_t rawToolMajor = 8;
7316
7317 float x = toDisplayX(rawX);
7318 float y = toDisplayY(rawY);
7319 float x2 = toDisplayX(rawX2);
7320 float y2 = toDisplayY(rawY2);
7321 float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
7322 float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
7323 float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
7324
7325 processPosition(mapper, rawX, rawY);
7326 processTouchMajor(mapper, rawTouchMajor);
7327 processToolMajor(mapper, rawToolMajor);
7328 processMTSync(mapper);
7329 processPosition(mapper, rawX2, rawY2);
7330 processTouchMajor(mapper, rawTouchMajor);
7331 processToolMajor(mapper, rawToolMajor);
7332 processMTSync(mapper);
7333 processSync(mapper);
7334
7335 NotifyMotionArgs args;
7336 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7337 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
7338
7339 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7340 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7341 args.action);
7342 ASSERT_EQ(size_t(2), args.pointerCount);
7343 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7344 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
7345 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
7346 x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
7347}
7348
7349TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007350 addConfigurationProperty("touch.deviceType", "touchScreen");
7351 prepareDisplay(DISPLAY_ORIENTATION_0);
7352 prepareAxes(POSITION | TOUCH | TOOL);
7353 addConfigurationProperty("touch.size.calibration", "area");
7354 addConfigurationProperty("touch.size.scale", "43");
7355 addConfigurationProperty("touch.size.bias", "3");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007356 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007357
7358 // These calculations are based on the input device calibration documentation.
7359 int32_t rawX = 100;
7360 int32_t rawY = 200;
7361 int32_t rawTouchMajor = 5;
7362 int32_t rawToolMajor = 8;
7363
7364 float x = toDisplayX(rawX);
7365 float y = toDisplayY(rawY);
7366 float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
7367 float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
7368 float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
7369
7370 processPosition(mapper, rawX, rawY);
7371 processTouchMajor(mapper, rawTouchMajor);
7372 processToolMajor(mapper, rawToolMajor);
7373 processMTSync(mapper);
7374 processSync(mapper);
7375
7376 NotifyMotionArgs args;
7377 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7378 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7379 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
7380}
7381
7382TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007383 addConfigurationProperty("touch.deviceType", "touchScreen");
7384 prepareDisplay(DISPLAY_ORIENTATION_0);
7385 prepareAxes(POSITION | PRESSURE);
7386 addConfigurationProperty("touch.pressure.calibration", "amplitude");
7387 addConfigurationProperty("touch.pressure.scale", "0.01");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007388 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007389
Michael Wrightaa449c92017-12-13 21:21:43 +00007390 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007391 mapper.populateDeviceInfo(&info);
Michael Wrightaa449c92017-12-13 21:21:43 +00007392 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
7393 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TOUCHSCREEN,
7394 0.0f, RAW_PRESSURE_MAX * 0.01, 0.0f, 0.0f));
7395
Michael Wrightd02c5b62014-02-10 15:10:22 -08007396 // These calculations are based on the input device calibration documentation.
7397 int32_t rawX = 100;
7398 int32_t rawY = 200;
7399 int32_t rawPressure = 60;
7400
7401 float x = toDisplayX(rawX);
7402 float y = toDisplayY(rawY);
7403 float pressure = float(rawPressure) * 0.01f;
7404
7405 processPosition(mapper, rawX, rawY);
7406 processPressure(mapper, rawPressure);
7407 processMTSync(mapper);
7408 processSync(mapper);
7409
7410 NotifyMotionArgs args;
7411 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7412 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7413 x, y, pressure, 0, 0, 0, 0, 0, 0, 0));
7414}
7415
7416TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007417 addConfigurationProperty("touch.deviceType", "touchScreen");
7418 prepareDisplay(DISPLAY_ORIENTATION_0);
7419 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007420 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007421
7422 NotifyMotionArgs motionArgs;
7423 NotifyKeyArgs keyArgs;
7424
7425 processId(mapper, 1);
7426 processPosition(mapper, 100, 200);
7427 processSync(mapper);
7428 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7429 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7430 ASSERT_EQ(0, motionArgs.buttonState);
7431
7432 // press BTN_LEFT, release BTN_LEFT
7433 processKey(mapper, BTN_LEFT, 1);
7434 processSync(mapper);
7435 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7436 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7437 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
7438
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007439 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7440 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7441 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
7442
Michael Wrightd02c5b62014-02-10 15:10:22 -08007443 processKey(mapper, BTN_LEFT, 0);
7444 processSync(mapper);
7445 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007446 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007447 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007448
7449 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007450 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007451 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007452
7453 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
7454 processKey(mapper, BTN_RIGHT, 1);
7455 processKey(mapper, BTN_MIDDLE, 1);
7456 processSync(mapper);
7457 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7458 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7459 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
7460 motionArgs.buttonState);
7461
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007462 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7463 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7464 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
7465
7466 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7467 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7468 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
7469 motionArgs.buttonState);
7470
Michael Wrightd02c5b62014-02-10 15:10:22 -08007471 processKey(mapper, BTN_RIGHT, 0);
7472 processSync(mapper);
7473 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007474 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007475 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007476
7477 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007478 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007479 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007480
7481 processKey(mapper, BTN_MIDDLE, 0);
7482 processSync(mapper);
7483 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007484 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007485 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007486
7487 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007488 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007489 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007490
7491 // press BTN_BACK, release BTN_BACK
7492 processKey(mapper, BTN_BACK, 1);
7493 processSync(mapper);
7494 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7495 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
7496 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007497
Michael Wrightd02c5b62014-02-10 15:10:22 -08007498 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007499 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007500 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
7501
7502 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7503 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7504 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007505
7506 processKey(mapper, BTN_BACK, 0);
7507 processSync(mapper);
7508 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007509 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007510 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007511
7512 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007513 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007514 ASSERT_EQ(0, motionArgs.buttonState);
7515
Michael Wrightd02c5b62014-02-10 15:10:22 -08007516 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7517 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
7518 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
7519
7520 // press BTN_SIDE, release BTN_SIDE
7521 processKey(mapper, BTN_SIDE, 1);
7522 processSync(mapper);
7523 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7524 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
7525 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007526
Michael Wrightd02c5b62014-02-10 15:10:22 -08007527 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007528 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007529 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
7530
7531 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7532 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7533 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007534
7535 processKey(mapper, BTN_SIDE, 0);
7536 processSync(mapper);
7537 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007538 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007539 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007540
7541 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007542 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007543 ASSERT_EQ(0, motionArgs.buttonState);
7544
Michael Wrightd02c5b62014-02-10 15:10:22 -08007545 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7546 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
7547 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
7548
7549 // press BTN_FORWARD, release BTN_FORWARD
7550 processKey(mapper, BTN_FORWARD, 1);
7551 processSync(mapper);
7552 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7553 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
7554 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007555
Michael Wrightd02c5b62014-02-10 15:10:22 -08007556 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007557 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007558 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
7559
7560 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7561 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7562 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007563
7564 processKey(mapper, BTN_FORWARD, 0);
7565 processSync(mapper);
7566 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007567 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007568 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007569
7570 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007571 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007572 ASSERT_EQ(0, motionArgs.buttonState);
7573
Michael Wrightd02c5b62014-02-10 15:10:22 -08007574 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7575 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
7576 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
7577
7578 // press BTN_EXTRA, release BTN_EXTRA
7579 processKey(mapper, BTN_EXTRA, 1);
7580 processSync(mapper);
7581 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7582 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
7583 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007584
Michael Wrightd02c5b62014-02-10 15:10:22 -08007585 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007586 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007587 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
7588
7589 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7590 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7591 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007592
7593 processKey(mapper, BTN_EXTRA, 0);
7594 processSync(mapper);
7595 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007596 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007597 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007598
7599 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007600 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007601 ASSERT_EQ(0, motionArgs.buttonState);
7602
Michael Wrightd02c5b62014-02-10 15:10:22 -08007603 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7604 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
7605 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
7606
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007607 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7608
Michael Wrightd02c5b62014-02-10 15:10:22 -08007609 // press BTN_STYLUS, release BTN_STYLUS
7610 processKey(mapper, BTN_STYLUS, 1);
7611 processSync(mapper);
7612 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7613 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007614 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
7615
7616 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7617 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7618 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007619
7620 processKey(mapper, BTN_STYLUS, 0);
7621 processSync(mapper);
7622 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007623 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007624 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007625
7626 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007627 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007628 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007629
7630 // press BTN_STYLUS2, release BTN_STYLUS2
7631 processKey(mapper, BTN_STYLUS2, 1);
7632 processSync(mapper);
7633 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7634 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007635 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
7636
7637 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7638 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7639 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007640
7641 processKey(mapper, BTN_STYLUS2, 0);
7642 processSync(mapper);
7643 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007644 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007645 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007646
7647 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007648 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007649 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007650
7651 // release touch
7652 processId(mapper, -1);
7653 processSync(mapper);
7654 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7655 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7656 ASSERT_EQ(0, motionArgs.buttonState);
7657}
7658
7659TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007660 addConfigurationProperty("touch.deviceType", "touchScreen");
7661 prepareDisplay(DISPLAY_ORIENTATION_0);
7662 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007663 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007664
7665 NotifyMotionArgs motionArgs;
7666
7667 // default tool type is finger
7668 processId(mapper, 1);
7669 processPosition(mapper, 100, 200);
7670 processSync(mapper);
7671 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7672 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7673 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7674
7675 // eraser
7676 processKey(mapper, BTN_TOOL_RUBBER, 1);
7677 processSync(mapper);
7678 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7679 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7680 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
7681
7682 // stylus
7683 processKey(mapper, BTN_TOOL_RUBBER, 0);
7684 processKey(mapper, BTN_TOOL_PEN, 1);
7685 processSync(mapper);
7686 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7687 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7688 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7689
7690 // brush
7691 processKey(mapper, BTN_TOOL_PEN, 0);
7692 processKey(mapper, BTN_TOOL_BRUSH, 1);
7693 processSync(mapper);
7694 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7695 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7696 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7697
7698 // pencil
7699 processKey(mapper, BTN_TOOL_BRUSH, 0);
7700 processKey(mapper, BTN_TOOL_PENCIL, 1);
7701 processSync(mapper);
7702 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7703 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7704 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7705
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08007706 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08007707 processKey(mapper, BTN_TOOL_PENCIL, 0);
7708 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
7709 processSync(mapper);
7710 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7711 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7712 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7713
7714 // mouse
7715 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
7716 processKey(mapper, BTN_TOOL_MOUSE, 1);
7717 processSync(mapper);
7718 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7719 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7720 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
7721
7722 // lens
7723 processKey(mapper, BTN_TOOL_MOUSE, 0);
7724 processKey(mapper, BTN_TOOL_LENS, 1);
7725 processSync(mapper);
7726 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7727 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7728 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
7729
7730 // double-tap
7731 processKey(mapper, BTN_TOOL_LENS, 0);
7732 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
7733 processSync(mapper);
7734 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7735 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7736 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7737
7738 // triple-tap
7739 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
7740 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
7741 processSync(mapper);
7742 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7743 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7744 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7745
7746 // quad-tap
7747 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
7748 processKey(mapper, BTN_TOOL_QUADTAP, 1);
7749 processSync(mapper);
7750 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7751 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7752 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7753
7754 // finger
7755 processKey(mapper, BTN_TOOL_QUADTAP, 0);
7756 processKey(mapper, BTN_TOOL_FINGER, 1);
7757 processSync(mapper);
7758 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7759 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7760 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7761
7762 // stylus trumps finger
7763 processKey(mapper, BTN_TOOL_PEN, 1);
7764 processSync(mapper);
7765 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7766 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7767 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7768
7769 // eraser trumps stylus
7770 processKey(mapper, BTN_TOOL_RUBBER, 1);
7771 processSync(mapper);
7772 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7773 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7774 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
7775
7776 // mouse trumps eraser
7777 processKey(mapper, BTN_TOOL_MOUSE, 1);
7778 processSync(mapper);
7779 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7780 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7781 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
7782
7783 // MT tool type trumps BTN tool types: MT_TOOL_FINGER
7784 processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
7785 processSync(mapper);
7786 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7787 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7788 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7789
7790 // MT tool type trumps BTN tool types: MT_TOOL_PEN
7791 processToolType(mapper, MT_TOOL_PEN);
7792 processSync(mapper);
7793 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7794 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7795 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7796
7797 // back to default tool type
7798 processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
7799 processKey(mapper, BTN_TOOL_MOUSE, 0);
7800 processKey(mapper, BTN_TOOL_RUBBER, 0);
7801 processKey(mapper, BTN_TOOL_PEN, 0);
7802 processKey(mapper, BTN_TOOL_FINGER, 0);
7803 processSync(mapper);
7804 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7805 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7806 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7807}
7808
7809TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007810 addConfigurationProperty("touch.deviceType", "touchScreen");
7811 prepareDisplay(DISPLAY_ORIENTATION_0);
7812 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007813 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007814 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007815
7816 NotifyMotionArgs motionArgs;
7817
7818 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
7819 processId(mapper, 1);
7820 processPosition(mapper, 100, 200);
7821 processSync(mapper);
7822 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7823 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
7824 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7825 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
7826
7827 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7828 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7829 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7830 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
7831
7832 // move a little
7833 processPosition(mapper, 150, 250);
7834 processSync(mapper);
7835 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7836 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7837 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7838 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7839
7840 // down when BTN_TOUCH is pressed, pressure defaults to 1
7841 processKey(mapper, BTN_TOUCH, 1);
7842 processSync(mapper);
7843 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7844 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
7845 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7846 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7847
7848 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7849 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7850 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7851 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
7852
7853 // up when BTN_TOUCH is released, hover restored
7854 processKey(mapper, BTN_TOUCH, 0);
7855 processSync(mapper);
7856 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7857 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7858 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7859 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
7860
7861 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7862 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
7863 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7864 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7865
7866 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7867 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7868 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7869 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7870
7871 // exit hover when pointer goes away
7872 processId(mapper, -1);
7873 processSync(mapper);
7874 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7875 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
7876 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7877 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7878}
7879
7880TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007881 addConfigurationProperty("touch.deviceType", "touchScreen");
7882 prepareDisplay(DISPLAY_ORIENTATION_0);
7883 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007884 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007885
7886 NotifyMotionArgs motionArgs;
7887
7888 // initially hovering because pressure is 0
7889 processId(mapper, 1);
7890 processPosition(mapper, 100, 200);
7891 processPressure(mapper, 0);
7892 processSync(mapper);
7893 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7894 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
7895 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7896 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
7897
7898 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7899 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7900 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7901 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
7902
7903 // move a little
7904 processPosition(mapper, 150, 250);
7905 processSync(mapper);
7906 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7907 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7908 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7909 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7910
7911 // down when pressure becomes non-zero
7912 processPressure(mapper, RAW_PRESSURE_MAX);
7913 processSync(mapper);
7914 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7915 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
7916 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7917 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7918
7919 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7920 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7921 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7922 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
7923
7924 // up when pressure becomes 0, hover restored
7925 processPressure(mapper, 0);
7926 processSync(mapper);
7927 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7928 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7929 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7930 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
7931
7932 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7933 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
7934 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7935 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7936
7937 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7938 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7939 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7940 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7941
7942 // exit hover when pointer goes away
7943 processId(mapper, -1);
7944 processSync(mapper);
7945 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7946 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
7947 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7948 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7949}
7950
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07007951/**
7952 * Set the input device port <--> display port associations, and check that the
7953 * events are routed to the display that matches the display port.
7954 * This can be checked by looking at the displayId of the resulting NotifyMotionArgs.
7955 */
7956TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayPort) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07007957 const std::string usb2 = "USB2";
7958 const uint8_t hdmi1 = 0;
7959 const uint8_t hdmi2 = 1;
7960 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007961 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07007962
7963 addConfigurationProperty("touch.deviceType", "touchScreen");
7964 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007965 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07007966
7967 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
7968 mFakePolicy->addInputPortAssociation(usb2, hdmi2);
7969
7970 // We are intentionally not adding the viewport for display 1 yet. Since the port association
7971 // for this input device is specified, and the matching viewport is not present,
7972 // the input device should be disabled (at the mapper level).
7973
7974 // Add viewport for display 2 on hdmi2
7975 prepareSecondaryDisplay(type, hdmi2);
7976 // Send a touch event
7977 processPosition(mapper, 100, 100);
7978 processSync(mapper);
7979 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7980
7981 // Add viewport for display 1 on hdmi1
7982 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
7983 // Send a touch event again
7984 processPosition(mapper, 100, 100);
7985 processSync(mapper);
7986
7987 NotifyMotionArgs args;
7988 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7989 ASSERT_EQ(DISPLAY_ID, args.displayId);
7990}
Michael Wrightd02c5b62014-02-10 15:10:22 -08007991
Arthur Hungc7ad2d02018-12-18 17:41:29 +08007992TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShouldHandleDisplayId) {
Garfield Tan888a6a42020-01-09 11:39:16 -08007993 // Setup for second display.
Michael Wright17db18e2020-06-26 20:51:44 +01007994 std::shared_ptr<FakePointerController> fakePointerController =
7995 std::make_shared<FakePointerController>();
Garfield Tan888a6a42020-01-09 11:39:16 -08007996 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08007997 fakePointerController->setPosition(100, 200);
7998 fakePointerController->setButtonState(0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00007999 mFakePolicy->setPointerController(fakePointerController);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008000
Garfield Tan888a6a42020-01-09 11:39:16 -08008001 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008002 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08008003
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008004 prepareDisplay(DISPLAY_ORIENTATION_0);
8005 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008006 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008007
8008 // Check source is mouse that would obtain the PointerController.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008009 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008010
8011 NotifyMotionArgs motionArgs;
8012 processPosition(mapper, 100, 100);
8013 processSync(mapper);
8014
8015 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8016 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8017 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
8018}
8019
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00008020/**
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008021 * Ensure that the readTime is set to the SYN_REPORT value when processing touch events.
8022 */
8023TEST_F(MultiTouchInputMapperTest, Process_SendsReadTime) {
8024 addConfigurationProperty("touch.deviceType", "touchScreen");
8025 prepareAxes(POSITION);
8026 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8027
8028 prepareDisplay(DISPLAY_ORIENTATION_0);
8029 process(mapper, 10, 11 /*readTime*/, EV_ABS, ABS_MT_TRACKING_ID, 1);
8030 process(mapper, 15, 16 /*readTime*/, EV_ABS, ABS_MT_POSITION_X, 100);
8031 process(mapper, 20, 21 /*readTime*/, EV_ABS, ABS_MT_POSITION_Y, 100);
8032 process(mapper, 25, 26 /*readTime*/, EV_SYN, SYN_REPORT, 0);
8033
8034 NotifyMotionArgs args;
8035 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8036 ASSERT_EQ(26, args.readTime);
8037
8038 process(mapper, 30, 31 /*readTime*/, EV_ABS, ABS_MT_POSITION_X, 110);
8039 process(mapper, 30, 32 /*readTime*/, EV_ABS, ABS_MT_POSITION_Y, 220);
8040 process(mapper, 30, 33 /*readTime*/, EV_SYN, SYN_REPORT, 0);
8041
8042 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8043 ASSERT_EQ(33, args.readTime);
8044}
8045
8046/**
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00008047 * When the viewport is not active (isActive=false), the touch mapper should be disabled and the
8048 * events should not be delivered to the listener.
8049 */
8050TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreDropped) {
8051 addConfigurationProperty("touch.deviceType", "touchScreen");
8052 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
8053 DISPLAY_ORIENTATION_0, false /*isActive*/, UNIQUE_ID, NO_PORT,
8054 ViewportType::INTERNAL);
8055 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8056 prepareAxes(POSITION);
8057 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8058
8059 NotifyMotionArgs motionArgs;
8060 processPosition(mapper, 100, 100);
8061 processSync(mapper);
8062
8063 mFakeListener->assertNotifyMotionWasNotCalled();
8064}
8065
Garfield Tanc734e4f2021-01-15 20:01:39 -08008066TEST_F(MultiTouchInputMapperTest, Process_DeactivateViewport_AbortTouches) {
8067 addConfigurationProperty("touch.deviceType", "touchScreen");
8068 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
8069 DISPLAY_ORIENTATION_0, true /*isActive*/, UNIQUE_ID, NO_PORT,
8070 ViewportType::INTERNAL);
8071 std::optional<DisplayViewport> optionalDisplayViewport =
8072 mFakePolicy->getDisplayViewportByUniqueId(UNIQUE_ID);
8073 ASSERT_TRUE(optionalDisplayViewport.has_value());
8074 DisplayViewport displayViewport = *optionalDisplayViewport;
8075
8076 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8077 prepareAxes(POSITION);
8078 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8079
8080 // Finger down
8081 int32_t x = 100, y = 100;
8082 processPosition(mapper, x, y);
8083 processSync(mapper);
8084
8085 NotifyMotionArgs motionArgs;
8086 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8087 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8088
8089 // Deactivate display viewport
8090 displayViewport.isActive = false;
8091 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
8092 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8093
8094 // Finger move
8095 x += 10, y += 10;
8096 processPosition(mapper, x, y);
8097 processSync(mapper);
8098
8099 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8100 EXPECT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
8101
8102 // Reactivate display viewport
8103 displayViewport.isActive = true;
8104 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
8105 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8106
8107 // Finger move again
8108 x += 10, y += 10;
8109 processPosition(mapper, x, y);
8110 processSync(mapper);
8111
8112 // Gesture is aborted, so events after display is activated won't be dispatched until there is
8113 // no pointer on the touch device.
8114 mFakeListener->assertNotifyMotionWasNotCalled();
8115}
8116
Arthur Hung7c645402019-01-25 17:45:42 +08008117TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShowTouches) {
8118 // Setup the first touch screen device.
Arthur Hung7c645402019-01-25 17:45:42 +08008119 prepareAxes(POSITION | ID | SLOT);
8120 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008121 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung7c645402019-01-25 17:45:42 +08008122
8123 // Create the second touch screen device, and enable multi fingers.
8124 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08008125 const std::string DEVICE_NAME2 = "TOUCHSCREEN2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08008126 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008127 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08008128 std::shared_ptr<InputDevice> device2 =
8129 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
8130 Flags<InputDeviceClass>(0));
8131
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008132 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
8133 0 /*flat*/, 0 /*fuzz*/);
8134 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
8135 0 /*flat*/, 0 /*fuzz*/);
8136 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX,
8137 0 /*flat*/, 0 /*fuzz*/);
8138 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX,
8139 0 /*flat*/, 0 /*fuzz*/);
8140 mFakeEventHub->setAbsoluteAxisValue(SECOND_EVENTHUB_ID, ABS_MT_SLOT, 0 /*value*/);
8141 mFakeEventHub->addConfigurationProperty(SECOND_EVENTHUB_ID, String8("touch.deviceType"),
8142 String8("touchScreen"));
Arthur Hung7c645402019-01-25 17:45:42 +08008143
8144 // Setup the second touch screen device.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008145 MultiTouchInputMapper& mapper2 = device2->addMapper<MultiTouchInputMapper>(SECOND_EVENTHUB_ID);
Arthur Hung7c645402019-01-25 17:45:42 +08008146 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
8147 device2->reset(ARBITRARY_TIME);
8148
8149 // Setup PointerController.
Michael Wright17db18e2020-06-26 20:51:44 +01008150 std::shared_ptr<FakePointerController> fakePointerController =
8151 std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00008152 mFakePolicy->setPointerController(fakePointerController);
Arthur Hung7c645402019-01-25 17:45:42 +08008153
8154 // Setup policy for associated displays and show touches.
8155 const uint8_t hdmi1 = 0;
8156 const uint8_t hdmi2 = 1;
8157 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
8158 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
8159 mFakePolicy->setShowTouches(true);
8160
8161 // Create displays.
8162 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008163 prepareSecondaryDisplay(ViewportType::EXTERNAL, hdmi2);
Arthur Hung7c645402019-01-25 17:45:42 +08008164
8165 // Default device will reconfigure above, need additional reconfiguration for another device.
8166 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008167 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung7c645402019-01-25 17:45:42 +08008168
8169 // Two fingers down at default display.
8170 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
8171 processPosition(mapper, x1, y1);
8172 processId(mapper, 1);
8173 processSlot(mapper, 1);
8174 processPosition(mapper, x2, y2);
8175 processId(mapper, 2);
8176 processSync(mapper);
8177
8178 std::map<int32_t, std::vector<int32_t>>::const_iterator iter =
8179 fakePointerController->getSpots().find(DISPLAY_ID);
8180 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
8181 ASSERT_EQ(size_t(2), iter->second.size());
8182
8183 // Two fingers down at second display.
8184 processPosition(mapper2, x1, y1);
8185 processId(mapper2, 1);
8186 processSlot(mapper2, 1);
8187 processPosition(mapper2, x2, y2);
8188 processId(mapper2, 2);
8189 processSync(mapper2);
8190
8191 iter = fakePointerController->getSpots().find(SECONDARY_DISPLAY_ID);
8192 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
8193 ASSERT_EQ(size_t(2), iter->second.size());
8194}
8195
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008196TEST_F(MultiTouchInputMapperTest, VideoFrames_ReceivedByListener) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008197 prepareAxes(POSITION);
8198 addConfigurationProperty("touch.deviceType", "touchScreen");
8199 prepareDisplay(DISPLAY_ORIENTATION_0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008200 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008201
8202 NotifyMotionArgs motionArgs;
8203 // Unrotated video frame
8204 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8205 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008206 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008207 processPosition(mapper, 100, 200);
8208 processSync(mapper);
8209 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8210 ASSERT_EQ(frames, motionArgs.videoFrames);
8211
8212 // Subsequent touch events should not have any videoframes
8213 // This is implemented separately in FakeEventHub,
8214 // but that should match the behaviour of TouchVideoDevice.
8215 processPosition(mapper, 200, 200);
8216 processSync(mapper);
8217 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8218 ASSERT_EQ(std::vector<TouchVideoFrame>(), motionArgs.videoFrames);
8219}
8220
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008221TEST_F(MultiTouchInputMapperTest, VideoFrames_AreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008222 prepareAxes(POSITION);
8223 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008224 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008225 // Unrotated video frame
8226 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8227 NotifyMotionArgs motionArgs;
8228
8229 // Test all 4 orientations
8230 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008231 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
8232 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
8233 clearViewports();
8234 prepareDisplay(orientation);
8235 std::vector<TouchVideoFrame> frames{frame};
8236 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
8237 processPosition(mapper, 100, 200);
8238 processSync(mapper);
8239 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8240 ASSERT_EQ(frames, motionArgs.videoFrames);
8241 }
8242}
8243
8244TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_AreRotated) {
8245 prepareAxes(POSITION);
8246 addConfigurationProperty("touch.deviceType", "touchScreen");
8247 // Since InputReader works in the un-rotated coordinate space, only devices that are not
8248 // orientation-aware are affected by display rotation.
8249 addConfigurationProperty("touch.orientationAware", "0");
8250 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8251 // Unrotated video frame
8252 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8253 NotifyMotionArgs motionArgs;
8254
8255 // Test all 4 orientations
8256 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008257 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
8258 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
8259 clearViewports();
8260 prepareDisplay(orientation);
8261 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008262 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008263 processPosition(mapper, 100, 200);
8264 processSync(mapper);
8265 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008266 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
8267 // compared to the display. This is so that when the window transform (which contains the
8268 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
8269 // window's coordinate space.
8270 frames[0].rotate(getInverseRotation(orientation));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008271 ASSERT_EQ(frames, motionArgs.videoFrames);
8272 }
8273}
8274
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008275TEST_F(MultiTouchInputMapperTest, VideoFrames_MultipleFramesAreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008276 prepareAxes(POSITION);
8277 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008278 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008279 // Unrotated video frames. There's no rule that they must all have the same dimensions,
8280 // so mix these.
8281 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8282 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
8283 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
8284 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
8285 NotifyMotionArgs motionArgs;
8286
8287 prepareDisplay(DISPLAY_ORIENTATION_90);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008288 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008289 processPosition(mapper, 100, 200);
8290 processSync(mapper);
8291 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008292 ASSERT_EQ(frames, motionArgs.videoFrames);
8293}
8294
8295TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_MultipleFramesAreRotated) {
8296 prepareAxes(POSITION);
8297 addConfigurationProperty("touch.deviceType", "touchScreen");
8298 // Since InputReader works in the un-rotated coordinate space, only devices that are not
8299 // orientation-aware are affected by display rotation.
8300 addConfigurationProperty("touch.orientationAware", "0");
8301 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8302 // Unrotated video frames. There's no rule that they must all have the same dimensions,
8303 // so mix these.
8304 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8305 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
8306 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
8307 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
8308 NotifyMotionArgs motionArgs;
8309
8310 prepareDisplay(DISPLAY_ORIENTATION_90);
8311 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
8312 processPosition(mapper, 100, 200);
8313 processSync(mapper);
8314 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8315 std::for_each(frames.begin(), frames.end(), [](TouchVideoFrame& frame) {
8316 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
8317 // compared to the display. This is so that when the window transform (which contains the
8318 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
8319 // window's coordinate space.
8320 frame.rotate(getInverseRotation(DISPLAY_ORIENTATION_90));
8321 });
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008322 ASSERT_EQ(frames, motionArgs.videoFrames);
8323}
8324
Arthur Hung9da14732019-09-02 16:16:58 +08008325/**
8326 * If we had defined port associations, but the viewport is not ready, the touch device would be
8327 * expected to be disabled, and it should be enabled after the viewport has found.
8328 */
8329TEST_F(MultiTouchInputMapperTest, Configure_EnabledForAssociatedDisplay) {
Arthur Hung9da14732019-09-02 16:16:58 +08008330 constexpr uint8_t hdmi2 = 1;
8331 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008332 constexpr ViewportType type = ViewportType::EXTERNAL;
Arthur Hung9da14732019-09-02 16:16:58 +08008333
8334 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi2);
8335
8336 addConfigurationProperty("touch.deviceType", "touchScreen");
8337 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008338 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung9da14732019-09-02 16:16:58 +08008339
8340 ASSERT_EQ(mDevice->isEnabled(), false);
8341
8342 // Add display on hdmi2, the device should be enabled and can receive touch event.
8343 prepareSecondaryDisplay(type, hdmi2);
8344 ASSERT_EQ(mDevice->isEnabled(), true);
8345
8346 // Send a touch event.
8347 processPosition(mapper, 100, 100);
8348 processSync(mapper);
8349
8350 NotifyMotionArgs args;
8351 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8352 ASSERT_EQ(SECONDARY_DISPLAY_ID, args.displayId);
8353}
8354
Arthur Hung421eb1c2020-01-16 00:09:42 +08008355TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleSingleTouch) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08008356 addConfigurationProperty("touch.deviceType", "touchScreen");
8357 prepareDisplay(DISPLAY_ORIENTATION_0);
8358 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008359 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08008360
8361 NotifyMotionArgs motionArgs;
8362
8363 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
8364 // finger down
8365 processId(mapper, 1);
8366 processPosition(mapper, x1, y1);
8367 processSync(mapper);
8368 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8369 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8370 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8371
8372 // finger move
8373 processId(mapper, 1);
8374 processPosition(mapper, x2, y2);
8375 processSync(mapper);
8376 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8377 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8378 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8379
8380 // finger up.
8381 processId(mapper, -1);
8382 processSync(mapper);
8383 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8384 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8385 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8386
8387 // new finger down
8388 processId(mapper, 1);
8389 processPosition(mapper, x3, y3);
8390 processSync(mapper);
8391 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8392 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8393 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8394}
8395
8396/**
arthurhungcc7f9802020-04-30 17:55:40 +08008397 * Test single touch should be canceled when received the MT_TOOL_PALM event, and the following
8398 * MOVE and UP events should be ignored.
Arthur Hung421eb1c2020-01-16 00:09:42 +08008399 */
arthurhungcc7f9802020-04-30 17:55:40 +08008400TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_SinglePointer) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08008401 addConfigurationProperty("touch.deviceType", "touchScreen");
8402 prepareDisplay(DISPLAY_ORIENTATION_0);
8403 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008404 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08008405
8406 NotifyMotionArgs motionArgs;
8407
8408 // default tool type is finger
8409 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
arthurhungcc7f9802020-04-30 17:55:40 +08008410 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008411 processPosition(mapper, x1, y1);
8412 processSync(mapper);
8413 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8414 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8415 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8416
8417 // Tool changed to MT_TOOL_PALM expect sending the cancel event.
8418 processToolType(mapper, MT_TOOL_PALM);
8419 processSync(mapper);
8420 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8421 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
8422
8423 // Ignore the following MOVE and UP events if had detect a palm event.
arthurhungcc7f9802020-04-30 17:55:40 +08008424 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008425 processPosition(mapper, x2, y2);
8426 processSync(mapper);
8427 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8428
8429 // finger up.
arthurhungcc7f9802020-04-30 17:55:40 +08008430 processId(mapper, INVALID_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008431 processSync(mapper);
8432 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8433
8434 // new finger down
arthurhungcc7f9802020-04-30 17:55:40 +08008435 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008436 processToolType(mapper, MT_TOOL_FINGER);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008437 processPosition(mapper, x3, y3);
8438 processSync(mapper);
8439 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8440 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8441 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8442}
8443
arthurhungbf89a482020-04-17 17:37:55 +08008444/**
arthurhungcc7f9802020-04-30 17:55:40 +08008445 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
8446 * and the rest active fingers could still be allowed to receive the events
arthurhungbf89a482020-04-17 17:37:55 +08008447 */
arthurhungcc7f9802020-04-30 17:55:40 +08008448TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_TwoPointers) {
arthurhungbf89a482020-04-17 17:37:55 +08008449 addConfigurationProperty("touch.deviceType", "touchScreen");
8450 prepareDisplay(DISPLAY_ORIENTATION_0);
8451 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
8452 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8453
8454 NotifyMotionArgs motionArgs;
8455
8456 // default tool type is finger
arthurhungcc7f9802020-04-30 17:55:40 +08008457 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
8458 processId(mapper, FIRST_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08008459 processPosition(mapper, x1, y1);
8460 processSync(mapper);
8461 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8462 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8463 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8464
8465 // Second finger down.
arthurhungcc7f9802020-04-30 17:55:40 +08008466 processSlot(mapper, SECOND_SLOT);
8467 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08008468 processPosition(mapper, x2, y2);
arthurhungcc7f9802020-04-30 17:55:40 +08008469 processSync(mapper);
8470 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8471 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
8472 motionArgs.action);
8473 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8474
8475 // If the tool type of the first finger changes to MT_TOOL_PALM,
8476 // we expect to receive ACTION_POINTER_UP with cancel flag.
8477 processSlot(mapper, FIRST_SLOT);
8478 processId(mapper, FIRST_TRACKING_ID);
8479 processToolType(mapper, MT_TOOL_PALM);
8480 processSync(mapper);
8481 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8482 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
8483 motionArgs.action);
8484 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
8485
8486 // The following MOVE events of second finger should be processed.
8487 processSlot(mapper, SECOND_SLOT);
8488 processId(mapper, SECOND_TRACKING_ID);
8489 processPosition(mapper, x2 + 1, y2 + 1);
8490 processSync(mapper);
8491 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8492 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8493 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8494
8495 // First finger up. It used to be in palm mode, and we already generated ACTION_POINTER_UP for
8496 // it. Second finger receive move.
8497 processSlot(mapper, FIRST_SLOT);
8498 processId(mapper, INVALID_TRACKING_ID);
8499 processSync(mapper);
8500 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8501 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8502 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8503
8504 // Second finger keeps moving.
8505 processSlot(mapper, SECOND_SLOT);
8506 processId(mapper, SECOND_TRACKING_ID);
8507 processPosition(mapper, x2 + 2, y2 + 2);
8508 processSync(mapper);
8509 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8510 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8511 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8512
8513 // Second finger up.
8514 processId(mapper, INVALID_TRACKING_ID);
8515 processSync(mapper);
8516 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8517 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8518 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
8519}
8520
8521/**
8522 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event, if only 1 finger
8523 * is active, it should send CANCEL after receiving the MT_TOOL_PALM event.
8524 */
8525TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_ShouldCancelWhenAllTouchIsPalm) {
8526 addConfigurationProperty("touch.deviceType", "touchScreen");
8527 prepareDisplay(DISPLAY_ORIENTATION_0);
8528 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
8529 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8530
8531 NotifyMotionArgs motionArgs;
8532
8533 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
8534 // First finger down.
8535 processId(mapper, FIRST_TRACKING_ID);
8536 processPosition(mapper, x1, y1);
8537 processSync(mapper);
8538 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8539 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8540 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8541
8542 // Second finger down.
8543 processSlot(mapper, SECOND_SLOT);
8544 processId(mapper, SECOND_TRACKING_ID);
8545 processPosition(mapper, x2, y2);
arthurhungbf89a482020-04-17 17:37:55 +08008546 processSync(mapper);
8547 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8548 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
8549 motionArgs.action);
8550 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8551
arthurhungcc7f9802020-04-30 17:55:40 +08008552 // If the tool type of the first finger changes to MT_TOOL_PALM,
8553 // we expect to receive ACTION_POINTER_UP with cancel flag.
8554 processSlot(mapper, FIRST_SLOT);
8555 processId(mapper, FIRST_TRACKING_ID);
8556 processToolType(mapper, MT_TOOL_PALM);
8557 processSync(mapper);
8558 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8559 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
8560 motionArgs.action);
8561 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
8562
8563 // Second finger keeps moving.
8564 processSlot(mapper, SECOND_SLOT);
8565 processId(mapper, SECOND_TRACKING_ID);
8566 processPosition(mapper, x2 + 1, y2 + 1);
8567 processSync(mapper);
8568 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8569 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8570
8571 // second finger becomes palm, receive cancel due to only 1 finger is active.
8572 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08008573 processToolType(mapper, MT_TOOL_PALM);
8574 processSync(mapper);
8575 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8576 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
8577
arthurhungcc7f9802020-04-30 17:55:40 +08008578 // third finger down.
8579 processSlot(mapper, THIRD_SLOT);
8580 processId(mapper, THIRD_TRACKING_ID);
8581 processToolType(mapper, MT_TOOL_FINGER);
arthurhungbf89a482020-04-17 17:37:55 +08008582 processPosition(mapper, x3, y3);
8583 processSync(mapper);
arthurhungbf89a482020-04-17 17:37:55 +08008584 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8585 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8586 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08008587 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8588
8589 // third finger move
8590 processId(mapper, THIRD_TRACKING_ID);
8591 processPosition(mapper, x3 + 1, y3 + 1);
8592 processSync(mapper);
8593 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8594 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8595
8596 // first finger up, third finger receive move.
8597 processSlot(mapper, FIRST_SLOT);
8598 processId(mapper, INVALID_TRACKING_ID);
8599 processSync(mapper);
8600 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8601 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8602 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8603
8604 // second finger up, third finger receive move.
8605 processSlot(mapper, SECOND_SLOT);
8606 processId(mapper, INVALID_TRACKING_ID);
8607 processSync(mapper);
8608 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8609 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8610 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8611
8612 // third finger up.
8613 processSlot(mapper, THIRD_SLOT);
8614 processId(mapper, INVALID_TRACKING_ID);
8615 processSync(mapper);
8616 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8617 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8618 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
8619}
8620
8621/**
8622 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
8623 * and the active finger could still be allowed to receive the events
8624 */
8625TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_KeepFirstPointer) {
8626 addConfigurationProperty("touch.deviceType", "touchScreen");
8627 prepareDisplay(DISPLAY_ORIENTATION_0);
8628 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
8629 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8630
8631 NotifyMotionArgs motionArgs;
8632
8633 // default tool type is finger
8634 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
8635 processId(mapper, FIRST_TRACKING_ID);
8636 processPosition(mapper, x1, y1);
8637 processSync(mapper);
8638 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8639 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8640 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8641
8642 // Second finger down.
8643 processSlot(mapper, SECOND_SLOT);
8644 processId(mapper, SECOND_TRACKING_ID);
8645 processPosition(mapper, x2, y2);
8646 processSync(mapper);
8647 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8648 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
8649 motionArgs.action);
8650 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8651
8652 // If the tool type of the second finger changes to MT_TOOL_PALM,
8653 // we expect to receive ACTION_POINTER_UP with cancel flag.
8654 processId(mapper, SECOND_TRACKING_ID);
8655 processToolType(mapper, MT_TOOL_PALM);
8656 processSync(mapper);
8657 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8658 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
8659 motionArgs.action);
8660 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
8661
8662 // The following MOVE event should be processed.
8663 processSlot(mapper, FIRST_SLOT);
8664 processId(mapper, FIRST_TRACKING_ID);
8665 processPosition(mapper, x1 + 1, y1 + 1);
8666 processSync(mapper);
8667 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8668 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8669 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8670
8671 // second finger up.
8672 processSlot(mapper, SECOND_SLOT);
8673 processId(mapper, INVALID_TRACKING_ID);
8674 processSync(mapper);
8675 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8676 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8677
8678 // first finger keep moving
8679 processSlot(mapper, FIRST_SLOT);
8680 processId(mapper, FIRST_TRACKING_ID);
8681 processPosition(mapper, x1 + 2, y1 + 2);
8682 processSync(mapper);
8683 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8684 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8685
8686 // first finger up.
8687 processId(mapper, INVALID_TRACKING_ID);
8688 processSync(mapper);
8689 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8690 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8691 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
arthurhungbf89a482020-04-17 17:37:55 +08008692}
8693
Arthur Hung9ad18942021-06-19 02:04:46 +00008694/**
8695 * Test multi-touch should sent ACTION_POINTER_UP/ACTION_UP when received the INVALID_TRACKING_ID,
8696 * to prevent the driver side may send unexpected data after set tracking id as INVALID_TRACKING_ID
8697 * cause slot be valid again.
8698 */
8699TEST_F(MultiTouchInputMapperTest, Process_MultiTouch_WithInvalidTrackingId) {
8700 addConfigurationProperty("touch.deviceType", "touchScreen");
8701 prepareDisplay(DISPLAY_ORIENTATION_0);
8702 prepareAxes(POSITION | ID | SLOT | PRESSURE);
8703 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8704
8705 NotifyMotionArgs motionArgs;
8706
8707 constexpr int32_t x1 = 100, y1 = 200, x2 = 0, y2 = 0;
8708 // First finger down.
8709 processId(mapper, FIRST_TRACKING_ID);
8710 processPosition(mapper, x1, y1);
8711 processPressure(mapper, RAW_PRESSURE_MAX);
8712 processSync(mapper);
8713 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8714 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8715 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8716
8717 // First finger move.
8718 processId(mapper, FIRST_TRACKING_ID);
8719 processPosition(mapper, x1 + 1, y1 + 1);
8720 processPressure(mapper, RAW_PRESSURE_MAX);
8721 processSync(mapper);
8722 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8723 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8724 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8725
8726 // Second finger down.
8727 processSlot(mapper, SECOND_SLOT);
8728 processId(mapper, SECOND_TRACKING_ID);
8729 processPosition(mapper, x2, y2);
8730 processPressure(mapper, RAW_PRESSURE_MAX);
8731 processSync(mapper);
8732 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8733 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
8734 motionArgs.action);
8735 ASSERT_EQ(uint32_t(2), motionArgs.pointerCount);
8736
8737 // second finger up with some unexpected data.
8738 processSlot(mapper, SECOND_SLOT);
8739 processId(mapper, INVALID_TRACKING_ID);
8740 processPosition(mapper, x2, y2);
8741 processSync(mapper);
8742 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8743 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
8744 motionArgs.action);
8745 ASSERT_EQ(uint32_t(2), motionArgs.pointerCount);
8746
8747 // first finger up with some unexpected data.
8748 processSlot(mapper, FIRST_SLOT);
8749 processId(mapper, INVALID_TRACKING_ID);
8750 processPosition(mapper, x2, y2);
8751 processPressure(mapper, RAW_PRESSURE_MAX);
8752 processSync(mapper);
8753 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8754 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8755 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8756}
8757
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008758// --- MultiTouchInputMapperTest_ExternalDevice ---
8759
8760class MultiTouchInputMapperTest_ExternalDevice : public MultiTouchInputMapperTest {
8761protected:
Chris Yea52ade12020-08-27 16:49:20 -07008762 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008763};
8764
8765/**
8766 * Expect fallback to internal viewport if device is external and external viewport is not present.
8767 */
8768TEST_F(MultiTouchInputMapperTest_ExternalDevice, Viewports_Fallback) {
8769 prepareAxes(POSITION);
8770 addConfigurationProperty("touch.deviceType", "touchScreen");
8771 prepareDisplay(DISPLAY_ORIENTATION_0);
8772 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8773
8774 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
8775
8776 NotifyMotionArgs motionArgs;
8777
8778 // Expect the event to be sent to the internal viewport,
8779 // because an external viewport is not present.
8780 processPosition(mapper, 100, 100);
8781 processSync(mapper);
8782 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8783 ASSERT_EQ(ADISPLAY_ID_DEFAULT, motionArgs.displayId);
8784
8785 // Expect the event to be sent to the external viewport if it is present.
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008786 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008787 processPosition(mapper, 100, 100);
8788 processSync(mapper);
8789 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8790 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
8791}
Arthur Hung4197f6b2020-03-16 15:39:59 +08008792
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08008793TEST_F(MultiTouchInputMapperTest, Process_TouchpadCapture) {
8794 // we need a pointer controller for mouse mode of touchpad (start pointer at 0,0)
8795 std::shared_ptr<FakePointerController> fakePointerController =
8796 std::make_shared<FakePointerController>();
8797 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
8798 fakePointerController->setPosition(0, 0);
8799 fakePointerController->setButtonState(0);
8800
8801 // prepare device and capture
8802 prepareDisplay(DISPLAY_ORIENTATION_0);
8803 prepareAxes(POSITION | ID | SLOT);
8804 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
8805 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
8806 mFakePolicy->setPointerCapture(true);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00008807 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08008808 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8809
8810 // captured touchpad should be a touchpad source
8811 NotifyDeviceResetArgs resetArgs;
8812 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
8813 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
8814
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00008815 InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
Chris Yef74dc422020-09-02 22:41:50 -07008816
8817 const InputDeviceInfo::MotionRange* relRangeX =
8818 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_X, AINPUT_SOURCE_TOUCHPAD);
8819 ASSERT_NE(relRangeX, nullptr);
8820 ASSERT_EQ(relRangeX->min, -(RAW_X_MAX - RAW_X_MIN));
8821 ASSERT_EQ(relRangeX->max, RAW_X_MAX - RAW_X_MIN);
8822 const InputDeviceInfo::MotionRange* relRangeY =
8823 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_Y, AINPUT_SOURCE_TOUCHPAD);
8824 ASSERT_NE(relRangeY, nullptr);
8825 ASSERT_EQ(relRangeY->min, -(RAW_Y_MAX - RAW_Y_MIN));
8826 ASSERT_EQ(relRangeY->max, RAW_Y_MAX - RAW_Y_MIN);
8827
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08008828 // run captured pointer tests - note that this is unscaled, so input listener events should be
8829 // identical to what the hardware sends (accounting for any
8830 // calibration).
8831 // FINGER 0 DOWN
Chris Ye364fdb52020-08-05 15:07:56 -07008832 processSlot(mapper, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08008833 processId(mapper, 1);
8834 processPosition(mapper, 100 + RAW_X_MIN, 100 + RAW_Y_MIN);
8835 processKey(mapper, BTN_TOUCH, 1);
8836 processSync(mapper);
8837
8838 // expect coord[0] to contain initial location of touch 0
8839 NotifyMotionArgs args;
8840 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8841 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
8842 ASSERT_EQ(1U, args.pointerCount);
8843 ASSERT_EQ(0, args.pointerProperties[0].id);
8844 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, args.source);
8845 ASSERT_NO_FATAL_FAILURE(
8846 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
8847
8848 // FINGER 1 DOWN
8849 processSlot(mapper, 1);
8850 processId(mapper, 2);
8851 processPosition(mapper, 560 + RAW_X_MIN, 154 + RAW_Y_MIN);
8852 processSync(mapper);
8853
8854 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
8855 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Chris Ye364fdb52020-08-05 15:07:56 -07008856 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
8857 args.action);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08008858 ASSERT_EQ(2U, args.pointerCount);
8859 ASSERT_EQ(0, args.pointerProperties[0].id);
8860 ASSERT_EQ(1, args.pointerProperties[1].id);
8861 ASSERT_NO_FATAL_FAILURE(
8862 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
8863 ASSERT_NO_FATAL_FAILURE(
8864 assertPointerCoords(args.pointerCoords[1], 560, 154, 1, 0, 0, 0, 0, 0, 0, 0));
8865
8866 // FINGER 1 MOVE
8867 processPosition(mapper, 540 + RAW_X_MIN, 690 + RAW_Y_MIN);
8868 processSync(mapper);
8869
8870 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
8871 // from move
8872 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8873 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
8874 ASSERT_NO_FATAL_FAILURE(
8875 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
8876 ASSERT_NO_FATAL_FAILURE(
8877 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
8878
8879 // FINGER 0 MOVE
8880 processSlot(mapper, 0);
8881 processPosition(mapper, 50 + RAW_X_MIN, 800 + RAW_Y_MIN);
8882 processSync(mapper);
8883
8884 // expect coord[0] to contain new touch 0 location, coord[1] to contain previous location
8885 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8886 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
8887 ASSERT_NO_FATAL_FAILURE(
8888 assertPointerCoords(args.pointerCoords[0], 50, 800, 1, 0, 0, 0, 0, 0, 0, 0));
8889 ASSERT_NO_FATAL_FAILURE(
8890 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
8891
8892 // BUTTON DOWN
8893 processKey(mapper, BTN_LEFT, 1);
8894 processSync(mapper);
8895
8896 // touchinputmapper design sends a move before button press
8897 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8898 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
8899 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8900 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
8901
8902 // BUTTON UP
8903 processKey(mapper, BTN_LEFT, 0);
8904 processSync(mapper);
8905
8906 // touchinputmapper design sends a move after button release
8907 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8908 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
8909 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8910 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
8911
8912 // FINGER 0 UP
8913 processId(mapper, -1);
8914 processSync(mapper);
8915 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8916 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | 0x0000, args.action);
8917
8918 // FINGER 1 MOVE
8919 processSlot(mapper, 1);
8920 processPosition(mapper, 320 + RAW_X_MIN, 900 + RAW_Y_MIN);
8921 processSync(mapper);
8922
8923 // expect coord[0] to contain new location of touch 1, and properties[0].id to contain 1
8924 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8925 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
8926 ASSERT_EQ(1U, args.pointerCount);
8927 ASSERT_EQ(1, args.pointerProperties[0].id);
8928 ASSERT_NO_FATAL_FAILURE(
8929 assertPointerCoords(args.pointerCoords[0], 320, 900, 1, 0, 0, 0, 0, 0, 0, 0));
8930
8931 // FINGER 1 UP
8932 processId(mapper, -1);
8933 processKey(mapper, BTN_TOUCH, 0);
8934 processSync(mapper);
8935 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8936 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
8937
8938 // non captured touchpad should be a mouse source
8939 mFakePolicy->setPointerCapture(false);
8940 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
8941 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
8942 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
8943}
8944
8945TEST_F(MultiTouchInputMapperTest, Process_UnCapturedTouchpadPointer) {
8946 std::shared_ptr<FakePointerController> fakePointerController =
8947 std::make_shared<FakePointerController>();
8948 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
8949 fakePointerController->setPosition(0, 0);
8950 fakePointerController->setButtonState(0);
8951
8952 // prepare device and capture
8953 prepareDisplay(DISPLAY_ORIENTATION_0);
8954 prepareAxes(POSITION | ID | SLOT);
8955 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
8956 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00008957 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08008958 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8959 // run uncaptured pointer tests - pushes out generic events
8960 // FINGER 0 DOWN
8961 processId(mapper, 3);
8962 processPosition(mapper, 100, 100);
8963 processKey(mapper, BTN_TOUCH, 1);
8964 processSync(mapper);
8965
8966 // start at (100,100), cursor should be at (0,0) * scale
8967 NotifyMotionArgs args;
8968 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8969 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
8970 ASSERT_NO_FATAL_FAILURE(
8971 assertPointerCoords(args.pointerCoords[0], 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
8972
8973 // FINGER 0 MOVE
8974 processPosition(mapper, 200, 200);
8975 processSync(mapper);
8976
8977 // compute scaling to help with touch position checking
8978 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
8979 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
8980 float scale =
8981 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
8982
8983 // translate from (100,100) -> (200,200), cursor should have changed to (100,100) * scale)
8984 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8985 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
8986 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 100 * scale, 100 * scale, 0,
8987 0, 0, 0, 0, 0, 0, 0));
8988}
8989
8990TEST_F(MultiTouchInputMapperTest, WhenCapturedAndNotCaptured_GetSources) {
8991 std::shared_ptr<FakePointerController> fakePointerController =
8992 std::make_shared<FakePointerController>();
8993
8994 prepareDisplay(DISPLAY_ORIENTATION_0);
8995 prepareAxes(POSITION | ID | SLOT);
8996 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00008997 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08008998 mFakePolicy->setPointerCapture(false);
8999 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9000
9001 // uncaptured touchpad should be a pointer device
9002 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
9003
9004 // captured touchpad should be a touchpad device
9005 mFakePolicy->setPointerCapture(true);
9006 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
9007 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
9008}
9009
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009010// --- PeripheralControllerTest ---
Chris Yee2b1e5c2021-03-10 22:45:12 -08009011
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009012class PeripheralControllerTest : public testing::Test {
Chris Yee2b1e5c2021-03-10 22:45:12 -08009013protected:
9014 static const char* DEVICE_NAME;
9015 static const char* DEVICE_LOCATION;
9016 static const int32_t DEVICE_ID;
9017 static const int32_t DEVICE_GENERATION;
9018 static const int32_t DEVICE_CONTROLLER_NUMBER;
9019 static const Flags<InputDeviceClass> DEVICE_CLASSES;
9020 static const int32_t EVENTHUB_ID;
9021
9022 std::shared_ptr<FakeEventHub> mFakeEventHub;
9023 sp<FakeInputReaderPolicy> mFakePolicy;
9024 sp<TestInputListener> mFakeListener;
9025 std::unique_ptr<InstrumentedInputReader> mReader;
9026 std::shared_ptr<InputDevice> mDevice;
9027
9028 virtual void SetUp(Flags<InputDeviceClass> classes) {
9029 mFakeEventHub = std::make_unique<FakeEventHub>();
9030 mFakePolicy = new FakeInputReaderPolicy();
9031 mFakeListener = new TestInputListener();
9032 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
9033 mFakeListener);
9034 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
9035 }
9036
9037 void SetUp() override { SetUp(DEVICE_CLASSES); }
9038
9039 void TearDown() override {
9040 mFakeListener.clear();
9041 mFakePolicy.clear();
9042 }
9043
9044 void configureDevice(uint32_t changes) {
9045 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
9046 mReader->requestRefreshConfiguration(changes);
9047 mReader->loopOnce();
9048 }
9049 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
9050 }
9051
9052 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
9053 const std::string& location, int32_t eventHubId,
9054 Flags<InputDeviceClass> classes) {
9055 InputDeviceIdentifier identifier;
9056 identifier.name = name;
9057 identifier.location = location;
9058 std::shared_ptr<InputDevice> device =
9059 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
9060 identifier);
9061 mReader->pushNextDevice(device);
9062 mFakeEventHub->addDevice(eventHubId, name, classes);
9063 mReader->loopOnce();
9064 return device;
9065 }
9066
9067 template <class T, typename... Args>
9068 T& addControllerAndConfigure(Args... args) {
9069 T& controller = mDevice->addController<T>(EVENTHUB_ID, args...);
9070
9071 return controller;
9072 }
9073};
9074
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009075const char* PeripheralControllerTest::DEVICE_NAME = "device";
9076const char* PeripheralControllerTest::DEVICE_LOCATION = "BLUETOOTH";
9077const int32_t PeripheralControllerTest::DEVICE_ID = END_RESERVED_ID + 1000;
9078const int32_t PeripheralControllerTest::DEVICE_GENERATION = 2;
9079const int32_t PeripheralControllerTest::DEVICE_CONTROLLER_NUMBER = 0;
9080const Flags<InputDeviceClass> PeripheralControllerTest::DEVICE_CLASSES =
Chris Yee2b1e5c2021-03-10 22:45:12 -08009081 Flags<InputDeviceClass>(0); // not needed for current tests
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009082const int32_t PeripheralControllerTest::EVENTHUB_ID = 1;
Chris Yee2b1e5c2021-03-10 22:45:12 -08009083
9084// --- BatteryControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009085class BatteryControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -08009086protected:
9087 void SetUp() override {
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009088 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::BATTERY);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009089 }
9090};
9091
9092TEST_F(BatteryControllerTest, GetBatteryCapacity) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009093 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009094
9095 ASSERT_TRUE(controller.getBatteryCapacity(DEFAULT_BATTERY));
9096 ASSERT_EQ(controller.getBatteryCapacity(DEFAULT_BATTERY).value_or(-1), BATTERY_CAPACITY);
9097}
9098
9099TEST_F(BatteryControllerTest, GetBatteryStatus) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009100 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009101
9102 ASSERT_TRUE(controller.getBatteryStatus(DEFAULT_BATTERY));
9103 ASSERT_EQ(controller.getBatteryStatus(DEFAULT_BATTERY).value_or(-1), BATTERY_STATUS);
9104}
9105
9106// --- LightControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009107class LightControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -08009108protected:
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009109 void SetUp() override {
9110 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::LIGHT);
9111 }
Chris Yee2b1e5c2021-03-10 22:45:12 -08009112};
9113
Chris Ye85758332021-05-16 23:05:17 -07009114TEST_F(LightControllerTest, MonoLight) {
9115 RawLightInfo infoMono = {.id = 1,
9116 .name = "Mono",
9117 .maxBrightness = 255,
9118 .flags = InputLightClass::BRIGHTNESS,
9119 .path = ""};
9120 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
Chris Yee2b1e5c2021-03-10 22:45:12 -08009121
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009122 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009123 InputDeviceInfo info;
9124 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009125 std::vector<InputDeviceLightInfo> lights = info.getLights();
9126 ASSERT_EQ(1U, lights.size());
9127 ASSERT_EQ(InputDeviceLightType::MONO, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009128
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009129 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
9130 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009131}
9132
9133TEST_F(LightControllerTest, RGBLight) {
9134 RawLightInfo infoRed = {.id = 1,
9135 .name = "red",
9136 .maxBrightness = 255,
9137 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
9138 .path = ""};
9139 RawLightInfo infoGreen = {.id = 2,
9140 .name = "green",
9141 .maxBrightness = 255,
9142 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
9143 .path = ""};
9144 RawLightInfo infoBlue = {.id = 3,
9145 .name = "blue",
9146 .maxBrightness = 255,
9147 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
9148 .path = ""};
9149 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
9150 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
9151 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
9152
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009153 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009154 InputDeviceInfo info;
9155 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009156 std::vector<InputDeviceLightInfo> lights = info.getLights();
9157 ASSERT_EQ(1U, lights.size());
9158 ASSERT_EQ(InputDeviceLightType::RGB, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009159
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009160 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
9161 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009162}
9163
9164TEST_F(LightControllerTest, MultiColorRGBLight) {
9165 RawLightInfo infoColor = {.id = 1,
9166 .name = "red",
9167 .maxBrightness = 255,
9168 .flags = InputLightClass::BRIGHTNESS |
9169 InputLightClass::MULTI_INTENSITY |
9170 InputLightClass::MULTI_INDEX,
9171 .path = ""};
9172
9173 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
9174
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009175 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009176 InputDeviceInfo info;
9177 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009178 std::vector<InputDeviceLightInfo> lights = info.getLights();
9179 ASSERT_EQ(1U, lights.size());
9180 ASSERT_EQ(InputDeviceLightType::MULTI_COLOR, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009181
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009182 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
9183 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009184}
9185
9186TEST_F(LightControllerTest, PlayerIdLight) {
9187 RawLightInfo info1 = {.id = 1,
9188 .name = "player1",
9189 .maxBrightness = 255,
9190 .flags = InputLightClass::BRIGHTNESS,
9191 .path = ""};
9192 RawLightInfo info2 = {.id = 2,
9193 .name = "player2",
9194 .maxBrightness = 255,
9195 .flags = InputLightClass::BRIGHTNESS,
9196 .path = ""};
9197 RawLightInfo info3 = {.id = 3,
9198 .name = "player3",
9199 .maxBrightness = 255,
9200 .flags = InputLightClass::BRIGHTNESS,
9201 .path = ""};
9202 RawLightInfo info4 = {.id = 4,
9203 .name = "player4",
9204 .maxBrightness = 255,
9205 .flags = InputLightClass::BRIGHTNESS,
9206 .path = ""};
9207 mFakeEventHub->addRawLightInfo(info1.id, std::move(info1));
9208 mFakeEventHub->addRawLightInfo(info2.id, std::move(info2));
9209 mFakeEventHub->addRawLightInfo(info3.id, std::move(info3));
9210 mFakeEventHub->addRawLightInfo(info4.id, std::move(info4));
9211
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009212 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009213 InputDeviceInfo info;
9214 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009215 std::vector<InputDeviceLightInfo> lights = info.getLights();
9216 ASSERT_EQ(1U, lights.size());
9217 ASSERT_EQ(InputDeviceLightType::PLAYER_ID, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009218
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009219 ASSERT_FALSE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
9220 ASSERT_TRUE(controller.setLightPlayerId(lights[0].id, LIGHT_PLAYER_ID));
9221 ASSERT_EQ(controller.getLightPlayerId(lights[0].id).value_or(-1), LIGHT_PLAYER_ID);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009222}
9223
Michael Wrightd02c5b62014-02-10 15:10:22 -08009224} // namespace android