blob: 91d246a35fe18d53deceb1f920a20f117623aa34 [file] [log] [blame]
Michael Wrightd02c5b62014-02-10 15:10:22 -08001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Prabir Pradhan2770d242019-09-02 18:07:11 -070017#include <CursorInputMapper.h>
18#include <InputDevice.h>
Prabir Pradhanc14266f2021-05-12 15:56:24 -070019#include <InputFlingerProperties.sysprop.h>
Prabir Pradhan2770d242019-09-02 18:07:11 -070020#include <InputMapper.h>
21#include <InputReader.h>
Prabir Pradhan1aed8582019-12-30 11:46:51 -080022#include <InputReaderBase.h>
23#include <InputReaderFactory.h>
Prabir Pradhan2770d242019-09-02 18:07:11 -070024#include <KeyboardInputMapper.h>
25#include <MultiTouchInputMapper.h>
Chris Ye1dd2e5c2021-04-04 23:12:41 -070026#include <PeripheralController.h>
Chris Yef59a2f42020-10-16 12:55:26 -070027#include <SensorInputMapper.h>
Prabir Pradhan2770d242019-09-02 18:07:11 -070028#include <SingleTouchInputMapper.h>
29#include <SwitchInputMapper.h>
30#include <TestInputListener.h>
31#include <TouchInputMapper.h>
Prabir Pradhan1aed8582019-12-30 11:46:51 -080032#include <UinputDevice.h>
Chris Ye87143712020-11-10 05:05:58 +000033#include <VibratorInputMapper.h>
Prabir Pradhan2574dfa2019-10-16 16:35:07 -070034#include <android-base/thread_annotations.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080035#include <gtest/gtest.h>
chaviw3277faf2021-05-19 16:45:23 -050036#include <gui/constants.h>
Siarhei Vishniakou473174e2017-12-27 16:44:42 -080037#include <inttypes.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080038#include <math.h>
39
Michael Wright17db18e2020-06-26 20:51:44 +010040#include <memory>
Chris Ye3fdbfef2021-01-06 18:45:18 -080041#include <regex>
Michael Wrightdde67b82020-10-27 16:09:22 +000042#include "input/DisplayViewport.h"
43#include "input/Input.h"
Michael Wright17db18e2020-06-26 20:51:44 +010044
Michael Wrightd02c5b62014-02-10 15:10:22 -080045namespace android {
46
Prabir Pradhan2574dfa2019-10-16 16:35:07 -070047using std::chrono_literals::operator""ms;
Chris Ye1b0c7342020-07-28 21:57:03 -070048using namespace android::flag_operators;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -070049
50// Timeout for waiting for an expected event
51static constexpr std::chrono::duration WAIT_TIMEOUT = 100ms;
52
Michael Wrightd02c5b62014-02-10 15:10:22 -080053// An arbitrary time value.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +000054static constexpr nsecs_t ARBITRARY_TIME = 1234;
55static constexpr nsecs_t READ_TIME = 4321;
Michael Wrightd02c5b62014-02-10 15:10:22 -080056
57// Arbitrary display properties.
arthurhungcc7f9802020-04-30 17:55:40 +080058static constexpr int32_t DISPLAY_ID = 0;
59static constexpr int32_t SECONDARY_DISPLAY_ID = DISPLAY_ID + 1;
60static constexpr int32_t DISPLAY_WIDTH = 480;
61static constexpr int32_t DISPLAY_HEIGHT = 800;
62static constexpr int32_t VIRTUAL_DISPLAY_ID = 1;
63static constexpr int32_t VIRTUAL_DISPLAY_WIDTH = 400;
64static constexpr int32_t VIRTUAL_DISPLAY_HEIGHT = 500;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -070065static const char* VIRTUAL_DISPLAY_UNIQUE_ID = "virtual:1";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -070066static constexpr std::optional<uint8_t> NO_PORT = std::nullopt; // no physical port is specified
Michael Wrightd02c5b62014-02-10 15:10:22 -080067
arthurhungcc7f9802020-04-30 17:55:40 +080068static constexpr int32_t FIRST_SLOT = 0;
69static constexpr int32_t SECOND_SLOT = 1;
70static constexpr int32_t THIRD_SLOT = 2;
71static constexpr int32_t INVALID_TRACKING_ID = -1;
72static constexpr int32_t FIRST_TRACKING_ID = 0;
73static constexpr int32_t SECOND_TRACKING_ID = 1;
74static constexpr int32_t THIRD_TRACKING_ID = 2;
Chris Yee2b1e5c2021-03-10 22:45:12 -080075static constexpr int32_t DEFAULT_BATTERY = 1;
Kim Low03ea0352020-11-06 12:45:07 -080076static constexpr int32_t BATTERY_STATUS = 4;
77static constexpr int32_t BATTERY_CAPACITY = 66;
Chris Ye3fdbfef2021-01-06 18:45:18 -080078static constexpr int32_t LIGHT_BRIGHTNESS = 0x55000000;
79static constexpr int32_t LIGHT_COLOR = 0x7F448866;
80static constexpr int32_t LIGHT_PLAYER_ID = 2;
arthurhungcc7f9802020-04-30 17:55:40 +080081
Michael Wrightd02c5b62014-02-10 15:10:22 -080082// Error tolerance for floating point assertions.
83static const float EPSILON = 0.001f;
84
85template<typename T>
86static inline T min(T a, T b) {
87 return a < b ? a : b;
88}
89
90static inline float avg(float x, float y) {
91 return (x + y) / 2;
92}
93
Chris Ye3fdbfef2021-01-06 18:45:18 -080094// Mapping for light color name and the light color
95const std::unordered_map<std::string, LightColor> LIGHT_COLORS = {{"red", LightColor::RED},
96 {"green", LightColor::GREEN},
97 {"blue", LightColor::BLUE}};
Michael Wrightd02c5b62014-02-10 15:10:22 -080098
Prabir Pradhanc14266f2021-05-12 15:56:24 -070099static int32_t getInverseRotation(int32_t orientation) {
100 switch (orientation) {
101 case DISPLAY_ORIENTATION_90:
102 return DISPLAY_ORIENTATION_270;
103 case DISPLAY_ORIENTATION_270:
104 return DISPLAY_ORIENTATION_90;
105 default:
106 return orientation;
107 }
108}
109
Michael Wrightd02c5b62014-02-10 15:10:22 -0800110// --- FakePointerController ---
111
112class FakePointerController : public PointerControllerInterface {
113 bool mHaveBounds;
114 float mMinX, mMinY, mMaxX, mMaxY;
115 float mX, mY;
116 int32_t mButtonState;
Arthur Hungc7ad2d02018-12-18 17:41:29 +0800117 int32_t mDisplayId;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800118
Michael Wrightd02c5b62014-02-10 15:10:22 -0800119public:
120 FakePointerController() :
121 mHaveBounds(false), mMinX(0), mMinY(0), mMaxX(0), mMaxY(0), mX(0), mY(0),
Arthur Hungc7ad2d02018-12-18 17:41:29 +0800122 mButtonState(0), mDisplayId(ADISPLAY_ID_DEFAULT) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800123 }
124
Michael Wright17db18e2020-06-26 20:51:44 +0100125 virtual ~FakePointerController() {}
126
Michael Wrightd02c5b62014-02-10 15:10:22 -0800127 void setBounds(float minX, float minY, float maxX, float maxY) {
128 mHaveBounds = true;
129 mMinX = minX;
130 mMinY = minY;
131 mMaxX = maxX;
132 mMaxY = maxY;
133 }
134
Chris Yea52ade12020-08-27 16:49:20 -0700135 void setPosition(float x, float y) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800136 mX = x;
137 mY = y;
138 }
139
Chris Yea52ade12020-08-27 16:49:20 -0700140 void setButtonState(int32_t buttonState) override { mButtonState = buttonState; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800141
Chris Yea52ade12020-08-27 16:49:20 -0700142 int32_t getButtonState() const override { return mButtonState; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800143
Chris Yea52ade12020-08-27 16:49:20 -0700144 void getPosition(float* outX, float* outY) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800145 *outX = mX;
146 *outY = mY;
147 }
148
Chris Yea52ade12020-08-27 16:49:20 -0700149 int32_t getDisplayId() const override { return mDisplayId; }
Arthur Hungc7ad2d02018-12-18 17:41:29 +0800150
Chris Yea52ade12020-08-27 16:49:20 -0700151 void setDisplayViewport(const DisplayViewport& viewport) override {
Garfield Tan888a6a42020-01-09 11:39:16 -0800152 mDisplayId = viewport.displayId;
153 }
154
Arthur Hung7c645402019-01-25 17:45:42 +0800155 const std::map<int32_t, std::vector<int32_t>>& getSpots() {
156 return mSpotsByDisplay;
157 }
158
Michael Wrightd02c5b62014-02-10 15:10:22 -0800159private:
Chris Yea52ade12020-08-27 16:49:20 -0700160 bool getBounds(float* outMinX, float* outMinY, float* outMaxX, float* outMaxY) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800161 *outMinX = mMinX;
162 *outMinY = mMinY;
163 *outMaxX = mMaxX;
164 *outMaxY = mMaxY;
165 return mHaveBounds;
166 }
167
Chris Yea52ade12020-08-27 16:49:20 -0700168 void move(float deltaX, float deltaY) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800169 mX += deltaX;
170 if (mX < mMinX) mX = mMinX;
171 if (mX > mMaxX) mX = mMaxX;
172 mY += deltaY;
173 if (mY < mMinY) mY = mMinY;
174 if (mY > mMaxY) mY = mMaxY;
175 }
176
Chris Yea52ade12020-08-27 16:49:20 -0700177 void fade(Transition) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800178
Chris Yea52ade12020-08-27 16:49:20 -0700179 void unfade(Transition) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800180
Chris Yea52ade12020-08-27 16:49:20 -0700181 void setPresentation(Presentation) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800182
Chris Yea52ade12020-08-27 16:49:20 -0700183 void setSpots(const PointerCoords*, const uint32_t*, BitSet32 spotIdBits,
184 int32_t displayId) override {
Arthur Hung7c645402019-01-25 17:45:42 +0800185 std::vector<int32_t> newSpots;
186 // Add spots for fingers that are down.
187 for (BitSet32 idBits(spotIdBits); !idBits.isEmpty(); ) {
188 uint32_t id = idBits.clearFirstMarkedBit();
189 newSpots.push_back(id);
190 }
191
192 mSpotsByDisplay[displayId] = newSpots;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800193 }
194
Chris Yea52ade12020-08-27 16:49:20 -0700195 void clearSpots() override {}
Arthur Hung7c645402019-01-25 17:45:42 +0800196
197 std::map<int32_t, std::vector<int32_t>> mSpotsByDisplay;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800198};
199
200
201// --- FakeInputReaderPolicy ---
202
203class FakeInputReaderPolicy : public InputReaderPolicyInterface {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700204 std::mutex mLock;
205 std::condition_variable mDevicesChangedCondition;
206
Michael Wrightd02c5b62014-02-10 15:10:22 -0800207 InputReaderConfiguration mConfig;
Prabir Pradhan2853b7a2021-08-23 14:08:51 +0000208 std::shared_ptr<FakePointerController> mPointerController;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700209 std::vector<InputDeviceInfo> mInputDevices GUARDED_BY(mLock);
210 bool mInputDevicesChanged GUARDED_BY(mLock){false};
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100211 std::vector<DisplayViewport> mViewports;
Jason Gerecke489fda82012-09-07 17:19:40 -0700212 TouchAffineTransformation transform;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800213
214protected:
Chris Yea52ade12020-08-27 16:49:20 -0700215 virtual ~FakeInputReaderPolicy() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800216
217public:
218 FakeInputReaderPolicy() {
219 }
220
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700221 void assertInputDevicesChanged() {
Prabir Pradhan1aed8582019-12-30 11:46:51 -0800222 waitForInputDevices([](bool devicesChanged) {
223 if (!devicesChanged) {
224 FAIL() << "Timed out waiting for notifyInputDevicesChanged() to be called.";
225 }
226 });
227 }
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700228
Prabir Pradhan1aed8582019-12-30 11:46:51 -0800229 void assertInputDevicesNotChanged() {
230 waitForInputDevices([](bool devicesChanged) {
231 if (devicesChanged) {
232 FAIL() << "Expected notifyInputDevicesChanged() to not be called.";
233 }
234 });
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700235 }
236
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700237 virtual void clearViewports() {
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100238 mViewports.clear();
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100239 mConfig.setDisplayViewports(mViewports);
Santos Cordonfa5cf462017-04-05 10:37:00 -0700240 }
241
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700242 std::optional<DisplayViewport> getDisplayViewportByUniqueId(const std::string& uniqueId) const {
243 return mConfig.getDisplayViewportByUniqueId(uniqueId);
244 }
245 std::optional<DisplayViewport> getDisplayViewportByType(ViewportType type) const {
246 return mConfig.getDisplayViewportByType(type);
247 }
248
249 std::optional<DisplayViewport> getDisplayViewportByPort(uint8_t displayPort) const {
250 return mConfig.getDisplayViewportByPort(displayPort);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700251 }
252
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
Prabir Pradhan2853b7a2021-08-23 14:08:51 +0000294 void setPointerController(std::shared_ptr<FakePointerController> controller) {
295 mPointerController = 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 Pradhan5cc1a692021-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 Pradhan5cc1a692021-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
Prabir Pradhan2853b7a2021-08-23 14:08:51 +0000363 std::shared_ptr<PointerControllerInterface> obtainPointerController(
364 int32_t /*deviceId*/) override {
365 return mPointerController;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800366 }
367
Chris Yea52ade12020-08-27 16:49:20 -0700368 void notifyInputDevicesChanged(const std::vector<InputDeviceInfo>& inputDevices) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700369 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800370 mInputDevices = inputDevices;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700371 mInputDevicesChanged = true;
372 mDevicesChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800373 }
374
Chris Yea52ade12020-08-27 16:49:20 -0700375 std::shared_ptr<KeyCharacterMap> getKeyboardLayoutOverlay(
376 const InputDeviceIdentifier&) override {
Yi Kong9b14ac62018-07-17 13:48:38 -0700377 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800378 }
379
Chris Yea52ade12020-08-27 16:49:20 -0700380 std::string getDeviceAlias(const InputDeviceIdentifier&) override { return ""; }
Prabir Pradhan1aed8582019-12-30 11:46:51 -0800381
382 void waitForInputDevices(std::function<void(bool)> processDevicesChanged) {
383 std::unique_lock<std::mutex> lock(mLock);
384 base::ScopedLockAssertion assumeLocked(mLock);
385
386 const bool devicesChanged =
387 mDevicesChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
388 return mInputDevicesChanged;
389 });
390 ASSERT_NO_FATAL_FAILURE(processDevicesChanged(devicesChanged));
391 mInputDevicesChanged = false;
392 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800393};
394
Michael Wrightd02c5b62014-02-10 15:10:22 -0800395// --- FakeEventHub ---
396
397class FakeEventHub : public EventHubInterface {
398 struct KeyInfo {
399 int32_t keyCode;
400 uint32_t flags;
401 };
402
Chris Yef59a2f42020-10-16 12:55:26 -0700403 struct SensorInfo {
404 InputDeviceSensorType sensorType;
405 int32_t sensorDataIndex;
406 };
407
Michael Wrightd02c5b62014-02-10 15:10:22 -0800408 struct Device {
409 InputDeviceIdentifier identifier;
Chris Ye1b0c7342020-07-28 21:57:03 -0700410 Flags<InputDeviceClass> classes;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800411 PropertyMap configuration;
412 KeyedVector<int, RawAbsoluteAxisInfo> absoluteAxes;
413 KeyedVector<int, bool> relativeAxes;
414 KeyedVector<int32_t, int32_t> keyCodeStates;
415 KeyedVector<int32_t, int32_t> scanCodeStates;
416 KeyedVector<int32_t, int32_t> switchStates;
417 KeyedVector<int32_t, int32_t> absoluteAxisValue;
418 KeyedVector<int32_t, KeyInfo> keysByScanCode;
419 KeyedVector<int32_t, KeyInfo> keysByUsageCode;
420 KeyedVector<int32_t, bool> leds;
Chris Yef59a2f42020-10-16 12:55:26 -0700421 std::unordered_map<int32_t, SensorInfo> sensorsByAbsCode;
422 BitArray<MSC_MAX> mscBitmask;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800423 std::vector<VirtualKeyDefinition> virtualKeys;
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700424 bool enabled;
425
426 status_t enable() {
427 enabled = true;
428 return OK;
429 }
430
431 status_t disable() {
432 enabled = false;
433 return OK;
434 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800435
Chris Ye1b0c7342020-07-28 21:57:03 -0700436 explicit Device(Flags<InputDeviceClass> classes) : classes(classes), enabled(true) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800437 };
438
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700439 std::mutex mLock;
440 std::condition_variable mEventsCondition;
441
Michael Wrightd02c5b62014-02-10 15:10:22 -0800442 KeyedVector<int32_t, Device*> mDevices;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100443 std::vector<std::string> mExcludedDevices;
Siarhei Vishniakou370039c2021-02-04 22:09:01 +0000444 std::vector<RawEvent> mEvents GUARDED_BY(mLock);
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600445 std::unordered_map<int32_t /*deviceId*/, std::vector<TouchVideoFrame>> mVideoFrames;
Chris Ye87143712020-11-10 05:05:58 +0000446 std::vector<int32_t> mVibrators = {0, 1};
Chris Ye3fdbfef2021-01-06 18:45:18 -0800447 std::unordered_map<int32_t, RawLightInfo> mRawLightInfos;
448 // Simulates a device light brightness, from light id to light brightness.
449 std::unordered_map<int32_t /* lightId */, int32_t /* brightness*/> mLightBrightness;
450 // Simulates a device light intensities, from light id to light intensities map.
451 std::unordered_map<int32_t /* lightId */, std::unordered_map<LightColor, int32_t>>
452 mLightIntensities;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800453
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700454public:
Michael Wrightd02c5b62014-02-10 15:10:22 -0800455 virtual ~FakeEventHub() {
456 for (size_t i = 0; i < mDevices.size(); i++) {
457 delete mDevices.valueAt(i);
458 }
459 }
460
Michael Wrightd02c5b62014-02-10 15:10:22 -0800461 FakeEventHub() { }
462
Chris Ye1b0c7342020-07-28 21:57:03 -0700463 void addDevice(int32_t deviceId, const std::string& name, Flags<InputDeviceClass> classes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800464 Device* device = new Device(classes);
465 device->identifier.name = name;
466 mDevices.add(deviceId, device);
467
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000468 enqueueEvent(ARBITRARY_TIME, READ_TIME, deviceId, EventHubInterface::DEVICE_ADDED, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800469 }
470
471 void removeDevice(int32_t deviceId) {
472 delete mDevices.valueFor(deviceId);
473 mDevices.removeItem(deviceId);
474
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000475 enqueueEvent(ARBITRARY_TIME, READ_TIME, deviceId, EventHubInterface::DEVICE_REMOVED, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800476 }
477
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700478 bool isDeviceEnabled(int32_t deviceId) {
479 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700480 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700481 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
482 return false;
483 }
484 return device->enabled;
485 }
486
487 status_t enableDevice(int32_t deviceId) {
488 status_t result;
489 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700490 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700491 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
492 return BAD_VALUE;
493 }
494 if (device->enabled) {
495 ALOGW("Duplicate call to %s, device %" PRId32 " already enabled", __func__, deviceId);
496 return OK;
497 }
498 result = device->enable();
499 return result;
500 }
501
502 status_t disableDevice(int32_t deviceId) {
503 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700504 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700505 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
506 return BAD_VALUE;
507 }
508 if (!device->enabled) {
509 ALOGW("Duplicate call to %s, device %" PRId32 " already disabled", __func__, deviceId);
510 return OK;
511 }
512 return device->disable();
513 }
514
Michael Wrightd02c5b62014-02-10 15:10:22 -0800515 void finishDeviceScan() {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000516 enqueueEvent(ARBITRARY_TIME, READ_TIME, 0, EventHubInterface::FINISHED_DEVICE_SCAN, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800517 }
518
519 void addConfigurationProperty(int32_t deviceId, const String8& key, const String8& value) {
520 Device* device = getDevice(deviceId);
521 device->configuration.addProperty(key, value);
522 }
523
524 void addConfigurationMap(int32_t deviceId, const PropertyMap* configuration) {
525 Device* device = getDevice(deviceId);
526 device->configuration.addAll(configuration);
527 }
528
529 void addAbsoluteAxis(int32_t deviceId, int axis,
530 int32_t minValue, int32_t maxValue, int flat, int fuzz, int resolution = 0) {
531 Device* device = getDevice(deviceId);
532
533 RawAbsoluteAxisInfo info;
534 info.valid = true;
535 info.minValue = minValue;
536 info.maxValue = maxValue;
537 info.flat = flat;
538 info.fuzz = fuzz;
539 info.resolution = resolution;
540 device->absoluteAxes.add(axis, info);
541 }
542
543 void addRelativeAxis(int32_t deviceId, int32_t axis) {
544 Device* device = getDevice(deviceId);
545 device->relativeAxes.add(axis, true);
546 }
547
548 void setKeyCodeState(int32_t deviceId, int32_t keyCode, int32_t state) {
549 Device* device = getDevice(deviceId);
550 device->keyCodeStates.replaceValueFor(keyCode, state);
551 }
552
553 void setScanCodeState(int32_t deviceId, int32_t scanCode, int32_t state) {
554 Device* device = getDevice(deviceId);
555 device->scanCodeStates.replaceValueFor(scanCode, state);
556 }
557
558 void setSwitchState(int32_t deviceId, int32_t switchCode, int32_t state) {
559 Device* device = getDevice(deviceId);
560 device->switchStates.replaceValueFor(switchCode, state);
561 }
562
563 void setAbsoluteAxisValue(int32_t deviceId, int32_t axis, int32_t value) {
564 Device* device = getDevice(deviceId);
565 device->absoluteAxisValue.replaceValueFor(axis, value);
566 }
567
568 void addKey(int32_t deviceId, int32_t scanCode, int32_t usageCode,
569 int32_t keyCode, uint32_t flags) {
570 Device* device = getDevice(deviceId);
571 KeyInfo info;
572 info.keyCode = keyCode;
573 info.flags = flags;
574 if (scanCode) {
575 device->keysByScanCode.add(scanCode, info);
576 }
577 if (usageCode) {
578 device->keysByUsageCode.add(usageCode, info);
579 }
580 }
581
582 void addLed(int32_t deviceId, int32_t led, bool initialState) {
583 Device* device = getDevice(deviceId);
584 device->leds.add(led, initialState);
585 }
586
Chris Yef59a2f42020-10-16 12:55:26 -0700587 void addSensorAxis(int32_t deviceId, int32_t absCode, InputDeviceSensorType sensorType,
588 int32_t sensorDataIndex) {
589 Device* device = getDevice(deviceId);
590 SensorInfo info;
591 info.sensorType = sensorType;
592 info.sensorDataIndex = sensorDataIndex;
593 device->sensorsByAbsCode.emplace(absCode, info);
594 }
595
596 void setMscEvent(int32_t deviceId, int32_t mscEvent) {
597 Device* device = getDevice(deviceId);
598 typename BitArray<MSC_MAX>::Buffer buffer;
599 buffer[mscEvent / 32] = 1 << mscEvent % 32;
600 device->mscBitmask.loadFromBuffer(buffer);
601 }
602
Chris Ye3fdbfef2021-01-06 18:45:18 -0800603 void addRawLightInfo(int32_t rawId, RawLightInfo&& info) {
604 mRawLightInfos.emplace(rawId, std::move(info));
605 }
606
607 void fakeLightBrightness(int32_t rawId, int32_t brightness) {
608 mLightBrightness.emplace(rawId, brightness);
609 }
610
611 void fakeLightIntensities(int32_t rawId,
612 const std::unordered_map<LightColor, int32_t> intensities) {
613 mLightIntensities.emplace(rawId, std::move(intensities));
614 }
615
Michael Wrightd02c5b62014-02-10 15:10:22 -0800616 bool getLedState(int32_t deviceId, int32_t led) {
617 Device* device = getDevice(deviceId);
618 return device->leds.valueFor(led);
619 }
620
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100621 std::vector<std::string>& getExcludedDevices() {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800622 return mExcludedDevices;
623 }
624
625 void addVirtualKeyDefinition(int32_t deviceId, const VirtualKeyDefinition& definition) {
626 Device* device = getDevice(deviceId);
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800627 device->virtualKeys.push_back(definition);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800628 }
629
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000630 void enqueueEvent(nsecs_t when, nsecs_t readTime, int32_t deviceId, int32_t type, int32_t code,
631 int32_t value) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700632 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800633 RawEvent event;
634 event.when = when;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000635 event.readTime = readTime;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800636 event.deviceId = deviceId;
637 event.type = type;
638 event.code = code;
639 event.value = value;
640 mEvents.push_back(event);
641
642 if (type == EV_ABS) {
643 setAbsoluteAxisValue(deviceId, code, value);
644 }
645 }
646
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600647 void setVideoFrames(std::unordered_map<int32_t /*deviceId*/,
648 std::vector<TouchVideoFrame>> videoFrames) {
649 mVideoFrames = std::move(videoFrames);
650 }
651
Michael Wrightd02c5b62014-02-10 15:10:22 -0800652 void assertQueueIsEmpty() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700653 std::unique_lock<std::mutex> lock(mLock);
654 base::ScopedLockAssertion assumeLocked(mLock);
655 const bool queueIsEmpty =
656 mEventsCondition.wait_for(lock, WAIT_TIMEOUT,
657 [this]() REQUIRES(mLock) { return mEvents.size() == 0; });
658 if (!queueIsEmpty) {
659 FAIL() << "Timed out waiting for EventHub queue to be emptied.";
660 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800661 }
662
663private:
664 Device* getDevice(int32_t deviceId) const {
665 ssize_t index = mDevices.indexOfKey(deviceId);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100666 return index >= 0 ? mDevices.valueAt(index) : nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800667 }
668
Chris Yea52ade12020-08-27 16:49:20 -0700669 Flags<InputDeviceClass> getDeviceClasses(int32_t deviceId) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800670 Device* device = getDevice(deviceId);
Chris Ye1b0c7342020-07-28 21:57:03 -0700671 return device ? device->classes : Flags<InputDeviceClass>(0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800672 }
673
Chris Yea52ade12020-08-27 16:49:20 -0700674 InputDeviceIdentifier getDeviceIdentifier(int32_t deviceId) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800675 Device* device = getDevice(deviceId);
676 return device ? device->identifier : InputDeviceIdentifier();
677 }
678
Chris Yea52ade12020-08-27 16:49:20 -0700679 int32_t getDeviceControllerNumber(int32_t) const override { return 0; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800680
Chris Yea52ade12020-08-27 16:49:20 -0700681 void getConfiguration(int32_t deviceId, PropertyMap* outConfiguration) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800682 Device* device = getDevice(deviceId);
683 if (device) {
684 *outConfiguration = device->configuration;
685 }
686 }
687
Chris Yea52ade12020-08-27 16:49:20 -0700688 status_t getAbsoluteAxisInfo(int32_t deviceId, int axis,
689 RawAbsoluteAxisInfo* outAxisInfo) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800690 Device* device = getDevice(deviceId);
Arthur Hung9da14732019-09-02 16:16:58 +0800691 if (device && device->enabled) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800692 ssize_t index = device->absoluteAxes.indexOfKey(axis);
693 if (index >= 0) {
694 *outAxisInfo = device->absoluteAxes.valueAt(index);
695 return OK;
696 }
697 }
698 outAxisInfo->clear();
699 return -1;
700 }
701
Chris Yea52ade12020-08-27 16:49:20 -0700702 bool hasRelativeAxis(int32_t deviceId, int axis) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800703 Device* device = getDevice(deviceId);
704 if (device) {
705 return device->relativeAxes.indexOfKey(axis) >= 0;
706 }
707 return false;
708 }
709
Chris Yea52ade12020-08-27 16:49:20 -0700710 bool hasInputProperty(int32_t, int) const override { return false; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800711
Chris Yef59a2f42020-10-16 12:55:26 -0700712 bool hasMscEvent(int32_t deviceId, int mscEvent) const override final {
713 Device* device = getDevice(deviceId);
714 if (device) {
715 return mscEvent >= 0 && mscEvent <= MSC_MAX ? device->mscBitmask.test(mscEvent) : false;
716 }
717 return false;
718 }
719
Chris Yea52ade12020-08-27 16:49:20 -0700720 status_t mapKey(int32_t deviceId, int32_t scanCode, int32_t usageCode, int32_t metaState,
721 int32_t* outKeycode, int32_t* outMetaState, uint32_t* outFlags) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800722 Device* device = getDevice(deviceId);
723 if (device) {
724 const KeyInfo* key = getKey(device, scanCode, usageCode);
725 if (key) {
726 if (outKeycode) {
727 *outKeycode = key->keyCode;
728 }
729 if (outFlags) {
730 *outFlags = key->flags;
731 }
Dmitry Torokhov0faaa0b2015-09-24 13:13:55 -0700732 if (outMetaState) {
733 *outMetaState = metaState;
734 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800735 return OK;
736 }
737 }
738 return NAME_NOT_FOUND;
739 }
740
741 const KeyInfo* getKey(Device* device, int32_t scanCode, int32_t usageCode) const {
742 if (usageCode) {
743 ssize_t index = device->keysByUsageCode.indexOfKey(usageCode);
744 if (index >= 0) {
745 return &device->keysByUsageCode.valueAt(index);
746 }
747 }
748 if (scanCode) {
749 ssize_t index = device->keysByScanCode.indexOfKey(scanCode);
750 if (index >= 0) {
751 return &device->keysByScanCode.valueAt(index);
752 }
753 }
Yi Kong9b14ac62018-07-17 13:48:38 -0700754 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800755 }
756
Chris Yea52ade12020-08-27 16:49:20 -0700757 status_t mapAxis(int32_t, int32_t, AxisInfo*) const override { return NAME_NOT_FOUND; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800758
Chris Yef59a2f42020-10-16 12:55:26 -0700759 base::Result<std::pair<InputDeviceSensorType, int32_t>> mapSensor(int32_t deviceId,
760 int32_t absCode) {
761 Device* device = getDevice(deviceId);
762 if (!device) {
763 return Errorf("Sensor device not found.");
764 }
765 auto it = device->sensorsByAbsCode.find(absCode);
766 if (it == device->sensorsByAbsCode.end()) {
767 return Errorf("Sensor map not found.");
768 }
769 const SensorInfo& info = it->second;
770 return std::make_pair(info.sensorType, info.sensorDataIndex);
771 }
772
Chris Yea52ade12020-08-27 16:49:20 -0700773 void setExcludedDevices(const std::vector<std::string>& devices) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800774 mExcludedDevices = devices;
775 }
776
Siarhei Vishniakou370039c2021-02-04 22:09:01 +0000777 size_t getEvents(int, RawEvent* buffer, size_t bufferSize) override {
778 std::scoped_lock lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800779
Siarhei Vishniakou370039c2021-02-04 22:09:01 +0000780 const size_t filledSize = std::min(mEvents.size(), bufferSize);
781 std::copy(mEvents.begin(), mEvents.begin() + filledSize, buffer);
782
783 mEvents.erase(mEvents.begin(), mEvents.begin() + filledSize);
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700784 mEventsCondition.notify_all();
Siarhei Vishniakou370039c2021-02-04 22:09:01 +0000785 return filledSize;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800786 }
787
Chris Yea52ade12020-08-27 16:49:20 -0700788 std::vector<TouchVideoFrame> getVideoFrames(int32_t deviceId) override {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600789 auto it = mVideoFrames.find(deviceId);
790 if (it != mVideoFrames.end()) {
791 std::vector<TouchVideoFrame> frames = std::move(it->second);
792 mVideoFrames.erase(deviceId);
793 return frames;
794 }
Siarhei Vishniakouadd89292018-12-13 19:23:36 -0800795 return {};
796 }
797
Chris Yea52ade12020-08-27 16:49:20 -0700798 int32_t getScanCodeState(int32_t deviceId, int32_t scanCode) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800799 Device* device = getDevice(deviceId);
800 if (device) {
801 ssize_t index = device->scanCodeStates.indexOfKey(scanCode);
802 if (index >= 0) {
803 return device->scanCodeStates.valueAt(index);
804 }
805 }
806 return AKEY_STATE_UNKNOWN;
807 }
808
Chris Yea52ade12020-08-27 16:49:20 -0700809 int32_t getKeyCodeState(int32_t deviceId, int32_t keyCode) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800810 Device* device = getDevice(deviceId);
811 if (device) {
812 ssize_t index = device->keyCodeStates.indexOfKey(keyCode);
813 if (index >= 0) {
814 return device->keyCodeStates.valueAt(index);
815 }
816 }
817 return AKEY_STATE_UNKNOWN;
818 }
819
Chris Yea52ade12020-08-27 16:49:20 -0700820 int32_t getSwitchState(int32_t deviceId, int32_t sw) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800821 Device* device = getDevice(deviceId);
822 if (device) {
823 ssize_t index = device->switchStates.indexOfKey(sw);
824 if (index >= 0) {
825 return device->switchStates.valueAt(index);
826 }
827 }
828 return AKEY_STATE_UNKNOWN;
829 }
830
Chris Yea52ade12020-08-27 16:49:20 -0700831 status_t getAbsoluteAxisValue(int32_t deviceId, int32_t axis,
832 int32_t* outValue) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800833 Device* device = getDevice(deviceId);
834 if (device) {
835 ssize_t index = device->absoluteAxisValue.indexOfKey(axis);
836 if (index >= 0) {
837 *outValue = device->absoluteAxisValue.valueAt(index);
838 return OK;
839 }
840 }
841 *outValue = 0;
842 return -1;
843 }
844
Chris Yea52ade12020-08-27 16:49:20 -0700845 // Return true if the device has non-empty key layout.
846 bool markSupportedKeyCodes(int32_t deviceId, size_t numCodes, const int32_t* keyCodes,
847 uint8_t* outFlags) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800848 bool result = false;
849 Device* device = getDevice(deviceId);
850 if (device) {
Chris Yea52ade12020-08-27 16:49:20 -0700851 result = device->keysByScanCode.size() > 0 || device->keysByUsageCode.size() > 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800852 for (size_t i = 0; i < numCodes; i++) {
853 for (size_t j = 0; j < device->keysByScanCode.size(); j++) {
854 if (keyCodes[i] == device->keysByScanCode.valueAt(j).keyCode) {
855 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800856 }
857 }
858 for (size_t j = 0; j < device->keysByUsageCode.size(); j++) {
859 if (keyCodes[i] == device->keysByUsageCode.valueAt(j).keyCode) {
860 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800861 }
862 }
863 }
864 }
865 return result;
866 }
867
Chris Yea52ade12020-08-27 16:49:20 -0700868 bool hasScanCode(int32_t deviceId, int32_t scanCode) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800869 Device* device = getDevice(deviceId);
870 if (device) {
871 ssize_t index = device->keysByScanCode.indexOfKey(scanCode);
872 return index >= 0;
873 }
874 return false;
875 }
876
Arthur Hungcb40a002021-08-03 14:31:01 +0000877 bool hasKeyCode(int32_t deviceId, int32_t keyCode) const override {
878 Device* device = getDevice(deviceId);
879 if (!device) {
880 return false;
881 }
882 for (size_t i = 0; i < device->keysByScanCode.size(); i++) {
883 if (keyCode == device->keysByScanCode.valueAt(i).keyCode) {
884 return true;
885 }
886 }
887 for (size_t j = 0; j < device->keysByUsageCode.size(); j++) {
888 if (keyCode == device->keysByUsageCode.valueAt(j).keyCode) {
889 return true;
890 }
891 }
892 return false;
893 }
894
Chris Yea52ade12020-08-27 16:49:20 -0700895 bool hasLed(int32_t deviceId, int32_t led) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800896 Device* device = getDevice(deviceId);
897 return device && device->leds.indexOfKey(led) >= 0;
898 }
899
Chris Yea52ade12020-08-27 16:49:20 -0700900 void setLedState(int32_t deviceId, int32_t led, bool on) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800901 Device* device = getDevice(deviceId);
902 if (device) {
903 ssize_t index = device->leds.indexOfKey(led);
904 if (index >= 0) {
905 device->leds.replaceValueAt(led, on);
906 } else {
907 ADD_FAILURE()
908 << "Attempted to set the state of an LED that the EventHub declared "
909 "was not present. led=" << led;
910 }
911 }
912 }
913
Chris Yea52ade12020-08-27 16:49:20 -0700914 void getVirtualKeyDefinitions(
915 int32_t deviceId, std::vector<VirtualKeyDefinition>& outVirtualKeys) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800916 outVirtualKeys.clear();
917
918 Device* device = getDevice(deviceId);
919 if (device) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800920 outVirtualKeys = device->virtualKeys;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800921 }
922 }
923
Chris Yea52ade12020-08-27 16:49:20 -0700924 const std::shared_ptr<KeyCharacterMap> getKeyCharacterMap(int32_t) const override {
Yi Kong9b14ac62018-07-17 13:48:38 -0700925 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800926 }
927
Chris Yea52ade12020-08-27 16:49:20 -0700928 bool setKeyboardLayoutOverlay(int32_t, std::shared_ptr<KeyCharacterMap>) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800929 return false;
930 }
931
Chris Yea52ade12020-08-27 16:49:20 -0700932 void vibrate(int32_t, const VibrationElement&) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800933
Chris Yea52ade12020-08-27 16:49:20 -0700934 void cancelVibrate(int32_t) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800935
Chris Ye87143712020-11-10 05:05:58 +0000936 std::vector<int32_t> getVibratorIds(int32_t deviceId) override { return mVibrators; };
937
Chris Yee2b1e5c2021-03-10 22:45:12 -0800938 std::optional<int32_t> getBatteryCapacity(int32_t, int32_t) const override {
939 return BATTERY_CAPACITY;
940 }
Kim Low03ea0352020-11-06 12:45:07 -0800941
Chris Yee2b1e5c2021-03-10 22:45:12 -0800942 std::optional<int32_t> getBatteryStatus(int32_t, int32_t) const override {
943 return BATTERY_STATUS;
944 }
945
946 const std::vector<int32_t> getRawBatteryIds(int32_t deviceId) { return {}; }
947
948 std::optional<RawBatteryInfo> getRawBatteryInfo(int32_t deviceId, int32_t batteryId) {
949 return std::nullopt;
950 }
Kim Low03ea0352020-11-06 12:45:07 -0800951
Chris Ye3fdbfef2021-01-06 18:45:18 -0800952 const std::vector<int32_t> getRawLightIds(int32_t deviceId) override {
953 std::vector<int32_t> ids;
954 for (const auto& [rawId, info] : mRawLightInfos) {
955 ids.push_back(rawId);
956 }
957 return ids;
958 }
959
960 std::optional<RawLightInfo> getRawLightInfo(int32_t deviceId, int32_t lightId) override {
961 auto it = mRawLightInfos.find(lightId);
962 if (it == mRawLightInfos.end()) {
963 return std::nullopt;
964 }
965 return it->second;
966 }
967
968 void setLightBrightness(int32_t deviceId, int32_t lightId, int32_t brightness) override {
969 mLightBrightness.emplace(lightId, brightness);
970 }
971
972 void setLightIntensities(int32_t deviceId, int32_t lightId,
973 std::unordered_map<LightColor, int32_t> intensities) override {
974 mLightIntensities.emplace(lightId, intensities);
975 };
976
977 std::optional<int32_t> getLightBrightness(int32_t deviceId, int32_t lightId) override {
978 auto lightIt = mLightBrightness.find(lightId);
979 if (lightIt == mLightBrightness.end()) {
980 return std::nullopt;
981 }
982 return lightIt->second;
983 }
984
985 std::optional<std::unordered_map<LightColor, int32_t>> getLightIntensities(
986 int32_t deviceId, int32_t lightId) override {
987 auto lightIt = mLightIntensities.find(lightId);
988 if (lightIt == mLightIntensities.end()) {
989 return std::nullopt;
990 }
991 return lightIt->second;
992 };
993
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100994 virtual bool isExternal(int32_t) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800995 return false;
996 }
997
Chris Yea52ade12020-08-27 16:49:20 -0700998 void dump(std::string&) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800999
Chris Yea52ade12020-08-27 16:49:20 -07001000 void monitor() override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001001
Chris Yea52ade12020-08-27 16:49:20 -07001002 void requestReopenDevices() override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001003
Chris Yea52ade12020-08-27 16:49:20 -07001004 void wake() override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001005};
1006
Michael Wrightd02c5b62014-02-10 15:10:22 -08001007// --- FakeInputMapper ---
1008
1009class FakeInputMapper : public InputMapper {
1010 uint32_t mSources;
1011 int32_t mKeyboardType;
1012 int32_t mMetaState;
1013 KeyedVector<int32_t, int32_t> mKeyCodeStates;
1014 KeyedVector<int32_t, int32_t> mScanCodeStates;
1015 KeyedVector<int32_t, int32_t> mSwitchStates;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001016 std::vector<int32_t> mSupportedKeyCodes;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001017
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001018 std::mutex mLock;
1019 std::condition_variable mStateChangedCondition;
1020 bool mConfigureWasCalled GUARDED_BY(mLock);
1021 bool mResetWasCalled GUARDED_BY(mLock);
1022 bool mProcessWasCalled GUARDED_BY(mLock);
1023 RawEvent mLastEvent GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001024
Arthur Hungc23540e2018-11-29 20:42:11 +08001025 std::optional<DisplayViewport> mViewport;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001026public:
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001027 FakeInputMapper(InputDeviceContext& deviceContext, uint32_t sources)
1028 : InputMapper(deviceContext),
1029 mSources(sources),
1030 mKeyboardType(AINPUT_KEYBOARD_TYPE_NONE),
Michael Wrightd02c5b62014-02-10 15:10:22 -08001031 mMetaState(0),
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001032 mConfigureWasCalled(false),
1033 mResetWasCalled(false),
1034 mProcessWasCalled(false) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001035
Chris Yea52ade12020-08-27 16:49:20 -07001036 virtual ~FakeInputMapper() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001037
1038 void setKeyboardType(int32_t keyboardType) {
1039 mKeyboardType = keyboardType;
1040 }
1041
1042 void setMetaState(int32_t metaState) {
1043 mMetaState = metaState;
1044 }
1045
1046 void assertConfigureWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001047 std::unique_lock<std::mutex> lock(mLock);
1048 base::ScopedLockAssertion assumeLocked(mLock);
1049 const bool configureCalled =
1050 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1051 return mConfigureWasCalled;
1052 });
1053 if (!configureCalled) {
1054 FAIL() << "Expected configure() to have been called.";
1055 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001056 mConfigureWasCalled = false;
1057 }
1058
1059 void assertResetWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001060 std::unique_lock<std::mutex> lock(mLock);
1061 base::ScopedLockAssertion assumeLocked(mLock);
1062 const bool resetCalled =
1063 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1064 return mResetWasCalled;
1065 });
1066 if (!resetCalled) {
1067 FAIL() << "Expected reset() to have been called.";
1068 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001069 mResetWasCalled = false;
1070 }
1071
Yi Kong9b14ac62018-07-17 13:48:38 -07001072 void assertProcessWasCalled(RawEvent* outLastEvent = nullptr) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001073 std::unique_lock<std::mutex> lock(mLock);
1074 base::ScopedLockAssertion assumeLocked(mLock);
1075 const bool processCalled =
1076 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1077 return mProcessWasCalled;
1078 });
1079 if (!processCalled) {
1080 FAIL() << "Expected process() to have been called.";
1081 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001082 if (outLastEvent) {
1083 *outLastEvent = mLastEvent;
1084 }
1085 mProcessWasCalled = false;
1086 }
1087
1088 void setKeyCodeState(int32_t keyCode, int32_t state) {
1089 mKeyCodeStates.replaceValueFor(keyCode, state);
1090 }
1091
1092 void setScanCodeState(int32_t scanCode, int32_t state) {
1093 mScanCodeStates.replaceValueFor(scanCode, state);
1094 }
1095
1096 void setSwitchState(int32_t switchCode, int32_t state) {
1097 mSwitchStates.replaceValueFor(switchCode, state);
1098 }
1099
1100 void addSupportedKeyCode(int32_t keyCode) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001101 mSupportedKeyCodes.push_back(keyCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001102 }
1103
1104private:
Chris Yea52ade12020-08-27 16:49:20 -07001105 uint32_t getSources() override { return mSources; }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001106
Chris Yea52ade12020-08-27 16:49:20 -07001107 void populateDeviceInfo(InputDeviceInfo* deviceInfo) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001108 InputMapper::populateDeviceInfo(deviceInfo);
1109
1110 if (mKeyboardType != AINPUT_KEYBOARD_TYPE_NONE) {
1111 deviceInfo->setKeyboardType(mKeyboardType);
1112 }
1113 }
1114
Chris Yea52ade12020-08-27 16:49:20 -07001115 void configure(nsecs_t, const InputReaderConfiguration* config, uint32_t changes) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001116 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001117 mConfigureWasCalled = true;
Arthur Hungc23540e2018-11-29 20:42:11 +08001118
1119 // Find the associated viewport if exist.
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001120 const std::optional<uint8_t> displayPort = getDeviceContext().getAssociatedDisplayPort();
Arthur Hungc23540e2018-11-29 20:42:11 +08001121 if (displayPort && (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
1122 mViewport = config->getDisplayViewportByPort(*displayPort);
1123 }
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001124
1125 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001126 }
1127
Chris Yea52ade12020-08-27 16:49:20 -07001128 void reset(nsecs_t) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001129 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001130 mResetWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001131 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001132 }
1133
Chris Yea52ade12020-08-27 16:49:20 -07001134 void process(const RawEvent* rawEvent) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001135 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001136 mLastEvent = *rawEvent;
1137 mProcessWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001138 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001139 }
1140
Chris Yea52ade12020-08-27 16:49:20 -07001141 int32_t getKeyCodeState(uint32_t, int32_t keyCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001142 ssize_t index = mKeyCodeStates.indexOfKey(keyCode);
1143 return index >= 0 ? mKeyCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1144 }
1145
Chris Yea52ade12020-08-27 16:49:20 -07001146 int32_t getScanCodeState(uint32_t, int32_t scanCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001147 ssize_t index = mScanCodeStates.indexOfKey(scanCode);
1148 return index >= 0 ? mScanCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1149 }
1150
Chris Yea52ade12020-08-27 16:49:20 -07001151 int32_t getSwitchState(uint32_t, int32_t switchCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001152 ssize_t index = mSwitchStates.indexOfKey(switchCode);
1153 return index >= 0 ? mSwitchStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1154 }
1155
Chris Yea52ade12020-08-27 16:49:20 -07001156 // Return true if the device has non-empty key layout.
1157 bool markSupportedKeyCodes(uint32_t, size_t numCodes, const int32_t* keyCodes,
1158 uint8_t* outFlags) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001159 for (size_t i = 0; i < numCodes; i++) {
1160 for (size_t j = 0; j < mSupportedKeyCodes.size(); j++) {
1161 if (keyCodes[i] == mSupportedKeyCodes[j]) {
1162 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001163 }
1164 }
1165 }
Chris Yea52ade12020-08-27 16:49:20 -07001166 bool result = mSupportedKeyCodes.size() > 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001167 return result;
1168 }
1169
1170 virtual int32_t getMetaState() {
1171 return mMetaState;
1172 }
1173
1174 virtual void fadePointer() {
1175 }
Arthur Hungc23540e2018-11-29 20:42:11 +08001176
1177 virtual std::optional<int32_t> getAssociatedDisplay() {
1178 if (mViewport) {
1179 return std::make_optional(mViewport->displayId);
1180 }
1181 return std::nullopt;
1182 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001183};
1184
1185
1186// --- InstrumentedInputReader ---
1187
1188class InstrumentedInputReader : public InputReader {
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001189 std::queue<std::shared_ptr<InputDevice>> mNextDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001190
1191public:
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001192 InstrumentedInputReader(std::shared_ptr<EventHubInterface> eventHub,
1193 const sp<InputReaderPolicyInterface>& policy,
1194 const sp<InputListenerInterface>& listener)
arthurhungdcef2dc2020-08-11 14:47:50 +08001195 : InputReader(eventHub, policy, listener), mFakeContext(this) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001196
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001197 virtual ~InstrumentedInputReader() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001198
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001199 void pushNextDevice(std::shared_ptr<InputDevice> device) { mNextDevices.push(device); }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001200
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001201 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001202 const std::string& location = "") {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001203 InputDeviceIdentifier identifier;
1204 identifier.name = name;
Arthur Hungc23540e2018-11-29 20:42:11 +08001205 identifier.location = location;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001206 int32_t generation = deviceId + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08001207 return std::make_shared<InputDevice>(&mFakeContext, deviceId, generation, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001208 }
1209
Prabir Pradhan28efc192019-11-05 01:10:04 +00001210 // Make the protected loopOnce method accessible to tests.
1211 using InputReader::loopOnce;
1212
Michael Wrightd02c5b62014-02-10 15:10:22 -08001213protected:
Chris Ye1c2e0892020-11-30 21:41:44 -08001214 virtual std::shared_ptr<InputDevice> createDeviceLocked(int32_t eventHubId,
1215 const InputDeviceIdentifier& identifier)
1216 REQUIRES(mLock) {
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001217 if (!mNextDevices.empty()) {
1218 std::shared_ptr<InputDevice> device(std::move(mNextDevices.front()));
1219 mNextDevices.pop();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001220 return device;
1221 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001222 return InputReader::createDeviceLocked(eventHubId, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001223 }
1224
arthurhungdcef2dc2020-08-11 14:47:50 +08001225 // --- FakeInputReaderContext ---
1226 class FakeInputReaderContext : public ContextImpl {
1227 int32_t mGlobalMetaState;
1228 bool mUpdateGlobalMetaStateWasCalled;
1229 int32_t mGeneration;
1230
1231 public:
1232 FakeInputReaderContext(InputReader* reader)
1233 : ContextImpl(reader),
1234 mGlobalMetaState(0),
1235 mUpdateGlobalMetaStateWasCalled(false),
1236 mGeneration(1) {}
1237
1238 virtual ~FakeInputReaderContext() {}
1239
1240 void assertUpdateGlobalMetaStateWasCalled() {
1241 ASSERT_TRUE(mUpdateGlobalMetaStateWasCalled)
1242 << "Expected updateGlobalMetaState() to have been called.";
1243 mUpdateGlobalMetaStateWasCalled = false;
1244 }
1245
1246 void setGlobalMetaState(int32_t state) { mGlobalMetaState = state; }
1247
1248 uint32_t getGeneration() { return mGeneration; }
1249
1250 void updateGlobalMetaState() override {
1251 mUpdateGlobalMetaStateWasCalled = true;
1252 ContextImpl::updateGlobalMetaState();
1253 }
1254
1255 int32_t getGlobalMetaState() override {
1256 return mGlobalMetaState | ContextImpl::getGlobalMetaState();
1257 }
1258
1259 int32_t bumpGeneration() override {
1260 mGeneration = ContextImpl::bumpGeneration();
1261 return mGeneration;
1262 }
1263 } mFakeContext;
1264
Michael Wrightd02c5b62014-02-10 15:10:22 -08001265 friend class InputReaderTest;
arthurhungdcef2dc2020-08-11 14:47:50 +08001266
1267public:
1268 FakeInputReaderContext* getContext() { return &mFakeContext; }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001269};
1270
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001271// --- InputReaderPolicyTest ---
1272class InputReaderPolicyTest : public testing::Test {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001273protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001274 sp<FakeInputReaderPolicy> mFakePolicy;
1275
Chris Yea52ade12020-08-27 16:49:20 -07001276 void SetUp() override { mFakePolicy = new FakeInputReaderPolicy(); }
1277 void TearDown() override { mFakePolicy.clear(); }
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001278};
1279
1280/**
1281 * Check that empty set of viewports is an acceptable configuration.
1282 * Also try to get internal viewport two different ways - by type and by uniqueId.
1283 *
1284 * There will be confusion if two viewports with empty uniqueId and identical type are present.
1285 * Such configuration is not currently allowed.
1286 */
1287TEST_F(InputReaderPolicyTest, Viewports_GetCleared) {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001288 static const std::string uniqueId = "local:0";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001289
1290 // We didn't add any viewports yet, so there shouldn't be any.
1291 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001292 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001293 ASSERT_FALSE(internalViewport);
1294
1295 // Add an internal viewport, then clear it
1296 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001297 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId, NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001298 ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001299
1300 // Check matching by uniqueId
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001301 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001302 ASSERT_TRUE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001303 ASSERT_EQ(ViewportType::INTERNAL, internalViewport->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001304
1305 // Check matching by viewport type
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001306 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001307 ASSERT_TRUE(internalViewport);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001308 ASSERT_EQ(uniqueId, internalViewport->uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001309
1310 mFakePolicy->clearViewports();
1311 // Make sure nothing is found after clear
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001312 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001313 ASSERT_FALSE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001314 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001315 ASSERT_FALSE(internalViewport);
1316}
1317
1318TEST_F(InputReaderPolicyTest, Viewports_GetByType) {
1319 const std::string internalUniqueId = "local:0";
1320 const std::string externalUniqueId = "local:1";
1321 const std::string virtualUniqueId1 = "virtual:2";
1322 const std::string virtualUniqueId2 = "virtual:3";
1323 constexpr int32_t virtualDisplayId1 = 2;
1324 constexpr int32_t virtualDisplayId2 = 3;
1325
1326 // Add an internal viewport
1327 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001328 DISPLAY_ORIENTATION_0, true /*isActive*/, internalUniqueId,
1329 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001330 // Add an external viewport
1331 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001332 DISPLAY_ORIENTATION_0, true /*isActive*/, externalUniqueId,
1333 NO_PORT, ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001334 // Add an virtual viewport
1335 mFakePolicy->addDisplayViewport(virtualDisplayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001336 DISPLAY_ORIENTATION_0, true /*isActive*/, virtualUniqueId1,
1337 NO_PORT, ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001338 // Add another virtual viewport
1339 mFakePolicy->addDisplayViewport(virtualDisplayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001340 DISPLAY_ORIENTATION_0, true /*isActive*/, virtualUniqueId2,
1341 NO_PORT, ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001342
1343 // Check matching by type for internal
1344 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001345 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001346 ASSERT_TRUE(internalViewport);
1347 ASSERT_EQ(internalUniqueId, internalViewport->uniqueId);
1348
1349 // Check matching by type for external
1350 std::optional<DisplayViewport> externalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001351 mFakePolicy->getDisplayViewportByType(ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001352 ASSERT_TRUE(externalViewport);
1353 ASSERT_EQ(externalUniqueId, externalViewport->uniqueId);
1354
1355 // Check matching by uniqueId for virtual viewport #1
1356 std::optional<DisplayViewport> virtualViewport1 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001357 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001358 ASSERT_TRUE(virtualViewport1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001359 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport1->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001360 ASSERT_EQ(virtualUniqueId1, virtualViewport1->uniqueId);
1361 ASSERT_EQ(virtualDisplayId1, virtualViewport1->displayId);
1362
1363 // Check matching by uniqueId for virtual viewport #2
1364 std::optional<DisplayViewport> virtualViewport2 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001365 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001366 ASSERT_TRUE(virtualViewport2);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001367 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport2->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001368 ASSERT_EQ(virtualUniqueId2, virtualViewport2->uniqueId);
1369 ASSERT_EQ(virtualDisplayId2, virtualViewport2->displayId);
1370}
1371
1372
1373/**
1374 * We can have 2 viewports of the same kind. We can distinguish them by uniqueId, and confirm
1375 * that lookup works by checking display id.
1376 * Check that 2 viewports of each kind is possible, for all existing viewport types.
1377 */
1378TEST_F(InputReaderPolicyTest, Viewports_TwoOfSameType) {
1379 const std::string uniqueId1 = "uniqueId1";
1380 const std::string uniqueId2 = "uniqueId2";
1381 constexpr int32_t displayId1 = 2;
1382 constexpr int32_t displayId2 = 3;
1383
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001384 std::vector<ViewportType> types = {ViewportType::INTERNAL, ViewportType::EXTERNAL,
1385 ViewportType::VIRTUAL};
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001386 for (const ViewportType& type : types) {
1387 mFakePolicy->clearViewports();
1388 // Add a viewport
1389 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001390 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1,
1391 NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001392 // Add another viewport
1393 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001394 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2,
1395 NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001396
1397 // Check that correct display viewport was returned by comparing the display IDs.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001398 std::optional<DisplayViewport> viewport1 =
1399 mFakePolicy->getDisplayViewportByUniqueId(uniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001400 ASSERT_TRUE(viewport1);
1401 ASSERT_EQ(displayId1, viewport1->displayId);
1402 ASSERT_EQ(type, viewport1->type);
1403
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001404 std::optional<DisplayViewport> viewport2 =
1405 mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001406 ASSERT_TRUE(viewport2);
1407 ASSERT_EQ(displayId2, viewport2->displayId);
1408 ASSERT_EQ(type, viewport2->type);
1409
1410 // When there are multiple viewports of the same kind, and uniqueId is not specified
1411 // in the call to getDisplayViewport, then that situation is not supported.
1412 // The viewports can be stored in any order, so we cannot rely on the order, since that
1413 // is just implementation detail.
1414 // However, we can check that it still returns *a* viewport, we just cannot assert
1415 // which one specifically is returned.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001416 std::optional<DisplayViewport> someViewport = mFakePolicy->getDisplayViewportByType(type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001417 ASSERT_TRUE(someViewport);
1418 }
1419}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001420
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001421/**
Michael Wrightdde67b82020-10-27 16:09:22 +00001422 * When we have multiple internal displays make sure we always return the default display when
1423 * querying by type.
1424 */
1425TEST_F(InputReaderPolicyTest, Viewports_ByTypeReturnsDefaultForInternal) {
1426 const std::string uniqueId1 = "uniqueId1";
1427 const std::string uniqueId2 = "uniqueId2";
1428 constexpr int32_t nonDefaultDisplayId = 2;
1429 static_assert(nonDefaultDisplayId != ADISPLAY_ID_DEFAULT,
1430 "Test display ID should not be ADISPLAY_ID_DEFAULT");
1431
1432 // Add the default display first and ensure it gets returned.
1433 mFakePolicy->clearViewports();
1434 mFakePolicy->addDisplayViewport(ADISPLAY_ID_DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001435 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001436 ViewportType::INTERNAL);
1437 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001438 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001439 ViewportType::INTERNAL);
1440
1441 std::optional<DisplayViewport> viewport =
1442 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
1443 ASSERT_TRUE(viewport);
1444 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
1445 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
1446
1447 // Add the default display second to make sure order doesn't matter.
1448 mFakePolicy->clearViewports();
1449 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001450 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001451 ViewportType::INTERNAL);
1452 mFakePolicy->addDisplayViewport(ADISPLAY_ID_DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001453 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001454 ViewportType::INTERNAL);
1455
1456 viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
1457 ASSERT_TRUE(viewport);
1458 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
1459 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
1460}
1461
1462/**
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001463 * Check getDisplayViewportByPort
1464 */
1465TEST_F(InputReaderPolicyTest, Viewports_GetByPort) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001466 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001467 const std::string uniqueId1 = "uniqueId1";
1468 const std::string uniqueId2 = "uniqueId2";
1469 constexpr int32_t displayId1 = 1;
1470 constexpr int32_t displayId2 = 2;
1471 const uint8_t hdmi1 = 0;
1472 const uint8_t hdmi2 = 1;
1473 const uint8_t hdmi3 = 2;
1474
1475 mFakePolicy->clearViewports();
1476 // Add a viewport that's associated with some display port that's not of interest.
1477 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001478 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, hdmi3,
1479 type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001480 // Add another viewport, connected to HDMI1 port
1481 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001482 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, hdmi1,
1483 type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001484
1485 // Check that correct display viewport was returned by comparing the display ports.
1486 std::optional<DisplayViewport> hdmi1Viewport = mFakePolicy->getDisplayViewportByPort(hdmi1);
1487 ASSERT_TRUE(hdmi1Viewport);
1488 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1489 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1490
1491 // Check that we can still get the same viewport using the uniqueId
1492 hdmi1Viewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
1493 ASSERT_TRUE(hdmi1Viewport);
1494 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1495 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1496 ASSERT_EQ(type, hdmi1Viewport->type);
1497
1498 // Check that we cannot find a port with "HDMI2", because we never added one
1499 std::optional<DisplayViewport> hdmi2Viewport = mFakePolicy->getDisplayViewportByPort(hdmi2);
1500 ASSERT_FALSE(hdmi2Viewport);
1501}
1502
Michael Wrightd02c5b62014-02-10 15:10:22 -08001503// --- InputReaderTest ---
1504
1505class InputReaderTest : public testing::Test {
1506protected:
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001507 sp<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001508 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001509 std::shared_ptr<FakeEventHub> mFakeEventHub;
Prabir Pradhan28efc192019-11-05 01:10:04 +00001510 std::unique_ptr<InstrumentedInputReader> mReader;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001511
Chris Yea52ade12020-08-27 16:49:20 -07001512 void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001513 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001514 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001515 mFakeListener = new TestInputListener();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001516
Prabir Pradhan28efc192019-11-05 01:10:04 +00001517 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
1518 mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001519 }
1520
Chris Yea52ade12020-08-27 16:49:20 -07001521 void TearDown() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001522 mFakeListener.clear();
1523 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001524 }
1525
Chris Ye1b0c7342020-07-28 21:57:03 -07001526 void addDevice(int32_t eventHubId, const std::string& name, Flags<InputDeviceClass> classes,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001527 const PropertyMap* configuration) {
1528 mFakeEventHub->addDevice(eventHubId, name, classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001529
1530 if (configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001531 mFakeEventHub->addConfigurationMap(eventHubId, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001532 }
1533 mFakeEventHub->finishDeviceScan();
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001534 mReader->loopOnce();
1535 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001536 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1537 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001538 }
1539
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001540 void disableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001541 mFakePolicy->addDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001542 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001543 }
1544
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001545 void enableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001546 mFakePolicy->removeDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001547 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001548 }
1549
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001550 FakeInputMapper& addDeviceWithFakeInputMapper(int32_t deviceId, int32_t eventHubId,
Chris Ye1b0c7342020-07-28 21:57:03 -07001551 const std::string& name,
1552 Flags<InputDeviceClass> classes, uint32_t sources,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001553 const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001554 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, name);
1555 FakeInputMapper& mapper = device->addMapper<FakeInputMapper>(eventHubId, sources);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001556 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001557 addDevice(eventHubId, name, classes, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001558 return mapper;
1559 }
1560};
1561
Chris Ye98d3f532020-10-01 21:48:59 -07001562TEST_F(InputReaderTest, PolicyGetInputDevices) {
1563 ASSERT_NO_FATAL_FAILURE(addDevice(1, "keyboard", InputDeviceClass::KEYBOARD, nullptr));
1564 ASSERT_NO_FATAL_FAILURE(addDevice(2, "ignored", Flags<InputDeviceClass>(0),
1565 nullptr)); // no classes so device will be ignored
Michael Wrightd02c5b62014-02-10 15:10:22 -08001566
1567 // Should also have received a notification describing the new input devices.
Chris Ye98d3f532020-10-01 21:48:59 -07001568 const std::vector<InputDeviceInfo>& inputDevices = mFakePolicy->getInputDevices();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001569 ASSERT_EQ(1U, inputDevices.size());
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001570 ASSERT_EQ(END_RESERVED_ID + 1, inputDevices[0].getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001571 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001572 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
1573 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00001574 ASSERT_EQ(0U, inputDevices[0].getMotionRanges().size());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001575}
1576
Chris Yee7310032020-09-22 15:36:28 -07001577TEST_F(InputReaderTest, GetMergedInputDevices) {
1578 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1579 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1580 // Add two subdevices to device
1581 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1582 // Must add at least one mapper or the device will be ignored!
1583 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1584 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1585
1586 // Push same device instance for next device to be added, so they'll have same identifier.
1587 mReader->pushNextDevice(device);
1588 mReader->pushNextDevice(device);
1589 ASSERT_NO_FATAL_FAILURE(
1590 addDevice(eventHubIds[0], "fake1", InputDeviceClass::KEYBOARD, nullptr));
1591 ASSERT_NO_FATAL_FAILURE(
1592 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
1593
1594 // Two devices will be merged to one input device as they have same identifier
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00001595 ASSERT_EQ(1U, mFakePolicy->getInputDevices().size());
Chris Yee7310032020-09-22 15:36:28 -07001596}
1597
Chris Yee14523a2020-12-19 13:46:00 -08001598TEST_F(InputReaderTest, GetMergedInputDevicesEnabled) {
1599 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1600 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1601 // Add two subdevices to device
1602 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1603 // Must add at least one mapper or the device will be ignored!
1604 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1605 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1606
1607 // Push same device instance for next device to be added, so they'll have same identifier.
1608 mReader->pushNextDevice(device);
1609 mReader->pushNextDevice(device);
1610 // Sensor device is initially disabled
1611 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1",
1612 InputDeviceClass::KEYBOARD | InputDeviceClass::SENSOR,
1613 nullptr));
1614 // Device is disabled because the only sub device is a sensor device and disabled initially.
1615 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1616 ASSERT_FALSE(device->isEnabled());
1617 ASSERT_NO_FATAL_FAILURE(
1618 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
1619 // The merged device is enabled if any sub device is enabled
1620 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1621 ASSERT_TRUE(device->isEnabled());
1622}
1623
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001624TEST_F(InputReaderTest, WhenEnabledChanges_SendsDeviceResetNotification) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001625 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001626 constexpr Flags<InputDeviceClass> deviceClass(InputDeviceClass::KEYBOARD);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001627 constexpr int32_t eventHubId = 1;
1628 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001629 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001630 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001631 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001632 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001633
Yi Kong9b14ac62018-07-17 13:48:38 -07001634 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001635
1636 NotifyDeviceResetArgs resetArgs;
1637 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001638 ASSERT_EQ(deviceId, resetArgs.deviceId);
1639
1640 ASSERT_EQ(device->isEnabled(), true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001641 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001642 mReader->loopOnce();
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001643
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001644 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001645 ASSERT_EQ(deviceId, resetArgs.deviceId);
1646 ASSERT_EQ(device->isEnabled(), false);
1647
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001648 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001649 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001650 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
1651 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasNotCalled());
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001652 ASSERT_EQ(device->isEnabled(), false);
1653
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001654 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001655 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001656 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001657 ASSERT_EQ(deviceId, resetArgs.deviceId);
1658 ASSERT_EQ(device->isEnabled(), true);
1659}
1660
Michael Wrightd02c5b62014-02-10 15:10:22 -08001661TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001662 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001663 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001664 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001665 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001666 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001667 AINPUT_SOURCE_KEYBOARD, nullptr);
1668 mapper.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001669
1670 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(0,
1671 AINPUT_SOURCE_ANY, AKEYCODE_A))
1672 << "Should return unknown when the device id is >= 0 but unknown.";
1673
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001674 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1675 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1676 << "Should return unknown when the device id is valid but the sources are not "
1677 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001678
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001679 ASSERT_EQ(AKEY_STATE_DOWN,
1680 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1681 AKEYCODE_A))
1682 << "Should return value provided by mapper when device id is valid and the device "
1683 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001684
1685 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(-1,
1686 AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1687 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1688
1689 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getKeyCodeState(-1,
1690 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1691 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1692}
1693
1694TEST_F(InputReaderTest, GetScanCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001695 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001696 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001697 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001698 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001699 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001700 AINPUT_SOURCE_KEYBOARD, nullptr);
1701 mapper.setScanCodeState(KEY_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001702
1703 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(0,
1704 AINPUT_SOURCE_ANY, KEY_A))
1705 << "Should return unknown when the device id is >= 0 but unknown.";
1706
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001707 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1708 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, KEY_A))
1709 << "Should return unknown when the device id is valid but the sources are not "
1710 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001711
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001712 ASSERT_EQ(AKEY_STATE_DOWN,
1713 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1714 KEY_A))
1715 << "Should return value provided by mapper when device id is valid and the device "
1716 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001717
1718 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(-1,
1719 AINPUT_SOURCE_TRACKBALL, KEY_A))
1720 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1721
1722 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getScanCodeState(-1,
1723 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, KEY_A))
1724 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1725}
1726
1727TEST_F(InputReaderTest, GetSwitchState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001728 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001729 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001730 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001731 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001732 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001733 AINPUT_SOURCE_KEYBOARD, nullptr);
1734 mapper.setSwitchState(SW_LID, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001735
1736 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(0,
1737 AINPUT_SOURCE_ANY, SW_LID))
1738 << "Should return unknown when the device id is >= 0 but unknown.";
1739
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001740 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1741 mReader->getSwitchState(deviceId, AINPUT_SOURCE_TRACKBALL, SW_LID))
1742 << "Should return unknown when the device id is valid but the sources are not "
1743 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001744
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001745 ASSERT_EQ(AKEY_STATE_DOWN,
1746 mReader->getSwitchState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1747 SW_LID))
1748 << "Should return value provided by mapper when device id is valid and the device "
1749 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001750
1751 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(-1,
1752 AINPUT_SOURCE_TRACKBALL, SW_LID))
1753 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1754
1755 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getSwitchState(-1,
1756 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, SW_LID))
1757 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1758}
1759
1760TEST_F(InputReaderTest, MarkSupportedKeyCodes_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001761 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001762 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001763 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001764 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001765 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001766 AINPUT_SOURCE_KEYBOARD, nullptr);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001767
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001768 mapper.addSupportedKeyCode(AKEYCODE_A);
1769 mapper.addSupportedKeyCode(AKEYCODE_B);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001770
1771 const int32_t keyCodes[4] = { AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2 };
1772 uint8_t flags[4] = { 0, 0, 0, 1 };
1773
1774 ASSERT_FALSE(mReader->hasKeys(0, AINPUT_SOURCE_ANY, 4, keyCodes, flags))
1775 << "Should return false when device id is >= 0 but unknown.";
1776 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1777
1778 flags[3] = 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001779 ASSERT_FALSE(mReader->hasKeys(deviceId, AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1780 << "Should return false when device id is valid but the sources are not supported by "
1781 "the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001782 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1783
1784 flags[3] = 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001785 ASSERT_TRUE(mReader->hasKeys(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, 4,
1786 keyCodes, flags))
1787 << "Should return value provided by mapper when device id is valid and the device "
1788 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001789 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1790
1791 flags[3] = 1;
1792 ASSERT_FALSE(mReader->hasKeys(-1, AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1793 << "Should return false when the device id is < 0 but the sources are not supported by any device.";
1794 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1795
1796 flags[3] = 1;
1797 ASSERT_TRUE(mReader->hasKeys(-1, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1798 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1799 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1800}
1801
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001802TEST_F(InputReaderTest, LoopOnce_WhenDeviceScanFinished_SendsConfigurationChanged) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001803 constexpr int32_t eventHubId = 1;
Chris Ye1b0c7342020-07-28 21:57:03 -07001804 addDevice(eventHubId, "ignored", InputDeviceClass::KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001805
1806 NotifyConfigurationChangedArgs args;
1807
1808 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(&args));
1809 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
1810}
1811
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001812TEST_F(InputReaderTest, LoopOnce_ForwardsRawEventsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001813 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001814 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001815 constexpr nsecs_t when = 0;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001816 constexpr int32_t eventHubId = 1;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001817 constexpr nsecs_t readTime = 2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001818 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001819 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001820 AINPUT_SOURCE_KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001821
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001822 mFakeEventHub->enqueueEvent(when, readTime, eventHubId, EV_KEY, KEY_A, 1);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001823 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001824 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
1825
1826 RawEvent event;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001827 ASSERT_NO_FATAL_FAILURE(mapper.assertProcessWasCalled(&event));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001828 ASSERT_EQ(when, event.when);
1829 ASSERT_EQ(readTime, event.readTime);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001830 ASSERT_EQ(eventHubId, event.deviceId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001831 ASSERT_EQ(EV_KEY, event.type);
1832 ASSERT_EQ(KEY_A, event.code);
1833 ASSERT_EQ(1, event.value);
1834}
1835
Garfield Tan1c7bc862020-01-28 13:24:04 -08001836TEST_F(InputReaderTest, DeviceReset_RandomId) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001837 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001838 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001839 constexpr int32_t eventHubId = 1;
1840 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Prabir Pradhan42611e02018-11-27 14:04:02 -08001841 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001842 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001843 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001844 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan42611e02018-11-27 14:04:02 -08001845
1846 NotifyDeviceResetArgs resetArgs;
1847 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001848 int32_t prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001849
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001850 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001851 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001852 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001853 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001854 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001855
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001856 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001857 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001858 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001859 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001860 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001861
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001862 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001863 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001864 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001865 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001866 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001867}
1868
Garfield Tan1c7bc862020-01-28 13:24:04 -08001869TEST_F(InputReaderTest, DeviceReset_GenerateIdWithInputReaderSource) {
1870 constexpr int32_t deviceId = 1;
Chris Ye1b0c7342020-07-28 21:57:03 -07001871 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Garfield Tan1c7bc862020-01-28 13:24:04 -08001872 constexpr int32_t eventHubId = 1;
1873 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1874 // Must add at least one mapper or the device will be ignored!
1875 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001876 mReader->pushNextDevice(device);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001877 ASSERT_NO_FATAL_FAILURE(addDevice(deviceId, "fake", deviceClass, nullptr));
1878
1879 NotifyDeviceResetArgs resetArgs;
1880 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1881 ASSERT_EQ(IdGenerator::Source::INPUT_READER, IdGenerator::getSource(resetArgs.id));
1882}
1883
Arthur Hungc23540e2018-11-29 20:42:11 +08001884TEST_F(InputReaderTest, Device_CanDispatchToDisplay) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001885 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001886 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001887 constexpr int32_t eventHubId = 1;
Arthur Hungc23540e2018-11-29 20:42:11 +08001888 const char* DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001889 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1890 FakeInputMapper& mapper =
1891 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001892 mReader->pushNextDevice(device);
Arthur Hungc23540e2018-11-29 20:42:11 +08001893
1894 const uint8_t hdmi1 = 1;
1895
1896 // Associated touch screen with second display.
1897 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
1898
1899 // Add default and second display.
Prabir Pradhan28efc192019-11-05 01:10:04 +00001900 mFakePolicy->clearViewports();
Arthur Hungc23540e2018-11-29 20:42:11 +08001901 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001902 DISPLAY_ORIENTATION_0, true /*isActive*/, "local:0", NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001903 ViewportType::INTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08001904 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001905 DISPLAY_ORIENTATION_0, true /*isActive*/, "local:1", hdmi1,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001906 ViewportType::EXTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08001907 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001908 mReader->loopOnce();
Prabir Pradhan28efc192019-11-05 01:10:04 +00001909
1910 // Add the device, and make sure all of the callbacks are triggered.
1911 // The device is added after the input port associations are processed since
1912 // we do not yet support dynamic device-to-display associations.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001913 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001914 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled());
Prabir Pradhan28efc192019-11-05 01:10:04 +00001915 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001916 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
Arthur Hungc23540e2018-11-29 20:42:11 +08001917
Arthur Hung2c9a3342019-07-23 14:18:59 +08001918 // Device should only dispatch to the specified display.
Arthur Hungc23540e2018-11-29 20:42:11 +08001919 ASSERT_EQ(deviceId, device->getId());
1920 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, DISPLAY_ID));
1921 ASSERT_TRUE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hung2c9a3342019-07-23 14:18:59 +08001922
1923 // Can't dispatch event from a disabled device.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001924 disableDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001925 mReader->loopOnce();
Arthur Hung2c9a3342019-07-23 14:18:59 +08001926 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hungc23540e2018-11-29 20:42:11 +08001927}
1928
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001929TEST_F(InputReaderTest, WhenEnabledChanges_AllSubdevicesAreUpdated) {
1930 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1931 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
1932 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1933 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1934 // Must add at least one mapper or the device will be ignored!
1935 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1936 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1937 mReader->pushNextDevice(device);
1938 mReader->pushNextDevice(device);
1939 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
1940 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
1941
1942 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
1943
1944 NotifyDeviceResetArgs resetArgs;
1945 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1946 ASSERT_EQ(deviceId, resetArgs.deviceId);
1947 ASSERT_TRUE(device->isEnabled());
1948 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1949 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1950
1951 disableDevice(deviceId);
1952 mReader->loopOnce();
1953
1954 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1955 ASSERT_EQ(deviceId, resetArgs.deviceId);
1956 ASSERT_FALSE(device->isEnabled());
1957 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1958 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1959
1960 enableDevice(deviceId);
1961 mReader->loopOnce();
1962
1963 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1964 ASSERT_EQ(deviceId, resetArgs.deviceId);
1965 ASSERT_TRUE(device->isEnabled());
1966 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1967 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1968}
1969
1970TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToSubdeviceMappers) {
1971 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1972 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
1973 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1974 // Add two subdevices to device
1975 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1976 FakeInputMapper& mapperDevice1 =
1977 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1978 FakeInputMapper& mapperDevice2 =
1979 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1980 mReader->pushNextDevice(device);
1981 mReader->pushNextDevice(device);
1982 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
1983 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
1984
1985 mapperDevice1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
1986 mapperDevice2.setKeyCodeState(AKEYCODE_B, AKEY_STATE_DOWN);
1987
1988 ASSERT_EQ(AKEY_STATE_DOWN,
1989 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_A));
1990 ASSERT_EQ(AKEY_STATE_DOWN,
1991 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_B));
1992 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1993 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_C));
1994}
1995
Prabir Pradhan7e186182020-11-10 13:56:45 -08001996TEST_F(InputReaderTest, ChangingPointerCaptureNotifiesInputListener) {
1997 NotifyPointerCaptureChangedArgs args;
1998
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001999 auto request = mFakePolicy->setPointerCapture(true);
Prabir Pradhan7e186182020-11-10 13:56:45 -08002000 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
2001 mReader->loopOnce();
2002 mFakeListener->assertNotifyCaptureWasCalled(&args);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002003 ASSERT_TRUE(args.request.enable) << "Pointer Capture should be enabled.";
2004 ASSERT_EQ(args.request, request) << "Pointer Capture sequence number should match.";
Prabir Pradhan7e186182020-11-10 13:56:45 -08002005
2006 mFakePolicy->setPointerCapture(false);
2007 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
2008 mReader->loopOnce();
2009 mFakeListener->assertNotifyCaptureWasCalled(&args);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002010 ASSERT_FALSE(args.request.enable) << "Pointer Capture should be disabled.";
Prabir Pradhan7e186182020-11-10 13:56:45 -08002011
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002012 // Verify that the Pointer Capture state is not updated when the configuration value
Prabir Pradhan7e186182020-11-10 13:56:45 -08002013 // does not change.
2014 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
2015 mReader->loopOnce();
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002016 mFakeListener->assertNotifyCaptureWasNotCalled();
Prabir Pradhan7e186182020-11-10 13:56:45 -08002017}
2018
Chris Ye87143712020-11-10 05:05:58 +00002019class FakeVibratorInputMapper : public FakeInputMapper {
2020public:
2021 FakeVibratorInputMapper(InputDeviceContext& deviceContext, uint32_t sources)
2022 : FakeInputMapper(deviceContext, sources) {}
2023
2024 std::vector<int32_t> getVibratorIds() override { return getDeviceContext().getVibratorIds(); }
2025};
2026
2027TEST_F(InputReaderTest, VibratorGetVibratorIds) {
2028 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
2029 Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD | InputDeviceClass::VIBRATOR;
2030 constexpr int32_t eventHubId = 1;
2031 const char* DEVICE_LOCATION = "BLUETOOTH";
2032 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
2033 FakeVibratorInputMapper& mapper =
2034 device->addMapper<FakeVibratorInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
2035 mReader->pushNextDevice(device);
2036
2037 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2038 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
2039
2040 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
2041 ASSERT_EQ(mReader->getVibratorIds(deviceId).size(), 2U);
2042}
2043
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002044// --- FakePeripheralController ---
Kim Low03ea0352020-11-06 12:45:07 -08002045
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002046class FakePeripheralController : public PeripheralControllerInterface {
Chris Yee2b1e5c2021-03-10 22:45:12 -08002047public:
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002048 FakePeripheralController(InputDeviceContext& deviceContext) : mDeviceContext(deviceContext) {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08002049
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002050 ~FakePeripheralController() override {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08002051
2052 void populateDeviceInfo(InputDeviceInfo* deviceInfo) override {}
2053
2054 void dump(std::string& dump) override {}
2055
2056 std::optional<int32_t> getBatteryCapacity(int32_t batteryId) override {
2057 return getDeviceContext().getBatteryCapacity(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08002058 }
2059
Chris Yee2b1e5c2021-03-10 22:45:12 -08002060 std::optional<int32_t> getBatteryStatus(int32_t batteryId) override {
2061 return getDeviceContext().getBatteryStatus(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08002062 }
Chris Ye3fdbfef2021-01-06 18:45:18 -08002063
2064 bool setLightColor(int32_t lightId, int32_t color) override {
2065 getDeviceContext().setLightBrightness(lightId, color >> 24);
2066 return true;
2067 }
2068
2069 std::optional<int32_t> getLightColor(int32_t lightId) override {
2070 std::optional<int32_t> result = getDeviceContext().getLightBrightness(lightId);
2071 if (!result.has_value()) {
2072 return std::nullopt;
2073 }
2074 return result.value() << 24;
2075 }
Chris Yee2b1e5c2021-03-10 22:45:12 -08002076
2077 bool setLightPlayerId(int32_t lightId, int32_t playerId) override { return true; }
2078
2079 std::optional<int32_t> getLightPlayerId(int32_t lightId) override { return std::nullopt; }
2080
2081private:
2082 InputDeviceContext& mDeviceContext;
2083 inline int32_t getDeviceId() { return mDeviceContext.getId(); }
2084 inline InputDeviceContext& getDeviceContext() { return mDeviceContext; }
Chris Ye3fdbfef2021-01-06 18:45:18 -08002085};
2086
Chris Yee2b1e5c2021-03-10 22:45:12 -08002087TEST_F(InputReaderTest, BatteryGetCapacity) {
2088 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
2089 Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
2090 constexpr int32_t eventHubId = 1;
2091 const char* DEVICE_LOCATION = "BLUETOOTH";
2092 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002093 FakePeripheralController& controller =
2094 device->addController<FakePeripheralController>(eventHubId);
Chris Yee2b1e5c2021-03-10 22:45:12 -08002095 mReader->pushNextDevice(device);
2096
2097 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2098
2099 ASSERT_EQ(controller.getBatteryCapacity(DEFAULT_BATTERY), BATTERY_CAPACITY);
2100 ASSERT_EQ(mReader->getBatteryCapacity(deviceId), BATTERY_CAPACITY);
2101}
2102
2103TEST_F(InputReaderTest, BatteryGetStatus) {
2104 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
2105 Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
2106 constexpr int32_t eventHubId = 1;
2107 const char* DEVICE_LOCATION = "BLUETOOTH";
2108 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002109 FakePeripheralController& controller =
2110 device->addController<FakePeripheralController>(eventHubId);
Chris Yee2b1e5c2021-03-10 22:45:12 -08002111 mReader->pushNextDevice(device);
2112
2113 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2114
2115 ASSERT_EQ(controller.getBatteryStatus(DEFAULT_BATTERY), BATTERY_STATUS);
2116 ASSERT_EQ(mReader->getBatteryStatus(deviceId), BATTERY_STATUS);
2117}
2118
Chris Ye3fdbfef2021-01-06 18:45:18 -08002119TEST_F(InputReaderTest, LightGetColor) {
2120 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
2121 Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD | InputDeviceClass::LIGHT;
2122 constexpr int32_t eventHubId = 1;
2123 const char* DEVICE_LOCATION = "BLUETOOTH";
2124 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002125 FakePeripheralController& controller =
2126 device->addController<FakePeripheralController>(eventHubId);
Chris Ye3fdbfef2021-01-06 18:45:18 -08002127 mReader->pushNextDevice(device);
2128 RawLightInfo info = {.id = 1,
2129 .name = "Mono",
2130 .maxBrightness = 255,
2131 .flags = InputLightClass::BRIGHTNESS,
2132 .path = ""};
2133 mFakeEventHub->addRawLightInfo(1 /* rawId */, std::move(info));
2134 mFakeEventHub->fakeLightBrightness(1 /* rawId */, 0x55);
2135
2136 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Chris Ye3fdbfef2021-01-06 18:45:18 -08002137
Chris Yee2b1e5c2021-03-10 22:45:12 -08002138 ASSERT_TRUE(controller.setLightColor(1 /* lightId */, LIGHT_BRIGHTNESS));
2139 ASSERT_EQ(controller.getLightColor(1 /* lightId */), LIGHT_BRIGHTNESS);
Chris Ye3fdbfef2021-01-06 18:45:18 -08002140 ASSERT_TRUE(mReader->setLightColor(deviceId, 1 /* lightId */, LIGHT_BRIGHTNESS));
2141 ASSERT_EQ(mReader->getLightColor(deviceId, 1 /* lightId */), LIGHT_BRIGHTNESS);
2142}
2143
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002144// --- InputReaderIntegrationTest ---
2145
2146// These tests create and interact with the InputReader only through its interface.
2147// The InputReader is started during SetUp(), which starts its processing in its own
2148// thread. The tests use linux uinput to emulate input devices.
2149// NOTE: Interacting with the physical device while these tests are running may cause
2150// the tests to fail.
2151class InputReaderIntegrationTest : public testing::Test {
2152protected:
2153 sp<TestInputListener> mTestListener;
2154 sp<FakeInputReaderPolicy> mFakePolicy;
2155 sp<InputReaderInterface> mReader;
2156
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00002157 std::shared_ptr<FakePointerController> mFakePointerController;
2158
Chris Yea52ade12020-08-27 16:49:20 -07002159 void SetUp() override {
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002160 mFakePolicy = new FakeInputReaderPolicy();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00002161 mFakePointerController = std::make_shared<FakePointerController>();
2162 mFakePolicy->setPointerController(mFakePointerController);
Siarhei Vishniakouf0db5b82020-04-08 19:22:14 -07002163 mTestListener = new TestInputListener(2000ms /*eventHappenedTimeout*/,
2164 30ms /*eventDidNotHappenTimeout*/);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002165
Prabir Pradhan9244aea2020-02-05 20:31:40 -08002166 mReader = new InputReader(std::make_shared<EventHub>(), mFakePolicy, mTestListener);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002167 ASSERT_EQ(mReader->start(), OK);
2168
2169 // Since this test is run on a real device, all the input devices connected
2170 // to the test device will show up in mReader. We wait for those input devices to
2171 // show up before beginning the tests.
2172 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2173 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2174 }
2175
Chris Yea52ade12020-08-27 16:49:20 -07002176 void TearDown() override {
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002177 ASSERT_EQ(mReader->stop(), OK);
2178 mTestListener.clear();
2179 mFakePolicy.clear();
2180 }
2181};
2182
2183TEST_F(InputReaderIntegrationTest, TestInvalidDevice) {
2184 // An invalid input device that is only used for this test.
2185 class InvalidUinputDevice : public UinputDevice {
2186 public:
2187 InvalidUinputDevice() : UinputDevice("Invalid Device") {}
2188
2189 private:
2190 void configureDevice(int fd, uinput_user_dev* device) override {}
2191 };
2192
2193 const size_t numDevices = mFakePolicy->getInputDevices().size();
2194
2195 // UinputDevice does not set any event or key bits, so InputReader should not
2196 // consider it as a valid device.
2197 std::unique_ptr<UinputDevice> invalidDevice = createUinputDevice<InvalidUinputDevice>();
2198 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
2199 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
2200 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
2201
2202 invalidDevice.reset();
2203 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
2204 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
2205 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
2206}
2207
2208TEST_F(InputReaderIntegrationTest, AddNewDevice) {
2209 const size_t initialNumDevices = mFakePolicy->getInputDevices().size();
2210
2211 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
2212 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2213 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2214 ASSERT_EQ(initialNumDevices + 1, mFakePolicy->getInputDevices().size());
2215
2216 // Find the test device by its name.
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002217 const std::vector<InputDeviceInfo> inputDevices = mFakePolicy->getInputDevices();
Chris Ye98d3f532020-10-01 21:48:59 -07002218 const auto& it =
2219 std::find_if(inputDevices.begin(), inputDevices.end(),
2220 [&keyboard](const InputDeviceInfo& info) {
2221 return info.getIdentifier().name == keyboard->getName();
2222 });
2223
2224 ASSERT_NE(it, inputDevices.end());
2225 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, it->getKeyboardType());
2226 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, it->getSources());
2227 ASSERT_EQ(0U, it->getMotionRanges().size());
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002228
2229 keyboard.reset();
2230 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2231 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2232 ASSERT_EQ(initialNumDevices, mFakePolicy->getInputDevices().size());
2233}
2234
2235TEST_F(InputReaderIntegrationTest, SendsEventsToInputListener) {
2236 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
2237 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2238
2239 NotifyConfigurationChangedArgs configChangedArgs;
2240 ASSERT_NO_FATAL_FAILURE(
2241 mTestListener->assertNotifyConfigurationChangedWasCalled(&configChangedArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002242 int32_t prevId = configChangedArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002243 nsecs_t prevTimestamp = configChangedArgs.eventTime;
2244
2245 NotifyKeyArgs keyArgs;
2246 keyboard->pressAndReleaseHomeKey();
2247 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
2248 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002249 ASSERT_NE(prevId, keyArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002250 prevId = keyArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002251 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002252 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002253 prevTimestamp = keyArgs.eventTime;
2254
2255 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
2256 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002257 ASSERT_NE(prevId, keyArgs.id);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002258 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002259 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002260}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002261
Siarhei Vishniakoua0d2b802020-05-13 14:00:31 -07002262/**
2263 * The Steam controller sends BTN_GEAR_DOWN and BTN_GEAR_UP for the two "paddle" buttons
2264 * on the back. In this test, we make sure that BTN_GEAR_DOWN / BTN_WHEEL and BTN_GEAR_UP
2265 * are passed to the listener.
2266 */
2267static_assert(BTN_GEAR_DOWN == BTN_WHEEL);
2268TEST_F(InputReaderIntegrationTest, SendsGearDownAndUpToInputListener) {
2269 std::unique_ptr<UinputSteamController> controller = createUinputDevice<UinputSteamController>();
2270 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2271 NotifyKeyArgs keyArgs;
2272
2273 controller->pressAndReleaseKey(BTN_GEAR_DOWN);
2274 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
2275 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
2276 ASSERT_EQ(BTN_GEAR_DOWN, keyArgs.scanCode);
2277
2278 controller->pressAndReleaseKey(BTN_GEAR_UP);
2279 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
2280 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
2281 ASSERT_EQ(BTN_GEAR_UP, keyArgs.scanCode);
2282}
2283
Arthur Hungaab25622020-01-16 11:22:11 +08002284// --- TouchProcessTest ---
2285class TouchIntegrationTest : public InputReaderIntegrationTest {
2286protected:
Arthur Hungaab25622020-01-16 11:22:11 +08002287 const std::string UNIQUE_ID = "local:0";
2288
Chris Yea52ade12020-08-27 16:49:20 -07002289 void SetUp() override {
Arthur Hungaab25622020-01-16 11:22:11 +08002290 InputReaderIntegrationTest::SetUp();
2291 // At least add an internal display.
2292 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2293 DISPLAY_ORIENTATION_0, UNIQUE_ID, NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002294 ViewportType::INTERNAL);
Arthur Hungaab25622020-01-16 11:22:11 +08002295
2296 mDevice = createUinputDevice<UinputTouchScreen>(Rect(0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT));
2297 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2298 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2299 }
2300
2301 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
2302 int32_t orientation, const std::string& uniqueId,
2303 std::optional<uint8_t> physicalPort,
2304 ViewportType viewportType) {
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002305 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, true /*isActive*/,
2306 uniqueId, physicalPort, viewportType);
Arthur Hungaab25622020-01-16 11:22:11 +08002307 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2308 }
2309
2310 std::unique_ptr<UinputTouchScreen> mDevice;
2311};
2312
2313TEST_F(TouchIntegrationTest, InputEvent_ProcessSingleTouch) {
2314 NotifyMotionArgs args;
2315 const Point centerPoint = mDevice->getCenterPoint();
2316
2317 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00002318 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002319 mDevice->sendDown(centerPoint);
2320 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2321 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2322
2323 // ACTION_MOVE
2324 mDevice->sendMove(centerPoint + Point(1, 1));
2325 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2326 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2327
2328 // ACTION_UP
2329 mDevice->sendUp();
2330 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2331 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2332}
2333
2334TEST_F(TouchIntegrationTest, InputEvent_ProcessMultiTouch) {
2335 NotifyMotionArgs args;
2336 const Point centerPoint = mDevice->getCenterPoint();
2337
2338 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00002339 mDevice->sendSlot(FIRST_SLOT);
2340 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002341 mDevice->sendDown(centerPoint);
2342 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2343 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2344
2345 // ACTION_POINTER_DOWN (Second slot)
2346 const Point secondPoint = centerPoint + Point(100, 100);
2347 mDevice->sendSlot(SECOND_SLOT);
2348 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2349 mDevice->sendDown(secondPoint + Point(1, 1));
2350 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2351 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
2352 args.action);
2353
2354 // ACTION_MOVE (Second slot)
2355 mDevice->sendMove(secondPoint);
2356 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2357 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2358
2359 // ACTION_POINTER_UP (Second slot)
arthurhungcc7f9802020-04-30 17:55:40 +08002360 mDevice->sendPointerUp();
Arthur Hungaab25622020-01-16 11:22:11 +08002361 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
arthurhungcc7f9802020-04-30 17:55:40 +08002362 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
Arthur Hungaab25622020-01-16 11:22:11 +08002363 args.action);
2364
2365 // ACTION_UP
2366 mDevice->sendSlot(FIRST_SLOT);
2367 mDevice->sendUp();
2368 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2369 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2370}
2371
2372TEST_F(TouchIntegrationTest, InputEvent_ProcessPalm) {
2373 NotifyMotionArgs args;
2374 const Point centerPoint = mDevice->getCenterPoint();
2375
2376 // ACTION_DOWN
arthurhungcc7f9802020-04-30 17:55:40 +08002377 mDevice->sendSlot(FIRST_SLOT);
2378 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002379 mDevice->sendDown(centerPoint);
2380 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2381 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2382
arthurhungcc7f9802020-04-30 17:55:40 +08002383 // ACTION_POINTER_DOWN (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002384 const Point secondPoint = centerPoint + Point(100, 100);
2385 mDevice->sendSlot(SECOND_SLOT);
2386 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2387 mDevice->sendDown(secondPoint);
2388 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2389 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
2390 args.action);
2391
arthurhungcc7f9802020-04-30 17:55:40 +08002392 // ACTION_MOVE (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002393 mDevice->sendMove(secondPoint + Point(1, 1));
2394 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2395 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2396
arthurhungcc7f9802020-04-30 17:55:40 +08002397 // Send MT_TOOL_PALM (second slot), which indicates that the touch IC has determined this to be
2398 // a palm event.
2399 // Expect to receive the ACTION_POINTER_UP with cancel flag.
Arthur Hungaab25622020-01-16 11:22:11 +08002400 mDevice->sendToolType(MT_TOOL_PALM);
2401 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
arthurhungcc7f9802020-04-30 17:55:40 +08002402 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
2403 args.action);
2404 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, args.flags);
Arthur Hungaab25622020-01-16 11:22:11 +08002405
arthurhungcc7f9802020-04-30 17:55:40 +08002406 // Send up to second slot, expect first slot send moving.
2407 mDevice->sendPointerUp();
2408 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2409 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002410
arthurhungcc7f9802020-04-30 17:55:40 +08002411 // Send ACTION_UP (first slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002412 mDevice->sendSlot(FIRST_SLOT);
2413 mDevice->sendUp();
2414
arthurhungcc7f9802020-04-30 17:55:40 +08002415 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2416 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002417}
2418
Michael Wrightd02c5b62014-02-10 15:10:22 -08002419// --- InputDeviceTest ---
Michael Wrightd02c5b62014-02-10 15:10:22 -08002420class InputDeviceTest : public testing::Test {
2421protected:
2422 static const char* DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002423 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002424 static const int32_t DEVICE_ID;
2425 static const int32_t DEVICE_GENERATION;
2426 static const int32_t DEVICE_CONTROLLER_NUMBER;
Chris Ye1b0c7342020-07-28 21:57:03 -07002427 static const Flags<InputDeviceClass> DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002428 static const int32_t EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002429
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002430 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002431 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08002432 sp<TestInputListener> mFakeListener;
arthurhungdcef2dc2020-08-11 14:47:50 +08002433 std::unique_ptr<InstrumentedInputReader> mReader;
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00002434 std::shared_ptr<InputDevice> mDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002435
Chris Yea52ade12020-08-27 16:49:20 -07002436 void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002437 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002438 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08002439 mFakeListener = new TestInputListener();
arthurhungdcef2dc2020-08-11 14:47:50 +08002440 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
2441 mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002442 InputDeviceIdentifier identifier;
2443 identifier.name = DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002444 identifier.location = DEVICE_LOCATION;
arthurhungdcef2dc2020-08-11 14:47:50 +08002445 mDevice = std::make_shared<InputDevice>(mReader->getContext(), DEVICE_ID, DEVICE_GENERATION,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002446 identifier);
arthurhungdcef2dc2020-08-11 14:47:50 +08002447 mReader->pushNextDevice(mDevice);
2448 mFakeEventHub->addDevice(EVENTHUB_ID, DEVICE_NAME, Flags<InputDeviceClass>(0));
2449 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002450 }
2451
Chris Yea52ade12020-08-27 16:49:20 -07002452 void TearDown() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002453 mFakeListener.clear();
2454 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002455 }
2456};
2457
2458const char* InputDeviceTest::DEVICE_NAME = "device";
Arthur Hung2c9a3342019-07-23 14:18:59 +08002459const char* InputDeviceTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002460const int32_t InputDeviceTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002461const int32_t InputDeviceTest::DEVICE_GENERATION = 2;
2462const int32_t InputDeviceTest::DEVICE_CONTROLLER_NUMBER = 0;
Chris Ye1b0c7342020-07-28 21:57:03 -07002463const Flags<InputDeviceClass> InputDeviceTest::DEVICE_CLASSES =
2464 InputDeviceClass::KEYBOARD | InputDeviceClass::TOUCH | InputDeviceClass::JOYSTICK;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002465const int32_t InputDeviceTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002466
2467TEST_F(InputDeviceTest, ImmutableProperties) {
2468 ASSERT_EQ(DEVICE_ID, mDevice->getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002469 ASSERT_STREQ(DEVICE_NAME, mDevice->getName().c_str());
Chris Ye1b0c7342020-07-28 21:57:03 -07002470 ASSERT_EQ(Flags<InputDeviceClass>(0), mDevice->getClasses());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002471}
2472
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002473TEST_F(InputDeviceTest, WhenDeviceCreated_EnabledIsFalse) {
2474 ASSERT_EQ(mDevice->isEnabled(), false);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07002475}
2476
Michael Wrightd02c5b62014-02-10 15:10:22 -08002477TEST_F(InputDeviceTest, WhenNoMappersAreRegistered_DeviceIsIgnored) {
2478 // Configuration.
2479 InputReaderConfiguration config;
2480 mDevice->configure(ARBITRARY_TIME, &config, 0);
2481
2482 // Reset.
2483 mDevice->reset(ARBITRARY_TIME);
2484
2485 NotifyDeviceResetArgs resetArgs;
2486 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2487 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2488 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2489
2490 // Metadata.
2491 ASSERT_TRUE(mDevice->isIgnored());
2492 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mDevice->getSources());
2493
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002494 InputDeviceInfo info = mDevice->getDeviceInfo();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002495 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002496 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002497 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NONE, info.getKeyboardType());
2498 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, info.getSources());
2499
2500 // State queries.
2501 ASSERT_EQ(0, mDevice->getMetaState());
2502
2503 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2504 << "Ignored device should return unknown key code state.";
2505 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2506 << "Ignored device should return unknown scan code state.";
2507 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 0))
2508 << "Ignored device should return unknown switch state.";
2509
2510 const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
2511 uint8_t flags[2] = { 0, 1 };
2512 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, 2, keyCodes, flags))
2513 << "Ignored device should never mark any key codes.";
2514 ASSERT_EQ(0, flags[0]) << "Flag for unsupported key should be unchanged.";
2515 ASSERT_EQ(1, flags[1]) << "Flag for unsupported key should be unchanged.";
2516}
2517
2518TEST_F(InputDeviceTest, WhenMappersAreRegistered_DeviceIsNotIgnoredAndForwardsRequestsToMappers) {
2519 // Configuration.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002520 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, String8("key"), String8("value"));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002521
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002522 FakeInputMapper& mapper1 =
2523 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002524 mapper1.setKeyboardType(AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2525 mapper1.setMetaState(AMETA_ALT_ON);
2526 mapper1.addSupportedKeyCode(AKEYCODE_A);
2527 mapper1.addSupportedKeyCode(AKEYCODE_B);
2528 mapper1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
2529 mapper1.setKeyCodeState(AKEYCODE_B, AKEY_STATE_UP);
2530 mapper1.setScanCodeState(2, AKEY_STATE_DOWN);
2531 mapper1.setScanCodeState(3, AKEY_STATE_UP);
2532 mapper1.setSwitchState(4, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002533
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002534 FakeInputMapper& mapper2 =
2535 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002536 mapper2.setMetaState(AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002537
2538 InputReaderConfiguration config;
2539 mDevice->configure(ARBITRARY_TIME, &config, 0);
2540
2541 String8 propertyValue;
2542 ASSERT_TRUE(mDevice->getConfiguration().tryGetProperty(String8("key"), propertyValue))
2543 << "Device should have read configuration during configuration phase.";
2544 ASSERT_STREQ("value", propertyValue.string());
2545
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002546 ASSERT_NO_FATAL_FAILURE(mapper1.assertConfigureWasCalled());
2547 ASSERT_NO_FATAL_FAILURE(mapper2.assertConfigureWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002548
2549 // Reset
2550 mDevice->reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002551 ASSERT_NO_FATAL_FAILURE(mapper1.assertResetWasCalled());
2552 ASSERT_NO_FATAL_FAILURE(mapper2.assertResetWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002553
2554 NotifyDeviceResetArgs resetArgs;
2555 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2556 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2557 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2558
2559 // Metadata.
2560 ASSERT_FALSE(mDevice->isIgnored());
2561 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), mDevice->getSources());
2562
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002563 InputDeviceInfo info = mDevice->getDeviceInfo();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002564 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002565 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002566 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_ALPHABETIC, info.getKeyboardType());
2567 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), info.getSources());
2568
2569 // State queries.
2570 ASSERT_EQ(AMETA_ALT_ON | AMETA_SHIFT_ON, mDevice->getMetaState())
2571 << "Should query mappers and combine meta states.";
2572
2573 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2574 << "Should return unknown key code state when source not supported.";
2575 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2576 << "Should return unknown scan code state when source not supported.";
2577 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2578 << "Should return unknown switch state when source not supported.";
2579
2580 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, AKEYCODE_A))
2581 << "Should query mapper when source is supported.";
2582 ASSERT_EQ(AKEY_STATE_UP, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 3))
2583 << "Should query mapper when source is supported.";
2584 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 4))
2585 << "Should query mapper when source is supported.";
2586
2587 const int32_t keyCodes[4] = { AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2 };
2588 uint8_t flags[4] = { 0, 0, 0, 1 };
2589 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
2590 << "Should do nothing when source is unsupported.";
2591 ASSERT_EQ(0, flags[0]) << "Flag should be unchanged when source is unsupported.";
2592 ASSERT_EQ(0, flags[1]) << "Flag should be unchanged when source is unsupported.";
2593 ASSERT_EQ(0, flags[2]) << "Flag should be unchanged when source is unsupported.";
2594 ASSERT_EQ(1, flags[3]) << "Flag should be unchanged when source is unsupported.";
2595
2596 ASSERT_TRUE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, 4, keyCodes, flags))
2597 << "Should query mapper when source is supported.";
2598 ASSERT_EQ(1, flags[0]) << "Flag for supported key should be set.";
2599 ASSERT_EQ(1, flags[1]) << "Flag for supported key should be set.";
2600 ASSERT_EQ(0, flags[2]) << "Flag for unsupported key should be unchanged.";
2601 ASSERT_EQ(1, flags[3]) << "Flag for unsupported key should be unchanged.";
2602
2603 // Event handling.
2604 RawEvent event;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002605 event.deviceId = EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002606 mDevice->process(&event, 1);
2607
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002608 ASSERT_NO_FATAL_FAILURE(mapper1.assertProcessWasCalled());
2609 ASSERT_NO_FATAL_FAILURE(mapper2.assertProcessWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002610}
2611
Arthur Hung2c9a3342019-07-23 14:18:59 +08002612// A single input device is associated with a specific display. Check that:
2613// 1. Device is disabled if the viewport corresponding to the associated display is not found
2614// 2. Device is disabled when setEnabled API is called
2615TEST_F(InputDeviceTest, Configure_AssignsDisplayPort) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002616 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002617
2618 // First Configuration.
2619 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
2620
2621 // Device should be enabled by default.
2622 ASSERT_TRUE(mDevice->isEnabled());
2623
2624 // Prepare associated info.
2625 constexpr uint8_t hdmi = 1;
2626 const std::string UNIQUE_ID = "local:1";
2627
2628 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi);
2629 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2630 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2631 // Device should be disabled because it is associated with a specific display via
2632 // input port <-> display port association, but the corresponding display is not found
2633 ASSERT_FALSE(mDevice->isEnabled());
2634
2635 // Prepare displays.
2636 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002637 DISPLAY_ORIENTATION_0, true /*isActive*/, UNIQUE_ID, hdmi,
2638 ViewportType::INTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002639 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2640 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2641 ASSERT_TRUE(mDevice->isEnabled());
2642
2643 // Device should be disabled after set disable.
2644 mFakePolicy->addDisabledDevice(mDevice->getId());
2645 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2646 InputReaderConfiguration::CHANGE_ENABLED_STATE);
2647 ASSERT_FALSE(mDevice->isEnabled());
2648
2649 // Device should still be disabled even found the associated display.
2650 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2651 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2652 ASSERT_FALSE(mDevice->isEnabled());
2653}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002654
Christine Franks1ba71cc2021-04-07 14:37:42 -07002655TEST_F(InputDeviceTest, Configure_AssignsDisplayUniqueId) {
2656 // Device should be enabled by default.
2657 mFakePolicy->clearViewports();
2658 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
2659 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
2660 ASSERT_TRUE(mDevice->isEnabled());
2661
2662 // Device should be disabled because it is associated with a specific display, but the
2663 // corresponding display is not found.
2664 const std::string DISPLAY_UNIQUE_ID = "displayUniqueId";
2665 mFakePolicy->addInputUniqueIdAssociation(DEVICE_NAME, DISPLAY_UNIQUE_ID);
2666 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2667 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2668 ASSERT_FALSE(mDevice->isEnabled());
2669
2670 // Device should be enabled when a display is found.
2671 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2672 DISPLAY_ORIENTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
2673 NO_PORT, ViewportType::INTERNAL);
2674 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2675 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2676 ASSERT_TRUE(mDevice->isEnabled());
2677
2678 // Device should be disabled after set disable.
2679 mFakePolicy->addDisabledDevice(mDevice->getId());
2680 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2681 InputReaderConfiguration::CHANGE_ENABLED_STATE);
2682 ASSERT_FALSE(mDevice->isEnabled());
2683
2684 // Device should still be disabled even found the associated display.
2685 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2686 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2687 ASSERT_FALSE(mDevice->isEnabled());
2688}
2689
Michael Wrightd02c5b62014-02-10 15:10:22 -08002690// --- InputMapperTest ---
2691
2692class InputMapperTest : public testing::Test {
2693protected:
2694 static const char* DEVICE_NAME;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002695 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002696 static const int32_t DEVICE_ID;
2697 static const int32_t DEVICE_GENERATION;
2698 static const int32_t DEVICE_CONTROLLER_NUMBER;
Chris Ye1b0c7342020-07-28 21:57:03 -07002699 static const Flags<InputDeviceClass> DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002700 static const int32_t EVENTHUB_ID;
Prabir Pradhanc14266f2021-05-12 15:56:24 -07002701 static const std::optional<bool> INITIAL_PER_WINDOW_INPUT_ROTATION_FLAG_VALUE;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002702
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002703 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002704 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08002705 sp<TestInputListener> mFakeListener;
arthurhungdcef2dc2020-08-11 14:47:50 +08002706 std::unique_ptr<InstrumentedInputReader> mReader;
2707 std::shared_ptr<InputDevice> mDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002708
Chris Ye1b0c7342020-07-28 21:57:03 -07002709 virtual void SetUp(Flags<InputDeviceClass> classes) {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002710 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002711 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08002712 mFakeListener = new TestInputListener();
arthurhungdcef2dc2020-08-11 14:47:50 +08002713 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
2714 mFakeListener);
2715 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002716 }
2717
Prabir Pradhanc14266f2021-05-12 15:56:24 -07002718 void SetUp() override {
2719 // Ensure per_window_input_rotation is enabled.
2720 sysprop::InputFlingerProperties::per_window_input_rotation(true);
2721
2722 SetUp(DEVICE_CLASSES);
2723 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002724
Chris Yea52ade12020-08-27 16:49:20 -07002725 void TearDown() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002726 mFakeListener.clear();
2727 mFakePolicy.clear();
Prabir Pradhanc14266f2021-05-12 15:56:24 -07002728
2729 sysprop::InputFlingerProperties::per_window_input_rotation(
2730 INITIAL_PER_WINDOW_INPUT_ROTATION_FLAG_VALUE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002731 }
2732
2733 void addConfigurationProperty(const char* key, const char* value) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002734 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, String8(key), String8(value));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002735 }
2736
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002737 void configureDevice(uint32_t changes) {
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -08002738 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
arthurhungdcef2dc2020-08-11 14:47:50 +08002739 mReader->requestRefreshConfiguration(changes);
2740 mReader->loopOnce();
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -08002741 }
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002742 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
2743 }
2744
arthurhungdcef2dc2020-08-11 14:47:50 +08002745 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
2746 const std::string& location, int32_t eventHubId,
2747 Flags<InputDeviceClass> classes) {
2748 InputDeviceIdentifier identifier;
2749 identifier.name = name;
2750 identifier.location = location;
2751 std::shared_ptr<InputDevice> device =
2752 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
2753 identifier);
2754 mReader->pushNextDevice(device);
2755 mFakeEventHub->addDevice(eventHubId, name, classes);
2756 mReader->loopOnce();
2757 return device;
2758 }
2759
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002760 template <class T, typename... Args>
2761 T& addMapperAndConfigure(Args... args) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002762 T& mapper = mDevice->addMapper<T>(EVENTHUB_ID, args...);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002763 configureDevice(0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002764 mDevice->reset(ARBITRARY_TIME);
Chris Ye42b06822020-08-07 11:39:33 -07002765 mapper.reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002766 return mapper;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002767 }
2768
2769 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002770 int32_t orientation, const std::string& uniqueId,
2771 std::optional<uint8_t> physicalPort, ViewportType viewportType) {
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002772 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, true /*isActive*/,
2773 uniqueId, physicalPort, viewportType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07002774 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2775 }
2776
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002777 void clearViewports() {
2778 mFakePolicy->clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002779 }
2780
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002781 void process(InputMapper& mapper, nsecs_t when, nsecs_t readTime, int32_t type, int32_t code,
2782 int32_t value) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002783 RawEvent event;
2784 event.when = when;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002785 event.readTime = readTime;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002786 event.deviceId = mapper.getDeviceContext().getEventHubId();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002787 event.type = type;
2788 event.code = code;
2789 event.value = value;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002790 mapper.process(&event);
arthurhungdcef2dc2020-08-11 14:47:50 +08002791 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002792 }
2793
2794 static void assertMotionRange(const InputDeviceInfo& info,
2795 int32_t axis, uint32_t source, float min, float max, float flat, float fuzz) {
2796 const InputDeviceInfo::MotionRange* range = info.getMotionRange(axis, source);
Yi Kong9b14ac62018-07-17 13:48:38 -07002797 ASSERT_TRUE(range != nullptr) << "Axis: " << axis << " Source: " << source;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002798 ASSERT_EQ(axis, range->axis) << "Axis: " << axis << " Source: " << source;
2799 ASSERT_EQ(source, range->source) << "Axis: " << axis << " Source: " << source;
2800 ASSERT_NEAR(min, range->min, EPSILON) << "Axis: " << axis << " Source: " << source;
2801 ASSERT_NEAR(max, range->max, EPSILON) << "Axis: " << axis << " Source: " << source;
2802 ASSERT_NEAR(flat, range->flat, EPSILON) << "Axis: " << axis << " Source: " << source;
2803 ASSERT_NEAR(fuzz, range->fuzz, EPSILON) << "Axis: " << axis << " Source: " << source;
2804 }
2805
Prabir Pradhanf5334b82021-05-13 14:00:39 -07002806 static void assertPointerCoords(const PointerCoords& coords, float x, float y, float pressure,
2807 float size, float touchMajor, float touchMinor, float toolMajor,
2808 float toolMinor, float orientation, float distance,
2809 float scaledAxisEpsilon = 1.f) {
2810 ASSERT_NEAR(x, coords.getAxisValue(AMOTION_EVENT_AXIS_X), scaledAxisEpsilon);
2811 ASSERT_NEAR(y, coords.getAxisValue(AMOTION_EVENT_AXIS_Y), scaledAxisEpsilon);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002812 ASSERT_NEAR(pressure, coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), EPSILON);
2813 ASSERT_NEAR(size, coords.getAxisValue(AMOTION_EVENT_AXIS_SIZE), EPSILON);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07002814 ASSERT_NEAR(touchMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR),
2815 scaledAxisEpsilon);
2816 ASSERT_NEAR(touchMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR),
2817 scaledAxisEpsilon);
2818 ASSERT_NEAR(toolMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR),
2819 scaledAxisEpsilon);
2820 ASSERT_NEAR(toolMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR),
2821 scaledAxisEpsilon);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002822 ASSERT_NEAR(orientation, coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION), EPSILON);
2823 ASSERT_NEAR(distance, coords.getAxisValue(AMOTION_EVENT_AXIS_DISTANCE), EPSILON);
2824 }
2825
Michael Wright17db18e2020-06-26 20:51:44 +01002826 static void assertPosition(const FakePointerController& controller, float x, float y) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002827 float actualX, actualY;
Michael Wright17db18e2020-06-26 20:51:44 +01002828 controller.getPosition(&actualX, &actualY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002829 ASSERT_NEAR(x, actualX, 1);
2830 ASSERT_NEAR(y, actualY, 1);
2831 }
2832};
2833
2834const char* InputMapperTest::DEVICE_NAME = "device";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002835const char* InputMapperTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002836const int32_t InputMapperTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002837const int32_t InputMapperTest::DEVICE_GENERATION = 2;
2838const int32_t InputMapperTest::DEVICE_CONTROLLER_NUMBER = 0;
Chris Ye1b0c7342020-07-28 21:57:03 -07002839const Flags<InputDeviceClass> InputMapperTest::DEVICE_CLASSES =
2840 Flags<InputDeviceClass>(0); // not needed for current tests
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002841const int32_t InputMapperTest::EVENTHUB_ID = 1;
Prabir Pradhanc14266f2021-05-12 15:56:24 -07002842const std::optional<bool> InputMapperTest::INITIAL_PER_WINDOW_INPUT_ROTATION_FLAG_VALUE =
2843 sysprop::InputFlingerProperties::per_window_input_rotation();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002844
2845// --- SwitchInputMapperTest ---
2846
2847class SwitchInputMapperTest : public InputMapperTest {
2848protected:
2849};
2850
2851TEST_F(SwitchInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002852 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002853
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002854 ASSERT_EQ(uint32_t(AINPUT_SOURCE_SWITCH), mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002855}
2856
2857TEST_F(SwitchInputMapperTest, GetSwitchState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002858 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002859
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002860 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002861 ASSERT_EQ(1, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002862
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002863 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002864 ASSERT_EQ(0, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002865}
2866
2867TEST_F(SwitchInputMapperTest, Process) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002868 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002869
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002870 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_LID, 1);
2871 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_JACK_PHYSICAL_INSERT, 1);
2872 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_HEADPHONE_INSERT, 0);
2873 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002874
2875 NotifySwitchArgs args;
2876 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySwitchWasCalled(&args));
2877 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
Dan Albert1bd2fc02016-02-02 15:11:57 -08002878 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT), args.switchValues);
2879 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT) | (1 << SW_HEADPHONE_INSERT),
Michael Wrightd02c5b62014-02-10 15:10:22 -08002880 args.switchMask);
2881 ASSERT_EQ(uint32_t(0), args.policyFlags);
2882}
2883
Chris Ye87143712020-11-10 05:05:58 +00002884// --- VibratorInputMapperTest ---
2885class VibratorInputMapperTest : public InputMapperTest {
2886protected:
2887 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::VIBRATOR); }
2888};
2889
2890TEST_F(VibratorInputMapperTest, GetSources) {
2891 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
2892
2893 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mapper.getSources());
2894}
2895
2896TEST_F(VibratorInputMapperTest, GetVibratorIds) {
2897 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
2898
2899 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
2900}
2901
2902TEST_F(VibratorInputMapperTest, Vibrate) {
2903 constexpr uint8_t DEFAULT_AMPLITUDE = 192;
Chris Yefb552902021-02-03 17:18:37 -08002904 constexpr int32_t VIBRATION_TOKEN = 100;
Chris Ye87143712020-11-10 05:05:58 +00002905 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
2906
2907 VibrationElement pattern(2);
2908 VibrationSequence sequence(2);
2909 pattern.duration = std::chrono::milliseconds(200);
2910 pattern.channels = {{0 /* vibratorId */, DEFAULT_AMPLITUDE / 2},
2911 {1 /* vibratorId */, DEFAULT_AMPLITUDE}};
2912 sequence.addElement(pattern);
2913 pattern.duration = std::chrono::milliseconds(500);
2914 pattern.channels = {{0 /* vibratorId */, DEFAULT_AMPLITUDE / 4},
2915 {1 /* vibratorId */, DEFAULT_AMPLITUDE}};
2916 sequence.addElement(pattern);
2917
2918 std::vector<int64_t> timings = {0, 1};
2919 std::vector<uint8_t> amplitudes = {DEFAULT_AMPLITUDE, DEFAULT_AMPLITUDE / 2};
2920
2921 ASSERT_FALSE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08002922 // Start vibrating
2923 mapper.vibrate(sequence, -1 /* repeat */, VIBRATION_TOKEN);
Chris Ye87143712020-11-10 05:05:58 +00002924 ASSERT_TRUE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08002925 // Verify vibrator state listener was notified.
2926 mReader->loopOnce();
2927 NotifyVibratorStateArgs args;
2928 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyVibratorStateWasCalled(&args));
2929 ASSERT_EQ(DEVICE_ID, args.deviceId);
2930 ASSERT_TRUE(args.isOn);
2931 // Stop vibrating
2932 mapper.cancelVibrate(VIBRATION_TOKEN);
2933 ASSERT_FALSE(mapper.isVibrating());
2934 // Verify vibrator state listener was notified.
2935 mReader->loopOnce();
2936 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyVibratorStateWasCalled(&args));
2937 ASSERT_EQ(DEVICE_ID, args.deviceId);
2938 ASSERT_FALSE(args.isOn);
Chris Ye87143712020-11-10 05:05:58 +00002939}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002940
Chris Yef59a2f42020-10-16 12:55:26 -07002941// --- SensorInputMapperTest ---
2942
2943class SensorInputMapperTest : public InputMapperTest {
2944protected:
2945 static const int32_t ACCEL_RAW_MIN;
2946 static const int32_t ACCEL_RAW_MAX;
2947 static const int32_t ACCEL_RAW_FUZZ;
2948 static const int32_t ACCEL_RAW_FLAT;
2949 static const int32_t ACCEL_RAW_RESOLUTION;
2950
2951 static const int32_t GYRO_RAW_MIN;
2952 static const int32_t GYRO_RAW_MAX;
2953 static const int32_t GYRO_RAW_FUZZ;
2954 static const int32_t GYRO_RAW_FLAT;
2955 static const int32_t GYRO_RAW_RESOLUTION;
2956
2957 static const float GRAVITY_MS2_UNIT;
2958 static const float DEGREE_RADIAN_UNIT;
2959
2960 void prepareAccelAxes();
2961 void prepareGyroAxes();
2962 void setAccelProperties();
2963 void setGyroProperties();
2964 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::SENSOR); }
2965};
2966
2967const int32_t SensorInputMapperTest::ACCEL_RAW_MIN = -32768;
2968const int32_t SensorInputMapperTest::ACCEL_RAW_MAX = 32768;
2969const int32_t SensorInputMapperTest::ACCEL_RAW_FUZZ = 16;
2970const int32_t SensorInputMapperTest::ACCEL_RAW_FLAT = 0;
2971const int32_t SensorInputMapperTest::ACCEL_RAW_RESOLUTION = 8192;
2972
2973const int32_t SensorInputMapperTest::GYRO_RAW_MIN = -2097152;
2974const int32_t SensorInputMapperTest::GYRO_RAW_MAX = 2097152;
2975const int32_t SensorInputMapperTest::GYRO_RAW_FUZZ = 16;
2976const int32_t SensorInputMapperTest::GYRO_RAW_FLAT = 0;
2977const int32_t SensorInputMapperTest::GYRO_RAW_RESOLUTION = 1024;
2978
2979const float SensorInputMapperTest::GRAVITY_MS2_UNIT = 9.80665f;
2980const float SensorInputMapperTest::DEGREE_RADIAN_UNIT = 0.0174533f;
2981
2982void SensorInputMapperTest::prepareAccelAxes() {
2983 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
2984 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
2985 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
2986 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
2987 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Z, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
2988 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
2989}
2990
2991void SensorInputMapperTest::prepareGyroAxes() {
2992 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RX, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
2993 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
2994 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RY, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
2995 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
2996 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RZ, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
2997 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
2998}
2999
3000void SensorInputMapperTest::setAccelProperties() {
3001 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 0, InputDeviceSensorType::ACCELEROMETER,
3002 /* sensorDataIndex */ 0);
3003 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 1, InputDeviceSensorType::ACCELEROMETER,
3004 /* sensorDataIndex */ 1);
3005 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 2, InputDeviceSensorType::ACCELEROMETER,
3006 /* sensorDataIndex */ 2);
3007 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
3008 addConfigurationProperty("sensor.accelerometer.reportingMode", "0");
3009 addConfigurationProperty("sensor.accelerometer.maxDelay", "100000");
3010 addConfigurationProperty("sensor.accelerometer.minDelay", "5000");
3011 addConfigurationProperty("sensor.accelerometer.power", "1.5");
3012}
3013
3014void SensorInputMapperTest::setGyroProperties() {
3015 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 3, InputDeviceSensorType::GYROSCOPE,
3016 /* sensorDataIndex */ 0);
3017 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 4, InputDeviceSensorType::GYROSCOPE,
3018 /* sensorDataIndex */ 1);
3019 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 5, InputDeviceSensorType::GYROSCOPE,
3020 /* sensorDataIndex */ 2);
3021 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
3022 addConfigurationProperty("sensor.gyroscope.reportingMode", "0");
3023 addConfigurationProperty("sensor.gyroscope.maxDelay", "100000");
3024 addConfigurationProperty("sensor.gyroscope.minDelay", "5000");
3025 addConfigurationProperty("sensor.gyroscope.power", "0.8");
3026}
3027
3028TEST_F(SensorInputMapperTest, GetSources) {
3029 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3030
3031 ASSERT_EQ(static_cast<uint32_t>(AINPUT_SOURCE_SENSOR), mapper.getSources());
3032}
3033
3034TEST_F(SensorInputMapperTest, ProcessAccelerometerSensor) {
3035 setAccelProperties();
3036 prepareAccelAxes();
3037 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3038
3039 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::ACCELEROMETER,
3040 std::chrono::microseconds(10000),
3041 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08003042 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003043 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, 20000);
3044 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, -20000);
3045 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Z, 40000);
3046 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
3047 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07003048
3049 NotifySensorArgs args;
3050 std::vector<float> values = {20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
3051 -20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
3052 40000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT};
3053
3054 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
3055 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
3056 ASSERT_EQ(args.deviceId, DEVICE_ID);
3057 ASSERT_EQ(args.sensorType, InputDeviceSensorType::ACCELEROMETER);
3058 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
3059 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
3060 ASSERT_EQ(args.values, values);
3061 mapper.flushSensor(InputDeviceSensorType::ACCELEROMETER);
3062}
3063
3064TEST_F(SensorInputMapperTest, ProcessGyroscopeSensor) {
3065 setGyroProperties();
3066 prepareGyroAxes();
3067 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3068
3069 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::GYROSCOPE,
3070 std::chrono::microseconds(10000),
3071 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08003072 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003073 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RX, 20000);
3074 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RY, -20000);
3075 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RZ, 40000);
3076 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
3077 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07003078
3079 NotifySensorArgs args;
3080 std::vector<float> values = {20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
3081 -20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
3082 40000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT};
3083
3084 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
3085 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
3086 ASSERT_EQ(args.deviceId, DEVICE_ID);
3087 ASSERT_EQ(args.sensorType, InputDeviceSensorType::GYROSCOPE);
3088 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
3089 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
3090 ASSERT_EQ(args.values, values);
3091 mapper.flushSensor(InputDeviceSensorType::GYROSCOPE);
3092}
3093
Michael Wrightd02c5b62014-02-10 15:10:22 -08003094// --- KeyboardInputMapperTest ---
3095
3096class KeyboardInputMapperTest : public InputMapperTest {
3097protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003098 const std::string UNIQUE_ID = "local:0";
3099
3100 void prepareDisplay(int32_t orientation);
3101
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003102 void testDPadKeyRotation(KeyboardInputMapper& mapper, int32_t originalScanCode,
Arthur Hung2c9a3342019-07-23 14:18:59 +08003103 int32_t originalKeyCode, int32_t rotatedKeyCode,
3104 int32_t displayId = ADISPLAY_ID_NONE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003105};
3106
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003107/* Similar to setDisplayInfoAndReconfigure, but pre-populates all parameters except for the
3108 * orientation.
3109 */
3110void KeyboardInputMapperTest::prepareDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003111 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
3112 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003113}
3114
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003115void KeyboardInputMapperTest::testDPadKeyRotation(KeyboardInputMapper& mapper,
Arthur Hung2c9a3342019-07-23 14:18:59 +08003116 int32_t originalScanCode, int32_t originalKeyCode,
3117 int32_t rotatedKeyCode, int32_t displayId) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003118 NotifyKeyArgs args;
3119
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003120 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003121 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3122 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3123 ASSERT_EQ(originalScanCode, args.scanCode);
3124 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003125 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003126
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003127 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003128 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3129 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3130 ASSERT_EQ(originalScanCode, args.scanCode);
3131 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003132 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003133}
3134
Michael Wrightd02c5b62014-02-10 15:10:22 -08003135TEST_F(KeyboardInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003136 KeyboardInputMapper& mapper =
3137 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3138 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003139
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003140 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003141}
3142
3143TEST_F(KeyboardInputMapperTest, Process_SimpleKeyPress) {
3144 const int32_t USAGE_A = 0x070004;
3145 const int32_t USAGE_UNKNOWN = 0x07ffff;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003146 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3147 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
Chris Yea52ade12020-08-27 16:49:20 -07003148 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, POLICY_FLAG_WAKE);
3149 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, POLICY_FLAG_WAKE);
3150 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003151
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003152 KeyboardInputMapper& mapper =
3153 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3154 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
arthurhungc903df12020-08-11 15:08:42 +08003155 // Initial metastate to AMETA_NONE.
3156 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
3157 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003158
3159 // Key down by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003160 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003161 NotifyKeyArgs args;
3162 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3163 ASSERT_EQ(DEVICE_ID, args.deviceId);
3164 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3165 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3166 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3167 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3168 ASSERT_EQ(KEY_HOME, args.scanCode);
3169 ASSERT_EQ(AMETA_NONE, args.metaState);
3170 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3171 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3172 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3173
3174 // Key up by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003175 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003176 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3177 ASSERT_EQ(DEVICE_ID, args.deviceId);
3178 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3179 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3180 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3181 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3182 ASSERT_EQ(KEY_HOME, args.scanCode);
3183 ASSERT_EQ(AMETA_NONE, args.metaState);
3184 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3185 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3186 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3187
3188 // Key down by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003189 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3190 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, 0, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003191 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3192 ASSERT_EQ(DEVICE_ID, args.deviceId);
3193 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3194 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3195 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3196 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3197 ASSERT_EQ(0, args.scanCode);
3198 ASSERT_EQ(AMETA_NONE, args.metaState);
3199 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3200 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3201 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3202
3203 // Key up by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003204 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3205 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003206 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3207 ASSERT_EQ(DEVICE_ID, args.deviceId);
3208 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3209 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3210 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3211 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3212 ASSERT_EQ(0, args.scanCode);
3213 ASSERT_EQ(AMETA_NONE, args.metaState);
3214 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3215 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3216 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3217
3218 // Key down with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003219 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3220 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UNKNOWN, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003221 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3222 ASSERT_EQ(DEVICE_ID, args.deviceId);
3223 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3224 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3225 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3226 ASSERT_EQ(0, args.keyCode);
3227 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3228 ASSERT_EQ(AMETA_NONE, args.metaState);
3229 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3230 ASSERT_EQ(0U, args.policyFlags);
3231 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3232
3233 // Key up with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003234 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3235 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003236 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3237 ASSERT_EQ(DEVICE_ID, args.deviceId);
3238 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3239 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3240 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3241 ASSERT_EQ(0, args.keyCode);
3242 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3243 ASSERT_EQ(AMETA_NONE, args.metaState);
3244 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3245 ASSERT_EQ(0U, args.policyFlags);
3246 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3247}
3248
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003249/**
3250 * Ensure that the readTime is set to the time when the EV_KEY is received.
3251 */
3252TEST_F(KeyboardInputMapperTest, Process_SendsReadTime) {
3253 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3254
3255 KeyboardInputMapper& mapper =
3256 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3257 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3258 NotifyKeyArgs args;
3259
3260 // Key down
3261 process(mapper, ARBITRARY_TIME, 12 /*readTime*/, EV_KEY, KEY_HOME, 1);
3262 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3263 ASSERT_EQ(12, args.readTime);
3264
3265 // Key up
3266 process(mapper, ARBITRARY_TIME, 15 /*readTime*/, EV_KEY, KEY_HOME, 1);
3267 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3268 ASSERT_EQ(15, args.readTime);
3269}
3270
Michael Wrightd02c5b62014-02-10 15:10:22 -08003271TEST_F(KeyboardInputMapperTest, Process_ShouldUpdateMetaState) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003272 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFTSHIFT, 0, AKEYCODE_SHIFT_LEFT, 0);
3273 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003274 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, 0);
3275 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, 0);
3276 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003277
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003278 KeyboardInputMapper& mapper =
3279 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3280 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003281
arthurhungc903df12020-08-11 15:08:42 +08003282 // Initial metastate to AMETA_NONE.
3283 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
3284 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003285
3286 // Metakey down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003287 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003288 NotifyKeyArgs args;
3289 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3290 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003291 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003292 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003293
3294 // Key down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003295 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_A, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003296 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3297 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003298 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003299
3300 // Key up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003301 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, KEY_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003302 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3303 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003304 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003305
3306 // Metakey up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003307 process(mapper, ARBITRARY_TIME + 3, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003308 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3309 ASSERT_EQ(AMETA_NONE, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003310 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003311 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003312}
3313
3314TEST_F(KeyboardInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003315 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3316 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3317 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3318 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003319
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003320 KeyboardInputMapper& mapper =
3321 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3322 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003323
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003324 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003325 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3326 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
3327 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3328 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
3329 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3330 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
3331 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3332 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
3333}
3334
3335TEST_F(KeyboardInputMapperTest, Process_WhenOrientationAware_ShouldRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003336 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3337 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3338 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3339 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003340
Michael Wrightd02c5b62014-02-10 15:10:22 -08003341 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003342 KeyboardInputMapper& mapper =
3343 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3344 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003345
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003346 prepareDisplay(DISPLAY_ORIENTATION_0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003347 ASSERT_NO_FATAL_FAILURE(
3348 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3349 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3350 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3351 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3352 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3353 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3354 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003355
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003356 clearViewports();
3357 prepareDisplay(DISPLAY_ORIENTATION_90);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003358 ASSERT_NO_FATAL_FAILURE(
3359 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3360 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3361 AKEYCODE_DPAD_UP, DISPLAY_ID));
3362 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3363 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3364 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3365 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003366
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003367 clearViewports();
3368 prepareDisplay(DISPLAY_ORIENTATION_180);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003369 ASSERT_NO_FATAL_FAILURE(
3370 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3371 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3372 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3373 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3374 AKEYCODE_DPAD_UP, DISPLAY_ID));
3375 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3376 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003377
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003378 clearViewports();
3379 prepareDisplay(DISPLAY_ORIENTATION_270);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003380 ASSERT_NO_FATAL_FAILURE(
3381 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3382 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3383 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3384 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3385 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3386 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3387 AKEYCODE_DPAD_UP, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003388
3389 // Special case: if orientation changes while key is down, we still emit the same keycode
3390 // in the key up as we did in the key down.
3391 NotifyKeyArgs args;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003392 clearViewports();
3393 prepareDisplay(DISPLAY_ORIENTATION_270);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003394 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003395 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3396 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3397 ASSERT_EQ(KEY_UP, args.scanCode);
3398 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3399
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003400 clearViewports();
3401 prepareDisplay(DISPLAY_ORIENTATION_180);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003402 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003403 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3404 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3405 ASSERT_EQ(KEY_UP, args.scanCode);
3406 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3407}
3408
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003409TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_NotOrientationAware) {
3410 // If the keyboard is not orientation aware,
3411 // key events should not be associated with a specific display id
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003412 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003413
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003414 KeyboardInputMapper& mapper =
3415 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3416 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003417 NotifyKeyArgs args;
3418
3419 // Display id should be ADISPLAY_ID_NONE without any display configuration.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003420 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003421 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003422 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003423 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3424 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3425
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003426 prepareDisplay(DISPLAY_ORIENTATION_0);
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(ADISPLAY_ID_NONE, args.displayId);
3432}
3433
3434TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_OrientationAware) {
3435 // If the keyboard is orientation aware,
3436 // key events should be associated with the internal viewport
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003437 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003438
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003439 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003440 KeyboardInputMapper& mapper =
3441 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3442 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003443 NotifyKeyArgs args;
3444
3445 // Display id should be ADISPLAY_ID_NONE without any display configuration.
3446 // ^--- already checked by the previous test
3447
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003448 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003449 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003450 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003451 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003452 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003453 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3454 ASSERT_EQ(DISPLAY_ID, args.displayId);
3455
3456 constexpr int32_t newDisplayId = 2;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003457 clearViewports();
3458 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003459 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003460 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003461 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003462 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003463 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3464 ASSERT_EQ(newDisplayId, args.displayId);
3465}
3466
Michael Wrightd02c5b62014-02-10 15:10:22 -08003467TEST_F(KeyboardInputMapperTest, GetKeyCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003468 KeyboardInputMapper& mapper =
3469 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3470 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003471
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003472 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003473 ASSERT_EQ(1, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003474
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003475 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003476 ASSERT_EQ(0, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003477}
3478
3479TEST_F(KeyboardInputMapperTest, GetScanCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003480 KeyboardInputMapper& mapper =
3481 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3482 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003483
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003484 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003485 ASSERT_EQ(1, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003486
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003487 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003488 ASSERT_EQ(0, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003489}
3490
3491TEST_F(KeyboardInputMapperTest, MarkSupportedKeyCodes) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003492 KeyboardInputMapper& mapper =
3493 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3494 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003495
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003496 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003497
3498 const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
3499 uint8_t flags[2] = { 0, 0 };
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003500 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, 1, keyCodes, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003501 ASSERT_TRUE(flags[0]);
3502 ASSERT_FALSE(flags[1]);
3503}
3504
3505TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleMetaStateAndLeds) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003506 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3507 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3508 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3509 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3510 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3511 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003512
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003513 KeyboardInputMapper& mapper =
3514 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3515 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Chris Yea52ade12020-08-27 16:49:20 -07003516 // Initialize metastate to AMETA_NUM_LOCK_ON.
arthurhungc903df12020-08-11 15:08:42 +08003517 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
3518 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003519
3520 // Initialization should have turned all of the lights off.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003521 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3522 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3523 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003524
3525 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003526 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3527 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003528 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3529 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3530 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003531 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003532
3533 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003534 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3535 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003536 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3537 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3538 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003539 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003540
3541 // Toggle caps lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003542 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3543 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003544 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3545 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3546 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003547 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003548
3549 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003550 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3551 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003552 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3553 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3554 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003555 ASSERT_EQ(AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003556
3557 // Toggle num lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003558 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3559 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003560 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3561 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3562 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003563 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003564
3565 // Toggle scroll lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003566 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3567 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003568 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3569 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3570 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003571 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003572}
3573
Chris Yea52ade12020-08-27 16:49:20 -07003574TEST_F(KeyboardInputMapperTest, NoMetaStateWhenMetaKeysNotPresent) {
3575 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_BUTTON_A, 0);
3576 mFakeEventHub->addKey(EVENTHUB_ID, BTN_B, 0, AKEYCODE_BUTTON_B, 0);
3577 mFakeEventHub->addKey(EVENTHUB_ID, BTN_X, 0, AKEYCODE_BUTTON_X, 0);
3578 mFakeEventHub->addKey(EVENTHUB_ID, BTN_Y, 0, AKEYCODE_BUTTON_Y, 0);
3579
3580 KeyboardInputMapper& mapper =
3581 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3582 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
3583
3584 // Initial metastate should be AMETA_NONE as no meta keys added.
3585 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3586 // Meta state should be AMETA_NONE after reset
3587 mapper.reset(ARBITRARY_TIME);
3588 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3589 // Meta state should be AMETA_NONE with update, as device doesn't have the keys.
3590 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
3591 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3592
3593 NotifyKeyArgs args;
3594 // Press button "A"
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003595 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_A, 1);
Chris Yea52ade12020-08-27 16:49:20 -07003596 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3597 ASSERT_EQ(AMETA_NONE, args.metaState);
3598 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3599 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3600 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3601
3602 // Button up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003603 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003604 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3605 ASSERT_EQ(AMETA_NONE, args.metaState);
3606 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3607 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3608 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3609}
3610
Arthur Hung2c9a3342019-07-23 14:18:59 +08003611TEST_F(KeyboardInputMapperTest, Configure_AssignsDisplayPort) {
3612 // keyboard 1.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003613 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3614 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3615 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3616 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003617
3618 // keyboard 2.
3619 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08003620 const std::string DEVICE_NAME2 = "KEYBOARD2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08003621 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003622 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08003623 std::shared_ptr<InputDevice> device2 =
3624 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
3625 Flags<InputDeviceClass>(0));
3626
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003627 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3628 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3629 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3630 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003631
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003632 KeyboardInputMapper& mapper =
3633 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3634 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003635
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003636 KeyboardInputMapper& mapper2 =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003637 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003638 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003639 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
3640 device2->reset(ARBITRARY_TIME);
3641
3642 // Prepared displays and associated info.
3643 constexpr uint8_t hdmi1 = 0;
3644 constexpr uint8_t hdmi2 = 1;
3645 const std::string SECONDARY_UNIQUE_ID = "local:1";
3646
3647 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
3648 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
3649
3650 // No associated display viewport found, should disable the device.
3651 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3652 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3653 ASSERT_FALSE(device2->isEnabled());
3654
3655 // Prepare second display.
3656 constexpr int32_t newDisplayId = 2;
3657 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003658 UNIQUE_ID, hdmi1, ViewportType::INTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003659 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003660 SECONDARY_UNIQUE_ID, hdmi2, ViewportType::EXTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003661 // Default device will reconfigure above, need additional reconfiguration for another device.
3662 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3663 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3664
3665 // Device should be enabled after the associated display is found.
3666 ASSERT_TRUE(mDevice->isEnabled());
3667 ASSERT_TRUE(device2->isEnabled());
3668
3669 // Test pad key events
3670 ASSERT_NO_FATAL_FAILURE(
3671 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3672 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3673 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3674 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3675 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3676 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3677 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3678
3679 ASSERT_NO_FATAL_FAILURE(
3680 testDPadKeyRotation(mapper2, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, newDisplayId));
3681 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3682 AKEYCODE_DPAD_RIGHT, newDisplayId));
3683 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3684 AKEYCODE_DPAD_DOWN, newDisplayId));
3685 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3686 AKEYCODE_DPAD_LEFT, newDisplayId));
3687}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003688
arthurhungc903df12020-08-11 15:08:42 +08003689TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleAfterReattach) {
3690 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3691 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3692 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3693 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3694 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3695 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3696
3697 KeyboardInputMapper& mapper =
3698 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3699 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3700 // Initial metastate to AMETA_NONE.
3701 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
3702 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
3703
3704 // Initialization should have turned all of the lights off.
3705 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3706 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3707 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3708
3709 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003710 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3711 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003712 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3713 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
3714
3715 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003716 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3717 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003718 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3719 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
3720
3721 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003722 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3723 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003724 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3725 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
3726
3727 mFakeEventHub->removeDevice(EVENTHUB_ID);
3728 mReader->loopOnce();
3729
3730 // keyboard 2 should default toggle keys.
3731 const std::string USB2 = "USB2";
3732 const std::string DEVICE_NAME2 = "KEYBOARD2";
3733 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
3734 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
3735 std::shared_ptr<InputDevice> device2 =
3736 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
3737 Flags<InputDeviceClass>(0));
3738 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3739 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
3740 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3741 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3742 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3743 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3744
arthurhung6fe95782020-10-05 22:41:16 +08003745 KeyboardInputMapper& mapper2 =
3746 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
3747 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
arthurhungc903df12020-08-11 15:08:42 +08003748 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
3749 device2->reset(ARBITRARY_TIME);
3750
3751 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_CAPSL));
3752 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_NUML));
3753 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_SCROLLL));
arthurhung6fe95782020-10-05 22:41:16 +08003754 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON,
3755 mapper2.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08003756}
3757
Arthur Hungcb40a002021-08-03 14:31:01 +00003758TEST_F(KeyboardInputMapperTest, Process_toggleCapsLockState) {
3759 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3760 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3761 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3762
3763 // Suppose we have two mappers. (DPAD + KEYBOARD)
3764 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_DPAD,
3765 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
3766 KeyboardInputMapper& mapper =
3767 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3768 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3769 // Initialize metastate to AMETA_NUM_LOCK_ON.
3770 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
3771 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
3772
3773 mReader->toggleCapsLockState(DEVICE_ID);
3774 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
3775}
3776
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003777// --- KeyboardInputMapperTest_ExternalDevice ---
3778
3779class KeyboardInputMapperTest_ExternalDevice : public InputMapperTest {
3780protected:
Chris Yea52ade12020-08-27 16:49:20 -07003781 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003782};
3783
3784TEST_F(KeyboardInputMapperTest_ExternalDevice, WakeBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07003785 // For external devices, non-media keys will trigger wake on key down. Media keys need to be
3786 // marked as WAKE in the keylayout file to trigger wake.
Powei Fengd041c5d2019-05-03 17:11:33 -07003787
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003788 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, 0);
3789 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, 0);
3790 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAYPAUSE, 0, AKEYCODE_MEDIA_PLAY_PAUSE,
3791 POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07003792
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003793 KeyboardInputMapper& mapper =
3794 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3795 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07003796
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003797 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003798 NotifyKeyArgs args;
3799 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 + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
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, READ_TIME, EV_KEY, KEY_PLAY, 1);
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 + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003811 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3812 ASSERT_EQ(uint32_t(0), args.policyFlags);
3813
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003814 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003815 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3816 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3817
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003818 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003819 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3820 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3821}
3822
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003823TEST_F(KeyboardInputMapperTest_ExternalDevice, DoNotWakeByDefaultBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07003824 // Tv Remote key's wake behavior is prescribed by the keylayout file.
Powei Fengd041c5d2019-05-03 17:11:33 -07003825
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003826 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3827 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3828 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07003829
Powei Fengd041c5d2019-05-03 17:11:33 -07003830 addConfigurationProperty("keyboard.doNotWakeByDefault", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003831 KeyboardInputMapper& mapper =
3832 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3833 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07003834
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003835 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003836 NotifyKeyArgs args;
3837 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3838 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3839
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003840 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003841 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3842 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3843
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003844 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_DOWN, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003845 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3846 ASSERT_EQ(uint32_t(0), args.policyFlags);
3847
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003848 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_DOWN, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003849 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3850 ASSERT_EQ(uint32_t(0), args.policyFlags);
3851
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003852 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003853 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3854 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3855
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003856 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003857 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3858 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3859}
3860
Michael Wrightd02c5b62014-02-10 15:10:22 -08003861// --- CursorInputMapperTest ---
3862
3863class CursorInputMapperTest : public InputMapperTest {
3864protected:
3865 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD;
3866
Michael Wright17db18e2020-06-26 20:51:44 +01003867 std::shared_ptr<FakePointerController> mFakePointerController;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003868
Chris Yea52ade12020-08-27 16:49:20 -07003869 void SetUp() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003870 InputMapperTest::SetUp();
3871
Michael Wright17db18e2020-06-26 20:51:44 +01003872 mFakePointerController = std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00003873 mFakePolicy->setPointerController(mFakePointerController);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003874 }
3875
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003876 void testMotionRotation(CursorInputMapper& mapper, int32_t originalX, int32_t originalY,
3877 int32_t rotatedX, int32_t rotatedY);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003878
3879 void prepareDisplay(int32_t orientation) {
3880 const std::string uniqueId = "local:0";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003881 const ViewportType viewportType = ViewportType::INTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003882 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
3883 orientation, uniqueId, NO_PORT, viewportType);
3884 }
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003885
3886 static void assertCursorPointerCoords(const PointerCoords& coords, float x, float y,
3887 float pressure) {
3888 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(coords, x, y, pressure, 0.0f, 0.0f, 0.0f, 0.0f,
3889 0.0f, 0.0f, 0.0f, EPSILON));
3890 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08003891};
3892
3893const int32_t CursorInputMapperTest::TRACKBALL_MOVEMENT_THRESHOLD = 6;
3894
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003895void CursorInputMapperTest::testMotionRotation(CursorInputMapper& mapper, int32_t originalX,
3896 int32_t originalY, int32_t rotatedX,
3897 int32_t rotatedY) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003898 NotifyMotionArgs args;
3899
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003900 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, originalX);
3901 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, originalY);
3902 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003903 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3904 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003905 ASSERT_NO_FATAL_FAILURE(
3906 assertCursorPointerCoords(args.pointerCoords[0],
3907 float(rotatedX) / TRACKBALL_MOVEMENT_THRESHOLD,
3908 float(rotatedY) / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003909}
3910
3911TEST_F(CursorInputMapperTest, WhenModeIsPointer_GetSources_ReturnsMouse) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003912 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003913 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003914
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003915 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003916}
3917
3918TEST_F(CursorInputMapperTest, WhenModeIsNavigation_GetSources_ReturnsTrackball) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003919 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003920 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003921
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003922 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003923}
3924
3925TEST_F(CursorInputMapperTest, WhenModeIsPointer_PopulateDeviceInfo_ReturnsRangeFromPointerController) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003926 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003927 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003928
3929 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003930 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003931
3932 // Initially there may not be a valid motion range.
Yi Kong9b14ac62018-07-17 13:48:38 -07003933 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE));
3934 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003935 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3936 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE, 0.0f, 1.0f, 0.0f, 0.0f));
3937
3938 // When the bounds are set, then there should be a valid motion range.
3939 mFakePointerController->setBounds(1, 2, 800 - 1, 480 - 1);
3940
3941 InputDeviceInfo info2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003942 mapper.populateDeviceInfo(&info2);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003943
3944 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3945 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE,
3946 1, 800 - 1, 0.0f, 0.0f));
3947 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3948 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE,
3949 2, 480 - 1, 0.0f, 0.0f));
3950 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3951 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE,
3952 0.0f, 1.0f, 0.0f, 0.0f));
3953}
3954
3955TEST_F(CursorInputMapperTest, WhenModeIsNavigation_PopulateDeviceInfo_ReturnsScaledRange) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003956 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003957 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003958
3959 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003960 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003961
3962 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3963 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_TRACKBALL,
3964 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
3965 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3966 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_TRACKBALL,
3967 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
3968 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3969 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TRACKBALL,
3970 0.0f, 1.0f, 0.0f, 0.0f));
3971}
3972
3973TEST_F(CursorInputMapperTest, Process_ShouldSetAllFieldsAndIncludeGlobalMetaState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003974 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003975 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003976
arthurhungdcef2dc2020-08-11 14:47:50 +08003977 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003978
3979 NotifyMotionArgs args;
3980
3981 // Button press.
3982 // Mostly testing non x/y behavior here so we don't need to check again elsewhere.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003983 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
3984 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003985 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3986 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3987 ASSERT_EQ(DEVICE_ID, args.deviceId);
3988 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3989 ASSERT_EQ(uint32_t(0), args.policyFlags);
3990 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3991 ASSERT_EQ(0, args.flags);
3992 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3993 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
3994 ASSERT_EQ(0, args.edgeFlags);
3995 ASSERT_EQ(uint32_t(1), args.pointerCount);
3996 ASSERT_EQ(0, args.pointerProperties[0].id);
3997 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003998 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003999 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4000 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4001 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4002
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004003 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4004 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4005 ASSERT_EQ(DEVICE_ID, args.deviceId);
4006 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4007 ASSERT_EQ(uint32_t(0), args.policyFlags);
4008 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
4009 ASSERT_EQ(0, args.flags);
4010 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4011 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
4012 ASSERT_EQ(0, args.edgeFlags);
4013 ASSERT_EQ(uint32_t(1), args.pointerCount);
4014 ASSERT_EQ(0, args.pointerProperties[0].id);
4015 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004016 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004017 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4018 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4019 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4020
Michael Wrightd02c5b62014-02-10 15:10:22 -08004021 // Button release. Should have same down time.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004022 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4023 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004024 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4025 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
4026 ASSERT_EQ(DEVICE_ID, args.deviceId);
4027 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4028 ASSERT_EQ(uint32_t(0), args.policyFlags);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004029 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
4030 ASSERT_EQ(0, args.flags);
4031 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4032 ASSERT_EQ(0, args.buttonState);
4033 ASSERT_EQ(0, args.edgeFlags);
4034 ASSERT_EQ(uint32_t(1), args.pointerCount);
4035 ASSERT_EQ(0, args.pointerProperties[0].id);
4036 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004037 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004038 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4039 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4040 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4041
4042 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4043 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
4044 ASSERT_EQ(DEVICE_ID, args.deviceId);
4045 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4046 ASSERT_EQ(uint32_t(0), args.policyFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004047 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
4048 ASSERT_EQ(0, args.flags);
4049 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4050 ASSERT_EQ(0, args.buttonState);
4051 ASSERT_EQ(0, args.edgeFlags);
4052 ASSERT_EQ(uint32_t(1), args.pointerCount);
4053 ASSERT_EQ(0, args.pointerProperties[0].id);
4054 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004055 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004056 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4057 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4058 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4059}
4060
4061TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentXYUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004062 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004063 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004064
4065 NotifyMotionArgs args;
4066
4067 // Motion in X but not Y.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004068 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4069 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004070 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4071 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004072 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4073 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f,
4074 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004075
4076 // Motion in Y but not X.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004077 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
4078 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004079 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4080 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004081 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f,
4082 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004083}
4084
4085TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004086 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004087 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004088
4089 NotifyMotionArgs args;
4090
4091 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004092 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4093 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004094 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4095 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004096 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004097
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004098 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4099 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004100 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004101
Michael Wrightd02c5b62014-02-10 15:10:22 -08004102 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004103 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4104 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004105 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004106 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004107 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004108
4109 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004110 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004111 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004112}
4113
4114TEST_F(CursorInputMapperTest, Process_ShouldHandleCombinedXYAndButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004115 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004116 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004117
4118 NotifyMotionArgs args;
4119
4120 // Combined X, Y and Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004121 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4122 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
4123 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4124 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004125 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4126 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004127 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4128 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4129 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004130
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004131 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4132 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004133 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4134 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4135 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004136
Michael Wrightd02c5b62014-02-10 15:10:22 -08004137 // Move X, Y a bit while pressed.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004138 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 2);
4139 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 1);
4140 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004141 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4142 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004143 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4144 2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4145 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004146
4147 // Release Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004148 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4149 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004150 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004151 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004152 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004153
4154 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004155 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004156 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004157}
4158
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004159TEST_F(CursorInputMapperTest, Process_WhenOrientationAware_ShouldNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004160 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004161 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
4162 // need to be rotated.
4163 addConfigurationProperty("cursor.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004164 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004165
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004166 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004167 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4168 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4169 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4170 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4171 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4172 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4173 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4174 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4175}
4176
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004177TEST_F(CursorInputMapperTest, Process_WhenNotOrientationAware_ShouldRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004178 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004179 // Since InputReader works in the un-rotated coordinate space, only devices that are not
4180 // orientation-aware are affected by display rotation.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004181 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004182
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004183 prepareDisplay(DISPLAY_ORIENTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004184 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4185 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4186 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4187 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4188 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4189 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4190 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4191 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4192
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004193 prepareDisplay(DISPLAY_ORIENTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004194 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, -1, 0));
4195 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, 1));
4196 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, 1));
4197 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, 1));
4198 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 1, 0));
4199 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, -1));
4200 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, -1));
4201 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, -1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004202
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004203 prepareDisplay(DISPLAY_ORIENTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004204 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, -1));
4205 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, -1));
4206 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, -1, 0));
4207 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, 1));
4208 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, 1));
4209 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, 1));
4210 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 1, 0));
4211 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, -1));
4212
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004213 prepareDisplay(DISPLAY_ORIENTATION_270);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004214 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 1, 0));
4215 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, -1));
4216 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, -1));
4217 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, -1));
4218 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, -1, 0));
4219 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, 1));
4220 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, 1));
4221 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, 1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004222}
4223
4224TEST_F(CursorInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004225 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004226 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004227
4228 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4229 mFakePointerController->setPosition(100, 200);
4230 mFakePointerController->setButtonState(0);
4231
4232 NotifyMotionArgs motionArgs;
4233 NotifyKeyArgs keyArgs;
4234
4235 // press BTN_LEFT, release BTN_LEFT
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004236 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 1);
4237 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004238 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4239 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4240 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4241 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004242 ASSERT_NO_FATAL_FAILURE(
4243 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004244
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004245 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4246 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4247 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4248 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004249 ASSERT_NO_FATAL_FAILURE(
4250 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004251
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004252 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 0);
4253 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004254 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004255 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004256 ASSERT_EQ(0, motionArgs.buttonState);
4257 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004258 ASSERT_NO_FATAL_FAILURE(
4259 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004260
4261 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004262 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004263 ASSERT_EQ(0, motionArgs.buttonState);
4264 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004265 ASSERT_NO_FATAL_FAILURE(
4266 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004267
4268 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004269 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004270 ASSERT_EQ(0, motionArgs.buttonState);
4271 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004272 ASSERT_NO_FATAL_FAILURE(
4273 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004274
4275 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004276 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 1);
4277 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 1);
4278 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004279 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4280 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4281 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4282 motionArgs.buttonState);
4283 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4284 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004285 ASSERT_NO_FATAL_FAILURE(
4286 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004287
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004288 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4289 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4290 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4291 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4292 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004293 ASSERT_NO_FATAL_FAILURE(
4294 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004295
4296 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4297 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4298 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4299 motionArgs.buttonState);
4300 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4301 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004302 ASSERT_NO_FATAL_FAILURE(
4303 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004304
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004305 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 0);
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->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004308 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004309 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4310 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004311 ASSERT_NO_FATAL_FAILURE(
4312 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004313
4314 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004315 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004316 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4317 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004318 ASSERT_NO_FATAL_FAILURE(
4319 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004320
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004321 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4322 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004323 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004324 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
4325 ASSERT_EQ(0, motionArgs.buttonState);
4326 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004327 ASSERT_NO_FATAL_FAILURE(
4328 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004329 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4330 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004331
4332 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004333 ASSERT_EQ(0, motionArgs.buttonState);
4334 ASSERT_EQ(0, mFakePointerController->getButtonState());
4335 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004336 ASSERT_NO_FATAL_FAILURE(
4337 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004338
Michael Wrightd02c5b62014-02-10 15:10:22 -08004339 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4340 ASSERT_EQ(0, motionArgs.buttonState);
4341 ASSERT_EQ(0, mFakePointerController->getButtonState());
4342 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004343 ASSERT_NO_FATAL_FAILURE(
4344 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004345
4346 // press BTN_BACK, release BTN_BACK
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004347 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 1);
4348 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004349 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4350 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4351 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004352
Michael Wrightd02c5b62014-02-10 15:10:22 -08004353 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004354 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004355 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4356 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004357 ASSERT_NO_FATAL_FAILURE(
4358 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004359
4360 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4361 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4362 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4363 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004364 ASSERT_NO_FATAL_FAILURE(
4365 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004366
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004367 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 0);
4368 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004369 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004370 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004371 ASSERT_EQ(0, motionArgs.buttonState);
4372 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004373 ASSERT_NO_FATAL_FAILURE(
4374 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004375
4376 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004377 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004378 ASSERT_EQ(0, motionArgs.buttonState);
4379 ASSERT_EQ(0, mFakePointerController->getButtonState());
4380
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004381 ASSERT_NO_FATAL_FAILURE(
4382 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004383 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4384 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4385 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4386
4387 // press BTN_SIDE, release BTN_SIDE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004388 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 1);
4389 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004390 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4391 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4392 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004393
Michael Wrightd02c5b62014-02-10 15:10:22 -08004394 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004395 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004396 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4397 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004398 ASSERT_NO_FATAL_FAILURE(
4399 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004400
4401 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4402 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4403 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4404 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004405 ASSERT_NO_FATAL_FAILURE(
4406 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004407
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004408 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 0);
4409 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004410 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004411 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004412 ASSERT_EQ(0, motionArgs.buttonState);
4413 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004414 ASSERT_NO_FATAL_FAILURE(
4415 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004416
4417 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4418 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4419 ASSERT_EQ(0, motionArgs.buttonState);
4420 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004421 ASSERT_NO_FATAL_FAILURE(
4422 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004423
Michael Wrightd02c5b62014-02-10 15:10:22 -08004424 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4425 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4426 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4427
4428 // press BTN_FORWARD, release BTN_FORWARD
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004429 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 1);
4430 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004431 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4432 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4433 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004434
Michael Wrightd02c5b62014-02-10 15:10:22 -08004435 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004436 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004437 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4438 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004439 ASSERT_NO_FATAL_FAILURE(
4440 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004441
4442 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4443 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4444 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4445 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004446 ASSERT_NO_FATAL_FAILURE(
4447 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004448
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004449 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 0);
4450 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004451 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004452 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004453 ASSERT_EQ(0, motionArgs.buttonState);
4454 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004455 ASSERT_NO_FATAL_FAILURE(
4456 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004457
4458 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4459 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4460 ASSERT_EQ(0, motionArgs.buttonState);
4461 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004462 ASSERT_NO_FATAL_FAILURE(
4463 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004464
Michael Wrightd02c5b62014-02-10 15:10:22 -08004465 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4466 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4467 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4468
4469 // press BTN_EXTRA, release BTN_EXTRA
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004470 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 1);
4471 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004472 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4473 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4474 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004475
Michael Wrightd02c5b62014-02-10 15:10:22 -08004476 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004477 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004478 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4479 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004480 ASSERT_NO_FATAL_FAILURE(
4481 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004482
4483 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4484 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4485 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4486 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004487 ASSERT_NO_FATAL_FAILURE(
4488 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004489
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004490 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 0);
4491 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004492 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004493 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004494 ASSERT_EQ(0, motionArgs.buttonState);
4495 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004496 ASSERT_NO_FATAL_FAILURE(
4497 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004498
4499 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4500 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4501 ASSERT_EQ(0, motionArgs.buttonState);
4502 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004503 ASSERT_NO_FATAL_FAILURE(
4504 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004505
Michael Wrightd02c5b62014-02-10 15:10:22 -08004506 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4507 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4508 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4509}
4510
4511TEST_F(CursorInputMapperTest, Process_WhenModeIsPointer_ShouldMoveThePointerAround) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004512 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004513 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004514
4515 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4516 mFakePointerController->setPosition(100, 200);
4517 mFakePointerController->setButtonState(0);
4518
4519 NotifyMotionArgs args;
4520
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004521 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4522 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4523 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004524 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004525 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4526 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4527 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4528 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 +01004529 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004530}
4531
4532TEST_F(CursorInputMapperTest, Process_PointerCapture) {
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004533 addConfigurationProperty("cursor.mode", "pointer");
4534 mFakePolicy->setPointerCapture(true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004535 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004536
4537 NotifyDeviceResetArgs resetArgs;
4538 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4539 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4540 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4541
4542 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4543 mFakePointerController->setPosition(100, 200);
4544 mFakePointerController->setButtonState(0);
4545
4546 NotifyMotionArgs args;
4547
4548 // Move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004549 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4550 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4551 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004552 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4553 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4554 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4555 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4556 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 +01004557 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004558
4559 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004560 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4561 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004562 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4563 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4564 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
4565 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4566 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4567 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4568 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4569 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
4570 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4571 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4572
4573 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004574 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4575 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004576 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4577 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4578 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
4579 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4580 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4581 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4582 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4583 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
4584 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4585 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4586
4587 // Another move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004588 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 30);
4589 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 40);
4590 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004591 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4592 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4593 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4594 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4595 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 +01004596 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004597
4598 // Disable pointer capture and check that the device generation got bumped
4599 // and events are generated the usual way.
arthurhungdcef2dc2020-08-11 14:47:50 +08004600 const uint32_t generation = mReader->getContext()->getGeneration();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004601 mFakePolicy->setPointerCapture(false);
4602 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
arthurhungdcef2dc2020-08-11 14:47:50 +08004603 ASSERT_TRUE(mReader->getContext()->getGeneration() != generation);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004604
4605 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4606 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4607 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4608
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004609 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4610 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4611 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004612 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4613 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004614 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4615 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4616 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 +01004617 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004618}
4619
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004620TEST_F(CursorInputMapperTest, Process_ShouldHandleDisplayId) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004621 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004622
Garfield Tan888a6a42020-01-09 11:39:16 -08004623 // Setup for second display.
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004624 constexpr int32_t SECOND_DISPLAY_ID = 1;
Garfield Tan888a6a42020-01-09 11:39:16 -08004625 const std::string SECOND_DISPLAY_UNIQUE_ID = "local:1";
4626 mFakePolicy->addDisplayViewport(SECOND_DISPLAY_ID, 800, 480, DISPLAY_ORIENTATION_0,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00004627 true /*isActive*/, SECOND_DISPLAY_UNIQUE_ID, NO_PORT,
4628 ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08004629 mFakePolicy->setDefaultPointerDisplayId(SECOND_DISPLAY_ID);
4630 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
4631
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004632 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4633 mFakePointerController->setPosition(100, 200);
4634 mFakePointerController->setButtonState(0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004635
4636 NotifyMotionArgs args;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004637 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4638 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4639 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004640 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4641 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4642 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4643 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4644 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 +01004645 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004646 ASSERT_EQ(SECOND_DISPLAY_ID, args.displayId);
4647}
4648
Michael Wrightd02c5b62014-02-10 15:10:22 -08004649// --- TouchInputMapperTest ---
4650
4651class TouchInputMapperTest : public InputMapperTest {
4652protected:
4653 static const int32_t RAW_X_MIN;
4654 static const int32_t RAW_X_MAX;
4655 static const int32_t RAW_Y_MIN;
4656 static const int32_t RAW_Y_MAX;
4657 static const int32_t RAW_TOUCH_MIN;
4658 static const int32_t RAW_TOUCH_MAX;
4659 static const int32_t RAW_TOOL_MIN;
4660 static const int32_t RAW_TOOL_MAX;
4661 static const int32_t RAW_PRESSURE_MIN;
4662 static const int32_t RAW_PRESSURE_MAX;
4663 static const int32_t RAW_ORIENTATION_MIN;
4664 static const int32_t RAW_ORIENTATION_MAX;
4665 static const int32_t RAW_DISTANCE_MIN;
4666 static const int32_t RAW_DISTANCE_MAX;
4667 static const int32_t RAW_TILT_MIN;
4668 static const int32_t RAW_TILT_MAX;
4669 static const int32_t RAW_ID_MIN;
4670 static const int32_t RAW_ID_MAX;
4671 static const int32_t RAW_SLOT_MIN;
4672 static const int32_t RAW_SLOT_MAX;
4673 static const float X_PRECISION;
4674 static const float Y_PRECISION;
Santos Cordonfa5cf462017-04-05 10:37:00 -07004675 static const float X_PRECISION_VIRTUAL;
4676 static const float Y_PRECISION_VIRTUAL;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004677
4678 static const float GEOMETRIC_SCALE;
Jason Gerecke489fda82012-09-07 17:19:40 -07004679 static const TouchAffineTransformation AFFINE_TRANSFORM;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004680
4681 static const VirtualKeyDefinition VIRTUAL_KEYS[2];
4682
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004683 const std::string UNIQUE_ID = "local:0";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004684 const std::string SECONDARY_UNIQUE_ID = "local:1";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004685
Michael Wrightd02c5b62014-02-10 15:10:22 -08004686 enum Axes {
4687 POSITION = 1 << 0,
4688 TOUCH = 1 << 1,
4689 TOOL = 1 << 2,
4690 PRESSURE = 1 << 3,
4691 ORIENTATION = 1 << 4,
4692 MINOR = 1 << 5,
4693 ID = 1 << 6,
4694 DISTANCE = 1 << 7,
4695 TILT = 1 << 8,
4696 SLOT = 1 << 9,
4697 TOOL_TYPE = 1 << 10,
4698 };
4699
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004700 void prepareDisplay(int32_t orientation, std::optional<uint8_t> port = NO_PORT);
4701 void prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port = NO_PORT);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004702 void prepareVirtualDisplay(int32_t orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004703 void prepareVirtualKeys();
Jason Gerecke489fda82012-09-07 17:19:40 -07004704 void prepareLocationCalibration();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004705 int32_t toRawX(float displayX);
4706 int32_t toRawY(float displayY);
Jason Gerecke489fda82012-09-07 17:19:40 -07004707 float toCookedX(float rawX, float rawY);
4708 float toCookedY(float rawX, float rawY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004709 float toDisplayX(int32_t rawX);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004710 float toDisplayX(int32_t rawX, int32_t displayWidth);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004711 float toDisplayY(int32_t rawY);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004712 float toDisplayY(int32_t rawY, int32_t displayHeight);
4713
Michael Wrightd02c5b62014-02-10 15:10:22 -08004714};
4715
4716const int32_t TouchInputMapperTest::RAW_X_MIN = 25;
4717const int32_t TouchInputMapperTest::RAW_X_MAX = 1019;
4718const int32_t TouchInputMapperTest::RAW_Y_MIN = 30;
4719const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009;
4720const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0;
4721const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31;
4722const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0;
4723const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15;
Michael Wrightaa449c92017-12-13 21:21:43 +00004724const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = 0;
4725const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = 255;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004726const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7;
4727const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
4728const int32_t TouchInputMapperTest::RAW_DISTANCE_MIN = 0;
4729const int32_t TouchInputMapperTest::RAW_DISTANCE_MAX = 7;
4730const int32_t TouchInputMapperTest::RAW_TILT_MIN = 0;
4731const int32_t TouchInputMapperTest::RAW_TILT_MAX = 150;
4732const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
4733const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
4734const int32_t TouchInputMapperTest::RAW_SLOT_MIN = 0;
4735const int32_t TouchInputMapperTest::RAW_SLOT_MAX = 9;
4736const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH;
4737const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT;
Santos Cordonfa5cf462017-04-05 10:37:00 -07004738const float TouchInputMapperTest::X_PRECISION_VIRTUAL =
4739 float(RAW_X_MAX - RAW_X_MIN + 1) / VIRTUAL_DISPLAY_WIDTH;
4740const float TouchInputMapperTest::Y_PRECISION_VIRTUAL =
4741 float(RAW_Y_MAX - RAW_Y_MIN + 1) / VIRTUAL_DISPLAY_HEIGHT;
Jason Gerecke489fda82012-09-07 17:19:40 -07004742const TouchAffineTransformation TouchInputMapperTest::AFFINE_TRANSFORM =
4743 TouchAffineTransformation(1, -2, 3, -4, 5, -6);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004744
4745const float TouchInputMapperTest::GEOMETRIC_SCALE =
4746 avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1),
4747 float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1));
4748
4749const VirtualKeyDefinition TouchInputMapperTest::VIRTUAL_KEYS[2] = {
4750 { KEY_HOME, 60, DISPLAY_HEIGHT + 15, 20, 20 },
4751 { KEY_MENU, DISPLAY_HEIGHT - 60, DISPLAY_WIDTH + 15, 20, 20 },
4752};
4753
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004754void TouchInputMapperTest::prepareDisplay(int32_t orientation, std::optional<uint8_t> port) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004755 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
4756 port, ViewportType::INTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004757}
4758
4759void TouchInputMapperTest::prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port) {
4760 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
4761 DISPLAY_ORIENTATION_0, SECONDARY_UNIQUE_ID, port, type);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004762}
4763
Santos Cordonfa5cf462017-04-05 10:37:00 -07004764void TouchInputMapperTest::prepareVirtualDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004765 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH, VIRTUAL_DISPLAY_HEIGHT,
4766 orientation, VIRTUAL_DISPLAY_UNIQUE_ID, NO_PORT,
4767 ViewportType::VIRTUAL);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004768}
4769
Michael Wrightd02c5b62014-02-10 15:10:22 -08004770void TouchInputMapperTest::prepareVirtualKeys() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004771 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[0]);
4772 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[1]);
4773 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4774 mFakeEventHub->addKey(EVENTHUB_ID, KEY_MENU, 0, AKEYCODE_MENU, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004775}
4776
Jason Gerecke489fda82012-09-07 17:19:40 -07004777void TouchInputMapperTest::prepareLocationCalibration() {
4778 mFakePolicy->setTouchAffineTransformation(AFFINE_TRANSFORM);
4779}
4780
Michael Wrightd02c5b62014-02-10 15:10:22 -08004781int32_t TouchInputMapperTest::toRawX(float displayX) {
4782 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN);
4783}
4784
4785int32_t TouchInputMapperTest::toRawY(float displayY) {
4786 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN);
4787}
4788
Jason Gerecke489fda82012-09-07 17:19:40 -07004789float TouchInputMapperTest::toCookedX(float rawX, float rawY) {
4790 AFFINE_TRANSFORM.applyTo(rawX, rawY);
4791 return rawX;
4792}
4793
4794float TouchInputMapperTest::toCookedY(float rawX, float rawY) {
4795 AFFINE_TRANSFORM.applyTo(rawX, rawY);
4796 return rawY;
4797}
4798
Michael Wrightd02c5b62014-02-10 15:10:22 -08004799float TouchInputMapperTest::toDisplayX(int32_t rawX) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004800 return toDisplayX(rawX, DISPLAY_WIDTH);
4801}
4802
4803float TouchInputMapperTest::toDisplayX(int32_t rawX, int32_t displayWidth) {
4804 return float(rawX - RAW_X_MIN) * displayWidth / (RAW_X_MAX - RAW_X_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004805}
4806
4807float TouchInputMapperTest::toDisplayY(int32_t rawY) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004808 return toDisplayY(rawY, DISPLAY_HEIGHT);
4809}
4810
4811float TouchInputMapperTest::toDisplayY(int32_t rawY, int32_t displayHeight) {
4812 return float(rawY - RAW_Y_MIN) * displayHeight / (RAW_Y_MAX - RAW_Y_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004813}
4814
4815
4816// --- SingleTouchInputMapperTest ---
4817
4818class SingleTouchInputMapperTest : public TouchInputMapperTest {
4819protected:
4820 void prepareButtons();
4821 void prepareAxes(int axes);
4822
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004823 void processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
4824 void processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
4825 void processUp(SingleTouchInputMapper& mappery);
4826 void processPressure(SingleTouchInputMapper& mapper, int32_t pressure);
4827 void processToolMajor(SingleTouchInputMapper& mapper, int32_t toolMajor);
4828 void processDistance(SingleTouchInputMapper& mapper, int32_t distance);
4829 void processTilt(SingleTouchInputMapper& mapper, int32_t tiltX, int32_t tiltY);
4830 void processKey(SingleTouchInputMapper& mapper, int32_t code, int32_t value);
4831 void processSync(SingleTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004832};
4833
4834void SingleTouchInputMapperTest::prepareButtons() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004835 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004836}
4837
4838void SingleTouchInputMapperTest::prepareAxes(int axes) {
4839 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004840 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
4841 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004842 }
4843 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004844 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MIN,
4845 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004846 }
4847 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004848 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TOOL_WIDTH, RAW_TOOL_MIN, RAW_TOOL_MAX, 0,
4849 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004850 }
4851 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004852 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_DISTANCE, RAW_DISTANCE_MIN,
4853 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004854 }
4855 if (axes & TILT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004856 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_X, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
4857 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_Y, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004858 }
4859}
4860
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004861void SingleTouchInputMapperTest::processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004862 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
4863 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
4864 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004865}
4866
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004867void SingleTouchInputMapperTest::processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004868 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
4869 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004870}
4871
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004872void SingleTouchInputMapperTest::processUp(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004873 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004874}
4875
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004876void SingleTouchInputMapperTest::processPressure(SingleTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004877 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004878}
4879
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004880void SingleTouchInputMapperTest::processToolMajor(SingleTouchInputMapper& mapper,
4881 int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004882 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TOOL_WIDTH, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004883}
4884
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004885void SingleTouchInputMapperTest::processDistance(SingleTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004886 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004887}
4888
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004889void SingleTouchInputMapperTest::processTilt(SingleTouchInputMapper& mapper, int32_t tiltX,
4890 int32_t tiltY) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004891 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_X, tiltX);
4892 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_Y, tiltY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004893}
4894
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004895void SingleTouchInputMapperTest::processKey(SingleTouchInputMapper& mapper, int32_t code,
4896 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004897 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004898}
4899
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004900void SingleTouchInputMapperTest::processSync(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004901 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004902}
4903
Michael Wrightd02c5b62014-02-10 15:10:22 -08004904TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004905 prepareButtons();
4906 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004907 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004908
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004909 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004910}
4911
4912TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndIsACursor_ReturnsTouchPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004913 mFakeEventHub->addRelativeAxis(EVENTHUB_ID, REL_X);
4914 mFakeEventHub->addRelativeAxis(EVENTHUB_ID, REL_Y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004915 prepareButtons();
4916 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004917 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004918
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004919 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004920}
4921
4922TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchPad_ReturnsTouchPad) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004923 prepareButtons();
4924 prepareAxes(POSITION);
4925 addConfigurationProperty("touch.deviceType", "touchPad");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004926 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004927
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004928 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004929}
4930
4931TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004932 prepareButtons();
4933 prepareAxes(POSITION);
4934 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004935 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004936
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004937 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004938}
4939
4940TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004941 addConfigurationProperty("touch.deviceType", "touchScreen");
4942 prepareDisplay(DISPLAY_ORIENTATION_0);
4943 prepareButtons();
4944 prepareAxes(POSITION);
4945 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004946 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004947
4948 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004949 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004950
4951 // Virtual key is down.
4952 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4953 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4954 processDown(mapper, x, y);
4955 processSync(mapper);
4956 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4957
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004958 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004959
4960 // Virtual key is up.
4961 processUp(mapper);
4962 processSync(mapper);
4963 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4964
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004965 ASSERT_EQ(AKEY_STATE_UP, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004966}
4967
4968TEST_F(SingleTouchInputMapperTest, GetScanCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004969 addConfigurationProperty("touch.deviceType", "touchScreen");
4970 prepareDisplay(DISPLAY_ORIENTATION_0);
4971 prepareButtons();
4972 prepareAxes(POSITION);
4973 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004974 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004975
4976 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004977 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004978
4979 // Virtual key is down.
4980 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4981 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4982 processDown(mapper, x, y);
4983 processSync(mapper);
4984 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4985
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004986 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004987
4988 // Virtual key is up.
4989 processUp(mapper);
4990 processSync(mapper);
4991 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4992
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004993 ASSERT_EQ(AKEY_STATE_UP, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004994}
4995
4996TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004997 addConfigurationProperty("touch.deviceType", "touchScreen");
4998 prepareDisplay(DISPLAY_ORIENTATION_0);
4999 prepareButtons();
5000 prepareAxes(POSITION);
5001 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005002 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005003
5004 const int32_t keys[2] = { AKEYCODE_HOME, AKEYCODE_A };
5005 uint8_t flags[2] = { 0, 0 };
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005006 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, 2, keys, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005007 ASSERT_TRUE(flags[0]);
5008 ASSERT_FALSE(flags[1]);
5009}
5010
5011TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005012 addConfigurationProperty("touch.deviceType", "touchScreen");
5013 prepareDisplay(DISPLAY_ORIENTATION_0);
5014 prepareButtons();
5015 prepareAxes(POSITION);
5016 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005017 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005018
arthurhungdcef2dc2020-08-11 14:47:50 +08005019 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005020
5021 NotifyKeyArgs args;
5022
5023 // Press virtual key.
5024 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5025 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5026 processDown(mapper, x, y);
5027 processSync(mapper);
5028
5029 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
5030 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5031 ASSERT_EQ(DEVICE_ID, args.deviceId);
5032 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
5033 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
5034 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
5035 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5036 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5037 ASSERT_EQ(KEY_HOME, args.scanCode);
5038 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5039 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5040
5041 // Release virtual key.
5042 processUp(mapper);
5043 processSync(mapper);
5044
5045 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
5046 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5047 ASSERT_EQ(DEVICE_ID, args.deviceId);
5048 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
5049 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
5050 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
5051 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5052 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5053 ASSERT_EQ(KEY_HOME, args.scanCode);
5054 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5055 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5056
5057 // Should not have sent any motions.
5058 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5059}
5060
5061TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005062 addConfigurationProperty("touch.deviceType", "touchScreen");
5063 prepareDisplay(DISPLAY_ORIENTATION_0);
5064 prepareButtons();
5065 prepareAxes(POSITION);
5066 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005067 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005068
arthurhungdcef2dc2020-08-11 14:47:50 +08005069 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005070
5071 NotifyKeyArgs keyArgs;
5072
5073 // Press virtual key.
5074 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5075 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5076 processDown(mapper, x, y);
5077 processSync(mapper);
5078
5079 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5080 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5081 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5082 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5083 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5084 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5085 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags);
5086 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5087 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5088 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5089 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5090
5091 // Move out of bounds. This should generate a cancel and a pointer down since we moved
5092 // into the display area.
5093 y -= 100;
5094 processMove(mapper, x, y);
5095 processSync(mapper);
5096
5097 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5098 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5099 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5100 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5101 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5102 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5103 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
5104 | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags);
5105 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5106 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5107 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5108 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5109
5110 NotifyMotionArgs motionArgs;
5111 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5112 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5113 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5114 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5115 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5116 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5117 ASSERT_EQ(0, motionArgs.flags);
5118 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5119 ASSERT_EQ(0, motionArgs.buttonState);
5120 ASSERT_EQ(0, motionArgs.edgeFlags);
5121 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5122 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5123 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5124 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5125 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5126 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5127 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5128 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5129
5130 // Keep moving out of bounds. Should generate a pointer move.
5131 y -= 50;
5132 processMove(mapper, x, y);
5133 processSync(mapper);
5134
5135 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5136 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5137 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5138 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5139 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5140 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5141 ASSERT_EQ(0, motionArgs.flags);
5142 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5143 ASSERT_EQ(0, motionArgs.buttonState);
5144 ASSERT_EQ(0, motionArgs.edgeFlags);
5145 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5146 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5147 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5148 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5149 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5150 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5151 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5152 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5153
5154 // Release out of bounds. Should generate a pointer up.
5155 processUp(mapper);
5156 processSync(mapper);
5157
5158 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5159 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5160 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5161 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5162 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5163 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5164 ASSERT_EQ(0, motionArgs.flags);
5165 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5166 ASSERT_EQ(0, motionArgs.buttonState);
5167 ASSERT_EQ(0, motionArgs.edgeFlags);
5168 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5169 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5170 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5171 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5172 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5173 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5174 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5175 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5176
5177 // Should not have sent any more keys or motions.
5178 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5179 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5180}
5181
5182TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005183 addConfigurationProperty("touch.deviceType", "touchScreen");
5184 prepareDisplay(DISPLAY_ORIENTATION_0);
5185 prepareButtons();
5186 prepareAxes(POSITION);
5187 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005188 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005189
arthurhungdcef2dc2020-08-11 14:47:50 +08005190 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005191
5192 NotifyMotionArgs motionArgs;
5193
5194 // Initially go down out of bounds.
5195 int32_t x = -10;
5196 int32_t y = -10;
5197 processDown(mapper, x, y);
5198 processSync(mapper);
5199
5200 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5201
5202 // Move into the display area. Should generate a pointer down.
5203 x = 50;
5204 y = 75;
5205 processMove(mapper, x, y);
5206 processSync(mapper);
5207
5208 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5209 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5210 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5211 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5212 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5213 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5214 ASSERT_EQ(0, motionArgs.flags);
5215 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5216 ASSERT_EQ(0, motionArgs.buttonState);
5217 ASSERT_EQ(0, motionArgs.edgeFlags);
5218 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5219 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5220 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5221 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5222 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5223 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5224 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5225 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5226
5227 // Release. Should generate a pointer up.
5228 processUp(mapper);
5229 processSync(mapper);
5230
5231 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5232 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5233 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5234 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5235 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5236 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5237 ASSERT_EQ(0, motionArgs.flags);
5238 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5239 ASSERT_EQ(0, motionArgs.buttonState);
5240 ASSERT_EQ(0, motionArgs.edgeFlags);
5241 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5242 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5243 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5244 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5245 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5246 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5247 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5248 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5249
5250 // Should not have sent any more keys or motions.
5251 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5252 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5253}
5254
Santos Cordonfa5cf462017-04-05 10:37:00 -07005255TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture_VirtualDisplay) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005256 addConfigurationProperty("touch.deviceType", "touchScreen");
5257 addConfigurationProperty("touch.displayId", VIRTUAL_DISPLAY_UNIQUE_ID);
5258
5259 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
5260 prepareButtons();
5261 prepareAxes(POSITION);
5262 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005263 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Santos Cordonfa5cf462017-04-05 10:37:00 -07005264
arthurhungdcef2dc2020-08-11 14:47:50 +08005265 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005266
5267 NotifyMotionArgs motionArgs;
5268
5269 // Down.
5270 int32_t x = 100;
5271 int32_t y = 125;
5272 processDown(mapper, x, y);
5273 processSync(mapper);
5274
5275 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5276 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5277 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5278 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5279 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5280 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5281 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5282 ASSERT_EQ(0, motionArgs.flags);
5283 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5284 ASSERT_EQ(0, motionArgs.buttonState);
5285 ASSERT_EQ(0, motionArgs.edgeFlags);
5286 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5287 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5288 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5289 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5290 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5291 1, 0, 0, 0, 0, 0, 0, 0));
5292 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5293 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5294 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5295
5296 // Move.
5297 x += 50;
5298 y += 75;
5299 processMove(mapper, x, y);
5300 processSync(mapper);
5301
5302 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5303 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5304 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5305 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5306 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5307 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5308 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5309 ASSERT_EQ(0, motionArgs.flags);
5310 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5311 ASSERT_EQ(0, motionArgs.buttonState);
5312 ASSERT_EQ(0, motionArgs.edgeFlags);
5313 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5314 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5315 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5316 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5317 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5318 1, 0, 0, 0, 0, 0, 0, 0));
5319 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5320 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5321 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5322
5323 // Up.
5324 processUp(mapper);
5325 processSync(mapper);
5326
5327 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5328 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5329 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5330 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5331 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5332 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5333 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5334 ASSERT_EQ(0, motionArgs.flags);
5335 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5336 ASSERT_EQ(0, motionArgs.buttonState);
5337 ASSERT_EQ(0, motionArgs.edgeFlags);
5338 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5339 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5340 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5341 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5342 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5343 1, 0, 0, 0, 0, 0, 0, 0));
5344 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5345 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5346 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5347
5348 // Should not have sent any more keys or motions.
5349 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5350 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5351}
5352
Michael Wrightd02c5b62014-02-10 15:10:22 -08005353TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005354 addConfigurationProperty("touch.deviceType", "touchScreen");
5355 prepareDisplay(DISPLAY_ORIENTATION_0);
5356 prepareButtons();
5357 prepareAxes(POSITION);
5358 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005359 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005360
arthurhungdcef2dc2020-08-11 14:47:50 +08005361 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005362
5363 NotifyMotionArgs motionArgs;
5364
5365 // Down.
5366 int32_t x = 100;
5367 int32_t y = 125;
5368 processDown(mapper, x, y);
5369 processSync(mapper);
5370
5371 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5372 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5373 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5374 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5375 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5376 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5377 ASSERT_EQ(0, motionArgs.flags);
5378 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5379 ASSERT_EQ(0, motionArgs.buttonState);
5380 ASSERT_EQ(0, motionArgs.edgeFlags);
5381 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5382 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5383 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5384 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5385 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5386 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5387 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5388 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5389
5390 // Move.
5391 x += 50;
5392 y += 75;
5393 processMove(mapper, x, y);
5394 processSync(mapper);
5395
5396 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5397 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5398 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5399 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5400 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5401 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5402 ASSERT_EQ(0, motionArgs.flags);
5403 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5404 ASSERT_EQ(0, motionArgs.buttonState);
5405 ASSERT_EQ(0, motionArgs.edgeFlags);
5406 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5407 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5408 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5409 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5410 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5411 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5412 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5413 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5414
5415 // Up.
5416 processUp(mapper);
5417 processSync(mapper);
5418
5419 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5420 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5421 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5422 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5423 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5424 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5425 ASSERT_EQ(0, motionArgs.flags);
5426 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5427 ASSERT_EQ(0, motionArgs.buttonState);
5428 ASSERT_EQ(0, motionArgs.edgeFlags);
5429 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5430 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5431 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5432 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5433 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5434 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5435 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5436 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5437
5438 // Should not have sent any more keys or motions.
5439 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5440 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5441}
5442
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005443TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_DoesNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005444 addConfigurationProperty("touch.deviceType", "touchScreen");
5445 prepareButtons();
5446 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005447 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
5448 // need to be rotated. Touchscreens are orientation-aware by default.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005449 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005450
5451 NotifyMotionArgs args;
5452
5453 // Rotation 90.
5454 prepareDisplay(DISPLAY_ORIENTATION_90);
5455 processDown(mapper, toRawX(50), toRawY(75));
5456 processSync(mapper);
5457
5458 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5459 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5460 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5461
5462 processUp(mapper);
5463 processSync(mapper);
5464 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5465}
5466
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005467TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_RotatesMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005468 addConfigurationProperty("touch.deviceType", "touchScreen");
5469 prepareButtons();
5470 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005471 // Since InputReader works in the un-rotated coordinate space, only devices that are not
5472 // orientation-aware are affected by display rotation.
5473 addConfigurationProperty("touch.orientationAware", "0");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005474 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005475
5476 NotifyMotionArgs args;
5477
5478 // Rotation 0.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005479 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005480 prepareDisplay(DISPLAY_ORIENTATION_0);
5481 processDown(mapper, toRawX(50), toRawY(75));
5482 processSync(mapper);
5483
5484 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5485 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5486 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5487
5488 processUp(mapper);
5489 processSync(mapper);
5490 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5491
5492 // Rotation 90.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005493 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005494 prepareDisplay(DISPLAY_ORIENTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005495 processDown(mapper, toRawX(75), RAW_Y_MAX - toRawY(50) + RAW_Y_MIN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005496 processSync(mapper);
5497
5498 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5499 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5500 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5501
5502 processUp(mapper);
5503 processSync(mapper);
5504 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5505
5506 // Rotation 180.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005507 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005508 prepareDisplay(DISPLAY_ORIENTATION_180);
5509 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
5510 processSync(mapper);
5511
5512 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5513 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5514 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5515
5516 processUp(mapper);
5517 processSync(mapper);
5518 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5519
5520 // Rotation 270.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005521 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005522 prepareDisplay(DISPLAY_ORIENTATION_270);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005523 processDown(mapper, RAW_X_MAX - toRawX(75) + RAW_X_MIN, toRawY(50));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005524 processSync(mapper);
5525
5526 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5527 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5528 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5529
5530 processUp(mapper);
5531 processSync(mapper);
5532 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5533}
5534
5535TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005536 addConfigurationProperty("touch.deviceType", "touchScreen");
5537 prepareDisplay(DISPLAY_ORIENTATION_0);
5538 prepareButtons();
5539 prepareAxes(POSITION | PRESSURE | TOOL | DISTANCE | TILT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005540 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005541
5542 // These calculations are based on the input device calibration documentation.
5543 int32_t rawX = 100;
5544 int32_t rawY = 200;
5545 int32_t rawPressure = 10;
5546 int32_t rawToolMajor = 12;
5547 int32_t rawDistance = 2;
5548 int32_t rawTiltX = 30;
5549 int32_t rawTiltY = 110;
5550
5551 float x = toDisplayX(rawX);
5552 float y = toDisplayY(rawY);
5553 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
5554 float size = float(rawToolMajor) / RAW_TOOL_MAX;
5555 float tool = float(rawToolMajor) * GEOMETRIC_SCALE;
5556 float distance = float(rawDistance);
5557
5558 float tiltCenter = (RAW_TILT_MAX + RAW_TILT_MIN) * 0.5f;
5559 float tiltScale = M_PI / 180;
5560 float tiltXAngle = (rawTiltX - tiltCenter) * tiltScale;
5561 float tiltYAngle = (rawTiltY - tiltCenter) * tiltScale;
5562 float orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
5563 float tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
5564
5565 processDown(mapper, rawX, rawY);
5566 processPressure(mapper, rawPressure);
5567 processToolMajor(mapper, rawToolMajor);
5568 processDistance(mapper, rawDistance);
5569 processTilt(mapper, rawTiltX, rawTiltY);
5570 processSync(mapper);
5571
5572 NotifyMotionArgs args;
5573 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5574 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5575 x, y, pressure, size, tool, tool, tool, tool, orientation, distance));
5576 ASSERT_EQ(tilt, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TILT));
5577}
5578
Jason Gerecke489fda82012-09-07 17:19:40 -07005579TEST_F(SingleTouchInputMapperTest, Process_XYAxes_AffineCalibration) {
Jason Gerecke489fda82012-09-07 17:19:40 -07005580 addConfigurationProperty("touch.deviceType", "touchScreen");
5581 prepareDisplay(DISPLAY_ORIENTATION_0);
5582 prepareLocationCalibration();
5583 prepareButtons();
5584 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005585 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Jason Gerecke489fda82012-09-07 17:19:40 -07005586
5587 int32_t rawX = 100;
5588 int32_t rawY = 200;
5589
5590 float x = toDisplayX(toCookedX(rawX, rawY));
5591 float y = toDisplayY(toCookedY(rawX, rawY));
5592
5593 processDown(mapper, rawX, rawY);
5594 processSync(mapper);
5595
5596 NotifyMotionArgs args;
5597 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5598 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5599 x, y, 1, 0, 0, 0, 0, 0, 0, 0));
5600}
5601
Michael Wrightd02c5b62014-02-10 15:10:22 -08005602TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005603 addConfigurationProperty("touch.deviceType", "touchScreen");
5604 prepareDisplay(DISPLAY_ORIENTATION_0);
5605 prepareButtons();
5606 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005607 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005608
5609 NotifyMotionArgs motionArgs;
5610 NotifyKeyArgs keyArgs;
5611
5612 processDown(mapper, 100, 200);
5613 processSync(mapper);
5614 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5615 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5616 ASSERT_EQ(0, motionArgs.buttonState);
5617
5618 // press BTN_LEFT, release BTN_LEFT
5619 processKey(mapper, BTN_LEFT, 1);
5620 processSync(mapper);
5621 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5622 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5623 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
5624
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005625 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5626 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5627 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
5628
Michael Wrightd02c5b62014-02-10 15:10:22 -08005629 processKey(mapper, BTN_LEFT, 0);
5630 processSync(mapper);
5631 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005632 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005633 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005634
5635 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005636 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005637 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005638
5639 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
5640 processKey(mapper, BTN_RIGHT, 1);
5641 processKey(mapper, BTN_MIDDLE, 1);
5642 processSync(mapper);
5643 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5644 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5645 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5646 motionArgs.buttonState);
5647
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005648 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5649 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5650 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
5651
5652 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5653 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5654 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5655 motionArgs.buttonState);
5656
Michael Wrightd02c5b62014-02-10 15:10:22 -08005657 processKey(mapper, BTN_RIGHT, 0);
5658 processSync(mapper);
5659 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005660 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005661 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005662
5663 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005664 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005665 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005666
5667 processKey(mapper, BTN_MIDDLE, 0);
5668 processSync(mapper);
5669 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005670 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005671 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005672
5673 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005674 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005675 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005676
5677 // press BTN_BACK, release BTN_BACK
5678 processKey(mapper, BTN_BACK, 1);
5679 processSync(mapper);
5680 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5681 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5682 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005683
Michael Wrightd02c5b62014-02-10 15:10:22 -08005684 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005685 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005686 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5687
5688 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5689 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5690 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005691
5692 processKey(mapper, BTN_BACK, 0);
5693 processSync(mapper);
5694 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005695 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005696 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005697
5698 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005699 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005700 ASSERT_EQ(0, motionArgs.buttonState);
5701
Michael Wrightd02c5b62014-02-10 15:10:22 -08005702 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5703 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5704 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
5705
5706 // press BTN_SIDE, release BTN_SIDE
5707 processKey(mapper, BTN_SIDE, 1);
5708 processSync(mapper);
5709 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5710 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5711 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005712
Michael Wrightd02c5b62014-02-10 15:10:22 -08005713 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005714 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005715 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5716
5717 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5718 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5719 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005720
5721 processKey(mapper, BTN_SIDE, 0);
5722 processSync(mapper);
5723 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005724 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005725 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005726
5727 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005728 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005729 ASSERT_EQ(0, motionArgs.buttonState);
5730
Michael Wrightd02c5b62014-02-10 15:10:22 -08005731 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5732 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5733 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
5734
5735 // press BTN_FORWARD, release BTN_FORWARD
5736 processKey(mapper, BTN_FORWARD, 1);
5737 processSync(mapper);
5738 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5739 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5740 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005741
Michael Wrightd02c5b62014-02-10 15:10:22 -08005742 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005743 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005744 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5745
5746 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5747 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5748 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005749
5750 processKey(mapper, BTN_FORWARD, 0);
5751 processSync(mapper);
5752 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005753 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005754 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005755
5756 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005757 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005758 ASSERT_EQ(0, motionArgs.buttonState);
5759
Michael Wrightd02c5b62014-02-10 15:10:22 -08005760 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5761 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5762 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
5763
5764 // press BTN_EXTRA, release BTN_EXTRA
5765 processKey(mapper, BTN_EXTRA, 1);
5766 processSync(mapper);
5767 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5768 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5769 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005770
Michael Wrightd02c5b62014-02-10 15:10:22 -08005771 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005772 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005773 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5774
5775 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5776 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5777 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005778
5779 processKey(mapper, BTN_EXTRA, 0);
5780 processSync(mapper);
5781 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005782 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005783 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005784
5785 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005786 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005787 ASSERT_EQ(0, motionArgs.buttonState);
5788
Michael Wrightd02c5b62014-02-10 15:10:22 -08005789 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5790 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5791 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
5792
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005793 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5794
Michael Wrightd02c5b62014-02-10 15:10:22 -08005795 // press BTN_STYLUS, release BTN_STYLUS
5796 processKey(mapper, BTN_STYLUS, 1);
5797 processSync(mapper);
5798 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5799 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005800 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
5801
5802 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5803 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5804 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005805
5806 processKey(mapper, BTN_STYLUS, 0);
5807 processSync(mapper);
5808 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005809 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005810 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005811
5812 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005813 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005814 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005815
5816 // press BTN_STYLUS2, release BTN_STYLUS2
5817 processKey(mapper, BTN_STYLUS2, 1);
5818 processSync(mapper);
5819 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5820 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005821 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
5822
5823 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5824 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5825 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005826
5827 processKey(mapper, BTN_STYLUS2, 0);
5828 processSync(mapper);
5829 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005830 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005831 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005832
5833 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005834 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005835 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005836
5837 // release touch
5838 processUp(mapper);
5839 processSync(mapper);
5840 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5841 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5842 ASSERT_EQ(0, motionArgs.buttonState);
5843}
5844
5845TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005846 addConfigurationProperty("touch.deviceType", "touchScreen");
5847 prepareDisplay(DISPLAY_ORIENTATION_0);
5848 prepareButtons();
5849 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005850 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005851
5852 NotifyMotionArgs motionArgs;
5853
5854 // default tool type is finger
5855 processDown(mapper, 100, 200);
5856 processSync(mapper);
5857 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5858 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5859 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5860
5861 // eraser
5862 processKey(mapper, BTN_TOOL_RUBBER, 1);
5863 processSync(mapper);
5864 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5865 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5866 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
5867
5868 // stylus
5869 processKey(mapper, BTN_TOOL_RUBBER, 0);
5870 processKey(mapper, BTN_TOOL_PEN, 1);
5871 processSync(mapper);
5872 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5873 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5874 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5875
5876 // brush
5877 processKey(mapper, BTN_TOOL_PEN, 0);
5878 processKey(mapper, BTN_TOOL_BRUSH, 1);
5879 processSync(mapper);
5880 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5881 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5882 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5883
5884 // pencil
5885 processKey(mapper, BTN_TOOL_BRUSH, 0);
5886 processKey(mapper, BTN_TOOL_PENCIL, 1);
5887 processSync(mapper);
5888 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5889 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5890 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5891
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08005892 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08005893 processKey(mapper, BTN_TOOL_PENCIL, 0);
5894 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
5895 processSync(mapper);
5896 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5897 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5898 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5899
5900 // mouse
5901 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
5902 processKey(mapper, BTN_TOOL_MOUSE, 1);
5903 processSync(mapper);
5904 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5905 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5906 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
5907
5908 // lens
5909 processKey(mapper, BTN_TOOL_MOUSE, 0);
5910 processKey(mapper, BTN_TOOL_LENS, 1);
5911 processSync(mapper);
5912 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5913 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5914 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
5915
5916 // double-tap
5917 processKey(mapper, BTN_TOOL_LENS, 0);
5918 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
5919 processSync(mapper);
5920 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5921 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5922 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5923
5924 // triple-tap
5925 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
5926 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
5927 processSync(mapper);
5928 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5929 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5930 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5931
5932 // quad-tap
5933 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
5934 processKey(mapper, BTN_TOOL_QUADTAP, 1);
5935 processSync(mapper);
5936 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5937 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5938 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5939
5940 // finger
5941 processKey(mapper, BTN_TOOL_QUADTAP, 0);
5942 processKey(mapper, BTN_TOOL_FINGER, 1);
5943 processSync(mapper);
5944 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5945 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5946 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5947
5948 // stylus trumps finger
5949 processKey(mapper, BTN_TOOL_PEN, 1);
5950 processSync(mapper);
5951 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5952 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5953 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5954
5955 // eraser trumps stylus
5956 processKey(mapper, BTN_TOOL_RUBBER, 1);
5957 processSync(mapper);
5958 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5959 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5960 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
5961
5962 // mouse trumps eraser
5963 processKey(mapper, BTN_TOOL_MOUSE, 1);
5964 processSync(mapper);
5965 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5966 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5967 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
5968
5969 // back to default tool type
5970 processKey(mapper, BTN_TOOL_MOUSE, 0);
5971 processKey(mapper, BTN_TOOL_RUBBER, 0);
5972 processKey(mapper, BTN_TOOL_PEN, 0);
5973 processKey(mapper, BTN_TOOL_FINGER, 0);
5974 processSync(mapper);
5975 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5976 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5977 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5978}
5979
5980TEST_F(SingleTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005981 addConfigurationProperty("touch.deviceType", "touchScreen");
5982 prepareDisplay(DISPLAY_ORIENTATION_0);
5983 prepareButtons();
5984 prepareAxes(POSITION);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005985 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_FINGER, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005986 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005987
5988 NotifyMotionArgs motionArgs;
5989
5990 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
5991 processKey(mapper, BTN_TOOL_FINGER, 1);
5992 processMove(mapper, 100, 200);
5993 processSync(mapper);
5994 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5995 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5996 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5997 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5998
5999 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6000 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6001 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6002 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6003
6004 // move a little
6005 processMove(mapper, 150, 250);
6006 processSync(mapper);
6007 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6008 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6009 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6010 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6011
6012 // down when BTN_TOUCH is pressed, pressure defaults to 1
6013 processKey(mapper, BTN_TOUCH, 1);
6014 processSync(mapper);
6015 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6016 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6017 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6018 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6019
6020 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6021 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6022 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6023 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6024
6025 // up when BTN_TOUCH is released, hover restored
6026 processKey(mapper, BTN_TOUCH, 0);
6027 processSync(mapper);
6028 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6029 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6030 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6031 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6032
6033 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6034 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6035 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6036 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6037
6038 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6039 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6040 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6041 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6042
6043 // exit hover when pointer goes away
6044 processKey(mapper, BTN_TOOL_FINGER, 0);
6045 processSync(mapper);
6046 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6047 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6048 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6049 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6050}
6051
6052TEST_F(SingleTouchInputMapperTest, Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006053 addConfigurationProperty("touch.deviceType", "touchScreen");
6054 prepareDisplay(DISPLAY_ORIENTATION_0);
6055 prepareButtons();
6056 prepareAxes(POSITION | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006057 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006058
6059 NotifyMotionArgs motionArgs;
6060
6061 // initially hovering because pressure is 0
6062 processDown(mapper, 100, 200);
6063 processPressure(mapper, 0);
6064 processSync(mapper);
6065 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6066 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6067 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6068 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6069
6070 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6071 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6072 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6073 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6074
6075 // move a little
6076 processMove(mapper, 150, 250);
6077 processSync(mapper);
6078 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6079 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6080 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6081 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6082
6083 // down when pressure is non-zero
6084 processPressure(mapper, RAW_PRESSURE_MAX);
6085 processSync(mapper);
6086 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6087 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6088 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6089 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6090
6091 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6092 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6093 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6094 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6095
6096 // up when pressure becomes 0, hover restored
6097 processPressure(mapper, 0);
6098 processSync(mapper);
6099 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6100 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6101 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6102 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6103
6104 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6105 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6106 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6107 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6108
6109 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6110 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6111 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6112 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6113
6114 // exit hover when pointer goes away
6115 processUp(mapper);
6116 processSync(mapper);
6117 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6118 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6119 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6120 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6121}
6122
Michael Wrightd02c5b62014-02-10 15:10:22 -08006123// --- MultiTouchInputMapperTest ---
6124
6125class MultiTouchInputMapperTest : public TouchInputMapperTest {
6126protected:
6127 void prepareAxes(int axes);
6128
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006129 void processPosition(MultiTouchInputMapper& mapper, int32_t x, int32_t y);
6130 void processTouchMajor(MultiTouchInputMapper& mapper, int32_t touchMajor);
6131 void processTouchMinor(MultiTouchInputMapper& mapper, int32_t touchMinor);
6132 void processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor);
6133 void processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor);
6134 void processOrientation(MultiTouchInputMapper& mapper, int32_t orientation);
6135 void processPressure(MultiTouchInputMapper& mapper, int32_t pressure);
6136 void processDistance(MultiTouchInputMapper& mapper, int32_t distance);
6137 void processId(MultiTouchInputMapper& mapper, int32_t id);
6138 void processSlot(MultiTouchInputMapper& mapper, int32_t slot);
6139 void processToolType(MultiTouchInputMapper& mapper, int32_t toolType);
6140 void processKey(MultiTouchInputMapper& mapper, int32_t code, int32_t value);
6141 void processMTSync(MultiTouchInputMapper& mapper);
6142 void processSync(MultiTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006143};
6144
6145void MultiTouchInputMapperTest::prepareAxes(int axes) {
6146 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006147 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
6148 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006149 }
6150 if (axes & TOUCH) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006151 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN,
6152 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006153 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006154 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN,
6155 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006156 }
6157 }
6158 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006159 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
6160 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006161 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006162 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MAX,
6163 RAW_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006164 }
6165 }
6166 if (axes & ORIENTATION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006167 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_ORIENTATION, RAW_ORIENTATION_MIN,
6168 RAW_ORIENTATION_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006169 }
6170 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006171 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_PRESSURE, RAW_PRESSURE_MIN,
6172 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006173 }
6174 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006175 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_DISTANCE, RAW_DISTANCE_MIN,
6176 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006177 }
6178 if (axes & ID) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006179 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX, 0,
6180 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006181 }
6182 if (axes & SLOT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006183 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
6184 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_MT_SLOT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006185 }
6186 if (axes & TOOL_TYPE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006187 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOOL_TYPE, 0, MT_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006188 }
6189}
6190
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006191void MultiTouchInputMapperTest::processPosition(MultiTouchInputMapper& mapper, int32_t x,
6192 int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006193 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_X, x);
6194 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006195}
6196
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006197void MultiTouchInputMapperTest::processTouchMajor(MultiTouchInputMapper& mapper,
6198 int32_t touchMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006199 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006200}
6201
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006202void MultiTouchInputMapperTest::processTouchMinor(MultiTouchInputMapper& mapper,
6203 int32_t touchMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006204 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006205}
6206
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006207void MultiTouchInputMapperTest::processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006208 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006209}
6210
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006211void MultiTouchInputMapperTest::processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006212 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006213}
6214
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006215void MultiTouchInputMapperTest::processOrientation(MultiTouchInputMapper& mapper,
6216 int32_t orientation) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006217 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_ORIENTATION, orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006218}
6219
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006220void MultiTouchInputMapperTest::processPressure(MultiTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006221 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006222}
6223
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006224void MultiTouchInputMapperTest::processDistance(MultiTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006225 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006226}
6227
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006228void MultiTouchInputMapperTest::processId(MultiTouchInputMapper& mapper, int32_t id) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006229 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TRACKING_ID, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006230}
6231
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006232void MultiTouchInputMapperTest::processSlot(MultiTouchInputMapper& mapper, int32_t slot) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006233 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_SLOT, slot);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006234}
6235
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006236void MultiTouchInputMapperTest::processToolType(MultiTouchInputMapper& mapper, int32_t toolType) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006237 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006238}
6239
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006240void MultiTouchInputMapperTest::processKey(MultiTouchInputMapper& mapper, int32_t code,
6241 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006242 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006243}
6244
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006245void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006246 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_MT_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006247}
6248
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006249void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006250 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006251}
6252
Michael Wrightd02c5b62014-02-10 15:10:22 -08006253TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006254 addConfigurationProperty("touch.deviceType", "touchScreen");
6255 prepareDisplay(DISPLAY_ORIENTATION_0);
6256 prepareAxes(POSITION);
6257 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006258 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006259
arthurhungdcef2dc2020-08-11 14:47:50 +08006260 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006261
6262 NotifyMotionArgs motionArgs;
6263
6264 // Two fingers down at once.
6265 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
6266 processPosition(mapper, x1, y1);
6267 processMTSync(mapper);
6268 processPosition(mapper, x2, y2);
6269 processMTSync(mapper);
6270 processSync(mapper);
6271
6272 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6273 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6274 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6275 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6276 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6277 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6278 ASSERT_EQ(0, motionArgs.flags);
6279 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6280 ASSERT_EQ(0, motionArgs.buttonState);
6281 ASSERT_EQ(0, motionArgs.edgeFlags);
6282 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6283 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6284 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6285 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6286 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6287 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6288 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6289 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6290
6291 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6292 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6293 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6294 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6295 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6296 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6297 motionArgs.action);
6298 ASSERT_EQ(0, motionArgs.flags);
6299 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6300 ASSERT_EQ(0, motionArgs.buttonState);
6301 ASSERT_EQ(0, motionArgs.edgeFlags);
6302 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6303 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6304 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6305 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6306 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6307 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6308 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6309 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6310 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6311 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6312 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6313 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6314
6315 // Move.
6316 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
6317 processPosition(mapper, x1, y1);
6318 processMTSync(mapper);
6319 processPosition(mapper, x2, y2);
6320 processMTSync(mapper);
6321 processSync(mapper);
6322
6323 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6324 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6325 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6326 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6327 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6328 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6329 ASSERT_EQ(0, motionArgs.flags);
6330 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6331 ASSERT_EQ(0, motionArgs.buttonState);
6332 ASSERT_EQ(0, motionArgs.edgeFlags);
6333 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6334 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6335 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6336 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6337 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6338 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6339 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6340 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6341 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6342 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6343 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6344 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6345
6346 // First finger up.
6347 x2 += 15; y2 -= 20;
6348 processPosition(mapper, x2, y2);
6349 processMTSync(mapper);
6350 processSync(mapper);
6351
6352 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6353 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6354 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6355 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6356 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6357 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6358 motionArgs.action);
6359 ASSERT_EQ(0, motionArgs.flags);
6360 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6361 ASSERT_EQ(0, motionArgs.buttonState);
6362 ASSERT_EQ(0, motionArgs.edgeFlags);
6363 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6364 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6365 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6366 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6367 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6368 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6369 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6370 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6371 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6372 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6373 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6374 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6375
6376 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6377 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6378 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6379 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6380 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6381 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6382 ASSERT_EQ(0, motionArgs.flags);
6383 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6384 ASSERT_EQ(0, motionArgs.buttonState);
6385 ASSERT_EQ(0, motionArgs.edgeFlags);
6386 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6387 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6388 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6389 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6390 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6391 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6392 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6393 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6394
6395 // Move.
6396 x2 += 20; y2 -= 25;
6397 processPosition(mapper, x2, y2);
6398 processMTSync(mapper);
6399 processSync(mapper);
6400
6401 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6402 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6403 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6404 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6405 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6406 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6407 ASSERT_EQ(0, motionArgs.flags);
6408 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6409 ASSERT_EQ(0, motionArgs.buttonState);
6410 ASSERT_EQ(0, motionArgs.edgeFlags);
6411 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6412 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6413 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6414 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6415 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6416 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6417 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6418 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6419
6420 // New finger down.
6421 int32_t x3 = 700, y3 = 300;
6422 processPosition(mapper, x2, y2);
6423 processMTSync(mapper);
6424 processPosition(mapper, x3, y3);
6425 processMTSync(mapper);
6426 processSync(mapper);
6427
6428 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6429 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6430 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6431 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6432 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6433 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6434 motionArgs.action);
6435 ASSERT_EQ(0, motionArgs.flags);
6436 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6437 ASSERT_EQ(0, motionArgs.buttonState);
6438 ASSERT_EQ(0, motionArgs.edgeFlags);
6439 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6440 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6441 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6442 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6443 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6444 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6445 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6446 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6447 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6448 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6449 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6450 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6451
6452 // Second finger up.
6453 x3 += 30; y3 -= 20;
6454 processPosition(mapper, x3, y3);
6455 processMTSync(mapper);
6456 processSync(mapper);
6457
6458 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6459 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6460 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6461 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6462 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6463 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6464 motionArgs.action);
6465 ASSERT_EQ(0, motionArgs.flags);
6466 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6467 ASSERT_EQ(0, motionArgs.buttonState);
6468 ASSERT_EQ(0, motionArgs.edgeFlags);
6469 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6470 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6471 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6472 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6473 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6474 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6475 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6476 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6477 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6478 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6479 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6480 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6481
6482 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6483 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6484 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6485 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6486 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6487 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6488 ASSERT_EQ(0, motionArgs.flags);
6489 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6490 ASSERT_EQ(0, motionArgs.buttonState);
6491 ASSERT_EQ(0, motionArgs.edgeFlags);
6492 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6493 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6494 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6495 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6496 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6497 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6498 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6499 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6500
6501 // Last finger up.
6502 processMTSync(mapper);
6503 processSync(mapper);
6504
6505 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6506 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6507 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6508 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6509 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6510 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6511 ASSERT_EQ(0, motionArgs.flags);
6512 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6513 ASSERT_EQ(0, motionArgs.buttonState);
6514 ASSERT_EQ(0, motionArgs.edgeFlags);
6515 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6516 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6517 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6518 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6519 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6520 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6521 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6522 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6523
6524 // Should not have sent any more keys or motions.
6525 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6526 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6527}
6528
6529TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006530 addConfigurationProperty("touch.deviceType", "touchScreen");
6531 prepareDisplay(DISPLAY_ORIENTATION_0);
6532 prepareAxes(POSITION | ID);
6533 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006534 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006535
arthurhungdcef2dc2020-08-11 14:47:50 +08006536 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006537
6538 NotifyMotionArgs motionArgs;
6539
6540 // Two fingers down at once.
6541 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
6542 processPosition(mapper, x1, y1);
6543 processId(mapper, 1);
6544 processMTSync(mapper);
6545 processPosition(mapper, x2, y2);
6546 processId(mapper, 2);
6547 processMTSync(mapper);
6548 processSync(mapper);
6549
6550 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6551 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6552 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6553 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6554 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6555 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6556 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6557
6558 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6559 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6560 motionArgs.action);
6561 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6562 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6563 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6564 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6565 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6566 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6567 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6568 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6569 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6570
6571 // Move.
6572 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
6573 processPosition(mapper, x1, y1);
6574 processId(mapper, 1);
6575 processMTSync(mapper);
6576 processPosition(mapper, x2, y2);
6577 processId(mapper, 2);
6578 processMTSync(mapper);
6579 processSync(mapper);
6580
6581 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6582 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6583 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6584 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6585 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6586 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6587 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6588 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6589 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6590 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6591 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6592
6593 // First finger up.
6594 x2 += 15; y2 -= 20;
6595 processPosition(mapper, x2, y2);
6596 processId(mapper, 2);
6597 processMTSync(mapper);
6598 processSync(mapper);
6599
6600 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6601 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6602 motionArgs.action);
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(x1), toDisplayY(y1), 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
6613 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6614 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6615 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6616 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6617 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6618 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6619 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6620
6621 // Move.
6622 x2 += 20; y2 -= 25;
6623 processPosition(mapper, x2, y2);
6624 processId(mapper, 2);
6625 processMTSync(mapper);
6626 processSync(mapper);
6627
6628 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6629 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6630 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6631 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6632 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6633 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6634 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6635
6636 // New finger down.
6637 int32_t x3 = 700, y3 = 300;
6638 processPosition(mapper, x2, y2);
6639 processId(mapper, 2);
6640 processMTSync(mapper);
6641 processPosition(mapper, x3, y3);
6642 processId(mapper, 3);
6643 processMTSync(mapper);
6644 processSync(mapper);
6645
6646 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6647 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6648 motionArgs.action);
6649 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6650 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6651 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6652 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6653 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6654 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6655 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6656 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6657 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6658
6659 // Second finger up.
6660 x3 += 30; y3 -= 20;
6661 processPosition(mapper, x3, y3);
6662 processId(mapper, 3);
6663 processMTSync(mapper);
6664 processSync(mapper);
6665
6666 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6667 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6668 motionArgs.action);
6669 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6670 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6671 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6672 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6673 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6674 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6675 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6676 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6677 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6678
6679 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6680 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6681 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6682 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6683 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6684 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6685 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6686
6687 // Last finger up.
6688 processMTSync(mapper);
6689 processSync(mapper);
6690
6691 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6692 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6693 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6694 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6695 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6696 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6697 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6698
6699 // Should not have sent any more keys or motions.
6700 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6701 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6702}
6703
6704TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006705 addConfigurationProperty("touch.deviceType", "touchScreen");
6706 prepareDisplay(DISPLAY_ORIENTATION_0);
6707 prepareAxes(POSITION | ID | SLOT);
6708 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006709 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006710
arthurhungdcef2dc2020-08-11 14:47:50 +08006711 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006712
6713 NotifyMotionArgs motionArgs;
6714
6715 // Two fingers down at once.
6716 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
6717 processPosition(mapper, x1, y1);
6718 processId(mapper, 1);
6719 processSlot(mapper, 1);
6720 processPosition(mapper, x2, y2);
6721 processId(mapper, 2);
6722 processSync(mapper);
6723
6724 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6725 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6726 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6727 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6728 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6729 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6730 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6731
6732 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6733 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6734 motionArgs.action);
6735 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6736 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6737 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6738 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6739 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6740 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6741 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6742 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6743 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6744
6745 // Move.
6746 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
6747 processSlot(mapper, 0);
6748 processPosition(mapper, x1, y1);
6749 processSlot(mapper, 1);
6750 processPosition(mapper, x2, y2);
6751 processSync(mapper);
6752
6753 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6754 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6755 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6756 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6757 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6758 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6759 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6760 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6761 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6762 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6763 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6764
6765 // First finger up.
6766 x2 += 15; y2 -= 20;
6767 processSlot(mapper, 0);
6768 processId(mapper, -1);
6769 processSlot(mapper, 1);
6770 processPosition(mapper, x2, y2);
6771 processSync(mapper);
6772
6773 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6774 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6775 motionArgs.action);
6776 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6777 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6778 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6779 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6780 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6781 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6782 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6783 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6784 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6785
6786 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6787 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6788 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6789 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6790 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6791 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6792 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6793
6794 // Move.
6795 x2 += 20; y2 -= 25;
6796 processPosition(mapper, x2, y2);
6797 processSync(mapper);
6798
6799 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6800 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6801 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6802 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6803 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6804 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6805 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6806
6807 // New finger down.
6808 int32_t x3 = 700, y3 = 300;
6809 processPosition(mapper, x2, y2);
6810 processSlot(mapper, 0);
6811 processId(mapper, 3);
6812 processPosition(mapper, x3, y3);
6813 processSync(mapper);
6814
6815 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6816 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6817 motionArgs.action);
6818 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6819 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6820 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6821 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6822 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6823 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6824 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6825 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6826 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6827
6828 // Second finger up.
6829 x3 += 30; y3 -= 20;
6830 processSlot(mapper, 1);
6831 processId(mapper, -1);
6832 processSlot(mapper, 0);
6833 processPosition(mapper, x3, y3);
6834 processSync(mapper);
6835
6836 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6837 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6838 motionArgs.action);
6839 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6840 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6841 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6842 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6843 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6844 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6845 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6846 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6847 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6848
6849 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6850 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6851 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6852 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6853 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6854 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6855 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6856
6857 // Last finger up.
6858 processId(mapper, -1);
6859 processSync(mapper);
6860
6861 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6862 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6863 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6864 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6865 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6866 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6867 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6868
6869 // Should not have sent any more keys or motions.
6870 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6871 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6872}
6873
6874TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006875 addConfigurationProperty("touch.deviceType", "touchScreen");
6876 prepareDisplay(DISPLAY_ORIENTATION_0);
6877 prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006878 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006879
6880 // These calculations are based on the input device calibration documentation.
6881 int32_t rawX = 100;
6882 int32_t rawY = 200;
6883 int32_t rawTouchMajor = 7;
6884 int32_t rawTouchMinor = 6;
6885 int32_t rawToolMajor = 9;
6886 int32_t rawToolMinor = 8;
6887 int32_t rawPressure = 11;
6888 int32_t rawDistance = 0;
6889 int32_t rawOrientation = 3;
6890 int32_t id = 5;
6891
6892 float x = toDisplayX(rawX);
6893 float y = toDisplayY(rawY);
6894 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
6895 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
6896 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
6897 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
6898 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
6899 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
6900 float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
6901 float distance = float(rawDistance);
6902
6903 processPosition(mapper, rawX, rawY);
6904 processTouchMajor(mapper, rawTouchMajor);
6905 processTouchMinor(mapper, rawTouchMinor);
6906 processToolMajor(mapper, rawToolMajor);
6907 processToolMinor(mapper, rawToolMinor);
6908 processPressure(mapper, rawPressure);
6909 processOrientation(mapper, rawOrientation);
6910 processDistance(mapper, rawDistance);
6911 processId(mapper, id);
6912 processMTSync(mapper);
6913 processSync(mapper);
6914
6915 NotifyMotionArgs args;
6916 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6917 ASSERT_EQ(0, args.pointerProperties[0].id);
6918 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6919 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
6920 orientation, distance));
6921}
6922
6923TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006924 addConfigurationProperty("touch.deviceType", "touchScreen");
6925 prepareDisplay(DISPLAY_ORIENTATION_0);
6926 prepareAxes(POSITION | TOUCH | TOOL | MINOR);
6927 addConfigurationProperty("touch.size.calibration", "geometric");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006928 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006929
6930 // These calculations are based on the input device calibration documentation.
6931 int32_t rawX = 100;
6932 int32_t rawY = 200;
6933 int32_t rawTouchMajor = 140;
6934 int32_t rawTouchMinor = 120;
6935 int32_t rawToolMajor = 180;
6936 int32_t rawToolMinor = 160;
6937
6938 float x = toDisplayX(rawX);
6939 float y = toDisplayY(rawY);
6940 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
6941 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
6942 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
6943 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
6944 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
6945
6946 processPosition(mapper, rawX, rawY);
6947 processTouchMajor(mapper, rawTouchMajor);
6948 processTouchMinor(mapper, rawTouchMinor);
6949 processToolMajor(mapper, rawToolMajor);
6950 processToolMinor(mapper, rawToolMinor);
6951 processMTSync(mapper);
6952 processSync(mapper);
6953
6954 NotifyMotionArgs args;
6955 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6956 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6957 x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
6958}
6959
6960TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006961 addConfigurationProperty("touch.deviceType", "touchScreen");
6962 prepareDisplay(DISPLAY_ORIENTATION_0);
6963 prepareAxes(POSITION | TOUCH | TOOL);
6964 addConfigurationProperty("touch.size.calibration", "diameter");
6965 addConfigurationProperty("touch.size.scale", "10");
6966 addConfigurationProperty("touch.size.bias", "160");
6967 addConfigurationProperty("touch.size.isSummed", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006968 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006969
6970 // These calculations are based on the input device calibration documentation.
6971 // Note: We only provide a single common touch/tool value because the device is assumed
6972 // not to emit separate values for each pointer (isSummed = 1).
6973 int32_t rawX = 100;
6974 int32_t rawY = 200;
6975 int32_t rawX2 = 150;
6976 int32_t rawY2 = 250;
6977 int32_t rawTouchMajor = 5;
6978 int32_t rawToolMajor = 8;
6979
6980 float x = toDisplayX(rawX);
6981 float y = toDisplayY(rawY);
6982 float x2 = toDisplayX(rawX2);
6983 float y2 = toDisplayY(rawY2);
6984 float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
6985 float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
6986 float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
6987
6988 processPosition(mapper, rawX, rawY);
6989 processTouchMajor(mapper, rawTouchMajor);
6990 processToolMajor(mapper, rawToolMajor);
6991 processMTSync(mapper);
6992 processPosition(mapper, rawX2, rawY2);
6993 processTouchMajor(mapper, rawTouchMajor);
6994 processToolMajor(mapper, rawToolMajor);
6995 processMTSync(mapper);
6996 processSync(mapper);
6997
6998 NotifyMotionArgs args;
6999 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7000 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
7001
7002 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7003 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
7004 args.action);
7005 ASSERT_EQ(size_t(2), args.pointerCount);
7006 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7007 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
7008 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
7009 x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
7010}
7011
7012TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007013 addConfigurationProperty("touch.deviceType", "touchScreen");
7014 prepareDisplay(DISPLAY_ORIENTATION_0);
7015 prepareAxes(POSITION | TOUCH | TOOL);
7016 addConfigurationProperty("touch.size.calibration", "area");
7017 addConfigurationProperty("touch.size.scale", "43");
7018 addConfigurationProperty("touch.size.bias", "3");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007019 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007020
7021 // These calculations are based on the input device calibration documentation.
7022 int32_t rawX = 100;
7023 int32_t rawY = 200;
7024 int32_t rawTouchMajor = 5;
7025 int32_t rawToolMajor = 8;
7026
7027 float x = toDisplayX(rawX);
7028 float y = toDisplayY(rawY);
7029 float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
7030 float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
7031 float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
7032
7033 processPosition(mapper, rawX, rawY);
7034 processTouchMajor(mapper, rawTouchMajor);
7035 processToolMajor(mapper, rawToolMajor);
7036 processMTSync(mapper);
7037 processSync(mapper);
7038
7039 NotifyMotionArgs args;
7040 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7041 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7042 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
7043}
7044
7045TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007046 addConfigurationProperty("touch.deviceType", "touchScreen");
7047 prepareDisplay(DISPLAY_ORIENTATION_0);
7048 prepareAxes(POSITION | PRESSURE);
7049 addConfigurationProperty("touch.pressure.calibration", "amplitude");
7050 addConfigurationProperty("touch.pressure.scale", "0.01");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007051 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007052
Michael Wrightaa449c92017-12-13 21:21:43 +00007053 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007054 mapper.populateDeviceInfo(&info);
Michael Wrightaa449c92017-12-13 21:21:43 +00007055 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
7056 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TOUCHSCREEN,
7057 0.0f, RAW_PRESSURE_MAX * 0.01, 0.0f, 0.0f));
7058
Michael Wrightd02c5b62014-02-10 15:10:22 -08007059 // These calculations are based on the input device calibration documentation.
7060 int32_t rawX = 100;
7061 int32_t rawY = 200;
7062 int32_t rawPressure = 60;
7063
7064 float x = toDisplayX(rawX);
7065 float y = toDisplayY(rawY);
7066 float pressure = float(rawPressure) * 0.01f;
7067
7068 processPosition(mapper, rawX, rawY);
7069 processPressure(mapper, rawPressure);
7070 processMTSync(mapper);
7071 processSync(mapper);
7072
7073 NotifyMotionArgs args;
7074 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7075 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7076 x, y, pressure, 0, 0, 0, 0, 0, 0, 0));
7077}
7078
7079TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007080 addConfigurationProperty("touch.deviceType", "touchScreen");
7081 prepareDisplay(DISPLAY_ORIENTATION_0);
7082 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007083 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007084
7085 NotifyMotionArgs motionArgs;
7086 NotifyKeyArgs keyArgs;
7087
7088 processId(mapper, 1);
7089 processPosition(mapper, 100, 200);
7090 processSync(mapper);
7091 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7092 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7093 ASSERT_EQ(0, motionArgs.buttonState);
7094
7095 // press BTN_LEFT, release BTN_LEFT
7096 processKey(mapper, BTN_LEFT, 1);
7097 processSync(mapper);
7098 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7099 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7100 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
7101
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007102 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7103 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7104 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
7105
Michael Wrightd02c5b62014-02-10 15:10:22 -08007106 processKey(mapper, BTN_LEFT, 0);
7107 processSync(mapper);
7108 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007109 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007110 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007111
7112 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007113 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007114 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007115
7116 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
7117 processKey(mapper, BTN_RIGHT, 1);
7118 processKey(mapper, BTN_MIDDLE, 1);
7119 processSync(mapper);
7120 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7121 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7122 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
7123 motionArgs.buttonState);
7124
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007125 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7126 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7127 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
7128
7129 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7130 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7131 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
7132 motionArgs.buttonState);
7133
Michael Wrightd02c5b62014-02-10 15:10:22 -08007134 processKey(mapper, BTN_RIGHT, 0);
7135 processSync(mapper);
7136 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007137 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007138 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007139
7140 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007141 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007142 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007143
7144 processKey(mapper, BTN_MIDDLE, 0);
7145 processSync(mapper);
7146 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007147 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007148 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007149
7150 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007151 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007152 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007153
7154 // press BTN_BACK, release BTN_BACK
7155 processKey(mapper, BTN_BACK, 1);
7156 processSync(mapper);
7157 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7158 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
7159 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007160
Michael Wrightd02c5b62014-02-10 15:10:22 -08007161 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007162 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007163 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
7164
7165 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7166 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7167 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007168
7169 processKey(mapper, BTN_BACK, 0);
7170 processSync(mapper);
7171 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007172 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007173 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007174
7175 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007176 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007177 ASSERT_EQ(0, motionArgs.buttonState);
7178
Michael Wrightd02c5b62014-02-10 15:10:22 -08007179 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7180 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
7181 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
7182
7183 // press BTN_SIDE, release BTN_SIDE
7184 processKey(mapper, BTN_SIDE, 1);
7185 processSync(mapper);
7186 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7187 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
7188 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007189
Michael Wrightd02c5b62014-02-10 15:10:22 -08007190 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007191 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007192 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
7193
7194 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7195 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7196 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007197
7198 processKey(mapper, BTN_SIDE, 0);
7199 processSync(mapper);
7200 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007201 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007202 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007203
7204 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007205 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007206 ASSERT_EQ(0, motionArgs.buttonState);
7207
Michael Wrightd02c5b62014-02-10 15:10:22 -08007208 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7209 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
7210 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
7211
7212 // press BTN_FORWARD, release BTN_FORWARD
7213 processKey(mapper, BTN_FORWARD, 1);
7214 processSync(mapper);
7215 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7216 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
7217 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007218
Michael Wrightd02c5b62014-02-10 15:10:22 -08007219 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007220 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007221 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
7222
7223 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7224 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7225 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007226
7227 processKey(mapper, BTN_FORWARD, 0);
7228 processSync(mapper);
7229 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007230 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007231 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007232
7233 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007234 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007235 ASSERT_EQ(0, motionArgs.buttonState);
7236
Michael Wrightd02c5b62014-02-10 15:10:22 -08007237 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7238 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
7239 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
7240
7241 // press BTN_EXTRA, release BTN_EXTRA
7242 processKey(mapper, BTN_EXTRA, 1);
7243 processSync(mapper);
7244 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7245 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
7246 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007247
Michael Wrightd02c5b62014-02-10 15:10:22 -08007248 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007249 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007250 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
7251
7252 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7253 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7254 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007255
7256 processKey(mapper, BTN_EXTRA, 0);
7257 processSync(mapper);
7258 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007259 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007260 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007261
7262 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007263 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007264 ASSERT_EQ(0, motionArgs.buttonState);
7265
Michael Wrightd02c5b62014-02-10 15:10:22 -08007266 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7267 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
7268 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
7269
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007270 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7271
Michael Wrightd02c5b62014-02-10 15:10:22 -08007272 // press BTN_STYLUS, release BTN_STYLUS
7273 processKey(mapper, BTN_STYLUS, 1);
7274 processSync(mapper);
7275 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7276 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007277 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
7278
7279 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7280 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7281 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007282
7283 processKey(mapper, BTN_STYLUS, 0);
7284 processSync(mapper);
7285 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007286 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007287 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007288
7289 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007290 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007291 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007292
7293 // press BTN_STYLUS2, release BTN_STYLUS2
7294 processKey(mapper, BTN_STYLUS2, 1);
7295 processSync(mapper);
7296 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7297 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007298 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
7299
7300 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7301 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7302 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007303
7304 processKey(mapper, BTN_STYLUS2, 0);
7305 processSync(mapper);
7306 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007307 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007308 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007309
7310 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007311 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007312 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007313
7314 // release touch
7315 processId(mapper, -1);
7316 processSync(mapper);
7317 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7318 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7319 ASSERT_EQ(0, motionArgs.buttonState);
7320}
7321
7322TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007323 addConfigurationProperty("touch.deviceType", "touchScreen");
7324 prepareDisplay(DISPLAY_ORIENTATION_0);
7325 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007326 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007327
7328 NotifyMotionArgs motionArgs;
7329
7330 // default tool type is finger
7331 processId(mapper, 1);
7332 processPosition(mapper, 100, 200);
7333 processSync(mapper);
7334 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7335 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7336 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7337
7338 // eraser
7339 processKey(mapper, BTN_TOOL_RUBBER, 1);
7340 processSync(mapper);
7341 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7342 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7343 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
7344
7345 // stylus
7346 processKey(mapper, BTN_TOOL_RUBBER, 0);
7347 processKey(mapper, BTN_TOOL_PEN, 1);
7348 processSync(mapper);
7349 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7350 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7351 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7352
7353 // brush
7354 processKey(mapper, BTN_TOOL_PEN, 0);
7355 processKey(mapper, BTN_TOOL_BRUSH, 1);
7356 processSync(mapper);
7357 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7358 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7359 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7360
7361 // pencil
7362 processKey(mapper, BTN_TOOL_BRUSH, 0);
7363 processKey(mapper, BTN_TOOL_PENCIL, 1);
7364 processSync(mapper);
7365 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7366 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7367 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7368
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08007369 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08007370 processKey(mapper, BTN_TOOL_PENCIL, 0);
7371 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
7372 processSync(mapper);
7373 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7374 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7375 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7376
7377 // mouse
7378 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
7379 processKey(mapper, BTN_TOOL_MOUSE, 1);
7380 processSync(mapper);
7381 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7382 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7383 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
7384
7385 // lens
7386 processKey(mapper, BTN_TOOL_MOUSE, 0);
7387 processKey(mapper, BTN_TOOL_LENS, 1);
7388 processSync(mapper);
7389 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7390 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7391 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
7392
7393 // double-tap
7394 processKey(mapper, BTN_TOOL_LENS, 0);
7395 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
7396 processSync(mapper);
7397 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7398 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7399 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7400
7401 // triple-tap
7402 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
7403 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
7404 processSync(mapper);
7405 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7406 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7407 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7408
7409 // quad-tap
7410 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
7411 processKey(mapper, BTN_TOOL_QUADTAP, 1);
7412 processSync(mapper);
7413 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7414 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7415 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7416
7417 // finger
7418 processKey(mapper, BTN_TOOL_QUADTAP, 0);
7419 processKey(mapper, BTN_TOOL_FINGER, 1);
7420 processSync(mapper);
7421 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7422 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7423 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7424
7425 // stylus trumps finger
7426 processKey(mapper, BTN_TOOL_PEN, 1);
7427 processSync(mapper);
7428 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7429 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7430 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7431
7432 // eraser trumps stylus
7433 processKey(mapper, BTN_TOOL_RUBBER, 1);
7434 processSync(mapper);
7435 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7436 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7437 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
7438
7439 // mouse trumps eraser
7440 processKey(mapper, BTN_TOOL_MOUSE, 1);
7441 processSync(mapper);
7442 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7443 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7444 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
7445
7446 // MT tool type trumps BTN tool types: MT_TOOL_FINGER
7447 processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
7448 processSync(mapper);
7449 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7450 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7451 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7452
7453 // MT tool type trumps BTN tool types: MT_TOOL_PEN
7454 processToolType(mapper, MT_TOOL_PEN);
7455 processSync(mapper);
7456 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7457 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7458 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7459
7460 // back to default tool type
7461 processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
7462 processKey(mapper, BTN_TOOL_MOUSE, 0);
7463 processKey(mapper, BTN_TOOL_RUBBER, 0);
7464 processKey(mapper, BTN_TOOL_PEN, 0);
7465 processKey(mapper, BTN_TOOL_FINGER, 0);
7466 processSync(mapper);
7467 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7468 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7469 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7470}
7471
7472TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007473 addConfigurationProperty("touch.deviceType", "touchScreen");
7474 prepareDisplay(DISPLAY_ORIENTATION_0);
7475 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007476 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007477 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007478
7479 NotifyMotionArgs motionArgs;
7480
7481 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
7482 processId(mapper, 1);
7483 processPosition(mapper, 100, 200);
7484 processSync(mapper);
7485 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7486 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
7487 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7488 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
7489
7490 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7491 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7492 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7493 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
7494
7495 // move a little
7496 processPosition(mapper, 150, 250);
7497 processSync(mapper);
7498 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7499 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7500 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7501 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7502
7503 // down when BTN_TOUCH is pressed, pressure defaults to 1
7504 processKey(mapper, BTN_TOUCH, 1);
7505 processSync(mapper);
7506 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7507 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
7508 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7509 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7510
7511 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7512 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7513 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7514 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
7515
7516 // up when BTN_TOUCH is released, hover restored
7517 processKey(mapper, BTN_TOUCH, 0);
7518 processSync(mapper);
7519 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7520 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7521 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7522 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
7523
7524 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7525 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
7526 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7527 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7528
7529 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7530 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7531 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7532 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7533
7534 // exit hover when pointer goes away
7535 processId(mapper, -1);
7536 processSync(mapper);
7537 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7538 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
7539 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7540 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7541}
7542
7543TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007544 addConfigurationProperty("touch.deviceType", "touchScreen");
7545 prepareDisplay(DISPLAY_ORIENTATION_0);
7546 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007547 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007548
7549 NotifyMotionArgs motionArgs;
7550
7551 // initially hovering because pressure is 0
7552 processId(mapper, 1);
7553 processPosition(mapper, 100, 200);
7554 processPressure(mapper, 0);
7555 processSync(mapper);
7556 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7557 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
7558 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7559 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
7560
7561 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7562 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7563 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7564 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
7565
7566 // move a little
7567 processPosition(mapper, 150, 250);
7568 processSync(mapper);
7569 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7570 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7571 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7572 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7573
7574 // down when pressure becomes non-zero
7575 processPressure(mapper, RAW_PRESSURE_MAX);
7576 processSync(mapper);
7577 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7578 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
7579 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7580 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7581
7582 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7583 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7584 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7585 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
7586
7587 // up when pressure becomes 0, hover restored
7588 processPressure(mapper, 0);
7589 processSync(mapper);
7590 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7591 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7592 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7593 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
7594
7595 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7596 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
7597 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7598 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7599
7600 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7601 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7602 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7603 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7604
7605 // exit hover when pointer goes away
7606 processId(mapper, -1);
7607 processSync(mapper);
7608 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7609 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
7610 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7611 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7612}
7613
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07007614/**
7615 * Set the input device port <--> display port associations, and check that the
7616 * events are routed to the display that matches the display port.
7617 * This can be checked by looking at the displayId of the resulting NotifyMotionArgs.
7618 */
7619TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayPort) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07007620 const std::string usb2 = "USB2";
7621 const uint8_t hdmi1 = 0;
7622 const uint8_t hdmi2 = 1;
7623 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007624 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07007625
7626 addConfigurationProperty("touch.deviceType", "touchScreen");
7627 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007628 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07007629
7630 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
7631 mFakePolicy->addInputPortAssociation(usb2, hdmi2);
7632
7633 // We are intentionally not adding the viewport for display 1 yet. Since the port association
7634 // for this input device is specified, and the matching viewport is not present,
7635 // the input device should be disabled (at the mapper level).
7636
7637 // Add viewport for display 2 on hdmi2
7638 prepareSecondaryDisplay(type, hdmi2);
7639 // Send a touch event
7640 processPosition(mapper, 100, 100);
7641 processSync(mapper);
7642 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7643
7644 // Add viewport for display 1 on hdmi1
7645 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
7646 // Send a touch event again
7647 processPosition(mapper, 100, 100);
7648 processSync(mapper);
7649
7650 NotifyMotionArgs args;
7651 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7652 ASSERT_EQ(DISPLAY_ID, args.displayId);
7653}
Michael Wrightd02c5b62014-02-10 15:10:22 -08007654
Arthur Hungc7ad2d02018-12-18 17:41:29 +08007655TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShouldHandleDisplayId) {
Garfield Tan888a6a42020-01-09 11:39:16 -08007656 // Setup for second display.
Michael Wright17db18e2020-06-26 20:51:44 +01007657 std::shared_ptr<FakePointerController> fakePointerController =
7658 std::make_shared<FakePointerController>();
Garfield Tan888a6a42020-01-09 11:39:16 -08007659 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08007660 fakePointerController->setPosition(100, 200);
7661 fakePointerController->setButtonState(0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00007662 mFakePolicy->setPointerController(fakePointerController);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08007663
Garfield Tan888a6a42020-01-09 11:39:16 -08007664 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007665 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08007666
Arthur Hungc7ad2d02018-12-18 17:41:29 +08007667 prepareDisplay(DISPLAY_ORIENTATION_0);
7668 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007669 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08007670
7671 // Check source is mouse that would obtain the PointerController.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007672 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08007673
7674 NotifyMotionArgs motionArgs;
7675 processPosition(mapper, 100, 100);
7676 processSync(mapper);
7677
7678 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7679 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7680 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
7681}
7682
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00007683/**
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007684 * Ensure that the readTime is set to the SYN_REPORT value when processing touch events.
7685 */
7686TEST_F(MultiTouchInputMapperTest, Process_SendsReadTime) {
7687 addConfigurationProperty("touch.deviceType", "touchScreen");
7688 prepareAxes(POSITION);
7689 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7690
7691 prepareDisplay(DISPLAY_ORIENTATION_0);
7692 process(mapper, 10, 11 /*readTime*/, EV_ABS, ABS_MT_TRACKING_ID, 1);
7693 process(mapper, 15, 16 /*readTime*/, EV_ABS, ABS_MT_POSITION_X, 100);
7694 process(mapper, 20, 21 /*readTime*/, EV_ABS, ABS_MT_POSITION_Y, 100);
7695 process(mapper, 25, 26 /*readTime*/, EV_SYN, SYN_REPORT, 0);
7696
7697 NotifyMotionArgs args;
7698 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7699 ASSERT_EQ(26, args.readTime);
7700
7701 process(mapper, 30, 31 /*readTime*/, EV_ABS, ABS_MT_POSITION_X, 110);
7702 process(mapper, 30, 32 /*readTime*/, EV_ABS, ABS_MT_POSITION_Y, 220);
7703 process(mapper, 30, 33 /*readTime*/, EV_SYN, SYN_REPORT, 0);
7704
7705 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7706 ASSERT_EQ(33, args.readTime);
7707}
7708
7709/**
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00007710 * When the viewport is not active (isActive=false), the touch mapper should be disabled and the
7711 * events should not be delivered to the listener.
7712 */
7713TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreDropped) {
7714 addConfigurationProperty("touch.deviceType", "touchScreen");
7715 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
7716 DISPLAY_ORIENTATION_0, false /*isActive*/, UNIQUE_ID, NO_PORT,
7717 ViewportType::INTERNAL);
7718 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
7719 prepareAxes(POSITION);
7720 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7721
7722 NotifyMotionArgs motionArgs;
7723 processPosition(mapper, 100, 100);
7724 processSync(mapper);
7725
7726 mFakeListener->assertNotifyMotionWasNotCalled();
7727}
7728
Garfield Tanc734e4f2021-01-15 20:01:39 -08007729TEST_F(MultiTouchInputMapperTest, Process_DeactivateViewport_AbortTouches) {
7730 addConfigurationProperty("touch.deviceType", "touchScreen");
7731 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
7732 DISPLAY_ORIENTATION_0, true /*isActive*/, UNIQUE_ID, NO_PORT,
7733 ViewportType::INTERNAL);
7734 std::optional<DisplayViewport> optionalDisplayViewport =
7735 mFakePolicy->getDisplayViewportByUniqueId(UNIQUE_ID);
7736 ASSERT_TRUE(optionalDisplayViewport.has_value());
7737 DisplayViewport displayViewport = *optionalDisplayViewport;
7738
7739 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
7740 prepareAxes(POSITION);
7741 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7742
7743 // Finger down
7744 int32_t x = 100, y = 100;
7745 processPosition(mapper, x, y);
7746 processSync(mapper);
7747
7748 NotifyMotionArgs motionArgs;
7749 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7750 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7751
7752 // Deactivate display viewport
7753 displayViewport.isActive = false;
7754 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
7755 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
7756
7757 // Finger move
7758 x += 10, y += 10;
7759 processPosition(mapper, x, y);
7760 processSync(mapper);
7761
7762 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7763 EXPECT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
7764
7765 // Reactivate display viewport
7766 displayViewport.isActive = true;
7767 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
7768 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
7769
7770 // Finger move again
7771 x += 10, y += 10;
7772 processPosition(mapper, x, y);
7773 processSync(mapper);
7774
7775 // Gesture is aborted, so events after display is activated won't be dispatched until there is
7776 // no pointer on the touch device.
7777 mFakeListener->assertNotifyMotionWasNotCalled();
7778}
7779
Arthur Hung7c645402019-01-25 17:45:42 +08007780TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShowTouches) {
7781 // Setup the first touch screen device.
Arthur Hung7c645402019-01-25 17:45:42 +08007782 prepareAxes(POSITION | ID | SLOT);
7783 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007784 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung7c645402019-01-25 17:45:42 +08007785
7786 // Create the second touch screen device, and enable multi fingers.
7787 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08007788 const std::string DEVICE_NAME2 = "TOUCHSCREEN2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08007789 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007790 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08007791 std::shared_ptr<InputDevice> device2 =
7792 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
7793 Flags<InputDeviceClass>(0));
7794
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007795 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
7796 0 /*flat*/, 0 /*fuzz*/);
7797 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
7798 0 /*flat*/, 0 /*fuzz*/);
7799 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX,
7800 0 /*flat*/, 0 /*fuzz*/);
7801 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX,
7802 0 /*flat*/, 0 /*fuzz*/);
7803 mFakeEventHub->setAbsoluteAxisValue(SECOND_EVENTHUB_ID, ABS_MT_SLOT, 0 /*value*/);
7804 mFakeEventHub->addConfigurationProperty(SECOND_EVENTHUB_ID, String8("touch.deviceType"),
7805 String8("touchScreen"));
Arthur Hung7c645402019-01-25 17:45:42 +08007806
7807 // Setup the second touch screen device.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007808 MultiTouchInputMapper& mapper2 = device2->addMapper<MultiTouchInputMapper>(SECOND_EVENTHUB_ID);
Arthur Hung7c645402019-01-25 17:45:42 +08007809 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
7810 device2->reset(ARBITRARY_TIME);
7811
7812 // Setup PointerController.
Michael Wright17db18e2020-06-26 20:51:44 +01007813 std::shared_ptr<FakePointerController> fakePointerController =
7814 std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00007815 mFakePolicy->setPointerController(fakePointerController);
Arthur Hung7c645402019-01-25 17:45:42 +08007816
7817 // Setup policy for associated displays and show touches.
7818 const uint8_t hdmi1 = 0;
7819 const uint8_t hdmi2 = 1;
7820 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
7821 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
7822 mFakePolicy->setShowTouches(true);
7823
7824 // Create displays.
7825 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007826 prepareSecondaryDisplay(ViewportType::EXTERNAL, hdmi2);
Arthur Hung7c645402019-01-25 17:45:42 +08007827
7828 // Default device will reconfigure above, need additional reconfiguration for another device.
7829 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007830 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung7c645402019-01-25 17:45:42 +08007831
7832 // Two fingers down at default display.
7833 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7834 processPosition(mapper, x1, y1);
7835 processId(mapper, 1);
7836 processSlot(mapper, 1);
7837 processPosition(mapper, x2, y2);
7838 processId(mapper, 2);
7839 processSync(mapper);
7840
7841 std::map<int32_t, std::vector<int32_t>>::const_iterator iter =
7842 fakePointerController->getSpots().find(DISPLAY_ID);
7843 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
7844 ASSERT_EQ(size_t(2), iter->second.size());
7845
7846 // Two fingers down at second display.
7847 processPosition(mapper2, x1, y1);
7848 processId(mapper2, 1);
7849 processSlot(mapper2, 1);
7850 processPosition(mapper2, x2, y2);
7851 processId(mapper2, 2);
7852 processSync(mapper2);
7853
7854 iter = fakePointerController->getSpots().find(SECONDARY_DISPLAY_ID);
7855 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
7856 ASSERT_EQ(size_t(2), iter->second.size());
7857}
7858
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06007859TEST_F(MultiTouchInputMapperTest, VideoFrames_ReceivedByListener) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06007860 prepareAxes(POSITION);
7861 addConfigurationProperty("touch.deviceType", "touchScreen");
7862 prepareDisplay(DISPLAY_ORIENTATION_0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007863 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06007864
7865 NotifyMotionArgs motionArgs;
7866 // Unrotated video frame
7867 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
7868 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007869 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06007870 processPosition(mapper, 100, 200);
7871 processSync(mapper);
7872 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7873 ASSERT_EQ(frames, motionArgs.videoFrames);
7874
7875 // Subsequent touch events should not have any videoframes
7876 // This is implemented separately in FakeEventHub,
7877 // but that should match the behaviour of TouchVideoDevice.
7878 processPosition(mapper, 200, 200);
7879 processSync(mapper);
7880 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7881 ASSERT_EQ(std::vector<TouchVideoFrame>(), motionArgs.videoFrames);
7882}
7883
Prabir Pradhanc14266f2021-05-12 15:56:24 -07007884TEST_F(MultiTouchInputMapperTest, VideoFrames_AreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007885 prepareAxes(POSITION);
7886 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007887 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007888 // Unrotated video frame
7889 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
7890 NotifyMotionArgs motionArgs;
7891
7892 // Test all 4 orientations
7893 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
Prabir Pradhanc14266f2021-05-12 15:56:24 -07007894 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
7895 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
7896 clearViewports();
7897 prepareDisplay(orientation);
7898 std::vector<TouchVideoFrame> frames{frame};
7899 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
7900 processPosition(mapper, 100, 200);
7901 processSync(mapper);
7902 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7903 ASSERT_EQ(frames, motionArgs.videoFrames);
7904 }
7905}
7906
7907TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_AreRotated) {
7908 prepareAxes(POSITION);
7909 addConfigurationProperty("touch.deviceType", "touchScreen");
7910 // Since InputReader works in the un-rotated coordinate space, only devices that are not
7911 // orientation-aware are affected by display rotation.
7912 addConfigurationProperty("touch.orientationAware", "0");
7913 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7914 // Unrotated video frame
7915 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
7916 NotifyMotionArgs motionArgs;
7917
7918 // Test all 4 orientations
7919 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007920 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
7921 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
7922 clearViewports();
7923 prepareDisplay(orientation);
7924 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007925 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007926 processPosition(mapper, 100, 200);
7927 processSync(mapper);
7928 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07007929 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
7930 // compared to the display. This is so that when the window transform (which contains the
7931 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
7932 // window's coordinate space.
7933 frames[0].rotate(getInverseRotation(orientation));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007934 ASSERT_EQ(frames, motionArgs.videoFrames);
7935 }
7936}
7937
Prabir Pradhanc14266f2021-05-12 15:56:24 -07007938TEST_F(MultiTouchInputMapperTest, VideoFrames_MultipleFramesAreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007939 prepareAxes(POSITION);
7940 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007941 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007942 // Unrotated video frames. There's no rule that they must all have the same dimensions,
7943 // so mix these.
7944 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
7945 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
7946 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
7947 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
7948 NotifyMotionArgs motionArgs;
7949
7950 prepareDisplay(DISPLAY_ORIENTATION_90);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007951 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007952 processPosition(mapper, 100, 200);
7953 processSync(mapper);
7954 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07007955 ASSERT_EQ(frames, motionArgs.videoFrames);
7956}
7957
7958TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_MultipleFramesAreRotated) {
7959 prepareAxes(POSITION);
7960 addConfigurationProperty("touch.deviceType", "touchScreen");
7961 // Since InputReader works in the un-rotated coordinate space, only devices that are not
7962 // orientation-aware are affected by display rotation.
7963 addConfigurationProperty("touch.orientationAware", "0");
7964 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7965 // Unrotated video frames. There's no rule that they must all have the same dimensions,
7966 // so mix these.
7967 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
7968 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
7969 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
7970 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
7971 NotifyMotionArgs motionArgs;
7972
7973 prepareDisplay(DISPLAY_ORIENTATION_90);
7974 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
7975 processPosition(mapper, 100, 200);
7976 processSync(mapper);
7977 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7978 std::for_each(frames.begin(), frames.end(), [](TouchVideoFrame& frame) {
7979 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
7980 // compared to the display. This is so that when the window transform (which contains the
7981 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
7982 // window's coordinate space.
7983 frame.rotate(getInverseRotation(DISPLAY_ORIENTATION_90));
7984 });
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007985 ASSERT_EQ(frames, motionArgs.videoFrames);
7986}
7987
Arthur Hung9da14732019-09-02 16:16:58 +08007988/**
7989 * If we had defined port associations, but the viewport is not ready, the touch device would be
7990 * expected to be disabled, and it should be enabled after the viewport has found.
7991 */
7992TEST_F(MultiTouchInputMapperTest, Configure_EnabledForAssociatedDisplay) {
Arthur Hung9da14732019-09-02 16:16:58 +08007993 constexpr uint8_t hdmi2 = 1;
7994 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007995 constexpr ViewportType type = ViewportType::EXTERNAL;
Arthur Hung9da14732019-09-02 16:16:58 +08007996
7997 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi2);
7998
7999 addConfigurationProperty("touch.deviceType", "touchScreen");
8000 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008001 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung9da14732019-09-02 16:16:58 +08008002
8003 ASSERT_EQ(mDevice->isEnabled(), false);
8004
8005 // Add display on hdmi2, the device should be enabled and can receive touch event.
8006 prepareSecondaryDisplay(type, hdmi2);
8007 ASSERT_EQ(mDevice->isEnabled(), true);
8008
8009 // Send a touch event.
8010 processPosition(mapper, 100, 100);
8011 processSync(mapper);
8012
8013 NotifyMotionArgs args;
8014 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8015 ASSERT_EQ(SECONDARY_DISPLAY_ID, args.displayId);
8016}
8017
Arthur Hung421eb1c2020-01-16 00:09:42 +08008018TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleSingleTouch) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08008019 addConfigurationProperty("touch.deviceType", "touchScreen");
8020 prepareDisplay(DISPLAY_ORIENTATION_0);
8021 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008022 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08008023
8024 NotifyMotionArgs motionArgs;
8025
8026 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
8027 // finger down
8028 processId(mapper, 1);
8029 processPosition(mapper, x1, y1);
8030 processSync(mapper);
8031 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8032 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8033 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8034
8035 // finger move
8036 processId(mapper, 1);
8037 processPosition(mapper, x2, y2);
8038 processSync(mapper);
8039 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8040 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8041 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8042
8043 // finger up.
8044 processId(mapper, -1);
8045 processSync(mapper);
8046 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8047 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8048 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8049
8050 // new finger down
8051 processId(mapper, 1);
8052 processPosition(mapper, x3, y3);
8053 processSync(mapper);
8054 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8055 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8056 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8057}
8058
8059/**
arthurhungcc7f9802020-04-30 17:55:40 +08008060 * Test single touch should be canceled when received the MT_TOOL_PALM event, and the following
8061 * MOVE and UP events should be ignored.
Arthur Hung421eb1c2020-01-16 00:09:42 +08008062 */
arthurhungcc7f9802020-04-30 17:55:40 +08008063TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_SinglePointer) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08008064 addConfigurationProperty("touch.deviceType", "touchScreen");
8065 prepareDisplay(DISPLAY_ORIENTATION_0);
8066 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008067 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08008068
8069 NotifyMotionArgs motionArgs;
8070
8071 // default tool type is finger
8072 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
arthurhungcc7f9802020-04-30 17:55:40 +08008073 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008074 processPosition(mapper, x1, y1);
8075 processSync(mapper);
8076 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8077 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8078 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8079
8080 // Tool changed to MT_TOOL_PALM expect sending the cancel event.
8081 processToolType(mapper, MT_TOOL_PALM);
8082 processSync(mapper);
8083 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8084 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
8085
8086 // Ignore the following MOVE and UP events if had detect a palm event.
arthurhungcc7f9802020-04-30 17:55:40 +08008087 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008088 processPosition(mapper, x2, y2);
8089 processSync(mapper);
8090 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8091
8092 // finger up.
arthurhungcc7f9802020-04-30 17:55:40 +08008093 processId(mapper, INVALID_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008094 processSync(mapper);
8095 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8096
8097 // new finger down
arthurhungcc7f9802020-04-30 17:55:40 +08008098 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008099 processToolType(mapper, MT_TOOL_FINGER);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008100 processPosition(mapper, x3, y3);
8101 processSync(mapper);
8102 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8103 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8104 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8105}
8106
arthurhungbf89a482020-04-17 17:37:55 +08008107/**
arthurhungcc7f9802020-04-30 17:55:40 +08008108 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
8109 * and the rest active fingers could still be allowed to receive the events
arthurhungbf89a482020-04-17 17:37:55 +08008110 */
arthurhungcc7f9802020-04-30 17:55:40 +08008111TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_TwoPointers) {
arthurhungbf89a482020-04-17 17:37:55 +08008112 addConfigurationProperty("touch.deviceType", "touchScreen");
8113 prepareDisplay(DISPLAY_ORIENTATION_0);
8114 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
8115 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8116
8117 NotifyMotionArgs motionArgs;
8118
8119 // default tool type is finger
arthurhungcc7f9802020-04-30 17:55:40 +08008120 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
8121 processId(mapper, FIRST_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08008122 processPosition(mapper, x1, y1);
8123 processSync(mapper);
8124 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8125 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8126 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8127
8128 // Second finger down.
arthurhungcc7f9802020-04-30 17:55:40 +08008129 processSlot(mapper, SECOND_SLOT);
8130 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08008131 processPosition(mapper, x2, y2);
arthurhungcc7f9802020-04-30 17:55:40 +08008132 processSync(mapper);
8133 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8134 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
8135 motionArgs.action);
8136 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8137
8138 // If the tool type of the first finger changes to MT_TOOL_PALM,
8139 // we expect to receive ACTION_POINTER_UP with cancel flag.
8140 processSlot(mapper, FIRST_SLOT);
8141 processId(mapper, FIRST_TRACKING_ID);
8142 processToolType(mapper, MT_TOOL_PALM);
8143 processSync(mapper);
8144 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8145 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
8146 motionArgs.action);
8147 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
8148
8149 // The following MOVE events of second finger should be processed.
8150 processSlot(mapper, SECOND_SLOT);
8151 processId(mapper, SECOND_TRACKING_ID);
8152 processPosition(mapper, x2 + 1, y2 + 1);
8153 processSync(mapper);
8154 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8155 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8156 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8157
8158 // First finger up. It used to be in palm mode, and we already generated ACTION_POINTER_UP for
8159 // it. Second finger receive move.
8160 processSlot(mapper, FIRST_SLOT);
8161 processId(mapper, INVALID_TRACKING_ID);
8162 processSync(mapper);
8163 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8164 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8165 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8166
8167 // Second finger keeps moving.
8168 processSlot(mapper, SECOND_SLOT);
8169 processId(mapper, SECOND_TRACKING_ID);
8170 processPosition(mapper, x2 + 2, y2 + 2);
8171 processSync(mapper);
8172 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8173 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8174 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8175
8176 // Second finger up.
8177 processId(mapper, INVALID_TRACKING_ID);
8178 processSync(mapper);
8179 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8180 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8181 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
8182}
8183
8184/**
8185 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event, if only 1 finger
8186 * is active, it should send CANCEL after receiving the MT_TOOL_PALM event.
8187 */
8188TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_ShouldCancelWhenAllTouchIsPalm) {
8189 addConfigurationProperty("touch.deviceType", "touchScreen");
8190 prepareDisplay(DISPLAY_ORIENTATION_0);
8191 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
8192 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8193
8194 NotifyMotionArgs motionArgs;
8195
8196 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
8197 // First finger down.
8198 processId(mapper, FIRST_TRACKING_ID);
8199 processPosition(mapper, x1, y1);
8200 processSync(mapper);
8201 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8202 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8203 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8204
8205 // Second finger down.
8206 processSlot(mapper, SECOND_SLOT);
8207 processId(mapper, SECOND_TRACKING_ID);
8208 processPosition(mapper, x2, y2);
arthurhungbf89a482020-04-17 17:37:55 +08008209 processSync(mapper);
8210 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8211 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
8212 motionArgs.action);
8213 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8214
arthurhungcc7f9802020-04-30 17:55:40 +08008215 // If the tool type of the first finger changes to MT_TOOL_PALM,
8216 // we expect to receive ACTION_POINTER_UP with cancel flag.
8217 processSlot(mapper, FIRST_SLOT);
8218 processId(mapper, FIRST_TRACKING_ID);
8219 processToolType(mapper, MT_TOOL_PALM);
8220 processSync(mapper);
8221 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8222 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
8223 motionArgs.action);
8224 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
8225
8226 // Second finger keeps moving.
8227 processSlot(mapper, SECOND_SLOT);
8228 processId(mapper, SECOND_TRACKING_ID);
8229 processPosition(mapper, x2 + 1, y2 + 1);
8230 processSync(mapper);
8231 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8232 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8233
8234 // second finger becomes palm, receive cancel due to only 1 finger is active.
8235 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08008236 processToolType(mapper, MT_TOOL_PALM);
8237 processSync(mapper);
8238 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8239 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
8240
arthurhungcc7f9802020-04-30 17:55:40 +08008241 // third finger down.
8242 processSlot(mapper, THIRD_SLOT);
8243 processId(mapper, THIRD_TRACKING_ID);
8244 processToolType(mapper, MT_TOOL_FINGER);
arthurhungbf89a482020-04-17 17:37:55 +08008245 processPosition(mapper, x3, y3);
8246 processSync(mapper);
arthurhungbf89a482020-04-17 17:37:55 +08008247 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8248 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8249 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08008250 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8251
8252 // third finger move
8253 processId(mapper, THIRD_TRACKING_ID);
8254 processPosition(mapper, x3 + 1, y3 + 1);
8255 processSync(mapper);
8256 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8257 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8258
8259 // first finger up, third finger receive move.
8260 processSlot(mapper, FIRST_SLOT);
8261 processId(mapper, INVALID_TRACKING_ID);
8262 processSync(mapper);
8263 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8264 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8265 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8266
8267 // second finger up, third finger receive move.
8268 processSlot(mapper, SECOND_SLOT);
8269 processId(mapper, INVALID_TRACKING_ID);
8270 processSync(mapper);
8271 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8272 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8273 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8274
8275 // third finger up.
8276 processSlot(mapper, THIRD_SLOT);
8277 processId(mapper, INVALID_TRACKING_ID);
8278 processSync(mapper);
8279 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8280 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8281 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
8282}
8283
8284/**
8285 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
8286 * and the active finger could still be allowed to receive the events
8287 */
8288TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_KeepFirstPointer) {
8289 addConfigurationProperty("touch.deviceType", "touchScreen");
8290 prepareDisplay(DISPLAY_ORIENTATION_0);
8291 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
8292 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8293
8294 NotifyMotionArgs motionArgs;
8295
8296 // default tool type is finger
8297 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
8298 processId(mapper, FIRST_TRACKING_ID);
8299 processPosition(mapper, x1, y1);
8300 processSync(mapper);
8301 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8302 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8303 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8304
8305 // Second finger down.
8306 processSlot(mapper, SECOND_SLOT);
8307 processId(mapper, SECOND_TRACKING_ID);
8308 processPosition(mapper, x2, y2);
8309 processSync(mapper);
8310 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8311 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
8312 motionArgs.action);
8313 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8314
8315 // If the tool type of the second finger changes to MT_TOOL_PALM,
8316 // we expect to receive ACTION_POINTER_UP with cancel flag.
8317 processId(mapper, SECOND_TRACKING_ID);
8318 processToolType(mapper, MT_TOOL_PALM);
8319 processSync(mapper);
8320 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8321 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
8322 motionArgs.action);
8323 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
8324
8325 // The following MOVE event should be processed.
8326 processSlot(mapper, FIRST_SLOT);
8327 processId(mapper, FIRST_TRACKING_ID);
8328 processPosition(mapper, x1 + 1, y1 + 1);
8329 processSync(mapper);
8330 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8331 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8332 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8333
8334 // second finger up.
8335 processSlot(mapper, SECOND_SLOT);
8336 processId(mapper, INVALID_TRACKING_ID);
8337 processSync(mapper);
8338 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8339 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8340
8341 // first finger keep moving
8342 processSlot(mapper, FIRST_SLOT);
8343 processId(mapper, FIRST_TRACKING_ID);
8344 processPosition(mapper, x1 + 2, y1 + 2);
8345 processSync(mapper);
8346 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8347 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8348
8349 // first finger up.
8350 processId(mapper, INVALID_TRACKING_ID);
8351 processSync(mapper);
8352 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8353 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8354 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
arthurhungbf89a482020-04-17 17:37:55 +08008355}
8356
Arthur Hung9ad18942021-06-19 02:04:46 +00008357/**
8358 * Test multi-touch should sent ACTION_POINTER_UP/ACTION_UP when received the INVALID_TRACKING_ID,
8359 * to prevent the driver side may send unexpected data after set tracking id as INVALID_TRACKING_ID
8360 * cause slot be valid again.
8361 */
8362TEST_F(MultiTouchInputMapperTest, Process_MultiTouch_WithInvalidTrackingId) {
8363 addConfigurationProperty("touch.deviceType", "touchScreen");
8364 prepareDisplay(DISPLAY_ORIENTATION_0);
8365 prepareAxes(POSITION | ID | SLOT | PRESSURE);
8366 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8367
8368 NotifyMotionArgs motionArgs;
8369
8370 constexpr int32_t x1 = 100, y1 = 200, x2 = 0, y2 = 0;
8371 // First finger down.
8372 processId(mapper, FIRST_TRACKING_ID);
8373 processPosition(mapper, x1, y1);
8374 processPressure(mapper, RAW_PRESSURE_MAX);
8375 processSync(mapper);
8376 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8377 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8378 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8379
8380 // First finger move.
8381 processId(mapper, FIRST_TRACKING_ID);
8382 processPosition(mapper, x1 + 1, y1 + 1);
8383 processPressure(mapper, RAW_PRESSURE_MAX);
8384 processSync(mapper);
8385 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8386 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8387 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8388
8389 // Second finger down.
8390 processSlot(mapper, SECOND_SLOT);
8391 processId(mapper, SECOND_TRACKING_ID);
8392 processPosition(mapper, x2, y2);
8393 processPressure(mapper, RAW_PRESSURE_MAX);
8394 processSync(mapper);
8395 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8396 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
8397 motionArgs.action);
8398 ASSERT_EQ(uint32_t(2), motionArgs.pointerCount);
8399
8400 // second finger up with some unexpected data.
8401 processSlot(mapper, SECOND_SLOT);
8402 processId(mapper, INVALID_TRACKING_ID);
8403 processPosition(mapper, x2, y2);
8404 processSync(mapper);
8405 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8406 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
8407 motionArgs.action);
8408 ASSERT_EQ(uint32_t(2), motionArgs.pointerCount);
8409
8410 // first finger up with some unexpected data.
8411 processSlot(mapper, FIRST_SLOT);
8412 processId(mapper, INVALID_TRACKING_ID);
8413 processPosition(mapper, x2, y2);
8414 processPressure(mapper, RAW_PRESSURE_MAX);
8415 processSync(mapper);
8416 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8417 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8418 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8419}
8420
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008421// --- MultiTouchInputMapperTest_ExternalDevice ---
8422
8423class MultiTouchInputMapperTest_ExternalDevice : public MultiTouchInputMapperTest {
8424protected:
Chris Yea52ade12020-08-27 16:49:20 -07008425 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008426};
8427
8428/**
8429 * Expect fallback to internal viewport if device is external and external viewport is not present.
8430 */
8431TEST_F(MultiTouchInputMapperTest_ExternalDevice, Viewports_Fallback) {
8432 prepareAxes(POSITION);
8433 addConfigurationProperty("touch.deviceType", "touchScreen");
8434 prepareDisplay(DISPLAY_ORIENTATION_0);
8435 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8436
8437 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
8438
8439 NotifyMotionArgs motionArgs;
8440
8441 // Expect the event to be sent to the internal viewport,
8442 // because an external viewport is not present.
8443 processPosition(mapper, 100, 100);
8444 processSync(mapper);
8445 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8446 ASSERT_EQ(ADISPLAY_ID_DEFAULT, motionArgs.displayId);
8447
8448 // Expect the event to be sent to the external viewport if it is present.
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008449 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008450 processPosition(mapper, 100, 100);
8451 processSync(mapper);
8452 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8453 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
8454}
Arthur Hung4197f6b2020-03-16 15:39:59 +08008455
8456/**
8457 * Test touch should not work if outside of surface.
8458 */
8459class MultiTouchInputMapperTest_SurfaceRange : public MultiTouchInputMapperTest {
8460protected:
8461 void halfDisplayToCenterHorizontal(int32_t orientation) {
8462 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008463 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Arthur Hung4197f6b2020-03-16 15:39:59 +08008464
8465 // Half display to (width/4, 0, width * 3/4, height) to make display has offset.
8466 internalViewport->orientation = orientation;
8467 if (orientation == DISPLAY_ORIENTATION_90 || orientation == DISPLAY_ORIENTATION_270) {
8468 internalViewport->logicalLeft = 0;
8469 internalViewport->logicalTop = 0;
8470 internalViewport->logicalRight = DISPLAY_HEIGHT;
8471 internalViewport->logicalBottom = DISPLAY_WIDTH / 2;
8472
8473 internalViewport->physicalLeft = 0;
8474 internalViewport->physicalTop = DISPLAY_WIDTH / 4;
8475 internalViewport->physicalRight = DISPLAY_HEIGHT;
8476 internalViewport->physicalBottom = DISPLAY_WIDTH * 3 / 4;
8477
8478 internalViewport->deviceWidth = DISPLAY_HEIGHT;
8479 internalViewport->deviceHeight = DISPLAY_WIDTH;
8480 } else {
8481 internalViewport->logicalLeft = 0;
8482 internalViewport->logicalTop = 0;
8483 internalViewport->logicalRight = DISPLAY_WIDTH / 2;
8484 internalViewport->logicalBottom = DISPLAY_HEIGHT;
8485
8486 internalViewport->physicalLeft = DISPLAY_WIDTH / 4;
8487 internalViewport->physicalTop = 0;
8488 internalViewport->physicalRight = DISPLAY_WIDTH * 3 / 4;
8489 internalViewport->physicalBottom = DISPLAY_HEIGHT;
8490
8491 internalViewport->deviceWidth = DISPLAY_WIDTH;
8492 internalViewport->deviceHeight = DISPLAY_HEIGHT;
8493 }
8494
8495 mFakePolicy->updateViewport(internalViewport.value());
8496 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8497 }
8498
arthurhung5d547942020-12-14 17:04:45 +08008499 void processPositionAndVerify(MultiTouchInputMapper& mapper, int32_t xOutside, int32_t yOutside,
8500 int32_t xInside, int32_t yInside, int32_t xExpected,
Arthur Hung4197f6b2020-03-16 15:39:59 +08008501 int32_t yExpected) {
8502 // touch on outside area should not work.
8503 processPosition(mapper, toRawX(xOutside), toRawY(yOutside));
8504 processSync(mapper);
8505 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8506
8507 // touch on inside area should receive the event.
8508 NotifyMotionArgs args;
8509 processPosition(mapper, toRawX(xInside), toRawY(yInside));
8510 processSync(mapper);
8511 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8512 ASSERT_NEAR(xExpected, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
8513 ASSERT_NEAR(yExpected, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
8514
8515 // Reset.
8516 mapper.reset(ARBITRARY_TIME);
8517 }
8518};
8519
arthurhung5d547942020-12-14 17:04:45 +08008520TEST_F(MultiTouchInputMapperTest_SurfaceRange, Viewports_SurfaceRange) {
Arthur Hung4197f6b2020-03-16 15:39:59 +08008521 addConfigurationProperty("touch.deviceType", "touchScreen");
8522 prepareDisplay(DISPLAY_ORIENTATION_0);
8523 prepareAxes(POSITION);
8524 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8525
8526 // Touch on center of normal display should work.
8527 const int32_t x = DISPLAY_WIDTH / 4;
8528 const int32_t y = DISPLAY_HEIGHT / 2;
8529 processPosition(mapper, toRawX(x), toRawY(y));
8530 processSync(mapper);
8531 NotifyMotionArgs args;
8532 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8533 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], x, y, 1.0f, 0.0f, 0.0f, 0.0f,
8534 0.0f, 0.0f, 0.0f, 0.0f));
8535 // Reset.
8536 mapper.reset(ARBITRARY_TIME);
8537
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008538 // Let physical display be different to device, and make surface and physical could be 1:1 in
8539 // all four orientations.
8540 for (int orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90, DISPLAY_ORIENTATION_180,
8541 DISPLAY_ORIENTATION_270}) {
8542 halfDisplayToCenterHorizontal(orientation);
Arthur Hung4197f6b2020-03-16 15:39:59 +08008543
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008544 const int32_t xExpected = (x + 1) - (DISPLAY_WIDTH / 4);
8545 const int32_t yExpected = y;
8546 processPositionAndVerify(mapper, x - 1, y, x + 1, y, xExpected, yExpected);
8547 }
Arthur Hung4197f6b2020-03-16 15:39:59 +08008548}
8549
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008550TEST_F(MultiTouchInputMapperTest_SurfaceRange, Viewports_SurfaceRange_90_NotOrientationAware) {
Arthur Hung4197f6b2020-03-16 15:39:59 +08008551 addConfigurationProperty("touch.deviceType", "touchScreen");
8552 prepareDisplay(DISPLAY_ORIENTATION_0);
8553 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008554 // Since InputReader works in the un-rotated coordinate space, only devices that are not
8555 // orientation-aware are affected by display rotation.
8556 addConfigurationProperty("touch.orientationAware", "0");
Arthur Hung4197f6b2020-03-16 15:39:59 +08008557 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8558
8559 // Half display to (width/4, 0, width * 3/4, height) and rotate 90-degrees.
8560 halfDisplayToCenterHorizontal(DISPLAY_ORIENTATION_90);
8561
8562 const int32_t x = DISPLAY_WIDTH / 4;
8563 const int32_t y = DISPLAY_HEIGHT / 2;
8564
Arthur Hung4197f6b2020-03-16 15:39:59 +08008565 // expect x/y = swap x/y then reverse x.
8566 constexpr int32_t xExpected = DISPLAY_HEIGHT - y;
8567 constexpr int32_t yExpected = (x + 1) - DISPLAY_WIDTH / 4;
8568 processPositionAndVerify(mapper, x - 1, y, x + 1, y, xExpected, yExpected);
8569}
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08008570
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008571TEST_F(MultiTouchInputMapperTest_SurfaceRange, Viewports_SurfaceRange_270_NotOrientationAware) {
arthurhunga36b28e2020-12-29 20:28:15 +08008572 addConfigurationProperty("touch.deviceType", "touchScreen");
8573 prepareDisplay(DISPLAY_ORIENTATION_0);
8574 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008575 // Since InputReader works in the un-rotated coordinate space, only devices that are not
8576 // orientation-aware are affected by display rotation.
8577 addConfigurationProperty("touch.orientationAware", "0");
8578 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8579
8580 // Half display to (width/4, 0, width * 3/4, height) and rotate 270-degrees.
8581 halfDisplayToCenterHorizontal(DISPLAY_ORIENTATION_270);
8582
8583 const int32_t x = DISPLAY_WIDTH / 4;
8584 const int32_t y = DISPLAY_HEIGHT / 2;
8585
8586 // expect x/y = swap x/y then reverse y.
8587 const int32_t xExpected = y;
8588 const int32_t yExpected = (DISPLAY_WIDTH * 3 / 4) - (x + 1);
8589 processPositionAndVerify(mapper, x - 1, y, x + 1, y, xExpected, yExpected);
8590}
8591
8592TEST_F(MultiTouchInputMapperTest_SurfaceRange, Viewports_SurfaceRange_Corner_NotOrientationAware) {
8593 addConfigurationProperty("touch.deviceType", "touchScreen");
8594 prepareDisplay(DISPLAY_ORIENTATION_0);
8595 prepareAxes(POSITION);
8596 // Since InputReader works in the un-rotated coordinate space, only devices that are not
8597 // orientation-aware are affected by display rotation.
8598 addConfigurationProperty("touch.orientationAware", "0");
arthurhunga36b28e2020-12-29 20:28:15 +08008599 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8600
8601 const int32_t x = 0;
8602 const int32_t y = 0;
8603
8604 const int32_t xExpected = x;
8605 const int32_t yExpected = y;
8606 processPositionAndVerify(mapper, x - 1, y, x, y, xExpected, yExpected);
8607
8608 clearViewports();
8609 prepareDisplay(DISPLAY_ORIENTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008610 // expect x/y = swap x/y then reverse x.
8611 const int32_t xExpected90 = DISPLAY_HEIGHT - 1;
8612 const int32_t yExpected90 = x;
arthurhunga36b28e2020-12-29 20:28:15 +08008613 processPositionAndVerify(mapper, x - 1, y, x, y, xExpected90, yExpected90);
8614
8615 clearViewports();
8616 prepareDisplay(DISPLAY_ORIENTATION_270);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008617 // expect x/y = swap x/y then reverse y.
8618 const int32_t xExpected270 = y;
8619 const int32_t yExpected270 = DISPLAY_WIDTH - 1;
arthurhunga36b28e2020-12-29 20:28:15 +08008620 processPositionAndVerify(mapper, x - 1, y, x, y, xExpected270, yExpected270);
8621}
8622
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08008623TEST_F(MultiTouchInputMapperTest, Process_TouchpadCapture) {
8624 // we need a pointer controller for mouse mode of touchpad (start pointer at 0,0)
8625 std::shared_ptr<FakePointerController> fakePointerController =
8626 std::make_shared<FakePointerController>();
8627 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
8628 fakePointerController->setPosition(0, 0);
8629 fakePointerController->setButtonState(0);
8630
8631 // prepare device and capture
8632 prepareDisplay(DISPLAY_ORIENTATION_0);
8633 prepareAxes(POSITION | ID | SLOT);
8634 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
8635 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
8636 mFakePolicy->setPointerCapture(true);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00008637 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08008638 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8639
8640 // captured touchpad should be a touchpad source
8641 NotifyDeviceResetArgs resetArgs;
8642 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
8643 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
8644
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00008645 InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
Chris Yef74dc422020-09-02 22:41:50 -07008646
8647 const InputDeviceInfo::MotionRange* relRangeX =
8648 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_X, AINPUT_SOURCE_TOUCHPAD);
8649 ASSERT_NE(relRangeX, nullptr);
8650 ASSERT_EQ(relRangeX->min, -(RAW_X_MAX - RAW_X_MIN));
8651 ASSERT_EQ(relRangeX->max, RAW_X_MAX - RAW_X_MIN);
8652 const InputDeviceInfo::MotionRange* relRangeY =
8653 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_Y, AINPUT_SOURCE_TOUCHPAD);
8654 ASSERT_NE(relRangeY, nullptr);
8655 ASSERT_EQ(relRangeY->min, -(RAW_Y_MAX - RAW_Y_MIN));
8656 ASSERT_EQ(relRangeY->max, RAW_Y_MAX - RAW_Y_MIN);
8657
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08008658 // run captured pointer tests - note that this is unscaled, so input listener events should be
8659 // identical to what the hardware sends (accounting for any
8660 // calibration).
8661 // FINGER 0 DOWN
Chris Ye364fdb52020-08-05 15:07:56 -07008662 processSlot(mapper, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08008663 processId(mapper, 1);
8664 processPosition(mapper, 100 + RAW_X_MIN, 100 + RAW_Y_MIN);
8665 processKey(mapper, BTN_TOUCH, 1);
8666 processSync(mapper);
8667
8668 // expect coord[0] to contain initial location of touch 0
8669 NotifyMotionArgs args;
8670 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8671 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
8672 ASSERT_EQ(1U, args.pointerCount);
8673 ASSERT_EQ(0, args.pointerProperties[0].id);
8674 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, args.source);
8675 ASSERT_NO_FATAL_FAILURE(
8676 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
8677
8678 // FINGER 1 DOWN
8679 processSlot(mapper, 1);
8680 processId(mapper, 2);
8681 processPosition(mapper, 560 + RAW_X_MIN, 154 + RAW_Y_MIN);
8682 processSync(mapper);
8683
8684 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
8685 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Chris Ye364fdb52020-08-05 15:07:56 -07008686 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
8687 args.action);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08008688 ASSERT_EQ(2U, args.pointerCount);
8689 ASSERT_EQ(0, args.pointerProperties[0].id);
8690 ASSERT_EQ(1, args.pointerProperties[1].id);
8691 ASSERT_NO_FATAL_FAILURE(
8692 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
8693 ASSERT_NO_FATAL_FAILURE(
8694 assertPointerCoords(args.pointerCoords[1], 560, 154, 1, 0, 0, 0, 0, 0, 0, 0));
8695
8696 // FINGER 1 MOVE
8697 processPosition(mapper, 540 + RAW_X_MIN, 690 + RAW_Y_MIN);
8698 processSync(mapper);
8699
8700 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
8701 // from move
8702 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8703 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
8704 ASSERT_NO_FATAL_FAILURE(
8705 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
8706 ASSERT_NO_FATAL_FAILURE(
8707 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
8708
8709 // FINGER 0 MOVE
8710 processSlot(mapper, 0);
8711 processPosition(mapper, 50 + RAW_X_MIN, 800 + RAW_Y_MIN);
8712 processSync(mapper);
8713
8714 // expect coord[0] to contain new touch 0 location, coord[1] to contain previous location
8715 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8716 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
8717 ASSERT_NO_FATAL_FAILURE(
8718 assertPointerCoords(args.pointerCoords[0], 50, 800, 1, 0, 0, 0, 0, 0, 0, 0));
8719 ASSERT_NO_FATAL_FAILURE(
8720 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
8721
8722 // BUTTON DOWN
8723 processKey(mapper, BTN_LEFT, 1);
8724 processSync(mapper);
8725
8726 // touchinputmapper design sends a move before button press
8727 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8728 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
8729 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8730 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
8731
8732 // BUTTON UP
8733 processKey(mapper, BTN_LEFT, 0);
8734 processSync(mapper);
8735
8736 // touchinputmapper design sends a move after button release
8737 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8738 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
8739 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8740 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
8741
8742 // FINGER 0 UP
8743 processId(mapper, -1);
8744 processSync(mapper);
8745 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8746 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | 0x0000, args.action);
8747
8748 // FINGER 1 MOVE
8749 processSlot(mapper, 1);
8750 processPosition(mapper, 320 + RAW_X_MIN, 900 + RAW_Y_MIN);
8751 processSync(mapper);
8752
8753 // expect coord[0] to contain new location of touch 1, and properties[0].id to contain 1
8754 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8755 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
8756 ASSERT_EQ(1U, args.pointerCount);
8757 ASSERT_EQ(1, args.pointerProperties[0].id);
8758 ASSERT_NO_FATAL_FAILURE(
8759 assertPointerCoords(args.pointerCoords[0], 320, 900, 1, 0, 0, 0, 0, 0, 0, 0));
8760
8761 // FINGER 1 UP
8762 processId(mapper, -1);
8763 processKey(mapper, BTN_TOUCH, 0);
8764 processSync(mapper);
8765 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8766 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
8767
8768 // non captured touchpad should be a mouse source
8769 mFakePolicy->setPointerCapture(false);
8770 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
8771 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
8772 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
8773}
8774
8775TEST_F(MultiTouchInputMapperTest, Process_UnCapturedTouchpadPointer) {
8776 std::shared_ptr<FakePointerController> fakePointerController =
8777 std::make_shared<FakePointerController>();
8778 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
8779 fakePointerController->setPosition(0, 0);
8780 fakePointerController->setButtonState(0);
8781
8782 // prepare device and capture
8783 prepareDisplay(DISPLAY_ORIENTATION_0);
8784 prepareAxes(POSITION | ID | SLOT);
8785 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
8786 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00008787 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08008788 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8789 // run uncaptured pointer tests - pushes out generic events
8790 // FINGER 0 DOWN
8791 processId(mapper, 3);
8792 processPosition(mapper, 100, 100);
8793 processKey(mapper, BTN_TOUCH, 1);
8794 processSync(mapper);
8795
8796 // start at (100,100), cursor should be at (0,0) * scale
8797 NotifyMotionArgs args;
8798 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8799 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
8800 ASSERT_NO_FATAL_FAILURE(
8801 assertPointerCoords(args.pointerCoords[0], 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
8802
8803 // FINGER 0 MOVE
8804 processPosition(mapper, 200, 200);
8805 processSync(mapper);
8806
8807 // compute scaling to help with touch position checking
8808 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
8809 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
8810 float scale =
8811 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
8812
8813 // translate from (100,100) -> (200,200), cursor should have changed to (100,100) * scale)
8814 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8815 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
8816 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 100 * scale, 100 * scale, 0,
8817 0, 0, 0, 0, 0, 0, 0));
8818}
8819
8820TEST_F(MultiTouchInputMapperTest, WhenCapturedAndNotCaptured_GetSources) {
8821 std::shared_ptr<FakePointerController> fakePointerController =
8822 std::make_shared<FakePointerController>();
8823
8824 prepareDisplay(DISPLAY_ORIENTATION_0);
8825 prepareAxes(POSITION | ID | SLOT);
8826 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00008827 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08008828 mFakePolicy->setPointerCapture(false);
8829 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8830
8831 // uncaptured touchpad should be a pointer device
8832 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
8833
8834 // captured touchpad should be a touchpad device
8835 mFakePolicy->setPointerCapture(true);
8836 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
8837 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
8838}
8839
Chris Ye1dd2e5c2021-04-04 23:12:41 -07008840// --- PeripheralControllerTest ---
Chris Yee2b1e5c2021-03-10 22:45:12 -08008841
Chris Ye1dd2e5c2021-04-04 23:12:41 -07008842class PeripheralControllerTest : public testing::Test {
Chris Yee2b1e5c2021-03-10 22:45:12 -08008843protected:
8844 static const char* DEVICE_NAME;
8845 static const char* DEVICE_LOCATION;
8846 static const int32_t DEVICE_ID;
8847 static const int32_t DEVICE_GENERATION;
8848 static const int32_t DEVICE_CONTROLLER_NUMBER;
8849 static const Flags<InputDeviceClass> DEVICE_CLASSES;
8850 static const int32_t EVENTHUB_ID;
8851
8852 std::shared_ptr<FakeEventHub> mFakeEventHub;
8853 sp<FakeInputReaderPolicy> mFakePolicy;
8854 sp<TestInputListener> mFakeListener;
8855 std::unique_ptr<InstrumentedInputReader> mReader;
8856 std::shared_ptr<InputDevice> mDevice;
8857
8858 virtual void SetUp(Flags<InputDeviceClass> classes) {
8859 mFakeEventHub = std::make_unique<FakeEventHub>();
8860 mFakePolicy = new FakeInputReaderPolicy();
8861 mFakeListener = new TestInputListener();
8862 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
8863 mFakeListener);
8864 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
8865 }
8866
8867 void SetUp() override { SetUp(DEVICE_CLASSES); }
8868
8869 void TearDown() override {
8870 mFakeListener.clear();
8871 mFakePolicy.clear();
8872 }
8873
8874 void configureDevice(uint32_t changes) {
8875 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
8876 mReader->requestRefreshConfiguration(changes);
8877 mReader->loopOnce();
8878 }
8879 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
8880 }
8881
8882 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
8883 const std::string& location, int32_t eventHubId,
8884 Flags<InputDeviceClass> classes) {
8885 InputDeviceIdentifier identifier;
8886 identifier.name = name;
8887 identifier.location = location;
8888 std::shared_ptr<InputDevice> device =
8889 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
8890 identifier);
8891 mReader->pushNextDevice(device);
8892 mFakeEventHub->addDevice(eventHubId, name, classes);
8893 mReader->loopOnce();
8894 return device;
8895 }
8896
8897 template <class T, typename... Args>
8898 T& addControllerAndConfigure(Args... args) {
8899 T& controller = mDevice->addController<T>(EVENTHUB_ID, args...);
8900
8901 return controller;
8902 }
8903};
8904
Chris Ye1dd2e5c2021-04-04 23:12:41 -07008905const char* PeripheralControllerTest::DEVICE_NAME = "device";
8906const char* PeripheralControllerTest::DEVICE_LOCATION = "BLUETOOTH";
8907const int32_t PeripheralControllerTest::DEVICE_ID = END_RESERVED_ID + 1000;
8908const int32_t PeripheralControllerTest::DEVICE_GENERATION = 2;
8909const int32_t PeripheralControllerTest::DEVICE_CONTROLLER_NUMBER = 0;
8910const Flags<InputDeviceClass> PeripheralControllerTest::DEVICE_CLASSES =
Chris Yee2b1e5c2021-03-10 22:45:12 -08008911 Flags<InputDeviceClass>(0); // not needed for current tests
Chris Ye1dd2e5c2021-04-04 23:12:41 -07008912const int32_t PeripheralControllerTest::EVENTHUB_ID = 1;
Chris Yee2b1e5c2021-03-10 22:45:12 -08008913
8914// --- BatteryControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -07008915class BatteryControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -08008916protected:
8917 void SetUp() override {
Chris Ye1dd2e5c2021-04-04 23:12:41 -07008918 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::BATTERY);
Chris Yee2b1e5c2021-03-10 22:45:12 -08008919 }
8920};
8921
8922TEST_F(BatteryControllerTest, GetBatteryCapacity) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -07008923 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08008924
8925 ASSERT_TRUE(controller.getBatteryCapacity(DEFAULT_BATTERY));
8926 ASSERT_EQ(controller.getBatteryCapacity(DEFAULT_BATTERY).value_or(-1), BATTERY_CAPACITY);
8927}
8928
8929TEST_F(BatteryControllerTest, GetBatteryStatus) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -07008930 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08008931
8932 ASSERT_TRUE(controller.getBatteryStatus(DEFAULT_BATTERY));
8933 ASSERT_EQ(controller.getBatteryStatus(DEFAULT_BATTERY).value_or(-1), BATTERY_STATUS);
8934}
8935
8936// --- LightControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -07008937class LightControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -08008938protected:
Chris Ye1dd2e5c2021-04-04 23:12:41 -07008939 void SetUp() override {
8940 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::LIGHT);
8941 }
Chris Yee2b1e5c2021-03-10 22:45:12 -08008942};
8943
Chris Ye85758332021-05-16 23:05:17 -07008944TEST_F(LightControllerTest, MonoLight) {
8945 RawLightInfo infoMono = {.id = 1,
8946 .name = "Mono",
8947 .maxBrightness = 255,
8948 .flags = InputLightClass::BRIGHTNESS,
8949 .path = ""};
8950 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
Chris Yee2b1e5c2021-03-10 22:45:12 -08008951
Chris Ye1dd2e5c2021-04-04 23:12:41 -07008952 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08008953 InputDeviceInfo info;
8954 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00008955 std::vector<InputDeviceLightInfo> lights = info.getLights();
8956 ASSERT_EQ(1U, lights.size());
8957 ASSERT_EQ(InputDeviceLightType::MONO, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -08008958
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00008959 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
8960 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
Chris Yee2b1e5c2021-03-10 22:45:12 -08008961}
8962
8963TEST_F(LightControllerTest, RGBLight) {
8964 RawLightInfo infoRed = {.id = 1,
8965 .name = "red",
8966 .maxBrightness = 255,
8967 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
8968 .path = ""};
8969 RawLightInfo infoGreen = {.id = 2,
8970 .name = "green",
8971 .maxBrightness = 255,
8972 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
8973 .path = ""};
8974 RawLightInfo infoBlue = {.id = 3,
8975 .name = "blue",
8976 .maxBrightness = 255,
8977 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
8978 .path = ""};
8979 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
8980 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
8981 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
8982
Chris Ye1dd2e5c2021-04-04 23:12:41 -07008983 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08008984 InputDeviceInfo info;
8985 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00008986 std::vector<InputDeviceLightInfo> lights = info.getLights();
8987 ASSERT_EQ(1U, lights.size());
8988 ASSERT_EQ(InputDeviceLightType::RGB, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -08008989
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00008990 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
8991 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -08008992}
8993
8994TEST_F(LightControllerTest, MultiColorRGBLight) {
8995 RawLightInfo infoColor = {.id = 1,
8996 .name = "red",
8997 .maxBrightness = 255,
8998 .flags = InputLightClass::BRIGHTNESS |
8999 InputLightClass::MULTI_INTENSITY |
9000 InputLightClass::MULTI_INDEX,
9001 .path = ""};
9002
9003 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
9004
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009005 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009006 InputDeviceInfo info;
9007 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009008 std::vector<InputDeviceLightInfo> lights = info.getLights();
9009 ASSERT_EQ(1U, lights.size());
9010 ASSERT_EQ(InputDeviceLightType::MULTI_COLOR, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009011
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009012 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
9013 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009014}
9015
9016TEST_F(LightControllerTest, PlayerIdLight) {
9017 RawLightInfo info1 = {.id = 1,
9018 .name = "player1",
9019 .maxBrightness = 255,
9020 .flags = InputLightClass::BRIGHTNESS,
9021 .path = ""};
9022 RawLightInfo info2 = {.id = 2,
9023 .name = "player2",
9024 .maxBrightness = 255,
9025 .flags = InputLightClass::BRIGHTNESS,
9026 .path = ""};
9027 RawLightInfo info3 = {.id = 3,
9028 .name = "player3",
9029 .maxBrightness = 255,
9030 .flags = InputLightClass::BRIGHTNESS,
9031 .path = ""};
9032 RawLightInfo info4 = {.id = 4,
9033 .name = "player4",
9034 .maxBrightness = 255,
9035 .flags = InputLightClass::BRIGHTNESS,
9036 .path = ""};
9037 mFakeEventHub->addRawLightInfo(info1.id, std::move(info1));
9038 mFakeEventHub->addRawLightInfo(info2.id, std::move(info2));
9039 mFakeEventHub->addRawLightInfo(info3.id, std::move(info3));
9040 mFakeEventHub->addRawLightInfo(info4.id, std::move(info4));
9041
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009042 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009043 InputDeviceInfo info;
9044 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009045 std::vector<InputDeviceLightInfo> lights = info.getLights();
9046 ASSERT_EQ(1U, lights.size());
9047 ASSERT_EQ(InputDeviceLightType::PLAYER_ID, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009048
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009049 ASSERT_FALSE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
9050 ASSERT_TRUE(controller.setLightPlayerId(lights[0].id, LIGHT_PLAYER_ID));
9051 ASSERT_EQ(controller.getLightPlayerId(lights[0].id).value_or(-1), LIGHT_PLAYER_ID);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009052}
9053
Michael Wrightd02c5b62014-02-10 15:10:22 -08009054} // namespace android