blob: b419d9ab3d4f9a92967e6d2f0204fa875e1c0db7 [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;
Michael Wright17db18e2020-06-26 20:51:44 +0100208 std::unordered_map<int32_t, std::shared_ptr<FakePointerController>> mPointerControllers;
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
253 void addDisplayViewport(int32_t displayId, int32_t width, int32_t height, int32_t orientation,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +0000254 bool isActive, const std::string& uniqueId,
255 std::optional<uint8_t> physicalPort, ViewportType viewportType) {
256 const DisplayViewport viewport =
257 createDisplayViewport(displayId, width, height, orientation, isActive, uniqueId,
258 physicalPort, viewportType);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700259 mViewports.push_back(viewport);
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100260 mConfig.setDisplayViewports(mViewports);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800261 }
262
Arthur Hung6cd19a42019-08-30 19:04:12 +0800263 bool updateViewport(const DisplayViewport& viewport) {
264 size_t count = mViewports.size();
265 for (size_t i = 0; i < count; i++) {
266 const DisplayViewport& currentViewport = mViewports[i];
267 if (currentViewport.displayId == viewport.displayId) {
268 mViewports[i] = viewport;
269 mConfig.setDisplayViewports(mViewports);
270 return true;
271 }
272 }
273 // no viewport found.
274 return false;
275 }
276
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100277 void addExcludedDeviceName(const std::string& deviceName) {
278 mConfig.excludedDeviceNames.push_back(deviceName);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800279 }
280
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700281 void addInputPortAssociation(const std::string& inputPort, uint8_t displayPort) {
282 mConfig.portAssociations.insert({inputPort, displayPort});
283 }
284
Christine Franks1ba71cc2021-04-07 14:37:42 -0700285 void addInputUniqueIdAssociation(const std::string& inputUniqueId,
286 const std::string& displayUniqueId) {
287 mConfig.uniqueIdAssociations.insert({inputUniqueId, displayUniqueId});
288 }
289
Siarhei Vishniakouc6f61192019-07-23 18:12:31 +0000290 void addDisabledDevice(int32_t deviceId) { mConfig.disabledDevices.insert(deviceId); }
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700291
Siarhei Vishniakouc6f61192019-07-23 18:12:31 +0000292 void removeDisabledDevice(int32_t deviceId) { mConfig.disabledDevices.erase(deviceId); }
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700293
Michael Wright17db18e2020-06-26 20:51:44 +0100294 void setPointerController(int32_t deviceId, std::shared_ptr<FakePointerController> controller) {
295 mPointerControllers.insert_or_assign(deviceId, std::move(controller));
Michael Wrightd02c5b62014-02-10 15:10:22 -0800296 }
297
298 const InputReaderConfiguration* getReaderConfiguration() const {
299 return &mConfig;
300 }
301
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800302 const std::vector<InputDeviceInfo>& getInputDevices() const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800303 return mInputDevices;
304 }
305
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100306 TouchAffineTransformation getTouchAffineTransformation(const std::string& inputDeviceDescriptor,
Jason Gerecke71b16e82014-03-10 09:47:59 -0700307 int32_t surfaceRotation) {
Jason Gerecke489fda82012-09-07 17:19:40 -0700308 return transform;
309 }
310
311 void setTouchAffineTransformation(const TouchAffineTransformation t) {
312 transform = t;
Jason Gerecke12d6baa2014-01-27 18:34:20 -0800313 }
314
Prabir Pradhanac483a62021-08-06 14:01:18 +0000315 PointerCaptureRequest setPointerCapture(bool enabled) {
316 mConfig.pointerCaptureRequest = {enabled, mNextPointerCaptureSequenceNumber++};
317 return mConfig.pointerCaptureRequest;
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -0800318 }
319
Arthur Hung7c645402019-01-25 17:45:42 +0800320 void setShowTouches(bool enabled) {
321 mConfig.showTouches = enabled;
322 }
323
Garfield Tan888a6a42020-01-09 11:39:16 -0800324 void setDefaultPointerDisplayId(int32_t pointerDisplayId) {
325 mConfig.defaultPointerDisplayId = pointerDisplayId;
326 }
327
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -0800328 float getPointerGestureMovementSpeedRatio() { return mConfig.pointerGestureMovementSpeedRatio; }
329
Michael Wrightd02c5b62014-02-10 15:10:22 -0800330private:
Prabir Pradhanac483a62021-08-06 14:01:18 +0000331 uint32_t mNextPointerCaptureSequenceNumber = 0;
332
Santos Cordonfa5cf462017-04-05 10:37:00 -0700333 DisplayViewport createDisplayViewport(int32_t displayId, int32_t width, int32_t height,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +0000334 int32_t orientation, bool isActive,
335 const std::string& uniqueId,
336 std::optional<uint8_t> physicalPort, ViewportType type) {
Santos Cordonfa5cf462017-04-05 10:37:00 -0700337 bool isRotated = (orientation == DISPLAY_ORIENTATION_90
338 || orientation == DISPLAY_ORIENTATION_270);
339 DisplayViewport v;
340 v.displayId = displayId;
341 v.orientation = orientation;
342 v.logicalLeft = 0;
343 v.logicalTop = 0;
344 v.logicalRight = isRotated ? height : width;
345 v.logicalBottom = isRotated ? width : height;
346 v.physicalLeft = 0;
347 v.physicalTop = 0;
348 v.physicalRight = isRotated ? height : width;
349 v.physicalBottom = isRotated ? width : height;
350 v.deviceWidth = isRotated ? height : width;
351 v.deviceHeight = isRotated ? width : height;
Siarhei Vishniakou6f778462020-12-09 23:39:07 +0000352 v.isActive = isActive;
Santos Cordonfa5cf462017-04-05 10:37:00 -0700353 v.uniqueId = uniqueId;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700354 v.physicalPort = physicalPort;
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100355 v.type = type;
Santos Cordonfa5cf462017-04-05 10:37:00 -0700356 return v;
357 }
358
Chris Yea52ade12020-08-27 16:49:20 -0700359 void getReaderConfiguration(InputReaderConfiguration* outConfig) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800360 *outConfig = mConfig;
361 }
362
Chris Yea52ade12020-08-27 16:49:20 -0700363 std::shared_ptr<PointerControllerInterface> obtainPointerController(int32_t deviceId) override {
Michael Wright17db18e2020-06-26 20:51:44 +0100364 return mPointerControllers[deviceId];
Michael Wrightd02c5b62014-02-10 15:10:22 -0800365 }
366
Chris Yea52ade12020-08-27 16:49:20 -0700367 void notifyInputDevicesChanged(const std::vector<InputDeviceInfo>& inputDevices) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700368 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800369 mInputDevices = inputDevices;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700370 mInputDevicesChanged = true;
371 mDevicesChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800372 }
373
Chris Yea52ade12020-08-27 16:49:20 -0700374 std::shared_ptr<KeyCharacterMap> getKeyboardLayoutOverlay(
375 const InputDeviceIdentifier&) override {
Yi Kong9b14ac62018-07-17 13:48:38 -0700376 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800377 }
378
Chris Yea52ade12020-08-27 16:49:20 -0700379 std::string getDeviceAlias(const InputDeviceIdentifier&) override { return ""; }
Prabir Pradhan1aed8582019-12-30 11:46:51 -0800380
381 void waitForInputDevices(std::function<void(bool)> processDevicesChanged) {
382 std::unique_lock<std::mutex> lock(mLock);
383 base::ScopedLockAssertion assumeLocked(mLock);
384
385 const bool devicesChanged =
386 mDevicesChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
387 return mInputDevicesChanged;
388 });
389 ASSERT_NO_FATAL_FAILURE(processDevicesChanged(devicesChanged));
390 mInputDevicesChanged = false;
391 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800392};
393
Michael Wrightd02c5b62014-02-10 15:10:22 -0800394// --- FakeEventHub ---
395
396class FakeEventHub : public EventHubInterface {
397 struct KeyInfo {
398 int32_t keyCode;
399 uint32_t flags;
400 };
401
Chris Yef59a2f42020-10-16 12:55:26 -0700402 struct SensorInfo {
403 InputDeviceSensorType sensorType;
404 int32_t sensorDataIndex;
405 };
406
Michael Wrightd02c5b62014-02-10 15:10:22 -0800407 struct Device {
408 InputDeviceIdentifier identifier;
Chris Ye1b0c7342020-07-28 21:57:03 -0700409 Flags<InputDeviceClass> classes;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800410 PropertyMap configuration;
411 KeyedVector<int, RawAbsoluteAxisInfo> absoluteAxes;
412 KeyedVector<int, bool> relativeAxes;
413 KeyedVector<int32_t, int32_t> keyCodeStates;
414 KeyedVector<int32_t, int32_t> scanCodeStates;
415 KeyedVector<int32_t, int32_t> switchStates;
416 KeyedVector<int32_t, int32_t> absoluteAxisValue;
417 KeyedVector<int32_t, KeyInfo> keysByScanCode;
418 KeyedVector<int32_t, KeyInfo> keysByUsageCode;
419 KeyedVector<int32_t, bool> leds;
Chris Yef59a2f42020-10-16 12:55:26 -0700420 std::unordered_map<int32_t, SensorInfo> sensorsByAbsCode;
421 BitArray<MSC_MAX> mscBitmask;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800422 std::vector<VirtualKeyDefinition> virtualKeys;
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700423 bool enabled;
424
425 status_t enable() {
426 enabled = true;
427 return OK;
428 }
429
430 status_t disable() {
431 enabled = false;
432 return OK;
433 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800434
Chris Ye1b0c7342020-07-28 21:57:03 -0700435 explicit Device(Flags<InputDeviceClass> classes) : classes(classes), enabled(true) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800436 };
437
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700438 std::mutex mLock;
439 std::condition_variable mEventsCondition;
440
Michael Wrightd02c5b62014-02-10 15:10:22 -0800441 KeyedVector<int32_t, Device*> mDevices;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100442 std::vector<std::string> mExcludedDevices;
Siarhei Vishniakou370039c2021-02-04 22:09:01 +0000443 std::vector<RawEvent> mEvents GUARDED_BY(mLock);
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600444 std::unordered_map<int32_t /*deviceId*/, std::vector<TouchVideoFrame>> mVideoFrames;
Chris Ye87143712020-11-10 05:05:58 +0000445 std::vector<int32_t> mVibrators = {0, 1};
Chris Ye3fdbfef2021-01-06 18:45:18 -0800446 std::unordered_map<int32_t, RawLightInfo> mRawLightInfos;
447 // Simulates a device light brightness, from light id to light brightness.
448 std::unordered_map<int32_t /* lightId */, int32_t /* brightness*/> mLightBrightness;
449 // Simulates a device light intensities, from light id to light intensities map.
450 std::unordered_map<int32_t /* lightId */, std::unordered_map<LightColor, int32_t>>
451 mLightIntensities;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800452
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700453public:
Michael Wrightd02c5b62014-02-10 15:10:22 -0800454 virtual ~FakeEventHub() {
455 for (size_t i = 0; i < mDevices.size(); i++) {
456 delete mDevices.valueAt(i);
457 }
458 }
459
Michael Wrightd02c5b62014-02-10 15:10:22 -0800460 FakeEventHub() { }
461
Chris Ye1b0c7342020-07-28 21:57:03 -0700462 void addDevice(int32_t deviceId, const std::string& name, Flags<InputDeviceClass> classes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800463 Device* device = new Device(classes);
464 device->identifier.name = name;
465 mDevices.add(deviceId, device);
466
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000467 enqueueEvent(ARBITRARY_TIME, READ_TIME, deviceId, EventHubInterface::DEVICE_ADDED, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800468 }
469
470 void removeDevice(int32_t deviceId) {
471 delete mDevices.valueFor(deviceId);
472 mDevices.removeItem(deviceId);
473
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000474 enqueueEvent(ARBITRARY_TIME, READ_TIME, deviceId, EventHubInterface::DEVICE_REMOVED, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800475 }
476
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700477 bool isDeviceEnabled(int32_t deviceId) {
478 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700479 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700480 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
481 return false;
482 }
483 return device->enabled;
484 }
485
486 status_t enableDevice(int32_t deviceId) {
487 status_t result;
488 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700489 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700490 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
491 return BAD_VALUE;
492 }
493 if (device->enabled) {
494 ALOGW("Duplicate call to %s, device %" PRId32 " already enabled", __func__, deviceId);
495 return OK;
496 }
497 result = device->enable();
498 return result;
499 }
500
501 status_t disableDevice(int32_t deviceId) {
502 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700503 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700504 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
505 return BAD_VALUE;
506 }
507 if (!device->enabled) {
508 ALOGW("Duplicate call to %s, device %" PRId32 " already disabled", __func__, deviceId);
509 return OK;
510 }
511 return device->disable();
512 }
513
Michael Wrightd02c5b62014-02-10 15:10:22 -0800514 void finishDeviceScan() {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000515 enqueueEvent(ARBITRARY_TIME, READ_TIME, 0, EventHubInterface::FINISHED_DEVICE_SCAN, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800516 }
517
518 void addConfigurationProperty(int32_t deviceId, const String8& key, const String8& value) {
519 Device* device = getDevice(deviceId);
520 device->configuration.addProperty(key, value);
521 }
522
523 void addConfigurationMap(int32_t deviceId, const PropertyMap* configuration) {
524 Device* device = getDevice(deviceId);
525 device->configuration.addAll(configuration);
526 }
527
528 void addAbsoluteAxis(int32_t deviceId, int axis,
529 int32_t minValue, int32_t maxValue, int flat, int fuzz, int resolution = 0) {
530 Device* device = getDevice(deviceId);
531
532 RawAbsoluteAxisInfo info;
533 info.valid = true;
534 info.minValue = minValue;
535 info.maxValue = maxValue;
536 info.flat = flat;
537 info.fuzz = fuzz;
538 info.resolution = resolution;
539 device->absoluteAxes.add(axis, info);
540 }
541
542 void addRelativeAxis(int32_t deviceId, int32_t axis) {
543 Device* device = getDevice(deviceId);
544 device->relativeAxes.add(axis, true);
545 }
546
547 void setKeyCodeState(int32_t deviceId, int32_t keyCode, int32_t state) {
548 Device* device = getDevice(deviceId);
549 device->keyCodeStates.replaceValueFor(keyCode, state);
550 }
551
552 void setScanCodeState(int32_t deviceId, int32_t scanCode, int32_t state) {
553 Device* device = getDevice(deviceId);
554 device->scanCodeStates.replaceValueFor(scanCode, state);
555 }
556
557 void setSwitchState(int32_t deviceId, int32_t switchCode, int32_t state) {
558 Device* device = getDevice(deviceId);
559 device->switchStates.replaceValueFor(switchCode, state);
560 }
561
562 void setAbsoluteAxisValue(int32_t deviceId, int32_t axis, int32_t value) {
563 Device* device = getDevice(deviceId);
564 device->absoluteAxisValue.replaceValueFor(axis, value);
565 }
566
567 void addKey(int32_t deviceId, int32_t scanCode, int32_t usageCode,
568 int32_t keyCode, uint32_t flags) {
569 Device* device = getDevice(deviceId);
570 KeyInfo info;
571 info.keyCode = keyCode;
572 info.flags = flags;
573 if (scanCode) {
574 device->keysByScanCode.add(scanCode, info);
575 }
576 if (usageCode) {
577 device->keysByUsageCode.add(usageCode, info);
578 }
579 }
580
581 void addLed(int32_t deviceId, int32_t led, bool initialState) {
582 Device* device = getDevice(deviceId);
583 device->leds.add(led, initialState);
584 }
585
Chris Yef59a2f42020-10-16 12:55:26 -0700586 void addSensorAxis(int32_t deviceId, int32_t absCode, InputDeviceSensorType sensorType,
587 int32_t sensorDataIndex) {
588 Device* device = getDevice(deviceId);
589 SensorInfo info;
590 info.sensorType = sensorType;
591 info.sensorDataIndex = sensorDataIndex;
592 device->sensorsByAbsCode.emplace(absCode, info);
593 }
594
595 void setMscEvent(int32_t deviceId, int32_t mscEvent) {
596 Device* device = getDevice(deviceId);
597 typename BitArray<MSC_MAX>::Buffer buffer;
598 buffer[mscEvent / 32] = 1 << mscEvent % 32;
599 device->mscBitmask.loadFromBuffer(buffer);
600 }
601
Chris Ye3fdbfef2021-01-06 18:45:18 -0800602 void addRawLightInfo(int32_t rawId, RawLightInfo&& info) {
603 mRawLightInfos.emplace(rawId, std::move(info));
604 }
605
606 void fakeLightBrightness(int32_t rawId, int32_t brightness) {
607 mLightBrightness.emplace(rawId, brightness);
608 }
609
610 void fakeLightIntensities(int32_t rawId,
611 const std::unordered_map<LightColor, int32_t> intensities) {
612 mLightIntensities.emplace(rawId, std::move(intensities));
613 }
614
Michael Wrightd02c5b62014-02-10 15:10:22 -0800615 bool getLedState(int32_t deviceId, int32_t led) {
616 Device* device = getDevice(deviceId);
617 return device->leds.valueFor(led);
618 }
619
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100620 std::vector<std::string>& getExcludedDevices() {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800621 return mExcludedDevices;
622 }
623
624 void addVirtualKeyDefinition(int32_t deviceId, const VirtualKeyDefinition& definition) {
625 Device* device = getDevice(deviceId);
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800626 device->virtualKeys.push_back(definition);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800627 }
628
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000629 void enqueueEvent(nsecs_t when, nsecs_t readTime, int32_t deviceId, int32_t type, int32_t code,
630 int32_t value) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700631 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800632 RawEvent event;
633 event.when = when;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000634 event.readTime = readTime;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800635 event.deviceId = deviceId;
636 event.type = type;
637 event.code = code;
638 event.value = value;
639 mEvents.push_back(event);
640
641 if (type == EV_ABS) {
642 setAbsoluteAxisValue(deviceId, code, value);
643 }
644 }
645
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600646 void setVideoFrames(std::unordered_map<int32_t /*deviceId*/,
647 std::vector<TouchVideoFrame>> videoFrames) {
648 mVideoFrames = std::move(videoFrames);
649 }
650
Michael Wrightd02c5b62014-02-10 15:10:22 -0800651 void assertQueueIsEmpty() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700652 std::unique_lock<std::mutex> lock(mLock);
653 base::ScopedLockAssertion assumeLocked(mLock);
654 const bool queueIsEmpty =
655 mEventsCondition.wait_for(lock, WAIT_TIMEOUT,
656 [this]() REQUIRES(mLock) { return mEvents.size() == 0; });
657 if (!queueIsEmpty) {
658 FAIL() << "Timed out waiting for EventHub queue to be emptied.";
659 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800660 }
661
662private:
663 Device* getDevice(int32_t deviceId) const {
664 ssize_t index = mDevices.indexOfKey(deviceId);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100665 return index >= 0 ? mDevices.valueAt(index) : nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800666 }
667
Chris Yea52ade12020-08-27 16:49:20 -0700668 Flags<InputDeviceClass> getDeviceClasses(int32_t deviceId) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800669 Device* device = getDevice(deviceId);
Chris Ye1b0c7342020-07-28 21:57:03 -0700670 return device ? device->classes : Flags<InputDeviceClass>(0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800671 }
672
Chris Yea52ade12020-08-27 16:49:20 -0700673 InputDeviceIdentifier getDeviceIdentifier(int32_t deviceId) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800674 Device* device = getDevice(deviceId);
675 return device ? device->identifier : InputDeviceIdentifier();
676 }
677
Chris Yea52ade12020-08-27 16:49:20 -0700678 int32_t getDeviceControllerNumber(int32_t) const override { return 0; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800679
Chris Yea52ade12020-08-27 16:49:20 -0700680 void getConfiguration(int32_t deviceId, PropertyMap* outConfiguration) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800681 Device* device = getDevice(deviceId);
682 if (device) {
683 *outConfiguration = device->configuration;
684 }
685 }
686
Chris Yea52ade12020-08-27 16:49:20 -0700687 status_t getAbsoluteAxisInfo(int32_t deviceId, int axis,
688 RawAbsoluteAxisInfo* outAxisInfo) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800689 Device* device = getDevice(deviceId);
Arthur Hung9da14732019-09-02 16:16:58 +0800690 if (device && device->enabled) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800691 ssize_t index = device->absoluteAxes.indexOfKey(axis);
692 if (index >= 0) {
693 *outAxisInfo = device->absoluteAxes.valueAt(index);
694 return OK;
695 }
696 }
697 outAxisInfo->clear();
698 return -1;
699 }
700
Chris Yea52ade12020-08-27 16:49:20 -0700701 bool hasRelativeAxis(int32_t deviceId, int axis) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800702 Device* device = getDevice(deviceId);
703 if (device) {
704 return device->relativeAxes.indexOfKey(axis) >= 0;
705 }
706 return false;
707 }
708
Chris Yea52ade12020-08-27 16:49:20 -0700709 bool hasInputProperty(int32_t, int) const override { return false; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800710
Chris Yef59a2f42020-10-16 12:55:26 -0700711 bool hasMscEvent(int32_t deviceId, int mscEvent) const override final {
712 Device* device = getDevice(deviceId);
713 if (device) {
714 return mscEvent >= 0 && mscEvent <= MSC_MAX ? device->mscBitmask.test(mscEvent) : false;
715 }
716 return false;
717 }
718
Chris Yea52ade12020-08-27 16:49:20 -0700719 status_t mapKey(int32_t deviceId, int32_t scanCode, int32_t usageCode, int32_t metaState,
720 int32_t* outKeycode, int32_t* outMetaState, uint32_t* outFlags) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800721 Device* device = getDevice(deviceId);
722 if (device) {
723 const KeyInfo* key = getKey(device, scanCode, usageCode);
724 if (key) {
725 if (outKeycode) {
726 *outKeycode = key->keyCode;
727 }
728 if (outFlags) {
729 *outFlags = key->flags;
730 }
Dmitry Torokhov0faaa0b2015-09-24 13:13:55 -0700731 if (outMetaState) {
732 *outMetaState = metaState;
733 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800734 return OK;
735 }
736 }
737 return NAME_NOT_FOUND;
738 }
739
740 const KeyInfo* getKey(Device* device, int32_t scanCode, int32_t usageCode) const {
741 if (usageCode) {
742 ssize_t index = device->keysByUsageCode.indexOfKey(usageCode);
743 if (index >= 0) {
744 return &device->keysByUsageCode.valueAt(index);
745 }
746 }
747 if (scanCode) {
748 ssize_t index = device->keysByScanCode.indexOfKey(scanCode);
749 if (index >= 0) {
750 return &device->keysByScanCode.valueAt(index);
751 }
752 }
Yi Kong9b14ac62018-07-17 13:48:38 -0700753 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800754 }
755
Chris Yea52ade12020-08-27 16:49:20 -0700756 status_t mapAxis(int32_t, int32_t, AxisInfo*) const override { return NAME_NOT_FOUND; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800757
Chris Yef59a2f42020-10-16 12:55:26 -0700758 base::Result<std::pair<InputDeviceSensorType, int32_t>> mapSensor(int32_t deviceId,
759 int32_t absCode) {
760 Device* device = getDevice(deviceId);
761 if (!device) {
762 return Errorf("Sensor device not found.");
763 }
764 auto it = device->sensorsByAbsCode.find(absCode);
765 if (it == device->sensorsByAbsCode.end()) {
766 return Errorf("Sensor map not found.");
767 }
768 const SensorInfo& info = it->second;
769 return std::make_pair(info.sensorType, info.sensorDataIndex);
770 }
771
Chris Yea52ade12020-08-27 16:49:20 -0700772 void setExcludedDevices(const std::vector<std::string>& devices) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800773 mExcludedDevices = devices;
774 }
775
Siarhei Vishniakou370039c2021-02-04 22:09:01 +0000776 size_t getEvents(int, RawEvent* buffer, size_t bufferSize) override {
777 std::scoped_lock lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800778
Siarhei Vishniakou370039c2021-02-04 22:09:01 +0000779 const size_t filledSize = std::min(mEvents.size(), bufferSize);
780 std::copy(mEvents.begin(), mEvents.begin() + filledSize, buffer);
781
782 mEvents.erase(mEvents.begin(), mEvents.begin() + filledSize);
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700783 mEventsCondition.notify_all();
Siarhei Vishniakou370039c2021-02-04 22:09:01 +0000784 return filledSize;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800785 }
786
Chris Yea52ade12020-08-27 16:49:20 -0700787 std::vector<TouchVideoFrame> getVideoFrames(int32_t deviceId) override {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600788 auto it = mVideoFrames.find(deviceId);
789 if (it != mVideoFrames.end()) {
790 std::vector<TouchVideoFrame> frames = std::move(it->second);
791 mVideoFrames.erase(deviceId);
792 return frames;
793 }
Siarhei Vishniakouadd89292018-12-13 19:23:36 -0800794 return {};
795 }
796
Chris Yea52ade12020-08-27 16:49:20 -0700797 int32_t getScanCodeState(int32_t deviceId, int32_t scanCode) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800798 Device* device = getDevice(deviceId);
799 if (device) {
800 ssize_t index = device->scanCodeStates.indexOfKey(scanCode);
801 if (index >= 0) {
802 return device->scanCodeStates.valueAt(index);
803 }
804 }
805 return AKEY_STATE_UNKNOWN;
806 }
807
Chris Yea52ade12020-08-27 16:49:20 -0700808 int32_t getKeyCodeState(int32_t deviceId, int32_t keyCode) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800809 Device* device = getDevice(deviceId);
810 if (device) {
811 ssize_t index = device->keyCodeStates.indexOfKey(keyCode);
812 if (index >= 0) {
813 return device->keyCodeStates.valueAt(index);
814 }
815 }
816 return AKEY_STATE_UNKNOWN;
817 }
818
Chris Yea52ade12020-08-27 16:49:20 -0700819 int32_t getSwitchState(int32_t deviceId, int32_t sw) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800820 Device* device = getDevice(deviceId);
821 if (device) {
822 ssize_t index = device->switchStates.indexOfKey(sw);
823 if (index >= 0) {
824 return device->switchStates.valueAt(index);
825 }
826 }
827 return AKEY_STATE_UNKNOWN;
828 }
829
Chris Yea52ade12020-08-27 16:49:20 -0700830 status_t getAbsoluteAxisValue(int32_t deviceId, int32_t axis,
831 int32_t* outValue) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800832 Device* device = getDevice(deviceId);
833 if (device) {
834 ssize_t index = device->absoluteAxisValue.indexOfKey(axis);
835 if (index >= 0) {
836 *outValue = device->absoluteAxisValue.valueAt(index);
837 return OK;
838 }
839 }
840 *outValue = 0;
841 return -1;
842 }
843
Chris Yea52ade12020-08-27 16:49:20 -0700844 // Return true if the device has non-empty key layout.
845 bool markSupportedKeyCodes(int32_t deviceId, size_t numCodes, const int32_t* keyCodes,
846 uint8_t* outFlags) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800847 bool result = false;
848 Device* device = getDevice(deviceId);
849 if (device) {
Chris Yea52ade12020-08-27 16:49:20 -0700850 result = device->keysByScanCode.size() > 0 || device->keysByUsageCode.size() > 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800851 for (size_t i = 0; i < numCodes; i++) {
852 for (size_t j = 0; j < device->keysByScanCode.size(); j++) {
853 if (keyCodes[i] == device->keysByScanCode.valueAt(j).keyCode) {
854 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800855 }
856 }
857 for (size_t j = 0; j < device->keysByUsageCode.size(); j++) {
858 if (keyCodes[i] == device->keysByUsageCode.valueAt(j).keyCode) {
859 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800860 }
861 }
862 }
863 }
864 return result;
865 }
866
Chris Yea52ade12020-08-27 16:49:20 -0700867 bool hasScanCode(int32_t deviceId, int32_t scanCode) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800868 Device* device = getDevice(deviceId);
869 if (device) {
870 ssize_t index = device->keysByScanCode.indexOfKey(scanCode);
871 return index >= 0;
872 }
873 return false;
874 }
875
Chris Yea52ade12020-08-27 16:49:20 -0700876 bool hasLed(int32_t deviceId, int32_t led) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800877 Device* device = getDevice(deviceId);
878 return device && device->leds.indexOfKey(led) >= 0;
879 }
880
Chris Yea52ade12020-08-27 16:49:20 -0700881 void setLedState(int32_t deviceId, int32_t led, bool on) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800882 Device* device = getDevice(deviceId);
883 if (device) {
884 ssize_t index = device->leds.indexOfKey(led);
885 if (index >= 0) {
886 device->leds.replaceValueAt(led, on);
887 } else {
888 ADD_FAILURE()
889 << "Attempted to set the state of an LED that the EventHub declared "
890 "was not present. led=" << led;
891 }
892 }
893 }
894
Chris Yea52ade12020-08-27 16:49:20 -0700895 void getVirtualKeyDefinitions(
896 int32_t deviceId, std::vector<VirtualKeyDefinition>& outVirtualKeys) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800897 outVirtualKeys.clear();
898
899 Device* device = getDevice(deviceId);
900 if (device) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800901 outVirtualKeys = device->virtualKeys;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800902 }
903 }
904
Chris Yea52ade12020-08-27 16:49:20 -0700905 const std::shared_ptr<KeyCharacterMap> getKeyCharacterMap(int32_t) const override {
Yi Kong9b14ac62018-07-17 13:48:38 -0700906 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800907 }
908
Chris Yea52ade12020-08-27 16:49:20 -0700909 bool setKeyboardLayoutOverlay(int32_t, std::shared_ptr<KeyCharacterMap>) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800910 return false;
911 }
912
Chris Yea52ade12020-08-27 16:49:20 -0700913 void vibrate(int32_t, const VibrationElement&) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800914
Chris Yea52ade12020-08-27 16:49:20 -0700915 void cancelVibrate(int32_t) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800916
Chris Ye87143712020-11-10 05:05:58 +0000917 std::vector<int32_t> getVibratorIds(int32_t deviceId) override { return mVibrators; };
918
Chris Yee2b1e5c2021-03-10 22:45:12 -0800919 std::optional<int32_t> getBatteryCapacity(int32_t, int32_t) const override {
920 return BATTERY_CAPACITY;
921 }
Kim Low03ea0352020-11-06 12:45:07 -0800922
Chris Yee2b1e5c2021-03-10 22:45:12 -0800923 std::optional<int32_t> getBatteryStatus(int32_t, int32_t) const override {
924 return BATTERY_STATUS;
925 }
926
927 const std::vector<int32_t> getRawBatteryIds(int32_t deviceId) { return {}; }
928
929 std::optional<RawBatteryInfo> getRawBatteryInfo(int32_t deviceId, int32_t batteryId) {
930 return std::nullopt;
931 }
Kim Low03ea0352020-11-06 12:45:07 -0800932
Chris Ye3fdbfef2021-01-06 18:45:18 -0800933 const std::vector<int32_t> getRawLightIds(int32_t deviceId) override {
934 std::vector<int32_t> ids;
935 for (const auto& [rawId, info] : mRawLightInfos) {
936 ids.push_back(rawId);
937 }
938 return ids;
939 }
940
941 std::optional<RawLightInfo> getRawLightInfo(int32_t deviceId, int32_t lightId) override {
942 auto it = mRawLightInfos.find(lightId);
943 if (it == mRawLightInfos.end()) {
944 return std::nullopt;
945 }
946 return it->second;
947 }
948
949 void setLightBrightness(int32_t deviceId, int32_t lightId, int32_t brightness) override {
950 mLightBrightness.emplace(lightId, brightness);
951 }
952
953 void setLightIntensities(int32_t deviceId, int32_t lightId,
954 std::unordered_map<LightColor, int32_t> intensities) override {
955 mLightIntensities.emplace(lightId, intensities);
956 };
957
958 std::optional<int32_t> getLightBrightness(int32_t deviceId, int32_t lightId) override {
959 auto lightIt = mLightBrightness.find(lightId);
960 if (lightIt == mLightBrightness.end()) {
961 return std::nullopt;
962 }
963 return lightIt->second;
964 }
965
966 std::optional<std::unordered_map<LightColor, int32_t>> getLightIntensities(
967 int32_t deviceId, int32_t lightId) override {
968 auto lightIt = mLightIntensities.find(lightId);
969 if (lightIt == mLightIntensities.end()) {
970 return std::nullopt;
971 }
972 return lightIt->second;
973 };
974
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100975 virtual bool isExternal(int32_t) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800976 return false;
977 }
978
Chris Yea52ade12020-08-27 16:49:20 -0700979 void dump(std::string&) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800980
Chris Yea52ade12020-08-27 16:49:20 -0700981 void monitor() override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800982
Chris Yea52ade12020-08-27 16:49:20 -0700983 void requestReopenDevices() override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800984
Chris Yea52ade12020-08-27 16:49:20 -0700985 void wake() override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800986};
987
Michael Wrightd02c5b62014-02-10 15:10:22 -0800988// --- FakeInputMapper ---
989
990class FakeInputMapper : public InputMapper {
991 uint32_t mSources;
992 int32_t mKeyboardType;
993 int32_t mMetaState;
994 KeyedVector<int32_t, int32_t> mKeyCodeStates;
995 KeyedVector<int32_t, int32_t> mScanCodeStates;
996 KeyedVector<int32_t, int32_t> mSwitchStates;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800997 std::vector<int32_t> mSupportedKeyCodes;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800998
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700999 std::mutex mLock;
1000 std::condition_variable mStateChangedCondition;
1001 bool mConfigureWasCalled GUARDED_BY(mLock);
1002 bool mResetWasCalled GUARDED_BY(mLock);
1003 bool mProcessWasCalled GUARDED_BY(mLock);
1004 RawEvent mLastEvent GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001005
Arthur Hungc23540e2018-11-29 20:42:11 +08001006 std::optional<DisplayViewport> mViewport;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001007public:
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001008 FakeInputMapper(InputDeviceContext& deviceContext, uint32_t sources)
1009 : InputMapper(deviceContext),
1010 mSources(sources),
1011 mKeyboardType(AINPUT_KEYBOARD_TYPE_NONE),
Michael Wrightd02c5b62014-02-10 15:10:22 -08001012 mMetaState(0),
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001013 mConfigureWasCalled(false),
1014 mResetWasCalled(false),
1015 mProcessWasCalled(false) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001016
Chris Yea52ade12020-08-27 16:49:20 -07001017 virtual ~FakeInputMapper() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001018
1019 void setKeyboardType(int32_t keyboardType) {
1020 mKeyboardType = keyboardType;
1021 }
1022
1023 void setMetaState(int32_t metaState) {
1024 mMetaState = metaState;
1025 }
1026
1027 void assertConfigureWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001028 std::unique_lock<std::mutex> lock(mLock);
1029 base::ScopedLockAssertion assumeLocked(mLock);
1030 const bool configureCalled =
1031 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1032 return mConfigureWasCalled;
1033 });
1034 if (!configureCalled) {
1035 FAIL() << "Expected configure() to have been called.";
1036 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001037 mConfigureWasCalled = false;
1038 }
1039
1040 void assertResetWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001041 std::unique_lock<std::mutex> lock(mLock);
1042 base::ScopedLockAssertion assumeLocked(mLock);
1043 const bool resetCalled =
1044 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1045 return mResetWasCalled;
1046 });
1047 if (!resetCalled) {
1048 FAIL() << "Expected reset() to have been called.";
1049 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001050 mResetWasCalled = false;
1051 }
1052
Yi Kong9b14ac62018-07-17 13:48:38 -07001053 void assertProcessWasCalled(RawEvent* outLastEvent = nullptr) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001054 std::unique_lock<std::mutex> lock(mLock);
1055 base::ScopedLockAssertion assumeLocked(mLock);
1056 const bool processCalled =
1057 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1058 return mProcessWasCalled;
1059 });
1060 if (!processCalled) {
1061 FAIL() << "Expected process() to have been called.";
1062 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001063 if (outLastEvent) {
1064 *outLastEvent = mLastEvent;
1065 }
1066 mProcessWasCalled = false;
1067 }
1068
1069 void setKeyCodeState(int32_t keyCode, int32_t state) {
1070 mKeyCodeStates.replaceValueFor(keyCode, state);
1071 }
1072
1073 void setScanCodeState(int32_t scanCode, int32_t state) {
1074 mScanCodeStates.replaceValueFor(scanCode, state);
1075 }
1076
1077 void setSwitchState(int32_t switchCode, int32_t state) {
1078 mSwitchStates.replaceValueFor(switchCode, state);
1079 }
1080
1081 void addSupportedKeyCode(int32_t keyCode) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001082 mSupportedKeyCodes.push_back(keyCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001083 }
1084
1085private:
Chris Yea52ade12020-08-27 16:49:20 -07001086 uint32_t getSources() override { return mSources; }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001087
Chris Yea52ade12020-08-27 16:49:20 -07001088 void populateDeviceInfo(InputDeviceInfo* deviceInfo) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001089 InputMapper::populateDeviceInfo(deviceInfo);
1090
1091 if (mKeyboardType != AINPUT_KEYBOARD_TYPE_NONE) {
1092 deviceInfo->setKeyboardType(mKeyboardType);
1093 }
1094 }
1095
Chris Yea52ade12020-08-27 16:49:20 -07001096 void configure(nsecs_t, const InputReaderConfiguration* config, uint32_t changes) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001097 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001098 mConfigureWasCalled = true;
Arthur Hungc23540e2018-11-29 20:42:11 +08001099
1100 // Find the associated viewport if exist.
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001101 const std::optional<uint8_t> displayPort = getDeviceContext().getAssociatedDisplayPort();
Arthur Hungc23540e2018-11-29 20:42:11 +08001102 if (displayPort && (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
1103 mViewport = config->getDisplayViewportByPort(*displayPort);
1104 }
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001105
1106 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001107 }
1108
Chris Yea52ade12020-08-27 16:49:20 -07001109 void reset(nsecs_t) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001110 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001111 mResetWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001112 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001113 }
1114
Chris Yea52ade12020-08-27 16:49:20 -07001115 void process(const RawEvent* rawEvent) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001116 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001117 mLastEvent = *rawEvent;
1118 mProcessWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001119 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001120 }
1121
Chris Yea52ade12020-08-27 16:49:20 -07001122 int32_t getKeyCodeState(uint32_t, int32_t keyCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001123 ssize_t index = mKeyCodeStates.indexOfKey(keyCode);
1124 return index >= 0 ? mKeyCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1125 }
1126
Chris Yea52ade12020-08-27 16:49:20 -07001127 int32_t getScanCodeState(uint32_t, int32_t scanCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001128 ssize_t index = mScanCodeStates.indexOfKey(scanCode);
1129 return index >= 0 ? mScanCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1130 }
1131
Chris Yea52ade12020-08-27 16:49:20 -07001132 int32_t getSwitchState(uint32_t, int32_t switchCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001133 ssize_t index = mSwitchStates.indexOfKey(switchCode);
1134 return index >= 0 ? mSwitchStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1135 }
1136
Chris Yea52ade12020-08-27 16:49:20 -07001137 // Return true if the device has non-empty key layout.
1138 bool markSupportedKeyCodes(uint32_t, size_t numCodes, const int32_t* keyCodes,
1139 uint8_t* outFlags) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001140 for (size_t i = 0; i < numCodes; i++) {
1141 for (size_t j = 0; j < mSupportedKeyCodes.size(); j++) {
1142 if (keyCodes[i] == mSupportedKeyCodes[j]) {
1143 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001144 }
1145 }
1146 }
Chris Yea52ade12020-08-27 16:49:20 -07001147 bool result = mSupportedKeyCodes.size() > 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001148 return result;
1149 }
1150
1151 virtual int32_t getMetaState() {
1152 return mMetaState;
1153 }
1154
1155 virtual void fadePointer() {
1156 }
Arthur Hungc23540e2018-11-29 20:42:11 +08001157
1158 virtual std::optional<int32_t> getAssociatedDisplay() {
1159 if (mViewport) {
1160 return std::make_optional(mViewport->displayId);
1161 }
1162 return std::nullopt;
1163 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001164};
1165
1166
1167// --- InstrumentedInputReader ---
1168
1169class InstrumentedInputReader : public InputReader {
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001170 std::queue<std::shared_ptr<InputDevice>> mNextDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001171
1172public:
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001173 InstrumentedInputReader(std::shared_ptr<EventHubInterface> eventHub,
1174 const sp<InputReaderPolicyInterface>& policy,
1175 const sp<InputListenerInterface>& listener)
arthurhungdcef2dc2020-08-11 14:47:50 +08001176 : InputReader(eventHub, policy, listener), mFakeContext(this) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001177
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001178 virtual ~InstrumentedInputReader() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001179
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001180 void pushNextDevice(std::shared_ptr<InputDevice> device) { mNextDevices.push(device); }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001181
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001182 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001183 const std::string& location = "") {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001184 InputDeviceIdentifier identifier;
1185 identifier.name = name;
Arthur Hungc23540e2018-11-29 20:42:11 +08001186 identifier.location = location;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001187 int32_t generation = deviceId + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08001188 return std::make_shared<InputDevice>(&mFakeContext, deviceId, generation, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001189 }
1190
Prabir Pradhan28efc192019-11-05 01:10:04 +00001191 // Make the protected loopOnce method accessible to tests.
1192 using InputReader::loopOnce;
1193
Michael Wrightd02c5b62014-02-10 15:10:22 -08001194protected:
Chris Ye1c2e0892020-11-30 21:41:44 -08001195 virtual std::shared_ptr<InputDevice> createDeviceLocked(int32_t eventHubId,
1196 const InputDeviceIdentifier& identifier)
1197 REQUIRES(mLock) {
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001198 if (!mNextDevices.empty()) {
1199 std::shared_ptr<InputDevice> device(std::move(mNextDevices.front()));
1200 mNextDevices.pop();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001201 return device;
1202 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001203 return InputReader::createDeviceLocked(eventHubId, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001204 }
1205
arthurhungdcef2dc2020-08-11 14:47:50 +08001206 // --- FakeInputReaderContext ---
1207 class FakeInputReaderContext : public ContextImpl {
1208 int32_t mGlobalMetaState;
1209 bool mUpdateGlobalMetaStateWasCalled;
1210 int32_t mGeneration;
1211
1212 public:
1213 FakeInputReaderContext(InputReader* reader)
1214 : ContextImpl(reader),
1215 mGlobalMetaState(0),
1216 mUpdateGlobalMetaStateWasCalled(false),
1217 mGeneration(1) {}
1218
1219 virtual ~FakeInputReaderContext() {}
1220
1221 void assertUpdateGlobalMetaStateWasCalled() {
1222 ASSERT_TRUE(mUpdateGlobalMetaStateWasCalled)
1223 << "Expected updateGlobalMetaState() to have been called.";
1224 mUpdateGlobalMetaStateWasCalled = false;
1225 }
1226
1227 void setGlobalMetaState(int32_t state) { mGlobalMetaState = state; }
1228
1229 uint32_t getGeneration() { return mGeneration; }
1230
1231 void updateGlobalMetaState() override {
1232 mUpdateGlobalMetaStateWasCalled = true;
1233 ContextImpl::updateGlobalMetaState();
1234 }
1235
1236 int32_t getGlobalMetaState() override {
1237 return mGlobalMetaState | ContextImpl::getGlobalMetaState();
1238 }
1239
1240 int32_t bumpGeneration() override {
1241 mGeneration = ContextImpl::bumpGeneration();
1242 return mGeneration;
1243 }
1244 } mFakeContext;
1245
Michael Wrightd02c5b62014-02-10 15:10:22 -08001246 friend class InputReaderTest;
arthurhungdcef2dc2020-08-11 14:47:50 +08001247
1248public:
1249 FakeInputReaderContext* getContext() { return &mFakeContext; }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001250};
1251
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001252// --- InputReaderPolicyTest ---
1253class InputReaderPolicyTest : public testing::Test {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001254protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001255 sp<FakeInputReaderPolicy> mFakePolicy;
1256
Chris Yea52ade12020-08-27 16:49:20 -07001257 void SetUp() override { mFakePolicy = new FakeInputReaderPolicy(); }
1258 void TearDown() override { mFakePolicy.clear(); }
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001259};
1260
1261/**
1262 * Check that empty set of viewports is an acceptable configuration.
1263 * Also try to get internal viewport two different ways - by type and by uniqueId.
1264 *
1265 * There will be confusion if two viewports with empty uniqueId and identical type are present.
1266 * Such configuration is not currently allowed.
1267 */
1268TEST_F(InputReaderPolicyTest, Viewports_GetCleared) {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001269 static const std::string uniqueId = "local:0";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001270
1271 // We didn't add any viewports yet, so there shouldn't be any.
1272 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001273 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001274 ASSERT_FALSE(internalViewport);
1275
1276 // Add an internal viewport, then clear it
1277 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001278 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId, NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001279 ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001280
1281 // Check matching by uniqueId
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001282 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001283 ASSERT_TRUE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001284 ASSERT_EQ(ViewportType::INTERNAL, internalViewport->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001285
1286 // Check matching by viewport type
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001287 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001288 ASSERT_TRUE(internalViewport);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001289 ASSERT_EQ(uniqueId, internalViewport->uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001290
1291 mFakePolicy->clearViewports();
1292 // Make sure nothing is found after clear
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001293 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001294 ASSERT_FALSE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001295 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001296 ASSERT_FALSE(internalViewport);
1297}
1298
1299TEST_F(InputReaderPolicyTest, Viewports_GetByType) {
1300 const std::string internalUniqueId = "local:0";
1301 const std::string externalUniqueId = "local:1";
1302 const std::string virtualUniqueId1 = "virtual:2";
1303 const std::string virtualUniqueId2 = "virtual:3";
1304 constexpr int32_t virtualDisplayId1 = 2;
1305 constexpr int32_t virtualDisplayId2 = 3;
1306
1307 // Add an internal viewport
1308 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001309 DISPLAY_ORIENTATION_0, true /*isActive*/, internalUniqueId,
1310 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001311 // Add an external viewport
1312 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001313 DISPLAY_ORIENTATION_0, true /*isActive*/, externalUniqueId,
1314 NO_PORT, ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001315 // Add an virtual viewport
1316 mFakePolicy->addDisplayViewport(virtualDisplayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001317 DISPLAY_ORIENTATION_0, true /*isActive*/, virtualUniqueId1,
1318 NO_PORT, ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001319 // Add another virtual viewport
1320 mFakePolicy->addDisplayViewport(virtualDisplayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001321 DISPLAY_ORIENTATION_0, true /*isActive*/, virtualUniqueId2,
1322 NO_PORT, ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001323
1324 // Check matching by type for internal
1325 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001326 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001327 ASSERT_TRUE(internalViewport);
1328 ASSERT_EQ(internalUniqueId, internalViewport->uniqueId);
1329
1330 // Check matching by type for external
1331 std::optional<DisplayViewport> externalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001332 mFakePolicy->getDisplayViewportByType(ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001333 ASSERT_TRUE(externalViewport);
1334 ASSERT_EQ(externalUniqueId, externalViewport->uniqueId);
1335
1336 // Check matching by uniqueId for virtual viewport #1
1337 std::optional<DisplayViewport> virtualViewport1 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001338 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001339 ASSERT_TRUE(virtualViewport1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001340 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport1->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001341 ASSERT_EQ(virtualUniqueId1, virtualViewport1->uniqueId);
1342 ASSERT_EQ(virtualDisplayId1, virtualViewport1->displayId);
1343
1344 // Check matching by uniqueId for virtual viewport #2
1345 std::optional<DisplayViewport> virtualViewport2 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001346 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001347 ASSERT_TRUE(virtualViewport2);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001348 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport2->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001349 ASSERT_EQ(virtualUniqueId2, virtualViewport2->uniqueId);
1350 ASSERT_EQ(virtualDisplayId2, virtualViewport2->displayId);
1351}
1352
1353
1354/**
1355 * We can have 2 viewports of the same kind. We can distinguish them by uniqueId, and confirm
1356 * that lookup works by checking display id.
1357 * Check that 2 viewports of each kind is possible, for all existing viewport types.
1358 */
1359TEST_F(InputReaderPolicyTest, Viewports_TwoOfSameType) {
1360 const std::string uniqueId1 = "uniqueId1";
1361 const std::string uniqueId2 = "uniqueId2";
1362 constexpr int32_t displayId1 = 2;
1363 constexpr int32_t displayId2 = 3;
1364
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001365 std::vector<ViewportType> types = {ViewportType::INTERNAL, ViewportType::EXTERNAL,
1366 ViewportType::VIRTUAL};
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001367 for (const ViewportType& type : types) {
1368 mFakePolicy->clearViewports();
1369 // Add a viewport
1370 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001371 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1,
1372 NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001373 // Add another viewport
1374 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001375 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2,
1376 NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001377
1378 // Check that correct display viewport was returned by comparing the display IDs.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001379 std::optional<DisplayViewport> viewport1 =
1380 mFakePolicy->getDisplayViewportByUniqueId(uniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001381 ASSERT_TRUE(viewport1);
1382 ASSERT_EQ(displayId1, viewport1->displayId);
1383 ASSERT_EQ(type, viewport1->type);
1384
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001385 std::optional<DisplayViewport> viewport2 =
1386 mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001387 ASSERT_TRUE(viewport2);
1388 ASSERT_EQ(displayId2, viewport2->displayId);
1389 ASSERT_EQ(type, viewport2->type);
1390
1391 // When there are multiple viewports of the same kind, and uniqueId is not specified
1392 // in the call to getDisplayViewport, then that situation is not supported.
1393 // The viewports can be stored in any order, so we cannot rely on the order, since that
1394 // is just implementation detail.
1395 // However, we can check that it still returns *a* viewport, we just cannot assert
1396 // which one specifically is returned.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001397 std::optional<DisplayViewport> someViewport = mFakePolicy->getDisplayViewportByType(type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001398 ASSERT_TRUE(someViewport);
1399 }
1400}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001401
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001402/**
Michael Wrightdde67b82020-10-27 16:09:22 +00001403 * When we have multiple internal displays make sure we always return the default display when
1404 * querying by type.
1405 */
1406TEST_F(InputReaderPolicyTest, Viewports_ByTypeReturnsDefaultForInternal) {
1407 const std::string uniqueId1 = "uniqueId1";
1408 const std::string uniqueId2 = "uniqueId2";
1409 constexpr int32_t nonDefaultDisplayId = 2;
1410 static_assert(nonDefaultDisplayId != ADISPLAY_ID_DEFAULT,
1411 "Test display ID should not be ADISPLAY_ID_DEFAULT");
1412
1413 // Add the default display first and ensure it gets returned.
1414 mFakePolicy->clearViewports();
1415 mFakePolicy->addDisplayViewport(ADISPLAY_ID_DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001416 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001417 ViewportType::INTERNAL);
1418 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001419 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001420 ViewportType::INTERNAL);
1421
1422 std::optional<DisplayViewport> viewport =
1423 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
1424 ASSERT_TRUE(viewport);
1425 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
1426 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
1427
1428 // Add the default display second to make sure order doesn't matter.
1429 mFakePolicy->clearViewports();
1430 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001431 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001432 ViewportType::INTERNAL);
1433 mFakePolicy->addDisplayViewport(ADISPLAY_ID_DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001434 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001435 ViewportType::INTERNAL);
1436
1437 viewport = 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
1443/**
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001444 * Check getDisplayViewportByPort
1445 */
1446TEST_F(InputReaderPolicyTest, Viewports_GetByPort) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001447 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001448 const std::string uniqueId1 = "uniqueId1";
1449 const std::string uniqueId2 = "uniqueId2";
1450 constexpr int32_t displayId1 = 1;
1451 constexpr int32_t displayId2 = 2;
1452 const uint8_t hdmi1 = 0;
1453 const uint8_t hdmi2 = 1;
1454 const uint8_t hdmi3 = 2;
1455
1456 mFakePolicy->clearViewports();
1457 // Add a viewport that's associated with some display port that's not of interest.
1458 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001459 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, hdmi3,
1460 type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001461 // Add another viewport, connected to HDMI1 port
1462 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001463 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, hdmi1,
1464 type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001465
1466 // Check that correct display viewport was returned by comparing the display ports.
1467 std::optional<DisplayViewport> hdmi1Viewport = mFakePolicy->getDisplayViewportByPort(hdmi1);
1468 ASSERT_TRUE(hdmi1Viewport);
1469 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1470 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1471
1472 // Check that we can still get the same viewport using the uniqueId
1473 hdmi1Viewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
1474 ASSERT_TRUE(hdmi1Viewport);
1475 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1476 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1477 ASSERT_EQ(type, hdmi1Viewport->type);
1478
1479 // Check that we cannot find a port with "HDMI2", because we never added one
1480 std::optional<DisplayViewport> hdmi2Viewport = mFakePolicy->getDisplayViewportByPort(hdmi2);
1481 ASSERT_FALSE(hdmi2Viewport);
1482}
1483
Michael Wrightd02c5b62014-02-10 15:10:22 -08001484// --- InputReaderTest ---
1485
1486class InputReaderTest : public testing::Test {
1487protected:
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001488 sp<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001489 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001490 std::shared_ptr<FakeEventHub> mFakeEventHub;
Prabir Pradhan28efc192019-11-05 01:10:04 +00001491 std::unique_ptr<InstrumentedInputReader> mReader;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001492
Chris Yea52ade12020-08-27 16:49:20 -07001493 void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001494 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001495 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001496 mFakeListener = new TestInputListener();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001497
Prabir Pradhan28efc192019-11-05 01:10:04 +00001498 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
1499 mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001500 }
1501
Chris Yea52ade12020-08-27 16:49:20 -07001502 void TearDown() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001503 mFakeListener.clear();
1504 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001505 }
1506
Chris Ye1b0c7342020-07-28 21:57:03 -07001507 void addDevice(int32_t eventHubId, const std::string& name, Flags<InputDeviceClass> classes,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001508 const PropertyMap* configuration) {
1509 mFakeEventHub->addDevice(eventHubId, name, classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001510
1511 if (configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001512 mFakeEventHub->addConfigurationMap(eventHubId, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001513 }
1514 mFakeEventHub->finishDeviceScan();
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001515 mReader->loopOnce();
1516 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001517 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1518 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001519 }
1520
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001521 void disableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001522 mFakePolicy->addDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001523 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001524 }
1525
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001526 void enableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001527 mFakePolicy->removeDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001528 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001529 }
1530
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001531 FakeInputMapper& addDeviceWithFakeInputMapper(int32_t deviceId, int32_t eventHubId,
Chris Ye1b0c7342020-07-28 21:57:03 -07001532 const std::string& name,
1533 Flags<InputDeviceClass> classes, uint32_t sources,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001534 const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001535 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, name);
1536 FakeInputMapper& mapper = device->addMapper<FakeInputMapper>(eventHubId, sources);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001537 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001538 addDevice(eventHubId, name, classes, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001539 return mapper;
1540 }
1541};
1542
Chris Ye98d3f532020-10-01 21:48:59 -07001543TEST_F(InputReaderTest, PolicyGetInputDevices) {
1544 ASSERT_NO_FATAL_FAILURE(addDevice(1, "keyboard", InputDeviceClass::KEYBOARD, nullptr));
1545 ASSERT_NO_FATAL_FAILURE(addDevice(2, "ignored", Flags<InputDeviceClass>(0),
1546 nullptr)); // no classes so device will be ignored
Michael Wrightd02c5b62014-02-10 15:10:22 -08001547
1548 // Should also have received a notification describing the new input devices.
Chris Ye98d3f532020-10-01 21:48:59 -07001549 const std::vector<InputDeviceInfo>& inputDevices = mFakePolicy->getInputDevices();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001550 ASSERT_EQ(1U, inputDevices.size());
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001551 ASSERT_EQ(END_RESERVED_ID + 1, inputDevices[0].getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001552 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001553 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
1554 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00001555 ASSERT_EQ(0U, inputDevices[0].getMotionRanges().size());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001556}
1557
Chris Yee7310032020-09-22 15:36:28 -07001558TEST_F(InputReaderTest, GetMergedInputDevices) {
1559 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1560 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1561 // Add two subdevices to device
1562 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1563 // Must add at least one mapper or the device will be ignored!
1564 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1565 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1566
1567 // Push same device instance for next device to be added, so they'll have same identifier.
1568 mReader->pushNextDevice(device);
1569 mReader->pushNextDevice(device);
1570 ASSERT_NO_FATAL_FAILURE(
1571 addDevice(eventHubIds[0], "fake1", InputDeviceClass::KEYBOARD, nullptr));
1572 ASSERT_NO_FATAL_FAILURE(
1573 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
1574
1575 // Two devices will be merged to one input device as they have same identifier
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00001576 ASSERT_EQ(1U, mFakePolicy->getInputDevices().size());
Chris Yee7310032020-09-22 15:36:28 -07001577}
1578
Chris Yee14523a2020-12-19 13:46:00 -08001579TEST_F(InputReaderTest, GetMergedInputDevicesEnabled) {
1580 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1581 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1582 // Add two subdevices to device
1583 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1584 // Must add at least one mapper or the device will be ignored!
1585 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1586 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1587
1588 // Push same device instance for next device to be added, so they'll have same identifier.
1589 mReader->pushNextDevice(device);
1590 mReader->pushNextDevice(device);
1591 // Sensor device is initially disabled
1592 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1",
1593 InputDeviceClass::KEYBOARD | InputDeviceClass::SENSOR,
1594 nullptr));
1595 // Device is disabled because the only sub device is a sensor device and disabled initially.
1596 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1597 ASSERT_FALSE(device->isEnabled());
1598 ASSERT_NO_FATAL_FAILURE(
1599 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
1600 // The merged device is enabled if any sub device is enabled
1601 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1602 ASSERT_TRUE(device->isEnabled());
1603}
1604
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001605TEST_F(InputReaderTest, WhenEnabledChanges_SendsDeviceResetNotification) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001606 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001607 constexpr Flags<InputDeviceClass> deviceClass(InputDeviceClass::KEYBOARD);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001608 constexpr int32_t eventHubId = 1;
1609 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001610 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001611 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001612 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001613 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001614
Yi Kong9b14ac62018-07-17 13:48:38 -07001615 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001616
1617 NotifyDeviceResetArgs resetArgs;
1618 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001619 ASSERT_EQ(deviceId, resetArgs.deviceId);
1620
1621 ASSERT_EQ(device->isEnabled(), true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001622 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001623 mReader->loopOnce();
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001624
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001625 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001626 ASSERT_EQ(deviceId, resetArgs.deviceId);
1627 ASSERT_EQ(device->isEnabled(), false);
1628
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001629 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001630 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001631 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
1632 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasNotCalled());
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001633 ASSERT_EQ(device->isEnabled(), false);
1634
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001635 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001636 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001637 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001638 ASSERT_EQ(deviceId, resetArgs.deviceId);
1639 ASSERT_EQ(device->isEnabled(), true);
1640}
1641
Michael Wrightd02c5b62014-02-10 15:10:22 -08001642TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001643 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001644 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001645 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001646 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001647 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001648 AINPUT_SOURCE_KEYBOARD, nullptr);
1649 mapper.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001650
1651 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(0,
1652 AINPUT_SOURCE_ANY, AKEYCODE_A))
1653 << "Should return unknown when the device id is >= 0 but unknown.";
1654
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001655 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1656 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1657 << "Should return unknown when the device id is valid but the sources are not "
1658 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001659
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001660 ASSERT_EQ(AKEY_STATE_DOWN,
1661 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1662 AKEYCODE_A))
1663 << "Should return value provided by mapper when device id is valid and the device "
1664 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001665
1666 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(-1,
1667 AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1668 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1669
1670 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getKeyCodeState(-1,
1671 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1672 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1673}
1674
1675TEST_F(InputReaderTest, GetScanCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001676 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001677 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001678 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001679 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001680 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001681 AINPUT_SOURCE_KEYBOARD, nullptr);
1682 mapper.setScanCodeState(KEY_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001683
1684 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(0,
1685 AINPUT_SOURCE_ANY, KEY_A))
1686 << "Should return unknown when the device id is >= 0 but unknown.";
1687
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001688 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1689 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, KEY_A))
1690 << "Should return unknown when the device id is valid but the sources are not "
1691 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001692
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001693 ASSERT_EQ(AKEY_STATE_DOWN,
1694 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1695 KEY_A))
1696 << "Should return value provided by mapper when device id is valid and the device "
1697 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001698
1699 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(-1,
1700 AINPUT_SOURCE_TRACKBALL, KEY_A))
1701 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1702
1703 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getScanCodeState(-1,
1704 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, KEY_A))
1705 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1706}
1707
1708TEST_F(InputReaderTest, GetSwitchState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001709 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001710 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001711 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001712 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001713 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001714 AINPUT_SOURCE_KEYBOARD, nullptr);
1715 mapper.setSwitchState(SW_LID, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001716
1717 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(0,
1718 AINPUT_SOURCE_ANY, SW_LID))
1719 << "Should return unknown when the device id is >= 0 but unknown.";
1720
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001721 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1722 mReader->getSwitchState(deviceId, AINPUT_SOURCE_TRACKBALL, SW_LID))
1723 << "Should return unknown when the device id is valid but the sources are not "
1724 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001725
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001726 ASSERT_EQ(AKEY_STATE_DOWN,
1727 mReader->getSwitchState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1728 SW_LID))
1729 << "Should return value provided by mapper when device id is valid and the device "
1730 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001731
1732 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(-1,
1733 AINPUT_SOURCE_TRACKBALL, SW_LID))
1734 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1735
1736 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getSwitchState(-1,
1737 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, SW_LID))
1738 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1739}
1740
1741TEST_F(InputReaderTest, MarkSupportedKeyCodes_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001742 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001743 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001744 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001745 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001746 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001747 AINPUT_SOURCE_KEYBOARD, nullptr);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001748
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001749 mapper.addSupportedKeyCode(AKEYCODE_A);
1750 mapper.addSupportedKeyCode(AKEYCODE_B);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001751
1752 const int32_t keyCodes[4] = { AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2 };
1753 uint8_t flags[4] = { 0, 0, 0, 1 };
1754
1755 ASSERT_FALSE(mReader->hasKeys(0, AINPUT_SOURCE_ANY, 4, keyCodes, flags))
1756 << "Should return false when device id is >= 0 but unknown.";
1757 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1758
1759 flags[3] = 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001760 ASSERT_FALSE(mReader->hasKeys(deviceId, AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1761 << "Should return false when device id is valid but the sources are not supported by "
1762 "the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001763 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1764
1765 flags[3] = 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001766 ASSERT_TRUE(mReader->hasKeys(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, 4,
1767 keyCodes, flags))
1768 << "Should return value provided by mapper when device id is valid and the device "
1769 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001770 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1771
1772 flags[3] = 1;
1773 ASSERT_FALSE(mReader->hasKeys(-1, AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1774 << "Should return false when the device id is < 0 but the sources are not supported by any device.";
1775 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1776
1777 flags[3] = 1;
1778 ASSERT_TRUE(mReader->hasKeys(-1, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1779 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1780 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1781}
1782
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001783TEST_F(InputReaderTest, LoopOnce_WhenDeviceScanFinished_SendsConfigurationChanged) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001784 constexpr int32_t eventHubId = 1;
Chris Ye1b0c7342020-07-28 21:57:03 -07001785 addDevice(eventHubId, "ignored", InputDeviceClass::KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001786
1787 NotifyConfigurationChangedArgs args;
1788
1789 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(&args));
1790 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
1791}
1792
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001793TEST_F(InputReaderTest, LoopOnce_ForwardsRawEventsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001794 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001795 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001796 constexpr nsecs_t when = 0;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001797 constexpr int32_t eventHubId = 1;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001798 constexpr nsecs_t readTime = 2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001799 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001800 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001801 AINPUT_SOURCE_KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001802
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001803 mFakeEventHub->enqueueEvent(when, readTime, eventHubId, EV_KEY, KEY_A, 1);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001804 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001805 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
1806
1807 RawEvent event;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001808 ASSERT_NO_FATAL_FAILURE(mapper.assertProcessWasCalled(&event));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001809 ASSERT_EQ(when, event.when);
1810 ASSERT_EQ(readTime, event.readTime);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001811 ASSERT_EQ(eventHubId, event.deviceId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001812 ASSERT_EQ(EV_KEY, event.type);
1813 ASSERT_EQ(KEY_A, event.code);
1814 ASSERT_EQ(1, event.value);
1815}
1816
Garfield Tan1c7bc862020-01-28 13:24:04 -08001817TEST_F(InputReaderTest, DeviceReset_RandomId) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001818 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001819 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001820 constexpr int32_t eventHubId = 1;
1821 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Prabir Pradhan42611e02018-11-27 14:04:02 -08001822 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001823 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001824 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001825 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan42611e02018-11-27 14:04:02 -08001826
1827 NotifyDeviceResetArgs resetArgs;
1828 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001829 int32_t prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001830
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001831 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001832 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001833 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001834 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001835 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001836
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001837 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001838 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001839 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001840 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001841 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001842
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001843 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001844 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001845 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001846 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001847 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001848}
1849
Garfield Tan1c7bc862020-01-28 13:24:04 -08001850TEST_F(InputReaderTest, DeviceReset_GenerateIdWithInputReaderSource) {
1851 constexpr int32_t deviceId = 1;
Chris Ye1b0c7342020-07-28 21:57:03 -07001852 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Garfield Tan1c7bc862020-01-28 13:24:04 -08001853 constexpr int32_t eventHubId = 1;
1854 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1855 // Must add at least one mapper or the device will be ignored!
1856 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001857 mReader->pushNextDevice(device);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001858 ASSERT_NO_FATAL_FAILURE(addDevice(deviceId, "fake", deviceClass, nullptr));
1859
1860 NotifyDeviceResetArgs resetArgs;
1861 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1862 ASSERT_EQ(IdGenerator::Source::INPUT_READER, IdGenerator::getSource(resetArgs.id));
1863}
1864
Arthur Hungc23540e2018-11-29 20:42:11 +08001865TEST_F(InputReaderTest, Device_CanDispatchToDisplay) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001866 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001867 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001868 constexpr int32_t eventHubId = 1;
Arthur Hungc23540e2018-11-29 20:42:11 +08001869 const char* DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001870 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1871 FakeInputMapper& mapper =
1872 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001873 mReader->pushNextDevice(device);
Arthur Hungc23540e2018-11-29 20:42:11 +08001874
1875 const uint8_t hdmi1 = 1;
1876
1877 // Associated touch screen with second display.
1878 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
1879
1880 // Add default and second display.
Prabir Pradhan28efc192019-11-05 01:10:04 +00001881 mFakePolicy->clearViewports();
Arthur Hungc23540e2018-11-29 20:42:11 +08001882 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001883 DISPLAY_ORIENTATION_0, true /*isActive*/, "local:0", NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001884 ViewportType::INTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08001885 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001886 DISPLAY_ORIENTATION_0, true /*isActive*/, "local:1", hdmi1,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001887 ViewportType::EXTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08001888 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001889 mReader->loopOnce();
Prabir Pradhan28efc192019-11-05 01:10:04 +00001890
1891 // Add the device, and make sure all of the callbacks are triggered.
1892 // The device is added after the input port associations are processed since
1893 // we do not yet support dynamic device-to-display associations.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001894 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001895 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled());
Prabir Pradhan28efc192019-11-05 01:10:04 +00001896 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001897 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
Arthur Hungc23540e2018-11-29 20:42:11 +08001898
Arthur Hung2c9a3342019-07-23 14:18:59 +08001899 // Device should only dispatch to the specified display.
Arthur Hungc23540e2018-11-29 20:42:11 +08001900 ASSERT_EQ(deviceId, device->getId());
1901 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, DISPLAY_ID));
1902 ASSERT_TRUE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hung2c9a3342019-07-23 14:18:59 +08001903
1904 // Can't dispatch event from a disabled device.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001905 disableDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001906 mReader->loopOnce();
Arthur Hung2c9a3342019-07-23 14:18:59 +08001907 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hungc23540e2018-11-29 20:42:11 +08001908}
1909
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001910TEST_F(InputReaderTest, WhenEnabledChanges_AllSubdevicesAreUpdated) {
1911 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1912 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
1913 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1914 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1915 // Must add at least one mapper or the device will be ignored!
1916 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1917 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1918 mReader->pushNextDevice(device);
1919 mReader->pushNextDevice(device);
1920 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
1921 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
1922
1923 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
1924
1925 NotifyDeviceResetArgs resetArgs;
1926 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1927 ASSERT_EQ(deviceId, resetArgs.deviceId);
1928 ASSERT_TRUE(device->isEnabled());
1929 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1930 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1931
1932 disableDevice(deviceId);
1933 mReader->loopOnce();
1934
1935 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1936 ASSERT_EQ(deviceId, resetArgs.deviceId);
1937 ASSERT_FALSE(device->isEnabled());
1938 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1939 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1940
1941 enableDevice(deviceId);
1942 mReader->loopOnce();
1943
1944 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1945 ASSERT_EQ(deviceId, resetArgs.deviceId);
1946 ASSERT_TRUE(device->isEnabled());
1947 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1948 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1949}
1950
1951TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToSubdeviceMappers) {
1952 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1953 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
1954 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1955 // Add two subdevices to device
1956 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1957 FakeInputMapper& mapperDevice1 =
1958 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1959 FakeInputMapper& mapperDevice2 =
1960 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1961 mReader->pushNextDevice(device);
1962 mReader->pushNextDevice(device);
1963 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
1964 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
1965
1966 mapperDevice1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
1967 mapperDevice2.setKeyCodeState(AKEYCODE_B, AKEY_STATE_DOWN);
1968
1969 ASSERT_EQ(AKEY_STATE_DOWN,
1970 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_A));
1971 ASSERT_EQ(AKEY_STATE_DOWN,
1972 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_B));
1973 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1974 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_C));
1975}
1976
Prabir Pradhan7e186182020-11-10 13:56:45 -08001977TEST_F(InputReaderTest, ChangingPointerCaptureNotifiesInputListener) {
1978 NotifyPointerCaptureChangedArgs args;
1979
Prabir Pradhanac483a62021-08-06 14:01:18 +00001980 auto request = mFakePolicy->setPointerCapture(true);
Prabir Pradhan7e186182020-11-10 13:56:45 -08001981 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
1982 mReader->loopOnce();
1983 mFakeListener->assertNotifyCaptureWasCalled(&args);
Prabir Pradhanac483a62021-08-06 14:01:18 +00001984 ASSERT_TRUE(args.request.enable) << "Pointer Capture should be enabled.";
1985 ASSERT_EQ(args.request, request) << "Pointer Capture sequence number should match.";
Prabir Pradhan7e186182020-11-10 13:56:45 -08001986
1987 mFakePolicy->setPointerCapture(false);
1988 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
1989 mReader->loopOnce();
1990 mFakeListener->assertNotifyCaptureWasCalled(&args);
Prabir Pradhanac483a62021-08-06 14:01:18 +00001991 ASSERT_FALSE(args.request.enable) << "Pointer Capture should be disabled.";
Prabir Pradhan7e186182020-11-10 13:56:45 -08001992
Prabir Pradhanac483a62021-08-06 14:01:18 +00001993 // Verify that the Pointer Capture state is not updated when the configuration value
Prabir Pradhan7e186182020-11-10 13:56:45 -08001994 // does not change.
1995 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
1996 mReader->loopOnce();
Prabir Pradhanac483a62021-08-06 14:01:18 +00001997 mFakeListener->assertNotifyCaptureWasNotCalled();
Prabir Pradhan7e186182020-11-10 13:56:45 -08001998}
1999
Chris Ye87143712020-11-10 05:05:58 +00002000class FakeVibratorInputMapper : public FakeInputMapper {
2001public:
2002 FakeVibratorInputMapper(InputDeviceContext& deviceContext, uint32_t sources)
2003 : FakeInputMapper(deviceContext, sources) {}
2004
2005 std::vector<int32_t> getVibratorIds() override { return getDeviceContext().getVibratorIds(); }
2006};
2007
2008TEST_F(InputReaderTest, VibratorGetVibratorIds) {
2009 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
2010 Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD | InputDeviceClass::VIBRATOR;
2011 constexpr int32_t eventHubId = 1;
2012 const char* DEVICE_LOCATION = "BLUETOOTH";
2013 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
2014 FakeVibratorInputMapper& mapper =
2015 device->addMapper<FakeVibratorInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
2016 mReader->pushNextDevice(device);
2017
2018 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2019 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
2020
2021 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
2022 ASSERT_EQ(mReader->getVibratorIds(deviceId).size(), 2U);
2023}
2024
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002025// --- FakePeripheralController ---
Kim Low03ea0352020-11-06 12:45:07 -08002026
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002027class FakePeripheralController : public PeripheralControllerInterface {
Chris Yee2b1e5c2021-03-10 22:45:12 -08002028public:
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002029 FakePeripheralController(InputDeviceContext& deviceContext) : mDeviceContext(deviceContext) {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08002030
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002031 ~FakePeripheralController() override {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08002032
2033 void populateDeviceInfo(InputDeviceInfo* deviceInfo) override {}
2034
2035 void dump(std::string& dump) override {}
2036
2037 std::optional<int32_t> getBatteryCapacity(int32_t batteryId) override {
2038 return getDeviceContext().getBatteryCapacity(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08002039 }
2040
Chris Yee2b1e5c2021-03-10 22:45:12 -08002041 std::optional<int32_t> getBatteryStatus(int32_t batteryId) override {
2042 return getDeviceContext().getBatteryStatus(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08002043 }
Chris Ye3fdbfef2021-01-06 18:45:18 -08002044
2045 bool setLightColor(int32_t lightId, int32_t color) override {
2046 getDeviceContext().setLightBrightness(lightId, color >> 24);
2047 return true;
2048 }
2049
2050 std::optional<int32_t> getLightColor(int32_t lightId) override {
2051 std::optional<int32_t> result = getDeviceContext().getLightBrightness(lightId);
2052 if (!result.has_value()) {
2053 return std::nullopt;
2054 }
2055 return result.value() << 24;
2056 }
Chris Yee2b1e5c2021-03-10 22:45:12 -08002057
2058 bool setLightPlayerId(int32_t lightId, int32_t playerId) override { return true; }
2059
2060 std::optional<int32_t> getLightPlayerId(int32_t lightId) override { return std::nullopt; }
2061
2062private:
2063 InputDeviceContext& mDeviceContext;
2064 inline int32_t getDeviceId() { return mDeviceContext.getId(); }
2065 inline InputDeviceContext& getDeviceContext() { return mDeviceContext; }
Chris Ye3fdbfef2021-01-06 18:45:18 -08002066};
2067
Chris Yee2b1e5c2021-03-10 22:45:12 -08002068TEST_F(InputReaderTest, BatteryGetCapacity) {
2069 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
2070 Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
2071 constexpr int32_t eventHubId = 1;
2072 const char* DEVICE_LOCATION = "BLUETOOTH";
2073 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002074 FakePeripheralController& controller =
2075 device->addController<FakePeripheralController>(eventHubId);
Chris Yee2b1e5c2021-03-10 22:45:12 -08002076 mReader->pushNextDevice(device);
2077
2078 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2079
2080 ASSERT_EQ(controller.getBatteryCapacity(DEFAULT_BATTERY), BATTERY_CAPACITY);
2081 ASSERT_EQ(mReader->getBatteryCapacity(deviceId), BATTERY_CAPACITY);
2082}
2083
2084TEST_F(InputReaderTest, BatteryGetStatus) {
2085 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
2086 Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
2087 constexpr int32_t eventHubId = 1;
2088 const char* DEVICE_LOCATION = "BLUETOOTH";
2089 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002090 FakePeripheralController& controller =
2091 device->addController<FakePeripheralController>(eventHubId);
Chris Yee2b1e5c2021-03-10 22:45:12 -08002092 mReader->pushNextDevice(device);
2093
2094 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2095
2096 ASSERT_EQ(controller.getBatteryStatus(DEFAULT_BATTERY), BATTERY_STATUS);
2097 ASSERT_EQ(mReader->getBatteryStatus(deviceId), BATTERY_STATUS);
2098}
2099
Chris Ye3fdbfef2021-01-06 18:45:18 -08002100TEST_F(InputReaderTest, LightGetColor) {
2101 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
2102 Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD | InputDeviceClass::LIGHT;
2103 constexpr int32_t eventHubId = 1;
2104 const char* DEVICE_LOCATION = "BLUETOOTH";
2105 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002106 FakePeripheralController& controller =
2107 device->addController<FakePeripheralController>(eventHubId);
Chris Ye3fdbfef2021-01-06 18:45:18 -08002108 mReader->pushNextDevice(device);
2109 RawLightInfo info = {.id = 1,
2110 .name = "Mono",
2111 .maxBrightness = 255,
2112 .flags = InputLightClass::BRIGHTNESS,
2113 .path = ""};
2114 mFakeEventHub->addRawLightInfo(1 /* rawId */, std::move(info));
2115 mFakeEventHub->fakeLightBrightness(1 /* rawId */, 0x55);
2116
2117 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Chris Ye3fdbfef2021-01-06 18:45:18 -08002118
Chris Yee2b1e5c2021-03-10 22:45:12 -08002119 ASSERT_TRUE(controller.setLightColor(1 /* lightId */, LIGHT_BRIGHTNESS));
2120 ASSERT_EQ(controller.getLightColor(1 /* lightId */), LIGHT_BRIGHTNESS);
Chris Ye3fdbfef2021-01-06 18:45:18 -08002121 ASSERT_TRUE(mReader->setLightColor(deviceId, 1 /* lightId */, LIGHT_BRIGHTNESS));
2122 ASSERT_EQ(mReader->getLightColor(deviceId, 1 /* lightId */), LIGHT_BRIGHTNESS);
2123}
2124
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002125// --- InputReaderIntegrationTest ---
2126
2127// These tests create and interact with the InputReader only through its interface.
2128// The InputReader is started during SetUp(), which starts its processing in its own
2129// thread. The tests use linux uinput to emulate input devices.
2130// NOTE: Interacting with the physical device while these tests are running may cause
2131// the tests to fail.
2132class InputReaderIntegrationTest : public testing::Test {
2133protected:
2134 sp<TestInputListener> mTestListener;
2135 sp<FakeInputReaderPolicy> mFakePolicy;
2136 sp<InputReaderInterface> mReader;
2137
Chris Yea52ade12020-08-27 16:49:20 -07002138 void SetUp() override {
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002139 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakouf0db5b82020-04-08 19:22:14 -07002140 mTestListener = new TestInputListener(2000ms /*eventHappenedTimeout*/,
2141 30ms /*eventDidNotHappenTimeout*/);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002142
Prabir Pradhan9244aea2020-02-05 20:31:40 -08002143 mReader = new InputReader(std::make_shared<EventHub>(), mFakePolicy, mTestListener);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002144 ASSERT_EQ(mReader->start(), OK);
2145
2146 // Since this test is run on a real device, all the input devices connected
2147 // to the test device will show up in mReader. We wait for those input devices to
2148 // show up before beginning the tests.
2149 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2150 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2151 }
2152
Chris Yea52ade12020-08-27 16:49:20 -07002153 void TearDown() override {
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002154 ASSERT_EQ(mReader->stop(), OK);
2155 mTestListener.clear();
2156 mFakePolicy.clear();
2157 }
2158};
2159
2160TEST_F(InputReaderIntegrationTest, TestInvalidDevice) {
2161 // An invalid input device that is only used for this test.
2162 class InvalidUinputDevice : public UinputDevice {
2163 public:
2164 InvalidUinputDevice() : UinputDevice("Invalid Device") {}
2165
2166 private:
2167 void configureDevice(int fd, uinput_user_dev* device) override {}
2168 };
2169
2170 const size_t numDevices = mFakePolicy->getInputDevices().size();
2171
2172 // UinputDevice does not set any event or key bits, so InputReader should not
2173 // consider it as a valid device.
2174 std::unique_ptr<UinputDevice> invalidDevice = createUinputDevice<InvalidUinputDevice>();
2175 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
2176 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
2177 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
2178
2179 invalidDevice.reset();
2180 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
2181 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
2182 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
2183}
2184
2185TEST_F(InputReaderIntegrationTest, AddNewDevice) {
2186 const size_t initialNumDevices = mFakePolicy->getInputDevices().size();
2187
2188 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
2189 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2190 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2191 ASSERT_EQ(initialNumDevices + 1, mFakePolicy->getInputDevices().size());
2192
2193 // Find the test device by its name.
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002194 const std::vector<InputDeviceInfo> inputDevices = mFakePolicy->getInputDevices();
Chris Ye98d3f532020-10-01 21:48:59 -07002195 const auto& it =
2196 std::find_if(inputDevices.begin(), inputDevices.end(),
2197 [&keyboard](const InputDeviceInfo& info) {
2198 return info.getIdentifier().name == keyboard->getName();
2199 });
2200
2201 ASSERT_NE(it, inputDevices.end());
2202 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, it->getKeyboardType());
2203 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, it->getSources());
2204 ASSERT_EQ(0U, it->getMotionRanges().size());
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002205
2206 keyboard.reset();
2207 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2208 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2209 ASSERT_EQ(initialNumDevices, mFakePolicy->getInputDevices().size());
2210}
2211
2212TEST_F(InputReaderIntegrationTest, SendsEventsToInputListener) {
2213 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
2214 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2215
2216 NotifyConfigurationChangedArgs configChangedArgs;
2217 ASSERT_NO_FATAL_FAILURE(
2218 mTestListener->assertNotifyConfigurationChangedWasCalled(&configChangedArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002219 int32_t prevId = configChangedArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002220 nsecs_t prevTimestamp = configChangedArgs.eventTime;
2221
2222 NotifyKeyArgs keyArgs;
2223 keyboard->pressAndReleaseHomeKey();
2224 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
2225 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002226 ASSERT_NE(prevId, keyArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002227 prevId = keyArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002228 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002229 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002230 prevTimestamp = keyArgs.eventTime;
2231
2232 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
2233 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002234 ASSERT_NE(prevId, keyArgs.id);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002235 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002236 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002237}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002238
Siarhei Vishniakoua0d2b802020-05-13 14:00:31 -07002239/**
2240 * The Steam controller sends BTN_GEAR_DOWN and BTN_GEAR_UP for the two "paddle" buttons
2241 * on the back. In this test, we make sure that BTN_GEAR_DOWN / BTN_WHEEL and BTN_GEAR_UP
2242 * are passed to the listener.
2243 */
2244static_assert(BTN_GEAR_DOWN == BTN_WHEEL);
2245TEST_F(InputReaderIntegrationTest, SendsGearDownAndUpToInputListener) {
2246 std::unique_ptr<UinputSteamController> controller = createUinputDevice<UinputSteamController>();
2247 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2248 NotifyKeyArgs keyArgs;
2249
2250 controller->pressAndReleaseKey(BTN_GEAR_DOWN);
2251 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
2252 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
2253 ASSERT_EQ(BTN_GEAR_DOWN, keyArgs.scanCode);
2254
2255 controller->pressAndReleaseKey(BTN_GEAR_UP);
2256 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
2257 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
2258 ASSERT_EQ(BTN_GEAR_UP, keyArgs.scanCode);
2259}
2260
Arthur Hungaab25622020-01-16 11:22:11 +08002261// --- TouchProcessTest ---
2262class TouchIntegrationTest : public InputReaderIntegrationTest {
2263protected:
Arthur Hungaab25622020-01-16 11:22:11 +08002264 const std::string UNIQUE_ID = "local:0";
2265
Chris Yea52ade12020-08-27 16:49:20 -07002266 void SetUp() override {
Arthur Hungaab25622020-01-16 11:22:11 +08002267 InputReaderIntegrationTest::SetUp();
2268 // At least add an internal display.
2269 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2270 DISPLAY_ORIENTATION_0, UNIQUE_ID, NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002271 ViewportType::INTERNAL);
Arthur Hungaab25622020-01-16 11:22:11 +08002272
2273 mDevice = createUinputDevice<UinputTouchScreen>(Rect(0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT));
2274 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2275 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2276 }
2277
2278 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
2279 int32_t orientation, const std::string& uniqueId,
2280 std::optional<uint8_t> physicalPort,
2281 ViewportType viewportType) {
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002282 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, true /*isActive*/,
2283 uniqueId, physicalPort, viewportType);
Arthur Hungaab25622020-01-16 11:22:11 +08002284 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2285 }
2286
2287 std::unique_ptr<UinputTouchScreen> mDevice;
2288};
2289
2290TEST_F(TouchIntegrationTest, InputEvent_ProcessSingleTouch) {
2291 NotifyMotionArgs args;
2292 const Point centerPoint = mDevice->getCenterPoint();
2293
2294 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00002295 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002296 mDevice->sendDown(centerPoint);
2297 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2298 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2299
2300 // ACTION_MOVE
2301 mDevice->sendMove(centerPoint + Point(1, 1));
2302 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2303 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2304
2305 // ACTION_UP
2306 mDevice->sendUp();
2307 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2308 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2309}
2310
2311TEST_F(TouchIntegrationTest, InputEvent_ProcessMultiTouch) {
2312 NotifyMotionArgs args;
2313 const Point centerPoint = mDevice->getCenterPoint();
2314
2315 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00002316 mDevice->sendSlot(FIRST_SLOT);
2317 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002318 mDevice->sendDown(centerPoint);
2319 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2320 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2321
2322 // ACTION_POINTER_DOWN (Second slot)
2323 const Point secondPoint = centerPoint + Point(100, 100);
2324 mDevice->sendSlot(SECOND_SLOT);
2325 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2326 mDevice->sendDown(secondPoint + Point(1, 1));
2327 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2328 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
2329 args.action);
2330
2331 // ACTION_MOVE (Second slot)
2332 mDevice->sendMove(secondPoint);
2333 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2334 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2335
2336 // ACTION_POINTER_UP (Second slot)
arthurhungcc7f9802020-04-30 17:55:40 +08002337 mDevice->sendPointerUp();
Arthur Hungaab25622020-01-16 11:22:11 +08002338 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
arthurhungcc7f9802020-04-30 17:55:40 +08002339 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
Arthur Hungaab25622020-01-16 11:22:11 +08002340 args.action);
2341
2342 // ACTION_UP
2343 mDevice->sendSlot(FIRST_SLOT);
2344 mDevice->sendUp();
2345 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2346 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2347}
2348
2349TEST_F(TouchIntegrationTest, InputEvent_ProcessPalm) {
2350 NotifyMotionArgs args;
2351 const Point centerPoint = mDevice->getCenterPoint();
2352
2353 // ACTION_DOWN
arthurhungcc7f9802020-04-30 17:55:40 +08002354 mDevice->sendSlot(FIRST_SLOT);
2355 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002356 mDevice->sendDown(centerPoint);
2357 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2358 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2359
arthurhungcc7f9802020-04-30 17:55:40 +08002360 // ACTION_POINTER_DOWN (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002361 const Point secondPoint = centerPoint + Point(100, 100);
2362 mDevice->sendSlot(SECOND_SLOT);
2363 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2364 mDevice->sendDown(secondPoint);
2365 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2366 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
2367 args.action);
2368
arthurhungcc7f9802020-04-30 17:55:40 +08002369 // ACTION_MOVE (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002370 mDevice->sendMove(secondPoint + Point(1, 1));
2371 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2372 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2373
arthurhungcc7f9802020-04-30 17:55:40 +08002374 // Send MT_TOOL_PALM (second slot), which indicates that the touch IC has determined this to be
2375 // a palm event.
2376 // Expect to receive the ACTION_POINTER_UP with cancel flag.
Arthur Hungaab25622020-01-16 11:22:11 +08002377 mDevice->sendToolType(MT_TOOL_PALM);
2378 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
arthurhungcc7f9802020-04-30 17:55:40 +08002379 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
2380 args.action);
2381 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, args.flags);
Arthur Hungaab25622020-01-16 11:22:11 +08002382
arthurhungcc7f9802020-04-30 17:55:40 +08002383 // Send up to second slot, expect first slot send moving.
2384 mDevice->sendPointerUp();
2385 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2386 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002387
arthurhungcc7f9802020-04-30 17:55:40 +08002388 // Send ACTION_UP (first slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002389 mDevice->sendSlot(FIRST_SLOT);
2390 mDevice->sendUp();
2391
arthurhungcc7f9802020-04-30 17:55:40 +08002392 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2393 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002394}
2395
Michael Wrightd02c5b62014-02-10 15:10:22 -08002396// --- InputDeviceTest ---
Michael Wrightd02c5b62014-02-10 15:10:22 -08002397class InputDeviceTest : public testing::Test {
2398protected:
2399 static const char* DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002400 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002401 static const int32_t DEVICE_ID;
2402 static const int32_t DEVICE_GENERATION;
2403 static const int32_t DEVICE_CONTROLLER_NUMBER;
Chris Ye1b0c7342020-07-28 21:57:03 -07002404 static const Flags<InputDeviceClass> DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002405 static const int32_t EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002406
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002407 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002408 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08002409 sp<TestInputListener> mFakeListener;
arthurhungdcef2dc2020-08-11 14:47:50 +08002410 std::unique_ptr<InstrumentedInputReader> mReader;
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00002411 std::shared_ptr<InputDevice> mDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002412
Chris Yea52ade12020-08-27 16:49:20 -07002413 void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002414 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002415 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08002416 mFakeListener = new TestInputListener();
arthurhungdcef2dc2020-08-11 14:47:50 +08002417 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
2418 mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002419 InputDeviceIdentifier identifier;
2420 identifier.name = DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002421 identifier.location = DEVICE_LOCATION;
arthurhungdcef2dc2020-08-11 14:47:50 +08002422 mDevice = std::make_shared<InputDevice>(mReader->getContext(), DEVICE_ID, DEVICE_GENERATION,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002423 identifier);
arthurhungdcef2dc2020-08-11 14:47:50 +08002424 mReader->pushNextDevice(mDevice);
2425 mFakeEventHub->addDevice(EVENTHUB_ID, DEVICE_NAME, Flags<InputDeviceClass>(0));
2426 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002427 }
2428
Chris Yea52ade12020-08-27 16:49:20 -07002429 void TearDown() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002430 mFakeListener.clear();
2431 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002432 }
2433};
2434
2435const char* InputDeviceTest::DEVICE_NAME = "device";
Arthur Hung2c9a3342019-07-23 14:18:59 +08002436const char* InputDeviceTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002437const int32_t InputDeviceTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002438const int32_t InputDeviceTest::DEVICE_GENERATION = 2;
2439const int32_t InputDeviceTest::DEVICE_CONTROLLER_NUMBER = 0;
Chris Ye1b0c7342020-07-28 21:57:03 -07002440const Flags<InputDeviceClass> InputDeviceTest::DEVICE_CLASSES =
2441 InputDeviceClass::KEYBOARD | InputDeviceClass::TOUCH | InputDeviceClass::JOYSTICK;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002442const int32_t InputDeviceTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002443
2444TEST_F(InputDeviceTest, ImmutableProperties) {
2445 ASSERT_EQ(DEVICE_ID, mDevice->getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002446 ASSERT_STREQ(DEVICE_NAME, mDevice->getName().c_str());
Chris Ye1b0c7342020-07-28 21:57:03 -07002447 ASSERT_EQ(Flags<InputDeviceClass>(0), mDevice->getClasses());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002448}
2449
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002450TEST_F(InputDeviceTest, WhenDeviceCreated_EnabledIsFalse) {
2451 ASSERT_EQ(mDevice->isEnabled(), false);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07002452}
2453
Michael Wrightd02c5b62014-02-10 15:10:22 -08002454TEST_F(InputDeviceTest, WhenNoMappersAreRegistered_DeviceIsIgnored) {
2455 // Configuration.
2456 InputReaderConfiguration config;
2457 mDevice->configure(ARBITRARY_TIME, &config, 0);
2458
2459 // Reset.
2460 mDevice->reset(ARBITRARY_TIME);
2461
2462 NotifyDeviceResetArgs resetArgs;
2463 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2464 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2465 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2466
2467 // Metadata.
2468 ASSERT_TRUE(mDevice->isIgnored());
2469 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mDevice->getSources());
2470
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002471 InputDeviceInfo info = mDevice->getDeviceInfo();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002472 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002473 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002474 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NONE, info.getKeyboardType());
2475 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, info.getSources());
2476
2477 // State queries.
2478 ASSERT_EQ(0, mDevice->getMetaState());
2479
2480 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2481 << "Ignored device should return unknown key code state.";
2482 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2483 << "Ignored device should return unknown scan code state.";
2484 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 0))
2485 << "Ignored device should return unknown switch state.";
2486
2487 const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
2488 uint8_t flags[2] = { 0, 1 };
2489 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, 2, keyCodes, flags))
2490 << "Ignored device should never mark any key codes.";
2491 ASSERT_EQ(0, flags[0]) << "Flag for unsupported key should be unchanged.";
2492 ASSERT_EQ(1, flags[1]) << "Flag for unsupported key should be unchanged.";
2493}
2494
2495TEST_F(InputDeviceTest, WhenMappersAreRegistered_DeviceIsNotIgnoredAndForwardsRequestsToMappers) {
2496 // Configuration.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002497 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, String8("key"), String8("value"));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002498
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002499 FakeInputMapper& mapper1 =
2500 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002501 mapper1.setKeyboardType(AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2502 mapper1.setMetaState(AMETA_ALT_ON);
2503 mapper1.addSupportedKeyCode(AKEYCODE_A);
2504 mapper1.addSupportedKeyCode(AKEYCODE_B);
2505 mapper1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
2506 mapper1.setKeyCodeState(AKEYCODE_B, AKEY_STATE_UP);
2507 mapper1.setScanCodeState(2, AKEY_STATE_DOWN);
2508 mapper1.setScanCodeState(3, AKEY_STATE_UP);
2509 mapper1.setSwitchState(4, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002510
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002511 FakeInputMapper& mapper2 =
2512 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002513 mapper2.setMetaState(AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002514
2515 InputReaderConfiguration config;
2516 mDevice->configure(ARBITRARY_TIME, &config, 0);
2517
2518 String8 propertyValue;
2519 ASSERT_TRUE(mDevice->getConfiguration().tryGetProperty(String8("key"), propertyValue))
2520 << "Device should have read configuration during configuration phase.";
2521 ASSERT_STREQ("value", propertyValue.string());
2522
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002523 ASSERT_NO_FATAL_FAILURE(mapper1.assertConfigureWasCalled());
2524 ASSERT_NO_FATAL_FAILURE(mapper2.assertConfigureWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002525
2526 // Reset
2527 mDevice->reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002528 ASSERT_NO_FATAL_FAILURE(mapper1.assertResetWasCalled());
2529 ASSERT_NO_FATAL_FAILURE(mapper2.assertResetWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002530
2531 NotifyDeviceResetArgs resetArgs;
2532 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2533 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2534 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2535
2536 // Metadata.
2537 ASSERT_FALSE(mDevice->isIgnored());
2538 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), mDevice->getSources());
2539
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002540 InputDeviceInfo info = mDevice->getDeviceInfo();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002541 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002542 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002543 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_ALPHABETIC, info.getKeyboardType());
2544 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), info.getSources());
2545
2546 // State queries.
2547 ASSERT_EQ(AMETA_ALT_ON | AMETA_SHIFT_ON, mDevice->getMetaState())
2548 << "Should query mappers and combine meta states.";
2549
2550 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2551 << "Should return unknown key code state when source not supported.";
2552 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2553 << "Should return unknown scan code state when source not supported.";
2554 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2555 << "Should return unknown switch state when source not supported.";
2556
2557 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, AKEYCODE_A))
2558 << "Should query mapper when source is supported.";
2559 ASSERT_EQ(AKEY_STATE_UP, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 3))
2560 << "Should query mapper when source is supported.";
2561 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 4))
2562 << "Should query mapper when source is supported.";
2563
2564 const int32_t keyCodes[4] = { AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2 };
2565 uint8_t flags[4] = { 0, 0, 0, 1 };
2566 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
2567 << "Should do nothing when source is unsupported.";
2568 ASSERT_EQ(0, flags[0]) << "Flag should be unchanged when source is unsupported.";
2569 ASSERT_EQ(0, flags[1]) << "Flag should be unchanged when source is unsupported.";
2570 ASSERT_EQ(0, flags[2]) << "Flag should be unchanged when source is unsupported.";
2571 ASSERT_EQ(1, flags[3]) << "Flag should be unchanged when source is unsupported.";
2572
2573 ASSERT_TRUE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, 4, keyCodes, flags))
2574 << "Should query mapper when source is supported.";
2575 ASSERT_EQ(1, flags[0]) << "Flag for supported key should be set.";
2576 ASSERT_EQ(1, flags[1]) << "Flag for supported key should be set.";
2577 ASSERT_EQ(0, flags[2]) << "Flag for unsupported key should be unchanged.";
2578 ASSERT_EQ(1, flags[3]) << "Flag for unsupported key should be unchanged.";
2579
2580 // Event handling.
2581 RawEvent event;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002582 event.deviceId = EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002583 mDevice->process(&event, 1);
2584
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002585 ASSERT_NO_FATAL_FAILURE(mapper1.assertProcessWasCalled());
2586 ASSERT_NO_FATAL_FAILURE(mapper2.assertProcessWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002587}
2588
Arthur Hung2c9a3342019-07-23 14:18:59 +08002589// A single input device is associated with a specific display. Check that:
2590// 1. Device is disabled if the viewport corresponding to the associated display is not found
2591// 2. Device is disabled when setEnabled API is called
2592TEST_F(InputDeviceTest, Configure_AssignsDisplayPort) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002593 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002594
2595 // First Configuration.
2596 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
2597
2598 // Device should be enabled by default.
2599 ASSERT_TRUE(mDevice->isEnabled());
2600
2601 // Prepare associated info.
2602 constexpr uint8_t hdmi = 1;
2603 const std::string UNIQUE_ID = "local:1";
2604
2605 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi);
2606 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2607 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2608 // Device should be disabled because it is associated with a specific display via
2609 // input port <-> display port association, but the corresponding display is not found
2610 ASSERT_FALSE(mDevice->isEnabled());
2611
2612 // Prepare displays.
2613 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002614 DISPLAY_ORIENTATION_0, true /*isActive*/, UNIQUE_ID, hdmi,
2615 ViewportType::INTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002616 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2617 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2618 ASSERT_TRUE(mDevice->isEnabled());
2619
2620 // Device should be disabled after set disable.
2621 mFakePolicy->addDisabledDevice(mDevice->getId());
2622 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2623 InputReaderConfiguration::CHANGE_ENABLED_STATE);
2624 ASSERT_FALSE(mDevice->isEnabled());
2625
2626 // Device should still be disabled even found the associated display.
2627 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2628 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2629 ASSERT_FALSE(mDevice->isEnabled());
2630}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002631
Christine Franks1ba71cc2021-04-07 14:37:42 -07002632TEST_F(InputDeviceTest, Configure_AssignsDisplayUniqueId) {
2633 // Device should be enabled by default.
2634 mFakePolicy->clearViewports();
2635 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
2636 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
2637 ASSERT_TRUE(mDevice->isEnabled());
2638
2639 // Device should be disabled because it is associated with a specific display, but the
2640 // corresponding display is not found.
2641 const std::string DISPLAY_UNIQUE_ID = "displayUniqueId";
2642 mFakePolicy->addInputUniqueIdAssociation(DEVICE_NAME, DISPLAY_UNIQUE_ID);
2643 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2644 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2645 ASSERT_FALSE(mDevice->isEnabled());
2646
2647 // Device should be enabled when a display is found.
2648 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2649 DISPLAY_ORIENTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
2650 NO_PORT, ViewportType::INTERNAL);
2651 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2652 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2653 ASSERT_TRUE(mDevice->isEnabled());
2654
2655 // Device should be disabled after set disable.
2656 mFakePolicy->addDisabledDevice(mDevice->getId());
2657 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2658 InputReaderConfiguration::CHANGE_ENABLED_STATE);
2659 ASSERT_FALSE(mDevice->isEnabled());
2660
2661 // Device should still be disabled even found the associated display.
2662 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2663 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2664 ASSERT_FALSE(mDevice->isEnabled());
2665}
2666
Michael Wrightd02c5b62014-02-10 15:10:22 -08002667// --- InputMapperTest ---
2668
2669class InputMapperTest : public testing::Test {
2670protected:
2671 static const char* DEVICE_NAME;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002672 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002673 static const int32_t DEVICE_ID;
2674 static const int32_t DEVICE_GENERATION;
2675 static const int32_t DEVICE_CONTROLLER_NUMBER;
Chris Ye1b0c7342020-07-28 21:57:03 -07002676 static const Flags<InputDeviceClass> DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002677 static const int32_t EVENTHUB_ID;
Prabir Pradhanc14266f2021-05-12 15:56:24 -07002678 static const std::optional<bool> INITIAL_PER_WINDOW_INPUT_ROTATION_FLAG_VALUE;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002679
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002680 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002681 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08002682 sp<TestInputListener> mFakeListener;
arthurhungdcef2dc2020-08-11 14:47:50 +08002683 std::unique_ptr<InstrumentedInputReader> mReader;
2684 std::shared_ptr<InputDevice> mDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002685
Chris Ye1b0c7342020-07-28 21:57:03 -07002686 virtual void SetUp(Flags<InputDeviceClass> classes) {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002687 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002688 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08002689 mFakeListener = new TestInputListener();
arthurhungdcef2dc2020-08-11 14:47:50 +08002690 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
2691 mFakeListener);
2692 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002693 }
2694
Prabir Pradhanc14266f2021-05-12 15:56:24 -07002695 void SetUp() override {
2696 // Ensure per_window_input_rotation is enabled.
2697 sysprop::InputFlingerProperties::per_window_input_rotation(true);
2698
2699 SetUp(DEVICE_CLASSES);
2700 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002701
Chris Yea52ade12020-08-27 16:49:20 -07002702 void TearDown() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002703 mFakeListener.clear();
2704 mFakePolicy.clear();
Prabir Pradhanc14266f2021-05-12 15:56:24 -07002705
2706 sysprop::InputFlingerProperties::per_window_input_rotation(
2707 INITIAL_PER_WINDOW_INPUT_ROTATION_FLAG_VALUE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002708 }
2709
2710 void addConfigurationProperty(const char* key, const char* value) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002711 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, String8(key), String8(value));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002712 }
2713
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002714 void configureDevice(uint32_t changes) {
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -08002715 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
arthurhungdcef2dc2020-08-11 14:47:50 +08002716 mReader->requestRefreshConfiguration(changes);
2717 mReader->loopOnce();
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -08002718 }
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002719 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
2720 }
2721
arthurhungdcef2dc2020-08-11 14:47:50 +08002722 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
2723 const std::string& location, int32_t eventHubId,
2724 Flags<InputDeviceClass> classes) {
2725 InputDeviceIdentifier identifier;
2726 identifier.name = name;
2727 identifier.location = location;
2728 std::shared_ptr<InputDevice> device =
2729 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
2730 identifier);
2731 mReader->pushNextDevice(device);
2732 mFakeEventHub->addDevice(eventHubId, name, classes);
2733 mReader->loopOnce();
2734 return device;
2735 }
2736
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002737 template <class T, typename... Args>
2738 T& addMapperAndConfigure(Args... args) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002739 T& mapper = mDevice->addMapper<T>(EVENTHUB_ID, args...);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002740 configureDevice(0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002741 mDevice->reset(ARBITRARY_TIME);
Chris Ye42b06822020-08-07 11:39:33 -07002742 mapper.reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002743 return mapper;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002744 }
2745
2746 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002747 int32_t orientation, const std::string& uniqueId,
2748 std::optional<uint8_t> physicalPort, ViewportType viewportType) {
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002749 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, true /*isActive*/,
2750 uniqueId, physicalPort, viewportType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07002751 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2752 }
2753
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002754 void clearViewports() {
2755 mFakePolicy->clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002756 }
2757
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002758 void process(InputMapper& mapper, nsecs_t when, nsecs_t readTime, int32_t type, int32_t code,
2759 int32_t value) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002760 RawEvent event;
2761 event.when = when;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002762 event.readTime = readTime;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002763 event.deviceId = mapper.getDeviceContext().getEventHubId();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002764 event.type = type;
2765 event.code = code;
2766 event.value = value;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002767 mapper.process(&event);
arthurhungdcef2dc2020-08-11 14:47:50 +08002768 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002769 }
2770
2771 static void assertMotionRange(const InputDeviceInfo& info,
2772 int32_t axis, uint32_t source, float min, float max, float flat, float fuzz) {
2773 const InputDeviceInfo::MotionRange* range = info.getMotionRange(axis, source);
Yi Kong9b14ac62018-07-17 13:48:38 -07002774 ASSERT_TRUE(range != nullptr) << "Axis: " << axis << " Source: " << source;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002775 ASSERT_EQ(axis, range->axis) << "Axis: " << axis << " Source: " << source;
2776 ASSERT_EQ(source, range->source) << "Axis: " << axis << " Source: " << source;
2777 ASSERT_NEAR(min, range->min, EPSILON) << "Axis: " << axis << " Source: " << source;
2778 ASSERT_NEAR(max, range->max, EPSILON) << "Axis: " << axis << " Source: " << source;
2779 ASSERT_NEAR(flat, range->flat, EPSILON) << "Axis: " << axis << " Source: " << source;
2780 ASSERT_NEAR(fuzz, range->fuzz, EPSILON) << "Axis: " << axis << " Source: " << source;
2781 }
2782
Prabir Pradhanf5334b82021-05-13 14:00:39 -07002783 static void assertPointerCoords(const PointerCoords& coords, float x, float y, float pressure,
2784 float size, float touchMajor, float touchMinor, float toolMajor,
2785 float toolMinor, float orientation, float distance,
2786 float scaledAxisEpsilon = 1.f) {
2787 ASSERT_NEAR(x, coords.getAxisValue(AMOTION_EVENT_AXIS_X), scaledAxisEpsilon);
2788 ASSERT_NEAR(y, coords.getAxisValue(AMOTION_EVENT_AXIS_Y), scaledAxisEpsilon);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002789 ASSERT_NEAR(pressure, coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), EPSILON);
2790 ASSERT_NEAR(size, coords.getAxisValue(AMOTION_EVENT_AXIS_SIZE), EPSILON);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07002791 ASSERT_NEAR(touchMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR),
2792 scaledAxisEpsilon);
2793 ASSERT_NEAR(touchMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR),
2794 scaledAxisEpsilon);
2795 ASSERT_NEAR(toolMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR),
2796 scaledAxisEpsilon);
2797 ASSERT_NEAR(toolMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR),
2798 scaledAxisEpsilon);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002799 ASSERT_NEAR(orientation, coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION), EPSILON);
2800 ASSERT_NEAR(distance, coords.getAxisValue(AMOTION_EVENT_AXIS_DISTANCE), EPSILON);
2801 }
2802
Michael Wright17db18e2020-06-26 20:51:44 +01002803 static void assertPosition(const FakePointerController& controller, float x, float y) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002804 float actualX, actualY;
Michael Wright17db18e2020-06-26 20:51:44 +01002805 controller.getPosition(&actualX, &actualY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002806 ASSERT_NEAR(x, actualX, 1);
2807 ASSERT_NEAR(y, actualY, 1);
2808 }
2809};
2810
2811const char* InputMapperTest::DEVICE_NAME = "device";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002812const char* InputMapperTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002813const int32_t InputMapperTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002814const int32_t InputMapperTest::DEVICE_GENERATION = 2;
2815const int32_t InputMapperTest::DEVICE_CONTROLLER_NUMBER = 0;
Chris Ye1b0c7342020-07-28 21:57:03 -07002816const Flags<InputDeviceClass> InputMapperTest::DEVICE_CLASSES =
2817 Flags<InputDeviceClass>(0); // not needed for current tests
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002818const int32_t InputMapperTest::EVENTHUB_ID = 1;
Prabir Pradhanc14266f2021-05-12 15:56:24 -07002819const std::optional<bool> InputMapperTest::INITIAL_PER_WINDOW_INPUT_ROTATION_FLAG_VALUE =
2820 sysprop::InputFlingerProperties::per_window_input_rotation();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002821
2822// --- SwitchInputMapperTest ---
2823
2824class SwitchInputMapperTest : public InputMapperTest {
2825protected:
2826};
2827
2828TEST_F(SwitchInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002829 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002830
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002831 ASSERT_EQ(uint32_t(AINPUT_SOURCE_SWITCH), mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002832}
2833
2834TEST_F(SwitchInputMapperTest, GetSwitchState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002835 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002836
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002837 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002838 ASSERT_EQ(1, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002839
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002840 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002841 ASSERT_EQ(0, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002842}
2843
2844TEST_F(SwitchInputMapperTest, Process) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002845 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002846
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002847 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_LID, 1);
2848 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_JACK_PHYSICAL_INSERT, 1);
2849 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_HEADPHONE_INSERT, 0);
2850 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002851
2852 NotifySwitchArgs args;
2853 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySwitchWasCalled(&args));
2854 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
Dan Albert1bd2fc02016-02-02 15:11:57 -08002855 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT), args.switchValues);
2856 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT) | (1 << SW_HEADPHONE_INSERT),
Michael Wrightd02c5b62014-02-10 15:10:22 -08002857 args.switchMask);
2858 ASSERT_EQ(uint32_t(0), args.policyFlags);
2859}
2860
Chris Ye87143712020-11-10 05:05:58 +00002861// --- VibratorInputMapperTest ---
2862class VibratorInputMapperTest : public InputMapperTest {
2863protected:
2864 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::VIBRATOR); }
2865};
2866
2867TEST_F(VibratorInputMapperTest, GetSources) {
2868 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
2869
2870 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mapper.getSources());
2871}
2872
2873TEST_F(VibratorInputMapperTest, GetVibratorIds) {
2874 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
2875
2876 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
2877}
2878
2879TEST_F(VibratorInputMapperTest, Vibrate) {
2880 constexpr uint8_t DEFAULT_AMPLITUDE = 192;
Chris Yefb552902021-02-03 17:18:37 -08002881 constexpr int32_t VIBRATION_TOKEN = 100;
Chris Ye87143712020-11-10 05:05:58 +00002882 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
2883
2884 VibrationElement pattern(2);
2885 VibrationSequence sequence(2);
2886 pattern.duration = std::chrono::milliseconds(200);
2887 pattern.channels = {{0 /* vibratorId */, DEFAULT_AMPLITUDE / 2},
2888 {1 /* vibratorId */, DEFAULT_AMPLITUDE}};
2889 sequence.addElement(pattern);
2890 pattern.duration = std::chrono::milliseconds(500);
2891 pattern.channels = {{0 /* vibratorId */, DEFAULT_AMPLITUDE / 4},
2892 {1 /* vibratorId */, DEFAULT_AMPLITUDE}};
2893 sequence.addElement(pattern);
2894
2895 std::vector<int64_t> timings = {0, 1};
2896 std::vector<uint8_t> amplitudes = {DEFAULT_AMPLITUDE, DEFAULT_AMPLITUDE / 2};
2897
2898 ASSERT_FALSE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08002899 // Start vibrating
2900 mapper.vibrate(sequence, -1 /* repeat */, VIBRATION_TOKEN);
Chris Ye87143712020-11-10 05:05:58 +00002901 ASSERT_TRUE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08002902 // Verify vibrator state listener was notified.
2903 mReader->loopOnce();
2904 NotifyVibratorStateArgs args;
2905 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyVibratorStateWasCalled(&args));
2906 ASSERT_EQ(DEVICE_ID, args.deviceId);
2907 ASSERT_TRUE(args.isOn);
2908 // Stop vibrating
2909 mapper.cancelVibrate(VIBRATION_TOKEN);
2910 ASSERT_FALSE(mapper.isVibrating());
2911 // Verify vibrator state listener was notified.
2912 mReader->loopOnce();
2913 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyVibratorStateWasCalled(&args));
2914 ASSERT_EQ(DEVICE_ID, args.deviceId);
2915 ASSERT_FALSE(args.isOn);
Chris Ye87143712020-11-10 05:05:58 +00002916}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002917
Chris Yef59a2f42020-10-16 12:55:26 -07002918// --- SensorInputMapperTest ---
2919
2920class SensorInputMapperTest : public InputMapperTest {
2921protected:
2922 static const int32_t ACCEL_RAW_MIN;
2923 static const int32_t ACCEL_RAW_MAX;
2924 static const int32_t ACCEL_RAW_FUZZ;
2925 static const int32_t ACCEL_RAW_FLAT;
2926 static const int32_t ACCEL_RAW_RESOLUTION;
2927
2928 static const int32_t GYRO_RAW_MIN;
2929 static const int32_t GYRO_RAW_MAX;
2930 static const int32_t GYRO_RAW_FUZZ;
2931 static const int32_t GYRO_RAW_FLAT;
2932 static const int32_t GYRO_RAW_RESOLUTION;
2933
2934 static const float GRAVITY_MS2_UNIT;
2935 static const float DEGREE_RADIAN_UNIT;
2936
2937 void prepareAccelAxes();
2938 void prepareGyroAxes();
2939 void setAccelProperties();
2940 void setGyroProperties();
2941 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::SENSOR); }
2942};
2943
2944const int32_t SensorInputMapperTest::ACCEL_RAW_MIN = -32768;
2945const int32_t SensorInputMapperTest::ACCEL_RAW_MAX = 32768;
2946const int32_t SensorInputMapperTest::ACCEL_RAW_FUZZ = 16;
2947const int32_t SensorInputMapperTest::ACCEL_RAW_FLAT = 0;
2948const int32_t SensorInputMapperTest::ACCEL_RAW_RESOLUTION = 8192;
2949
2950const int32_t SensorInputMapperTest::GYRO_RAW_MIN = -2097152;
2951const int32_t SensorInputMapperTest::GYRO_RAW_MAX = 2097152;
2952const int32_t SensorInputMapperTest::GYRO_RAW_FUZZ = 16;
2953const int32_t SensorInputMapperTest::GYRO_RAW_FLAT = 0;
2954const int32_t SensorInputMapperTest::GYRO_RAW_RESOLUTION = 1024;
2955
2956const float SensorInputMapperTest::GRAVITY_MS2_UNIT = 9.80665f;
2957const float SensorInputMapperTest::DEGREE_RADIAN_UNIT = 0.0174533f;
2958
2959void SensorInputMapperTest::prepareAccelAxes() {
2960 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
2961 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
2962 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
2963 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
2964 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Z, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
2965 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
2966}
2967
2968void SensorInputMapperTest::prepareGyroAxes() {
2969 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RX, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
2970 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
2971 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RY, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
2972 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
2973 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RZ, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
2974 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
2975}
2976
2977void SensorInputMapperTest::setAccelProperties() {
2978 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 0, InputDeviceSensorType::ACCELEROMETER,
2979 /* sensorDataIndex */ 0);
2980 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 1, InputDeviceSensorType::ACCELEROMETER,
2981 /* sensorDataIndex */ 1);
2982 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 2, InputDeviceSensorType::ACCELEROMETER,
2983 /* sensorDataIndex */ 2);
2984 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
2985 addConfigurationProperty("sensor.accelerometer.reportingMode", "0");
2986 addConfigurationProperty("sensor.accelerometer.maxDelay", "100000");
2987 addConfigurationProperty("sensor.accelerometer.minDelay", "5000");
2988 addConfigurationProperty("sensor.accelerometer.power", "1.5");
2989}
2990
2991void SensorInputMapperTest::setGyroProperties() {
2992 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 3, InputDeviceSensorType::GYROSCOPE,
2993 /* sensorDataIndex */ 0);
2994 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 4, InputDeviceSensorType::GYROSCOPE,
2995 /* sensorDataIndex */ 1);
2996 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 5, InputDeviceSensorType::GYROSCOPE,
2997 /* sensorDataIndex */ 2);
2998 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
2999 addConfigurationProperty("sensor.gyroscope.reportingMode", "0");
3000 addConfigurationProperty("sensor.gyroscope.maxDelay", "100000");
3001 addConfigurationProperty("sensor.gyroscope.minDelay", "5000");
3002 addConfigurationProperty("sensor.gyroscope.power", "0.8");
3003}
3004
3005TEST_F(SensorInputMapperTest, GetSources) {
3006 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3007
3008 ASSERT_EQ(static_cast<uint32_t>(AINPUT_SOURCE_SENSOR), mapper.getSources());
3009}
3010
3011TEST_F(SensorInputMapperTest, ProcessAccelerometerSensor) {
3012 setAccelProperties();
3013 prepareAccelAxes();
3014 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3015
3016 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::ACCELEROMETER,
3017 std::chrono::microseconds(10000),
3018 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08003019 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003020 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, 20000);
3021 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, -20000);
3022 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Z, 40000);
3023 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
3024 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07003025
3026 NotifySensorArgs args;
3027 std::vector<float> values = {20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
3028 -20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
3029 40000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT};
3030
3031 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
3032 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
3033 ASSERT_EQ(args.deviceId, DEVICE_ID);
3034 ASSERT_EQ(args.sensorType, InputDeviceSensorType::ACCELEROMETER);
3035 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
3036 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
3037 ASSERT_EQ(args.values, values);
3038 mapper.flushSensor(InputDeviceSensorType::ACCELEROMETER);
3039}
3040
3041TEST_F(SensorInputMapperTest, ProcessGyroscopeSensor) {
3042 setGyroProperties();
3043 prepareGyroAxes();
3044 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3045
3046 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::GYROSCOPE,
3047 std::chrono::microseconds(10000),
3048 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08003049 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003050 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RX, 20000);
3051 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RY, -20000);
3052 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RZ, 40000);
3053 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
3054 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07003055
3056 NotifySensorArgs args;
3057 std::vector<float> values = {20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
3058 -20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
3059 40000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT};
3060
3061 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
3062 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
3063 ASSERT_EQ(args.deviceId, DEVICE_ID);
3064 ASSERT_EQ(args.sensorType, InputDeviceSensorType::GYROSCOPE);
3065 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
3066 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
3067 ASSERT_EQ(args.values, values);
3068 mapper.flushSensor(InputDeviceSensorType::GYROSCOPE);
3069}
3070
Michael Wrightd02c5b62014-02-10 15:10:22 -08003071// --- KeyboardInputMapperTest ---
3072
3073class KeyboardInputMapperTest : public InputMapperTest {
3074protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003075 const std::string UNIQUE_ID = "local:0";
3076
3077 void prepareDisplay(int32_t orientation);
3078
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003079 void testDPadKeyRotation(KeyboardInputMapper& mapper, int32_t originalScanCode,
Arthur Hung2c9a3342019-07-23 14:18:59 +08003080 int32_t originalKeyCode, int32_t rotatedKeyCode,
3081 int32_t displayId = ADISPLAY_ID_NONE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003082};
3083
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003084/* Similar to setDisplayInfoAndReconfigure, but pre-populates all parameters except for the
3085 * orientation.
3086 */
3087void KeyboardInputMapperTest::prepareDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003088 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
3089 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003090}
3091
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003092void KeyboardInputMapperTest::testDPadKeyRotation(KeyboardInputMapper& mapper,
Arthur Hung2c9a3342019-07-23 14:18:59 +08003093 int32_t originalScanCode, int32_t originalKeyCode,
3094 int32_t rotatedKeyCode, int32_t displayId) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003095 NotifyKeyArgs args;
3096
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003097 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003098 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3099 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3100 ASSERT_EQ(originalScanCode, args.scanCode);
3101 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003102 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003103
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003104 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003105 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3106 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3107 ASSERT_EQ(originalScanCode, args.scanCode);
3108 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003109 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003110}
3111
Michael Wrightd02c5b62014-02-10 15:10:22 -08003112TEST_F(KeyboardInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003113 KeyboardInputMapper& mapper =
3114 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3115 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003116
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003117 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003118}
3119
3120TEST_F(KeyboardInputMapperTest, Process_SimpleKeyPress) {
3121 const int32_t USAGE_A = 0x070004;
3122 const int32_t USAGE_UNKNOWN = 0x07ffff;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003123 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3124 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
Chris Yea52ade12020-08-27 16:49:20 -07003125 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, POLICY_FLAG_WAKE);
3126 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, POLICY_FLAG_WAKE);
3127 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003128
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003129 KeyboardInputMapper& mapper =
3130 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3131 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
arthurhungc903df12020-08-11 15:08:42 +08003132 // Initial metastate to AMETA_NONE.
3133 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
3134 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003135
3136 // Key down by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003137 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003138 NotifyKeyArgs args;
3139 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3140 ASSERT_EQ(DEVICE_ID, args.deviceId);
3141 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3142 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3143 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3144 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3145 ASSERT_EQ(KEY_HOME, args.scanCode);
3146 ASSERT_EQ(AMETA_NONE, args.metaState);
3147 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3148 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3149 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3150
3151 // Key up by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003152 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003153 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3154 ASSERT_EQ(DEVICE_ID, args.deviceId);
3155 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3156 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3157 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3158 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3159 ASSERT_EQ(KEY_HOME, args.scanCode);
3160 ASSERT_EQ(AMETA_NONE, args.metaState);
3161 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3162 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3163 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3164
3165 // Key down by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003166 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3167 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, 0, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003168 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3169 ASSERT_EQ(DEVICE_ID, args.deviceId);
3170 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3171 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3172 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3173 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3174 ASSERT_EQ(0, args.scanCode);
3175 ASSERT_EQ(AMETA_NONE, args.metaState);
3176 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3177 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3178 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3179
3180 // Key up by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003181 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3182 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003183 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3184 ASSERT_EQ(DEVICE_ID, args.deviceId);
3185 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3186 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3187 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3188 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3189 ASSERT_EQ(0, args.scanCode);
3190 ASSERT_EQ(AMETA_NONE, args.metaState);
3191 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3192 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3193 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3194
3195 // Key down with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003196 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3197 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UNKNOWN, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003198 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3199 ASSERT_EQ(DEVICE_ID, args.deviceId);
3200 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3201 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3202 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3203 ASSERT_EQ(0, args.keyCode);
3204 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3205 ASSERT_EQ(AMETA_NONE, args.metaState);
3206 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3207 ASSERT_EQ(0U, args.policyFlags);
3208 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3209
3210 // Key up with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003211 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3212 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003213 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3214 ASSERT_EQ(DEVICE_ID, args.deviceId);
3215 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3216 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3217 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3218 ASSERT_EQ(0, args.keyCode);
3219 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3220 ASSERT_EQ(AMETA_NONE, args.metaState);
3221 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3222 ASSERT_EQ(0U, args.policyFlags);
3223 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3224}
3225
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003226/**
3227 * Ensure that the readTime is set to the time when the EV_KEY is received.
3228 */
3229TEST_F(KeyboardInputMapperTest, Process_SendsReadTime) {
3230 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3231
3232 KeyboardInputMapper& mapper =
3233 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3234 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3235 NotifyKeyArgs args;
3236
3237 // Key down
3238 process(mapper, ARBITRARY_TIME, 12 /*readTime*/, EV_KEY, KEY_HOME, 1);
3239 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3240 ASSERT_EQ(12, args.readTime);
3241
3242 // Key up
3243 process(mapper, ARBITRARY_TIME, 15 /*readTime*/, EV_KEY, KEY_HOME, 1);
3244 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3245 ASSERT_EQ(15, args.readTime);
3246}
3247
Michael Wrightd02c5b62014-02-10 15:10:22 -08003248TEST_F(KeyboardInputMapperTest, Process_ShouldUpdateMetaState) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003249 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFTSHIFT, 0, AKEYCODE_SHIFT_LEFT, 0);
3250 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003251 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, 0);
3252 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, 0);
3253 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003254
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003255 KeyboardInputMapper& mapper =
3256 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3257 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003258
arthurhungc903df12020-08-11 15:08:42 +08003259 // Initial metastate to AMETA_NONE.
3260 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
3261 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003262
3263 // Metakey down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003264 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003265 NotifyKeyArgs args;
3266 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3267 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003268 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003269 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003270
3271 // Key down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003272 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_A, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003273 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3274 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003275 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003276
3277 // Key up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003278 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, KEY_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003279 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3280 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003281 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003282
3283 // Metakey up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003284 process(mapper, ARBITRARY_TIME + 3, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003285 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3286 ASSERT_EQ(AMETA_NONE, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003287 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003288 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003289}
3290
3291TEST_F(KeyboardInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003292 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3293 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3294 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3295 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003296
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003297 KeyboardInputMapper& mapper =
3298 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3299 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003300
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003301 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003302 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3303 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
3304 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3305 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
3306 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3307 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
3308 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3309 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
3310}
3311
3312TEST_F(KeyboardInputMapperTest, Process_WhenOrientationAware_ShouldRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003313 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3314 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3315 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3316 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003317
Michael Wrightd02c5b62014-02-10 15:10:22 -08003318 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003319 KeyboardInputMapper& mapper =
3320 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3321 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003322
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003323 prepareDisplay(DISPLAY_ORIENTATION_0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003324 ASSERT_NO_FATAL_FAILURE(
3325 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3326 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3327 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3328 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3329 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3330 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3331 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003332
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003333 clearViewports();
3334 prepareDisplay(DISPLAY_ORIENTATION_90);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003335 ASSERT_NO_FATAL_FAILURE(
3336 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3337 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3338 AKEYCODE_DPAD_UP, DISPLAY_ID));
3339 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3340 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3341 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3342 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003343
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003344 clearViewports();
3345 prepareDisplay(DISPLAY_ORIENTATION_180);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003346 ASSERT_NO_FATAL_FAILURE(
3347 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3348 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3349 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3350 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3351 AKEYCODE_DPAD_UP, DISPLAY_ID));
3352 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3353 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003354
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003355 clearViewports();
3356 prepareDisplay(DISPLAY_ORIENTATION_270);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003357 ASSERT_NO_FATAL_FAILURE(
3358 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3359 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3360 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3361 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3362 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3363 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3364 AKEYCODE_DPAD_UP, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003365
3366 // Special case: if orientation changes while key is down, we still emit the same keycode
3367 // in the key up as we did in the key down.
3368 NotifyKeyArgs args;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003369 clearViewports();
3370 prepareDisplay(DISPLAY_ORIENTATION_270);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003371 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003372 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3373 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3374 ASSERT_EQ(KEY_UP, args.scanCode);
3375 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3376
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003377 clearViewports();
3378 prepareDisplay(DISPLAY_ORIENTATION_180);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003379 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003380 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3381 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3382 ASSERT_EQ(KEY_UP, args.scanCode);
3383 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3384}
3385
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003386TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_NotOrientationAware) {
3387 // If the keyboard is not orientation aware,
3388 // key events should not be associated with a specific display id
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003389 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003390
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003391 KeyboardInputMapper& mapper =
3392 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3393 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003394 NotifyKeyArgs args;
3395
3396 // Display id should be ADISPLAY_ID_NONE without any display configuration.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003397 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003398 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003399 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003400 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3401 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3402
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003403 prepareDisplay(DISPLAY_ORIENTATION_0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003404 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003405 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003406 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003407 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3408 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3409}
3410
3411TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_OrientationAware) {
3412 // If the keyboard is orientation aware,
3413 // key events should be associated with the internal viewport
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003414 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003415
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003416 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003417 KeyboardInputMapper& mapper =
3418 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3419 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003420 NotifyKeyArgs args;
3421
3422 // Display id should be ADISPLAY_ID_NONE without any display configuration.
3423 // ^--- already checked by the previous test
3424
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003425 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003426 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003427 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003428 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003429 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003430 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3431 ASSERT_EQ(DISPLAY_ID, args.displayId);
3432
3433 constexpr int32_t newDisplayId = 2;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003434 clearViewports();
3435 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003436 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003437 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003438 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003439 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003440 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3441 ASSERT_EQ(newDisplayId, args.displayId);
3442}
3443
Michael Wrightd02c5b62014-02-10 15:10:22 -08003444TEST_F(KeyboardInputMapperTest, GetKeyCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003445 KeyboardInputMapper& mapper =
3446 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3447 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003448
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003449 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003450 ASSERT_EQ(1, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003451
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003452 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003453 ASSERT_EQ(0, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003454}
3455
3456TEST_F(KeyboardInputMapperTest, GetScanCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003457 KeyboardInputMapper& mapper =
3458 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3459 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003460
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003461 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003462 ASSERT_EQ(1, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003463
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003464 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003465 ASSERT_EQ(0, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003466}
3467
3468TEST_F(KeyboardInputMapperTest, MarkSupportedKeyCodes) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003469 KeyboardInputMapper& mapper =
3470 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3471 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003472
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003473 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003474
3475 const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
3476 uint8_t flags[2] = { 0, 0 };
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003477 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, 1, keyCodes, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003478 ASSERT_TRUE(flags[0]);
3479 ASSERT_FALSE(flags[1]);
3480}
3481
3482TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleMetaStateAndLeds) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003483 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3484 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3485 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3486 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3487 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3488 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003489
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003490 KeyboardInputMapper& mapper =
3491 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3492 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Chris Yea52ade12020-08-27 16:49:20 -07003493 // Initialize metastate to AMETA_NUM_LOCK_ON.
arthurhungc903df12020-08-11 15:08:42 +08003494 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
3495 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003496
3497 // Initialization should have turned all of the lights off.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003498 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3499 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3500 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003501
3502 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003503 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3504 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003505 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3506 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3507 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003508 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003509
3510 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003511 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3512 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003513 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3514 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3515 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003516 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003517
3518 // Toggle caps lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003519 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3520 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003521 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3522 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3523 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003524 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003525
3526 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003527 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3528 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003529 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3530 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3531 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003532 ASSERT_EQ(AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003533
3534 // Toggle num lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003535 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3536 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003537 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3538 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3539 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003540 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003541
3542 // Toggle scroll lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003543 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3544 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003545 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3546 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3547 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003548 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003549}
3550
Chris Yea52ade12020-08-27 16:49:20 -07003551TEST_F(KeyboardInputMapperTest, NoMetaStateWhenMetaKeysNotPresent) {
3552 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_BUTTON_A, 0);
3553 mFakeEventHub->addKey(EVENTHUB_ID, BTN_B, 0, AKEYCODE_BUTTON_B, 0);
3554 mFakeEventHub->addKey(EVENTHUB_ID, BTN_X, 0, AKEYCODE_BUTTON_X, 0);
3555 mFakeEventHub->addKey(EVENTHUB_ID, BTN_Y, 0, AKEYCODE_BUTTON_Y, 0);
3556
3557 KeyboardInputMapper& mapper =
3558 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3559 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
3560
3561 // Initial metastate should be AMETA_NONE as no meta keys added.
3562 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3563 // Meta state should be AMETA_NONE after reset
3564 mapper.reset(ARBITRARY_TIME);
3565 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3566 // Meta state should be AMETA_NONE with update, as device doesn't have the keys.
3567 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
3568 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3569
3570 NotifyKeyArgs args;
3571 // Press button "A"
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003572 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_A, 1);
Chris Yea52ade12020-08-27 16:49:20 -07003573 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3574 ASSERT_EQ(AMETA_NONE, args.metaState);
3575 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3576 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3577 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3578
3579 // Button up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003580 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003581 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3582 ASSERT_EQ(AMETA_NONE, args.metaState);
3583 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3584 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3585 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3586}
3587
Arthur Hung2c9a3342019-07-23 14:18:59 +08003588TEST_F(KeyboardInputMapperTest, Configure_AssignsDisplayPort) {
3589 // keyboard 1.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003590 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3591 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3592 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3593 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003594
3595 // keyboard 2.
3596 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08003597 const std::string DEVICE_NAME2 = "KEYBOARD2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08003598 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003599 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08003600 std::shared_ptr<InputDevice> device2 =
3601 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
3602 Flags<InputDeviceClass>(0));
3603
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003604 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3605 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3606 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3607 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003608
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003609 KeyboardInputMapper& mapper =
3610 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3611 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003612
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003613 KeyboardInputMapper& mapper2 =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003614 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003615 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003616 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
3617 device2->reset(ARBITRARY_TIME);
3618
3619 // Prepared displays and associated info.
3620 constexpr uint8_t hdmi1 = 0;
3621 constexpr uint8_t hdmi2 = 1;
3622 const std::string SECONDARY_UNIQUE_ID = "local:1";
3623
3624 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
3625 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
3626
3627 // No associated display viewport found, should disable the device.
3628 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3629 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3630 ASSERT_FALSE(device2->isEnabled());
3631
3632 // Prepare second display.
3633 constexpr int32_t newDisplayId = 2;
3634 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003635 UNIQUE_ID, hdmi1, ViewportType::INTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003636 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003637 SECONDARY_UNIQUE_ID, hdmi2, ViewportType::EXTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003638 // Default device will reconfigure above, need additional reconfiguration for another device.
3639 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3640 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3641
3642 // Device should be enabled after the associated display is found.
3643 ASSERT_TRUE(mDevice->isEnabled());
3644 ASSERT_TRUE(device2->isEnabled());
3645
3646 // Test pad key events
3647 ASSERT_NO_FATAL_FAILURE(
3648 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3649 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3650 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3651 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3652 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3653 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3654 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3655
3656 ASSERT_NO_FATAL_FAILURE(
3657 testDPadKeyRotation(mapper2, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, newDisplayId));
3658 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3659 AKEYCODE_DPAD_RIGHT, newDisplayId));
3660 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3661 AKEYCODE_DPAD_DOWN, newDisplayId));
3662 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3663 AKEYCODE_DPAD_LEFT, newDisplayId));
3664}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003665
arthurhungc903df12020-08-11 15:08:42 +08003666TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleAfterReattach) {
3667 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3668 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3669 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3670 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3671 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3672 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3673
3674 KeyboardInputMapper& mapper =
3675 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3676 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3677 // Initial metastate to AMETA_NONE.
3678 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
3679 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
3680
3681 // Initialization should have turned all of the lights off.
3682 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3683 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3684 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3685
3686 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003687 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3688 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003689 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3690 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
3691
3692 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003693 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3694 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003695 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3696 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
3697
3698 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003699 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3700 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003701 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3702 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
3703
3704 mFakeEventHub->removeDevice(EVENTHUB_ID);
3705 mReader->loopOnce();
3706
3707 // keyboard 2 should default toggle keys.
3708 const std::string USB2 = "USB2";
3709 const std::string DEVICE_NAME2 = "KEYBOARD2";
3710 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
3711 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
3712 std::shared_ptr<InputDevice> device2 =
3713 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
3714 Flags<InputDeviceClass>(0));
3715 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3716 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
3717 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3718 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3719 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3720 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3721
arthurhung6fe95782020-10-05 22:41:16 +08003722 KeyboardInputMapper& mapper2 =
3723 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
3724 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
arthurhungc903df12020-08-11 15:08:42 +08003725 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
3726 device2->reset(ARBITRARY_TIME);
3727
3728 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_CAPSL));
3729 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_NUML));
3730 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_SCROLLL));
arthurhung6fe95782020-10-05 22:41:16 +08003731 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON,
3732 mapper2.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08003733}
3734
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003735// --- KeyboardInputMapperTest_ExternalDevice ---
3736
3737class KeyboardInputMapperTest_ExternalDevice : public InputMapperTest {
3738protected:
Chris Yea52ade12020-08-27 16:49:20 -07003739 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003740};
3741
3742TEST_F(KeyboardInputMapperTest_ExternalDevice, WakeBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07003743 // For external devices, non-media keys will trigger wake on key down. Media keys need to be
3744 // marked as WAKE in the keylayout file to trigger wake.
Powei Fengd041c5d2019-05-03 17:11:33 -07003745
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003746 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, 0);
3747 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, 0);
3748 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAYPAUSE, 0, AKEYCODE_MEDIA_PLAY_PAUSE,
3749 POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07003750
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003751 KeyboardInputMapper& mapper =
3752 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3753 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07003754
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003755 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003756 NotifyKeyArgs args;
3757 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3758 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3759
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003760 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003761 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3762 ASSERT_EQ(uint32_t(0), args.policyFlags);
3763
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003764 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003765 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3766 ASSERT_EQ(uint32_t(0), args.policyFlags);
3767
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003768 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003769 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3770 ASSERT_EQ(uint32_t(0), args.policyFlags);
3771
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003772 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003773 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3774 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3775
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003776 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003777 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3778 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3779}
3780
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003781TEST_F(KeyboardInputMapperTest_ExternalDevice, DoNotWakeByDefaultBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07003782 // Tv Remote key's wake behavior is prescribed by the keylayout file.
Powei Fengd041c5d2019-05-03 17:11:33 -07003783
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003784 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3785 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3786 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07003787
Powei Fengd041c5d2019-05-03 17:11:33 -07003788 addConfigurationProperty("keyboard.doNotWakeByDefault", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003789 KeyboardInputMapper& mapper =
3790 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3791 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07003792
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003793 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003794 NotifyKeyArgs args;
3795 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3796 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3797
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003798 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003799 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3800 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3801
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003802 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_DOWN, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003803 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3804 ASSERT_EQ(uint32_t(0), args.policyFlags);
3805
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003806 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_DOWN, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003807 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3808 ASSERT_EQ(uint32_t(0), args.policyFlags);
3809
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003810 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003811 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3812 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3813
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003814 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003815 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3816 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3817}
3818
Michael Wrightd02c5b62014-02-10 15:10:22 -08003819// --- CursorInputMapperTest ---
3820
3821class CursorInputMapperTest : public InputMapperTest {
3822protected:
3823 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD;
3824
Michael Wright17db18e2020-06-26 20:51:44 +01003825 std::shared_ptr<FakePointerController> mFakePointerController;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003826
Chris Yea52ade12020-08-27 16:49:20 -07003827 void SetUp() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003828 InputMapperTest::SetUp();
3829
Michael Wright17db18e2020-06-26 20:51:44 +01003830 mFakePointerController = std::make_shared<FakePointerController>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003831 mFakePolicy->setPointerController(mDevice->getId(), mFakePointerController);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003832 }
3833
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003834 void testMotionRotation(CursorInputMapper& mapper, int32_t originalX, int32_t originalY,
3835 int32_t rotatedX, int32_t rotatedY);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003836
3837 void prepareDisplay(int32_t orientation) {
3838 const std::string uniqueId = "local:0";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003839 const ViewportType viewportType = ViewportType::INTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003840 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
3841 orientation, uniqueId, NO_PORT, viewportType);
3842 }
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003843
3844 static void assertCursorPointerCoords(const PointerCoords& coords, float x, float y,
3845 float pressure) {
3846 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(coords, x, y, pressure, 0.0f, 0.0f, 0.0f, 0.0f,
3847 0.0f, 0.0f, 0.0f, EPSILON));
3848 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08003849};
3850
3851const int32_t CursorInputMapperTest::TRACKBALL_MOVEMENT_THRESHOLD = 6;
3852
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003853void CursorInputMapperTest::testMotionRotation(CursorInputMapper& mapper, int32_t originalX,
3854 int32_t originalY, int32_t rotatedX,
3855 int32_t rotatedY) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003856 NotifyMotionArgs args;
3857
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003858 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, originalX);
3859 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, originalY);
3860 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003861 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3862 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003863 ASSERT_NO_FATAL_FAILURE(
3864 assertCursorPointerCoords(args.pointerCoords[0],
3865 float(rotatedX) / TRACKBALL_MOVEMENT_THRESHOLD,
3866 float(rotatedY) / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003867}
3868
3869TEST_F(CursorInputMapperTest, WhenModeIsPointer_GetSources_ReturnsMouse) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003870 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003871 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003872
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003873 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003874}
3875
3876TEST_F(CursorInputMapperTest, WhenModeIsNavigation_GetSources_ReturnsTrackball) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003877 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003878 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003879
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003880 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003881}
3882
3883TEST_F(CursorInputMapperTest, WhenModeIsPointer_PopulateDeviceInfo_ReturnsRangeFromPointerController) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003884 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003885 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003886
3887 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003888 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003889
3890 // Initially there may not be a valid motion range.
Yi Kong9b14ac62018-07-17 13:48:38 -07003891 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE));
3892 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003893 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3894 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE, 0.0f, 1.0f, 0.0f, 0.0f));
3895
3896 // When the bounds are set, then there should be a valid motion range.
3897 mFakePointerController->setBounds(1, 2, 800 - 1, 480 - 1);
3898
3899 InputDeviceInfo info2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003900 mapper.populateDeviceInfo(&info2);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003901
3902 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3903 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE,
3904 1, 800 - 1, 0.0f, 0.0f));
3905 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3906 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE,
3907 2, 480 - 1, 0.0f, 0.0f));
3908 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3909 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE,
3910 0.0f, 1.0f, 0.0f, 0.0f));
3911}
3912
3913TEST_F(CursorInputMapperTest, WhenModeIsNavigation_PopulateDeviceInfo_ReturnsScaledRange) {
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
3917 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003918 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003919
3920 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3921 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_TRACKBALL,
3922 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
3923 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3924 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_TRACKBALL,
3925 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
3926 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3927 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TRACKBALL,
3928 0.0f, 1.0f, 0.0f, 0.0f));
3929}
3930
3931TEST_F(CursorInputMapperTest, Process_ShouldSetAllFieldsAndIncludeGlobalMetaState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003932 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003933 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003934
arthurhungdcef2dc2020-08-11 14:47:50 +08003935 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003936
3937 NotifyMotionArgs args;
3938
3939 // Button press.
3940 // Mostly testing non x/y behavior here so we don't need to check again elsewhere.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003941 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
3942 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003943 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3944 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3945 ASSERT_EQ(DEVICE_ID, args.deviceId);
3946 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3947 ASSERT_EQ(uint32_t(0), args.policyFlags);
3948 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3949 ASSERT_EQ(0, args.flags);
3950 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3951 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
3952 ASSERT_EQ(0, args.edgeFlags);
3953 ASSERT_EQ(uint32_t(1), args.pointerCount);
3954 ASSERT_EQ(0, args.pointerProperties[0].id);
3955 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003956 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003957 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3958 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3959 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3960
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003961 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3962 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3963 ASSERT_EQ(DEVICE_ID, args.deviceId);
3964 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3965 ASSERT_EQ(uint32_t(0), args.policyFlags);
3966 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
3967 ASSERT_EQ(0, args.flags);
3968 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3969 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
3970 ASSERT_EQ(0, args.edgeFlags);
3971 ASSERT_EQ(uint32_t(1), args.pointerCount);
3972 ASSERT_EQ(0, args.pointerProperties[0].id);
3973 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003974 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003975 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3976 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3977 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3978
Michael Wrightd02c5b62014-02-10 15:10:22 -08003979 // Button release. Should have same down time.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003980 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, BTN_MOUSE, 0);
3981 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003982 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3983 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3984 ASSERT_EQ(DEVICE_ID, args.deviceId);
3985 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3986 ASSERT_EQ(uint32_t(0), args.policyFlags);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003987 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
3988 ASSERT_EQ(0, args.flags);
3989 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3990 ASSERT_EQ(0, args.buttonState);
3991 ASSERT_EQ(0, args.edgeFlags);
3992 ASSERT_EQ(uint32_t(1), args.pointerCount);
3993 ASSERT_EQ(0, args.pointerProperties[0].id);
3994 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003995 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003996 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3997 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3998 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3999
4000 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4001 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
4002 ASSERT_EQ(DEVICE_ID, args.deviceId);
4003 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4004 ASSERT_EQ(uint32_t(0), args.policyFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004005 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
4006 ASSERT_EQ(0, args.flags);
4007 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4008 ASSERT_EQ(0, args.buttonState);
4009 ASSERT_EQ(0, args.edgeFlags);
4010 ASSERT_EQ(uint32_t(1), args.pointerCount);
4011 ASSERT_EQ(0, args.pointerProperties[0].id);
4012 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004013 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004014 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4015 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4016 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4017}
4018
4019TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentXYUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004020 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004021 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004022
4023 NotifyMotionArgs args;
4024
4025 // Motion in X but not Y.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004026 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4027 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004028 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4029 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004030 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4031 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f,
4032 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004033
4034 // Motion in Y but not X.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004035 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
4036 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004037 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4038 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004039 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f,
4040 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004041}
4042
4043TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004044 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004045 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004046
4047 NotifyMotionArgs args;
4048
4049 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004050 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4051 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004052 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4053 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004054 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004055
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004056 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4057 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004058 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004059
Michael Wrightd02c5b62014-02-10 15:10:22 -08004060 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004061 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4062 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004063 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004064 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004065 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004066
4067 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004068 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004069 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004070}
4071
4072TEST_F(CursorInputMapperTest, Process_ShouldHandleCombinedXYAndButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004073 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004074 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004075
4076 NotifyMotionArgs args;
4077
4078 // Combined X, Y and Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004079 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4080 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
4081 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4082 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004083 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4084 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004085 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4086 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4087 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004088
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004089 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4090 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004091 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4092 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4093 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004094
Michael Wrightd02c5b62014-02-10 15:10:22 -08004095 // Move X, Y a bit while pressed.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004096 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 2);
4097 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 1);
4098 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004099 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4100 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004101 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4102 2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4103 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004104
4105 // Release Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004106 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4107 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004108 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004109 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004110 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004111
4112 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004113 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004114 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004115}
4116
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004117TEST_F(CursorInputMapperTest, Process_WhenOrientationAware_ShouldNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004118 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004119 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
4120 // need to be rotated.
4121 addConfigurationProperty("cursor.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004122 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004123
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004124 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004125 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4126 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4127 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4128 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4129 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4130 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4131 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4132 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4133}
4134
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004135TEST_F(CursorInputMapperTest, Process_WhenNotOrientationAware_ShouldRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004136 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004137 // Since InputReader works in the un-rotated coordinate space, only devices that are not
4138 // orientation-aware are affected by display rotation.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004139 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004140
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004141 prepareDisplay(DISPLAY_ORIENTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004142 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4143 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4144 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4145 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4146 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4147 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4148 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4149 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4150
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004151 prepareDisplay(DISPLAY_ORIENTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004152 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, -1, 0));
4153 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, 1));
4154 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, 1));
4155 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, 1));
4156 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 1, 0));
4157 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, -1));
4158 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, -1));
4159 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, -1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004160
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004161 prepareDisplay(DISPLAY_ORIENTATION_180);
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
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004171 prepareDisplay(DISPLAY_ORIENTATION_270);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004172 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 1, 0));
4173 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, -1));
4174 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, -1));
4175 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, -1));
4176 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, -1, 0));
4177 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, 1));
4178 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, 1));
4179 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, 1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004180}
4181
4182TEST_F(CursorInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004183 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004184 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004185
4186 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4187 mFakePointerController->setPosition(100, 200);
4188 mFakePointerController->setButtonState(0);
4189
4190 NotifyMotionArgs motionArgs;
4191 NotifyKeyArgs keyArgs;
4192
4193 // press BTN_LEFT, release BTN_LEFT
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004194 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 1);
4195 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004196 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4197 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4198 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4199 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004200 ASSERT_NO_FATAL_FAILURE(
4201 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004202
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004203 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4204 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4205 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4206 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004207 ASSERT_NO_FATAL_FAILURE(
4208 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004209
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004210 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 0);
4211 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004212 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004213 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004214 ASSERT_EQ(0, motionArgs.buttonState);
4215 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004216 ASSERT_NO_FATAL_FAILURE(
4217 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004218
4219 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004220 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004221 ASSERT_EQ(0, motionArgs.buttonState);
4222 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004223 ASSERT_NO_FATAL_FAILURE(
4224 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004225
4226 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004227 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004228 ASSERT_EQ(0, motionArgs.buttonState);
4229 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004230 ASSERT_NO_FATAL_FAILURE(
4231 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004232
4233 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004234 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 1);
4235 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 1);
4236 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004237 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4238 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4239 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4240 motionArgs.buttonState);
4241 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4242 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004243 ASSERT_NO_FATAL_FAILURE(
4244 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004245
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004246 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4247 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4248 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4249 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4250 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004251 ASSERT_NO_FATAL_FAILURE(
4252 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004253
4254 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4255 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4256 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4257 motionArgs.buttonState);
4258 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4259 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004260 ASSERT_NO_FATAL_FAILURE(
4261 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004262
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004263 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 0);
4264 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004265 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004266 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004267 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4268 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004269 ASSERT_NO_FATAL_FAILURE(
4270 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004271
4272 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004273 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004274 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4275 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004276 ASSERT_NO_FATAL_FAILURE(
4277 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004278
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004279 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4280 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004281 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004282 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
4283 ASSERT_EQ(0, motionArgs.buttonState);
4284 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004285 ASSERT_NO_FATAL_FAILURE(
4286 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004287 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4288 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004289
4290 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004291 ASSERT_EQ(0, motionArgs.buttonState);
4292 ASSERT_EQ(0, mFakePointerController->getButtonState());
4293 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004294 ASSERT_NO_FATAL_FAILURE(
4295 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004296
Michael Wrightd02c5b62014-02-10 15:10:22 -08004297 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4298 ASSERT_EQ(0, motionArgs.buttonState);
4299 ASSERT_EQ(0, mFakePointerController->getButtonState());
4300 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004301 ASSERT_NO_FATAL_FAILURE(
4302 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004303
4304 // press BTN_BACK, release BTN_BACK
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004305 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 1);
4306 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004307 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4308 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4309 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004310
Michael Wrightd02c5b62014-02-10 15:10:22 -08004311 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004312 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004313 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4314 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004315 ASSERT_NO_FATAL_FAILURE(
4316 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004317
4318 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4319 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4320 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4321 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, 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));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004324
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004325 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 0);
4326 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004327 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004328 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004329 ASSERT_EQ(0, motionArgs.buttonState);
4330 ASSERT_EQ(0, mFakePointerController->getButtonState());
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
4334 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004335 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004336 ASSERT_EQ(0, motionArgs.buttonState);
4337 ASSERT_EQ(0, mFakePointerController->getButtonState());
4338
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004339 ASSERT_NO_FATAL_FAILURE(
4340 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004341 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4342 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4343 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4344
4345 // press BTN_SIDE, release BTN_SIDE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004346 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 1);
4347 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004348 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4349 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4350 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004351
Michael Wrightd02c5b62014-02-10 15:10:22 -08004352 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004353 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004354 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4355 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004356 ASSERT_NO_FATAL_FAILURE(
4357 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004358
4359 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4360 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4361 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4362 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004363 ASSERT_NO_FATAL_FAILURE(
4364 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004365
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004366 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 0);
4367 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004368 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004369 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004370 ASSERT_EQ(0, motionArgs.buttonState);
4371 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004372 ASSERT_NO_FATAL_FAILURE(
4373 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004374
4375 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4376 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4377 ASSERT_EQ(0, motionArgs.buttonState);
4378 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004379 ASSERT_NO_FATAL_FAILURE(
4380 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004381
Michael Wrightd02c5b62014-02-10 15:10:22 -08004382 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4383 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4384 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4385
4386 // press BTN_FORWARD, release BTN_FORWARD
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004387 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 1);
4388 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004389 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4390 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4391 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004392
Michael Wrightd02c5b62014-02-10 15:10:22 -08004393 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004394 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004395 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4396 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004397 ASSERT_NO_FATAL_FAILURE(
4398 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004399
4400 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4401 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4402 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4403 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004404 ASSERT_NO_FATAL_FAILURE(
4405 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004406
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004407 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 0);
4408 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004409 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004410 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004411 ASSERT_EQ(0, motionArgs.buttonState);
4412 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004413 ASSERT_NO_FATAL_FAILURE(
4414 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004415
4416 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4417 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4418 ASSERT_EQ(0, motionArgs.buttonState);
4419 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004420 ASSERT_NO_FATAL_FAILURE(
4421 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004422
Michael Wrightd02c5b62014-02-10 15:10:22 -08004423 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4424 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4425 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4426
4427 // press BTN_EXTRA, release BTN_EXTRA
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004428 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 1);
4429 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004430 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4431 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4432 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004433
Michael Wrightd02c5b62014-02-10 15:10:22 -08004434 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004435 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004436 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4437 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004438 ASSERT_NO_FATAL_FAILURE(
4439 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004440
4441 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4442 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4443 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4444 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004445 ASSERT_NO_FATAL_FAILURE(
4446 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004447
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004448 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 0);
4449 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004450 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004451 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004452 ASSERT_EQ(0, motionArgs.buttonState);
4453 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004454 ASSERT_NO_FATAL_FAILURE(
4455 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004456
4457 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4458 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4459 ASSERT_EQ(0, motionArgs.buttonState);
4460 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004461 ASSERT_NO_FATAL_FAILURE(
4462 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004463
Michael Wrightd02c5b62014-02-10 15:10:22 -08004464 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4465 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4466 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4467}
4468
4469TEST_F(CursorInputMapperTest, Process_WhenModeIsPointer_ShouldMoveThePointerAround) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004470 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004471 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004472
4473 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4474 mFakePointerController->setPosition(100, 200);
4475 mFakePointerController->setButtonState(0);
4476
4477 NotifyMotionArgs args;
4478
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004479 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4480 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4481 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004482 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004483 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4484 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4485 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4486 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 +01004487 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004488}
4489
4490TEST_F(CursorInputMapperTest, Process_PointerCapture) {
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004491 addConfigurationProperty("cursor.mode", "pointer");
4492 mFakePolicy->setPointerCapture(true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004493 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004494
4495 NotifyDeviceResetArgs resetArgs;
4496 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4497 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4498 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4499
4500 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4501 mFakePointerController->setPosition(100, 200);
4502 mFakePointerController->setButtonState(0);
4503
4504 NotifyMotionArgs args;
4505
4506 // Move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004507 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4508 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4509 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004510 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4511 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4512 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4513 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4514 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 +01004515 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004516
4517 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004518 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4519 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004520 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4521 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4522 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
4523 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4524 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4525 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4526 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4527 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
4528 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4529 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4530
4531 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004532 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4533 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004534 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4535 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4536 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
4537 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4538 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4539 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4540 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4541 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
4542 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4543 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4544
4545 // Another move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004546 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 30);
4547 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 40);
4548 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004549 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4550 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4551 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4552 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4553 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 +01004554 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004555
4556 // Disable pointer capture and check that the device generation got bumped
4557 // and events are generated the usual way.
arthurhungdcef2dc2020-08-11 14:47:50 +08004558 const uint32_t generation = mReader->getContext()->getGeneration();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004559 mFakePolicy->setPointerCapture(false);
4560 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
arthurhungdcef2dc2020-08-11 14:47:50 +08004561 ASSERT_TRUE(mReader->getContext()->getGeneration() != generation);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004562
4563 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4564 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4565 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4566
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004567 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4568 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4569 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004570 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4571 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004572 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4573 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4574 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 +01004575 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004576}
4577
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004578TEST_F(CursorInputMapperTest, Process_ShouldHandleDisplayId) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004579 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004580
Garfield Tan888a6a42020-01-09 11:39:16 -08004581 // Setup for second display.
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004582 constexpr int32_t SECOND_DISPLAY_ID = 1;
Garfield Tan888a6a42020-01-09 11:39:16 -08004583 const std::string SECOND_DISPLAY_UNIQUE_ID = "local:1";
4584 mFakePolicy->addDisplayViewport(SECOND_DISPLAY_ID, 800, 480, DISPLAY_ORIENTATION_0,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00004585 true /*isActive*/, SECOND_DISPLAY_UNIQUE_ID, NO_PORT,
4586 ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08004587 mFakePolicy->setDefaultPointerDisplayId(SECOND_DISPLAY_ID);
4588 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
4589
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004590 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4591 mFakePointerController->setPosition(100, 200);
4592 mFakePointerController->setButtonState(0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004593
4594 NotifyMotionArgs args;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004595 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4596 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4597 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004598 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4599 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4600 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4601 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4602 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 +01004603 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004604 ASSERT_EQ(SECOND_DISPLAY_ID, args.displayId);
4605}
4606
Michael Wrightd02c5b62014-02-10 15:10:22 -08004607// --- TouchInputMapperTest ---
4608
4609class TouchInputMapperTest : public InputMapperTest {
4610protected:
4611 static const int32_t RAW_X_MIN;
4612 static const int32_t RAW_X_MAX;
4613 static const int32_t RAW_Y_MIN;
4614 static const int32_t RAW_Y_MAX;
4615 static const int32_t RAW_TOUCH_MIN;
4616 static const int32_t RAW_TOUCH_MAX;
4617 static const int32_t RAW_TOOL_MIN;
4618 static const int32_t RAW_TOOL_MAX;
4619 static const int32_t RAW_PRESSURE_MIN;
4620 static const int32_t RAW_PRESSURE_MAX;
4621 static const int32_t RAW_ORIENTATION_MIN;
4622 static const int32_t RAW_ORIENTATION_MAX;
4623 static const int32_t RAW_DISTANCE_MIN;
4624 static const int32_t RAW_DISTANCE_MAX;
4625 static const int32_t RAW_TILT_MIN;
4626 static const int32_t RAW_TILT_MAX;
4627 static const int32_t RAW_ID_MIN;
4628 static const int32_t RAW_ID_MAX;
4629 static const int32_t RAW_SLOT_MIN;
4630 static const int32_t RAW_SLOT_MAX;
4631 static const float X_PRECISION;
4632 static const float Y_PRECISION;
Santos Cordonfa5cf462017-04-05 10:37:00 -07004633 static const float X_PRECISION_VIRTUAL;
4634 static const float Y_PRECISION_VIRTUAL;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004635
4636 static const float GEOMETRIC_SCALE;
Jason Gerecke489fda82012-09-07 17:19:40 -07004637 static const TouchAffineTransformation AFFINE_TRANSFORM;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004638
4639 static const VirtualKeyDefinition VIRTUAL_KEYS[2];
4640
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004641 const std::string UNIQUE_ID = "local:0";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004642 const std::string SECONDARY_UNIQUE_ID = "local:1";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004643
Michael Wrightd02c5b62014-02-10 15:10:22 -08004644 enum Axes {
4645 POSITION = 1 << 0,
4646 TOUCH = 1 << 1,
4647 TOOL = 1 << 2,
4648 PRESSURE = 1 << 3,
4649 ORIENTATION = 1 << 4,
4650 MINOR = 1 << 5,
4651 ID = 1 << 6,
4652 DISTANCE = 1 << 7,
4653 TILT = 1 << 8,
4654 SLOT = 1 << 9,
4655 TOOL_TYPE = 1 << 10,
4656 };
4657
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004658 void prepareDisplay(int32_t orientation, std::optional<uint8_t> port = NO_PORT);
4659 void prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port = NO_PORT);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004660 void prepareVirtualDisplay(int32_t orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004661 void prepareVirtualKeys();
Jason Gerecke489fda82012-09-07 17:19:40 -07004662 void prepareLocationCalibration();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004663 int32_t toRawX(float displayX);
4664 int32_t toRawY(float displayY);
Prabir Pradhan3b134cc2021-08-20 16:09:32 -07004665 int32_t toRotatedRawX(float displayX);
4666 int32_t toRotatedRawY(float displayY);
Jason Gerecke489fda82012-09-07 17:19:40 -07004667 float toCookedX(float rawX, float rawY);
4668 float toCookedY(float rawX, float rawY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004669 float toDisplayX(int32_t rawX);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004670 float toDisplayX(int32_t rawX, int32_t displayWidth);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004671 float toDisplayY(int32_t rawY);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004672 float toDisplayY(int32_t rawY, int32_t displayHeight);
4673
Michael Wrightd02c5b62014-02-10 15:10:22 -08004674};
4675
4676const int32_t TouchInputMapperTest::RAW_X_MIN = 25;
4677const int32_t TouchInputMapperTest::RAW_X_MAX = 1019;
4678const int32_t TouchInputMapperTest::RAW_Y_MIN = 30;
4679const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009;
4680const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0;
4681const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31;
4682const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0;
4683const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15;
Michael Wrightaa449c92017-12-13 21:21:43 +00004684const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = 0;
4685const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = 255;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004686const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7;
4687const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
4688const int32_t TouchInputMapperTest::RAW_DISTANCE_MIN = 0;
4689const int32_t TouchInputMapperTest::RAW_DISTANCE_MAX = 7;
4690const int32_t TouchInputMapperTest::RAW_TILT_MIN = 0;
4691const int32_t TouchInputMapperTest::RAW_TILT_MAX = 150;
4692const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
4693const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
4694const int32_t TouchInputMapperTest::RAW_SLOT_MIN = 0;
4695const int32_t TouchInputMapperTest::RAW_SLOT_MAX = 9;
4696const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH;
4697const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT;
Santos Cordonfa5cf462017-04-05 10:37:00 -07004698const float TouchInputMapperTest::X_PRECISION_VIRTUAL =
4699 float(RAW_X_MAX - RAW_X_MIN + 1) / VIRTUAL_DISPLAY_WIDTH;
4700const float TouchInputMapperTest::Y_PRECISION_VIRTUAL =
4701 float(RAW_Y_MAX - RAW_Y_MIN + 1) / VIRTUAL_DISPLAY_HEIGHT;
Jason Gerecke489fda82012-09-07 17:19:40 -07004702const TouchAffineTransformation TouchInputMapperTest::AFFINE_TRANSFORM =
4703 TouchAffineTransformation(1, -2, 3, -4, 5, -6);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004704
4705const float TouchInputMapperTest::GEOMETRIC_SCALE =
4706 avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1),
4707 float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1));
4708
4709const VirtualKeyDefinition TouchInputMapperTest::VIRTUAL_KEYS[2] = {
4710 { KEY_HOME, 60, DISPLAY_HEIGHT + 15, 20, 20 },
4711 { KEY_MENU, DISPLAY_HEIGHT - 60, DISPLAY_WIDTH + 15, 20, 20 },
4712};
4713
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004714void TouchInputMapperTest::prepareDisplay(int32_t orientation, std::optional<uint8_t> port) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004715 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
4716 port, ViewportType::INTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004717}
4718
4719void TouchInputMapperTest::prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port) {
4720 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
4721 DISPLAY_ORIENTATION_0, SECONDARY_UNIQUE_ID, port, type);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004722}
4723
Santos Cordonfa5cf462017-04-05 10:37:00 -07004724void TouchInputMapperTest::prepareVirtualDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004725 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH, VIRTUAL_DISPLAY_HEIGHT,
4726 orientation, VIRTUAL_DISPLAY_UNIQUE_ID, NO_PORT,
4727 ViewportType::VIRTUAL);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004728}
4729
Michael Wrightd02c5b62014-02-10 15:10:22 -08004730void TouchInputMapperTest::prepareVirtualKeys() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004731 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[0]);
4732 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[1]);
4733 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4734 mFakeEventHub->addKey(EVENTHUB_ID, KEY_MENU, 0, AKEYCODE_MENU, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004735}
4736
Jason Gerecke489fda82012-09-07 17:19:40 -07004737void TouchInputMapperTest::prepareLocationCalibration() {
4738 mFakePolicy->setTouchAffineTransformation(AFFINE_TRANSFORM);
4739}
4740
Michael Wrightd02c5b62014-02-10 15:10:22 -08004741int32_t TouchInputMapperTest::toRawX(float displayX) {
4742 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN);
4743}
4744
4745int32_t TouchInputMapperTest::toRawY(float displayY) {
4746 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN);
4747}
4748
Prabir Pradhan3b134cc2021-08-20 16:09:32 -07004749int32_t TouchInputMapperTest::toRotatedRawX(float displayX) {
4750 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_HEIGHT + RAW_X_MIN);
4751}
4752
4753int32_t TouchInputMapperTest::toRotatedRawY(float displayY) {
4754 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_WIDTH + RAW_Y_MIN);
4755}
4756
Jason Gerecke489fda82012-09-07 17:19:40 -07004757float TouchInputMapperTest::toCookedX(float rawX, float rawY) {
4758 AFFINE_TRANSFORM.applyTo(rawX, rawY);
4759 return rawX;
4760}
4761
4762float TouchInputMapperTest::toCookedY(float rawX, float rawY) {
4763 AFFINE_TRANSFORM.applyTo(rawX, rawY);
4764 return rawY;
4765}
4766
Michael Wrightd02c5b62014-02-10 15:10:22 -08004767float TouchInputMapperTest::toDisplayX(int32_t rawX) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004768 return toDisplayX(rawX, DISPLAY_WIDTH);
4769}
4770
4771float TouchInputMapperTest::toDisplayX(int32_t rawX, int32_t displayWidth) {
4772 return float(rawX - RAW_X_MIN) * displayWidth / (RAW_X_MAX - RAW_X_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004773}
4774
4775float TouchInputMapperTest::toDisplayY(int32_t rawY) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004776 return toDisplayY(rawY, DISPLAY_HEIGHT);
4777}
4778
4779float TouchInputMapperTest::toDisplayY(int32_t rawY, int32_t displayHeight) {
4780 return float(rawY - RAW_Y_MIN) * displayHeight / (RAW_Y_MAX - RAW_Y_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004781}
4782
4783
4784// --- SingleTouchInputMapperTest ---
4785
4786class SingleTouchInputMapperTest : public TouchInputMapperTest {
4787protected:
4788 void prepareButtons();
4789 void prepareAxes(int axes);
4790
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004791 void processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
4792 void processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
4793 void processUp(SingleTouchInputMapper& mappery);
4794 void processPressure(SingleTouchInputMapper& mapper, int32_t pressure);
4795 void processToolMajor(SingleTouchInputMapper& mapper, int32_t toolMajor);
4796 void processDistance(SingleTouchInputMapper& mapper, int32_t distance);
4797 void processTilt(SingleTouchInputMapper& mapper, int32_t tiltX, int32_t tiltY);
4798 void processKey(SingleTouchInputMapper& mapper, int32_t code, int32_t value);
4799 void processSync(SingleTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004800};
4801
4802void SingleTouchInputMapperTest::prepareButtons() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004803 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004804}
4805
4806void SingleTouchInputMapperTest::prepareAxes(int axes) {
4807 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004808 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
4809 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004810 }
4811 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004812 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MIN,
4813 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004814 }
4815 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004816 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TOOL_WIDTH, RAW_TOOL_MIN, RAW_TOOL_MAX, 0,
4817 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004818 }
4819 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004820 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_DISTANCE, RAW_DISTANCE_MIN,
4821 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004822 }
4823 if (axes & TILT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004824 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_X, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
4825 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_Y, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004826 }
4827}
4828
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004829void SingleTouchInputMapperTest::processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004830 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
4831 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
4832 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004833}
4834
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004835void SingleTouchInputMapperTest::processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004836 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
4837 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004838}
4839
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004840void SingleTouchInputMapperTest::processUp(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004841 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004842}
4843
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004844void SingleTouchInputMapperTest::processPressure(SingleTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004845 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004846}
4847
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004848void SingleTouchInputMapperTest::processToolMajor(SingleTouchInputMapper& mapper,
4849 int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004850 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TOOL_WIDTH, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004851}
4852
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004853void SingleTouchInputMapperTest::processDistance(SingleTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004854 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004855}
4856
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004857void SingleTouchInputMapperTest::processTilt(SingleTouchInputMapper& mapper, int32_t tiltX,
4858 int32_t tiltY) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004859 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_X, tiltX);
4860 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_Y, tiltY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004861}
4862
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004863void SingleTouchInputMapperTest::processKey(SingleTouchInputMapper& mapper, int32_t code,
4864 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004865 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004866}
4867
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004868void SingleTouchInputMapperTest::processSync(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004869 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004870}
4871
Michael Wrightd02c5b62014-02-10 15:10:22 -08004872TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004873 prepareButtons();
4874 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004875 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004876
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004877 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004878}
4879
4880TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndIsACursor_ReturnsTouchPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004881 mFakeEventHub->addRelativeAxis(EVENTHUB_ID, REL_X);
4882 mFakeEventHub->addRelativeAxis(EVENTHUB_ID, REL_Y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004883 prepareButtons();
4884 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004885 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004886
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004887 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004888}
4889
4890TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchPad_ReturnsTouchPad) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004891 prepareButtons();
4892 prepareAxes(POSITION);
4893 addConfigurationProperty("touch.deviceType", "touchPad");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004894 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004895
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004896 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004897}
4898
4899TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004900 prepareButtons();
4901 prepareAxes(POSITION);
4902 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004903 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004904
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004905 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004906}
4907
4908TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004909 addConfigurationProperty("touch.deviceType", "touchScreen");
4910 prepareDisplay(DISPLAY_ORIENTATION_0);
4911 prepareButtons();
4912 prepareAxes(POSITION);
4913 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004914 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004915
4916 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004917 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004918
4919 // Virtual key is down.
4920 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4921 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4922 processDown(mapper, x, y);
4923 processSync(mapper);
4924 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4925
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004926 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004927
4928 // Virtual key is up.
4929 processUp(mapper);
4930 processSync(mapper);
4931 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4932
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004933 ASSERT_EQ(AKEY_STATE_UP, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004934}
4935
4936TEST_F(SingleTouchInputMapperTest, GetScanCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004937 addConfigurationProperty("touch.deviceType", "touchScreen");
4938 prepareDisplay(DISPLAY_ORIENTATION_0);
4939 prepareButtons();
4940 prepareAxes(POSITION);
4941 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004942 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004943
4944 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004945 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004946
4947 // Virtual key is down.
4948 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4949 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4950 processDown(mapper, x, y);
4951 processSync(mapper);
4952 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4953
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004954 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004955
4956 // Virtual key is up.
4957 processUp(mapper);
4958 processSync(mapper);
4959 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4960
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004961 ASSERT_EQ(AKEY_STATE_UP, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004962}
4963
4964TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004965 addConfigurationProperty("touch.deviceType", "touchScreen");
4966 prepareDisplay(DISPLAY_ORIENTATION_0);
4967 prepareButtons();
4968 prepareAxes(POSITION);
4969 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004970 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004971
4972 const int32_t keys[2] = { AKEYCODE_HOME, AKEYCODE_A };
4973 uint8_t flags[2] = { 0, 0 };
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004974 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, 2, keys, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004975 ASSERT_TRUE(flags[0]);
4976 ASSERT_FALSE(flags[1]);
4977}
4978
4979TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004980 addConfigurationProperty("touch.deviceType", "touchScreen");
4981 prepareDisplay(DISPLAY_ORIENTATION_0);
4982 prepareButtons();
4983 prepareAxes(POSITION);
4984 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004985 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004986
arthurhungdcef2dc2020-08-11 14:47:50 +08004987 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004988
4989 NotifyKeyArgs args;
4990
4991 // Press virtual key.
4992 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4993 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4994 processDown(mapper, x, y);
4995 processSync(mapper);
4996
4997 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4998 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4999 ASSERT_EQ(DEVICE_ID, args.deviceId);
5000 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
5001 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
5002 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
5003 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5004 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5005 ASSERT_EQ(KEY_HOME, args.scanCode);
5006 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5007 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5008
5009 // Release virtual key.
5010 processUp(mapper);
5011 processSync(mapper);
5012
5013 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
5014 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5015 ASSERT_EQ(DEVICE_ID, args.deviceId);
5016 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
5017 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
5018 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
5019 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5020 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5021 ASSERT_EQ(KEY_HOME, args.scanCode);
5022 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5023 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5024
5025 // Should not have sent any motions.
5026 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5027}
5028
5029TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005030 addConfigurationProperty("touch.deviceType", "touchScreen");
5031 prepareDisplay(DISPLAY_ORIENTATION_0);
5032 prepareButtons();
5033 prepareAxes(POSITION);
5034 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005035 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005036
arthurhungdcef2dc2020-08-11 14:47:50 +08005037 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005038
5039 NotifyKeyArgs keyArgs;
5040
5041 // Press virtual key.
5042 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5043 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5044 processDown(mapper, x, y);
5045 processSync(mapper);
5046
5047 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5048 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5049 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5050 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5051 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5052 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5053 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags);
5054 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5055 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5056 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5057 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5058
5059 // Move out of bounds. This should generate a cancel and a pointer down since we moved
5060 // into the display area.
5061 y -= 100;
5062 processMove(mapper, x, y);
5063 processSync(mapper);
5064
5065 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5066 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5067 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5068 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5069 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5070 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5071 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
5072 | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags);
5073 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5074 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5075 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5076 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5077
5078 NotifyMotionArgs motionArgs;
5079 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5080 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5081 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5082 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5083 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5084 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5085 ASSERT_EQ(0, motionArgs.flags);
5086 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5087 ASSERT_EQ(0, motionArgs.buttonState);
5088 ASSERT_EQ(0, motionArgs.edgeFlags);
5089 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5090 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5091 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5092 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5093 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5094 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5095 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5096 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5097
5098 // Keep moving out of bounds. Should generate a pointer move.
5099 y -= 50;
5100 processMove(mapper, x, y);
5101 processSync(mapper);
5102
5103 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5104 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5105 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5106 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5107 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5108 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5109 ASSERT_EQ(0, motionArgs.flags);
5110 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5111 ASSERT_EQ(0, motionArgs.buttonState);
5112 ASSERT_EQ(0, motionArgs.edgeFlags);
5113 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5114 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5115 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5116 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5117 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5118 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5119 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5120 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5121
5122 // Release out of bounds. Should generate a pointer up.
5123 processUp(mapper);
5124 processSync(mapper);
5125
5126 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5127 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5128 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5129 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5130 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5131 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5132 ASSERT_EQ(0, motionArgs.flags);
5133 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5134 ASSERT_EQ(0, motionArgs.buttonState);
5135 ASSERT_EQ(0, motionArgs.edgeFlags);
5136 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5137 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5138 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5139 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5140 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5141 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5142 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5143 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5144
5145 // Should not have sent any more keys or motions.
5146 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5147 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5148}
5149
5150TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005151 addConfigurationProperty("touch.deviceType", "touchScreen");
5152 prepareDisplay(DISPLAY_ORIENTATION_0);
5153 prepareButtons();
5154 prepareAxes(POSITION);
5155 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005156 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005157
arthurhungdcef2dc2020-08-11 14:47:50 +08005158 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005159
5160 NotifyMotionArgs motionArgs;
5161
5162 // Initially go down out of bounds.
5163 int32_t x = -10;
5164 int32_t y = -10;
5165 processDown(mapper, x, y);
5166 processSync(mapper);
5167
5168 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5169
5170 // Move into the display area. Should generate a pointer down.
5171 x = 50;
5172 y = 75;
5173 processMove(mapper, x, y);
5174 processSync(mapper);
5175
5176 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5177 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5178 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5179 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5180 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5181 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5182 ASSERT_EQ(0, motionArgs.flags);
5183 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5184 ASSERT_EQ(0, motionArgs.buttonState);
5185 ASSERT_EQ(0, motionArgs.edgeFlags);
5186 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5187 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5188 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5189 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5190 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5191 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5192 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5193 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5194
5195 // Release. Should generate a pointer up.
5196 processUp(mapper);
5197 processSync(mapper);
5198
5199 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5200 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5201 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5202 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5203 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5204 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5205 ASSERT_EQ(0, motionArgs.flags);
5206 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5207 ASSERT_EQ(0, motionArgs.buttonState);
5208 ASSERT_EQ(0, motionArgs.edgeFlags);
5209 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5210 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5211 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5212 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5213 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5214 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5215 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5216 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5217
5218 // Should not have sent any more keys or motions.
5219 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5220 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5221}
5222
Santos Cordonfa5cf462017-04-05 10:37:00 -07005223TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture_VirtualDisplay) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005224 addConfigurationProperty("touch.deviceType", "touchScreen");
5225 addConfigurationProperty("touch.displayId", VIRTUAL_DISPLAY_UNIQUE_ID);
5226
5227 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
5228 prepareButtons();
5229 prepareAxes(POSITION);
5230 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005231 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Santos Cordonfa5cf462017-04-05 10:37:00 -07005232
arthurhungdcef2dc2020-08-11 14:47:50 +08005233 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005234
5235 NotifyMotionArgs motionArgs;
5236
5237 // Down.
5238 int32_t x = 100;
5239 int32_t y = 125;
5240 processDown(mapper, x, y);
5241 processSync(mapper);
5242
5243 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5244 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5245 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5246 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5247 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5248 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5249 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5250 ASSERT_EQ(0, motionArgs.flags);
5251 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5252 ASSERT_EQ(0, motionArgs.buttonState);
5253 ASSERT_EQ(0, motionArgs.edgeFlags);
5254 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5255 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5256 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5257 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5258 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5259 1, 0, 0, 0, 0, 0, 0, 0));
5260 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5261 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5262 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5263
5264 // Move.
5265 x += 50;
5266 y += 75;
5267 processMove(mapper, x, y);
5268 processSync(mapper);
5269
5270 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5271 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5272 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5273 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5274 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5275 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5276 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5277 ASSERT_EQ(0, motionArgs.flags);
5278 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5279 ASSERT_EQ(0, motionArgs.buttonState);
5280 ASSERT_EQ(0, motionArgs.edgeFlags);
5281 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5282 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5283 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5284 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5285 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5286 1, 0, 0, 0, 0, 0, 0, 0));
5287 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5288 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5289 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5290
5291 // Up.
5292 processUp(mapper);
5293 processSync(mapper);
5294
5295 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5296 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5297 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5298 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5299 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5300 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5301 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5302 ASSERT_EQ(0, motionArgs.flags);
5303 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5304 ASSERT_EQ(0, motionArgs.buttonState);
5305 ASSERT_EQ(0, motionArgs.edgeFlags);
5306 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5307 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5308 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5309 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5310 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5311 1, 0, 0, 0, 0, 0, 0, 0));
5312 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5313 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5314 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5315
5316 // Should not have sent any more keys or motions.
5317 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5318 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5319}
5320
Michael Wrightd02c5b62014-02-10 15:10:22 -08005321TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005322 addConfigurationProperty("touch.deviceType", "touchScreen");
5323 prepareDisplay(DISPLAY_ORIENTATION_0);
5324 prepareButtons();
5325 prepareAxes(POSITION);
5326 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005327 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005328
arthurhungdcef2dc2020-08-11 14:47:50 +08005329 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005330
5331 NotifyMotionArgs motionArgs;
5332
5333 // Down.
5334 int32_t x = 100;
5335 int32_t y = 125;
5336 processDown(mapper, x, y);
5337 processSync(mapper);
5338
5339 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5340 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5341 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5342 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5343 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5344 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5345 ASSERT_EQ(0, motionArgs.flags);
5346 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5347 ASSERT_EQ(0, motionArgs.buttonState);
5348 ASSERT_EQ(0, motionArgs.edgeFlags);
5349 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5350 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5351 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5352 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5353 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5354 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5355 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5356 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5357
5358 // Move.
5359 x += 50;
5360 y += 75;
5361 processMove(mapper, x, y);
5362 processSync(mapper);
5363
5364 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5365 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5366 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5367 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5368 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5369 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5370 ASSERT_EQ(0, motionArgs.flags);
5371 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5372 ASSERT_EQ(0, motionArgs.buttonState);
5373 ASSERT_EQ(0, motionArgs.edgeFlags);
5374 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5375 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5376 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5377 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5378 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5379 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5380 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5381 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5382
5383 // Up.
5384 processUp(mapper);
5385 processSync(mapper);
5386
5387 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5388 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5389 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5390 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5391 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5392 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5393 ASSERT_EQ(0, motionArgs.flags);
5394 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5395 ASSERT_EQ(0, motionArgs.buttonState);
5396 ASSERT_EQ(0, motionArgs.edgeFlags);
5397 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5398 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5399 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5400 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5401 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5402 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5403 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5404 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5405
5406 // Should not have sent any more keys or motions.
5407 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5408 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5409}
5410
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005411TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_DoesNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005412 addConfigurationProperty("touch.deviceType", "touchScreen");
5413 prepareButtons();
5414 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005415 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
5416 // need to be rotated. Touchscreens are orientation-aware by default.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005417 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005418
5419 NotifyMotionArgs args;
5420
5421 // Rotation 90.
5422 prepareDisplay(DISPLAY_ORIENTATION_90);
5423 processDown(mapper, toRawX(50), toRawY(75));
5424 processSync(mapper);
5425
5426 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5427 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5428 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5429
5430 processUp(mapper);
5431 processSync(mapper);
5432 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5433}
5434
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005435TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_RotatesMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005436 addConfigurationProperty("touch.deviceType", "touchScreen");
5437 prepareButtons();
5438 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005439 // Since InputReader works in the un-rotated coordinate space, only devices that are not
5440 // orientation-aware are affected by display rotation.
5441 addConfigurationProperty("touch.orientationAware", "0");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005442 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005443
5444 NotifyMotionArgs args;
5445
5446 // Rotation 0.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005447 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005448 prepareDisplay(DISPLAY_ORIENTATION_0);
5449 processDown(mapper, toRawX(50), toRawY(75));
5450 processSync(mapper);
5451
5452 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5453 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5454 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5455
5456 processUp(mapper);
5457 processSync(mapper);
5458 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5459
5460 // Rotation 90.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005461 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005462 prepareDisplay(DISPLAY_ORIENTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005463 processDown(mapper, toRawX(75), RAW_Y_MAX - toRawY(50) + RAW_Y_MIN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005464 processSync(mapper);
5465
5466 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5467 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5468 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5469
5470 processUp(mapper);
5471 processSync(mapper);
5472 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5473
5474 // Rotation 180.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005475 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005476 prepareDisplay(DISPLAY_ORIENTATION_180);
5477 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
5478 processSync(mapper);
5479
5480 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5481 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5482 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5483
5484 processUp(mapper);
5485 processSync(mapper);
5486 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5487
5488 // Rotation 270.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005489 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005490 prepareDisplay(DISPLAY_ORIENTATION_270);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005491 processDown(mapper, RAW_X_MAX - toRawX(75) + RAW_X_MIN, toRawY(50));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005492 processSync(mapper);
5493
5494 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5495 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5496 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5497
5498 processUp(mapper);
5499 processSync(mapper);
5500 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5501}
5502
Prabir Pradhan3b134cc2021-08-20 16:09:32 -07005503TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation0_RotatesMotions) {
5504 addConfigurationProperty("touch.deviceType", "touchScreen");
5505 prepareButtons();
5506 prepareAxes(POSITION);
5507 addConfigurationProperty("touch.orientationAware", "1");
5508 addConfigurationProperty("touch.orientation", "ORIENTATION_0");
5509 clearViewports();
5510 prepareDisplay(DISPLAY_ORIENTATION_0);
5511 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5512 NotifyMotionArgs args;
5513
5514 // Orientation 0.
5515 processDown(mapper, toRawX(50), toRawY(75));
5516 processSync(mapper);
5517
5518 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5519 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5520 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5521
5522 processUp(mapper);
5523 processSync(mapper);
5524 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5525}
5526
5527TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation90_RotatesMotions) {
5528 addConfigurationProperty("touch.deviceType", "touchScreen");
5529 prepareButtons();
5530 prepareAxes(POSITION);
5531 addConfigurationProperty("touch.orientationAware", "1");
5532 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
5533 clearViewports();
5534 prepareDisplay(DISPLAY_ORIENTATION_0);
5535 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5536 NotifyMotionArgs args;
5537
5538 // Orientation 90.
5539 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
5540 processSync(mapper);
5541
5542 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5543 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5544 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5545
5546 processUp(mapper);
5547 processSync(mapper);
5548 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5549}
5550
5551TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation180_RotatesMotions) {
5552 addConfigurationProperty("touch.deviceType", "touchScreen");
5553 prepareButtons();
5554 prepareAxes(POSITION);
5555 addConfigurationProperty("touch.orientationAware", "1");
5556 addConfigurationProperty("touch.orientation", "ORIENTATION_180");
5557 clearViewports();
5558 prepareDisplay(DISPLAY_ORIENTATION_0);
5559 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5560 NotifyMotionArgs args;
5561
5562 // Orientation 180.
5563 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
5564 processSync(mapper);
5565
5566 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5567 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5568 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5569
5570 processUp(mapper);
5571 processSync(mapper);
5572 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5573}
5574
5575TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation270_RotatesMotions) {
5576 addConfigurationProperty("touch.deviceType", "touchScreen");
5577 prepareButtons();
5578 prepareAxes(POSITION);
5579 addConfigurationProperty("touch.orientationAware", "1");
5580 addConfigurationProperty("touch.orientation", "ORIENTATION_270");
5581 clearViewports();
5582 prepareDisplay(DISPLAY_ORIENTATION_0);
5583 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5584 NotifyMotionArgs args;
5585
5586 // Orientation 270.
5587 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
5588 processSync(mapper);
5589
5590 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5591 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5592 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5593
5594 processUp(mapper);
5595 processSync(mapper);
5596 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5597}
5598
5599TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationSpecified_RotatesMotionWithDisplay) {
5600 addConfigurationProperty("touch.deviceType", "touchScreen");
5601 prepareButtons();
5602 prepareAxes(POSITION);
5603 // Since InputReader works in the un-rotated coordinate space, only devices that are not
5604 // orientation-aware are affected by display rotation.
5605 addConfigurationProperty("touch.orientationAware", "0");
5606 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
5607 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5608
5609 NotifyMotionArgs args;
5610
5611 // Orientation 90, Rotation 0.
5612 clearViewports();
5613 prepareDisplay(DISPLAY_ORIENTATION_0);
5614 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
5615 processSync(mapper);
5616
5617 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5618 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5619 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5620
5621 processUp(mapper);
5622 processSync(mapper);
5623 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5624
5625 // Orientation 90, Rotation 90.
5626 clearViewports();
5627 prepareDisplay(DISPLAY_ORIENTATION_90);
5628 processDown(mapper, toRotatedRawX(50), toRotatedRawY(75));
5629 processSync(mapper);
5630
5631 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5632 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5633 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5634
5635 processUp(mapper);
5636 processSync(mapper);
5637 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5638
5639 // Orientation 90, Rotation 180.
5640 clearViewports();
5641 prepareDisplay(DISPLAY_ORIENTATION_180);
5642 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
5643 processSync(mapper);
5644
5645 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5646 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5647 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5648
5649 processUp(mapper);
5650 processSync(mapper);
5651 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5652
5653 // Orientation 90, Rotation 270.
5654 clearViewports();
5655 prepareDisplay(DISPLAY_ORIENTATION_270);
5656 processDown(mapper, RAW_X_MAX - toRotatedRawX(50) + RAW_X_MIN,
5657 RAW_Y_MAX - toRotatedRawY(75) + RAW_Y_MIN);
5658 processSync(mapper);
5659
5660 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5661 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5662 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5663
5664 processUp(mapper);
5665 processSync(mapper);
5666 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5667}
5668
Michael Wrightd02c5b62014-02-10 15:10:22 -08005669TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005670 addConfigurationProperty("touch.deviceType", "touchScreen");
5671 prepareDisplay(DISPLAY_ORIENTATION_0);
5672 prepareButtons();
5673 prepareAxes(POSITION | PRESSURE | TOOL | DISTANCE | TILT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005674 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005675
5676 // These calculations are based on the input device calibration documentation.
5677 int32_t rawX = 100;
5678 int32_t rawY = 200;
5679 int32_t rawPressure = 10;
5680 int32_t rawToolMajor = 12;
5681 int32_t rawDistance = 2;
5682 int32_t rawTiltX = 30;
5683 int32_t rawTiltY = 110;
5684
5685 float x = toDisplayX(rawX);
5686 float y = toDisplayY(rawY);
5687 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
5688 float size = float(rawToolMajor) / RAW_TOOL_MAX;
5689 float tool = float(rawToolMajor) * GEOMETRIC_SCALE;
5690 float distance = float(rawDistance);
5691
5692 float tiltCenter = (RAW_TILT_MAX + RAW_TILT_MIN) * 0.5f;
5693 float tiltScale = M_PI / 180;
5694 float tiltXAngle = (rawTiltX - tiltCenter) * tiltScale;
5695 float tiltYAngle = (rawTiltY - tiltCenter) * tiltScale;
5696 float orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
5697 float tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
5698
5699 processDown(mapper, rawX, rawY);
5700 processPressure(mapper, rawPressure);
5701 processToolMajor(mapper, rawToolMajor);
5702 processDistance(mapper, rawDistance);
5703 processTilt(mapper, rawTiltX, rawTiltY);
5704 processSync(mapper);
5705
5706 NotifyMotionArgs args;
5707 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5708 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5709 x, y, pressure, size, tool, tool, tool, tool, orientation, distance));
5710 ASSERT_EQ(tilt, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TILT));
5711}
5712
Jason Gerecke489fda82012-09-07 17:19:40 -07005713TEST_F(SingleTouchInputMapperTest, Process_XYAxes_AffineCalibration) {
Jason Gerecke489fda82012-09-07 17:19:40 -07005714 addConfigurationProperty("touch.deviceType", "touchScreen");
5715 prepareDisplay(DISPLAY_ORIENTATION_0);
5716 prepareLocationCalibration();
5717 prepareButtons();
5718 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005719 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Jason Gerecke489fda82012-09-07 17:19:40 -07005720
5721 int32_t rawX = 100;
5722 int32_t rawY = 200;
5723
5724 float x = toDisplayX(toCookedX(rawX, rawY));
5725 float y = toDisplayY(toCookedY(rawX, rawY));
5726
5727 processDown(mapper, rawX, rawY);
5728 processSync(mapper);
5729
5730 NotifyMotionArgs args;
5731 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5732 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5733 x, y, 1, 0, 0, 0, 0, 0, 0, 0));
5734}
5735
Michael Wrightd02c5b62014-02-10 15:10:22 -08005736TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005737 addConfigurationProperty("touch.deviceType", "touchScreen");
5738 prepareDisplay(DISPLAY_ORIENTATION_0);
5739 prepareButtons();
5740 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005741 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005742
5743 NotifyMotionArgs motionArgs;
5744 NotifyKeyArgs keyArgs;
5745
5746 processDown(mapper, 100, 200);
5747 processSync(mapper);
5748 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5749 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5750 ASSERT_EQ(0, motionArgs.buttonState);
5751
5752 // press BTN_LEFT, release BTN_LEFT
5753 processKey(mapper, BTN_LEFT, 1);
5754 processSync(mapper);
5755 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5756 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5757 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
5758
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005759 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5760 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5761 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
5762
Michael Wrightd02c5b62014-02-10 15:10:22 -08005763 processKey(mapper, BTN_LEFT, 0);
5764 processSync(mapper);
5765 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005766 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005767 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005768
5769 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005770 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005771 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005772
5773 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
5774 processKey(mapper, BTN_RIGHT, 1);
5775 processKey(mapper, BTN_MIDDLE, 1);
5776 processSync(mapper);
5777 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5778 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5779 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5780 motionArgs.buttonState);
5781
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005782 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5783 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5784 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
5785
5786 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5787 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5788 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5789 motionArgs.buttonState);
5790
Michael Wrightd02c5b62014-02-10 15:10:22 -08005791 processKey(mapper, BTN_RIGHT, 0);
5792 processSync(mapper);
5793 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005794 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005795 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005796
5797 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005798 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005799 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005800
5801 processKey(mapper, BTN_MIDDLE, 0);
5802 processSync(mapper);
5803 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005804 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005805 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005806
5807 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005808 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005809 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005810
5811 // press BTN_BACK, release BTN_BACK
5812 processKey(mapper, BTN_BACK, 1);
5813 processSync(mapper);
5814 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5815 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5816 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005817
Michael Wrightd02c5b62014-02-10 15:10:22 -08005818 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005819 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005820 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5821
5822 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5823 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5824 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005825
5826 processKey(mapper, BTN_BACK, 0);
5827 processSync(mapper);
5828 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005829 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005830 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005831
5832 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005833 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005834 ASSERT_EQ(0, motionArgs.buttonState);
5835
Michael Wrightd02c5b62014-02-10 15:10:22 -08005836 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5837 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5838 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
5839
5840 // press BTN_SIDE, release BTN_SIDE
5841 processKey(mapper, BTN_SIDE, 1);
5842 processSync(mapper);
5843 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5844 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5845 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005846
Michael Wrightd02c5b62014-02-10 15:10:22 -08005847 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005848 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005849 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5850
5851 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5852 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5853 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005854
5855 processKey(mapper, BTN_SIDE, 0);
5856 processSync(mapper);
5857 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005858 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005859 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005860
5861 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005862 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005863 ASSERT_EQ(0, motionArgs.buttonState);
5864
Michael Wrightd02c5b62014-02-10 15:10:22 -08005865 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5866 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5867 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
5868
5869 // press BTN_FORWARD, release BTN_FORWARD
5870 processKey(mapper, BTN_FORWARD, 1);
5871 processSync(mapper);
5872 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5873 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5874 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005875
Michael Wrightd02c5b62014-02-10 15:10:22 -08005876 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005877 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005878 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5879
5880 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5881 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5882 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005883
5884 processKey(mapper, BTN_FORWARD, 0);
5885 processSync(mapper);
5886 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005887 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005888 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005889
5890 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005891 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005892 ASSERT_EQ(0, motionArgs.buttonState);
5893
Michael Wrightd02c5b62014-02-10 15:10:22 -08005894 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5895 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5896 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
5897
5898 // press BTN_EXTRA, release BTN_EXTRA
5899 processKey(mapper, BTN_EXTRA, 1);
5900 processSync(mapper);
5901 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5902 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5903 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005904
Michael Wrightd02c5b62014-02-10 15:10:22 -08005905 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005906 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005907 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5908
5909 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5910 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5911 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005912
5913 processKey(mapper, BTN_EXTRA, 0);
5914 processSync(mapper);
5915 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005916 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005917 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005918
5919 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005920 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005921 ASSERT_EQ(0, motionArgs.buttonState);
5922
Michael Wrightd02c5b62014-02-10 15:10:22 -08005923 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5924 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5925 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
5926
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005927 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5928
Michael Wrightd02c5b62014-02-10 15:10:22 -08005929 // press BTN_STYLUS, release BTN_STYLUS
5930 processKey(mapper, BTN_STYLUS, 1);
5931 processSync(mapper);
5932 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5933 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005934 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
5935
5936 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5937 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5938 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005939
5940 processKey(mapper, BTN_STYLUS, 0);
5941 processSync(mapper);
5942 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005943 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005944 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005945
5946 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005947 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005948 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005949
5950 // press BTN_STYLUS2, release BTN_STYLUS2
5951 processKey(mapper, BTN_STYLUS2, 1);
5952 processSync(mapper);
5953 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5954 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005955 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
5956
5957 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5958 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5959 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005960
5961 processKey(mapper, BTN_STYLUS2, 0);
5962 processSync(mapper);
5963 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005964 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005965 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005966
5967 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005968 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005969 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005970
5971 // release touch
5972 processUp(mapper);
5973 processSync(mapper);
5974 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5975 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5976 ASSERT_EQ(0, motionArgs.buttonState);
5977}
5978
5979TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005980 addConfigurationProperty("touch.deviceType", "touchScreen");
5981 prepareDisplay(DISPLAY_ORIENTATION_0);
5982 prepareButtons();
5983 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005984 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005985
5986 NotifyMotionArgs motionArgs;
5987
5988 // default tool type is finger
5989 processDown(mapper, 100, 200);
5990 processSync(mapper);
5991 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5992 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5993 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5994
5995 // eraser
5996 processKey(mapper, BTN_TOOL_RUBBER, 1);
5997 processSync(mapper);
5998 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5999 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6000 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6001
6002 // stylus
6003 processKey(mapper, BTN_TOOL_RUBBER, 0);
6004 processKey(mapper, BTN_TOOL_PEN, 1);
6005 processSync(mapper);
6006 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6007 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6008 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6009
6010 // brush
6011 processKey(mapper, BTN_TOOL_PEN, 0);
6012 processKey(mapper, BTN_TOOL_BRUSH, 1);
6013 processSync(mapper);
6014 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6015 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6016 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6017
6018 // pencil
6019 processKey(mapper, BTN_TOOL_BRUSH, 0);
6020 processKey(mapper, BTN_TOOL_PENCIL, 1);
6021 processSync(mapper);
6022 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6023 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6024 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6025
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08006026 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08006027 processKey(mapper, BTN_TOOL_PENCIL, 0);
6028 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
6029 processSync(mapper);
6030 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6031 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6032 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6033
6034 // mouse
6035 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
6036 processKey(mapper, BTN_TOOL_MOUSE, 1);
6037 processSync(mapper);
6038 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6039 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6040 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6041
6042 // lens
6043 processKey(mapper, BTN_TOOL_MOUSE, 0);
6044 processKey(mapper, BTN_TOOL_LENS, 1);
6045 processSync(mapper);
6046 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6047 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6048 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6049
6050 // double-tap
6051 processKey(mapper, BTN_TOOL_LENS, 0);
6052 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
6053 processSync(mapper);
6054 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6055 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6056 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6057
6058 // triple-tap
6059 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
6060 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
6061 processSync(mapper);
6062 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6063 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6064 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6065
6066 // quad-tap
6067 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
6068 processKey(mapper, BTN_TOOL_QUADTAP, 1);
6069 processSync(mapper);
6070 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6071 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6072 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6073
6074 // finger
6075 processKey(mapper, BTN_TOOL_QUADTAP, 0);
6076 processKey(mapper, BTN_TOOL_FINGER, 1);
6077 processSync(mapper);
6078 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6079 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6080 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6081
6082 // stylus trumps finger
6083 processKey(mapper, BTN_TOOL_PEN, 1);
6084 processSync(mapper);
6085 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6086 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6087 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6088
6089 // eraser trumps stylus
6090 processKey(mapper, BTN_TOOL_RUBBER, 1);
6091 processSync(mapper);
6092 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6093 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6094 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6095
6096 // mouse trumps eraser
6097 processKey(mapper, BTN_TOOL_MOUSE, 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_MOUSE, motionArgs.pointerProperties[0].toolType);
6102
6103 // back to default tool type
6104 processKey(mapper, BTN_TOOL_MOUSE, 0);
6105 processKey(mapper, BTN_TOOL_RUBBER, 0);
6106 processKey(mapper, BTN_TOOL_PEN, 0);
6107 processKey(mapper, BTN_TOOL_FINGER, 0);
6108 processSync(mapper);
6109 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6110 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6111 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6112}
6113
6114TEST_F(SingleTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006115 addConfigurationProperty("touch.deviceType", "touchScreen");
6116 prepareDisplay(DISPLAY_ORIENTATION_0);
6117 prepareButtons();
6118 prepareAxes(POSITION);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006119 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_FINGER, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006120 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006121
6122 NotifyMotionArgs motionArgs;
6123
6124 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
6125 processKey(mapper, BTN_TOOL_FINGER, 1);
6126 processMove(mapper, 100, 200);
6127 processSync(mapper);
6128 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6129 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6130 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6131 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6132
6133 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6134 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6135 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6136 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6137
6138 // move a little
6139 processMove(mapper, 150, 250);
6140 processSync(mapper);
6141 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6142 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6143 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6144 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6145
6146 // down when BTN_TOUCH is pressed, pressure defaults to 1
6147 processKey(mapper, BTN_TOUCH, 1);
6148 processSync(mapper);
6149 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6150 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6151 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6152 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6153
6154 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6155 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6156 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6157 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6158
6159 // up when BTN_TOUCH is released, hover restored
6160 processKey(mapper, BTN_TOUCH, 0);
6161 processSync(mapper);
6162 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6163 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6164 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6165 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6166
6167 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6168 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6169 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6170 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6171
6172 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6173 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6174 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6175 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6176
6177 // exit hover when pointer goes away
6178 processKey(mapper, BTN_TOOL_FINGER, 0);
6179 processSync(mapper);
6180 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6181 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6182 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6183 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6184}
6185
6186TEST_F(SingleTouchInputMapperTest, Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006187 addConfigurationProperty("touch.deviceType", "touchScreen");
6188 prepareDisplay(DISPLAY_ORIENTATION_0);
6189 prepareButtons();
6190 prepareAxes(POSITION | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006191 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006192
6193 NotifyMotionArgs motionArgs;
6194
6195 // initially hovering because pressure is 0
6196 processDown(mapper, 100, 200);
6197 processPressure(mapper, 0);
6198 processSync(mapper);
6199 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6200 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6201 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6202 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6203
6204 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6205 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6206 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6207 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6208
6209 // move a little
6210 processMove(mapper, 150, 250);
6211 processSync(mapper);
6212 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6213 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6214 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6215 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6216
6217 // down when pressure is non-zero
6218 processPressure(mapper, RAW_PRESSURE_MAX);
6219 processSync(mapper);
6220 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6221 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6222 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6223 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6224
6225 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6226 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6227 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6228 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6229
6230 // up when pressure becomes 0, hover restored
6231 processPressure(mapper, 0);
6232 processSync(mapper);
6233 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6234 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6235 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6236 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6237
6238 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6239 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6240 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6241 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6242
6243 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6244 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6245 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6246 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6247
6248 // exit hover when pointer goes away
6249 processUp(mapper);
6250 processSync(mapper);
6251 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6252 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6253 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6254 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6255}
6256
Michael Wrightd02c5b62014-02-10 15:10:22 -08006257// --- MultiTouchInputMapperTest ---
6258
6259class MultiTouchInputMapperTest : public TouchInputMapperTest {
6260protected:
6261 void prepareAxes(int axes);
6262
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006263 void processPosition(MultiTouchInputMapper& mapper, int32_t x, int32_t y);
6264 void processTouchMajor(MultiTouchInputMapper& mapper, int32_t touchMajor);
6265 void processTouchMinor(MultiTouchInputMapper& mapper, int32_t touchMinor);
6266 void processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor);
6267 void processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor);
6268 void processOrientation(MultiTouchInputMapper& mapper, int32_t orientation);
6269 void processPressure(MultiTouchInputMapper& mapper, int32_t pressure);
6270 void processDistance(MultiTouchInputMapper& mapper, int32_t distance);
6271 void processId(MultiTouchInputMapper& mapper, int32_t id);
6272 void processSlot(MultiTouchInputMapper& mapper, int32_t slot);
6273 void processToolType(MultiTouchInputMapper& mapper, int32_t toolType);
6274 void processKey(MultiTouchInputMapper& mapper, int32_t code, int32_t value);
6275 void processMTSync(MultiTouchInputMapper& mapper);
6276 void processSync(MultiTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006277};
6278
6279void MultiTouchInputMapperTest::prepareAxes(int axes) {
6280 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006281 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
6282 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006283 }
6284 if (axes & TOUCH) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006285 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN,
6286 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006287 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006288 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN,
6289 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006290 }
6291 }
6292 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006293 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
6294 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006295 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006296 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MAX,
6297 RAW_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006298 }
6299 }
6300 if (axes & ORIENTATION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006301 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_ORIENTATION, RAW_ORIENTATION_MIN,
6302 RAW_ORIENTATION_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006303 }
6304 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006305 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_PRESSURE, RAW_PRESSURE_MIN,
6306 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006307 }
6308 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006309 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_DISTANCE, RAW_DISTANCE_MIN,
6310 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006311 }
6312 if (axes & ID) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006313 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX, 0,
6314 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006315 }
6316 if (axes & SLOT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006317 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
6318 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_MT_SLOT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006319 }
6320 if (axes & TOOL_TYPE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006321 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOOL_TYPE, 0, MT_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006322 }
6323}
6324
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006325void MultiTouchInputMapperTest::processPosition(MultiTouchInputMapper& mapper, int32_t x,
6326 int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006327 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_X, x);
6328 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006329}
6330
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006331void MultiTouchInputMapperTest::processTouchMajor(MultiTouchInputMapper& mapper,
6332 int32_t touchMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006333 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006334}
6335
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006336void MultiTouchInputMapperTest::processTouchMinor(MultiTouchInputMapper& mapper,
6337 int32_t touchMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006338 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006339}
6340
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006341void MultiTouchInputMapperTest::processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006342 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006343}
6344
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006345void MultiTouchInputMapperTest::processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006346 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006347}
6348
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006349void MultiTouchInputMapperTest::processOrientation(MultiTouchInputMapper& mapper,
6350 int32_t orientation) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006351 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_ORIENTATION, orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006352}
6353
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006354void MultiTouchInputMapperTest::processPressure(MultiTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006355 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006356}
6357
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006358void MultiTouchInputMapperTest::processDistance(MultiTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006359 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006360}
6361
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006362void MultiTouchInputMapperTest::processId(MultiTouchInputMapper& mapper, int32_t id) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006363 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TRACKING_ID, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006364}
6365
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006366void MultiTouchInputMapperTest::processSlot(MultiTouchInputMapper& mapper, int32_t slot) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006367 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_SLOT, slot);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006368}
6369
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006370void MultiTouchInputMapperTest::processToolType(MultiTouchInputMapper& mapper, int32_t toolType) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006371 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006372}
6373
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006374void MultiTouchInputMapperTest::processKey(MultiTouchInputMapper& mapper, int32_t code,
6375 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006376 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006377}
6378
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006379void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006380 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_MT_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006381}
6382
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006383void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006384 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006385}
6386
Michael Wrightd02c5b62014-02-10 15:10:22 -08006387TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006388 addConfigurationProperty("touch.deviceType", "touchScreen");
6389 prepareDisplay(DISPLAY_ORIENTATION_0);
6390 prepareAxes(POSITION);
6391 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006392 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006393
arthurhungdcef2dc2020-08-11 14:47:50 +08006394 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006395
6396 NotifyMotionArgs motionArgs;
6397
6398 // Two fingers down at once.
6399 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
6400 processPosition(mapper, x1, y1);
6401 processMTSync(mapper);
6402 processPosition(mapper, x2, y2);
6403 processMTSync(mapper);
6404 processSync(mapper);
6405
6406 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6407 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6408 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6409 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6410 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6411 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6412 ASSERT_EQ(0, motionArgs.flags);
6413 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6414 ASSERT_EQ(0, motionArgs.buttonState);
6415 ASSERT_EQ(0, motionArgs.edgeFlags);
6416 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6417 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6418 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6419 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6420 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6421 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6422 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6423 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6424
6425 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6426 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6427 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6428 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6429 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6430 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6431 motionArgs.action);
6432 ASSERT_EQ(0, motionArgs.flags);
6433 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6434 ASSERT_EQ(0, motionArgs.buttonState);
6435 ASSERT_EQ(0, motionArgs.edgeFlags);
6436 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6437 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6438 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6439 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6440 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6441 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6442 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6443 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6444 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6445 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6446 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6447 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6448
6449 // Move.
6450 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
6451 processPosition(mapper, x1, y1);
6452 processMTSync(mapper);
6453 processPosition(mapper, x2, y2);
6454 processMTSync(mapper);
6455 processSync(mapper);
6456
6457 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6458 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6459 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6460 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6461 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6462 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6463 ASSERT_EQ(0, motionArgs.flags);
6464 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6465 ASSERT_EQ(0, motionArgs.buttonState);
6466 ASSERT_EQ(0, motionArgs.edgeFlags);
6467 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6468 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6469 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6470 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6471 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6472 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6473 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6474 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6475 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6476 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6477 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6478 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6479
6480 // First finger up.
6481 x2 += 15; y2 -= 20;
6482 processPosition(mapper, x2, y2);
6483 processMTSync(mapper);
6484 processSync(mapper);
6485
6486 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6487 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6488 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6489 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6490 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6491 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6492 motionArgs.action);
6493 ASSERT_EQ(0, motionArgs.flags);
6494 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6495 ASSERT_EQ(0, motionArgs.buttonState);
6496 ASSERT_EQ(0, motionArgs.edgeFlags);
6497 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6498 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6499 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6500 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6501 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6502 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6503 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6504 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6505 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6506 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6507 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6508 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6509
6510 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6511 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6512 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6513 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6514 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6515 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6516 ASSERT_EQ(0, motionArgs.flags);
6517 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6518 ASSERT_EQ(0, motionArgs.buttonState);
6519 ASSERT_EQ(0, motionArgs.edgeFlags);
6520 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6521 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6522 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6523 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6524 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6525 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6526 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6527 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6528
6529 // Move.
6530 x2 += 20; y2 -= 25;
6531 processPosition(mapper, x2, y2);
6532 processMTSync(mapper);
6533 processSync(mapper);
6534
6535 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6536 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6537 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6538 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6539 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6540 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6541 ASSERT_EQ(0, motionArgs.flags);
6542 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6543 ASSERT_EQ(0, motionArgs.buttonState);
6544 ASSERT_EQ(0, motionArgs.edgeFlags);
6545 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6546 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6547 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6548 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6549 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6550 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6551 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6552 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6553
6554 // New finger down.
6555 int32_t x3 = 700, y3 = 300;
6556 processPosition(mapper, x2, y2);
6557 processMTSync(mapper);
6558 processPosition(mapper, x3, y3);
6559 processMTSync(mapper);
6560 processSync(mapper);
6561
6562 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6563 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6564 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6565 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6566 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6567 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6568 motionArgs.action);
6569 ASSERT_EQ(0, motionArgs.flags);
6570 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6571 ASSERT_EQ(0, motionArgs.buttonState);
6572 ASSERT_EQ(0, motionArgs.edgeFlags);
6573 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6574 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6575 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6576 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6577 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6578 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6579 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6580 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6581 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6582 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6583 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6584 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6585
6586 // Second finger up.
6587 x3 += 30; y3 -= 20;
6588 processPosition(mapper, x3, y3);
6589 processMTSync(mapper);
6590 processSync(mapper);
6591
6592 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6593 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6594 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6595 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6596 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6597 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6598 motionArgs.action);
6599 ASSERT_EQ(0, motionArgs.flags);
6600 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6601 ASSERT_EQ(0, motionArgs.buttonState);
6602 ASSERT_EQ(0, motionArgs.edgeFlags);
6603 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6604 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6605 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6606 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6607 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6608 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6609 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6610 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6611 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6612 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6613 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6614 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6615
6616 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6617 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6618 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6619 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6620 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6621 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6622 ASSERT_EQ(0, motionArgs.flags);
6623 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6624 ASSERT_EQ(0, motionArgs.buttonState);
6625 ASSERT_EQ(0, motionArgs.edgeFlags);
6626 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6627 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6628 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6629 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6630 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6631 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6632 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6633 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6634
6635 // Last finger up.
6636 processMTSync(mapper);
6637 processSync(mapper);
6638
6639 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6640 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6641 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6642 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6643 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6644 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6645 ASSERT_EQ(0, motionArgs.flags);
6646 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6647 ASSERT_EQ(0, motionArgs.buttonState);
6648 ASSERT_EQ(0, motionArgs.edgeFlags);
6649 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6650 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6651 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6652 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6653 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6654 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6655 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6656 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6657
6658 // Should not have sent any more keys or motions.
6659 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6660 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6661}
6662
6663TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006664 addConfigurationProperty("touch.deviceType", "touchScreen");
6665 prepareDisplay(DISPLAY_ORIENTATION_0);
6666 prepareAxes(POSITION | ID);
6667 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006668 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006669
arthurhungdcef2dc2020-08-11 14:47:50 +08006670 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006671
6672 NotifyMotionArgs motionArgs;
6673
6674 // Two fingers down at once.
6675 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
6676 processPosition(mapper, x1, y1);
6677 processId(mapper, 1);
6678 processMTSync(mapper);
6679 processPosition(mapper, x2, y2);
6680 processId(mapper, 2);
6681 processMTSync(mapper);
6682 processSync(mapper);
6683
6684 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6685 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6686 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6687 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6688 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6689 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6690 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6691
6692 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6693 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6694 motionArgs.action);
6695 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6696 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6697 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6698 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6699 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6700 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6701 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6702 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6703 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6704
6705 // Move.
6706 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
6707 processPosition(mapper, x1, y1);
6708 processId(mapper, 1);
6709 processMTSync(mapper);
6710 processPosition(mapper, x2, y2);
6711 processId(mapper, 2);
6712 processMTSync(mapper);
6713 processSync(mapper);
6714
6715 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6716 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6717 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6718 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6719 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6720 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6721 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6722 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6723 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6724 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6725 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6726
6727 // First finger up.
6728 x2 += 15; y2 -= 20;
6729 processPosition(mapper, x2, y2);
6730 processId(mapper, 2);
6731 processMTSync(mapper);
6732 processSync(mapper);
6733
6734 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6735 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6736 motionArgs.action);
6737 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6738 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6739 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6740 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6741 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6742 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6743 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6744 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6745 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6746
6747 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6748 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6749 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6750 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6751 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6752 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6753 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6754
6755 // Move.
6756 x2 += 20; y2 -= 25;
6757 processPosition(mapper, x2, y2);
6758 processId(mapper, 2);
6759 processMTSync(mapper);
6760 processSync(mapper);
6761
6762 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6763 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6764 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6765 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6766 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6767 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6768 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6769
6770 // New finger down.
6771 int32_t x3 = 700, y3 = 300;
6772 processPosition(mapper, x2, y2);
6773 processId(mapper, 2);
6774 processMTSync(mapper);
6775 processPosition(mapper, x3, y3);
6776 processId(mapper, 3);
6777 processMTSync(mapper);
6778 processSync(mapper);
6779
6780 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6781 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6782 motionArgs.action);
6783 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6784 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6785 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6786 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6787 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6788 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6789 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6790 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6791 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6792
6793 // Second finger up.
6794 x3 += 30; y3 -= 20;
6795 processPosition(mapper, x3, y3);
6796 processId(mapper, 3);
6797 processMTSync(mapper);
6798 processSync(mapper);
6799
6800 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6801 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6802 motionArgs.action);
6803 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6804 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6805 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6806 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6807 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6808 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6809 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6810 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6811 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6812
6813 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6814 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6815 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6816 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6817 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6818 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6819 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6820
6821 // Last finger up.
6822 processMTSync(mapper);
6823 processSync(mapper);
6824
6825 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6826 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6827 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6828 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6829 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6830 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6831 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6832
6833 // Should not have sent any more keys or motions.
6834 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6835 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6836}
6837
6838TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006839 addConfigurationProperty("touch.deviceType", "touchScreen");
6840 prepareDisplay(DISPLAY_ORIENTATION_0);
6841 prepareAxes(POSITION | ID | SLOT);
6842 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006843 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006844
arthurhungdcef2dc2020-08-11 14:47:50 +08006845 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006846
6847 NotifyMotionArgs motionArgs;
6848
6849 // Two fingers down at once.
6850 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
6851 processPosition(mapper, x1, y1);
6852 processId(mapper, 1);
6853 processSlot(mapper, 1);
6854 processPosition(mapper, x2, y2);
6855 processId(mapper, 2);
6856 processSync(mapper);
6857
6858 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6859 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6860 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6861 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6862 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6863 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6864 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6865
6866 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6867 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6868 motionArgs.action);
6869 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6870 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6871 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6872 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6873 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6874 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6875 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6876 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6877 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6878
6879 // Move.
6880 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
6881 processSlot(mapper, 0);
6882 processPosition(mapper, x1, y1);
6883 processSlot(mapper, 1);
6884 processPosition(mapper, x2, y2);
6885 processSync(mapper);
6886
6887 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6888 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6889 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6890 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6891 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6892 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6893 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6894 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6895 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6896 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6897 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6898
6899 // First finger up.
6900 x2 += 15; y2 -= 20;
6901 processSlot(mapper, 0);
6902 processId(mapper, -1);
6903 processSlot(mapper, 1);
6904 processPosition(mapper, x2, y2);
6905 processSync(mapper);
6906
6907 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6908 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6909 motionArgs.action);
6910 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6911 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6912 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6913 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6914 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6915 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6916 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6917 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6918 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6919
6920 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6921 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6922 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6923 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6924 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6925 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6926 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6927
6928 // Move.
6929 x2 += 20; y2 -= 25;
6930 processPosition(mapper, x2, y2);
6931 processSync(mapper);
6932
6933 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6934 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6935 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6936 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6937 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6938 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6939 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6940
6941 // New finger down.
6942 int32_t x3 = 700, y3 = 300;
6943 processPosition(mapper, x2, y2);
6944 processSlot(mapper, 0);
6945 processId(mapper, 3);
6946 processPosition(mapper, x3, y3);
6947 processSync(mapper);
6948
6949 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6950 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6951 motionArgs.action);
6952 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6953 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6954 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6955 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6956 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6957 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6958 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6959 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6960 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6961
6962 // Second finger up.
6963 x3 += 30; y3 -= 20;
6964 processSlot(mapper, 1);
6965 processId(mapper, -1);
6966 processSlot(mapper, 0);
6967 processPosition(mapper, x3, y3);
6968 processSync(mapper);
6969
6970 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6971 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6972 motionArgs.action);
6973 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6974 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6975 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6976 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6977 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6978 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6979 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6980 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6981 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6982
6983 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6984 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6985 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6986 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6987 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6988 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6989 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6990
6991 // Last finger up.
6992 processId(mapper, -1);
6993 processSync(mapper);
6994
6995 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6996 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6997 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6998 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6999 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7000 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7001 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7002
7003 // Should not have sent any more keys or motions.
7004 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7005 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7006}
7007
7008TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007009 addConfigurationProperty("touch.deviceType", "touchScreen");
7010 prepareDisplay(DISPLAY_ORIENTATION_0);
7011 prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007012 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007013
7014 // These calculations are based on the input device calibration documentation.
7015 int32_t rawX = 100;
7016 int32_t rawY = 200;
7017 int32_t rawTouchMajor = 7;
7018 int32_t rawTouchMinor = 6;
7019 int32_t rawToolMajor = 9;
7020 int32_t rawToolMinor = 8;
7021 int32_t rawPressure = 11;
7022 int32_t rawDistance = 0;
7023 int32_t rawOrientation = 3;
7024 int32_t id = 5;
7025
7026 float x = toDisplayX(rawX);
7027 float y = toDisplayY(rawY);
7028 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
7029 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
7030 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
7031 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
7032 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
7033 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
7034 float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
7035 float distance = float(rawDistance);
7036
7037 processPosition(mapper, rawX, rawY);
7038 processTouchMajor(mapper, rawTouchMajor);
7039 processTouchMinor(mapper, rawTouchMinor);
7040 processToolMajor(mapper, rawToolMajor);
7041 processToolMinor(mapper, rawToolMinor);
7042 processPressure(mapper, rawPressure);
7043 processOrientation(mapper, rawOrientation);
7044 processDistance(mapper, rawDistance);
7045 processId(mapper, id);
7046 processMTSync(mapper);
7047 processSync(mapper);
7048
7049 NotifyMotionArgs args;
7050 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7051 ASSERT_EQ(0, args.pointerProperties[0].id);
7052 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7053 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
7054 orientation, distance));
7055}
7056
7057TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007058 addConfigurationProperty("touch.deviceType", "touchScreen");
7059 prepareDisplay(DISPLAY_ORIENTATION_0);
7060 prepareAxes(POSITION | TOUCH | TOOL | MINOR);
7061 addConfigurationProperty("touch.size.calibration", "geometric");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007062 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007063
7064 // These calculations are based on the input device calibration documentation.
7065 int32_t rawX = 100;
7066 int32_t rawY = 200;
7067 int32_t rawTouchMajor = 140;
7068 int32_t rawTouchMinor = 120;
7069 int32_t rawToolMajor = 180;
7070 int32_t rawToolMinor = 160;
7071
7072 float x = toDisplayX(rawX);
7073 float y = toDisplayY(rawY);
7074 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
7075 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
7076 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
7077 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
7078 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
7079
7080 processPosition(mapper, rawX, rawY);
7081 processTouchMajor(mapper, rawTouchMajor);
7082 processTouchMinor(mapper, rawTouchMinor);
7083 processToolMajor(mapper, rawToolMajor);
7084 processToolMinor(mapper, rawToolMinor);
7085 processMTSync(mapper);
7086 processSync(mapper);
7087
7088 NotifyMotionArgs args;
7089 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7090 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7091 x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
7092}
7093
7094TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007095 addConfigurationProperty("touch.deviceType", "touchScreen");
7096 prepareDisplay(DISPLAY_ORIENTATION_0);
7097 prepareAxes(POSITION | TOUCH | TOOL);
7098 addConfigurationProperty("touch.size.calibration", "diameter");
7099 addConfigurationProperty("touch.size.scale", "10");
7100 addConfigurationProperty("touch.size.bias", "160");
7101 addConfigurationProperty("touch.size.isSummed", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007102 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007103
7104 // These calculations are based on the input device calibration documentation.
7105 // Note: We only provide a single common touch/tool value because the device is assumed
7106 // not to emit separate values for each pointer (isSummed = 1).
7107 int32_t rawX = 100;
7108 int32_t rawY = 200;
7109 int32_t rawX2 = 150;
7110 int32_t rawY2 = 250;
7111 int32_t rawTouchMajor = 5;
7112 int32_t rawToolMajor = 8;
7113
7114 float x = toDisplayX(rawX);
7115 float y = toDisplayY(rawY);
7116 float x2 = toDisplayX(rawX2);
7117 float y2 = toDisplayY(rawY2);
7118 float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
7119 float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
7120 float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
7121
7122 processPosition(mapper, rawX, rawY);
7123 processTouchMajor(mapper, rawTouchMajor);
7124 processToolMajor(mapper, rawToolMajor);
7125 processMTSync(mapper);
7126 processPosition(mapper, rawX2, rawY2);
7127 processTouchMajor(mapper, rawTouchMajor);
7128 processToolMajor(mapper, rawToolMajor);
7129 processMTSync(mapper);
7130 processSync(mapper);
7131
7132 NotifyMotionArgs args;
7133 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7134 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
7135
7136 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7137 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7138 args.action);
7139 ASSERT_EQ(size_t(2), args.pointerCount);
7140 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7141 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
7142 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
7143 x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
7144}
7145
7146TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007147 addConfigurationProperty("touch.deviceType", "touchScreen");
7148 prepareDisplay(DISPLAY_ORIENTATION_0);
7149 prepareAxes(POSITION | TOUCH | TOOL);
7150 addConfigurationProperty("touch.size.calibration", "area");
7151 addConfigurationProperty("touch.size.scale", "43");
7152 addConfigurationProperty("touch.size.bias", "3");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007153 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007154
7155 // These calculations are based on the input device calibration documentation.
7156 int32_t rawX = 100;
7157 int32_t rawY = 200;
7158 int32_t rawTouchMajor = 5;
7159 int32_t rawToolMajor = 8;
7160
7161 float x = toDisplayX(rawX);
7162 float y = toDisplayY(rawY);
7163 float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
7164 float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
7165 float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
7166
7167 processPosition(mapper, rawX, rawY);
7168 processTouchMajor(mapper, rawTouchMajor);
7169 processToolMajor(mapper, rawToolMajor);
7170 processMTSync(mapper);
7171 processSync(mapper);
7172
7173 NotifyMotionArgs args;
7174 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7175 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7176 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
7177}
7178
7179TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007180 addConfigurationProperty("touch.deviceType", "touchScreen");
7181 prepareDisplay(DISPLAY_ORIENTATION_0);
7182 prepareAxes(POSITION | PRESSURE);
7183 addConfigurationProperty("touch.pressure.calibration", "amplitude");
7184 addConfigurationProperty("touch.pressure.scale", "0.01");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007185 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007186
Michael Wrightaa449c92017-12-13 21:21:43 +00007187 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007188 mapper.populateDeviceInfo(&info);
Michael Wrightaa449c92017-12-13 21:21:43 +00007189 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
7190 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TOUCHSCREEN,
7191 0.0f, RAW_PRESSURE_MAX * 0.01, 0.0f, 0.0f));
7192
Michael Wrightd02c5b62014-02-10 15:10:22 -08007193 // These calculations are based on the input device calibration documentation.
7194 int32_t rawX = 100;
7195 int32_t rawY = 200;
7196 int32_t rawPressure = 60;
7197
7198 float x = toDisplayX(rawX);
7199 float y = toDisplayY(rawY);
7200 float pressure = float(rawPressure) * 0.01f;
7201
7202 processPosition(mapper, rawX, rawY);
7203 processPressure(mapper, rawPressure);
7204 processMTSync(mapper);
7205 processSync(mapper);
7206
7207 NotifyMotionArgs args;
7208 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7209 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7210 x, y, pressure, 0, 0, 0, 0, 0, 0, 0));
7211}
7212
7213TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007214 addConfigurationProperty("touch.deviceType", "touchScreen");
7215 prepareDisplay(DISPLAY_ORIENTATION_0);
7216 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007217 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007218
7219 NotifyMotionArgs motionArgs;
7220 NotifyKeyArgs keyArgs;
7221
7222 processId(mapper, 1);
7223 processPosition(mapper, 100, 200);
7224 processSync(mapper);
7225 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7226 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7227 ASSERT_EQ(0, motionArgs.buttonState);
7228
7229 // press BTN_LEFT, release BTN_LEFT
7230 processKey(mapper, BTN_LEFT, 1);
7231 processSync(mapper);
7232 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7233 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7234 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
7235
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007236 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7237 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7238 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
7239
Michael Wrightd02c5b62014-02-10 15:10:22 -08007240 processKey(mapper, BTN_LEFT, 0);
7241 processSync(mapper);
7242 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007243 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007244 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007245
7246 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007247 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007248 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007249
7250 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
7251 processKey(mapper, BTN_RIGHT, 1);
7252 processKey(mapper, BTN_MIDDLE, 1);
7253 processSync(mapper);
7254 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7255 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7256 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
7257 motionArgs.buttonState);
7258
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007259 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7260 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7261 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
7262
7263 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7264 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7265 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
7266 motionArgs.buttonState);
7267
Michael Wrightd02c5b62014-02-10 15:10:22 -08007268 processKey(mapper, BTN_RIGHT, 0);
7269 processSync(mapper);
7270 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007271 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007272 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007273
7274 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007275 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007276 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007277
7278 processKey(mapper, BTN_MIDDLE, 0);
7279 processSync(mapper);
7280 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007281 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007282 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007283
7284 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007285 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007286 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007287
7288 // press BTN_BACK, release BTN_BACK
7289 processKey(mapper, BTN_BACK, 1);
7290 processSync(mapper);
7291 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7292 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
7293 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007294
Michael Wrightd02c5b62014-02-10 15:10:22 -08007295 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007296 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007297 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
7298
7299 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7300 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7301 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007302
7303 processKey(mapper, BTN_BACK, 0);
7304 processSync(mapper);
7305 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007306 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007307 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007308
7309 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007310 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007311 ASSERT_EQ(0, motionArgs.buttonState);
7312
Michael Wrightd02c5b62014-02-10 15:10:22 -08007313 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7314 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
7315 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
7316
7317 // press BTN_SIDE, release BTN_SIDE
7318 processKey(mapper, BTN_SIDE, 1);
7319 processSync(mapper);
7320 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7321 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
7322 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007323
Michael Wrightd02c5b62014-02-10 15:10:22 -08007324 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007325 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007326 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
7327
7328 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7329 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7330 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007331
7332 processKey(mapper, BTN_SIDE, 0);
7333 processSync(mapper);
7334 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007335 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007336 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007337
7338 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007339 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007340 ASSERT_EQ(0, motionArgs.buttonState);
7341
Michael Wrightd02c5b62014-02-10 15:10:22 -08007342 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7343 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
7344 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
7345
7346 // press BTN_FORWARD, release BTN_FORWARD
7347 processKey(mapper, BTN_FORWARD, 1);
7348 processSync(mapper);
7349 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7350 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
7351 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007352
Michael Wrightd02c5b62014-02-10 15:10:22 -08007353 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007354 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007355 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
7356
7357 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7358 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7359 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007360
7361 processKey(mapper, BTN_FORWARD, 0);
7362 processSync(mapper);
7363 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007364 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007365 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007366
7367 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007368 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007369 ASSERT_EQ(0, motionArgs.buttonState);
7370
Michael Wrightd02c5b62014-02-10 15:10:22 -08007371 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7372 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
7373 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
7374
7375 // press BTN_EXTRA, release BTN_EXTRA
7376 processKey(mapper, BTN_EXTRA, 1);
7377 processSync(mapper);
7378 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7379 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
7380 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007381
Michael Wrightd02c5b62014-02-10 15:10:22 -08007382 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007383 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007384 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
7385
7386 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7387 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7388 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007389
7390 processKey(mapper, BTN_EXTRA, 0);
7391 processSync(mapper);
7392 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007393 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007394 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007395
7396 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007397 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007398 ASSERT_EQ(0, motionArgs.buttonState);
7399
Michael Wrightd02c5b62014-02-10 15:10:22 -08007400 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7401 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
7402 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
7403
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007404 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7405
Michael Wrightd02c5b62014-02-10 15:10:22 -08007406 // press BTN_STYLUS, release BTN_STYLUS
7407 processKey(mapper, BTN_STYLUS, 1);
7408 processSync(mapper);
7409 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7410 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007411 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
7412
7413 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7414 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7415 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007416
7417 processKey(mapper, BTN_STYLUS, 0);
7418 processSync(mapper);
7419 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007420 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007421 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007422
7423 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007424 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007425 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007426
7427 // press BTN_STYLUS2, release BTN_STYLUS2
7428 processKey(mapper, BTN_STYLUS2, 1);
7429 processSync(mapper);
7430 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7431 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007432 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
7433
7434 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7435 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7436 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007437
7438 processKey(mapper, BTN_STYLUS2, 0);
7439 processSync(mapper);
7440 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007441 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007442 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007443
7444 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007445 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007446 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007447
7448 // release touch
7449 processId(mapper, -1);
7450 processSync(mapper);
7451 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7452 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7453 ASSERT_EQ(0, motionArgs.buttonState);
7454}
7455
7456TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007457 addConfigurationProperty("touch.deviceType", "touchScreen");
7458 prepareDisplay(DISPLAY_ORIENTATION_0);
7459 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007460 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007461
7462 NotifyMotionArgs motionArgs;
7463
7464 // default tool type is finger
7465 processId(mapper, 1);
7466 processPosition(mapper, 100, 200);
7467 processSync(mapper);
7468 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7469 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7470 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7471
7472 // eraser
7473 processKey(mapper, BTN_TOOL_RUBBER, 1);
7474 processSync(mapper);
7475 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7476 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7477 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
7478
7479 // stylus
7480 processKey(mapper, BTN_TOOL_RUBBER, 0);
7481 processKey(mapper, BTN_TOOL_PEN, 1);
7482 processSync(mapper);
7483 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7484 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7485 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7486
7487 // brush
7488 processKey(mapper, BTN_TOOL_PEN, 0);
7489 processKey(mapper, BTN_TOOL_BRUSH, 1);
7490 processSync(mapper);
7491 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7492 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7493 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7494
7495 // pencil
7496 processKey(mapper, BTN_TOOL_BRUSH, 0);
7497 processKey(mapper, BTN_TOOL_PENCIL, 1);
7498 processSync(mapper);
7499 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7500 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7501 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7502
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08007503 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08007504 processKey(mapper, BTN_TOOL_PENCIL, 0);
7505 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
7506 processSync(mapper);
7507 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7508 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7509 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7510
7511 // mouse
7512 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
7513 processKey(mapper, BTN_TOOL_MOUSE, 1);
7514 processSync(mapper);
7515 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7516 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7517 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
7518
7519 // lens
7520 processKey(mapper, BTN_TOOL_MOUSE, 0);
7521 processKey(mapper, BTN_TOOL_LENS, 1);
7522 processSync(mapper);
7523 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7524 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7525 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
7526
7527 // double-tap
7528 processKey(mapper, BTN_TOOL_LENS, 0);
7529 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
7530 processSync(mapper);
7531 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7532 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7533 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7534
7535 // triple-tap
7536 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
7537 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
7538 processSync(mapper);
7539 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7540 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7541 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7542
7543 // quad-tap
7544 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
7545 processKey(mapper, BTN_TOOL_QUADTAP, 1);
7546 processSync(mapper);
7547 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7548 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7549 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7550
7551 // finger
7552 processKey(mapper, BTN_TOOL_QUADTAP, 0);
7553 processKey(mapper, BTN_TOOL_FINGER, 1);
7554 processSync(mapper);
7555 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7556 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7557 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7558
7559 // stylus trumps finger
7560 processKey(mapper, BTN_TOOL_PEN, 1);
7561 processSync(mapper);
7562 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7563 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7564 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7565
7566 // eraser trumps stylus
7567 processKey(mapper, BTN_TOOL_RUBBER, 1);
7568 processSync(mapper);
7569 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7570 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7571 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
7572
7573 // mouse trumps eraser
7574 processKey(mapper, BTN_TOOL_MOUSE, 1);
7575 processSync(mapper);
7576 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7577 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7578 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
7579
7580 // MT tool type trumps BTN tool types: MT_TOOL_FINGER
7581 processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
7582 processSync(mapper);
7583 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7584 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7585 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7586
7587 // MT tool type trumps BTN tool types: MT_TOOL_PEN
7588 processToolType(mapper, MT_TOOL_PEN);
7589 processSync(mapper);
7590 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7591 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7592 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7593
7594 // back to default tool type
7595 processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
7596 processKey(mapper, BTN_TOOL_MOUSE, 0);
7597 processKey(mapper, BTN_TOOL_RUBBER, 0);
7598 processKey(mapper, BTN_TOOL_PEN, 0);
7599 processKey(mapper, BTN_TOOL_FINGER, 0);
7600 processSync(mapper);
7601 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7602 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7603 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7604}
7605
7606TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007607 addConfigurationProperty("touch.deviceType", "touchScreen");
7608 prepareDisplay(DISPLAY_ORIENTATION_0);
7609 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007610 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007611 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007612
7613 NotifyMotionArgs motionArgs;
7614
7615 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
7616 processId(mapper, 1);
7617 processPosition(mapper, 100, 200);
7618 processSync(mapper);
7619 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7620 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
7621 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7622 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
7623
7624 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7625 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7626 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7627 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
7628
7629 // move a little
7630 processPosition(mapper, 150, 250);
7631 processSync(mapper);
7632 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7633 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7634 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7635 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7636
7637 // down when BTN_TOUCH is pressed, pressure defaults to 1
7638 processKey(mapper, BTN_TOUCH, 1);
7639 processSync(mapper);
7640 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7641 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
7642 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7643 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7644
7645 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7646 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7647 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7648 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
7649
7650 // up when BTN_TOUCH is released, hover restored
7651 processKey(mapper, BTN_TOUCH, 0);
7652 processSync(mapper);
7653 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7654 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7655 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7656 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
7657
7658 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7659 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
7660 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7661 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7662
7663 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7664 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7665 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7666 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7667
7668 // exit hover when pointer goes away
7669 processId(mapper, -1);
7670 processSync(mapper);
7671 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7672 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
7673 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7674 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7675}
7676
7677TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007678 addConfigurationProperty("touch.deviceType", "touchScreen");
7679 prepareDisplay(DISPLAY_ORIENTATION_0);
7680 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007681 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007682
7683 NotifyMotionArgs motionArgs;
7684
7685 // initially hovering because pressure is 0
7686 processId(mapper, 1);
7687 processPosition(mapper, 100, 200);
7688 processPressure(mapper, 0);
7689 processSync(mapper);
7690 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7691 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
7692 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7693 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
7694
7695 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7696 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7697 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7698 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
7699
7700 // move a little
7701 processPosition(mapper, 150, 250);
7702 processSync(mapper);
7703 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7704 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7705 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7706 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7707
7708 // down when pressure becomes non-zero
7709 processPressure(mapper, RAW_PRESSURE_MAX);
7710 processSync(mapper);
7711 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7712 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
7713 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7714 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7715
7716 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7717 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7718 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7719 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
7720
7721 // up when pressure becomes 0, hover restored
7722 processPressure(mapper, 0);
7723 processSync(mapper);
7724 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7725 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7726 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7727 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
7728
7729 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7730 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
7731 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7732 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7733
7734 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7735 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7736 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7737 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7738
7739 // exit hover when pointer goes away
7740 processId(mapper, -1);
7741 processSync(mapper);
7742 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7743 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
7744 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7745 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7746}
7747
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07007748/**
7749 * Set the input device port <--> display port associations, and check that the
7750 * events are routed to the display that matches the display port.
7751 * This can be checked by looking at the displayId of the resulting NotifyMotionArgs.
7752 */
7753TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayPort) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07007754 const std::string usb2 = "USB2";
7755 const uint8_t hdmi1 = 0;
7756 const uint8_t hdmi2 = 1;
7757 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007758 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07007759
7760 addConfigurationProperty("touch.deviceType", "touchScreen");
7761 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007762 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07007763
7764 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
7765 mFakePolicy->addInputPortAssociation(usb2, hdmi2);
7766
7767 // We are intentionally not adding the viewport for display 1 yet. Since the port association
7768 // for this input device is specified, and the matching viewport is not present,
7769 // the input device should be disabled (at the mapper level).
7770
7771 // Add viewport for display 2 on hdmi2
7772 prepareSecondaryDisplay(type, hdmi2);
7773 // Send a touch event
7774 processPosition(mapper, 100, 100);
7775 processSync(mapper);
7776 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7777
7778 // Add viewport for display 1 on hdmi1
7779 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
7780 // Send a touch event again
7781 processPosition(mapper, 100, 100);
7782 processSync(mapper);
7783
7784 NotifyMotionArgs args;
7785 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7786 ASSERT_EQ(DISPLAY_ID, args.displayId);
7787}
Michael Wrightd02c5b62014-02-10 15:10:22 -08007788
Arthur Hungc7ad2d02018-12-18 17:41:29 +08007789TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShouldHandleDisplayId) {
Garfield Tan888a6a42020-01-09 11:39:16 -08007790 // Setup for second display.
Michael Wright17db18e2020-06-26 20:51:44 +01007791 std::shared_ptr<FakePointerController> fakePointerController =
7792 std::make_shared<FakePointerController>();
Garfield Tan888a6a42020-01-09 11:39:16 -08007793 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08007794 fakePointerController->setPosition(100, 200);
7795 fakePointerController->setButtonState(0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08007796 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
7797
Garfield Tan888a6a42020-01-09 11:39:16 -08007798 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007799 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08007800
Arthur Hungc7ad2d02018-12-18 17:41:29 +08007801 prepareDisplay(DISPLAY_ORIENTATION_0);
7802 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007803 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08007804
7805 // Check source is mouse that would obtain the PointerController.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007806 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08007807
7808 NotifyMotionArgs motionArgs;
7809 processPosition(mapper, 100, 100);
7810 processSync(mapper);
7811
7812 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7813 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7814 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
7815}
7816
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00007817/**
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007818 * Ensure that the readTime is set to the SYN_REPORT value when processing touch events.
7819 */
7820TEST_F(MultiTouchInputMapperTest, Process_SendsReadTime) {
7821 addConfigurationProperty("touch.deviceType", "touchScreen");
7822 prepareAxes(POSITION);
7823 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7824
7825 prepareDisplay(DISPLAY_ORIENTATION_0);
7826 process(mapper, 10, 11 /*readTime*/, EV_ABS, ABS_MT_TRACKING_ID, 1);
7827 process(mapper, 15, 16 /*readTime*/, EV_ABS, ABS_MT_POSITION_X, 100);
7828 process(mapper, 20, 21 /*readTime*/, EV_ABS, ABS_MT_POSITION_Y, 100);
7829 process(mapper, 25, 26 /*readTime*/, EV_SYN, SYN_REPORT, 0);
7830
7831 NotifyMotionArgs args;
7832 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7833 ASSERT_EQ(26, args.readTime);
7834
7835 process(mapper, 30, 31 /*readTime*/, EV_ABS, ABS_MT_POSITION_X, 110);
7836 process(mapper, 30, 32 /*readTime*/, EV_ABS, ABS_MT_POSITION_Y, 220);
7837 process(mapper, 30, 33 /*readTime*/, EV_SYN, SYN_REPORT, 0);
7838
7839 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7840 ASSERT_EQ(33, args.readTime);
7841}
7842
7843/**
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00007844 * When the viewport is not active (isActive=false), the touch mapper should be disabled and the
7845 * events should not be delivered to the listener.
7846 */
7847TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreDropped) {
7848 addConfigurationProperty("touch.deviceType", "touchScreen");
7849 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
7850 DISPLAY_ORIENTATION_0, false /*isActive*/, UNIQUE_ID, NO_PORT,
7851 ViewportType::INTERNAL);
7852 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
7853 prepareAxes(POSITION);
7854 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7855
7856 NotifyMotionArgs motionArgs;
7857 processPosition(mapper, 100, 100);
7858 processSync(mapper);
7859
7860 mFakeListener->assertNotifyMotionWasNotCalled();
7861}
7862
Garfield Tanc734e4f2021-01-15 20:01:39 -08007863TEST_F(MultiTouchInputMapperTest, Process_DeactivateViewport_AbortTouches) {
7864 addConfigurationProperty("touch.deviceType", "touchScreen");
7865 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
7866 DISPLAY_ORIENTATION_0, true /*isActive*/, UNIQUE_ID, NO_PORT,
7867 ViewportType::INTERNAL);
7868 std::optional<DisplayViewport> optionalDisplayViewport =
7869 mFakePolicy->getDisplayViewportByUniqueId(UNIQUE_ID);
7870 ASSERT_TRUE(optionalDisplayViewport.has_value());
7871 DisplayViewport displayViewport = *optionalDisplayViewport;
7872
7873 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
7874 prepareAxes(POSITION);
7875 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7876
7877 // Finger down
7878 int32_t x = 100, y = 100;
7879 processPosition(mapper, x, y);
7880 processSync(mapper);
7881
7882 NotifyMotionArgs motionArgs;
7883 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7884 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7885
7886 // Deactivate display viewport
7887 displayViewport.isActive = false;
7888 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
7889 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
7890
7891 // Finger move
7892 x += 10, y += 10;
7893 processPosition(mapper, x, y);
7894 processSync(mapper);
7895
7896 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7897 EXPECT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
7898
7899 // Reactivate display viewport
7900 displayViewport.isActive = true;
7901 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
7902 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
7903
7904 // Finger move again
7905 x += 10, y += 10;
7906 processPosition(mapper, x, y);
7907 processSync(mapper);
7908
7909 // Gesture is aborted, so events after display is activated won't be dispatched until there is
7910 // no pointer on the touch device.
7911 mFakeListener->assertNotifyMotionWasNotCalled();
7912}
7913
Arthur Hung7c645402019-01-25 17:45:42 +08007914TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShowTouches) {
7915 // Setup the first touch screen device.
Arthur Hung7c645402019-01-25 17:45:42 +08007916 prepareAxes(POSITION | ID | SLOT);
7917 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007918 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung7c645402019-01-25 17:45:42 +08007919
7920 // Create the second touch screen device, and enable multi fingers.
7921 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08007922 const std::string DEVICE_NAME2 = "TOUCHSCREEN2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08007923 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007924 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08007925 std::shared_ptr<InputDevice> device2 =
7926 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
7927 Flags<InputDeviceClass>(0));
7928
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007929 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
7930 0 /*flat*/, 0 /*fuzz*/);
7931 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
7932 0 /*flat*/, 0 /*fuzz*/);
7933 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX,
7934 0 /*flat*/, 0 /*fuzz*/);
7935 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX,
7936 0 /*flat*/, 0 /*fuzz*/);
7937 mFakeEventHub->setAbsoluteAxisValue(SECOND_EVENTHUB_ID, ABS_MT_SLOT, 0 /*value*/);
7938 mFakeEventHub->addConfigurationProperty(SECOND_EVENTHUB_ID, String8("touch.deviceType"),
7939 String8("touchScreen"));
Arthur Hung7c645402019-01-25 17:45:42 +08007940
7941 // Setup the second touch screen device.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007942 MultiTouchInputMapper& mapper2 = device2->addMapper<MultiTouchInputMapper>(SECOND_EVENTHUB_ID);
Arthur Hung7c645402019-01-25 17:45:42 +08007943 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
7944 device2->reset(ARBITRARY_TIME);
7945
7946 // Setup PointerController.
Michael Wright17db18e2020-06-26 20:51:44 +01007947 std::shared_ptr<FakePointerController> fakePointerController =
7948 std::make_shared<FakePointerController>();
Arthur Hung7c645402019-01-25 17:45:42 +08007949 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
7950 mFakePolicy->setPointerController(SECOND_DEVICE_ID, fakePointerController);
7951
7952 // Setup policy for associated displays and show touches.
7953 const uint8_t hdmi1 = 0;
7954 const uint8_t hdmi2 = 1;
7955 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
7956 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
7957 mFakePolicy->setShowTouches(true);
7958
7959 // Create displays.
7960 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007961 prepareSecondaryDisplay(ViewportType::EXTERNAL, hdmi2);
Arthur Hung7c645402019-01-25 17:45:42 +08007962
7963 // Default device will reconfigure above, need additional reconfiguration for another device.
7964 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007965 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung7c645402019-01-25 17:45:42 +08007966
7967 // Two fingers down at default display.
7968 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7969 processPosition(mapper, x1, y1);
7970 processId(mapper, 1);
7971 processSlot(mapper, 1);
7972 processPosition(mapper, x2, y2);
7973 processId(mapper, 2);
7974 processSync(mapper);
7975
7976 std::map<int32_t, std::vector<int32_t>>::const_iterator iter =
7977 fakePointerController->getSpots().find(DISPLAY_ID);
7978 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
7979 ASSERT_EQ(size_t(2), iter->second.size());
7980
7981 // Two fingers down at second display.
7982 processPosition(mapper2, x1, y1);
7983 processId(mapper2, 1);
7984 processSlot(mapper2, 1);
7985 processPosition(mapper2, x2, y2);
7986 processId(mapper2, 2);
7987 processSync(mapper2);
7988
7989 iter = fakePointerController->getSpots().find(SECONDARY_DISPLAY_ID);
7990 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
7991 ASSERT_EQ(size_t(2), iter->second.size());
7992}
7993
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06007994TEST_F(MultiTouchInputMapperTest, VideoFrames_ReceivedByListener) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06007995 prepareAxes(POSITION);
7996 addConfigurationProperty("touch.deviceType", "touchScreen");
7997 prepareDisplay(DISPLAY_ORIENTATION_0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007998 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06007999
8000 NotifyMotionArgs motionArgs;
8001 // Unrotated video frame
8002 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8003 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008004 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008005 processPosition(mapper, 100, 200);
8006 processSync(mapper);
8007 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8008 ASSERT_EQ(frames, motionArgs.videoFrames);
8009
8010 // Subsequent touch events should not have any videoframes
8011 // This is implemented separately in FakeEventHub,
8012 // but that should match the behaviour of TouchVideoDevice.
8013 processPosition(mapper, 200, 200);
8014 processSync(mapper);
8015 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8016 ASSERT_EQ(std::vector<TouchVideoFrame>(), motionArgs.videoFrames);
8017}
8018
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008019TEST_F(MultiTouchInputMapperTest, VideoFrames_AreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008020 prepareAxes(POSITION);
8021 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008022 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008023 // Unrotated video frame
8024 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8025 NotifyMotionArgs motionArgs;
8026
8027 // Test all 4 orientations
8028 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008029 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
8030 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
8031 clearViewports();
8032 prepareDisplay(orientation);
8033 std::vector<TouchVideoFrame> frames{frame};
8034 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
8035 processPosition(mapper, 100, 200);
8036 processSync(mapper);
8037 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8038 ASSERT_EQ(frames, motionArgs.videoFrames);
8039 }
8040}
8041
8042TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_AreRotated) {
8043 prepareAxes(POSITION);
8044 addConfigurationProperty("touch.deviceType", "touchScreen");
8045 // Since InputReader works in the un-rotated coordinate space, only devices that are not
8046 // orientation-aware are affected by display rotation.
8047 addConfigurationProperty("touch.orientationAware", "0");
8048 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8049 // Unrotated video frame
8050 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8051 NotifyMotionArgs motionArgs;
8052
8053 // Test all 4 orientations
8054 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008055 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
8056 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
8057 clearViewports();
8058 prepareDisplay(orientation);
8059 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008060 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008061 processPosition(mapper, 100, 200);
8062 processSync(mapper);
8063 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008064 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
8065 // compared to the display. This is so that when the window transform (which contains the
8066 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
8067 // window's coordinate space.
8068 frames[0].rotate(getInverseRotation(orientation));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008069 ASSERT_EQ(frames, motionArgs.videoFrames);
8070 }
8071}
8072
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008073TEST_F(MultiTouchInputMapperTest, VideoFrames_MultipleFramesAreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008074 prepareAxes(POSITION);
8075 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008076 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008077 // Unrotated video frames. There's no rule that they must all have the same dimensions,
8078 // so mix these.
8079 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8080 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
8081 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
8082 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
8083 NotifyMotionArgs motionArgs;
8084
8085 prepareDisplay(DISPLAY_ORIENTATION_90);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008086 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008087 processPosition(mapper, 100, 200);
8088 processSync(mapper);
8089 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008090 ASSERT_EQ(frames, motionArgs.videoFrames);
8091}
8092
8093TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_MultipleFramesAreRotated) {
8094 prepareAxes(POSITION);
8095 addConfigurationProperty("touch.deviceType", "touchScreen");
8096 // Since InputReader works in the un-rotated coordinate space, only devices that are not
8097 // orientation-aware are affected by display rotation.
8098 addConfigurationProperty("touch.orientationAware", "0");
8099 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8100 // Unrotated video frames. There's no rule that they must all have the same dimensions,
8101 // so mix these.
8102 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8103 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
8104 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
8105 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
8106 NotifyMotionArgs motionArgs;
8107
8108 prepareDisplay(DISPLAY_ORIENTATION_90);
8109 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
8110 processPosition(mapper, 100, 200);
8111 processSync(mapper);
8112 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8113 std::for_each(frames.begin(), frames.end(), [](TouchVideoFrame& frame) {
8114 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
8115 // compared to the display. This is so that when the window transform (which contains the
8116 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
8117 // window's coordinate space.
8118 frame.rotate(getInverseRotation(DISPLAY_ORIENTATION_90));
8119 });
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008120 ASSERT_EQ(frames, motionArgs.videoFrames);
8121}
8122
Arthur Hung9da14732019-09-02 16:16:58 +08008123/**
8124 * If we had defined port associations, but the viewport is not ready, the touch device would be
8125 * expected to be disabled, and it should be enabled after the viewport has found.
8126 */
8127TEST_F(MultiTouchInputMapperTest, Configure_EnabledForAssociatedDisplay) {
Arthur Hung9da14732019-09-02 16:16:58 +08008128 constexpr uint8_t hdmi2 = 1;
8129 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008130 constexpr ViewportType type = ViewportType::EXTERNAL;
Arthur Hung9da14732019-09-02 16:16:58 +08008131
8132 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi2);
8133
8134 addConfigurationProperty("touch.deviceType", "touchScreen");
8135 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008136 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung9da14732019-09-02 16:16:58 +08008137
8138 ASSERT_EQ(mDevice->isEnabled(), false);
8139
8140 // Add display on hdmi2, the device should be enabled and can receive touch event.
8141 prepareSecondaryDisplay(type, hdmi2);
8142 ASSERT_EQ(mDevice->isEnabled(), true);
8143
8144 // Send a touch event.
8145 processPosition(mapper, 100, 100);
8146 processSync(mapper);
8147
8148 NotifyMotionArgs args;
8149 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8150 ASSERT_EQ(SECONDARY_DISPLAY_ID, args.displayId);
8151}
8152
Arthur Hung421eb1c2020-01-16 00:09:42 +08008153TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleSingleTouch) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08008154 addConfigurationProperty("touch.deviceType", "touchScreen");
8155 prepareDisplay(DISPLAY_ORIENTATION_0);
8156 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008157 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08008158
8159 NotifyMotionArgs motionArgs;
8160
8161 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
8162 // finger down
8163 processId(mapper, 1);
8164 processPosition(mapper, x1, y1);
8165 processSync(mapper);
8166 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8167 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8168 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8169
8170 // finger move
8171 processId(mapper, 1);
8172 processPosition(mapper, x2, y2);
8173 processSync(mapper);
8174 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8175 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8176 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8177
8178 // finger up.
8179 processId(mapper, -1);
8180 processSync(mapper);
8181 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8182 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8183 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8184
8185 // new finger down
8186 processId(mapper, 1);
8187 processPosition(mapper, x3, y3);
8188 processSync(mapper);
8189 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8190 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8191 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8192}
8193
8194/**
arthurhungcc7f9802020-04-30 17:55:40 +08008195 * Test single touch should be canceled when received the MT_TOOL_PALM event, and the following
8196 * MOVE and UP events should be ignored.
Arthur Hung421eb1c2020-01-16 00:09:42 +08008197 */
arthurhungcc7f9802020-04-30 17:55:40 +08008198TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_SinglePointer) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08008199 addConfigurationProperty("touch.deviceType", "touchScreen");
8200 prepareDisplay(DISPLAY_ORIENTATION_0);
8201 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008202 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08008203
8204 NotifyMotionArgs motionArgs;
8205
8206 // default tool type is finger
8207 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
arthurhungcc7f9802020-04-30 17:55:40 +08008208 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008209 processPosition(mapper, x1, y1);
8210 processSync(mapper);
8211 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8212 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8213 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8214
8215 // Tool changed to MT_TOOL_PALM expect sending the cancel event.
8216 processToolType(mapper, MT_TOOL_PALM);
8217 processSync(mapper);
8218 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8219 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
8220
8221 // Ignore the following MOVE and UP events if had detect a palm event.
arthurhungcc7f9802020-04-30 17:55:40 +08008222 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008223 processPosition(mapper, x2, y2);
8224 processSync(mapper);
8225 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8226
8227 // finger up.
arthurhungcc7f9802020-04-30 17:55:40 +08008228 processId(mapper, INVALID_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008229 processSync(mapper);
8230 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8231
8232 // new finger down
arthurhungcc7f9802020-04-30 17:55:40 +08008233 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008234 processToolType(mapper, MT_TOOL_FINGER);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008235 processPosition(mapper, x3, y3);
8236 processSync(mapper);
8237 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8238 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8239 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8240}
8241
arthurhungbf89a482020-04-17 17:37:55 +08008242/**
arthurhungcc7f9802020-04-30 17:55:40 +08008243 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
8244 * and the rest active fingers could still be allowed to receive the events
arthurhungbf89a482020-04-17 17:37:55 +08008245 */
arthurhungcc7f9802020-04-30 17:55:40 +08008246TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_TwoPointers) {
arthurhungbf89a482020-04-17 17:37:55 +08008247 addConfigurationProperty("touch.deviceType", "touchScreen");
8248 prepareDisplay(DISPLAY_ORIENTATION_0);
8249 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
8250 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8251
8252 NotifyMotionArgs motionArgs;
8253
8254 // default tool type is finger
arthurhungcc7f9802020-04-30 17:55:40 +08008255 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
8256 processId(mapper, FIRST_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08008257 processPosition(mapper, x1, y1);
8258 processSync(mapper);
8259 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8260 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8261 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8262
8263 // Second finger down.
arthurhungcc7f9802020-04-30 17:55:40 +08008264 processSlot(mapper, SECOND_SLOT);
8265 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08008266 processPosition(mapper, x2, y2);
arthurhungcc7f9802020-04-30 17:55:40 +08008267 processSync(mapper);
8268 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8269 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
8270 motionArgs.action);
8271 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8272
8273 // If the tool type of the first finger changes to MT_TOOL_PALM,
8274 // we expect to receive ACTION_POINTER_UP with cancel flag.
8275 processSlot(mapper, FIRST_SLOT);
8276 processId(mapper, FIRST_TRACKING_ID);
8277 processToolType(mapper, MT_TOOL_PALM);
8278 processSync(mapper);
8279 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8280 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
8281 motionArgs.action);
8282 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
8283
8284 // The following MOVE events of second finger should be processed.
8285 processSlot(mapper, SECOND_SLOT);
8286 processId(mapper, SECOND_TRACKING_ID);
8287 processPosition(mapper, x2 + 1, y2 + 1);
8288 processSync(mapper);
8289 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8290 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8291 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8292
8293 // First finger up. It used to be in palm mode, and we already generated ACTION_POINTER_UP for
8294 // it. Second finger receive move.
8295 processSlot(mapper, FIRST_SLOT);
8296 processId(mapper, INVALID_TRACKING_ID);
8297 processSync(mapper);
8298 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8299 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8300 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8301
8302 // Second finger keeps moving.
8303 processSlot(mapper, SECOND_SLOT);
8304 processId(mapper, SECOND_TRACKING_ID);
8305 processPosition(mapper, x2 + 2, y2 + 2);
8306 processSync(mapper);
8307 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8308 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8309 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8310
8311 // Second finger up.
8312 processId(mapper, INVALID_TRACKING_ID);
8313 processSync(mapper);
8314 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8315 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8316 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
8317}
8318
8319/**
8320 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event, if only 1 finger
8321 * is active, it should send CANCEL after receiving the MT_TOOL_PALM event.
8322 */
8323TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_ShouldCancelWhenAllTouchIsPalm) {
8324 addConfigurationProperty("touch.deviceType", "touchScreen");
8325 prepareDisplay(DISPLAY_ORIENTATION_0);
8326 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
8327 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8328
8329 NotifyMotionArgs motionArgs;
8330
8331 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
8332 // First finger down.
8333 processId(mapper, FIRST_TRACKING_ID);
8334 processPosition(mapper, x1, y1);
8335 processSync(mapper);
8336 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8337 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8338 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8339
8340 // Second finger down.
8341 processSlot(mapper, SECOND_SLOT);
8342 processId(mapper, SECOND_TRACKING_ID);
8343 processPosition(mapper, x2, y2);
arthurhungbf89a482020-04-17 17:37:55 +08008344 processSync(mapper);
8345 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8346 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
8347 motionArgs.action);
8348 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8349
arthurhungcc7f9802020-04-30 17:55:40 +08008350 // If the tool type of the first finger changes to MT_TOOL_PALM,
8351 // we expect to receive ACTION_POINTER_UP with cancel flag.
8352 processSlot(mapper, FIRST_SLOT);
8353 processId(mapper, FIRST_TRACKING_ID);
8354 processToolType(mapper, MT_TOOL_PALM);
8355 processSync(mapper);
8356 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8357 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
8358 motionArgs.action);
8359 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
8360
8361 // Second finger keeps moving.
8362 processSlot(mapper, SECOND_SLOT);
8363 processId(mapper, SECOND_TRACKING_ID);
8364 processPosition(mapper, x2 + 1, y2 + 1);
8365 processSync(mapper);
8366 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8367 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8368
8369 // second finger becomes palm, receive cancel due to only 1 finger is active.
8370 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08008371 processToolType(mapper, MT_TOOL_PALM);
8372 processSync(mapper);
8373 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8374 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
8375
arthurhungcc7f9802020-04-30 17:55:40 +08008376 // third finger down.
8377 processSlot(mapper, THIRD_SLOT);
8378 processId(mapper, THIRD_TRACKING_ID);
8379 processToolType(mapper, MT_TOOL_FINGER);
arthurhungbf89a482020-04-17 17:37:55 +08008380 processPosition(mapper, x3, y3);
8381 processSync(mapper);
arthurhungbf89a482020-04-17 17:37:55 +08008382 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8383 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8384 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08008385 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8386
8387 // third finger move
8388 processId(mapper, THIRD_TRACKING_ID);
8389 processPosition(mapper, x3 + 1, y3 + 1);
8390 processSync(mapper);
8391 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8392 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8393
8394 // first finger up, third finger receive move.
8395 processSlot(mapper, FIRST_SLOT);
8396 processId(mapper, INVALID_TRACKING_ID);
8397 processSync(mapper);
8398 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8399 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8400 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8401
8402 // second finger up, third finger receive move.
8403 processSlot(mapper, SECOND_SLOT);
8404 processId(mapper, INVALID_TRACKING_ID);
8405 processSync(mapper);
8406 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8407 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8408 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8409
8410 // third finger up.
8411 processSlot(mapper, THIRD_SLOT);
8412 processId(mapper, INVALID_TRACKING_ID);
8413 processSync(mapper);
8414 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8415 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8416 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
8417}
8418
8419/**
8420 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
8421 * and the active finger could still be allowed to receive the events
8422 */
8423TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_KeepFirstPointer) {
8424 addConfigurationProperty("touch.deviceType", "touchScreen");
8425 prepareDisplay(DISPLAY_ORIENTATION_0);
8426 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
8427 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8428
8429 NotifyMotionArgs motionArgs;
8430
8431 // default tool type is finger
8432 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
8433 processId(mapper, FIRST_TRACKING_ID);
8434 processPosition(mapper, x1, y1);
8435 processSync(mapper);
8436 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8437 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8438 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8439
8440 // Second finger down.
8441 processSlot(mapper, SECOND_SLOT);
8442 processId(mapper, SECOND_TRACKING_ID);
8443 processPosition(mapper, x2, y2);
8444 processSync(mapper);
8445 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8446 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
8447 motionArgs.action);
8448 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8449
8450 // If the tool type of the second finger changes to MT_TOOL_PALM,
8451 // we expect to receive ACTION_POINTER_UP with cancel flag.
8452 processId(mapper, SECOND_TRACKING_ID);
8453 processToolType(mapper, MT_TOOL_PALM);
8454 processSync(mapper);
8455 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8456 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
8457 motionArgs.action);
8458 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
8459
8460 // The following MOVE event should be processed.
8461 processSlot(mapper, FIRST_SLOT);
8462 processId(mapper, FIRST_TRACKING_ID);
8463 processPosition(mapper, x1 + 1, y1 + 1);
8464 processSync(mapper);
8465 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8466 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8467 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8468
8469 // second finger up.
8470 processSlot(mapper, SECOND_SLOT);
8471 processId(mapper, INVALID_TRACKING_ID);
8472 processSync(mapper);
8473 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8474 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8475
8476 // first finger keep moving
8477 processSlot(mapper, FIRST_SLOT);
8478 processId(mapper, FIRST_TRACKING_ID);
8479 processPosition(mapper, x1 + 2, y1 + 2);
8480 processSync(mapper);
8481 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8482 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8483
8484 // first finger up.
8485 processId(mapper, INVALID_TRACKING_ID);
8486 processSync(mapper);
8487 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8488 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8489 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
arthurhungbf89a482020-04-17 17:37:55 +08008490}
8491
Arthur Hung9ad18942021-06-19 02:04:46 +00008492/**
8493 * Test multi-touch should sent ACTION_POINTER_UP/ACTION_UP when received the INVALID_TRACKING_ID,
8494 * to prevent the driver side may send unexpected data after set tracking id as INVALID_TRACKING_ID
8495 * cause slot be valid again.
8496 */
8497TEST_F(MultiTouchInputMapperTest, Process_MultiTouch_WithInvalidTrackingId) {
8498 addConfigurationProperty("touch.deviceType", "touchScreen");
8499 prepareDisplay(DISPLAY_ORIENTATION_0);
8500 prepareAxes(POSITION | ID | SLOT | PRESSURE);
8501 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8502
8503 NotifyMotionArgs motionArgs;
8504
8505 constexpr int32_t x1 = 100, y1 = 200, x2 = 0, y2 = 0;
8506 // First finger down.
8507 processId(mapper, FIRST_TRACKING_ID);
8508 processPosition(mapper, x1, y1);
8509 processPressure(mapper, RAW_PRESSURE_MAX);
8510 processSync(mapper);
8511 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8512 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8513 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8514
8515 // First finger move.
8516 processId(mapper, FIRST_TRACKING_ID);
8517 processPosition(mapper, x1 + 1, y1 + 1);
8518 processPressure(mapper, RAW_PRESSURE_MAX);
8519 processSync(mapper);
8520 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8521 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8522 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8523
8524 // Second finger down.
8525 processSlot(mapper, SECOND_SLOT);
8526 processId(mapper, SECOND_TRACKING_ID);
8527 processPosition(mapper, x2, y2);
8528 processPressure(mapper, RAW_PRESSURE_MAX);
8529 processSync(mapper);
8530 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8531 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
8532 motionArgs.action);
8533 ASSERT_EQ(uint32_t(2), motionArgs.pointerCount);
8534
8535 // second finger up with some unexpected data.
8536 processSlot(mapper, SECOND_SLOT);
8537 processId(mapper, INVALID_TRACKING_ID);
8538 processPosition(mapper, x2, y2);
8539 processSync(mapper);
8540 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8541 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
8542 motionArgs.action);
8543 ASSERT_EQ(uint32_t(2), motionArgs.pointerCount);
8544
8545 // first finger up with some unexpected data.
8546 processSlot(mapper, FIRST_SLOT);
8547 processId(mapper, INVALID_TRACKING_ID);
8548 processPosition(mapper, x2, y2);
8549 processPressure(mapper, RAW_PRESSURE_MAX);
8550 processSync(mapper);
8551 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8552 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8553 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8554}
8555
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008556// --- MultiTouchInputMapperTest_ExternalDevice ---
8557
8558class MultiTouchInputMapperTest_ExternalDevice : public MultiTouchInputMapperTest {
8559protected:
Chris Yea52ade12020-08-27 16:49:20 -07008560 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008561};
8562
8563/**
8564 * Expect fallback to internal viewport if device is external and external viewport is not present.
8565 */
8566TEST_F(MultiTouchInputMapperTest_ExternalDevice, Viewports_Fallback) {
8567 prepareAxes(POSITION);
8568 addConfigurationProperty("touch.deviceType", "touchScreen");
8569 prepareDisplay(DISPLAY_ORIENTATION_0);
8570 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8571
8572 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
8573
8574 NotifyMotionArgs motionArgs;
8575
8576 // Expect the event to be sent to the internal viewport,
8577 // because an external viewport is not present.
8578 processPosition(mapper, 100, 100);
8579 processSync(mapper);
8580 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8581 ASSERT_EQ(ADISPLAY_ID_DEFAULT, motionArgs.displayId);
8582
8583 // Expect the event to be sent to the external viewport if it is present.
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008584 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008585 processPosition(mapper, 100, 100);
8586 processSync(mapper);
8587 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8588 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
8589}
Arthur Hung4197f6b2020-03-16 15:39:59 +08008590
8591/**
8592 * Test touch should not work if outside of surface.
8593 */
8594class MultiTouchInputMapperTest_SurfaceRange : public MultiTouchInputMapperTest {
8595protected:
8596 void halfDisplayToCenterHorizontal(int32_t orientation) {
8597 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008598 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Arthur Hung4197f6b2020-03-16 15:39:59 +08008599
8600 // Half display to (width/4, 0, width * 3/4, height) to make display has offset.
8601 internalViewport->orientation = orientation;
8602 if (orientation == DISPLAY_ORIENTATION_90 || orientation == DISPLAY_ORIENTATION_270) {
8603 internalViewport->logicalLeft = 0;
8604 internalViewport->logicalTop = 0;
8605 internalViewport->logicalRight = DISPLAY_HEIGHT;
8606 internalViewport->logicalBottom = DISPLAY_WIDTH / 2;
8607
8608 internalViewport->physicalLeft = 0;
8609 internalViewport->physicalTop = DISPLAY_WIDTH / 4;
8610 internalViewport->physicalRight = DISPLAY_HEIGHT;
8611 internalViewport->physicalBottom = DISPLAY_WIDTH * 3 / 4;
8612
8613 internalViewport->deviceWidth = DISPLAY_HEIGHT;
8614 internalViewport->deviceHeight = DISPLAY_WIDTH;
8615 } else {
8616 internalViewport->logicalLeft = 0;
8617 internalViewport->logicalTop = 0;
8618 internalViewport->logicalRight = DISPLAY_WIDTH / 2;
8619 internalViewport->logicalBottom = DISPLAY_HEIGHT;
8620
8621 internalViewport->physicalLeft = DISPLAY_WIDTH / 4;
8622 internalViewport->physicalTop = 0;
8623 internalViewport->physicalRight = DISPLAY_WIDTH * 3 / 4;
8624 internalViewport->physicalBottom = DISPLAY_HEIGHT;
8625
8626 internalViewport->deviceWidth = DISPLAY_WIDTH;
8627 internalViewport->deviceHeight = DISPLAY_HEIGHT;
8628 }
8629
8630 mFakePolicy->updateViewport(internalViewport.value());
8631 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8632 }
8633
arthurhung5d547942020-12-14 17:04:45 +08008634 void processPositionAndVerify(MultiTouchInputMapper& mapper, int32_t xOutside, int32_t yOutside,
8635 int32_t xInside, int32_t yInside, int32_t xExpected,
Arthur Hung4197f6b2020-03-16 15:39:59 +08008636 int32_t yExpected) {
8637 // touch on outside area should not work.
8638 processPosition(mapper, toRawX(xOutside), toRawY(yOutside));
8639 processSync(mapper);
8640 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8641
8642 // touch on inside area should receive the event.
8643 NotifyMotionArgs args;
8644 processPosition(mapper, toRawX(xInside), toRawY(yInside));
8645 processSync(mapper);
8646 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8647 ASSERT_NEAR(xExpected, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
8648 ASSERT_NEAR(yExpected, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
8649
8650 // Reset.
8651 mapper.reset(ARBITRARY_TIME);
8652 }
8653};
8654
arthurhung5d547942020-12-14 17:04:45 +08008655TEST_F(MultiTouchInputMapperTest_SurfaceRange, Viewports_SurfaceRange) {
Arthur Hung4197f6b2020-03-16 15:39:59 +08008656 addConfigurationProperty("touch.deviceType", "touchScreen");
8657 prepareDisplay(DISPLAY_ORIENTATION_0);
8658 prepareAxes(POSITION);
8659 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8660
8661 // Touch on center of normal display should work.
8662 const int32_t x = DISPLAY_WIDTH / 4;
8663 const int32_t y = DISPLAY_HEIGHT / 2;
8664 processPosition(mapper, toRawX(x), toRawY(y));
8665 processSync(mapper);
8666 NotifyMotionArgs args;
8667 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8668 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], x, y, 1.0f, 0.0f, 0.0f, 0.0f,
8669 0.0f, 0.0f, 0.0f, 0.0f));
8670 // Reset.
8671 mapper.reset(ARBITRARY_TIME);
8672
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008673 // Let physical display be different to device, and make surface and physical could be 1:1 in
8674 // all four orientations.
8675 for (int orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90, DISPLAY_ORIENTATION_180,
8676 DISPLAY_ORIENTATION_270}) {
8677 halfDisplayToCenterHorizontal(orientation);
Arthur Hung4197f6b2020-03-16 15:39:59 +08008678
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008679 const int32_t xExpected = (x + 1) - (DISPLAY_WIDTH / 4);
8680 const int32_t yExpected = y;
8681 processPositionAndVerify(mapper, x - 1, y, x + 1, y, xExpected, yExpected);
8682 }
Arthur Hung4197f6b2020-03-16 15:39:59 +08008683}
8684
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008685TEST_F(MultiTouchInputMapperTest_SurfaceRange, Viewports_SurfaceRange_90_NotOrientationAware) {
Arthur Hung4197f6b2020-03-16 15:39:59 +08008686 addConfigurationProperty("touch.deviceType", "touchScreen");
8687 prepareDisplay(DISPLAY_ORIENTATION_0);
8688 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008689 // Since InputReader works in the un-rotated coordinate space, only devices that are not
8690 // orientation-aware are affected by display rotation.
8691 addConfigurationProperty("touch.orientationAware", "0");
Arthur Hung4197f6b2020-03-16 15:39:59 +08008692 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8693
8694 // Half display to (width/4, 0, width * 3/4, height) and rotate 90-degrees.
8695 halfDisplayToCenterHorizontal(DISPLAY_ORIENTATION_90);
8696
8697 const int32_t x = DISPLAY_WIDTH / 4;
8698 const int32_t y = DISPLAY_HEIGHT / 2;
8699
Arthur Hung4197f6b2020-03-16 15:39:59 +08008700 // expect x/y = swap x/y then reverse x.
8701 constexpr int32_t xExpected = DISPLAY_HEIGHT - y;
8702 constexpr int32_t yExpected = (x + 1) - DISPLAY_WIDTH / 4;
8703 processPositionAndVerify(mapper, x - 1, y, x + 1, y, xExpected, yExpected);
8704}
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08008705
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008706TEST_F(MultiTouchInputMapperTest_SurfaceRange, Viewports_SurfaceRange_270_NotOrientationAware) {
arthurhunga36b28e2020-12-29 20:28:15 +08008707 addConfigurationProperty("touch.deviceType", "touchScreen");
8708 prepareDisplay(DISPLAY_ORIENTATION_0);
8709 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008710 // Since InputReader works in the un-rotated coordinate space, only devices that are not
8711 // orientation-aware are affected by display rotation.
8712 addConfigurationProperty("touch.orientationAware", "0");
8713 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8714
8715 // Half display to (width/4, 0, width * 3/4, height) and rotate 270-degrees.
8716 halfDisplayToCenterHorizontal(DISPLAY_ORIENTATION_270);
8717
8718 const int32_t x = DISPLAY_WIDTH / 4;
8719 const int32_t y = DISPLAY_HEIGHT / 2;
8720
8721 // expect x/y = swap x/y then reverse y.
8722 const int32_t xExpected = y;
8723 const int32_t yExpected = (DISPLAY_WIDTH * 3 / 4) - (x + 1);
8724 processPositionAndVerify(mapper, x - 1, y, x + 1, y, xExpected, yExpected);
8725}
8726
8727TEST_F(MultiTouchInputMapperTest_SurfaceRange, Viewports_SurfaceRange_Corner_NotOrientationAware) {
8728 addConfigurationProperty("touch.deviceType", "touchScreen");
8729 prepareDisplay(DISPLAY_ORIENTATION_0);
8730 prepareAxes(POSITION);
8731 // Since InputReader works in the un-rotated coordinate space, only devices that are not
8732 // orientation-aware are affected by display rotation.
8733 addConfigurationProperty("touch.orientationAware", "0");
arthurhunga36b28e2020-12-29 20:28:15 +08008734 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8735
8736 const int32_t x = 0;
8737 const int32_t y = 0;
8738
8739 const int32_t xExpected = x;
8740 const int32_t yExpected = y;
8741 processPositionAndVerify(mapper, x - 1, y, x, y, xExpected, yExpected);
8742
8743 clearViewports();
8744 prepareDisplay(DISPLAY_ORIENTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008745 // expect x/y = swap x/y then reverse x.
8746 const int32_t xExpected90 = DISPLAY_HEIGHT - 1;
8747 const int32_t yExpected90 = x;
arthurhunga36b28e2020-12-29 20:28:15 +08008748 processPositionAndVerify(mapper, x - 1, y, x, y, xExpected90, yExpected90);
8749
8750 clearViewports();
8751 prepareDisplay(DISPLAY_ORIENTATION_270);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008752 // expect x/y = swap x/y then reverse y.
8753 const int32_t xExpected270 = y;
8754 const int32_t yExpected270 = DISPLAY_WIDTH - 1;
arthurhunga36b28e2020-12-29 20:28:15 +08008755 processPositionAndVerify(mapper, x - 1, y, x, y, xExpected270, yExpected270);
8756}
8757
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08008758TEST_F(MultiTouchInputMapperTest, Process_TouchpadCapture) {
8759 // we need a pointer controller for mouse mode of touchpad (start pointer at 0,0)
8760 std::shared_ptr<FakePointerController> fakePointerController =
8761 std::make_shared<FakePointerController>();
8762 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
8763 fakePointerController->setPosition(0, 0);
8764 fakePointerController->setButtonState(0);
8765
8766 // prepare device and capture
8767 prepareDisplay(DISPLAY_ORIENTATION_0);
8768 prepareAxes(POSITION | ID | SLOT);
8769 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
8770 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
8771 mFakePolicy->setPointerCapture(true);
8772 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
8773 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8774
8775 // captured touchpad should be a touchpad source
8776 NotifyDeviceResetArgs resetArgs;
8777 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
8778 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
8779
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00008780 InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
Chris Yef74dc422020-09-02 22:41:50 -07008781
8782 const InputDeviceInfo::MotionRange* relRangeX =
8783 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_X, AINPUT_SOURCE_TOUCHPAD);
8784 ASSERT_NE(relRangeX, nullptr);
8785 ASSERT_EQ(relRangeX->min, -(RAW_X_MAX - RAW_X_MIN));
8786 ASSERT_EQ(relRangeX->max, RAW_X_MAX - RAW_X_MIN);
8787 const InputDeviceInfo::MotionRange* relRangeY =
8788 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_Y, AINPUT_SOURCE_TOUCHPAD);
8789 ASSERT_NE(relRangeY, nullptr);
8790 ASSERT_EQ(relRangeY->min, -(RAW_Y_MAX - RAW_Y_MIN));
8791 ASSERT_EQ(relRangeY->max, RAW_Y_MAX - RAW_Y_MIN);
8792
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08008793 // run captured pointer tests - note that this is unscaled, so input listener events should be
8794 // identical to what the hardware sends (accounting for any
8795 // calibration).
8796 // FINGER 0 DOWN
Chris Ye364fdb52020-08-05 15:07:56 -07008797 processSlot(mapper, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08008798 processId(mapper, 1);
8799 processPosition(mapper, 100 + RAW_X_MIN, 100 + RAW_Y_MIN);
8800 processKey(mapper, BTN_TOUCH, 1);
8801 processSync(mapper);
8802
8803 // expect coord[0] to contain initial location of touch 0
8804 NotifyMotionArgs args;
8805 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8806 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
8807 ASSERT_EQ(1U, args.pointerCount);
8808 ASSERT_EQ(0, args.pointerProperties[0].id);
8809 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, args.source);
8810 ASSERT_NO_FATAL_FAILURE(
8811 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
8812
8813 // FINGER 1 DOWN
8814 processSlot(mapper, 1);
8815 processId(mapper, 2);
8816 processPosition(mapper, 560 + RAW_X_MIN, 154 + RAW_Y_MIN);
8817 processSync(mapper);
8818
8819 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
8820 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Chris Ye364fdb52020-08-05 15:07:56 -07008821 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
8822 args.action);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08008823 ASSERT_EQ(2U, args.pointerCount);
8824 ASSERT_EQ(0, args.pointerProperties[0].id);
8825 ASSERT_EQ(1, args.pointerProperties[1].id);
8826 ASSERT_NO_FATAL_FAILURE(
8827 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
8828 ASSERT_NO_FATAL_FAILURE(
8829 assertPointerCoords(args.pointerCoords[1], 560, 154, 1, 0, 0, 0, 0, 0, 0, 0));
8830
8831 // FINGER 1 MOVE
8832 processPosition(mapper, 540 + RAW_X_MIN, 690 + RAW_Y_MIN);
8833 processSync(mapper);
8834
8835 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
8836 // from move
8837 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8838 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
8839 ASSERT_NO_FATAL_FAILURE(
8840 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
8841 ASSERT_NO_FATAL_FAILURE(
8842 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
8843
8844 // FINGER 0 MOVE
8845 processSlot(mapper, 0);
8846 processPosition(mapper, 50 + RAW_X_MIN, 800 + RAW_Y_MIN);
8847 processSync(mapper);
8848
8849 // expect coord[0] to contain new touch 0 location, coord[1] to contain previous location
8850 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8851 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
8852 ASSERT_NO_FATAL_FAILURE(
8853 assertPointerCoords(args.pointerCoords[0], 50, 800, 1, 0, 0, 0, 0, 0, 0, 0));
8854 ASSERT_NO_FATAL_FAILURE(
8855 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
8856
8857 // BUTTON DOWN
8858 processKey(mapper, BTN_LEFT, 1);
8859 processSync(mapper);
8860
8861 // touchinputmapper design sends a move before button press
8862 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8863 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
8864 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8865 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
8866
8867 // BUTTON UP
8868 processKey(mapper, BTN_LEFT, 0);
8869 processSync(mapper);
8870
8871 // touchinputmapper design sends a move after button release
8872 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8873 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
8874 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8875 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
8876
8877 // FINGER 0 UP
8878 processId(mapper, -1);
8879 processSync(mapper);
8880 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8881 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | 0x0000, args.action);
8882
8883 // FINGER 1 MOVE
8884 processSlot(mapper, 1);
8885 processPosition(mapper, 320 + RAW_X_MIN, 900 + RAW_Y_MIN);
8886 processSync(mapper);
8887
8888 // expect coord[0] to contain new location of touch 1, and properties[0].id to contain 1
8889 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8890 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
8891 ASSERT_EQ(1U, args.pointerCount);
8892 ASSERT_EQ(1, args.pointerProperties[0].id);
8893 ASSERT_NO_FATAL_FAILURE(
8894 assertPointerCoords(args.pointerCoords[0], 320, 900, 1, 0, 0, 0, 0, 0, 0, 0));
8895
8896 // FINGER 1 UP
8897 processId(mapper, -1);
8898 processKey(mapper, BTN_TOUCH, 0);
8899 processSync(mapper);
8900 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8901 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
8902
8903 // non captured touchpad should be a mouse source
8904 mFakePolicy->setPointerCapture(false);
8905 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
8906 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
8907 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
8908}
8909
8910TEST_F(MultiTouchInputMapperTest, Process_UnCapturedTouchpadPointer) {
8911 std::shared_ptr<FakePointerController> fakePointerController =
8912 std::make_shared<FakePointerController>();
8913 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
8914 fakePointerController->setPosition(0, 0);
8915 fakePointerController->setButtonState(0);
8916
8917 // prepare device and capture
8918 prepareDisplay(DISPLAY_ORIENTATION_0);
8919 prepareAxes(POSITION | ID | SLOT);
8920 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
8921 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
8922 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
8923 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8924 // run uncaptured pointer tests - pushes out generic events
8925 // FINGER 0 DOWN
8926 processId(mapper, 3);
8927 processPosition(mapper, 100, 100);
8928 processKey(mapper, BTN_TOUCH, 1);
8929 processSync(mapper);
8930
8931 // start at (100,100), cursor should be at (0,0) * scale
8932 NotifyMotionArgs args;
8933 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8934 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
8935 ASSERT_NO_FATAL_FAILURE(
8936 assertPointerCoords(args.pointerCoords[0], 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
8937
8938 // FINGER 0 MOVE
8939 processPosition(mapper, 200, 200);
8940 processSync(mapper);
8941
8942 // compute scaling to help with touch position checking
8943 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
8944 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
8945 float scale =
8946 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
8947
8948 // translate from (100,100) -> (200,200), cursor should have changed to (100,100) * scale)
8949 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8950 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
8951 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 100 * scale, 100 * scale, 0,
8952 0, 0, 0, 0, 0, 0, 0));
8953}
8954
8955TEST_F(MultiTouchInputMapperTest, WhenCapturedAndNotCaptured_GetSources) {
8956 std::shared_ptr<FakePointerController> fakePointerController =
8957 std::make_shared<FakePointerController>();
8958
8959 prepareDisplay(DISPLAY_ORIENTATION_0);
8960 prepareAxes(POSITION | ID | SLOT);
8961 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
8962 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
8963 mFakePolicy->setPointerCapture(false);
8964 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8965
8966 // uncaptured touchpad should be a pointer device
8967 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
8968
8969 // captured touchpad should be a touchpad device
8970 mFakePolicy->setPointerCapture(true);
8971 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
8972 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
8973}
8974
Chris Ye1dd2e5c2021-04-04 23:12:41 -07008975// --- PeripheralControllerTest ---
Chris Yee2b1e5c2021-03-10 22:45:12 -08008976
Chris Ye1dd2e5c2021-04-04 23:12:41 -07008977class PeripheralControllerTest : public testing::Test {
Chris Yee2b1e5c2021-03-10 22:45:12 -08008978protected:
8979 static const char* DEVICE_NAME;
8980 static const char* DEVICE_LOCATION;
8981 static const int32_t DEVICE_ID;
8982 static const int32_t DEVICE_GENERATION;
8983 static const int32_t DEVICE_CONTROLLER_NUMBER;
8984 static const Flags<InputDeviceClass> DEVICE_CLASSES;
8985 static const int32_t EVENTHUB_ID;
8986
8987 std::shared_ptr<FakeEventHub> mFakeEventHub;
8988 sp<FakeInputReaderPolicy> mFakePolicy;
8989 sp<TestInputListener> mFakeListener;
8990 std::unique_ptr<InstrumentedInputReader> mReader;
8991 std::shared_ptr<InputDevice> mDevice;
8992
8993 virtual void SetUp(Flags<InputDeviceClass> classes) {
8994 mFakeEventHub = std::make_unique<FakeEventHub>();
8995 mFakePolicy = new FakeInputReaderPolicy();
8996 mFakeListener = new TestInputListener();
8997 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
8998 mFakeListener);
8999 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
9000 }
9001
9002 void SetUp() override { SetUp(DEVICE_CLASSES); }
9003
9004 void TearDown() override {
9005 mFakeListener.clear();
9006 mFakePolicy.clear();
9007 }
9008
9009 void configureDevice(uint32_t changes) {
9010 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
9011 mReader->requestRefreshConfiguration(changes);
9012 mReader->loopOnce();
9013 }
9014 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
9015 }
9016
9017 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
9018 const std::string& location, int32_t eventHubId,
9019 Flags<InputDeviceClass> classes) {
9020 InputDeviceIdentifier identifier;
9021 identifier.name = name;
9022 identifier.location = location;
9023 std::shared_ptr<InputDevice> device =
9024 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
9025 identifier);
9026 mReader->pushNextDevice(device);
9027 mFakeEventHub->addDevice(eventHubId, name, classes);
9028 mReader->loopOnce();
9029 return device;
9030 }
9031
9032 template <class T, typename... Args>
9033 T& addControllerAndConfigure(Args... args) {
9034 T& controller = mDevice->addController<T>(EVENTHUB_ID, args...);
9035
9036 return controller;
9037 }
9038};
9039
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009040const char* PeripheralControllerTest::DEVICE_NAME = "device";
9041const char* PeripheralControllerTest::DEVICE_LOCATION = "BLUETOOTH";
9042const int32_t PeripheralControllerTest::DEVICE_ID = END_RESERVED_ID + 1000;
9043const int32_t PeripheralControllerTest::DEVICE_GENERATION = 2;
9044const int32_t PeripheralControllerTest::DEVICE_CONTROLLER_NUMBER = 0;
9045const Flags<InputDeviceClass> PeripheralControllerTest::DEVICE_CLASSES =
Chris Yee2b1e5c2021-03-10 22:45:12 -08009046 Flags<InputDeviceClass>(0); // not needed for current tests
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009047const int32_t PeripheralControllerTest::EVENTHUB_ID = 1;
Chris Yee2b1e5c2021-03-10 22:45:12 -08009048
9049// --- BatteryControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009050class BatteryControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -08009051protected:
9052 void SetUp() override {
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009053 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::BATTERY);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009054 }
9055};
9056
9057TEST_F(BatteryControllerTest, GetBatteryCapacity) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009058 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009059
9060 ASSERT_TRUE(controller.getBatteryCapacity(DEFAULT_BATTERY));
9061 ASSERT_EQ(controller.getBatteryCapacity(DEFAULT_BATTERY).value_or(-1), BATTERY_CAPACITY);
9062}
9063
9064TEST_F(BatteryControllerTest, GetBatteryStatus) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009065 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009066
9067 ASSERT_TRUE(controller.getBatteryStatus(DEFAULT_BATTERY));
9068 ASSERT_EQ(controller.getBatteryStatus(DEFAULT_BATTERY).value_or(-1), BATTERY_STATUS);
9069}
9070
9071// --- LightControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009072class LightControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -08009073protected:
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009074 void SetUp() override {
9075 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::LIGHT);
9076 }
Chris Yee2b1e5c2021-03-10 22:45:12 -08009077};
9078
Chris Ye85758332021-05-16 23:05:17 -07009079TEST_F(LightControllerTest, MonoLight) {
9080 RawLightInfo infoMono = {.id = 1,
9081 .name = "Mono",
9082 .maxBrightness = 255,
9083 .flags = InputLightClass::BRIGHTNESS,
9084 .path = ""};
9085 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
Chris Yee2b1e5c2021-03-10 22:45:12 -08009086
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009087 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009088 InputDeviceInfo info;
9089 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009090 std::vector<InputDeviceLightInfo> lights = info.getLights();
9091 ASSERT_EQ(1U, lights.size());
9092 ASSERT_EQ(InputDeviceLightType::MONO, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009093
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009094 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
9095 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009096}
9097
9098TEST_F(LightControllerTest, RGBLight) {
9099 RawLightInfo infoRed = {.id = 1,
9100 .name = "red",
9101 .maxBrightness = 255,
9102 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
9103 .path = ""};
9104 RawLightInfo infoGreen = {.id = 2,
9105 .name = "green",
9106 .maxBrightness = 255,
9107 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
9108 .path = ""};
9109 RawLightInfo infoBlue = {.id = 3,
9110 .name = "blue",
9111 .maxBrightness = 255,
9112 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
9113 .path = ""};
9114 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
9115 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
9116 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
9117
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009118 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009119 InputDeviceInfo info;
9120 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009121 std::vector<InputDeviceLightInfo> lights = info.getLights();
9122 ASSERT_EQ(1U, lights.size());
9123 ASSERT_EQ(InputDeviceLightType::RGB, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009124
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009125 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
9126 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009127}
9128
9129TEST_F(LightControllerTest, MultiColorRGBLight) {
9130 RawLightInfo infoColor = {.id = 1,
9131 .name = "red",
9132 .maxBrightness = 255,
9133 .flags = InputLightClass::BRIGHTNESS |
9134 InputLightClass::MULTI_INTENSITY |
9135 InputLightClass::MULTI_INDEX,
9136 .path = ""};
9137
9138 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
9139
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009140 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009141 InputDeviceInfo info;
9142 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009143 std::vector<InputDeviceLightInfo> lights = info.getLights();
9144 ASSERT_EQ(1U, lights.size());
9145 ASSERT_EQ(InputDeviceLightType::MULTI_COLOR, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009146
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009147 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
9148 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009149}
9150
9151TEST_F(LightControllerTest, PlayerIdLight) {
9152 RawLightInfo info1 = {.id = 1,
9153 .name = "player1",
9154 .maxBrightness = 255,
9155 .flags = InputLightClass::BRIGHTNESS,
9156 .path = ""};
9157 RawLightInfo info2 = {.id = 2,
9158 .name = "player2",
9159 .maxBrightness = 255,
9160 .flags = InputLightClass::BRIGHTNESS,
9161 .path = ""};
9162 RawLightInfo info3 = {.id = 3,
9163 .name = "player3",
9164 .maxBrightness = 255,
9165 .flags = InputLightClass::BRIGHTNESS,
9166 .path = ""};
9167 RawLightInfo info4 = {.id = 4,
9168 .name = "player4",
9169 .maxBrightness = 255,
9170 .flags = InputLightClass::BRIGHTNESS,
9171 .path = ""};
9172 mFakeEventHub->addRawLightInfo(info1.id, std::move(info1));
9173 mFakeEventHub->addRawLightInfo(info2.id, std::move(info2));
9174 mFakeEventHub->addRawLightInfo(info3.id, std::move(info3));
9175 mFakeEventHub->addRawLightInfo(info4.id, std::move(info4));
9176
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009177 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009178 InputDeviceInfo info;
9179 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009180 std::vector<InputDeviceLightInfo> lights = info.getLights();
9181 ASSERT_EQ(1U, lights.size());
9182 ASSERT_EQ(InputDeviceLightType::PLAYER_ID, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009183
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009184 ASSERT_FALSE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
9185 ASSERT_TRUE(controller.setLightPlayerId(lights[0].id, LIGHT_PLAYER_ID));
9186 ASSERT_EQ(controller.getLightPlayerId(lights[0].id).value_or(-1), LIGHT_PLAYER_ID);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009187}
9188
Michael Wrightd02c5b62014-02-10 15:10:22 -08009189} // namespace android