blob: 43f14bd340c22192742bef53893b1db5953a2624 [file] [log] [blame]
Michael Wrightd02c5b62014-02-10 15:10:22 -08001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Prabir Pradhan2770d242019-09-02 18:07:11 -070017#include <CursorInputMapper.h>
18#include <InputDevice.h>
Prabir Pradhanc14266f2021-05-12 15:56:24 -070019#include <InputFlingerProperties.sysprop.h>
Prabir Pradhan2770d242019-09-02 18:07:11 -070020#include <InputMapper.h>
21#include <InputReader.h>
Prabir Pradhan1aed8582019-12-30 11:46:51 -080022#include <InputReaderBase.h>
23#include <InputReaderFactory.h>
Prabir Pradhan2770d242019-09-02 18:07:11 -070024#include <KeyboardInputMapper.h>
25#include <MultiTouchInputMapper.h>
Chris Ye1dd2e5c2021-04-04 23:12:41 -070026#include <PeripheralController.h>
Chris Yef59a2f42020-10-16 12:55:26 -070027#include <SensorInputMapper.h>
Prabir Pradhan2770d242019-09-02 18:07:11 -070028#include <SingleTouchInputMapper.h>
29#include <SwitchInputMapper.h>
30#include <TestInputListener.h>
31#include <TouchInputMapper.h>
Prabir Pradhan1aed8582019-12-30 11:46:51 -080032#include <UinputDevice.h>
Chris Ye87143712020-11-10 05:05:58 +000033#include <VibratorInputMapper.h>
Prabir Pradhan2574dfa2019-10-16 16:35:07 -070034#include <android-base/thread_annotations.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080035#include <gtest/gtest.h>
chaviw3277faf2021-05-19 16:45:23 -050036#include <gui/constants.h>
Siarhei Vishniakou473174e2017-12-27 16:44:42 -080037#include <inttypes.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080038#include <math.h>
39
Michael Wright17db18e2020-06-26 20:51:44 +010040#include <memory>
Chris Ye3fdbfef2021-01-06 18:45:18 -080041#include <regex>
Michael Wrightdde67b82020-10-27 16:09:22 +000042#include "input/DisplayViewport.h"
43#include "input/Input.h"
Michael Wright17db18e2020-06-26 20:51:44 +010044
Michael Wrightd02c5b62014-02-10 15:10:22 -080045namespace android {
46
Prabir Pradhan2574dfa2019-10-16 16:35:07 -070047using std::chrono_literals::operator""ms;
Chris Ye1b0c7342020-07-28 21:57:03 -070048using namespace android::flag_operators;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -070049
50// Timeout for waiting for an expected event
51static constexpr std::chrono::duration WAIT_TIMEOUT = 100ms;
52
Michael Wrightd02c5b62014-02-10 15:10:22 -080053// An arbitrary time value.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +000054static constexpr nsecs_t ARBITRARY_TIME = 1234;
55static constexpr nsecs_t READ_TIME = 4321;
Michael Wrightd02c5b62014-02-10 15:10:22 -080056
57// Arbitrary display properties.
arthurhungcc7f9802020-04-30 17:55:40 +080058static constexpr int32_t DISPLAY_ID = 0;
59static constexpr int32_t SECONDARY_DISPLAY_ID = DISPLAY_ID + 1;
60static constexpr int32_t DISPLAY_WIDTH = 480;
61static constexpr int32_t DISPLAY_HEIGHT = 800;
62static constexpr int32_t VIRTUAL_DISPLAY_ID = 1;
63static constexpr int32_t VIRTUAL_DISPLAY_WIDTH = 400;
64static constexpr int32_t VIRTUAL_DISPLAY_HEIGHT = 500;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -070065static const char* VIRTUAL_DISPLAY_UNIQUE_ID = "virtual:1";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -070066static constexpr std::optional<uint8_t> NO_PORT = std::nullopt; // no physical port is specified
Michael Wrightd02c5b62014-02-10 15:10:22 -080067
arthurhungcc7f9802020-04-30 17:55:40 +080068static constexpr int32_t FIRST_SLOT = 0;
69static constexpr int32_t SECOND_SLOT = 1;
70static constexpr int32_t THIRD_SLOT = 2;
71static constexpr int32_t INVALID_TRACKING_ID = -1;
72static constexpr int32_t FIRST_TRACKING_ID = 0;
73static constexpr int32_t SECOND_TRACKING_ID = 1;
74static constexpr int32_t THIRD_TRACKING_ID = 2;
Chris Yee2b1e5c2021-03-10 22:45:12 -080075static constexpr int32_t DEFAULT_BATTERY = 1;
Kim Low03ea0352020-11-06 12:45:07 -080076static constexpr int32_t BATTERY_STATUS = 4;
77static constexpr int32_t BATTERY_CAPACITY = 66;
Chris Ye3fdbfef2021-01-06 18:45:18 -080078static constexpr int32_t LIGHT_BRIGHTNESS = 0x55000000;
79static constexpr int32_t LIGHT_COLOR = 0x7F448866;
80static constexpr int32_t LIGHT_PLAYER_ID = 2;
arthurhungcc7f9802020-04-30 17:55:40 +080081
Michael Wrightd02c5b62014-02-10 15:10:22 -080082// Error tolerance for floating point assertions.
83static const float EPSILON = 0.001f;
84
85template<typename T>
86static inline T min(T a, T b) {
87 return a < b ? a : b;
88}
89
90static inline float avg(float x, float y) {
91 return (x + y) / 2;
92}
93
Chris Ye3fdbfef2021-01-06 18:45:18 -080094// Mapping for light color name and the light color
95const std::unordered_map<std::string, LightColor> LIGHT_COLORS = {{"red", LightColor::RED},
96 {"green", LightColor::GREEN},
97 {"blue", LightColor::BLUE}};
Michael Wrightd02c5b62014-02-10 15:10:22 -080098
Prabir Pradhanc14266f2021-05-12 15:56:24 -070099static int32_t getInverseRotation(int32_t orientation) {
100 switch (orientation) {
101 case DISPLAY_ORIENTATION_90:
102 return DISPLAY_ORIENTATION_270;
103 case DISPLAY_ORIENTATION_270:
104 return DISPLAY_ORIENTATION_90;
105 default:
106 return orientation;
107 }
108}
109
Michael Wrightd02c5b62014-02-10 15:10:22 -0800110// --- FakePointerController ---
111
112class FakePointerController : public PointerControllerInterface {
113 bool mHaveBounds;
114 float mMinX, mMinY, mMaxX, mMaxY;
115 float mX, mY;
116 int32_t mButtonState;
Arthur Hungc7ad2d02018-12-18 17:41:29 +0800117 int32_t mDisplayId;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800118
Michael Wrightd02c5b62014-02-10 15:10:22 -0800119public:
120 FakePointerController() :
121 mHaveBounds(false), mMinX(0), mMinY(0), mMaxX(0), mMaxY(0), mX(0), mY(0),
Arthur Hungc7ad2d02018-12-18 17:41:29 +0800122 mButtonState(0), mDisplayId(ADISPLAY_ID_DEFAULT) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800123 }
124
Michael Wright17db18e2020-06-26 20:51:44 +0100125 virtual ~FakePointerController() {}
126
Michael Wrightd02c5b62014-02-10 15:10:22 -0800127 void setBounds(float minX, float minY, float maxX, float maxY) {
128 mHaveBounds = true;
129 mMinX = minX;
130 mMinY = minY;
131 mMaxX = maxX;
132 mMaxY = maxY;
133 }
134
Chris Yea52ade12020-08-27 16:49:20 -0700135 void setPosition(float x, float y) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800136 mX = x;
137 mY = y;
138 }
139
Chris Yea52ade12020-08-27 16:49:20 -0700140 void setButtonState(int32_t buttonState) override { mButtonState = buttonState; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800141
Chris Yea52ade12020-08-27 16:49:20 -0700142 int32_t getButtonState() const override { return mButtonState; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800143
Chris Yea52ade12020-08-27 16:49:20 -0700144 void getPosition(float* outX, float* outY) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800145 *outX = mX;
146 *outY = mY;
147 }
148
Chris Yea52ade12020-08-27 16:49:20 -0700149 int32_t getDisplayId() const override { return mDisplayId; }
Arthur Hungc7ad2d02018-12-18 17:41:29 +0800150
Chris Yea52ade12020-08-27 16:49:20 -0700151 void setDisplayViewport(const DisplayViewport& viewport) override {
Garfield Tan888a6a42020-01-09 11:39:16 -0800152 mDisplayId = viewport.displayId;
153 }
154
Arthur Hung7c645402019-01-25 17:45:42 +0800155 const std::map<int32_t, std::vector<int32_t>>& getSpots() {
156 return mSpotsByDisplay;
157 }
158
Michael Wrightd02c5b62014-02-10 15:10:22 -0800159private:
Chris Yea52ade12020-08-27 16:49:20 -0700160 bool getBounds(float* outMinX, float* outMinY, float* outMaxX, float* outMaxY) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800161 *outMinX = mMinX;
162 *outMinY = mMinY;
163 *outMaxX = mMaxX;
164 *outMaxY = mMaxY;
165 return mHaveBounds;
166 }
167
Chris Yea52ade12020-08-27 16:49:20 -0700168 void move(float deltaX, float deltaY) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800169 mX += deltaX;
170 if (mX < mMinX) mX = mMinX;
171 if (mX > mMaxX) mX = mMaxX;
172 mY += deltaY;
173 if (mY < mMinY) mY = mMinY;
174 if (mY > mMaxY) mY = mMaxY;
175 }
176
Chris Yea52ade12020-08-27 16:49:20 -0700177 void fade(Transition) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800178
Chris Yea52ade12020-08-27 16:49:20 -0700179 void unfade(Transition) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800180
Chris Yea52ade12020-08-27 16:49:20 -0700181 void setPresentation(Presentation) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800182
Chris Yea52ade12020-08-27 16:49:20 -0700183 void setSpots(const PointerCoords*, const uint32_t*, BitSet32 spotIdBits,
184 int32_t displayId) override {
Arthur Hung7c645402019-01-25 17:45:42 +0800185 std::vector<int32_t> newSpots;
186 // Add spots for fingers that are down.
187 for (BitSet32 idBits(spotIdBits); !idBits.isEmpty(); ) {
188 uint32_t id = idBits.clearFirstMarkedBit();
189 newSpots.push_back(id);
190 }
191
192 mSpotsByDisplay[displayId] = newSpots;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800193 }
194
Chris Yea52ade12020-08-27 16:49:20 -0700195 void clearSpots() override {}
Arthur Hung7c645402019-01-25 17:45:42 +0800196
197 std::map<int32_t, std::vector<int32_t>> mSpotsByDisplay;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800198};
199
200
201// --- FakeInputReaderPolicy ---
202
203class FakeInputReaderPolicy : public InputReaderPolicyInterface {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700204 std::mutex mLock;
205 std::condition_variable mDevicesChangedCondition;
206
Michael Wrightd02c5b62014-02-10 15:10:22 -0800207 InputReaderConfiguration mConfig;
Michael Wright17db18e2020-06-26 20:51:44 +0100208 std::unordered_map<int32_t, std::shared_ptr<FakePointerController>> mPointerControllers;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700209 std::vector<InputDeviceInfo> mInputDevices GUARDED_BY(mLock);
210 bool mInputDevicesChanged GUARDED_BY(mLock){false};
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100211 std::vector<DisplayViewport> mViewports;
Jason Gerecke489fda82012-09-07 17:19:40 -0700212 TouchAffineTransformation transform;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800213
214protected:
Chris Yea52ade12020-08-27 16:49:20 -0700215 virtual ~FakeInputReaderPolicy() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800216
217public:
218 FakeInputReaderPolicy() {
219 }
220
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700221 void assertInputDevicesChanged() {
Prabir Pradhan1aed8582019-12-30 11:46:51 -0800222 waitForInputDevices([](bool devicesChanged) {
223 if (!devicesChanged) {
224 FAIL() << "Timed out waiting for notifyInputDevicesChanged() to be called.";
225 }
226 });
227 }
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700228
Prabir Pradhan1aed8582019-12-30 11:46:51 -0800229 void assertInputDevicesNotChanged() {
230 waitForInputDevices([](bool devicesChanged) {
231 if (devicesChanged) {
232 FAIL() << "Expected notifyInputDevicesChanged() to not be called.";
233 }
234 });
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700235 }
236
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700237 virtual void clearViewports() {
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100238 mViewports.clear();
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100239 mConfig.setDisplayViewports(mViewports);
Santos Cordonfa5cf462017-04-05 10:37:00 -0700240 }
241
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700242 std::optional<DisplayViewport> getDisplayViewportByUniqueId(const std::string& uniqueId) const {
243 return mConfig.getDisplayViewportByUniqueId(uniqueId);
244 }
245 std::optional<DisplayViewport> getDisplayViewportByType(ViewportType type) const {
246 return mConfig.getDisplayViewportByType(type);
247 }
248
249 std::optional<DisplayViewport> getDisplayViewportByPort(uint8_t displayPort) const {
250 return mConfig.getDisplayViewportByPort(displayPort);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700251 }
252
253 void addDisplayViewport(int32_t displayId, int32_t width, int32_t height, int32_t orientation,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +0000254 bool isActive, const std::string& uniqueId,
255 std::optional<uint8_t> physicalPort, ViewportType viewportType) {
256 const DisplayViewport viewport =
257 createDisplayViewport(displayId, width, height, orientation, isActive, uniqueId,
258 physicalPort, viewportType);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700259 mViewports.push_back(viewport);
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100260 mConfig.setDisplayViewports(mViewports);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800261 }
262
Arthur Hung6cd19a42019-08-30 19:04:12 +0800263 bool updateViewport(const DisplayViewport& viewport) {
264 size_t count = mViewports.size();
265 for (size_t i = 0; i < count; i++) {
266 const DisplayViewport& currentViewport = mViewports[i];
267 if (currentViewport.displayId == viewport.displayId) {
268 mViewports[i] = viewport;
269 mConfig.setDisplayViewports(mViewports);
270 return true;
271 }
272 }
273 // no viewport found.
274 return false;
275 }
276
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100277 void addExcludedDeviceName(const std::string& deviceName) {
278 mConfig.excludedDeviceNames.push_back(deviceName);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800279 }
280
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700281 void addInputPortAssociation(const std::string& inputPort, uint8_t displayPort) {
282 mConfig.portAssociations.insert({inputPort, displayPort});
283 }
284
Christine Franks1ba71cc2021-04-07 14:37:42 -0700285 void addInputUniqueIdAssociation(const std::string& inputUniqueId,
286 const std::string& displayUniqueId) {
287 mConfig.uniqueIdAssociations.insert({inputUniqueId, displayUniqueId});
288 }
289
Siarhei Vishniakouc6f61192019-07-23 18:12:31 +0000290 void addDisabledDevice(int32_t deviceId) { mConfig.disabledDevices.insert(deviceId); }
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700291
Siarhei Vishniakouc6f61192019-07-23 18:12:31 +0000292 void removeDisabledDevice(int32_t deviceId) { mConfig.disabledDevices.erase(deviceId); }
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700293
Michael Wright17db18e2020-06-26 20:51:44 +0100294 void setPointerController(int32_t deviceId, std::shared_ptr<FakePointerController> controller) {
295 mPointerControllers.insert_or_assign(deviceId, std::move(controller));
Michael Wrightd02c5b62014-02-10 15:10:22 -0800296 }
297
298 const InputReaderConfiguration* getReaderConfiguration() const {
299 return &mConfig;
300 }
301
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800302 const std::vector<InputDeviceInfo>& getInputDevices() const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800303 return mInputDevices;
304 }
305
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100306 TouchAffineTransformation getTouchAffineTransformation(const std::string& inputDeviceDescriptor,
Jason Gerecke71b16e82014-03-10 09:47:59 -0700307 int32_t surfaceRotation) {
Jason Gerecke489fda82012-09-07 17:19:40 -0700308 return transform;
309 }
310
311 void setTouchAffineTransformation(const TouchAffineTransformation t) {
312 transform = t;
Jason Gerecke12d6baa2014-01-27 18:34:20 -0800313 }
314
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -0800315 void setPointerCapture(bool enabled) {
316 mConfig.pointerCapture = enabled;
317 }
318
Arthur Hung7c645402019-01-25 17:45:42 +0800319 void setShowTouches(bool enabled) {
320 mConfig.showTouches = enabled;
321 }
322
Garfield Tan888a6a42020-01-09 11:39:16 -0800323 void setDefaultPointerDisplayId(int32_t pointerDisplayId) {
324 mConfig.defaultPointerDisplayId = pointerDisplayId;
325 }
326
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -0800327 float getPointerGestureMovementSpeedRatio() { return mConfig.pointerGestureMovementSpeedRatio; }
328
Michael Wrightd02c5b62014-02-10 15:10:22 -0800329private:
Santos Cordonfa5cf462017-04-05 10:37:00 -0700330 DisplayViewport createDisplayViewport(int32_t displayId, int32_t width, int32_t height,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +0000331 int32_t orientation, bool isActive,
332 const std::string& uniqueId,
333 std::optional<uint8_t> physicalPort, ViewportType type) {
Santos Cordonfa5cf462017-04-05 10:37:00 -0700334 bool isRotated = (orientation == DISPLAY_ORIENTATION_90
335 || orientation == DISPLAY_ORIENTATION_270);
336 DisplayViewport v;
337 v.displayId = displayId;
338 v.orientation = orientation;
339 v.logicalLeft = 0;
340 v.logicalTop = 0;
341 v.logicalRight = isRotated ? height : width;
342 v.logicalBottom = isRotated ? width : height;
343 v.physicalLeft = 0;
344 v.physicalTop = 0;
345 v.physicalRight = isRotated ? height : width;
346 v.physicalBottom = isRotated ? width : height;
347 v.deviceWidth = isRotated ? height : width;
348 v.deviceHeight = isRotated ? width : height;
Siarhei Vishniakou6f778462020-12-09 23:39:07 +0000349 v.isActive = isActive;
Santos Cordonfa5cf462017-04-05 10:37:00 -0700350 v.uniqueId = uniqueId;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700351 v.physicalPort = physicalPort;
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100352 v.type = type;
Santos Cordonfa5cf462017-04-05 10:37:00 -0700353 return v;
354 }
355
Chris Yea52ade12020-08-27 16:49:20 -0700356 void getReaderConfiguration(InputReaderConfiguration* outConfig) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800357 *outConfig = mConfig;
358 }
359
Chris Yea52ade12020-08-27 16:49:20 -0700360 std::shared_ptr<PointerControllerInterface> obtainPointerController(int32_t deviceId) override {
Michael Wright17db18e2020-06-26 20:51:44 +0100361 return mPointerControllers[deviceId];
Michael Wrightd02c5b62014-02-10 15:10:22 -0800362 }
363
Chris Yea52ade12020-08-27 16:49:20 -0700364 void notifyInputDevicesChanged(const std::vector<InputDeviceInfo>& inputDevices) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700365 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800366 mInputDevices = inputDevices;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700367 mInputDevicesChanged = true;
368 mDevicesChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800369 }
370
Chris Yea52ade12020-08-27 16:49:20 -0700371 std::shared_ptr<KeyCharacterMap> getKeyboardLayoutOverlay(
372 const InputDeviceIdentifier&) override {
Yi Kong9b14ac62018-07-17 13:48:38 -0700373 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800374 }
375
Chris Yea52ade12020-08-27 16:49:20 -0700376 std::string getDeviceAlias(const InputDeviceIdentifier&) override { return ""; }
Prabir Pradhan1aed8582019-12-30 11:46:51 -0800377
378 void waitForInputDevices(std::function<void(bool)> processDevicesChanged) {
379 std::unique_lock<std::mutex> lock(mLock);
380 base::ScopedLockAssertion assumeLocked(mLock);
381
382 const bool devicesChanged =
383 mDevicesChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
384 return mInputDevicesChanged;
385 });
386 ASSERT_NO_FATAL_FAILURE(processDevicesChanged(devicesChanged));
387 mInputDevicesChanged = false;
388 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800389};
390
Michael Wrightd02c5b62014-02-10 15:10:22 -0800391// --- FakeEventHub ---
392
393class FakeEventHub : public EventHubInterface {
394 struct KeyInfo {
395 int32_t keyCode;
396 uint32_t flags;
397 };
398
Chris Yef59a2f42020-10-16 12:55:26 -0700399 struct SensorInfo {
400 InputDeviceSensorType sensorType;
401 int32_t sensorDataIndex;
402 };
403
Michael Wrightd02c5b62014-02-10 15:10:22 -0800404 struct Device {
405 InputDeviceIdentifier identifier;
Chris Ye1b0c7342020-07-28 21:57:03 -0700406 Flags<InputDeviceClass> classes;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800407 PropertyMap configuration;
408 KeyedVector<int, RawAbsoluteAxisInfo> absoluteAxes;
409 KeyedVector<int, bool> relativeAxes;
410 KeyedVector<int32_t, int32_t> keyCodeStates;
411 KeyedVector<int32_t, int32_t> scanCodeStates;
412 KeyedVector<int32_t, int32_t> switchStates;
413 KeyedVector<int32_t, int32_t> absoluteAxisValue;
414 KeyedVector<int32_t, KeyInfo> keysByScanCode;
415 KeyedVector<int32_t, KeyInfo> keysByUsageCode;
416 KeyedVector<int32_t, bool> leds;
Chris Yef59a2f42020-10-16 12:55:26 -0700417 std::unordered_map<int32_t, SensorInfo> sensorsByAbsCode;
418 BitArray<MSC_MAX> mscBitmask;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800419 std::vector<VirtualKeyDefinition> virtualKeys;
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700420 bool enabled;
421
422 status_t enable() {
423 enabled = true;
424 return OK;
425 }
426
427 status_t disable() {
428 enabled = false;
429 return OK;
430 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800431
Chris Ye1b0c7342020-07-28 21:57:03 -0700432 explicit Device(Flags<InputDeviceClass> classes) : classes(classes), enabled(true) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800433 };
434
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700435 std::mutex mLock;
436 std::condition_variable mEventsCondition;
437
Michael Wrightd02c5b62014-02-10 15:10:22 -0800438 KeyedVector<int32_t, Device*> mDevices;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100439 std::vector<std::string> mExcludedDevices;
Siarhei Vishniakou370039c2021-02-04 22:09:01 +0000440 std::vector<RawEvent> mEvents GUARDED_BY(mLock);
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600441 std::unordered_map<int32_t /*deviceId*/, std::vector<TouchVideoFrame>> mVideoFrames;
Chris Ye87143712020-11-10 05:05:58 +0000442 std::vector<int32_t> mVibrators = {0, 1};
Chris Ye3fdbfef2021-01-06 18:45:18 -0800443 std::unordered_map<int32_t, RawLightInfo> mRawLightInfos;
444 // Simulates a device light brightness, from light id to light brightness.
445 std::unordered_map<int32_t /* lightId */, int32_t /* brightness*/> mLightBrightness;
446 // Simulates a device light intensities, from light id to light intensities map.
447 std::unordered_map<int32_t /* lightId */, std::unordered_map<LightColor, int32_t>>
448 mLightIntensities;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800449
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700450public:
Michael Wrightd02c5b62014-02-10 15:10:22 -0800451 virtual ~FakeEventHub() {
452 for (size_t i = 0; i < mDevices.size(); i++) {
453 delete mDevices.valueAt(i);
454 }
455 }
456
Michael Wrightd02c5b62014-02-10 15:10:22 -0800457 FakeEventHub() { }
458
Chris Ye1b0c7342020-07-28 21:57:03 -0700459 void addDevice(int32_t deviceId, const std::string& name, Flags<InputDeviceClass> classes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800460 Device* device = new Device(classes);
461 device->identifier.name = name;
462 mDevices.add(deviceId, device);
463
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000464 enqueueEvent(ARBITRARY_TIME, READ_TIME, deviceId, EventHubInterface::DEVICE_ADDED, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800465 }
466
467 void removeDevice(int32_t deviceId) {
468 delete mDevices.valueFor(deviceId);
469 mDevices.removeItem(deviceId);
470
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000471 enqueueEvent(ARBITRARY_TIME, READ_TIME, deviceId, EventHubInterface::DEVICE_REMOVED, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800472 }
473
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700474 bool isDeviceEnabled(int32_t deviceId) {
475 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700476 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700477 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
478 return false;
479 }
480 return device->enabled;
481 }
482
483 status_t enableDevice(int32_t deviceId) {
484 status_t result;
485 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700486 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700487 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
488 return BAD_VALUE;
489 }
490 if (device->enabled) {
491 ALOGW("Duplicate call to %s, device %" PRId32 " already enabled", __func__, deviceId);
492 return OK;
493 }
494 result = device->enable();
495 return result;
496 }
497
498 status_t disableDevice(int32_t deviceId) {
499 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700500 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700501 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
502 return BAD_VALUE;
503 }
504 if (!device->enabled) {
505 ALOGW("Duplicate call to %s, device %" PRId32 " already disabled", __func__, deviceId);
506 return OK;
507 }
508 return device->disable();
509 }
510
Michael Wrightd02c5b62014-02-10 15:10:22 -0800511 void finishDeviceScan() {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000512 enqueueEvent(ARBITRARY_TIME, READ_TIME, 0, EventHubInterface::FINISHED_DEVICE_SCAN, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800513 }
514
515 void addConfigurationProperty(int32_t deviceId, const String8& key, const String8& value) {
516 Device* device = getDevice(deviceId);
517 device->configuration.addProperty(key, value);
518 }
519
520 void addConfigurationMap(int32_t deviceId, const PropertyMap* configuration) {
521 Device* device = getDevice(deviceId);
522 device->configuration.addAll(configuration);
523 }
524
525 void addAbsoluteAxis(int32_t deviceId, int axis,
526 int32_t minValue, int32_t maxValue, int flat, int fuzz, int resolution = 0) {
527 Device* device = getDevice(deviceId);
528
529 RawAbsoluteAxisInfo info;
530 info.valid = true;
531 info.minValue = minValue;
532 info.maxValue = maxValue;
533 info.flat = flat;
534 info.fuzz = fuzz;
535 info.resolution = resolution;
536 device->absoluteAxes.add(axis, info);
537 }
538
539 void addRelativeAxis(int32_t deviceId, int32_t axis) {
540 Device* device = getDevice(deviceId);
541 device->relativeAxes.add(axis, true);
542 }
543
544 void setKeyCodeState(int32_t deviceId, int32_t keyCode, int32_t state) {
545 Device* device = getDevice(deviceId);
546 device->keyCodeStates.replaceValueFor(keyCode, state);
547 }
548
549 void setScanCodeState(int32_t deviceId, int32_t scanCode, int32_t state) {
550 Device* device = getDevice(deviceId);
551 device->scanCodeStates.replaceValueFor(scanCode, state);
552 }
553
554 void setSwitchState(int32_t deviceId, int32_t switchCode, int32_t state) {
555 Device* device = getDevice(deviceId);
556 device->switchStates.replaceValueFor(switchCode, state);
557 }
558
559 void setAbsoluteAxisValue(int32_t deviceId, int32_t axis, int32_t value) {
560 Device* device = getDevice(deviceId);
561 device->absoluteAxisValue.replaceValueFor(axis, value);
562 }
563
564 void addKey(int32_t deviceId, int32_t scanCode, int32_t usageCode,
565 int32_t keyCode, uint32_t flags) {
566 Device* device = getDevice(deviceId);
567 KeyInfo info;
568 info.keyCode = keyCode;
569 info.flags = flags;
570 if (scanCode) {
571 device->keysByScanCode.add(scanCode, info);
572 }
573 if (usageCode) {
574 device->keysByUsageCode.add(usageCode, info);
575 }
576 }
577
578 void addLed(int32_t deviceId, int32_t led, bool initialState) {
579 Device* device = getDevice(deviceId);
580 device->leds.add(led, initialState);
581 }
582
Chris Yef59a2f42020-10-16 12:55:26 -0700583 void addSensorAxis(int32_t deviceId, int32_t absCode, InputDeviceSensorType sensorType,
584 int32_t sensorDataIndex) {
585 Device* device = getDevice(deviceId);
586 SensorInfo info;
587 info.sensorType = sensorType;
588 info.sensorDataIndex = sensorDataIndex;
589 device->sensorsByAbsCode.emplace(absCode, info);
590 }
591
592 void setMscEvent(int32_t deviceId, int32_t mscEvent) {
593 Device* device = getDevice(deviceId);
594 typename BitArray<MSC_MAX>::Buffer buffer;
595 buffer[mscEvent / 32] = 1 << mscEvent % 32;
596 device->mscBitmask.loadFromBuffer(buffer);
597 }
598
Chris Ye3fdbfef2021-01-06 18:45:18 -0800599 void addRawLightInfo(int32_t rawId, RawLightInfo&& info) {
600 mRawLightInfos.emplace(rawId, std::move(info));
601 }
602
603 void fakeLightBrightness(int32_t rawId, int32_t brightness) {
604 mLightBrightness.emplace(rawId, brightness);
605 }
606
607 void fakeLightIntensities(int32_t rawId,
608 const std::unordered_map<LightColor, int32_t> intensities) {
609 mLightIntensities.emplace(rawId, std::move(intensities));
610 }
611
Michael Wrightd02c5b62014-02-10 15:10:22 -0800612 bool getLedState(int32_t deviceId, int32_t led) {
613 Device* device = getDevice(deviceId);
614 return device->leds.valueFor(led);
615 }
616
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100617 std::vector<std::string>& getExcludedDevices() {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800618 return mExcludedDevices;
619 }
620
621 void addVirtualKeyDefinition(int32_t deviceId, const VirtualKeyDefinition& definition) {
622 Device* device = getDevice(deviceId);
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800623 device->virtualKeys.push_back(definition);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800624 }
625
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000626 void enqueueEvent(nsecs_t when, nsecs_t readTime, int32_t deviceId, int32_t type, int32_t code,
627 int32_t value) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700628 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800629 RawEvent event;
630 event.when = when;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000631 event.readTime = readTime;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800632 event.deviceId = deviceId;
633 event.type = type;
634 event.code = code;
635 event.value = value;
636 mEvents.push_back(event);
637
638 if (type == EV_ABS) {
639 setAbsoluteAxisValue(deviceId, code, value);
640 }
641 }
642
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600643 void setVideoFrames(std::unordered_map<int32_t /*deviceId*/,
644 std::vector<TouchVideoFrame>> videoFrames) {
645 mVideoFrames = std::move(videoFrames);
646 }
647
Michael Wrightd02c5b62014-02-10 15:10:22 -0800648 void assertQueueIsEmpty() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700649 std::unique_lock<std::mutex> lock(mLock);
650 base::ScopedLockAssertion assumeLocked(mLock);
651 const bool queueIsEmpty =
652 mEventsCondition.wait_for(lock, WAIT_TIMEOUT,
653 [this]() REQUIRES(mLock) { return mEvents.size() == 0; });
654 if (!queueIsEmpty) {
655 FAIL() << "Timed out waiting for EventHub queue to be emptied.";
656 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800657 }
658
659private:
660 Device* getDevice(int32_t deviceId) const {
661 ssize_t index = mDevices.indexOfKey(deviceId);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100662 return index >= 0 ? mDevices.valueAt(index) : nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800663 }
664
Chris Yea52ade12020-08-27 16:49:20 -0700665 Flags<InputDeviceClass> getDeviceClasses(int32_t deviceId) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800666 Device* device = getDevice(deviceId);
Chris Ye1b0c7342020-07-28 21:57:03 -0700667 return device ? device->classes : Flags<InputDeviceClass>(0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800668 }
669
Chris Yea52ade12020-08-27 16:49:20 -0700670 InputDeviceIdentifier getDeviceIdentifier(int32_t deviceId) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800671 Device* device = getDevice(deviceId);
672 return device ? device->identifier : InputDeviceIdentifier();
673 }
674
Chris Yea52ade12020-08-27 16:49:20 -0700675 int32_t getDeviceControllerNumber(int32_t) const override { return 0; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800676
Chris Yea52ade12020-08-27 16:49:20 -0700677 void getConfiguration(int32_t deviceId, PropertyMap* outConfiguration) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800678 Device* device = getDevice(deviceId);
679 if (device) {
680 *outConfiguration = device->configuration;
681 }
682 }
683
Chris Yea52ade12020-08-27 16:49:20 -0700684 status_t getAbsoluteAxisInfo(int32_t deviceId, int axis,
685 RawAbsoluteAxisInfo* outAxisInfo) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800686 Device* device = getDevice(deviceId);
Arthur Hung9da14732019-09-02 16:16:58 +0800687 if (device && device->enabled) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800688 ssize_t index = device->absoluteAxes.indexOfKey(axis);
689 if (index >= 0) {
690 *outAxisInfo = device->absoluteAxes.valueAt(index);
691 return OK;
692 }
693 }
694 outAxisInfo->clear();
695 return -1;
696 }
697
Chris Yea52ade12020-08-27 16:49:20 -0700698 bool hasRelativeAxis(int32_t deviceId, int axis) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800699 Device* device = getDevice(deviceId);
700 if (device) {
701 return device->relativeAxes.indexOfKey(axis) >= 0;
702 }
703 return false;
704 }
705
Chris Yea52ade12020-08-27 16:49:20 -0700706 bool hasInputProperty(int32_t, int) const override { return false; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800707
Chris Yef59a2f42020-10-16 12:55:26 -0700708 bool hasMscEvent(int32_t deviceId, int mscEvent) const override final {
709 Device* device = getDevice(deviceId);
710 if (device) {
711 return mscEvent >= 0 && mscEvent <= MSC_MAX ? device->mscBitmask.test(mscEvent) : false;
712 }
713 return false;
714 }
715
Chris Yea52ade12020-08-27 16:49:20 -0700716 status_t mapKey(int32_t deviceId, int32_t scanCode, int32_t usageCode, int32_t metaState,
717 int32_t* outKeycode, int32_t* outMetaState, uint32_t* outFlags) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800718 Device* device = getDevice(deviceId);
719 if (device) {
720 const KeyInfo* key = getKey(device, scanCode, usageCode);
721 if (key) {
722 if (outKeycode) {
723 *outKeycode = key->keyCode;
724 }
725 if (outFlags) {
726 *outFlags = key->flags;
727 }
Dmitry Torokhov0faaa0b2015-09-24 13:13:55 -0700728 if (outMetaState) {
729 *outMetaState = metaState;
730 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800731 return OK;
732 }
733 }
734 return NAME_NOT_FOUND;
735 }
736
737 const KeyInfo* getKey(Device* device, int32_t scanCode, int32_t usageCode) const {
738 if (usageCode) {
739 ssize_t index = device->keysByUsageCode.indexOfKey(usageCode);
740 if (index >= 0) {
741 return &device->keysByUsageCode.valueAt(index);
742 }
743 }
744 if (scanCode) {
745 ssize_t index = device->keysByScanCode.indexOfKey(scanCode);
746 if (index >= 0) {
747 return &device->keysByScanCode.valueAt(index);
748 }
749 }
Yi Kong9b14ac62018-07-17 13:48:38 -0700750 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800751 }
752
Chris Yea52ade12020-08-27 16:49:20 -0700753 status_t mapAxis(int32_t, int32_t, AxisInfo*) const override { return NAME_NOT_FOUND; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800754
Chris Yef59a2f42020-10-16 12:55:26 -0700755 base::Result<std::pair<InputDeviceSensorType, int32_t>> mapSensor(int32_t deviceId,
756 int32_t absCode) {
757 Device* device = getDevice(deviceId);
758 if (!device) {
759 return Errorf("Sensor device not found.");
760 }
761 auto it = device->sensorsByAbsCode.find(absCode);
762 if (it == device->sensorsByAbsCode.end()) {
763 return Errorf("Sensor map not found.");
764 }
765 const SensorInfo& info = it->second;
766 return std::make_pair(info.sensorType, info.sensorDataIndex);
767 }
768
Chris Yea52ade12020-08-27 16:49:20 -0700769 void setExcludedDevices(const std::vector<std::string>& devices) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800770 mExcludedDevices = devices;
771 }
772
Siarhei Vishniakou370039c2021-02-04 22:09:01 +0000773 size_t getEvents(int, RawEvent* buffer, size_t bufferSize) override {
774 std::scoped_lock lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800775
Siarhei Vishniakou370039c2021-02-04 22:09:01 +0000776 const size_t filledSize = std::min(mEvents.size(), bufferSize);
777 std::copy(mEvents.begin(), mEvents.begin() + filledSize, buffer);
778
779 mEvents.erase(mEvents.begin(), mEvents.begin() + filledSize);
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700780 mEventsCondition.notify_all();
Siarhei Vishniakou370039c2021-02-04 22:09:01 +0000781 return filledSize;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800782 }
783
Chris Yea52ade12020-08-27 16:49:20 -0700784 std::vector<TouchVideoFrame> getVideoFrames(int32_t deviceId) override {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600785 auto it = mVideoFrames.find(deviceId);
786 if (it != mVideoFrames.end()) {
787 std::vector<TouchVideoFrame> frames = std::move(it->second);
788 mVideoFrames.erase(deviceId);
789 return frames;
790 }
Siarhei Vishniakouadd89292018-12-13 19:23:36 -0800791 return {};
792 }
793
Chris Yea52ade12020-08-27 16:49:20 -0700794 int32_t getScanCodeState(int32_t deviceId, int32_t scanCode) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800795 Device* device = getDevice(deviceId);
796 if (device) {
797 ssize_t index = device->scanCodeStates.indexOfKey(scanCode);
798 if (index >= 0) {
799 return device->scanCodeStates.valueAt(index);
800 }
801 }
802 return AKEY_STATE_UNKNOWN;
803 }
804
Chris Yea52ade12020-08-27 16:49:20 -0700805 int32_t getKeyCodeState(int32_t deviceId, int32_t keyCode) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800806 Device* device = getDevice(deviceId);
807 if (device) {
808 ssize_t index = device->keyCodeStates.indexOfKey(keyCode);
809 if (index >= 0) {
810 return device->keyCodeStates.valueAt(index);
811 }
812 }
813 return AKEY_STATE_UNKNOWN;
814 }
815
Chris Yea52ade12020-08-27 16:49:20 -0700816 int32_t getSwitchState(int32_t deviceId, int32_t sw) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800817 Device* device = getDevice(deviceId);
818 if (device) {
819 ssize_t index = device->switchStates.indexOfKey(sw);
820 if (index >= 0) {
821 return device->switchStates.valueAt(index);
822 }
823 }
824 return AKEY_STATE_UNKNOWN;
825 }
826
Chris Yea52ade12020-08-27 16:49:20 -0700827 status_t getAbsoluteAxisValue(int32_t deviceId, int32_t axis,
828 int32_t* outValue) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800829 Device* device = getDevice(deviceId);
830 if (device) {
831 ssize_t index = device->absoluteAxisValue.indexOfKey(axis);
832 if (index >= 0) {
833 *outValue = device->absoluteAxisValue.valueAt(index);
834 return OK;
835 }
836 }
837 *outValue = 0;
838 return -1;
839 }
840
Chris Yea52ade12020-08-27 16:49:20 -0700841 // Return true if the device has non-empty key layout.
842 bool markSupportedKeyCodes(int32_t deviceId, size_t numCodes, const int32_t* keyCodes,
843 uint8_t* outFlags) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800844 bool result = false;
845 Device* device = getDevice(deviceId);
846 if (device) {
Chris Yea52ade12020-08-27 16:49:20 -0700847 result = device->keysByScanCode.size() > 0 || device->keysByUsageCode.size() > 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800848 for (size_t i = 0; i < numCodes; i++) {
849 for (size_t j = 0; j < device->keysByScanCode.size(); j++) {
850 if (keyCodes[i] == device->keysByScanCode.valueAt(j).keyCode) {
851 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800852 }
853 }
854 for (size_t j = 0; j < device->keysByUsageCode.size(); j++) {
855 if (keyCodes[i] == device->keysByUsageCode.valueAt(j).keyCode) {
856 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800857 }
858 }
859 }
860 }
861 return result;
862 }
863
Chris Yea52ade12020-08-27 16:49:20 -0700864 bool hasScanCode(int32_t deviceId, int32_t scanCode) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800865 Device* device = getDevice(deviceId);
866 if (device) {
867 ssize_t index = device->keysByScanCode.indexOfKey(scanCode);
868 return index >= 0;
869 }
870 return false;
871 }
872
Chris Yea52ade12020-08-27 16:49:20 -0700873 bool hasLed(int32_t deviceId, int32_t led) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800874 Device* device = getDevice(deviceId);
875 return device && device->leds.indexOfKey(led) >= 0;
876 }
877
Chris Yea52ade12020-08-27 16:49:20 -0700878 void setLedState(int32_t deviceId, int32_t led, bool on) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800879 Device* device = getDevice(deviceId);
880 if (device) {
881 ssize_t index = device->leds.indexOfKey(led);
882 if (index >= 0) {
883 device->leds.replaceValueAt(led, on);
884 } else {
885 ADD_FAILURE()
886 << "Attempted to set the state of an LED that the EventHub declared "
887 "was not present. led=" << led;
888 }
889 }
890 }
891
Chris Yea52ade12020-08-27 16:49:20 -0700892 void getVirtualKeyDefinitions(
893 int32_t deviceId, std::vector<VirtualKeyDefinition>& outVirtualKeys) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800894 outVirtualKeys.clear();
895
896 Device* device = getDevice(deviceId);
897 if (device) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800898 outVirtualKeys = device->virtualKeys;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800899 }
900 }
901
Chris Yea52ade12020-08-27 16:49:20 -0700902 const std::shared_ptr<KeyCharacterMap> getKeyCharacterMap(int32_t) const override {
Yi Kong9b14ac62018-07-17 13:48:38 -0700903 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800904 }
905
Chris Yea52ade12020-08-27 16:49:20 -0700906 bool setKeyboardLayoutOverlay(int32_t, std::shared_ptr<KeyCharacterMap>) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800907 return false;
908 }
909
Chris Yea52ade12020-08-27 16:49:20 -0700910 void vibrate(int32_t, const VibrationElement&) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800911
Chris Yea52ade12020-08-27 16:49:20 -0700912 void cancelVibrate(int32_t) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800913
Chris Ye87143712020-11-10 05:05:58 +0000914 std::vector<int32_t> getVibratorIds(int32_t deviceId) override { return mVibrators; };
915
Chris Yee2b1e5c2021-03-10 22:45:12 -0800916 std::optional<int32_t> getBatteryCapacity(int32_t, int32_t) const override {
917 return BATTERY_CAPACITY;
918 }
Kim Low03ea0352020-11-06 12:45:07 -0800919
Chris Yee2b1e5c2021-03-10 22:45:12 -0800920 std::optional<int32_t> getBatteryStatus(int32_t, int32_t) const override {
921 return BATTERY_STATUS;
922 }
923
924 const std::vector<int32_t> getRawBatteryIds(int32_t deviceId) { return {}; }
925
926 std::optional<RawBatteryInfo> getRawBatteryInfo(int32_t deviceId, int32_t batteryId) {
927 return std::nullopt;
928 }
Kim Low03ea0352020-11-06 12:45:07 -0800929
Chris Ye3fdbfef2021-01-06 18:45:18 -0800930 const std::vector<int32_t> getRawLightIds(int32_t deviceId) override {
931 std::vector<int32_t> ids;
932 for (const auto& [rawId, info] : mRawLightInfos) {
933 ids.push_back(rawId);
934 }
935 return ids;
936 }
937
938 std::optional<RawLightInfo> getRawLightInfo(int32_t deviceId, int32_t lightId) override {
939 auto it = mRawLightInfos.find(lightId);
940 if (it == mRawLightInfos.end()) {
941 return std::nullopt;
942 }
943 return it->second;
944 }
945
946 void setLightBrightness(int32_t deviceId, int32_t lightId, int32_t brightness) override {
947 mLightBrightness.emplace(lightId, brightness);
948 }
949
950 void setLightIntensities(int32_t deviceId, int32_t lightId,
951 std::unordered_map<LightColor, int32_t> intensities) override {
952 mLightIntensities.emplace(lightId, intensities);
953 };
954
955 std::optional<int32_t> getLightBrightness(int32_t deviceId, int32_t lightId) override {
956 auto lightIt = mLightBrightness.find(lightId);
957 if (lightIt == mLightBrightness.end()) {
958 return std::nullopt;
959 }
960 return lightIt->second;
961 }
962
963 std::optional<std::unordered_map<LightColor, int32_t>> getLightIntensities(
964 int32_t deviceId, int32_t lightId) override {
965 auto lightIt = mLightIntensities.find(lightId);
966 if (lightIt == mLightIntensities.end()) {
967 return std::nullopt;
968 }
969 return lightIt->second;
970 };
971
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100972 virtual bool isExternal(int32_t) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800973 return false;
974 }
975
Chris Yea52ade12020-08-27 16:49:20 -0700976 void dump(std::string&) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800977
Chris Yea52ade12020-08-27 16:49:20 -0700978 void monitor() override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800979
Chris Yea52ade12020-08-27 16:49:20 -0700980 void requestReopenDevices() override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800981
Chris Yea52ade12020-08-27 16:49:20 -0700982 void wake() override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800983};
984
Michael Wrightd02c5b62014-02-10 15:10:22 -0800985// --- FakeInputMapper ---
986
987class FakeInputMapper : public InputMapper {
988 uint32_t mSources;
989 int32_t mKeyboardType;
990 int32_t mMetaState;
991 KeyedVector<int32_t, int32_t> mKeyCodeStates;
992 KeyedVector<int32_t, int32_t> mScanCodeStates;
993 KeyedVector<int32_t, int32_t> mSwitchStates;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800994 std::vector<int32_t> mSupportedKeyCodes;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800995
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700996 std::mutex mLock;
997 std::condition_variable mStateChangedCondition;
998 bool mConfigureWasCalled GUARDED_BY(mLock);
999 bool mResetWasCalled GUARDED_BY(mLock);
1000 bool mProcessWasCalled GUARDED_BY(mLock);
1001 RawEvent mLastEvent GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001002
Arthur Hungc23540e2018-11-29 20:42:11 +08001003 std::optional<DisplayViewport> mViewport;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001004public:
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001005 FakeInputMapper(InputDeviceContext& deviceContext, uint32_t sources)
1006 : InputMapper(deviceContext),
1007 mSources(sources),
1008 mKeyboardType(AINPUT_KEYBOARD_TYPE_NONE),
Michael Wrightd02c5b62014-02-10 15:10:22 -08001009 mMetaState(0),
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001010 mConfigureWasCalled(false),
1011 mResetWasCalled(false),
1012 mProcessWasCalled(false) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001013
Chris Yea52ade12020-08-27 16:49:20 -07001014 virtual ~FakeInputMapper() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001015
1016 void setKeyboardType(int32_t keyboardType) {
1017 mKeyboardType = keyboardType;
1018 }
1019
1020 void setMetaState(int32_t metaState) {
1021 mMetaState = metaState;
1022 }
1023
1024 void assertConfigureWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001025 std::unique_lock<std::mutex> lock(mLock);
1026 base::ScopedLockAssertion assumeLocked(mLock);
1027 const bool configureCalled =
1028 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1029 return mConfigureWasCalled;
1030 });
1031 if (!configureCalled) {
1032 FAIL() << "Expected configure() to have been called.";
1033 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001034 mConfigureWasCalled = false;
1035 }
1036
1037 void assertResetWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001038 std::unique_lock<std::mutex> lock(mLock);
1039 base::ScopedLockAssertion assumeLocked(mLock);
1040 const bool resetCalled =
1041 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1042 return mResetWasCalled;
1043 });
1044 if (!resetCalled) {
1045 FAIL() << "Expected reset() to have been called.";
1046 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001047 mResetWasCalled = false;
1048 }
1049
Yi Kong9b14ac62018-07-17 13:48:38 -07001050 void assertProcessWasCalled(RawEvent* outLastEvent = nullptr) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001051 std::unique_lock<std::mutex> lock(mLock);
1052 base::ScopedLockAssertion assumeLocked(mLock);
1053 const bool processCalled =
1054 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1055 return mProcessWasCalled;
1056 });
1057 if (!processCalled) {
1058 FAIL() << "Expected process() to have been called.";
1059 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001060 if (outLastEvent) {
1061 *outLastEvent = mLastEvent;
1062 }
1063 mProcessWasCalled = false;
1064 }
1065
1066 void setKeyCodeState(int32_t keyCode, int32_t state) {
1067 mKeyCodeStates.replaceValueFor(keyCode, state);
1068 }
1069
1070 void setScanCodeState(int32_t scanCode, int32_t state) {
1071 mScanCodeStates.replaceValueFor(scanCode, state);
1072 }
1073
1074 void setSwitchState(int32_t switchCode, int32_t state) {
1075 mSwitchStates.replaceValueFor(switchCode, state);
1076 }
1077
1078 void addSupportedKeyCode(int32_t keyCode) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001079 mSupportedKeyCodes.push_back(keyCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001080 }
1081
1082private:
Chris Yea52ade12020-08-27 16:49:20 -07001083 uint32_t getSources() override { return mSources; }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001084
Chris Yea52ade12020-08-27 16:49:20 -07001085 void populateDeviceInfo(InputDeviceInfo* deviceInfo) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001086 InputMapper::populateDeviceInfo(deviceInfo);
1087
1088 if (mKeyboardType != AINPUT_KEYBOARD_TYPE_NONE) {
1089 deviceInfo->setKeyboardType(mKeyboardType);
1090 }
1091 }
1092
Chris Yea52ade12020-08-27 16:49:20 -07001093 void configure(nsecs_t, const InputReaderConfiguration* config, uint32_t changes) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001094 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001095 mConfigureWasCalled = true;
Arthur Hungc23540e2018-11-29 20:42:11 +08001096
1097 // Find the associated viewport if exist.
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001098 const std::optional<uint8_t> displayPort = getDeviceContext().getAssociatedDisplayPort();
Arthur Hungc23540e2018-11-29 20:42:11 +08001099 if (displayPort && (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
1100 mViewport = config->getDisplayViewportByPort(*displayPort);
1101 }
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001102
1103 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001104 }
1105
Chris Yea52ade12020-08-27 16:49:20 -07001106 void reset(nsecs_t) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001107 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001108 mResetWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001109 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001110 }
1111
Chris Yea52ade12020-08-27 16:49:20 -07001112 void process(const RawEvent* rawEvent) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001113 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001114 mLastEvent = *rawEvent;
1115 mProcessWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001116 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001117 }
1118
Chris Yea52ade12020-08-27 16:49:20 -07001119 int32_t getKeyCodeState(uint32_t, int32_t keyCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001120 ssize_t index = mKeyCodeStates.indexOfKey(keyCode);
1121 return index >= 0 ? mKeyCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1122 }
1123
Chris Yea52ade12020-08-27 16:49:20 -07001124 int32_t getScanCodeState(uint32_t, int32_t scanCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001125 ssize_t index = mScanCodeStates.indexOfKey(scanCode);
1126 return index >= 0 ? mScanCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1127 }
1128
Chris Yea52ade12020-08-27 16:49:20 -07001129 int32_t getSwitchState(uint32_t, int32_t switchCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001130 ssize_t index = mSwitchStates.indexOfKey(switchCode);
1131 return index >= 0 ? mSwitchStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1132 }
1133
Chris Yea52ade12020-08-27 16:49:20 -07001134 // Return true if the device has non-empty key layout.
1135 bool markSupportedKeyCodes(uint32_t, size_t numCodes, const int32_t* keyCodes,
1136 uint8_t* outFlags) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001137 for (size_t i = 0; i < numCodes; i++) {
1138 for (size_t j = 0; j < mSupportedKeyCodes.size(); j++) {
1139 if (keyCodes[i] == mSupportedKeyCodes[j]) {
1140 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001141 }
1142 }
1143 }
Chris Yea52ade12020-08-27 16:49:20 -07001144 bool result = mSupportedKeyCodes.size() > 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001145 return result;
1146 }
1147
1148 virtual int32_t getMetaState() {
1149 return mMetaState;
1150 }
1151
1152 virtual void fadePointer() {
1153 }
Arthur Hungc23540e2018-11-29 20:42:11 +08001154
1155 virtual std::optional<int32_t> getAssociatedDisplay() {
1156 if (mViewport) {
1157 return std::make_optional(mViewport->displayId);
1158 }
1159 return std::nullopt;
1160 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001161};
1162
1163
1164// --- InstrumentedInputReader ---
1165
1166class InstrumentedInputReader : public InputReader {
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001167 std::queue<std::shared_ptr<InputDevice>> mNextDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001168
1169public:
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001170 InstrumentedInputReader(std::shared_ptr<EventHubInterface> eventHub,
1171 const sp<InputReaderPolicyInterface>& policy,
1172 const sp<InputListenerInterface>& listener)
arthurhungdcef2dc2020-08-11 14:47:50 +08001173 : InputReader(eventHub, policy, listener), mFakeContext(this) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001174
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001175 virtual ~InstrumentedInputReader() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001176
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001177 void pushNextDevice(std::shared_ptr<InputDevice> device) { mNextDevices.push(device); }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001178
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001179 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001180 const std::string& location = "") {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001181 InputDeviceIdentifier identifier;
1182 identifier.name = name;
Arthur Hungc23540e2018-11-29 20:42:11 +08001183 identifier.location = location;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001184 int32_t generation = deviceId + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08001185 return std::make_shared<InputDevice>(&mFakeContext, deviceId, generation, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001186 }
1187
Prabir Pradhan28efc192019-11-05 01:10:04 +00001188 // Make the protected loopOnce method accessible to tests.
1189 using InputReader::loopOnce;
1190
Michael Wrightd02c5b62014-02-10 15:10:22 -08001191protected:
Chris Ye1c2e0892020-11-30 21:41:44 -08001192 virtual std::shared_ptr<InputDevice> createDeviceLocked(int32_t eventHubId,
1193 const InputDeviceIdentifier& identifier)
1194 REQUIRES(mLock) {
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001195 if (!mNextDevices.empty()) {
1196 std::shared_ptr<InputDevice> device(std::move(mNextDevices.front()));
1197 mNextDevices.pop();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001198 return device;
1199 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001200 return InputReader::createDeviceLocked(eventHubId, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001201 }
1202
arthurhungdcef2dc2020-08-11 14:47:50 +08001203 // --- FakeInputReaderContext ---
1204 class FakeInputReaderContext : public ContextImpl {
1205 int32_t mGlobalMetaState;
1206 bool mUpdateGlobalMetaStateWasCalled;
1207 int32_t mGeneration;
1208
1209 public:
1210 FakeInputReaderContext(InputReader* reader)
1211 : ContextImpl(reader),
1212 mGlobalMetaState(0),
1213 mUpdateGlobalMetaStateWasCalled(false),
1214 mGeneration(1) {}
1215
1216 virtual ~FakeInputReaderContext() {}
1217
1218 void assertUpdateGlobalMetaStateWasCalled() {
1219 ASSERT_TRUE(mUpdateGlobalMetaStateWasCalled)
1220 << "Expected updateGlobalMetaState() to have been called.";
1221 mUpdateGlobalMetaStateWasCalled = false;
1222 }
1223
1224 void setGlobalMetaState(int32_t state) { mGlobalMetaState = state; }
1225
1226 uint32_t getGeneration() { return mGeneration; }
1227
1228 void updateGlobalMetaState() override {
1229 mUpdateGlobalMetaStateWasCalled = true;
1230 ContextImpl::updateGlobalMetaState();
1231 }
1232
1233 int32_t getGlobalMetaState() override {
1234 return mGlobalMetaState | ContextImpl::getGlobalMetaState();
1235 }
1236
1237 int32_t bumpGeneration() override {
1238 mGeneration = ContextImpl::bumpGeneration();
1239 return mGeneration;
1240 }
1241 } mFakeContext;
1242
Michael Wrightd02c5b62014-02-10 15:10:22 -08001243 friend class InputReaderTest;
arthurhungdcef2dc2020-08-11 14:47:50 +08001244
1245public:
1246 FakeInputReaderContext* getContext() { return &mFakeContext; }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001247};
1248
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001249// --- InputReaderPolicyTest ---
1250class InputReaderPolicyTest : public testing::Test {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001251protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001252 sp<FakeInputReaderPolicy> mFakePolicy;
1253
Chris Yea52ade12020-08-27 16:49:20 -07001254 void SetUp() override { mFakePolicy = new FakeInputReaderPolicy(); }
1255 void TearDown() override { mFakePolicy.clear(); }
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001256};
1257
1258/**
1259 * Check that empty set of viewports is an acceptable configuration.
1260 * Also try to get internal viewport two different ways - by type and by uniqueId.
1261 *
1262 * There will be confusion if two viewports with empty uniqueId and identical type are present.
1263 * Such configuration is not currently allowed.
1264 */
1265TEST_F(InputReaderPolicyTest, Viewports_GetCleared) {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001266 static const std::string uniqueId = "local:0";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001267
1268 // We didn't add any viewports yet, so there shouldn't be any.
1269 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001270 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001271 ASSERT_FALSE(internalViewport);
1272
1273 // Add an internal viewport, then clear it
1274 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001275 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId, NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001276 ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001277
1278 // Check matching by uniqueId
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001279 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001280 ASSERT_TRUE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001281 ASSERT_EQ(ViewportType::INTERNAL, internalViewport->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001282
1283 // Check matching by viewport type
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001284 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001285 ASSERT_TRUE(internalViewport);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001286 ASSERT_EQ(uniqueId, internalViewport->uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001287
1288 mFakePolicy->clearViewports();
1289 // Make sure nothing is found after clear
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001290 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001291 ASSERT_FALSE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001292 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001293 ASSERT_FALSE(internalViewport);
1294}
1295
1296TEST_F(InputReaderPolicyTest, Viewports_GetByType) {
1297 const std::string internalUniqueId = "local:0";
1298 const std::string externalUniqueId = "local:1";
1299 const std::string virtualUniqueId1 = "virtual:2";
1300 const std::string virtualUniqueId2 = "virtual:3";
1301 constexpr int32_t virtualDisplayId1 = 2;
1302 constexpr int32_t virtualDisplayId2 = 3;
1303
1304 // Add an internal viewport
1305 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001306 DISPLAY_ORIENTATION_0, true /*isActive*/, internalUniqueId,
1307 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001308 // Add an external viewport
1309 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001310 DISPLAY_ORIENTATION_0, true /*isActive*/, externalUniqueId,
1311 NO_PORT, ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001312 // Add an virtual viewport
1313 mFakePolicy->addDisplayViewport(virtualDisplayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001314 DISPLAY_ORIENTATION_0, true /*isActive*/, virtualUniqueId1,
1315 NO_PORT, ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001316 // Add another virtual viewport
1317 mFakePolicy->addDisplayViewport(virtualDisplayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001318 DISPLAY_ORIENTATION_0, true /*isActive*/, virtualUniqueId2,
1319 NO_PORT, ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001320
1321 // Check matching by type for internal
1322 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001323 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001324 ASSERT_TRUE(internalViewport);
1325 ASSERT_EQ(internalUniqueId, internalViewport->uniqueId);
1326
1327 // Check matching by type for external
1328 std::optional<DisplayViewport> externalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001329 mFakePolicy->getDisplayViewportByType(ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001330 ASSERT_TRUE(externalViewport);
1331 ASSERT_EQ(externalUniqueId, externalViewport->uniqueId);
1332
1333 // Check matching by uniqueId for virtual viewport #1
1334 std::optional<DisplayViewport> virtualViewport1 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001335 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001336 ASSERT_TRUE(virtualViewport1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001337 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport1->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001338 ASSERT_EQ(virtualUniqueId1, virtualViewport1->uniqueId);
1339 ASSERT_EQ(virtualDisplayId1, virtualViewport1->displayId);
1340
1341 // Check matching by uniqueId for virtual viewport #2
1342 std::optional<DisplayViewport> virtualViewport2 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001343 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001344 ASSERT_TRUE(virtualViewport2);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001345 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport2->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001346 ASSERT_EQ(virtualUniqueId2, virtualViewport2->uniqueId);
1347 ASSERT_EQ(virtualDisplayId2, virtualViewport2->displayId);
1348}
1349
1350
1351/**
1352 * We can have 2 viewports of the same kind. We can distinguish them by uniqueId, and confirm
1353 * that lookup works by checking display id.
1354 * Check that 2 viewports of each kind is possible, for all existing viewport types.
1355 */
1356TEST_F(InputReaderPolicyTest, Viewports_TwoOfSameType) {
1357 const std::string uniqueId1 = "uniqueId1";
1358 const std::string uniqueId2 = "uniqueId2";
1359 constexpr int32_t displayId1 = 2;
1360 constexpr int32_t displayId2 = 3;
1361
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001362 std::vector<ViewportType> types = {ViewportType::INTERNAL, ViewportType::EXTERNAL,
1363 ViewportType::VIRTUAL};
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001364 for (const ViewportType& type : types) {
1365 mFakePolicy->clearViewports();
1366 // Add a viewport
1367 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001368 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1,
1369 NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001370 // Add another viewport
1371 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001372 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2,
1373 NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001374
1375 // Check that correct display viewport was returned by comparing the display IDs.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001376 std::optional<DisplayViewport> viewport1 =
1377 mFakePolicy->getDisplayViewportByUniqueId(uniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001378 ASSERT_TRUE(viewport1);
1379 ASSERT_EQ(displayId1, viewport1->displayId);
1380 ASSERT_EQ(type, viewport1->type);
1381
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001382 std::optional<DisplayViewport> viewport2 =
1383 mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001384 ASSERT_TRUE(viewport2);
1385 ASSERT_EQ(displayId2, viewport2->displayId);
1386 ASSERT_EQ(type, viewport2->type);
1387
1388 // When there are multiple viewports of the same kind, and uniqueId is not specified
1389 // in the call to getDisplayViewport, then that situation is not supported.
1390 // The viewports can be stored in any order, so we cannot rely on the order, since that
1391 // is just implementation detail.
1392 // However, we can check that it still returns *a* viewport, we just cannot assert
1393 // which one specifically is returned.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001394 std::optional<DisplayViewport> someViewport = mFakePolicy->getDisplayViewportByType(type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001395 ASSERT_TRUE(someViewport);
1396 }
1397}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001398
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001399/**
Michael Wrightdde67b82020-10-27 16:09:22 +00001400 * When we have multiple internal displays make sure we always return the default display when
1401 * querying by type.
1402 */
1403TEST_F(InputReaderPolicyTest, Viewports_ByTypeReturnsDefaultForInternal) {
1404 const std::string uniqueId1 = "uniqueId1";
1405 const std::string uniqueId2 = "uniqueId2";
1406 constexpr int32_t nonDefaultDisplayId = 2;
1407 static_assert(nonDefaultDisplayId != ADISPLAY_ID_DEFAULT,
1408 "Test display ID should not be ADISPLAY_ID_DEFAULT");
1409
1410 // Add the default display first and ensure it gets returned.
1411 mFakePolicy->clearViewports();
1412 mFakePolicy->addDisplayViewport(ADISPLAY_ID_DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001413 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001414 ViewportType::INTERNAL);
1415 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001416 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001417 ViewportType::INTERNAL);
1418
1419 std::optional<DisplayViewport> viewport =
1420 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
1421 ASSERT_TRUE(viewport);
1422 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
1423 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
1424
1425 // Add the default display second to make sure order doesn't matter.
1426 mFakePolicy->clearViewports();
1427 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001428 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001429 ViewportType::INTERNAL);
1430 mFakePolicy->addDisplayViewport(ADISPLAY_ID_DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001431 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001432 ViewportType::INTERNAL);
1433
1434 viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
1435 ASSERT_TRUE(viewport);
1436 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
1437 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
1438}
1439
1440/**
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001441 * Check getDisplayViewportByPort
1442 */
1443TEST_F(InputReaderPolicyTest, Viewports_GetByPort) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001444 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001445 const std::string uniqueId1 = "uniqueId1";
1446 const std::string uniqueId2 = "uniqueId2";
1447 constexpr int32_t displayId1 = 1;
1448 constexpr int32_t displayId2 = 2;
1449 const uint8_t hdmi1 = 0;
1450 const uint8_t hdmi2 = 1;
1451 const uint8_t hdmi3 = 2;
1452
1453 mFakePolicy->clearViewports();
1454 // Add a viewport that's associated with some display port that's not of interest.
1455 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001456 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, hdmi3,
1457 type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001458 // Add another viewport, connected to HDMI1 port
1459 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001460 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, hdmi1,
1461 type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001462
1463 // Check that correct display viewport was returned by comparing the display ports.
1464 std::optional<DisplayViewport> hdmi1Viewport = mFakePolicy->getDisplayViewportByPort(hdmi1);
1465 ASSERT_TRUE(hdmi1Viewport);
1466 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1467 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1468
1469 // Check that we can still get the same viewport using the uniqueId
1470 hdmi1Viewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
1471 ASSERT_TRUE(hdmi1Viewport);
1472 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1473 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1474 ASSERT_EQ(type, hdmi1Viewport->type);
1475
1476 // Check that we cannot find a port with "HDMI2", because we never added one
1477 std::optional<DisplayViewport> hdmi2Viewport = mFakePolicy->getDisplayViewportByPort(hdmi2);
1478 ASSERT_FALSE(hdmi2Viewport);
1479}
1480
Michael Wrightd02c5b62014-02-10 15:10:22 -08001481// --- InputReaderTest ---
1482
1483class InputReaderTest : public testing::Test {
1484protected:
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001485 sp<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001486 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001487 std::shared_ptr<FakeEventHub> mFakeEventHub;
Prabir Pradhan28efc192019-11-05 01:10:04 +00001488 std::unique_ptr<InstrumentedInputReader> mReader;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001489
Chris Yea52ade12020-08-27 16:49:20 -07001490 void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001491 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001492 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001493 mFakeListener = new TestInputListener();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001494
Prabir Pradhan28efc192019-11-05 01:10:04 +00001495 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
1496 mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001497 }
1498
Chris Yea52ade12020-08-27 16:49:20 -07001499 void TearDown() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001500 mFakeListener.clear();
1501 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001502 }
1503
Chris Ye1b0c7342020-07-28 21:57:03 -07001504 void addDevice(int32_t eventHubId, const std::string& name, Flags<InputDeviceClass> classes,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001505 const PropertyMap* configuration) {
1506 mFakeEventHub->addDevice(eventHubId, name, classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001507
1508 if (configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001509 mFakeEventHub->addConfigurationMap(eventHubId, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001510 }
1511 mFakeEventHub->finishDeviceScan();
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001512 mReader->loopOnce();
1513 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001514 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1515 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001516 }
1517
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001518 void disableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001519 mFakePolicy->addDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001520 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001521 }
1522
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001523 void enableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001524 mFakePolicy->removeDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001525 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001526 }
1527
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001528 FakeInputMapper& addDeviceWithFakeInputMapper(int32_t deviceId, int32_t eventHubId,
Chris Ye1b0c7342020-07-28 21:57:03 -07001529 const std::string& name,
1530 Flags<InputDeviceClass> classes, uint32_t sources,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001531 const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001532 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, name);
1533 FakeInputMapper& mapper = device->addMapper<FakeInputMapper>(eventHubId, sources);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001534 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001535 addDevice(eventHubId, name, classes, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001536 return mapper;
1537 }
1538};
1539
Chris Ye98d3f532020-10-01 21:48:59 -07001540TEST_F(InputReaderTest, PolicyGetInputDevices) {
1541 ASSERT_NO_FATAL_FAILURE(addDevice(1, "keyboard", InputDeviceClass::KEYBOARD, nullptr));
1542 ASSERT_NO_FATAL_FAILURE(addDevice(2, "ignored", Flags<InputDeviceClass>(0),
1543 nullptr)); // no classes so device will be ignored
Michael Wrightd02c5b62014-02-10 15:10:22 -08001544
1545 // Should also have received a notification describing the new input devices.
Chris Ye98d3f532020-10-01 21:48:59 -07001546 const std::vector<InputDeviceInfo>& inputDevices = mFakePolicy->getInputDevices();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001547 ASSERT_EQ(1U, inputDevices.size());
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001548 ASSERT_EQ(END_RESERVED_ID + 1, inputDevices[0].getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001549 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001550 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
1551 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00001552 ASSERT_EQ(0U, inputDevices[0].getMotionRanges().size());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001553}
1554
Chris Yee7310032020-09-22 15:36:28 -07001555TEST_F(InputReaderTest, GetMergedInputDevices) {
1556 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1557 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1558 // Add two subdevices to device
1559 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1560 // Must add at least one mapper or the device will be ignored!
1561 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1562 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1563
1564 // Push same device instance for next device to be added, so they'll have same identifier.
1565 mReader->pushNextDevice(device);
1566 mReader->pushNextDevice(device);
1567 ASSERT_NO_FATAL_FAILURE(
1568 addDevice(eventHubIds[0], "fake1", InputDeviceClass::KEYBOARD, nullptr));
1569 ASSERT_NO_FATAL_FAILURE(
1570 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
1571
1572 // Two devices will be merged to one input device as they have same identifier
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00001573 ASSERT_EQ(1U, mFakePolicy->getInputDevices().size());
Chris Yee7310032020-09-22 15:36:28 -07001574}
1575
Chris Yee14523a2020-12-19 13:46:00 -08001576TEST_F(InputReaderTest, GetMergedInputDevicesEnabled) {
1577 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1578 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1579 // Add two subdevices to device
1580 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1581 // Must add at least one mapper or the device will be ignored!
1582 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1583 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1584
1585 // Push same device instance for next device to be added, so they'll have same identifier.
1586 mReader->pushNextDevice(device);
1587 mReader->pushNextDevice(device);
1588 // Sensor device is initially disabled
1589 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1",
1590 InputDeviceClass::KEYBOARD | InputDeviceClass::SENSOR,
1591 nullptr));
1592 // Device is disabled because the only sub device is a sensor device and disabled initially.
1593 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1594 ASSERT_FALSE(device->isEnabled());
1595 ASSERT_NO_FATAL_FAILURE(
1596 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
1597 // The merged device is enabled if any sub device is enabled
1598 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1599 ASSERT_TRUE(device->isEnabled());
1600}
1601
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001602TEST_F(InputReaderTest, WhenEnabledChanges_SendsDeviceResetNotification) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001603 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001604 constexpr Flags<InputDeviceClass> deviceClass(InputDeviceClass::KEYBOARD);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001605 constexpr int32_t eventHubId = 1;
1606 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001607 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001608 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001609 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001610 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001611
Yi Kong9b14ac62018-07-17 13:48:38 -07001612 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001613
1614 NotifyDeviceResetArgs resetArgs;
1615 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001616 ASSERT_EQ(deviceId, resetArgs.deviceId);
1617
1618 ASSERT_EQ(device->isEnabled(), true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001619 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001620 mReader->loopOnce();
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001621
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001622 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001623 ASSERT_EQ(deviceId, resetArgs.deviceId);
1624 ASSERT_EQ(device->isEnabled(), false);
1625
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001626 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001627 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001628 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
1629 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasNotCalled());
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001630 ASSERT_EQ(device->isEnabled(), false);
1631
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001632 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001633 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001634 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001635 ASSERT_EQ(deviceId, resetArgs.deviceId);
1636 ASSERT_EQ(device->isEnabled(), true);
1637}
1638
Michael Wrightd02c5b62014-02-10 15:10:22 -08001639TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001640 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001641 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001642 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001643 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001644 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001645 AINPUT_SOURCE_KEYBOARD, nullptr);
1646 mapper.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001647
1648 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(0,
1649 AINPUT_SOURCE_ANY, AKEYCODE_A))
1650 << "Should return unknown when the device id is >= 0 but unknown.";
1651
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001652 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1653 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1654 << "Should return unknown when the device id is valid but the sources are not "
1655 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001656
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001657 ASSERT_EQ(AKEY_STATE_DOWN,
1658 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1659 AKEYCODE_A))
1660 << "Should return value provided by mapper when device id is valid and the device "
1661 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001662
1663 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(-1,
1664 AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1665 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1666
1667 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getKeyCodeState(-1,
1668 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1669 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1670}
1671
1672TEST_F(InputReaderTest, GetScanCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001673 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001674 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001675 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001676 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001677 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001678 AINPUT_SOURCE_KEYBOARD, nullptr);
1679 mapper.setScanCodeState(KEY_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001680
1681 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(0,
1682 AINPUT_SOURCE_ANY, KEY_A))
1683 << "Should return unknown when the device id is >= 0 but unknown.";
1684
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001685 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1686 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, KEY_A))
1687 << "Should return unknown when the device id is valid but the sources are not "
1688 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001689
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001690 ASSERT_EQ(AKEY_STATE_DOWN,
1691 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1692 KEY_A))
1693 << "Should return value provided by mapper when device id is valid and the device "
1694 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001695
1696 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(-1,
1697 AINPUT_SOURCE_TRACKBALL, KEY_A))
1698 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1699
1700 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getScanCodeState(-1,
1701 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, KEY_A))
1702 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1703}
1704
1705TEST_F(InputReaderTest, GetSwitchState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001706 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001707 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001708 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001709 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001710 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001711 AINPUT_SOURCE_KEYBOARD, nullptr);
1712 mapper.setSwitchState(SW_LID, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001713
1714 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(0,
1715 AINPUT_SOURCE_ANY, SW_LID))
1716 << "Should return unknown when the device id is >= 0 but unknown.";
1717
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001718 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1719 mReader->getSwitchState(deviceId, AINPUT_SOURCE_TRACKBALL, SW_LID))
1720 << "Should return unknown when the device id is valid but the sources are not "
1721 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001722
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001723 ASSERT_EQ(AKEY_STATE_DOWN,
1724 mReader->getSwitchState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1725 SW_LID))
1726 << "Should return value provided by mapper when device id is valid and the device "
1727 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001728
1729 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(-1,
1730 AINPUT_SOURCE_TRACKBALL, SW_LID))
1731 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1732
1733 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getSwitchState(-1,
1734 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, SW_LID))
1735 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1736}
1737
1738TEST_F(InputReaderTest, MarkSupportedKeyCodes_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001739 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001740 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001741 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001742 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001743 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001744 AINPUT_SOURCE_KEYBOARD, nullptr);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001745
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001746 mapper.addSupportedKeyCode(AKEYCODE_A);
1747 mapper.addSupportedKeyCode(AKEYCODE_B);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001748
1749 const int32_t keyCodes[4] = { AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2 };
1750 uint8_t flags[4] = { 0, 0, 0, 1 };
1751
1752 ASSERT_FALSE(mReader->hasKeys(0, AINPUT_SOURCE_ANY, 4, keyCodes, flags))
1753 << "Should return false when device id is >= 0 but unknown.";
1754 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1755
1756 flags[3] = 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001757 ASSERT_FALSE(mReader->hasKeys(deviceId, AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1758 << "Should return false when device id is valid but the sources are not supported by "
1759 "the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001760 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1761
1762 flags[3] = 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001763 ASSERT_TRUE(mReader->hasKeys(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, 4,
1764 keyCodes, flags))
1765 << "Should return value provided by mapper when device id is valid and the device "
1766 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001767 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1768
1769 flags[3] = 1;
1770 ASSERT_FALSE(mReader->hasKeys(-1, AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1771 << "Should return false when the device id is < 0 but the sources are not supported by any device.";
1772 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1773
1774 flags[3] = 1;
1775 ASSERT_TRUE(mReader->hasKeys(-1, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1776 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1777 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1778}
1779
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001780TEST_F(InputReaderTest, LoopOnce_WhenDeviceScanFinished_SendsConfigurationChanged) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001781 constexpr int32_t eventHubId = 1;
Chris Ye1b0c7342020-07-28 21:57:03 -07001782 addDevice(eventHubId, "ignored", InputDeviceClass::KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001783
1784 NotifyConfigurationChangedArgs args;
1785
1786 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(&args));
1787 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
1788}
1789
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001790TEST_F(InputReaderTest, LoopOnce_ForwardsRawEventsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001791 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001792 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001793 constexpr nsecs_t when = 0;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001794 constexpr int32_t eventHubId = 1;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001795 constexpr nsecs_t readTime = 2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001796 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001797 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001798 AINPUT_SOURCE_KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001799
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001800 mFakeEventHub->enqueueEvent(when, readTime, eventHubId, EV_KEY, KEY_A, 1);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001801 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001802 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
1803
1804 RawEvent event;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001805 ASSERT_NO_FATAL_FAILURE(mapper.assertProcessWasCalled(&event));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001806 ASSERT_EQ(when, event.when);
1807 ASSERT_EQ(readTime, event.readTime);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001808 ASSERT_EQ(eventHubId, event.deviceId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001809 ASSERT_EQ(EV_KEY, event.type);
1810 ASSERT_EQ(KEY_A, event.code);
1811 ASSERT_EQ(1, event.value);
1812}
1813
Garfield Tan1c7bc862020-01-28 13:24:04 -08001814TEST_F(InputReaderTest, DeviceReset_RandomId) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001815 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001816 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001817 constexpr int32_t eventHubId = 1;
1818 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Prabir Pradhan42611e02018-11-27 14:04:02 -08001819 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001820 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001821 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001822 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan42611e02018-11-27 14:04:02 -08001823
1824 NotifyDeviceResetArgs resetArgs;
1825 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001826 int32_t prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001827
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001828 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001829 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001830 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001831 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001832 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001833
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001834 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001835 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001836 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001837 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001838 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001839
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001840 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001841 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001842 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001843 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001844 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001845}
1846
Garfield Tan1c7bc862020-01-28 13:24:04 -08001847TEST_F(InputReaderTest, DeviceReset_GenerateIdWithInputReaderSource) {
1848 constexpr int32_t deviceId = 1;
Chris Ye1b0c7342020-07-28 21:57:03 -07001849 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Garfield Tan1c7bc862020-01-28 13:24:04 -08001850 constexpr int32_t eventHubId = 1;
1851 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1852 // Must add at least one mapper or the device will be ignored!
1853 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001854 mReader->pushNextDevice(device);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001855 ASSERT_NO_FATAL_FAILURE(addDevice(deviceId, "fake", deviceClass, nullptr));
1856
1857 NotifyDeviceResetArgs resetArgs;
1858 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1859 ASSERT_EQ(IdGenerator::Source::INPUT_READER, IdGenerator::getSource(resetArgs.id));
1860}
1861
Arthur Hungc23540e2018-11-29 20:42:11 +08001862TEST_F(InputReaderTest, Device_CanDispatchToDisplay) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001863 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001864 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001865 constexpr int32_t eventHubId = 1;
Arthur Hungc23540e2018-11-29 20:42:11 +08001866 const char* DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001867 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1868 FakeInputMapper& mapper =
1869 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001870 mReader->pushNextDevice(device);
Arthur Hungc23540e2018-11-29 20:42:11 +08001871
1872 const uint8_t hdmi1 = 1;
1873
1874 // Associated touch screen with second display.
1875 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
1876
1877 // Add default and second display.
Prabir Pradhan28efc192019-11-05 01:10:04 +00001878 mFakePolicy->clearViewports();
Arthur Hungc23540e2018-11-29 20:42:11 +08001879 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001880 DISPLAY_ORIENTATION_0, true /*isActive*/, "local:0", NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001881 ViewportType::INTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08001882 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001883 DISPLAY_ORIENTATION_0, true /*isActive*/, "local:1", hdmi1,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001884 ViewportType::EXTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08001885 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001886 mReader->loopOnce();
Prabir Pradhan28efc192019-11-05 01:10:04 +00001887
1888 // Add the device, and make sure all of the callbacks are triggered.
1889 // The device is added after the input port associations are processed since
1890 // we do not yet support dynamic device-to-display associations.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001891 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001892 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled());
Prabir Pradhan28efc192019-11-05 01:10:04 +00001893 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001894 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
Arthur Hungc23540e2018-11-29 20:42:11 +08001895
Arthur Hung2c9a3342019-07-23 14:18:59 +08001896 // Device should only dispatch to the specified display.
Arthur Hungc23540e2018-11-29 20:42:11 +08001897 ASSERT_EQ(deviceId, device->getId());
1898 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, DISPLAY_ID));
1899 ASSERT_TRUE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hung2c9a3342019-07-23 14:18:59 +08001900
1901 // Can't dispatch event from a disabled device.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001902 disableDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001903 mReader->loopOnce();
Arthur Hung2c9a3342019-07-23 14:18:59 +08001904 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hungc23540e2018-11-29 20:42:11 +08001905}
1906
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001907TEST_F(InputReaderTest, WhenEnabledChanges_AllSubdevicesAreUpdated) {
1908 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1909 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
1910 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1911 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1912 // Must add at least one mapper or the device will be ignored!
1913 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1914 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1915 mReader->pushNextDevice(device);
1916 mReader->pushNextDevice(device);
1917 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
1918 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
1919
1920 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
1921
1922 NotifyDeviceResetArgs resetArgs;
1923 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1924 ASSERT_EQ(deviceId, resetArgs.deviceId);
1925 ASSERT_TRUE(device->isEnabled());
1926 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1927 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1928
1929 disableDevice(deviceId);
1930 mReader->loopOnce();
1931
1932 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1933 ASSERT_EQ(deviceId, resetArgs.deviceId);
1934 ASSERT_FALSE(device->isEnabled());
1935 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1936 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1937
1938 enableDevice(deviceId);
1939 mReader->loopOnce();
1940
1941 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1942 ASSERT_EQ(deviceId, resetArgs.deviceId);
1943 ASSERT_TRUE(device->isEnabled());
1944 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1945 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1946}
1947
1948TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToSubdeviceMappers) {
1949 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1950 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
1951 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1952 // Add two subdevices to device
1953 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1954 FakeInputMapper& mapperDevice1 =
1955 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1956 FakeInputMapper& mapperDevice2 =
1957 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1958 mReader->pushNextDevice(device);
1959 mReader->pushNextDevice(device);
1960 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
1961 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
1962
1963 mapperDevice1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
1964 mapperDevice2.setKeyCodeState(AKEYCODE_B, AKEY_STATE_DOWN);
1965
1966 ASSERT_EQ(AKEY_STATE_DOWN,
1967 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_A));
1968 ASSERT_EQ(AKEY_STATE_DOWN,
1969 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_B));
1970 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1971 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_C));
1972}
1973
Prabir Pradhan7e186182020-11-10 13:56:45 -08001974TEST_F(InputReaderTest, ChangingPointerCaptureNotifiesInputListener) {
1975 NotifyPointerCaptureChangedArgs args;
1976
1977 mFakePolicy->setPointerCapture(true);
1978 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
1979 mReader->loopOnce();
1980 mFakeListener->assertNotifyCaptureWasCalled(&args);
1981 ASSERT_TRUE(args.enabled) << "Pointer Capture should be enabled.";
1982
1983 mFakePolicy->setPointerCapture(false);
1984 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
1985 mReader->loopOnce();
1986 mFakeListener->assertNotifyCaptureWasCalled(&args);
1987 ASSERT_FALSE(args.enabled) << "Pointer Capture should be disabled.";
1988
1989 // Verify that the Pointer Capture state is re-configured correctly when the configuration value
1990 // does not change.
1991 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
1992 mReader->loopOnce();
1993 mFakeListener->assertNotifyCaptureWasCalled(&args);
1994 ASSERT_FALSE(args.enabled) << "Pointer Capture should be disabled.";
1995}
1996
Chris Ye87143712020-11-10 05:05:58 +00001997class FakeVibratorInputMapper : public FakeInputMapper {
1998public:
1999 FakeVibratorInputMapper(InputDeviceContext& deviceContext, uint32_t sources)
2000 : FakeInputMapper(deviceContext, sources) {}
2001
2002 std::vector<int32_t> getVibratorIds() override { return getDeviceContext().getVibratorIds(); }
2003};
2004
2005TEST_F(InputReaderTest, VibratorGetVibratorIds) {
2006 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
2007 Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD | InputDeviceClass::VIBRATOR;
2008 constexpr int32_t eventHubId = 1;
2009 const char* DEVICE_LOCATION = "BLUETOOTH";
2010 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
2011 FakeVibratorInputMapper& mapper =
2012 device->addMapper<FakeVibratorInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
2013 mReader->pushNextDevice(device);
2014
2015 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2016 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
2017
2018 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
2019 ASSERT_EQ(mReader->getVibratorIds(deviceId).size(), 2U);
2020}
2021
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002022// --- FakePeripheralController ---
Kim Low03ea0352020-11-06 12:45:07 -08002023
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002024class FakePeripheralController : public PeripheralControllerInterface {
Chris Yee2b1e5c2021-03-10 22:45:12 -08002025public:
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002026 FakePeripheralController(InputDeviceContext& deviceContext) : mDeviceContext(deviceContext) {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08002027
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002028 ~FakePeripheralController() override {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08002029
2030 void populateDeviceInfo(InputDeviceInfo* deviceInfo) override {}
2031
2032 void dump(std::string& dump) override {}
2033
2034 std::optional<int32_t> getBatteryCapacity(int32_t batteryId) override {
2035 return getDeviceContext().getBatteryCapacity(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08002036 }
2037
Chris Yee2b1e5c2021-03-10 22:45:12 -08002038 std::optional<int32_t> getBatteryStatus(int32_t batteryId) override {
2039 return getDeviceContext().getBatteryStatus(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08002040 }
Chris Ye3fdbfef2021-01-06 18:45:18 -08002041
2042 bool setLightColor(int32_t lightId, int32_t color) override {
2043 getDeviceContext().setLightBrightness(lightId, color >> 24);
2044 return true;
2045 }
2046
2047 std::optional<int32_t> getLightColor(int32_t lightId) override {
2048 std::optional<int32_t> result = getDeviceContext().getLightBrightness(lightId);
2049 if (!result.has_value()) {
2050 return std::nullopt;
2051 }
2052 return result.value() << 24;
2053 }
Chris Yee2b1e5c2021-03-10 22:45:12 -08002054
2055 bool setLightPlayerId(int32_t lightId, int32_t playerId) override { return true; }
2056
2057 std::optional<int32_t> getLightPlayerId(int32_t lightId) override { return std::nullopt; }
2058
2059private:
2060 InputDeviceContext& mDeviceContext;
2061 inline int32_t getDeviceId() { return mDeviceContext.getId(); }
2062 inline InputDeviceContext& getDeviceContext() { return mDeviceContext; }
Chris Ye3fdbfef2021-01-06 18:45:18 -08002063};
2064
Chris Yee2b1e5c2021-03-10 22:45:12 -08002065TEST_F(InputReaderTest, BatteryGetCapacity) {
2066 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
2067 Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
2068 constexpr int32_t eventHubId = 1;
2069 const char* DEVICE_LOCATION = "BLUETOOTH";
2070 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002071 FakePeripheralController& controller =
2072 device->addController<FakePeripheralController>(eventHubId);
Chris Yee2b1e5c2021-03-10 22:45:12 -08002073 mReader->pushNextDevice(device);
2074
2075 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2076
2077 ASSERT_EQ(controller.getBatteryCapacity(DEFAULT_BATTERY), BATTERY_CAPACITY);
2078 ASSERT_EQ(mReader->getBatteryCapacity(deviceId), BATTERY_CAPACITY);
2079}
2080
2081TEST_F(InputReaderTest, BatteryGetStatus) {
2082 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
2083 Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
2084 constexpr int32_t eventHubId = 1;
2085 const char* DEVICE_LOCATION = "BLUETOOTH";
2086 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002087 FakePeripheralController& controller =
2088 device->addController<FakePeripheralController>(eventHubId);
Chris Yee2b1e5c2021-03-10 22:45:12 -08002089 mReader->pushNextDevice(device);
2090
2091 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2092
2093 ASSERT_EQ(controller.getBatteryStatus(DEFAULT_BATTERY), BATTERY_STATUS);
2094 ASSERT_EQ(mReader->getBatteryStatus(deviceId), BATTERY_STATUS);
2095}
2096
Chris Ye3fdbfef2021-01-06 18:45:18 -08002097TEST_F(InputReaderTest, LightGetColor) {
2098 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
2099 Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD | InputDeviceClass::LIGHT;
2100 constexpr int32_t eventHubId = 1;
2101 const char* DEVICE_LOCATION = "BLUETOOTH";
2102 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002103 FakePeripheralController& controller =
2104 device->addController<FakePeripheralController>(eventHubId);
Chris Ye3fdbfef2021-01-06 18:45:18 -08002105 mReader->pushNextDevice(device);
2106 RawLightInfo info = {.id = 1,
2107 .name = "Mono",
2108 .maxBrightness = 255,
2109 .flags = InputLightClass::BRIGHTNESS,
2110 .path = ""};
2111 mFakeEventHub->addRawLightInfo(1 /* rawId */, std::move(info));
2112 mFakeEventHub->fakeLightBrightness(1 /* rawId */, 0x55);
2113
2114 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Chris Ye3fdbfef2021-01-06 18:45:18 -08002115
Chris Yee2b1e5c2021-03-10 22:45:12 -08002116 ASSERT_TRUE(controller.setLightColor(1 /* lightId */, LIGHT_BRIGHTNESS));
2117 ASSERT_EQ(controller.getLightColor(1 /* lightId */), LIGHT_BRIGHTNESS);
Chris Ye3fdbfef2021-01-06 18:45:18 -08002118 ASSERT_TRUE(mReader->setLightColor(deviceId, 1 /* lightId */, LIGHT_BRIGHTNESS));
2119 ASSERT_EQ(mReader->getLightColor(deviceId, 1 /* lightId */), LIGHT_BRIGHTNESS);
2120}
2121
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002122// --- InputReaderIntegrationTest ---
2123
2124// These tests create and interact with the InputReader only through its interface.
2125// The InputReader is started during SetUp(), which starts its processing in its own
2126// thread. The tests use linux uinput to emulate input devices.
2127// NOTE: Interacting with the physical device while these tests are running may cause
2128// the tests to fail.
2129class InputReaderIntegrationTest : public testing::Test {
2130protected:
2131 sp<TestInputListener> mTestListener;
2132 sp<FakeInputReaderPolicy> mFakePolicy;
2133 sp<InputReaderInterface> mReader;
2134
Chris Yea52ade12020-08-27 16:49:20 -07002135 void SetUp() override {
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002136 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakouf0db5b82020-04-08 19:22:14 -07002137 mTestListener = new TestInputListener(2000ms /*eventHappenedTimeout*/,
2138 30ms /*eventDidNotHappenTimeout*/);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002139
Prabir Pradhan9244aea2020-02-05 20:31:40 -08002140 mReader = new InputReader(std::make_shared<EventHub>(), mFakePolicy, mTestListener);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002141 ASSERT_EQ(mReader->start(), OK);
2142
2143 // Since this test is run on a real device, all the input devices connected
2144 // to the test device will show up in mReader. We wait for those input devices to
2145 // show up before beginning the tests.
2146 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2147 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2148 }
2149
Chris Yea52ade12020-08-27 16:49:20 -07002150 void TearDown() override {
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002151 ASSERT_EQ(mReader->stop(), OK);
2152 mTestListener.clear();
2153 mFakePolicy.clear();
2154 }
2155};
2156
2157TEST_F(InputReaderIntegrationTest, TestInvalidDevice) {
2158 // An invalid input device that is only used for this test.
2159 class InvalidUinputDevice : public UinputDevice {
2160 public:
2161 InvalidUinputDevice() : UinputDevice("Invalid Device") {}
2162
2163 private:
2164 void configureDevice(int fd, uinput_user_dev* device) override {}
2165 };
2166
2167 const size_t numDevices = mFakePolicy->getInputDevices().size();
2168
2169 // UinputDevice does not set any event or key bits, so InputReader should not
2170 // consider it as a valid device.
2171 std::unique_ptr<UinputDevice> invalidDevice = createUinputDevice<InvalidUinputDevice>();
2172 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
2173 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
2174 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
2175
2176 invalidDevice.reset();
2177 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
2178 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
2179 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
2180}
2181
2182TEST_F(InputReaderIntegrationTest, AddNewDevice) {
2183 const size_t initialNumDevices = mFakePolicy->getInputDevices().size();
2184
2185 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
2186 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2187 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2188 ASSERT_EQ(initialNumDevices + 1, mFakePolicy->getInputDevices().size());
2189
2190 // Find the test device by its name.
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002191 const std::vector<InputDeviceInfo> inputDevices = mFakePolicy->getInputDevices();
Chris Ye98d3f532020-10-01 21:48:59 -07002192 const auto& it =
2193 std::find_if(inputDevices.begin(), inputDevices.end(),
2194 [&keyboard](const InputDeviceInfo& info) {
2195 return info.getIdentifier().name == keyboard->getName();
2196 });
2197
2198 ASSERT_NE(it, inputDevices.end());
2199 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, it->getKeyboardType());
2200 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, it->getSources());
2201 ASSERT_EQ(0U, it->getMotionRanges().size());
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002202
2203 keyboard.reset();
2204 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2205 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2206 ASSERT_EQ(initialNumDevices, mFakePolicy->getInputDevices().size());
2207}
2208
2209TEST_F(InputReaderIntegrationTest, SendsEventsToInputListener) {
2210 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
2211 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2212
2213 NotifyConfigurationChangedArgs configChangedArgs;
2214 ASSERT_NO_FATAL_FAILURE(
2215 mTestListener->assertNotifyConfigurationChangedWasCalled(&configChangedArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002216 int32_t prevId = configChangedArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002217 nsecs_t prevTimestamp = configChangedArgs.eventTime;
2218
2219 NotifyKeyArgs keyArgs;
2220 keyboard->pressAndReleaseHomeKey();
2221 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
2222 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002223 ASSERT_NE(prevId, keyArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002224 prevId = keyArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002225 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002226 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002227 prevTimestamp = keyArgs.eventTime;
2228
2229 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
2230 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002231 ASSERT_NE(prevId, keyArgs.id);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002232 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002233 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002234}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002235
Siarhei Vishniakoua0d2b802020-05-13 14:00:31 -07002236/**
2237 * The Steam controller sends BTN_GEAR_DOWN and BTN_GEAR_UP for the two "paddle" buttons
2238 * on the back. In this test, we make sure that BTN_GEAR_DOWN / BTN_WHEEL and BTN_GEAR_UP
2239 * are passed to the listener.
2240 */
2241static_assert(BTN_GEAR_DOWN == BTN_WHEEL);
2242TEST_F(InputReaderIntegrationTest, SendsGearDownAndUpToInputListener) {
2243 std::unique_ptr<UinputSteamController> controller = createUinputDevice<UinputSteamController>();
2244 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2245 NotifyKeyArgs keyArgs;
2246
2247 controller->pressAndReleaseKey(BTN_GEAR_DOWN);
2248 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
2249 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
2250 ASSERT_EQ(BTN_GEAR_DOWN, keyArgs.scanCode);
2251
2252 controller->pressAndReleaseKey(BTN_GEAR_UP);
2253 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
2254 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
2255 ASSERT_EQ(BTN_GEAR_UP, keyArgs.scanCode);
2256}
2257
Arthur Hungaab25622020-01-16 11:22:11 +08002258// --- TouchProcessTest ---
2259class TouchIntegrationTest : public InputReaderIntegrationTest {
2260protected:
Arthur Hungaab25622020-01-16 11:22:11 +08002261 const std::string UNIQUE_ID = "local:0";
2262
Chris Yea52ade12020-08-27 16:49:20 -07002263 void SetUp() override {
Arthur Hungaab25622020-01-16 11:22:11 +08002264 InputReaderIntegrationTest::SetUp();
2265 // At least add an internal display.
2266 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2267 DISPLAY_ORIENTATION_0, UNIQUE_ID, NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002268 ViewportType::INTERNAL);
Arthur Hungaab25622020-01-16 11:22:11 +08002269
2270 mDevice = createUinputDevice<UinputTouchScreen>(Rect(0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT));
2271 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2272 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2273 }
2274
2275 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
2276 int32_t orientation, const std::string& uniqueId,
2277 std::optional<uint8_t> physicalPort,
2278 ViewportType viewportType) {
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002279 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, true /*isActive*/,
2280 uniqueId, physicalPort, viewportType);
Arthur Hungaab25622020-01-16 11:22:11 +08002281 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2282 }
2283
2284 std::unique_ptr<UinputTouchScreen> mDevice;
2285};
2286
2287TEST_F(TouchIntegrationTest, InputEvent_ProcessSingleTouch) {
2288 NotifyMotionArgs args;
2289 const Point centerPoint = mDevice->getCenterPoint();
2290
2291 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00002292 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002293 mDevice->sendDown(centerPoint);
2294 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2295 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2296
2297 // ACTION_MOVE
2298 mDevice->sendMove(centerPoint + Point(1, 1));
2299 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2300 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2301
2302 // ACTION_UP
2303 mDevice->sendUp();
2304 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2305 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2306}
2307
2308TEST_F(TouchIntegrationTest, InputEvent_ProcessMultiTouch) {
2309 NotifyMotionArgs args;
2310 const Point centerPoint = mDevice->getCenterPoint();
2311
2312 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00002313 mDevice->sendSlot(FIRST_SLOT);
2314 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002315 mDevice->sendDown(centerPoint);
2316 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2317 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2318
2319 // ACTION_POINTER_DOWN (Second slot)
2320 const Point secondPoint = centerPoint + Point(100, 100);
2321 mDevice->sendSlot(SECOND_SLOT);
2322 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2323 mDevice->sendDown(secondPoint + Point(1, 1));
2324 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2325 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
2326 args.action);
2327
2328 // ACTION_MOVE (Second slot)
2329 mDevice->sendMove(secondPoint);
2330 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2331 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2332
2333 // ACTION_POINTER_UP (Second slot)
arthurhungcc7f9802020-04-30 17:55:40 +08002334 mDevice->sendPointerUp();
Arthur Hungaab25622020-01-16 11:22:11 +08002335 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
arthurhungcc7f9802020-04-30 17:55:40 +08002336 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
Arthur Hungaab25622020-01-16 11:22:11 +08002337 args.action);
2338
2339 // ACTION_UP
2340 mDevice->sendSlot(FIRST_SLOT);
2341 mDevice->sendUp();
2342 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2343 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2344}
2345
2346TEST_F(TouchIntegrationTest, InputEvent_ProcessPalm) {
2347 NotifyMotionArgs args;
2348 const Point centerPoint = mDevice->getCenterPoint();
2349
2350 // ACTION_DOWN
arthurhungcc7f9802020-04-30 17:55:40 +08002351 mDevice->sendSlot(FIRST_SLOT);
2352 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002353 mDevice->sendDown(centerPoint);
2354 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2355 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2356
arthurhungcc7f9802020-04-30 17:55:40 +08002357 // ACTION_POINTER_DOWN (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002358 const Point secondPoint = centerPoint + Point(100, 100);
2359 mDevice->sendSlot(SECOND_SLOT);
2360 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2361 mDevice->sendDown(secondPoint);
2362 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2363 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
2364 args.action);
2365
arthurhungcc7f9802020-04-30 17:55:40 +08002366 // ACTION_MOVE (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002367 mDevice->sendMove(secondPoint + Point(1, 1));
2368 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2369 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2370
arthurhungcc7f9802020-04-30 17:55:40 +08002371 // Send MT_TOOL_PALM (second slot), which indicates that the touch IC has determined this to be
2372 // a palm event.
2373 // Expect to receive the ACTION_POINTER_UP with cancel flag.
Arthur Hungaab25622020-01-16 11:22:11 +08002374 mDevice->sendToolType(MT_TOOL_PALM);
2375 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
arthurhungcc7f9802020-04-30 17:55:40 +08002376 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
2377 args.action);
2378 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, args.flags);
Arthur Hungaab25622020-01-16 11:22:11 +08002379
arthurhungcc7f9802020-04-30 17:55:40 +08002380 // Send up to second slot, expect first slot send moving.
2381 mDevice->sendPointerUp();
2382 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2383 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002384
arthurhungcc7f9802020-04-30 17:55:40 +08002385 // Send ACTION_UP (first slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002386 mDevice->sendSlot(FIRST_SLOT);
2387 mDevice->sendUp();
2388
arthurhungcc7f9802020-04-30 17:55:40 +08002389 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2390 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002391}
2392
Michael Wrightd02c5b62014-02-10 15:10:22 -08002393// --- InputDeviceTest ---
Michael Wrightd02c5b62014-02-10 15:10:22 -08002394class InputDeviceTest : public testing::Test {
2395protected:
2396 static const char* DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002397 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002398 static const int32_t DEVICE_ID;
2399 static const int32_t DEVICE_GENERATION;
2400 static const int32_t DEVICE_CONTROLLER_NUMBER;
Chris Ye1b0c7342020-07-28 21:57:03 -07002401 static const Flags<InputDeviceClass> DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002402 static const int32_t EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002403
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002404 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002405 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08002406 sp<TestInputListener> mFakeListener;
arthurhungdcef2dc2020-08-11 14:47:50 +08002407 std::unique_ptr<InstrumentedInputReader> mReader;
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00002408 std::shared_ptr<InputDevice> mDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002409
Chris Yea52ade12020-08-27 16:49:20 -07002410 void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002411 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002412 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08002413 mFakeListener = new TestInputListener();
arthurhungdcef2dc2020-08-11 14:47:50 +08002414 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
2415 mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002416 InputDeviceIdentifier identifier;
2417 identifier.name = DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002418 identifier.location = DEVICE_LOCATION;
arthurhungdcef2dc2020-08-11 14:47:50 +08002419 mDevice = std::make_shared<InputDevice>(mReader->getContext(), DEVICE_ID, DEVICE_GENERATION,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002420 identifier);
arthurhungdcef2dc2020-08-11 14:47:50 +08002421 mReader->pushNextDevice(mDevice);
2422 mFakeEventHub->addDevice(EVENTHUB_ID, DEVICE_NAME, Flags<InputDeviceClass>(0));
2423 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002424 }
2425
Chris Yea52ade12020-08-27 16:49:20 -07002426 void TearDown() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002427 mFakeListener.clear();
2428 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002429 }
2430};
2431
2432const char* InputDeviceTest::DEVICE_NAME = "device";
Arthur Hung2c9a3342019-07-23 14:18:59 +08002433const char* InputDeviceTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002434const int32_t InputDeviceTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002435const int32_t InputDeviceTest::DEVICE_GENERATION = 2;
2436const int32_t InputDeviceTest::DEVICE_CONTROLLER_NUMBER = 0;
Chris Ye1b0c7342020-07-28 21:57:03 -07002437const Flags<InputDeviceClass> InputDeviceTest::DEVICE_CLASSES =
2438 InputDeviceClass::KEYBOARD | InputDeviceClass::TOUCH | InputDeviceClass::JOYSTICK;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002439const int32_t InputDeviceTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002440
2441TEST_F(InputDeviceTest, ImmutableProperties) {
2442 ASSERT_EQ(DEVICE_ID, mDevice->getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002443 ASSERT_STREQ(DEVICE_NAME, mDevice->getName().c_str());
Chris Ye1b0c7342020-07-28 21:57:03 -07002444 ASSERT_EQ(Flags<InputDeviceClass>(0), mDevice->getClasses());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002445}
2446
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002447TEST_F(InputDeviceTest, WhenDeviceCreated_EnabledIsFalse) {
2448 ASSERT_EQ(mDevice->isEnabled(), false);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07002449}
2450
Michael Wrightd02c5b62014-02-10 15:10:22 -08002451TEST_F(InputDeviceTest, WhenNoMappersAreRegistered_DeviceIsIgnored) {
2452 // Configuration.
2453 InputReaderConfiguration config;
2454 mDevice->configure(ARBITRARY_TIME, &config, 0);
2455
2456 // Reset.
2457 mDevice->reset(ARBITRARY_TIME);
2458
2459 NotifyDeviceResetArgs resetArgs;
2460 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2461 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2462 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2463
2464 // Metadata.
2465 ASSERT_TRUE(mDevice->isIgnored());
2466 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mDevice->getSources());
2467
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002468 InputDeviceInfo info = mDevice->getDeviceInfo();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002469 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002470 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002471 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NONE, info.getKeyboardType());
2472 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, info.getSources());
2473
2474 // State queries.
2475 ASSERT_EQ(0, mDevice->getMetaState());
2476
2477 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2478 << "Ignored device should return unknown key code state.";
2479 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2480 << "Ignored device should return unknown scan code state.";
2481 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 0))
2482 << "Ignored device should return unknown switch state.";
2483
2484 const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
2485 uint8_t flags[2] = { 0, 1 };
2486 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, 2, keyCodes, flags))
2487 << "Ignored device should never mark any key codes.";
2488 ASSERT_EQ(0, flags[0]) << "Flag for unsupported key should be unchanged.";
2489 ASSERT_EQ(1, flags[1]) << "Flag for unsupported key should be unchanged.";
2490}
2491
2492TEST_F(InputDeviceTest, WhenMappersAreRegistered_DeviceIsNotIgnoredAndForwardsRequestsToMappers) {
2493 // Configuration.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002494 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, String8("key"), String8("value"));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002495
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002496 FakeInputMapper& mapper1 =
2497 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002498 mapper1.setKeyboardType(AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2499 mapper1.setMetaState(AMETA_ALT_ON);
2500 mapper1.addSupportedKeyCode(AKEYCODE_A);
2501 mapper1.addSupportedKeyCode(AKEYCODE_B);
2502 mapper1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
2503 mapper1.setKeyCodeState(AKEYCODE_B, AKEY_STATE_UP);
2504 mapper1.setScanCodeState(2, AKEY_STATE_DOWN);
2505 mapper1.setScanCodeState(3, AKEY_STATE_UP);
2506 mapper1.setSwitchState(4, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002507
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002508 FakeInputMapper& mapper2 =
2509 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002510 mapper2.setMetaState(AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002511
2512 InputReaderConfiguration config;
2513 mDevice->configure(ARBITRARY_TIME, &config, 0);
2514
2515 String8 propertyValue;
2516 ASSERT_TRUE(mDevice->getConfiguration().tryGetProperty(String8("key"), propertyValue))
2517 << "Device should have read configuration during configuration phase.";
2518 ASSERT_STREQ("value", propertyValue.string());
2519
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002520 ASSERT_NO_FATAL_FAILURE(mapper1.assertConfigureWasCalled());
2521 ASSERT_NO_FATAL_FAILURE(mapper2.assertConfigureWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002522
2523 // Reset
2524 mDevice->reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002525 ASSERT_NO_FATAL_FAILURE(mapper1.assertResetWasCalled());
2526 ASSERT_NO_FATAL_FAILURE(mapper2.assertResetWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002527
2528 NotifyDeviceResetArgs resetArgs;
2529 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2530 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2531 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2532
2533 // Metadata.
2534 ASSERT_FALSE(mDevice->isIgnored());
2535 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), mDevice->getSources());
2536
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002537 InputDeviceInfo info = mDevice->getDeviceInfo();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002538 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002539 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002540 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_ALPHABETIC, info.getKeyboardType());
2541 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), info.getSources());
2542
2543 // State queries.
2544 ASSERT_EQ(AMETA_ALT_ON | AMETA_SHIFT_ON, mDevice->getMetaState())
2545 << "Should query mappers and combine meta states.";
2546
2547 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2548 << "Should return unknown key code state when source not supported.";
2549 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2550 << "Should return unknown scan code state when source not supported.";
2551 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2552 << "Should return unknown switch state when source not supported.";
2553
2554 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, AKEYCODE_A))
2555 << "Should query mapper when source is supported.";
2556 ASSERT_EQ(AKEY_STATE_UP, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 3))
2557 << "Should query mapper when source is supported.";
2558 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 4))
2559 << "Should query mapper when source is supported.";
2560
2561 const int32_t keyCodes[4] = { AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2 };
2562 uint8_t flags[4] = { 0, 0, 0, 1 };
2563 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
2564 << "Should do nothing when source is unsupported.";
2565 ASSERT_EQ(0, flags[0]) << "Flag should be unchanged when source is unsupported.";
2566 ASSERT_EQ(0, flags[1]) << "Flag should be unchanged when source is unsupported.";
2567 ASSERT_EQ(0, flags[2]) << "Flag should be unchanged when source is unsupported.";
2568 ASSERT_EQ(1, flags[3]) << "Flag should be unchanged when source is unsupported.";
2569
2570 ASSERT_TRUE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, 4, keyCodes, flags))
2571 << "Should query mapper when source is supported.";
2572 ASSERT_EQ(1, flags[0]) << "Flag for supported key should be set.";
2573 ASSERT_EQ(1, flags[1]) << "Flag for supported key should be set.";
2574 ASSERT_EQ(0, flags[2]) << "Flag for unsupported key should be unchanged.";
2575 ASSERT_EQ(1, flags[3]) << "Flag for unsupported key should be unchanged.";
2576
2577 // Event handling.
2578 RawEvent event;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002579 event.deviceId = EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002580 mDevice->process(&event, 1);
2581
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002582 ASSERT_NO_FATAL_FAILURE(mapper1.assertProcessWasCalled());
2583 ASSERT_NO_FATAL_FAILURE(mapper2.assertProcessWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002584}
2585
Arthur Hung2c9a3342019-07-23 14:18:59 +08002586// A single input device is associated with a specific display. Check that:
2587// 1. Device is disabled if the viewport corresponding to the associated display is not found
2588// 2. Device is disabled when setEnabled API is called
2589TEST_F(InputDeviceTest, Configure_AssignsDisplayPort) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002590 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002591
2592 // First Configuration.
2593 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
2594
2595 // Device should be enabled by default.
2596 ASSERT_TRUE(mDevice->isEnabled());
2597
2598 // Prepare associated info.
2599 constexpr uint8_t hdmi = 1;
2600 const std::string UNIQUE_ID = "local:1";
2601
2602 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi);
2603 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2604 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2605 // Device should be disabled because it is associated with a specific display via
2606 // input port <-> display port association, but the corresponding display is not found
2607 ASSERT_FALSE(mDevice->isEnabled());
2608
2609 // Prepare displays.
2610 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002611 DISPLAY_ORIENTATION_0, true /*isActive*/, UNIQUE_ID, hdmi,
2612 ViewportType::INTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002613 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2614 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2615 ASSERT_TRUE(mDevice->isEnabled());
2616
2617 // Device should be disabled after set disable.
2618 mFakePolicy->addDisabledDevice(mDevice->getId());
2619 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2620 InputReaderConfiguration::CHANGE_ENABLED_STATE);
2621 ASSERT_FALSE(mDevice->isEnabled());
2622
2623 // Device should still be disabled even found the associated display.
2624 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2625 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2626 ASSERT_FALSE(mDevice->isEnabled());
2627}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002628
Christine Franks1ba71cc2021-04-07 14:37:42 -07002629TEST_F(InputDeviceTest, Configure_AssignsDisplayUniqueId) {
2630 // Device should be enabled by default.
2631 mFakePolicy->clearViewports();
2632 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
2633 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
2634 ASSERT_TRUE(mDevice->isEnabled());
2635
2636 // Device should be disabled because it is associated with a specific display, but the
2637 // corresponding display is not found.
2638 const std::string DISPLAY_UNIQUE_ID = "displayUniqueId";
2639 mFakePolicy->addInputUniqueIdAssociation(DEVICE_NAME, DISPLAY_UNIQUE_ID);
2640 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2641 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2642 ASSERT_FALSE(mDevice->isEnabled());
2643
2644 // Device should be enabled when a display is found.
2645 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2646 DISPLAY_ORIENTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
2647 NO_PORT, ViewportType::INTERNAL);
2648 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2649 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2650 ASSERT_TRUE(mDevice->isEnabled());
2651
2652 // Device should be disabled after set disable.
2653 mFakePolicy->addDisabledDevice(mDevice->getId());
2654 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2655 InputReaderConfiguration::CHANGE_ENABLED_STATE);
2656 ASSERT_FALSE(mDevice->isEnabled());
2657
2658 // Device should still be disabled even found the associated display.
2659 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2660 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2661 ASSERT_FALSE(mDevice->isEnabled());
2662}
2663
Michael Wrightd02c5b62014-02-10 15:10:22 -08002664// --- InputMapperTest ---
2665
2666class InputMapperTest : public testing::Test {
2667protected:
2668 static const char* DEVICE_NAME;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002669 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002670 static const int32_t DEVICE_ID;
2671 static const int32_t DEVICE_GENERATION;
2672 static const int32_t DEVICE_CONTROLLER_NUMBER;
Chris Ye1b0c7342020-07-28 21:57:03 -07002673 static const Flags<InputDeviceClass> DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002674 static const int32_t EVENTHUB_ID;
Prabir Pradhanc14266f2021-05-12 15:56:24 -07002675 static const std::optional<bool> INITIAL_PER_WINDOW_INPUT_ROTATION_FLAG_VALUE;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002676
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002677 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002678 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08002679 sp<TestInputListener> mFakeListener;
arthurhungdcef2dc2020-08-11 14:47:50 +08002680 std::unique_ptr<InstrumentedInputReader> mReader;
2681 std::shared_ptr<InputDevice> mDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002682
Chris Ye1b0c7342020-07-28 21:57:03 -07002683 virtual void SetUp(Flags<InputDeviceClass> classes) {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002684 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002685 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08002686 mFakeListener = new TestInputListener();
arthurhungdcef2dc2020-08-11 14:47:50 +08002687 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
2688 mFakeListener);
2689 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002690 }
2691
Prabir Pradhanc14266f2021-05-12 15:56:24 -07002692 void SetUp() override {
2693 // Ensure per_window_input_rotation is enabled.
2694 sysprop::InputFlingerProperties::per_window_input_rotation(true);
2695
2696 SetUp(DEVICE_CLASSES);
2697 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002698
Chris Yea52ade12020-08-27 16:49:20 -07002699 void TearDown() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002700 mFakeListener.clear();
2701 mFakePolicy.clear();
Prabir Pradhanc14266f2021-05-12 15:56:24 -07002702
2703 sysprop::InputFlingerProperties::per_window_input_rotation(
2704 INITIAL_PER_WINDOW_INPUT_ROTATION_FLAG_VALUE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002705 }
2706
2707 void addConfigurationProperty(const char* key, const char* value) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002708 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, String8(key), String8(value));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002709 }
2710
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002711 void configureDevice(uint32_t changes) {
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -08002712 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
arthurhungdcef2dc2020-08-11 14:47:50 +08002713 mReader->requestRefreshConfiguration(changes);
2714 mReader->loopOnce();
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -08002715 }
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002716 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
2717 }
2718
arthurhungdcef2dc2020-08-11 14:47:50 +08002719 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
2720 const std::string& location, int32_t eventHubId,
2721 Flags<InputDeviceClass> classes) {
2722 InputDeviceIdentifier identifier;
2723 identifier.name = name;
2724 identifier.location = location;
2725 std::shared_ptr<InputDevice> device =
2726 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
2727 identifier);
2728 mReader->pushNextDevice(device);
2729 mFakeEventHub->addDevice(eventHubId, name, classes);
2730 mReader->loopOnce();
2731 return device;
2732 }
2733
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002734 template <class T, typename... Args>
2735 T& addMapperAndConfigure(Args... args) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002736 T& mapper = mDevice->addMapper<T>(EVENTHUB_ID, args...);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002737 configureDevice(0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002738 mDevice->reset(ARBITRARY_TIME);
Chris Ye42b06822020-08-07 11:39:33 -07002739 mapper.reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002740 return mapper;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002741 }
2742
2743 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002744 int32_t orientation, const std::string& uniqueId,
2745 std::optional<uint8_t> physicalPort, ViewportType viewportType) {
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002746 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, true /*isActive*/,
2747 uniqueId, physicalPort, viewportType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07002748 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2749 }
2750
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002751 void clearViewports() {
2752 mFakePolicy->clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002753 }
2754
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002755 void process(InputMapper& mapper, nsecs_t when, nsecs_t readTime, int32_t type, int32_t code,
2756 int32_t value) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002757 RawEvent event;
2758 event.when = when;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002759 event.readTime = readTime;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002760 event.deviceId = mapper.getDeviceContext().getEventHubId();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002761 event.type = type;
2762 event.code = code;
2763 event.value = value;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002764 mapper.process(&event);
arthurhungdcef2dc2020-08-11 14:47:50 +08002765 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002766 }
2767
2768 static void assertMotionRange(const InputDeviceInfo& info,
2769 int32_t axis, uint32_t source, float min, float max, float flat, float fuzz) {
2770 const InputDeviceInfo::MotionRange* range = info.getMotionRange(axis, source);
Yi Kong9b14ac62018-07-17 13:48:38 -07002771 ASSERT_TRUE(range != nullptr) << "Axis: " << axis << " Source: " << source;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002772 ASSERT_EQ(axis, range->axis) << "Axis: " << axis << " Source: " << source;
2773 ASSERT_EQ(source, range->source) << "Axis: " << axis << " Source: " << source;
2774 ASSERT_NEAR(min, range->min, EPSILON) << "Axis: " << axis << " Source: " << source;
2775 ASSERT_NEAR(max, range->max, EPSILON) << "Axis: " << axis << " Source: " << source;
2776 ASSERT_NEAR(flat, range->flat, EPSILON) << "Axis: " << axis << " Source: " << source;
2777 ASSERT_NEAR(fuzz, range->fuzz, EPSILON) << "Axis: " << axis << " Source: " << source;
2778 }
2779
Prabir Pradhanf5334b82021-05-13 14:00:39 -07002780 static void assertPointerCoords(const PointerCoords& coords, float x, float y, float pressure,
2781 float size, float touchMajor, float touchMinor, float toolMajor,
2782 float toolMinor, float orientation, float distance,
2783 float scaledAxisEpsilon = 1.f) {
2784 ASSERT_NEAR(x, coords.getAxisValue(AMOTION_EVENT_AXIS_X), scaledAxisEpsilon);
2785 ASSERT_NEAR(y, coords.getAxisValue(AMOTION_EVENT_AXIS_Y), scaledAxisEpsilon);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002786 ASSERT_NEAR(pressure, coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), EPSILON);
2787 ASSERT_NEAR(size, coords.getAxisValue(AMOTION_EVENT_AXIS_SIZE), EPSILON);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07002788 ASSERT_NEAR(touchMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR),
2789 scaledAxisEpsilon);
2790 ASSERT_NEAR(touchMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR),
2791 scaledAxisEpsilon);
2792 ASSERT_NEAR(toolMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR),
2793 scaledAxisEpsilon);
2794 ASSERT_NEAR(toolMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR),
2795 scaledAxisEpsilon);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002796 ASSERT_NEAR(orientation, coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION), EPSILON);
2797 ASSERT_NEAR(distance, coords.getAxisValue(AMOTION_EVENT_AXIS_DISTANCE), EPSILON);
2798 }
2799
Michael Wright17db18e2020-06-26 20:51:44 +01002800 static void assertPosition(const FakePointerController& controller, float x, float y) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002801 float actualX, actualY;
Michael Wright17db18e2020-06-26 20:51:44 +01002802 controller.getPosition(&actualX, &actualY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002803 ASSERT_NEAR(x, actualX, 1);
2804 ASSERT_NEAR(y, actualY, 1);
2805 }
2806};
2807
2808const char* InputMapperTest::DEVICE_NAME = "device";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002809const char* InputMapperTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002810const int32_t InputMapperTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002811const int32_t InputMapperTest::DEVICE_GENERATION = 2;
2812const int32_t InputMapperTest::DEVICE_CONTROLLER_NUMBER = 0;
Chris Ye1b0c7342020-07-28 21:57:03 -07002813const Flags<InputDeviceClass> InputMapperTest::DEVICE_CLASSES =
2814 Flags<InputDeviceClass>(0); // not needed for current tests
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002815const int32_t InputMapperTest::EVENTHUB_ID = 1;
Prabir Pradhanc14266f2021-05-12 15:56:24 -07002816const std::optional<bool> InputMapperTest::INITIAL_PER_WINDOW_INPUT_ROTATION_FLAG_VALUE =
2817 sysprop::InputFlingerProperties::per_window_input_rotation();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002818
2819// --- SwitchInputMapperTest ---
2820
2821class SwitchInputMapperTest : public InputMapperTest {
2822protected:
2823};
2824
2825TEST_F(SwitchInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002826 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002827
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002828 ASSERT_EQ(uint32_t(AINPUT_SOURCE_SWITCH), mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002829}
2830
2831TEST_F(SwitchInputMapperTest, GetSwitchState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002832 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002833
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002834 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002835 ASSERT_EQ(1, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002836
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002837 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002838 ASSERT_EQ(0, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002839}
2840
2841TEST_F(SwitchInputMapperTest, Process) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002842 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002843
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002844 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_LID, 1);
2845 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_JACK_PHYSICAL_INSERT, 1);
2846 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_HEADPHONE_INSERT, 0);
2847 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002848
2849 NotifySwitchArgs args;
2850 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySwitchWasCalled(&args));
2851 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
Dan Albert1bd2fc02016-02-02 15:11:57 -08002852 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT), args.switchValues);
2853 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT) | (1 << SW_HEADPHONE_INSERT),
Michael Wrightd02c5b62014-02-10 15:10:22 -08002854 args.switchMask);
2855 ASSERT_EQ(uint32_t(0), args.policyFlags);
2856}
2857
Chris Ye87143712020-11-10 05:05:58 +00002858// --- VibratorInputMapperTest ---
2859class VibratorInputMapperTest : public InputMapperTest {
2860protected:
2861 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::VIBRATOR); }
2862};
2863
2864TEST_F(VibratorInputMapperTest, GetSources) {
2865 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
2866
2867 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mapper.getSources());
2868}
2869
2870TEST_F(VibratorInputMapperTest, GetVibratorIds) {
2871 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
2872
2873 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
2874}
2875
2876TEST_F(VibratorInputMapperTest, Vibrate) {
2877 constexpr uint8_t DEFAULT_AMPLITUDE = 192;
Chris Yefb552902021-02-03 17:18:37 -08002878 constexpr int32_t VIBRATION_TOKEN = 100;
Chris Ye87143712020-11-10 05:05:58 +00002879 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
2880
2881 VibrationElement pattern(2);
2882 VibrationSequence sequence(2);
2883 pattern.duration = std::chrono::milliseconds(200);
2884 pattern.channels = {{0 /* vibratorId */, DEFAULT_AMPLITUDE / 2},
2885 {1 /* vibratorId */, DEFAULT_AMPLITUDE}};
2886 sequence.addElement(pattern);
2887 pattern.duration = std::chrono::milliseconds(500);
2888 pattern.channels = {{0 /* vibratorId */, DEFAULT_AMPLITUDE / 4},
2889 {1 /* vibratorId */, DEFAULT_AMPLITUDE}};
2890 sequence.addElement(pattern);
2891
2892 std::vector<int64_t> timings = {0, 1};
2893 std::vector<uint8_t> amplitudes = {DEFAULT_AMPLITUDE, DEFAULT_AMPLITUDE / 2};
2894
2895 ASSERT_FALSE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08002896 // Start vibrating
2897 mapper.vibrate(sequence, -1 /* repeat */, VIBRATION_TOKEN);
Chris Ye87143712020-11-10 05:05:58 +00002898 ASSERT_TRUE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08002899 // Verify vibrator state listener was notified.
2900 mReader->loopOnce();
2901 NotifyVibratorStateArgs args;
2902 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyVibratorStateWasCalled(&args));
2903 ASSERT_EQ(DEVICE_ID, args.deviceId);
2904 ASSERT_TRUE(args.isOn);
2905 // Stop vibrating
2906 mapper.cancelVibrate(VIBRATION_TOKEN);
2907 ASSERT_FALSE(mapper.isVibrating());
2908 // Verify vibrator state listener was notified.
2909 mReader->loopOnce();
2910 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyVibratorStateWasCalled(&args));
2911 ASSERT_EQ(DEVICE_ID, args.deviceId);
2912 ASSERT_FALSE(args.isOn);
Chris Ye87143712020-11-10 05:05:58 +00002913}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002914
Chris Yef59a2f42020-10-16 12:55:26 -07002915// --- SensorInputMapperTest ---
2916
2917class SensorInputMapperTest : public InputMapperTest {
2918protected:
2919 static const int32_t ACCEL_RAW_MIN;
2920 static const int32_t ACCEL_RAW_MAX;
2921 static const int32_t ACCEL_RAW_FUZZ;
2922 static const int32_t ACCEL_RAW_FLAT;
2923 static const int32_t ACCEL_RAW_RESOLUTION;
2924
2925 static const int32_t GYRO_RAW_MIN;
2926 static const int32_t GYRO_RAW_MAX;
2927 static const int32_t GYRO_RAW_FUZZ;
2928 static const int32_t GYRO_RAW_FLAT;
2929 static const int32_t GYRO_RAW_RESOLUTION;
2930
2931 static const float GRAVITY_MS2_UNIT;
2932 static const float DEGREE_RADIAN_UNIT;
2933
2934 void prepareAccelAxes();
2935 void prepareGyroAxes();
2936 void setAccelProperties();
2937 void setGyroProperties();
2938 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::SENSOR); }
2939};
2940
2941const int32_t SensorInputMapperTest::ACCEL_RAW_MIN = -32768;
2942const int32_t SensorInputMapperTest::ACCEL_RAW_MAX = 32768;
2943const int32_t SensorInputMapperTest::ACCEL_RAW_FUZZ = 16;
2944const int32_t SensorInputMapperTest::ACCEL_RAW_FLAT = 0;
2945const int32_t SensorInputMapperTest::ACCEL_RAW_RESOLUTION = 8192;
2946
2947const int32_t SensorInputMapperTest::GYRO_RAW_MIN = -2097152;
2948const int32_t SensorInputMapperTest::GYRO_RAW_MAX = 2097152;
2949const int32_t SensorInputMapperTest::GYRO_RAW_FUZZ = 16;
2950const int32_t SensorInputMapperTest::GYRO_RAW_FLAT = 0;
2951const int32_t SensorInputMapperTest::GYRO_RAW_RESOLUTION = 1024;
2952
2953const float SensorInputMapperTest::GRAVITY_MS2_UNIT = 9.80665f;
2954const float SensorInputMapperTest::DEGREE_RADIAN_UNIT = 0.0174533f;
2955
2956void SensorInputMapperTest::prepareAccelAxes() {
2957 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
2958 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
2959 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
2960 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
2961 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Z, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
2962 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
2963}
2964
2965void SensorInputMapperTest::prepareGyroAxes() {
2966 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RX, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
2967 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
2968 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RY, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
2969 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
2970 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RZ, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
2971 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
2972}
2973
2974void SensorInputMapperTest::setAccelProperties() {
2975 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 0, InputDeviceSensorType::ACCELEROMETER,
2976 /* sensorDataIndex */ 0);
2977 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 1, InputDeviceSensorType::ACCELEROMETER,
2978 /* sensorDataIndex */ 1);
2979 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 2, InputDeviceSensorType::ACCELEROMETER,
2980 /* sensorDataIndex */ 2);
2981 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
2982 addConfigurationProperty("sensor.accelerometer.reportingMode", "0");
2983 addConfigurationProperty("sensor.accelerometer.maxDelay", "100000");
2984 addConfigurationProperty("sensor.accelerometer.minDelay", "5000");
2985 addConfigurationProperty("sensor.accelerometer.power", "1.5");
2986}
2987
2988void SensorInputMapperTest::setGyroProperties() {
2989 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 3, InputDeviceSensorType::GYROSCOPE,
2990 /* sensorDataIndex */ 0);
2991 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 4, InputDeviceSensorType::GYROSCOPE,
2992 /* sensorDataIndex */ 1);
2993 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 5, InputDeviceSensorType::GYROSCOPE,
2994 /* sensorDataIndex */ 2);
2995 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
2996 addConfigurationProperty("sensor.gyroscope.reportingMode", "0");
2997 addConfigurationProperty("sensor.gyroscope.maxDelay", "100000");
2998 addConfigurationProperty("sensor.gyroscope.minDelay", "5000");
2999 addConfigurationProperty("sensor.gyroscope.power", "0.8");
3000}
3001
3002TEST_F(SensorInputMapperTest, GetSources) {
3003 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3004
3005 ASSERT_EQ(static_cast<uint32_t>(AINPUT_SOURCE_SENSOR), mapper.getSources());
3006}
3007
3008TEST_F(SensorInputMapperTest, ProcessAccelerometerSensor) {
3009 setAccelProperties();
3010 prepareAccelAxes();
3011 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3012
3013 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::ACCELEROMETER,
3014 std::chrono::microseconds(10000),
3015 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08003016 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003017 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, 20000);
3018 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, -20000);
3019 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Z, 40000);
3020 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
3021 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07003022
3023 NotifySensorArgs args;
3024 std::vector<float> values = {20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
3025 -20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
3026 40000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT};
3027
3028 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
3029 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
3030 ASSERT_EQ(args.deviceId, DEVICE_ID);
3031 ASSERT_EQ(args.sensorType, InputDeviceSensorType::ACCELEROMETER);
3032 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
3033 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
3034 ASSERT_EQ(args.values, values);
3035 mapper.flushSensor(InputDeviceSensorType::ACCELEROMETER);
3036}
3037
3038TEST_F(SensorInputMapperTest, ProcessGyroscopeSensor) {
3039 setGyroProperties();
3040 prepareGyroAxes();
3041 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3042
3043 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::GYROSCOPE,
3044 std::chrono::microseconds(10000),
3045 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08003046 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003047 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RX, 20000);
3048 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RY, -20000);
3049 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RZ, 40000);
3050 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
3051 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07003052
3053 NotifySensorArgs args;
3054 std::vector<float> values = {20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
3055 -20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
3056 40000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT};
3057
3058 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
3059 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
3060 ASSERT_EQ(args.deviceId, DEVICE_ID);
3061 ASSERT_EQ(args.sensorType, InputDeviceSensorType::GYROSCOPE);
3062 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
3063 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
3064 ASSERT_EQ(args.values, values);
3065 mapper.flushSensor(InputDeviceSensorType::GYROSCOPE);
3066}
3067
Michael Wrightd02c5b62014-02-10 15:10:22 -08003068// --- KeyboardInputMapperTest ---
3069
3070class KeyboardInputMapperTest : public InputMapperTest {
3071protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003072 const std::string UNIQUE_ID = "local:0";
3073
3074 void prepareDisplay(int32_t orientation);
3075
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003076 void testDPadKeyRotation(KeyboardInputMapper& mapper, int32_t originalScanCode,
Arthur Hung2c9a3342019-07-23 14:18:59 +08003077 int32_t originalKeyCode, int32_t rotatedKeyCode,
3078 int32_t displayId = ADISPLAY_ID_NONE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003079};
3080
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003081/* Similar to setDisplayInfoAndReconfigure, but pre-populates all parameters except for the
3082 * orientation.
3083 */
3084void KeyboardInputMapperTest::prepareDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003085 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
3086 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003087}
3088
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003089void KeyboardInputMapperTest::testDPadKeyRotation(KeyboardInputMapper& mapper,
Arthur Hung2c9a3342019-07-23 14:18:59 +08003090 int32_t originalScanCode, int32_t originalKeyCode,
3091 int32_t rotatedKeyCode, int32_t displayId) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003092 NotifyKeyArgs args;
3093
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003094 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003095 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3096 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3097 ASSERT_EQ(originalScanCode, args.scanCode);
3098 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003099 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003100
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003101 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003102 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3103 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3104 ASSERT_EQ(originalScanCode, args.scanCode);
3105 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003106 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003107}
3108
Michael Wrightd02c5b62014-02-10 15:10:22 -08003109TEST_F(KeyboardInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003110 KeyboardInputMapper& mapper =
3111 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3112 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003113
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003114 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003115}
3116
3117TEST_F(KeyboardInputMapperTest, Process_SimpleKeyPress) {
3118 const int32_t USAGE_A = 0x070004;
3119 const int32_t USAGE_UNKNOWN = 0x07ffff;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003120 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3121 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
Chris Yea52ade12020-08-27 16:49:20 -07003122 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, POLICY_FLAG_WAKE);
3123 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, POLICY_FLAG_WAKE);
3124 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003125
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003126 KeyboardInputMapper& mapper =
3127 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3128 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
arthurhungc903df12020-08-11 15:08:42 +08003129 // Initial metastate to AMETA_NONE.
3130 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
3131 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003132
3133 // Key down by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003134 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003135 NotifyKeyArgs args;
3136 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3137 ASSERT_EQ(DEVICE_ID, args.deviceId);
3138 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3139 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3140 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3141 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3142 ASSERT_EQ(KEY_HOME, args.scanCode);
3143 ASSERT_EQ(AMETA_NONE, args.metaState);
3144 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3145 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3146 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3147
3148 // Key up by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003149 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003150 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3151 ASSERT_EQ(DEVICE_ID, args.deviceId);
3152 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3153 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3154 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3155 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3156 ASSERT_EQ(KEY_HOME, args.scanCode);
3157 ASSERT_EQ(AMETA_NONE, args.metaState);
3158 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3159 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3160 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3161
3162 // Key down by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003163 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3164 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, 0, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003165 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3166 ASSERT_EQ(DEVICE_ID, args.deviceId);
3167 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3168 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3169 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3170 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3171 ASSERT_EQ(0, args.scanCode);
3172 ASSERT_EQ(AMETA_NONE, args.metaState);
3173 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3174 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3175 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3176
3177 // Key up by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003178 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3179 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003180 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3181 ASSERT_EQ(DEVICE_ID, args.deviceId);
3182 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3183 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3184 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3185 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3186 ASSERT_EQ(0, args.scanCode);
3187 ASSERT_EQ(AMETA_NONE, args.metaState);
3188 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3189 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3190 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3191
3192 // Key down with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003193 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3194 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UNKNOWN, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003195 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3196 ASSERT_EQ(DEVICE_ID, args.deviceId);
3197 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3198 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3199 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3200 ASSERT_EQ(0, args.keyCode);
3201 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3202 ASSERT_EQ(AMETA_NONE, args.metaState);
3203 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3204 ASSERT_EQ(0U, args.policyFlags);
3205 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3206
3207 // Key up with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003208 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3209 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003210 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3211 ASSERT_EQ(DEVICE_ID, args.deviceId);
3212 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3213 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3214 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3215 ASSERT_EQ(0, args.keyCode);
3216 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3217 ASSERT_EQ(AMETA_NONE, args.metaState);
3218 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3219 ASSERT_EQ(0U, args.policyFlags);
3220 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3221}
3222
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003223/**
3224 * Ensure that the readTime is set to the time when the EV_KEY is received.
3225 */
3226TEST_F(KeyboardInputMapperTest, Process_SendsReadTime) {
3227 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3228
3229 KeyboardInputMapper& mapper =
3230 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3231 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3232 NotifyKeyArgs args;
3233
3234 // Key down
3235 process(mapper, ARBITRARY_TIME, 12 /*readTime*/, EV_KEY, KEY_HOME, 1);
3236 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3237 ASSERT_EQ(12, args.readTime);
3238
3239 // Key up
3240 process(mapper, ARBITRARY_TIME, 15 /*readTime*/, EV_KEY, KEY_HOME, 1);
3241 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3242 ASSERT_EQ(15, args.readTime);
3243}
3244
Michael Wrightd02c5b62014-02-10 15:10:22 -08003245TEST_F(KeyboardInputMapperTest, Process_ShouldUpdateMetaState) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003246 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFTSHIFT, 0, AKEYCODE_SHIFT_LEFT, 0);
3247 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003248 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, 0);
3249 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, 0);
3250 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003251
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003252 KeyboardInputMapper& mapper =
3253 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3254 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003255
arthurhungc903df12020-08-11 15:08:42 +08003256 // Initial metastate to AMETA_NONE.
3257 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
3258 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003259
3260 // Metakey down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003261 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003262 NotifyKeyArgs args;
3263 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3264 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003265 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003266 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003267
3268 // Key down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003269 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_A, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003270 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3271 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003272 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003273
3274 // Key up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003275 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, KEY_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003276 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3277 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003278 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003279
3280 // Metakey up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003281 process(mapper, ARBITRARY_TIME + 3, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003282 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3283 ASSERT_EQ(AMETA_NONE, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003284 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003285 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003286}
3287
3288TEST_F(KeyboardInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003289 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3290 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3291 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3292 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003293
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003294 KeyboardInputMapper& mapper =
3295 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3296 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003297
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003298 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003299 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3300 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
3301 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3302 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
3303 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3304 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
3305 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3306 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
3307}
3308
3309TEST_F(KeyboardInputMapperTest, Process_WhenOrientationAware_ShouldRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003310 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3311 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3312 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3313 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003314
Michael Wrightd02c5b62014-02-10 15:10:22 -08003315 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003316 KeyboardInputMapper& mapper =
3317 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3318 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003319
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003320 prepareDisplay(DISPLAY_ORIENTATION_0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003321 ASSERT_NO_FATAL_FAILURE(
3322 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3323 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3324 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3325 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3326 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3327 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3328 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003329
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003330 clearViewports();
3331 prepareDisplay(DISPLAY_ORIENTATION_90);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003332 ASSERT_NO_FATAL_FAILURE(
3333 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3334 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3335 AKEYCODE_DPAD_UP, DISPLAY_ID));
3336 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3337 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3338 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3339 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003340
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003341 clearViewports();
3342 prepareDisplay(DISPLAY_ORIENTATION_180);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003343 ASSERT_NO_FATAL_FAILURE(
3344 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3345 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3346 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3347 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3348 AKEYCODE_DPAD_UP, DISPLAY_ID));
3349 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3350 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003351
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003352 clearViewports();
3353 prepareDisplay(DISPLAY_ORIENTATION_270);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003354 ASSERT_NO_FATAL_FAILURE(
3355 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3356 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3357 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3358 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3359 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3360 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3361 AKEYCODE_DPAD_UP, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003362
3363 // Special case: if orientation changes while key is down, we still emit the same keycode
3364 // in the key up as we did in the key down.
3365 NotifyKeyArgs args;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003366 clearViewports();
3367 prepareDisplay(DISPLAY_ORIENTATION_270);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003368 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003369 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3370 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3371 ASSERT_EQ(KEY_UP, args.scanCode);
3372 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3373
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003374 clearViewports();
3375 prepareDisplay(DISPLAY_ORIENTATION_180);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003376 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003377 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3378 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3379 ASSERT_EQ(KEY_UP, args.scanCode);
3380 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3381}
3382
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003383TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_NotOrientationAware) {
3384 // If the keyboard is not orientation aware,
3385 // key events should not be associated with a specific display id
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003386 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003387
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003388 KeyboardInputMapper& mapper =
3389 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3390 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003391 NotifyKeyArgs args;
3392
3393 // Display id should be ADISPLAY_ID_NONE without any display configuration.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003394 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003395 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003396 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003397 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3398 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3399
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003400 prepareDisplay(DISPLAY_ORIENTATION_0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003401 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003402 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003403 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003404 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3405 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3406}
3407
3408TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_OrientationAware) {
3409 // If the keyboard is orientation aware,
3410 // key events should be associated with the internal viewport
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003411 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003412
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003413 addConfigurationProperty("keyboard.orientationAware", "1");
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.
3420 // ^--- already checked by the previous test
3421
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003422 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003423 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003424 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003425 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003426 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003427 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3428 ASSERT_EQ(DISPLAY_ID, args.displayId);
3429
3430 constexpr int32_t newDisplayId = 2;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003431 clearViewports();
3432 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003433 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003434 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003435 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003436 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003437 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3438 ASSERT_EQ(newDisplayId, args.displayId);
3439}
3440
Michael Wrightd02c5b62014-02-10 15:10:22 -08003441TEST_F(KeyboardInputMapperTest, GetKeyCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003442 KeyboardInputMapper& mapper =
3443 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3444 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003445
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003446 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003447 ASSERT_EQ(1, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003448
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003449 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003450 ASSERT_EQ(0, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003451}
3452
3453TEST_F(KeyboardInputMapperTest, GetScanCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003454 KeyboardInputMapper& mapper =
3455 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3456 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003457
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003458 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003459 ASSERT_EQ(1, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003460
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003461 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003462 ASSERT_EQ(0, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003463}
3464
3465TEST_F(KeyboardInputMapperTest, MarkSupportedKeyCodes) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003466 KeyboardInputMapper& mapper =
3467 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3468 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003469
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003470 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003471
3472 const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
3473 uint8_t flags[2] = { 0, 0 };
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003474 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, 1, keyCodes, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003475 ASSERT_TRUE(flags[0]);
3476 ASSERT_FALSE(flags[1]);
3477}
3478
3479TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleMetaStateAndLeds) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003480 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3481 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3482 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3483 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3484 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3485 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003486
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003487 KeyboardInputMapper& mapper =
3488 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3489 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Chris Yea52ade12020-08-27 16:49:20 -07003490 // Initialize metastate to AMETA_NUM_LOCK_ON.
arthurhungc903df12020-08-11 15:08:42 +08003491 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
3492 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003493
3494 // Initialization should have turned all of the lights off.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003495 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3496 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3497 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003498
3499 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003500 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3501 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003502 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3503 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3504 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003505 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003506
3507 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003508 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3509 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003510 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3511 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3512 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003513 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003514
3515 // Toggle caps lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003516 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3517 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003518 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3519 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3520 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003521 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003522
3523 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003524 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3525 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003526 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3527 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3528 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003529 ASSERT_EQ(AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003530
3531 // Toggle num lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003532 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3533 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003534 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3535 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3536 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003537 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003538
3539 // Toggle scroll lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003540 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3541 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003542 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3543 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3544 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003545 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003546}
3547
Chris Yea52ade12020-08-27 16:49:20 -07003548TEST_F(KeyboardInputMapperTest, NoMetaStateWhenMetaKeysNotPresent) {
3549 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_BUTTON_A, 0);
3550 mFakeEventHub->addKey(EVENTHUB_ID, BTN_B, 0, AKEYCODE_BUTTON_B, 0);
3551 mFakeEventHub->addKey(EVENTHUB_ID, BTN_X, 0, AKEYCODE_BUTTON_X, 0);
3552 mFakeEventHub->addKey(EVENTHUB_ID, BTN_Y, 0, AKEYCODE_BUTTON_Y, 0);
3553
3554 KeyboardInputMapper& mapper =
3555 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3556 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
3557
3558 // Initial metastate should be AMETA_NONE as no meta keys added.
3559 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3560 // Meta state should be AMETA_NONE after reset
3561 mapper.reset(ARBITRARY_TIME);
3562 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3563 // Meta state should be AMETA_NONE with update, as device doesn't have the keys.
3564 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
3565 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3566
3567 NotifyKeyArgs args;
3568 // Press button "A"
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003569 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_A, 1);
Chris Yea52ade12020-08-27 16:49:20 -07003570 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3571 ASSERT_EQ(AMETA_NONE, args.metaState);
3572 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3573 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3574 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3575
3576 // Button up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003577 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003578 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3579 ASSERT_EQ(AMETA_NONE, args.metaState);
3580 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3581 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3582 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3583}
3584
Arthur Hung2c9a3342019-07-23 14:18:59 +08003585TEST_F(KeyboardInputMapperTest, Configure_AssignsDisplayPort) {
3586 // keyboard 1.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003587 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3588 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3589 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3590 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003591
3592 // keyboard 2.
3593 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08003594 const std::string DEVICE_NAME2 = "KEYBOARD2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08003595 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003596 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08003597 std::shared_ptr<InputDevice> device2 =
3598 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
3599 Flags<InputDeviceClass>(0));
3600
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003601 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3602 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3603 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3604 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003605
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003606 KeyboardInputMapper& mapper =
3607 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3608 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003609
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003610 KeyboardInputMapper& mapper2 =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003611 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003612 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003613 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
3614 device2->reset(ARBITRARY_TIME);
3615
3616 // Prepared displays and associated info.
3617 constexpr uint8_t hdmi1 = 0;
3618 constexpr uint8_t hdmi2 = 1;
3619 const std::string SECONDARY_UNIQUE_ID = "local:1";
3620
3621 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
3622 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
3623
3624 // No associated display viewport found, should disable the device.
3625 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3626 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3627 ASSERT_FALSE(device2->isEnabled());
3628
3629 // Prepare second display.
3630 constexpr int32_t newDisplayId = 2;
3631 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003632 UNIQUE_ID, hdmi1, ViewportType::INTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003633 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003634 SECONDARY_UNIQUE_ID, hdmi2, ViewportType::EXTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003635 // Default device will reconfigure above, need additional reconfiguration for another device.
3636 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3637 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3638
3639 // Device should be enabled after the associated display is found.
3640 ASSERT_TRUE(mDevice->isEnabled());
3641 ASSERT_TRUE(device2->isEnabled());
3642
3643 // Test pad key events
3644 ASSERT_NO_FATAL_FAILURE(
3645 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3646 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3647 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3648 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3649 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3650 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3651 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3652
3653 ASSERT_NO_FATAL_FAILURE(
3654 testDPadKeyRotation(mapper2, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, newDisplayId));
3655 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3656 AKEYCODE_DPAD_RIGHT, newDisplayId));
3657 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3658 AKEYCODE_DPAD_DOWN, newDisplayId));
3659 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3660 AKEYCODE_DPAD_LEFT, newDisplayId));
3661}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003662
arthurhungc903df12020-08-11 15:08:42 +08003663TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleAfterReattach) {
3664 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3665 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3666 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3667 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3668 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3669 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3670
3671 KeyboardInputMapper& mapper =
3672 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3673 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3674 // Initial metastate to AMETA_NONE.
3675 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
3676 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
3677
3678 // Initialization should have turned all of the lights off.
3679 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3680 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3681 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3682
3683 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003684 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3685 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003686 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3687 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
3688
3689 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003690 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3691 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003692 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3693 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
3694
3695 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003696 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3697 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003698 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3699 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
3700
3701 mFakeEventHub->removeDevice(EVENTHUB_ID);
3702 mReader->loopOnce();
3703
3704 // keyboard 2 should default toggle keys.
3705 const std::string USB2 = "USB2";
3706 const std::string DEVICE_NAME2 = "KEYBOARD2";
3707 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
3708 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
3709 std::shared_ptr<InputDevice> device2 =
3710 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
3711 Flags<InputDeviceClass>(0));
3712 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3713 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
3714 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3715 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3716 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3717 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3718
arthurhung6fe95782020-10-05 22:41:16 +08003719 KeyboardInputMapper& mapper2 =
3720 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
3721 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
arthurhungc903df12020-08-11 15:08:42 +08003722 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
3723 device2->reset(ARBITRARY_TIME);
3724
3725 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_CAPSL));
3726 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_NUML));
3727 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_SCROLLL));
arthurhung6fe95782020-10-05 22:41:16 +08003728 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON,
3729 mapper2.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08003730}
3731
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003732// --- KeyboardInputMapperTest_ExternalDevice ---
3733
3734class KeyboardInputMapperTest_ExternalDevice : public InputMapperTest {
3735protected:
Chris Yea52ade12020-08-27 16:49:20 -07003736 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003737};
3738
3739TEST_F(KeyboardInputMapperTest_ExternalDevice, WakeBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07003740 // For external devices, non-media keys will trigger wake on key down. Media keys need to be
3741 // marked as WAKE in the keylayout file to trigger wake.
Powei Fengd041c5d2019-05-03 17:11:33 -07003742
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003743 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, 0);
3744 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, 0);
3745 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAYPAUSE, 0, AKEYCODE_MEDIA_PLAY_PAUSE,
3746 POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07003747
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003748 KeyboardInputMapper& mapper =
3749 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3750 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07003751
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003752 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003753 NotifyKeyArgs args;
3754 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3755 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3756
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003757 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003758 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3759 ASSERT_EQ(uint32_t(0), args.policyFlags);
3760
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003761 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003762 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3763 ASSERT_EQ(uint32_t(0), args.policyFlags);
3764
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003765 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003766 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3767 ASSERT_EQ(uint32_t(0), args.policyFlags);
3768
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003769 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003770 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3771 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3772
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003773 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003774 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3775 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3776}
3777
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003778TEST_F(KeyboardInputMapperTest_ExternalDevice, DoNotWakeByDefaultBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07003779 // Tv Remote key's wake behavior is prescribed by the keylayout file.
Powei Fengd041c5d2019-05-03 17:11:33 -07003780
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003781 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3782 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3783 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07003784
Powei Fengd041c5d2019-05-03 17:11:33 -07003785 addConfigurationProperty("keyboard.doNotWakeByDefault", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003786 KeyboardInputMapper& mapper =
3787 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3788 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07003789
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003790 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003791 NotifyKeyArgs args;
3792 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3793 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3794
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003795 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003796 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3797 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3798
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003799 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_DOWN, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003800 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3801 ASSERT_EQ(uint32_t(0), args.policyFlags);
3802
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003803 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_DOWN, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003804 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3805 ASSERT_EQ(uint32_t(0), args.policyFlags);
3806
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003807 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003808 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3809 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3810
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003811 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003812 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3813 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3814}
3815
Michael Wrightd02c5b62014-02-10 15:10:22 -08003816// --- CursorInputMapperTest ---
3817
3818class CursorInputMapperTest : public InputMapperTest {
3819protected:
3820 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD;
3821
Michael Wright17db18e2020-06-26 20:51:44 +01003822 std::shared_ptr<FakePointerController> mFakePointerController;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003823
Chris Yea52ade12020-08-27 16:49:20 -07003824 void SetUp() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003825 InputMapperTest::SetUp();
3826
Michael Wright17db18e2020-06-26 20:51:44 +01003827 mFakePointerController = std::make_shared<FakePointerController>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003828 mFakePolicy->setPointerController(mDevice->getId(), mFakePointerController);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003829 }
3830
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003831 void testMotionRotation(CursorInputMapper& mapper, int32_t originalX, int32_t originalY,
3832 int32_t rotatedX, int32_t rotatedY);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003833
3834 void prepareDisplay(int32_t orientation) {
3835 const std::string uniqueId = "local:0";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003836 const ViewportType viewportType = ViewportType::INTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003837 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
3838 orientation, uniqueId, NO_PORT, viewportType);
3839 }
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003840
3841 static void assertCursorPointerCoords(const PointerCoords& coords, float x, float y,
3842 float pressure) {
3843 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(coords, x, y, pressure, 0.0f, 0.0f, 0.0f, 0.0f,
3844 0.0f, 0.0f, 0.0f, EPSILON));
3845 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08003846};
3847
3848const int32_t CursorInputMapperTest::TRACKBALL_MOVEMENT_THRESHOLD = 6;
3849
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003850void CursorInputMapperTest::testMotionRotation(CursorInputMapper& mapper, int32_t originalX,
3851 int32_t originalY, int32_t rotatedX,
3852 int32_t rotatedY) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003853 NotifyMotionArgs args;
3854
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003855 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, originalX);
3856 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, originalY);
3857 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003858 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3859 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003860 ASSERT_NO_FATAL_FAILURE(
3861 assertCursorPointerCoords(args.pointerCoords[0],
3862 float(rotatedX) / TRACKBALL_MOVEMENT_THRESHOLD,
3863 float(rotatedY) / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003864}
3865
3866TEST_F(CursorInputMapperTest, WhenModeIsPointer_GetSources_ReturnsMouse) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003867 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003868 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003869
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003870 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003871}
3872
3873TEST_F(CursorInputMapperTest, WhenModeIsNavigation_GetSources_ReturnsTrackball) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003874 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003875 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003876
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003877 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003878}
3879
3880TEST_F(CursorInputMapperTest, WhenModeIsPointer_PopulateDeviceInfo_ReturnsRangeFromPointerController) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003881 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003882 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003883
3884 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003885 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003886
3887 // Initially there may not be a valid motion range.
Yi Kong9b14ac62018-07-17 13:48:38 -07003888 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE));
3889 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003890 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3891 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE, 0.0f, 1.0f, 0.0f, 0.0f));
3892
3893 // When the bounds are set, then there should be a valid motion range.
3894 mFakePointerController->setBounds(1, 2, 800 - 1, 480 - 1);
3895
3896 InputDeviceInfo info2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003897 mapper.populateDeviceInfo(&info2);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003898
3899 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3900 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE,
3901 1, 800 - 1, 0.0f, 0.0f));
3902 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3903 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE,
3904 2, 480 - 1, 0.0f, 0.0f));
3905 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3906 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE,
3907 0.0f, 1.0f, 0.0f, 0.0f));
3908}
3909
3910TEST_F(CursorInputMapperTest, WhenModeIsNavigation_PopulateDeviceInfo_ReturnsScaledRange) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003911 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003912 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003913
3914 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003915 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003916
3917 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3918 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_TRACKBALL,
3919 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
3920 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3921 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_TRACKBALL,
3922 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
3923 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3924 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TRACKBALL,
3925 0.0f, 1.0f, 0.0f, 0.0f));
3926}
3927
3928TEST_F(CursorInputMapperTest, Process_ShouldSetAllFieldsAndIncludeGlobalMetaState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003929 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003930 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003931
arthurhungdcef2dc2020-08-11 14:47:50 +08003932 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003933
3934 NotifyMotionArgs args;
3935
3936 // Button press.
3937 // Mostly testing non x/y behavior here so we don't need to check again elsewhere.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003938 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
3939 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003940 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3941 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3942 ASSERT_EQ(DEVICE_ID, args.deviceId);
3943 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3944 ASSERT_EQ(uint32_t(0), args.policyFlags);
3945 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3946 ASSERT_EQ(0, args.flags);
3947 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3948 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
3949 ASSERT_EQ(0, args.edgeFlags);
3950 ASSERT_EQ(uint32_t(1), args.pointerCount);
3951 ASSERT_EQ(0, args.pointerProperties[0].id);
3952 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003953 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003954 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3955 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3956 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3957
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003958 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3959 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3960 ASSERT_EQ(DEVICE_ID, args.deviceId);
3961 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3962 ASSERT_EQ(uint32_t(0), args.policyFlags);
3963 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
3964 ASSERT_EQ(0, args.flags);
3965 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3966 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
3967 ASSERT_EQ(0, args.edgeFlags);
3968 ASSERT_EQ(uint32_t(1), args.pointerCount);
3969 ASSERT_EQ(0, args.pointerProperties[0].id);
3970 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003971 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003972 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3973 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3974 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3975
Michael Wrightd02c5b62014-02-10 15:10:22 -08003976 // Button release. Should have same down time.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003977 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, BTN_MOUSE, 0);
3978 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003979 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3980 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3981 ASSERT_EQ(DEVICE_ID, args.deviceId);
3982 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3983 ASSERT_EQ(uint32_t(0), args.policyFlags);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003984 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
3985 ASSERT_EQ(0, args.flags);
3986 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3987 ASSERT_EQ(0, args.buttonState);
3988 ASSERT_EQ(0, args.edgeFlags);
3989 ASSERT_EQ(uint32_t(1), args.pointerCount);
3990 ASSERT_EQ(0, args.pointerProperties[0].id);
3991 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003992 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003993 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3994 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3995 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3996
3997 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3998 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3999 ASSERT_EQ(DEVICE_ID, args.deviceId);
4000 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4001 ASSERT_EQ(uint32_t(0), args.policyFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004002 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
4003 ASSERT_EQ(0, args.flags);
4004 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4005 ASSERT_EQ(0, args.buttonState);
4006 ASSERT_EQ(0, args.edgeFlags);
4007 ASSERT_EQ(uint32_t(1), args.pointerCount);
4008 ASSERT_EQ(0, args.pointerProperties[0].id);
4009 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004010 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004011 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4012 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4013 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4014}
4015
4016TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentXYUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004017 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004018 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004019
4020 NotifyMotionArgs args;
4021
4022 // Motion in X but not Y.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004023 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4024 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004025 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4026 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004027 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4028 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f,
4029 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004030
4031 // Motion in Y but not X.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004032 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
4033 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004034 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4035 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004036 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f,
4037 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004038}
4039
4040TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004041 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004042 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004043
4044 NotifyMotionArgs args;
4045
4046 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004047 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4048 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004049 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4050 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004051 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004052
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004053 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4054 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004055 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004056
Michael Wrightd02c5b62014-02-10 15:10:22 -08004057 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004058 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4059 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004060 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004061 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004062 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004063
4064 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004065 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004066 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004067}
4068
4069TEST_F(CursorInputMapperTest, Process_ShouldHandleCombinedXYAndButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004070 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004071 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004072
4073 NotifyMotionArgs args;
4074
4075 // Combined X, Y and Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004076 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4077 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
4078 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4079 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004080 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4081 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004082 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4083 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4084 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004085
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004086 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4087 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004088 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4089 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4090 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004091
Michael Wrightd02c5b62014-02-10 15:10:22 -08004092 // Move X, Y a bit while pressed.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004093 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 2);
4094 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 1);
4095 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004096 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4097 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004098 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4099 2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4100 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004101
4102 // Release Button.
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
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004114TEST_F(CursorInputMapperTest, Process_WhenOrientationAware_ShouldNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004115 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004116 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
4117 // need to be rotated.
4118 addConfigurationProperty("cursor.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004119 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004120
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004121 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004122 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4123 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4124 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4125 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4126 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4127 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4128 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4129 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4130}
4131
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004132TEST_F(CursorInputMapperTest, Process_WhenNotOrientationAware_ShouldRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004133 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004134 // Since InputReader works in the un-rotated coordinate space, only devices that are not
4135 // orientation-aware are affected by display rotation.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004136 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004137
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004138 prepareDisplay(DISPLAY_ORIENTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004139 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4140 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4141 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4142 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4143 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4144 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4145 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4146 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4147
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004148 prepareDisplay(DISPLAY_ORIENTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004149 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, -1, 0));
4150 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, 1));
4151 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, 1));
4152 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, 1));
4153 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 1, 0));
4154 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, -1));
4155 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, -1));
4156 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, -1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004157
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004158 prepareDisplay(DISPLAY_ORIENTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004159 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, -1));
4160 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, -1));
4161 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, -1, 0));
4162 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, 1));
4163 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, 1));
4164 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, 1));
4165 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 1, 0));
4166 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, -1));
4167
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004168 prepareDisplay(DISPLAY_ORIENTATION_270);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004169 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 1, 0));
4170 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, -1));
4171 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, -1));
4172 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, -1));
4173 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, -1, 0));
4174 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, 1));
4175 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, 1));
4176 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, 1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004177}
4178
4179TEST_F(CursorInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004180 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004181 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004182
4183 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4184 mFakePointerController->setPosition(100, 200);
4185 mFakePointerController->setButtonState(0);
4186
4187 NotifyMotionArgs motionArgs;
4188 NotifyKeyArgs keyArgs;
4189
4190 // press BTN_LEFT, release BTN_LEFT
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004191 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 1);
4192 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004193 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4194 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4195 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4196 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004197 ASSERT_NO_FATAL_FAILURE(
4198 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004199
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004200 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4201 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4202 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4203 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004204 ASSERT_NO_FATAL_FAILURE(
4205 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004206
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004207 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 0);
4208 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004209 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004210 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004211 ASSERT_EQ(0, motionArgs.buttonState);
4212 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004213 ASSERT_NO_FATAL_FAILURE(
4214 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004215
4216 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004217 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004218 ASSERT_EQ(0, motionArgs.buttonState);
4219 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004220 ASSERT_NO_FATAL_FAILURE(
4221 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004222
4223 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004224 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004225 ASSERT_EQ(0, motionArgs.buttonState);
4226 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004227 ASSERT_NO_FATAL_FAILURE(
4228 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004229
4230 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004231 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 1);
4232 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 1);
4233 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004234 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4235 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4236 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4237 motionArgs.buttonState);
4238 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4239 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004240 ASSERT_NO_FATAL_FAILURE(
4241 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004242
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004243 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4244 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4245 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4246 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4247 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004248 ASSERT_NO_FATAL_FAILURE(
4249 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004250
4251 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4252 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4253 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4254 motionArgs.buttonState);
4255 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4256 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004257 ASSERT_NO_FATAL_FAILURE(
4258 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004259
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004260 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 0);
4261 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004262 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004263 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004264 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4265 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004266 ASSERT_NO_FATAL_FAILURE(
4267 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004268
4269 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004270 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004271 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4272 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004273 ASSERT_NO_FATAL_FAILURE(
4274 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004275
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004276 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4277 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004278 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004279 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
4280 ASSERT_EQ(0, motionArgs.buttonState);
4281 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004282 ASSERT_NO_FATAL_FAILURE(
4283 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004284 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4285 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004286
4287 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004288 ASSERT_EQ(0, motionArgs.buttonState);
4289 ASSERT_EQ(0, mFakePointerController->getButtonState());
4290 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004291 ASSERT_NO_FATAL_FAILURE(
4292 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004293
Michael Wrightd02c5b62014-02-10 15:10:22 -08004294 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4295 ASSERT_EQ(0, motionArgs.buttonState);
4296 ASSERT_EQ(0, mFakePointerController->getButtonState());
4297 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004298 ASSERT_NO_FATAL_FAILURE(
4299 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004300
4301 // press BTN_BACK, release BTN_BACK
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004302 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 1);
4303 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004304 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4305 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4306 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004307
Michael Wrightd02c5b62014-02-10 15:10:22 -08004308 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004309 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004310 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4311 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004312 ASSERT_NO_FATAL_FAILURE(
4313 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004314
4315 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4316 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4317 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4318 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004319 ASSERT_NO_FATAL_FAILURE(
4320 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004321
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004322 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 0);
4323 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004324 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004325 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004326 ASSERT_EQ(0, motionArgs.buttonState);
4327 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004328 ASSERT_NO_FATAL_FAILURE(
4329 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004330
4331 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004332 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004333 ASSERT_EQ(0, motionArgs.buttonState);
4334 ASSERT_EQ(0, mFakePointerController->getButtonState());
4335
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004336 ASSERT_NO_FATAL_FAILURE(
4337 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004338 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4339 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4340 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4341
4342 // press BTN_SIDE, release BTN_SIDE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004343 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 1);
4344 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004345 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4346 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4347 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004348
Michael Wrightd02c5b62014-02-10 15:10:22 -08004349 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004350 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004351 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4352 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004353 ASSERT_NO_FATAL_FAILURE(
4354 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004355
4356 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4357 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4358 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4359 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004360 ASSERT_NO_FATAL_FAILURE(
4361 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004362
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004363 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 0);
4364 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004365 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004366 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004367 ASSERT_EQ(0, motionArgs.buttonState);
4368 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004369 ASSERT_NO_FATAL_FAILURE(
4370 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004371
4372 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4373 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4374 ASSERT_EQ(0, motionArgs.buttonState);
4375 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004376 ASSERT_NO_FATAL_FAILURE(
4377 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004378
Michael Wrightd02c5b62014-02-10 15:10:22 -08004379 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4380 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4381 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4382
4383 // press BTN_FORWARD, release BTN_FORWARD
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004384 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 1);
4385 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004386 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4387 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4388 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004389
Michael Wrightd02c5b62014-02-10 15:10:22 -08004390 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004391 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004392 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4393 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004394 ASSERT_NO_FATAL_FAILURE(
4395 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004396
4397 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4398 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4399 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4400 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004401 ASSERT_NO_FATAL_FAILURE(
4402 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004403
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004404 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 0);
4405 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004406 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004407 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004408 ASSERT_EQ(0, motionArgs.buttonState);
4409 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004410 ASSERT_NO_FATAL_FAILURE(
4411 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004412
4413 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4414 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4415 ASSERT_EQ(0, motionArgs.buttonState);
4416 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004417 ASSERT_NO_FATAL_FAILURE(
4418 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004419
Michael Wrightd02c5b62014-02-10 15:10:22 -08004420 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4421 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4422 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4423
4424 // press BTN_EXTRA, release BTN_EXTRA
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004425 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 1);
4426 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004427 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4428 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4429 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004430
Michael Wrightd02c5b62014-02-10 15:10:22 -08004431 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004432 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004433 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4434 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004435 ASSERT_NO_FATAL_FAILURE(
4436 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004437
4438 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4439 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4440 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4441 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004442 ASSERT_NO_FATAL_FAILURE(
4443 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004444
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004445 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 0);
4446 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004447 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004448 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004449 ASSERT_EQ(0, motionArgs.buttonState);
4450 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004451 ASSERT_NO_FATAL_FAILURE(
4452 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004453
4454 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4455 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4456 ASSERT_EQ(0, motionArgs.buttonState);
4457 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004458 ASSERT_NO_FATAL_FAILURE(
4459 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004460
Michael Wrightd02c5b62014-02-10 15:10:22 -08004461 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4462 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4463 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4464}
4465
4466TEST_F(CursorInputMapperTest, Process_WhenModeIsPointer_ShouldMoveThePointerAround) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004467 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004468 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004469
4470 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4471 mFakePointerController->setPosition(100, 200);
4472 mFakePointerController->setButtonState(0);
4473
4474 NotifyMotionArgs args;
4475
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004476 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4477 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4478 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004479 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004480 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4481 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4482 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4483 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 +01004484 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004485}
4486
4487TEST_F(CursorInputMapperTest, Process_PointerCapture) {
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004488 addConfigurationProperty("cursor.mode", "pointer");
4489 mFakePolicy->setPointerCapture(true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004490 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004491
4492 NotifyDeviceResetArgs resetArgs;
4493 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4494 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4495 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4496
4497 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4498 mFakePointerController->setPosition(100, 200);
4499 mFakePointerController->setButtonState(0);
4500
4501 NotifyMotionArgs args;
4502
4503 // Move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004504 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4505 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4506 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004507 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4508 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4509 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4510 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4511 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 +01004512 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004513
4514 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004515 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4516 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004517 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4518 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4519 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
4520 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4521 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4522 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4523 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4524 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
4525 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4526 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4527
4528 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004529 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4530 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004531 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4532 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4533 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
4534 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4535 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4536 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4537 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4538 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
4539 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4540 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4541
4542 // Another move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004543 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 30);
4544 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 40);
4545 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004546 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4547 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4548 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4549 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4550 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 +01004551 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004552
4553 // Disable pointer capture and check that the device generation got bumped
4554 // and events are generated the usual way.
arthurhungdcef2dc2020-08-11 14:47:50 +08004555 const uint32_t generation = mReader->getContext()->getGeneration();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004556 mFakePolicy->setPointerCapture(false);
4557 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
arthurhungdcef2dc2020-08-11 14:47:50 +08004558 ASSERT_TRUE(mReader->getContext()->getGeneration() != generation);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004559
4560 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4561 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4562 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4563
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004564 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4565 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4566 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004567 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4568 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004569 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4570 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4571 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 +01004572 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004573}
4574
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004575TEST_F(CursorInputMapperTest, Process_ShouldHandleDisplayId) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004576 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004577
Garfield Tan888a6a42020-01-09 11:39:16 -08004578 // Setup for second display.
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004579 constexpr int32_t SECOND_DISPLAY_ID = 1;
Garfield Tan888a6a42020-01-09 11:39:16 -08004580 const std::string SECOND_DISPLAY_UNIQUE_ID = "local:1";
4581 mFakePolicy->addDisplayViewport(SECOND_DISPLAY_ID, 800, 480, DISPLAY_ORIENTATION_0,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00004582 true /*isActive*/, SECOND_DISPLAY_UNIQUE_ID, NO_PORT,
4583 ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08004584 mFakePolicy->setDefaultPointerDisplayId(SECOND_DISPLAY_ID);
4585 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
4586
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004587 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4588 mFakePointerController->setPosition(100, 200);
4589 mFakePointerController->setButtonState(0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004590
4591 NotifyMotionArgs args;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004592 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4593 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4594 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004595 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4596 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4597 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4598 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4599 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 +01004600 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004601 ASSERT_EQ(SECOND_DISPLAY_ID, args.displayId);
4602}
4603
Michael Wrightd02c5b62014-02-10 15:10:22 -08004604// --- TouchInputMapperTest ---
4605
4606class TouchInputMapperTest : public InputMapperTest {
4607protected:
4608 static const int32_t RAW_X_MIN;
4609 static const int32_t RAW_X_MAX;
4610 static const int32_t RAW_Y_MIN;
4611 static const int32_t RAW_Y_MAX;
4612 static const int32_t RAW_TOUCH_MIN;
4613 static const int32_t RAW_TOUCH_MAX;
4614 static const int32_t RAW_TOOL_MIN;
4615 static const int32_t RAW_TOOL_MAX;
4616 static const int32_t RAW_PRESSURE_MIN;
4617 static const int32_t RAW_PRESSURE_MAX;
4618 static const int32_t RAW_ORIENTATION_MIN;
4619 static const int32_t RAW_ORIENTATION_MAX;
4620 static const int32_t RAW_DISTANCE_MIN;
4621 static const int32_t RAW_DISTANCE_MAX;
4622 static const int32_t RAW_TILT_MIN;
4623 static const int32_t RAW_TILT_MAX;
4624 static const int32_t RAW_ID_MIN;
4625 static const int32_t RAW_ID_MAX;
4626 static const int32_t RAW_SLOT_MIN;
4627 static const int32_t RAW_SLOT_MAX;
4628 static const float X_PRECISION;
4629 static const float Y_PRECISION;
Santos Cordonfa5cf462017-04-05 10:37:00 -07004630 static const float X_PRECISION_VIRTUAL;
4631 static const float Y_PRECISION_VIRTUAL;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004632
4633 static const float GEOMETRIC_SCALE;
Jason Gerecke489fda82012-09-07 17:19:40 -07004634 static const TouchAffineTransformation AFFINE_TRANSFORM;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004635
4636 static const VirtualKeyDefinition VIRTUAL_KEYS[2];
4637
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004638 const std::string UNIQUE_ID = "local:0";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004639 const std::string SECONDARY_UNIQUE_ID = "local:1";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004640
Michael Wrightd02c5b62014-02-10 15:10:22 -08004641 enum Axes {
4642 POSITION = 1 << 0,
4643 TOUCH = 1 << 1,
4644 TOOL = 1 << 2,
4645 PRESSURE = 1 << 3,
4646 ORIENTATION = 1 << 4,
4647 MINOR = 1 << 5,
4648 ID = 1 << 6,
4649 DISTANCE = 1 << 7,
4650 TILT = 1 << 8,
4651 SLOT = 1 << 9,
4652 TOOL_TYPE = 1 << 10,
4653 };
4654
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004655 void prepareDisplay(int32_t orientation, std::optional<uint8_t> port = NO_PORT);
4656 void prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port = NO_PORT);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004657 void prepareVirtualDisplay(int32_t orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004658 void prepareVirtualKeys();
Jason Gerecke489fda82012-09-07 17:19:40 -07004659 void prepareLocationCalibration();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004660 int32_t toRawX(float displayX);
4661 int32_t toRawY(float displayY);
Jason Gerecke489fda82012-09-07 17:19:40 -07004662 float toCookedX(float rawX, float rawY);
4663 float toCookedY(float rawX, float rawY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004664 float toDisplayX(int32_t rawX);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004665 float toDisplayX(int32_t rawX, int32_t displayWidth);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004666 float toDisplayY(int32_t rawY);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004667 float toDisplayY(int32_t rawY, int32_t displayHeight);
4668
Michael Wrightd02c5b62014-02-10 15:10:22 -08004669};
4670
4671const int32_t TouchInputMapperTest::RAW_X_MIN = 25;
4672const int32_t TouchInputMapperTest::RAW_X_MAX = 1019;
4673const int32_t TouchInputMapperTest::RAW_Y_MIN = 30;
4674const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009;
4675const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0;
4676const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31;
4677const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0;
4678const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15;
Michael Wrightaa449c92017-12-13 21:21:43 +00004679const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = 0;
4680const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = 255;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004681const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7;
4682const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
4683const int32_t TouchInputMapperTest::RAW_DISTANCE_MIN = 0;
4684const int32_t TouchInputMapperTest::RAW_DISTANCE_MAX = 7;
4685const int32_t TouchInputMapperTest::RAW_TILT_MIN = 0;
4686const int32_t TouchInputMapperTest::RAW_TILT_MAX = 150;
4687const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
4688const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
4689const int32_t TouchInputMapperTest::RAW_SLOT_MIN = 0;
4690const int32_t TouchInputMapperTest::RAW_SLOT_MAX = 9;
4691const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH;
4692const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT;
Santos Cordonfa5cf462017-04-05 10:37:00 -07004693const float TouchInputMapperTest::X_PRECISION_VIRTUAL =
4694 float(RAW_X_MAX - RAW_X_MIN + 1) / VIRTUAL_DISPLAY_WIDTH;
4695const float TouchInputMapperTest::Y_PRECISION_VIRTUAL =
4696 float(RAW_Y_MAX - RAW_Y_MIN + 1) / VIRTUAL_DISPLAY_HEIGHT;
Jason Gerecke489fda82012-09-07 17:19:40 -07004697const TouchAffineTransformation TouchInputMapperTest::AFFINE_TRANSFORM =
4698 TouchAffineTransformation(1, -2, 3, -4, 5, -6);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004699
4700const float TouchInputMapperTest::GEOMETRIC_SCALE =
4701 avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1),
4702 float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1));
4703
4704const VirtualKeyDefinition TouchInputMapperTest::VIRTUAL_KEYS[2] = {
4705 { KEY_HOME, 60, DISPLAY_HEIGHT + 15, 20, 20 },
4706 { KEY_MENU, DISPLAY_HEIGHT - 60, DISPLAY_WIDTH + 15, 20, 20 },
4707};
4708
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004709void TouchInputMapperTest::prepareDisplay(int32_t orientation, std::optional<uint8_t> port) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004710 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
4711 port, ViewportType::INTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004712}
4713
4714void TouchInputMapperTest::prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port) {
4715 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
4716 DISPLAY_ORIENTATION_0, SECONDARY_UNIQUE_ID, port, type);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004717}
4718
Santos Cordonfa5cf462017-04-05 10:37:00 -07004719void TouchInputMapperTest::prepareVirtualDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004720 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH, VIRTUAL_DISPLAY_HEIGHT,
4721 orientation, VIRTUAL_DISPLAY_UNIQUE_ID, NO_PORT,
4722 ViewportType::VIRTUAL);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004723}
4724
Michael Wrightd02c5b62014-02-10 15:10:22 -08004725void TouchInputMapperTest::prepareVirtualKeys() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004726 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[0]);
4727 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[1]);
4728 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4729 mFakeEventHub->addKey(EVENTHUB_ID, KEY_MENU, 0, AKEYCODE_MENU, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004730}
4731
Jason Gerecke489fda82012-09-07 17:19:40 -07004732void TouchInputMapperTest::prepareLocationCalibration() {
4733 mFakePolicy->setTouchAffineTransformation(AFFINE_TRANSFORM);
4734}
4735
Michael Wrightd02c5b62014-02-10 15:10:22 -08004736int32_t TouchInputMapperTest::toRawX(float displayX) {
4737 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN);
4738}
4739
4740int32_t TouchInputMapperTest::toRawY(float displayY) {
4741 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN);
4742}
4743
Jason Gerecke489fda82012-09-07 17:19:40 -07004744float TouchInputMapperTest::toCookedX(float rawX, float rawY) {
4745 AFFINE_TRANSFORM.applyTo(rawX, rawY);
4746 return rawX;
4747}
4748
4749float TouchInputMapperTest::toCookedY(float rawX, float rawY) {
4750 AFFINE_TRANSFORM.applyTo(rawX, rawY);
4751 return rawY;
4752}
4753
Michael Wrightd02c5b62014-02-10 15:10:22 -08004754float TouchInputMapperTest::toDisplayX(int32_t rawX) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004755 return toDisplayX(rawX, DISPLAY_WIDTH);
4756}
4757
4758float TouchInputMapperTest::toDisplayX(int32_t rawX, int32_t displayWidth) {
4759 return float(rawX - RAW_X_MIN) * displayWidth / (RAW_X_MAX - RAW_X_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004760}
4761
4762float TouchInputMapperTest::toDisplayY(int32_t rawY) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004763 return toDisplayY(rawY, DISPLAY_HEIGHT);
4764}
4765
4766float TouchInputMapperTest::toDisplayY(int32_t rawY, int32_t displayHeight) {
4767 return float(rawY - RAW_Y_MIN) * displayHeight / (RAW_Y_MAX - RAW_Y_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004768}
4769
4770
4771// --- SingleTouchInputMapperTest ---
4772
4773class SingleTouchInputMapperTest : public TouchInputMapperTest {
4774protected:
4775 void prepareButtons();
4776 void prepareAxes(int axes);
4777
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004778 void processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
4779 void processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
4780 void processUp(SingleTouchInputMapper& mappery);
4781 void processPressure(SingleTouchInputMapper& mapper, int32_t pressure);
4782 void processToolMajor(SingleTouchInputMapper& mapper, int32_t toolMajor);
4783 void processDistance(SingleTouchInputMapper& mapper, int32_t distance);
4784 void processTilt(SingleTouchInputMapper& mapper, int32_t tiltX, int32_t tiltY);
4785 void processKey(SingleTouchInputMapper& mapper, int32_t code, int32_t value);
4786 void processSync(SingleTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004787};
4788
4789void SingleTouchInputMapperTest::prepareButtons() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004790 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004791}
4792
4793void SingleTouchInputMapperTest::prepareAxes(int axes) {
4794 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004795 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
4796 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004797 }
4798 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004799 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MIN,
4800 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004801 }
4802 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004803 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TOOL_WIDTH, RAW_TOOL_MIN, RAW_TOOL_MAX, 0,
4804 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004805 }
4806 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004807 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_DISTANCE, RAW_DISTANCE_MIN,
4808 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004809 }
4810 if (axes & TILT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004811 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_X, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
4812 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_Y, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004813 }
4814}
4815
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004816void SingleTouchInputMapperTest::processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004817 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
4818 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
4819 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004820}
4821
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004822void SingleTouchInputMapperTest::processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004823 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
4824 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004825}
4826
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004827void SingleTouchInputMapperTest::processUp(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004828 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004829}
4830
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004831void SingleTouchInputMapperTest::processPressure(SingleTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004832 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004833}
4834
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004835void SingleTouchInputMapperTest::processToolMajor(SingleTouchInputMapper& mapper,
4836 int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004837 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TOOL_WIDTH, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004838}
4839
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004840void SingleTouchInputMapperTest::processDistance(SingleTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004841 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004842}
4843
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004844void SingleTouchInputMapperTest::processTilt(SingleTouchInputMapper& mapper, int32_t tiltX,
4845 int32_t tiltY) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004846 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_X, tiltX);
4847 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_Y, tiltY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004848}
4849
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004850void SingleTouchInputMapperTest::processKey(SingleTouchInputMapper& mapper, int32_t code,
4851 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004852 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004853}
4854
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004855void SingleTouchInputMapperTest::processSync(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004856 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004857}
4858
Michael Wrightd02c5b62014-02-10 15:10:22 -08004859TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004860 prepareButtons();
4861 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004862 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004863
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004864 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004865}
4866
4867TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndIsACursor_ReturnsTouchPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004868 mFakeEventHub->addRelativeAxis(EVENTHUB_ID, REL_X);
4869 mFakeEventHub->addRelativeAxis(EVENTHUB_ID, REL_Y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004870 prepareButtons();
4871 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004872 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004873
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004874 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004875}
4876
4877TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchPad_ReturnsTouchPad) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004878 prepareButtons();
4879 prepareAxes(POSITION);
4880 addConfigurationProperty("touch.deviceType", "touchPad");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004881 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004882
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004883 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004884}
4885
4886TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004887 prepareButtons();
4888 prepareAxes(POSITION);
4889 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004890 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004891
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004892 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004893}
4894
4895TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004896 addConfigurationProperty("touch.deviceType", "touchScreen");
4897 prepareDisplay(DISPLAY_ORIENTATION_0);
4898 prepareButtons();
4899 prepareAxes(POSITION);
4900 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004901 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004902
4903 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004904 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004905
4906 // Virtual key is down.
4907 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4908 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4909 processDown(mapper, x, y);
4910 processSync(mapper);
4911 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4912
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004913 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004914
4915 // Virtual key is up.
4916 processUp(mapper);
4917 processSync(mapper);
4918 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4919
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004920 ASSERT_EQ(AKEY_STATE_UP, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004921}
4922
4923TEST_F(SingleTouchInputMapperTest, GetScanCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004924 addConfigurationProperty("touch.deviceType", "touchScreen");
4925 prepareDisplay(DISPLAY_ORIENTATION_0);
4926 prepareButtons();
4927 prepareAxes(POSITION);
4928 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004929 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004930
4931 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004932 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004933
4934 // Virtual key is down.
4935 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4936 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4937 processDown(mapper, x, y);
4938 processSync(mapper);
4939 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4940
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004941 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004942
4943 // Virtual key is up.
4944 processUp(mapper);
4945 processSync(mapper);
4946 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4947
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004948 ASSERT_EQ(AKEY_STATE_UP, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004949}
4950
4951TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004952 addConfigurationProperty("touch.deviceType", "touchScreen");
4953 prepareDisplay(DISPLAY_ORIENTATION_0);
4954 prepareButtons();
4955 prepareAxes(POSITION);
4956 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004957 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004958
4959 const int32_t keys[2] = { AKEYCODE_HOME, AKEYCODE_A };
4960 uint8_t flags[2] = { 0, 0 };
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004961 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, 2, keys, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004962 ASSERT_TRUE(flags[0]);
4963 ASSERT_FALSE(flags[1]);
4964}
4965
4966TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004967 addConfigurationProperty("touch.deviceType", "touchScreen");
4968 prepareDisplay(DISPLAY_ORIENTATION_0);
4969 prepareButtons();
4970 prepareAxes(POSITION);
4971 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004972 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004973
arthurhungdcef2dc2020-08-11 14:47:50 +08004974 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004975
4976 NotifyKeyArgs args;
4977
4978 // Press virtual key.
4979 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4980 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4981 processDown(mapper, x, y);
4982 processSync(mapper);
4983
4984 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4985 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4986 ASSERT_EQ(DEVICE_ID, args.deviceId);
4987 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
4988 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
4989 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
4990 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
4991 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4992 ASSERT_EQ(KEY_HOME, args.scanCode);
4993 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4994 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4995
4996 // Release virtual key.
4997 processUp(mapper);
4998 processSync(mapper);
4999
5000 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
5001 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5002 ASSERT_EQ(DEVICE_ID, args.deviceId);
5003 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
5004 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
5005 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
5006 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5007 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5008 ASSERT_EQ(KEY_HOME, args.scanCode);
5009 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5010 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5011
5012 // Should not have sent any motions.
5013 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5014}
5015
5016TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005017 addConfigurationProperty("touch.deviceType", "touchScreen");
5018 prepareDisplay(DISPLAY_ORIENTATION_0);
5019 prepareButtons();
5020 prepareAxes(POSITION);
5021 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005022 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005023
arthurhungdcef2dc2020-08-11 14:47:50 +08005024 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005025
5026 NotifyKeyArgs keyArgs;
5027
5028 // Press virtual key.
5029 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5030 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5031 processDown(mapper, x, y);
5032 processSync(mapper);
5033
5034 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5035 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5036 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5037 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5038 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5039 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5040 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags);
5041 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5042 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5043 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5044 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5045
5046 // Move out of bounds. This should generate a cancel and a pointer down since we moved
5047 // into the display area.
5048 y -= 100;
5049 processMove(mapper, x, y);
5050 processSync(mapper);
5051
5052 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5053 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5054 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5055 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5056 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5057 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5058 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
5059 | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags);
5060 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5061 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5062 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5063 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5064
5065 NotifyMotionArgs motionArgs;
5066 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5067 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5068 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5069 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5070 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5071 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5072 ASSERT_EQ(0, motionArgs.flags);
5073 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5074 ASSERT_EQ(0, motionArgs.buttonState);
5075 ASSERT_EQ(0, motionArgs.edgeFlags);
5076 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5077 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5078 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5079 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5080 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5081 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5082 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5083 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5084
5085 // Keep moving out of bounds. Should generate a pointer move.
5086 y -= 50;
5087 processMove(mapper, x, y);
5088 processSync(mapper);
5089
5090 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5091 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5092 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5093 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5094 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5095 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5096 ASSERT_EQ(0, motionArgs.flags);
5097 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5098 ASSERT_EQ(0, motionArgs.buttonState);
5099 ASSERT_EQ(0, motionArgs.edgeFlags);
5100 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5101 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5102 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5103 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5104 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5105 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5106 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5107 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5108
5109 // Release out of bounds. Should generate a pointer up.
5110 processUp(mapper);
5111 processSync(mapper);
5112
5113 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5114 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5115 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5116 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5117 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5118 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5119 ASSERT_EQ(0, motionArgs.flags);
5120 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5121 ASSERT_EQ(0, motionArgs.buttonState);
5122 ASSERT_EQ(0, motionArgs.edgeFlags);
5123 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5124 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5125 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5126 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5127 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5128 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5129 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5130 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5131
5132 // Should not have sent any more keys or motions.
5133 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5134 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5135}
5136
5137TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005138 addConfigurationProperty("touch.deviceType", "touchScreen");
5139 prepareDisplay(DISPLAY_ORIENTATION_0);
5140 prepareButtons();
5141 prepareAxes(POSITION);
5142 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005143 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005144
arthurhungdcef2dc2020-08-11 14:47:50 +08005145 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005146
5147 NotifyMotionArgs motionArgs;
5148
5149 // Initially go down out of bounds.
5150 int32_t x = -10;
5151 int32_t y = -10;
5152 processDown(mapper, x, y);
5153 processSync(mapper);
5154
5155 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5156
5157 // Move into the display area. Should generate a pointer down.
5158 x = 50;
5159 y = 75;
5160 processMove(mapper, x, y);
5161 processSync(mapper);
5162
5163 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5164 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5165 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5166 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5167 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5168 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5169 ASSERT_EQ(0, motionArgs.flags);
5170 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5171 ASSERT_EQ(0, motionArgs.buttonState);
5172 ASSERT_EQ(0, motionArgs.edgeFlags);
5173 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5174 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5175 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5176 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5177 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5178 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5179 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5180 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5181
5182 // Release. Should generate a pointer up.
5183 processUp(mapper);
5184 processSync(mapper);
5185
5186 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5187 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5188 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5189 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5190 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5191 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5192 ASSERT_EQ(0, motionArgs.flags);
5193 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5194 ASSERT_EQ(0, motionArgs.buttonState);
5195 ASSERT_EQ(0, motionArgs.edgeFlags);
5196 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5197 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5198 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5199 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5200 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5201 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5202 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5203 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5204
5205 // Should not have sent any more keys or motions.
5206 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5207 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5208}
5209
Santos Cordonfa5cf462017-04-05 10:37:00 -07005210TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture_VirtualDisplay) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005211 addConfigurationProperty("touch.deviceType", "touchScreen");
5212 addConfigurationProperty("touch.displayId", VIRTUAL_DISPLAY_UNIQUE_ID);
5213
5214 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
5215 prepareButtons();
5216 prepareAxes(POSITION);
5217 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005218 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Santos Cordonfa5cf462017-04-05 10:37:00 -07005219
arthurhungdcef2dc2020-08-11 14:47:50 +08005220 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005221
5222 NotifyMotionArgs motionArgs;
5223
5224 // Down.
5225 int32_t x = 100;
5226 int32_t y = 125;
5227 processDown(mapper, x, y);
5228 processSync(mapper);
5229
5230 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5231 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5232 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5233 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5234 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5235 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5236 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, 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, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5246 1, 0, 0, 0, 0, 0, 0, 0));
5247 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5248 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5249 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5250
5251 // Move.
5252 x += 50;
5253 y += 75;
5254 processMove(mapper, x, y);
5255 processSync(mapper);
5256
5257 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5258 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5259 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5260 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5261 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5262 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5263 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5264 ASSERT_EQ(0, motionArgs.flags);
5265 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5266 ASSERT_EQ(0, motionArgs.buttonState);
5267 ASSERT_EQ(0, motionArgs.edgeFlags);
5268 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5269 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5270 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5271 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5272 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5273 1, 0, 0, 0, 0, 0, 0, 0));
5274 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5275 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5276 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5277
5278 // Up.
5279 processUp(mapper);
5280 processSync(mapper);
5281
5282 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5283 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5284 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5285 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5286 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5287 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5288 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5289 ASSERT_EQ(0, motionArgs.flags);
5290 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5291 ASSERT_EQ(0, motionArgs.buttonState);
5292 ASSERT_EQ(0, motionArgs.edgeFlags);
5293 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5294 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5295 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5296 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5297 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5298 1, 0, 0, 0, 0, 0, 0, 0));
5299 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5300 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5301 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5302
5303 // Should not have sent any more keys or motions.
5304 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5305 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5306}
5307
Michael Wrightd02c5b62014-02-10 15:10:22 -08005308TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005309 addConfigurationProperty("touch.deviceType", "touchScreen");
5310 prepareDisplay(DISPLAY_ORIENTATION_0);
5311 prepareButtons();
5312 prepareAxes(POSITION);
5313 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005314 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005315
arthurhungdcef2dc2020-08-11 14:47:50 +08005316 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005317
5318 NotifyMotionArgs motionArgs;
5319
5320 // Down.
5321 int32_t x = 100;
5322 int32_t y = 125;
5323 processDown(mapper, x, y);
5324 processSync(mapper);
5325
5326 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5327 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5328 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5329 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5330 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5331 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5332 ASSERT_EQ(0, motionArgs.flags);
5333 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5334 ASSERT_EQ(0, motionArgs.buttonState);
5335 ASSERT_EQ(0, motionArgs.edgeFlags);
5336 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5337 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5338 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5339 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5340 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5341 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5342 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5343 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5344
5345 // Move.
5346 x += 50;
5347 y += 75;
5348 processMove(mapper, x, y);
5349 processSync(mapper);
5350
5351 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5352 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5353 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5354 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5355 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5356 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5357 ASSERT_EQ(0, motionArgs.flags);
5358 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5359 ASSERT_EQ(0, motionArgs.buttonState);
5360 ASSERT_EQ(0, motionArgs.edgeFlags);
5361 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5362 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5363 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5364 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5365 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5366 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5367 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5368 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5369
5370 // Up.
5371 processUp(mapper);
5372 processSync(mapper);
5373
5374 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5375 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5376 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5377 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5378 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5379 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5380 ASSERT_EQ(0, motionArgs.flags);
5381 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5382 ASSERT_EQ(0, motionArgs.buttonState);
5383 ASSERT_EQ(0, motionArgs.edgeFlags);
5384 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5385 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5386 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5387 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5388 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5389 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5390 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5391 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5392
5393 // Should not have sent any more keys or motions.
5394 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5395 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5396}
5397
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005398TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_DoesNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005399 addConfigurationProperty("touch.deviceType", "touchScreen");
5400 prepareButtons();
5401 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005402 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
5403 // need to be rotated. Touchscreens are orientation-aware by default.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005404 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005405
5406 NotifyMotionArgs args;
5407
5408 // Rotation 90.
5409 prepareDisplay(DISPLAY_ORIENTATION_90);
5410 processDown(mapper, toRawX(50), toRawY(75));
5411 processSync(mapper);
5412
5413 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5414 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5415 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5416
5417 processUp(mapper);
5418 processSync(mapper);
5419 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5420}
5421
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005422TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_RotatesMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005423 addConfigurationProperty("touch.deviceType", "touchScreen");
5424 prepareButtons();
5425 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005426 // Since InputReader works in the un-rotated coordinate space, only devices that are not
5427 // orientation-aware are affected by display rotation.
5428 addConfigurationProperty("touch.orientationAware", "0");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005429 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005430
5431 NotifyMotionArgs args;
5432
5433 // Rotation 0.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005434 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005435 prepareDisplay(DISPLAY_ORIENTATION_0);
5436 processDown(mapper, toRawX(50), toRawY(75));
5437 processSync(mapper);
5438
5439 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5440 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5441 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5442
5443 processUp(mapper);
5444 processSync(mapper);
5445 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5446
5447 // Rotation 90.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005448 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005449 prepareDisplay(DISPLAY_ORIENTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005450 processDown(mapper, toRawX(75), RAW_Y_MAX - toRawY(50) + RAW_Y_MIN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005451 processSync(mapper);
5452
5453 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5454 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5455 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5456
5457 processUp(mapper);
5458 processSync(mapper);
5459 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5460
5461 // Rotation 180.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005462 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005463 prepareDisplay(DISPLAY_ORIENTATION_180);
5464 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
5465 processSync(mapper);
5466
5467 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5468 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5469 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5470
5471 processUp(mapper);
5472 processSync(mapper);
5473 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5474
5475 // Rotation 270.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005476 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005477 prepareDisplay(DISPLAY_ORIENTATION_270);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005478 processDown(mapper, RAW_X_MAX - toRawX(75) + RAW_X_MIN, toRawY(50));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005479 processSync(mapper);
5480
5481 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5482 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5483 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5484
5485 processUp(mapper);
5486 processSync(mapper);
5487 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5488}
5489
5490TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005491 addConfigurationProperty("touch.deviceType", "touchScreen");
5492 prepareDisplay(DISPLAY_ORIENTATION_0);
5493 prepareButtons();
5494 prepareAxes(POSITION | PRESSURE | TOOL | DISTANCE | TILT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005495 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005496
5497 // These calculations are based on the input device calibration documentation.
5498 int32_t rawX = 100;
5499 int32_t rawY = 200;
5500 int32_t rawPressure = 10;
5501 int32_t rawToolMajor = 12;
5502 int32_t rawDistance = 2;
5503 int32_t rawTiltX = 30;
5504 int32_t rawTiltY = 110;
5505
5506 float x = toDisplayX(rawX);
5507 float y = toDisplayY(rawY);
5508 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
5509 float size = float(rawToolMajor) / RAW_TOOL_MAX;
5510 float tool = float(rawToolMajor) * GEOMETRIC_SCALE;
5511 float distance = float(rawDistance);
5512
5513 float tiltCenter = (RAW_TILT_MAX + RAW_TILT_MIN) * 0.5f;
5514 float tiltScale = M_PI / 180;
5515 float tiltXAngle = (rawTiltX - tiltCenter) * tiltScale;
5516 float tiltYAngle = (rawTiltY - tiltCenter) * tiltScale;
5517 float orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
5518 float tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
5519
5520 processDown(mapper, rawX, rawY);
5521 processPressure(mapper, rawPressure);
5522 processToolMajor(mapper, rawToolMajor);
5523 processDistance(mapper, rawDistance);
5524 processTilt(mapper, rawTiltX, rawTiltY);
5525 processSync(mapper);
5526
5527 NotifyMotionArgs args;
5528 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5529 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5530 x, y, pressure, size, tool, tool, tool, tool, orientation, distance));
5531 ASSERT_EQ(tilt, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TILT));
5532}
5533
Jason Gerecke489fda82012-09-07 17:19:40 -07005534TEST_F(SingleTouchInputMapperTest, Process_XYAxes_AffineCalibration) {
Jason Gerecke489fda82012-09-07 17:19:40 -07005535 addConfigurationProperty("touch.deviceType", "touchScreen");
5536 prepareDisplay(DISPLAY_ORIENTATION_0);
5537 prepareLocationCalibration();
5538 prepareButtons();
5539 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005540 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Jason Gerecke489fda82012-09-07 17:19:40 -07005541
5542 int32_t rawX = 100;
5543 int32_t rawY = 200;
5544
5545 float x = toDisplayX(toCookedX(rawX, rawY));
5546 float y = toDisplayY(toCookedY(rawX, rawY));
5547
5548 processDown(mapper, rawX, rawY);
5549 processSync(mapper);
5550
5551 NotifyMotionArgs args;
5552 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5553 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5554 x, y, 1, 0, 0, 0, 0, 0, 0, 0));
5555}
5556
Michael Wrightd02c5b62014-02-10 15:10:22 -08005557TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005558 addConfigurationProperty("touch.deviceType", "touchScreen");
5559 prepareDisplay(DISPLAY_ORIENTATION_0);
5560 prepareButtons();
5561 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005562 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005563
5564 NotifyMotionArgs motionArgs;
5565 NotifyKeyArgs keyArgs;
5566
5567 processDown(mapper, 100, 200);
5568 processSync(mapper);
5569 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5570 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5571 ASSERT_EQ(0, motionArgs.buttonState);
5572
5573 // press BTN_LEFT, release BTN_LEFT
5574 processKey(mapper, BTN_LEFT, 1);
5575 processSync(mapper);
5576 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5577 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5578 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
5579
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005580 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5581 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5582 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
5583
Michael Wrightd02c5b62014-02-10 15:10:22 -08005584 processKey(mapper, BTN_LEFT, 0);
5585 processSync(mapper);
5586 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005587 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005588 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005589
5590 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005591 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005592 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005593
5594 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
5595 processKey(mapper, BTN_RIGHT, 1);
5596 processKey(mapper, BTN_MIDDLE, 1);
5597 processSync(mapper);
5598 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5599 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5600 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5601 motionArgs.buttonState);
5602
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005603 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5604 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5605 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
5606
5607 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5608 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5609 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5610 motionArgs.buttonState);
5611
Michael Wrightd02c5b62014-02-10 15:10:22 -08005612 processKey(mapper, BTN_RIGHT, 0);
5613 processSync(mapper);
5614 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005615 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005616 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005617
5618 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005619 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005620 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005621
5622 processKey(mapper, BTN_MIDDLE, 0);
5623 processSync(mapper);
5624 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005625 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005626 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005627
5628 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005629 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005630 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005631
5632 // press BTN_BACK, release BTN_BACK
5633 processKey(mapper, BTN_BACK, 1);
5634 processSync(mapper);
5635 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5636 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5637 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005638
Michael Wrightd02c5b62014-02-10 15:10:22 -08005639 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005640 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005641 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5642
5643 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5644 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5645 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005646
5647 processKey(mapper, BTN_BACK, 0);
5648 processSync(mapper);
5649 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005650 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005651 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005652
5653 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005654 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005655 ASSERT_EQ(0, motionArgs.buttonState);
5656
Michael Wrightd02c5b62014-02-10 15:10:22 -08005657 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5658 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5659 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
5660
5661 // press BTN_SIDE, release BTN_SIDE
5662 processKey(mapper, BTN_SIDE, 1);
5663 processSync(mapper);
5664 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5665 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5666 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005667
Michael Wrightd02c5b62014-02-10 15:10:22 -08005668 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005669 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005670 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5671
5672 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5673 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5674 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005675
5676 processKey(mapper, BTN_SIDE, 0);
5677 processSync(mapper);
5678 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005679 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005680 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005681
5682 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005683 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005684 ASSERT_EQ(0, motionArgs.buttonState);
5685
Michael Wrightd02c5b62014-02-10 15:10:22 -08005686 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5687 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5688 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
5689
5690 // press BTN_FORWARD, release BTN_FORWARD
5691 processKey(mapper, BTN_FORWARD, 1);
5692 processSync(mapper);
5693 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5694 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5695 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005696
Michael Wrightd02c5b62014-02-10 15:10:22 -08005697 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005698 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005699 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5700
5701 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5702 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5703 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005704
5705 processKey(mapper, BTN_FORWARD, 0);
5706 processSync(mapper);
5707 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005708 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005709 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005710
5711 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005712 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005713 ASSERT_EQ(0, motionArgs.buttonState);
5714
Michael Wrightd02c5b62014-02-10 15:10:22 -08005715 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5716 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5717 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
5718
5719 // press BTN_EXTRA, release BTN_EXTRA
5720 processKey(mapper, BTN_EXTRA, 1);
5721 processSync(mapper);
5722 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5723 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5724 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005725
Michael Wrightd02c5b62014-02-10 15:10:22 -08005726 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005727 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005728 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5729
5730 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5731 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5732 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005733
5734 processKey(mapper, BTN_EXTRA, 0);
5735 processSync(mapper);
5736 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005737 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005738 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005739
5740 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005741 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005742 ASSERT_EQ(0, motionArgs.buttonState);
5743
Michael Wrightd02c5b62014-02-10 15:10:22 -08005744 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5745 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5746 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
5747
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005748 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5749
Michael Wrightd02c5b62014-02-10 15:10:22 -08005750 // press BTN_STYLUS, release BTN_STYLUS
5751 processKey(mapper, BTN_STYLUS, 1);
5752 processSync(mapper);
5753 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5754 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005755 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
5756
5757 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5758 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5759 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005760
5761 processKey(mapper, BTN_STYLUS, 0);
5762 processSync(mapper);
5763 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005764 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005765 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005766
5767 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005768 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005769 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005770
5771 // press BTN_STYLUS2, release BTN_STYLUS2
5772 processKey(mapper, BTN_STYLUS2, 1);
5773 processSync(mapper);
5774 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5775 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005776 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
5777
5778 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5779 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5780 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005781
5782 processKey(mapper, BTN_STYLUS2, 0);
5783 processSync(mapper);
5784 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005785 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005786 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005787
5788 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005789 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005790 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005791
5792 // release touch
5793 processUp(mapper);
5794 processSync(mapper);
5795 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5796 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5797 ASSERT_EQ(0, motionArgs.buttonState);
5798}
5799
5800TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005801 addConfigurationProperty("touch.deviceType", "touchScreen");
5802 prepareDisplay(DISPLAY_ORIENTATION_0);
5803 prepareButtons();
5804 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005805 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005806
5807 NotifyMotionArgs motionArgs;
5808
5809 // default tool type is finger
5810 processDown(mapper, 100, 200);
5811 processSync(mapper);
5812 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5813 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5814 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5815
5816 // eraser
5817 processKey(mapper, BTN_TOOL_RUBBER, 1);
5818 processSync(mapper);
5819 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5820 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5821 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
5822
5823 // stylus
5824 processKey(mapper, BTN_TOOL_RUBBER, 0);
5825 processKey(mapper, BTN_TOOL_PEN, 1);
5826 processSync(mapper);
5827 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5828 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5829 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5830
5831 // brush
5832 processKey(mapper, BTN_TOOL_PEN, 0);
5833 processKey(mapper, BTN_TOOL_BRUSH, 1);
5834 processSync(mapper);
5835 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5836 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5837 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5838
5839 // pencil
5840 processKey(mapper, BTN_TOOL_BRUSH, 0);
5841 processKey(mapper, BTN_TOOL_PENCIL, 1);
5842 processSync(mapper);
5843 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5844 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5845 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5846
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08005847 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08005848 processKey(mapper, BTN_TOOL_PENCIL, 0);
5849 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
5850 processSync(mapper);
5851 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5852 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5853 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5854
5855 // mouse
5856 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
5857 processKey(mapper, BTN_TOOL_MOUSE, 1);
5858 processSync(mapper);
5859 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5860 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5861 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
5862
5863 // lens
5864 processKey(mapper, BTN_TOOL_MOUSE, 0);
5865 processKey(mapper, BTN_TOOL_LENS, 1);
5866 processSync(mapper);
5867 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5868 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5869 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
5870
5871 // double-tap
5872 processKey(mapper, BTN_TOOL_LENS, 0);
5873 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
5874 processSync(mapper);
5875 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5876 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5877 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5878
5879 // triple-tap
5880 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
5881 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
5882 processSync(mapper);
5883 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5884 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5885 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5886
5887 // quad-tap
5888 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
5889 processKey(mapper, BTN_TOOL_QUADTAP, 1);
5890 processSync(mapper);
5891 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5892 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5893 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5894
5895 // finger
5896 processKey(mapper, BTN_TOOL_QUADTAP, 0);
5897 processKey(mapper, BTN_TOOL_FINGER, 1);
5898 processSync(mapper);
5899 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5900 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5901 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5902
5903 // stylus trumps finger
5904 processKey(mapper, BTN_TOOL_PEN, 1);
5905 processSync(mapper);
5906 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5907 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5908 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5909
5910 // eraser trumps stylus
5911 processKey(mapper, BTN_TOOL_RUBBER, 1);
5912 processSync(mapper);
5913 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5914 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5915 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
5916
5917 // mouse trumps eraser
5918 processKey(mapper, BTN_TOOL_MOUSE, 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_MOUSE, motionArgs.pointerProperties[0].toolType);
5923
5924 // back to default tool type
5925 processKey(mapper, BTN_TOOL_MOUSE, 0);
5926 processKey(mapper, BTN_TOOL_RUBBER, 0);
5927 processKey(mapper, BTN_TOOL_PEN, 0);
5928 processKey(mapper, BTN_TOOL_FINGER, 0);
5929 processSync(mapper);
5930 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5931 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5932 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5933}
5934
5935TEST_F(SingleTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005936 addConfigurationProperty("touch.deviceType", "touchScreen");
5937 prepareDisplay(DISPLAY_ORIENTATION_0);
5938 prepareButtons();
5939 prepareAxes(POSITION);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005940 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_FINGER, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005941 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005942
5943 NotifyMotionArgs motionArgs;
5944
5945 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
5946 processKey(mapper, BTN_TOOL_FINGER, 1);
5947 processMove(mapper, 100, 200);
5948 processSync(mapper);
5949 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5950 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5951 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5952 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5953
5954 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5955 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5956 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5957 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5958
5959 // move a little
5960 processMove(mapper, 150, 250);
5961 processSync(mapper);
5962 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5963 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5964 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5965 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5966
5967 // down when BTN_TOUCH is pressed, pressure defaults to 1
5968 processKey(mapper, BTN_TOUCH, 1);
5969 processSync(mapper);
5970 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5971 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5972 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5973 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5974
5975 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5976 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5977 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5978 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5979
5980 // up when BTN_TOUCH is released, hover restored
5981 processKey(mapper, BTN_TOUCH, 0);
5982 processSync(mapper);
5983 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5984 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5985 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5986 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5987
5988 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5989 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5990 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5991 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5992
5993 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5994 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5995 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5996 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5997
5998 // exit hover when pointer goes away
5999 processKey(mapper, BTN_TOOL_FINGER, 0);
6000 processSync(mapper);
6001 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6002 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6003 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6004 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6005}
6006
6007TEST_F(SingleTouchInputMapperTest, Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006008 addConfigurationProperty("touch.deviceType", "touchScreen");
6009 prepareDisplay(DISPLAY_ORIENTATION_0);
6010 prepareButtons();
6011 prepareAxes(POSITION | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006012 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006013
6014 NotifyMotionArgs motionArgs;
6015
6016 // initially hovering because pressure is 0
6017 processDown(mapper, 100, 200);
6018 processPressure(mapper, 0);
6019 processSync(mapper);
6020 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6021 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6022 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6023 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6024
6025 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6026 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6027 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6028 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6029
6030 // move a little
6031 processMove(mapper, 150, 250);
6032 processSync(mapper);
6033 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6034 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, 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 // down when pressure is non-zero
6039 processPressure(mapper, RAW_PRESSURE_MAX);
6040 processSync(mapper);
6041 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6042 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6043 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6044 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6045
6046 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6047 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6048 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6049 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6050
6051 // up when pressure becomes 0, hover restored
6052 processPressure(mapper, 0);
6053 processSync(mapper);
6054 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6055 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6056 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6057 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6058
6059 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6060 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6061 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6062 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6063
6064 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6065 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6066 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6067 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6068
6069 // exit hover when pointer goes away
6070 processUp(mapper);
6071 processSync(mapper);
6072 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6073 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6074 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6075 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6076}
6077
Michael Wrightd02c5b62014-02-10 15:10:22 -08006078// --- MultiTouchInputMapperTest ---
6079
6080class MultiTouchInputMapperTest : public TouchInputMapperTest {
6081protected:
6082 void prepareAxes(int axes);
6083
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006084 void processPosition(MultiTouchInputMapper& mapper, int32_t x, int32_t y);
6085 void processTouchMajor(MultiTouchInputMapper& mapper, int32_t touchMajor);
6086 void processTouchMinor(MultiTouchInputMapper& mapper, int32_t touchMinor);
6087 void processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor);
6088 void processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor);
6089 void processOrientation(MultiTouchInputMapper& mapper, int32_t orientation);
6090 void processPressure(MultiTouchInputMapper& mapper, int32_t pressure);
6091 void processDistance(MultiTouchInputMapper& mapper, int32_t distance);
6092 void processId(MultiTouchInputMapper& mapper, int32_t id);
6093 void processSlot(MultiTouchInputMapper& mapper, int32_t slot);
6094 void processToolType(MultiTouchInputMapper& mapper, int32_t toolType);
6095 void processKey(MultiTouchInputMapper& mapper, int32_t code, int32_t value);
6096 void processMTSync(MultiTouchInputMapper& mapper);
6097 void processSync(MultiTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006098};
6099
6100void MultiTouchInputMapperTest::prepareAxes(int axes) {
6101 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006102 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
6103 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006104 }
6105 if (axes & TOUCH) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006106 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN,
6107 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006108 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006109 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN,
6110 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006111 }
6112 }
6113 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006114 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
6115 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006116 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006117 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MAX,
6118 RAW_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006119 }
6120 }
6121 if (axes & ORIENTATION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006122 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_ORIENTATION, RAW_ORIENTATION_MIN,
6123 RAW_ORIENTATION_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006124 }
6125 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006126 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_PRESSURE, RAW_PRESSURE_MIN,
6127 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006128 }
6129 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006130 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_DISTANCE, RAW_DISTANCE_MIN,
6131 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006132 }
6133 if (axes & ID) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006134 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX, 0,
6135 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006136 }
6137 if (axes & SLOT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006138 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
6139 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_MT_SLOT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006140 }
6141 if (axes & TOOL_TYPE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006142 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOOL_TYPE, 0, MT_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006143 }
6144}
6145
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006146void MultiTouchInputMapperTest::processPosition(MultiTouchInputMapper& mapper, int32_t x,
6147 int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006148 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_X, x);
6149 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006150}
6151
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006152void MultiTouchInputMapperTest::processTouchMajor(MultiTouchInputMapper& mapper,
6153 int32_t touchMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006154 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006155}
6156
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006157void MultiTouchInputMapperTest::processTouchMinor(MultiTouchInputMapper& mapper,
6158 int32_t touchMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006159 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006160}
6161
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006162void MultiTouchInputMapperTest::processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006163 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006164}
6165
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006166void MultiTouchInputMapperTest::processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006167 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006168}
6169
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006170void MultiTouchInputMapperTest::processOrientation(MultiTouchInputMapper& mapper,
6171 int32_t orientation) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006172 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_ORIENTATION, orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006173}
6174
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006175void MultiTouchInputMapperTest::processPressure(MultiTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006176 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006177}
6178
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006179void MultiTouchInputMapperTest::processDistance(MultiTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006180 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006181}
6182
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006183void MultiTouchInputMapperTest::processId(MultiTouchInputMapper& mapper, int32_t id) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006184 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TRACKING_ID, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006185}
6186
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006187void MultiTouchInputMapperTest::processSlot(MultiTouchInputMapper& mapper, int32_t slot) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006188 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_SLOT, slot);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006189}
6190
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006191void MultiTouchInputMapperTest::processToolType(MultiTouchInputMapper& mapper, int32_t toolType) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006192 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006193}
6194
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006195void MultiTouchInputMapperTest::processKey(MultiTouchInputMapper& mapper, int32_t code,
6196 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006197 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006198}
6199
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006200void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006201 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_MT_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006202}
6203
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006204void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006205 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006206}
6207
Michael Wrightd02c5b62014-02-10 15:10:22 -08006208TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006209 addConfigurationProperty("touch.deviceType", "touchScreen");
6210 prepareDisplay(DISPLAY_ORIENTATION_0);
6211 prepareAxes(POSITION);
6212 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006213 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006214
arthurhungdcef2dc2020-08-11 14:47:50 +08006215 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006216
6217 NotifyMotionArgs motionArgs;
6218
6219 // Two fingers down at once.
6220 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
6221 processPosition(mapper, x1, y1);
6222 processMTSync(mapper);
6223 processPosition(mapper, x2, y2);
6224 processMTSync(mapper);
6225 processSync(mapper);
6226
6227 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6228 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6229 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6230 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6231 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6232 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6233 ASSERT_EQ(0, motionArgs.flags);
6234 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6235 ASSERT_EQ(0, motionArgs.buttonState);
6236 ASSERT_EQ(0, motionArgs.edgeFlags);
6237 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6238 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6239 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6240 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6241 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6242 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6243 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6244 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6245
6246 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6247 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6248 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6249 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6250 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6251 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6252 motionArgs.action);
6253 ASSERT_EQ(0, motionArgs.flags);
6254 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6255 ASSERT_EQ(0, motionArgs.buttonState);
6256 ASSERT_EQ(0, motionArgs.edgeFlags);
6257 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6258 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6259 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6260 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6261 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6262 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6263 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6264 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6265 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6266 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6267 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6268 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6269
6270 // Move.
6271 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
6272 processPosition(mapper, x1, y1);
6273 processMTSync(mapper);
6274 processPosition(mapper, x2, y2);
6275 processMTSync(mapper);
6276 processSync(mapper);
6277
6278 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6279 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6280 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6281 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6282 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6283 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6284 ASSERT_EQ(0, motionArgs.flags);
6285 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6286 ASSERT_EQ(0, motionArgs.buttonState);
6287 ASSERT_EQ(0, motionArgs.edgeFlags);
6288 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6289 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6290 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6291 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6292 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6293 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6294 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6295 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6296 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6297 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6298 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6299 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6300
6301 // First finger up.
6302 x2 += 15; y2 -= 20;
6303 processPosition(mapper, x2, y2);
6304 processMTSync(mapper);
6305 processSync(mapper);
6306
6307 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6308 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6309 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6310 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6311 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6312 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6313 motionArgs.action);
6314 ASSERT_EQ(0, motionArgs.flags);
6315 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6316 ASSERT_EQ(0, motionArgs.buttonState);
6317 ASSERT_EQ(0, motionArgs.edgeFlags);
6318 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6319 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6320 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6321 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6322 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6323 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6324 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6325 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6326 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6327 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6328 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6329 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6330
6331 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6332 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6333 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6334 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6335 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6336 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6337 ASSERT_EQ(0, motionArgs.flags);
6338 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6339 ASSERT_EQ(0, motionArgs.buttonState);
6340 ASSERT_EQ(0, motionArgs.edgeFlags);
6341 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6342 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6343 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6344 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6345 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6346 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6347 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6348 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6349
6350 // Move.
6351 x2 += 20; y2 -= 25;
6352 processPosition(mapper, x2, y2);
6353 processMTSync(mapper);
6354 processSync(mapper);
6355
6356 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6357 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6358 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6359 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6360 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6361 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6362 ASSERT_EQ(0, motionArgs.flags);
6363 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6364 ASSERT_EQ(0, motionArgs.buttonState);
6365 ASSERT_EQ(0, motionArgs.edgeFlags);
6366 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6367 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6368 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6369 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6370 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6371 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6372 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6373 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6374
6375 // New finger down.
6376 int32_t x3 = 700, y3 = 300;
6377 processPosition(mapper, x2, y2);
6378 processMTSync(mapper);
6379 processPosition(mapper, x3, y3);
6380 processMTSync(mapper);
6381 processSync(mapper);
6382
6383 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6384 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6385 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6386 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6387 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6388 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6389 motionArgs.action);
6390 ASSERT_EQ(0, motionArgs.flags);
6391 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6392 ASSERT_EQ(0, motionArgs.buttonState);
6393 ASSERT_EQ(0, motionArgs.edgeFlags);
6394 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6395 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6396 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6397 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6398 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6399 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6400 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6401 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6402 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6403 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6404 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6405 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6406
6407 // Second finger up.
6408 x3 += 30; y3 -= 20;
6409 processPosition(mapper, x3, y3);
6410 processMTSync(mapper);
6411 processSync(mapper);
6412
6413 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6414 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6415 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6416 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6417 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6418 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6419 motionArgs.action);
6420 ASSERT_EQ(0, motionArgs.flags);
6421 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6422 ASSERT_EQ(0, motionArgs.buttonState);
6423 ASSERT_EQ(0, motionArgs.edgeFlags);
6424 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6425 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6426 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6427 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6428 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6429 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6430 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6431 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6432 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6433 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6434 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6435 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6436
6437 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6438 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6439 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6440 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6441 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6442 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6443 ASSERT_EQ(0, motionArgs.flags);
6444 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6445 ASSERT_EQ(0, motionArgs.buttonState);
6446 ASSERT_EQ(0, motionArgs.edgeFlags);
6447 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6448 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6449 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6450 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6451 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6452 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6453 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6454 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6455
6456 // Last finger up.
6457 processMTSync(mapper);
6458 processSync(mapper);
6459
6460 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6461 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6462 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6463 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6464 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6465 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6466 ASSERT_EQ(0, motionArgs.flags);
6467 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6468 ASSERT_EQ(0, motionArgs.buttonState);
6469 ASSERT_EQ(0, motionArgs.edgeFlags);
6470 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6471 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6472 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6473 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6474 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6475 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6476 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6477 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6478
6479 // Should not have sent any more keys or motions.
6480 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6481 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6482}
6483
6484TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006485 addConfigurationProperty("touch.deviceType", "touchScreen");
6486 prepareDisplay(DISPLAY_ORIENTATION_0);
6487 prepareAxes(POSITION | ID);
6488 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006489 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006490
arthurhungdcef2dc2020-08-11 14:47:50 +08006491 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006492
6493 NotifyMotionArgs motionArgs;
6494
6495 // Two fingers down at once.
6496 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
6497 processPosition(mapper, x1, y1);
6498 processId(mapper, 1);
6499 processMTSync(mapper);
6500 processPosition(mapper, x2, y2);
6501 processId(mapper, 2);
6502 processMTSync(mapper);
6503 processSync(mapper);
6504
6505 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6506 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6507 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6508 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6509 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6510 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6511 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6512
6513 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6514 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6515 motionArgs.action);
6516 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6517 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6518 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6519 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6520 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6521 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6522 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6523 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6524 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6525
6526 // Move.
6527 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
6528 processPosition(mapper, x1, y1);
6529 processId(mapper, 1);
6530 processMTSync(mapper);
6531 processPosition(mapper, x2, y2);
6532 processId(mapper, 2);
6533 processMTSync(mapper);
6534 processSync(mapper);
6535
6536 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6537 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6538 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6539 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6540 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6541 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6542 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6543 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6544 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6545 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6546 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6547
6548 // First finger up.
6549 x2 += 15; y2 -= 20;
6550 processPosition(mapper, x2, y2);
6551 processId(mapper, 2);
6552 processMTSync(mapper);
6553 processSync(mapper);
6554
6555 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6556 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6557 motionArgs.action);
6558 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6559 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6560 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6561 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6562 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6563 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6564 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6565 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6566 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6567
6568 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6569 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6570 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6571 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6572 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6573 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6574 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6575
6576 // Move.
6577 x2 += 20; y2 -= 25;
6578 processPosition(mapper, x2, y2);
6579 processId(mapper, 2);
6580 processMTSync(mapper);
6581 processSync(mapper);
6582
6583 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6584 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6585 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6586 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6587 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6588 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6589 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6590
6591 // New finger down.
6592 int32_t x3 = 700, y3 = 300;
6593 processPosition(mapper, x2, y2);
6594 processId(mapper, 2);
6595 processMTSync(mapper);
6596 processPosition(mapper, x3, y3);
6597 processId(mapper, 3);
6598 processMTSync(mapper);
6599 processSync(mapper);
6600
6601 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6602 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6603 motionArgs.action);
6604 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6605 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6606 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6607 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6608 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6609 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6610 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6611 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6612 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6613
6614 // Second finger up.
6615 x3 += 30; y3 -= 20;
6616 processPosition(mapper, x3, y3);
6617 processId(mapper, 3);
6618 processMTSync(mapper);
6619 processSync(mapper);
6620
6621 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6622 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6623 motionArgs.action);
6624 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6625 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6626 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6627 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6628 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6629 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6630 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6631 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6632 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6633
6634 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6635 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6636 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6637 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6638 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6639 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6640 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6641
6642 // Last finger up.
6643 processMTSync(mapper);
6644 processSync(mapper);
6645
6646 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6647 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6648 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6649 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6650 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6651 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6652 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6653
6654 // Should not have sent any more keys or motions.
6655 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6656 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6657}
6658
6659TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006660 addConfigurationProperty("touch.deviceType", "touchScreen");
6661 prepareDisplay(DISPLAY_ORIENTATION_0);
6662 prepareAxes(POSITION | ID | SLOT);
6663 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006664 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006665
arthurhungdcef2dc2020-08-11 14:47:50 +08006666 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006667
6668 NotifyMotionArgs motionArgs;
6669
6670 // Two fingers down at once.
6671 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
6672 processPosition(mapper, x1, y1);
6673 processId(mapper, 1);
6674 processSlot(mapper, 1);
6675 processPosition(mapper, x2, y2);
6676 processId(mapper, 2);
6677 processSync(mapper);
6678
6679 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6680 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, 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(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6686
6687 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6688 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6689 motionArgs.action);
6690 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6691 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6692 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6693 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6694 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6695 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6696 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6697 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6698 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6699
6700 // Move.
6701 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
6702 processSlot(mapper, 0);
6703 processPosition(mapper, x1, y1);
6704 processSlot(mapper, 1);
6705 processPosition(mapper, x2, y2);
6706 processSync(mapper);
6707
6708 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6709 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6710 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6711 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6712 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6713 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6714 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6715 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6716 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6717 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6718 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6719
6720 // First finger up.
6721 x2 += 15; y2 -= 20;
6722 processSlot(mapper, 0);
6723 processId(mapper, -1);
6724 processSlot(mapper, 1);
6725 processPosition(mapper, x2, y2);
6726 processSync(mapper);
6727
6728 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6729 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6730 motionArgs.action);
6731 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6732 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6733 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6734 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6735 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6736 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6737 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6738 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6739 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6740
6741 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6742 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6743 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6744 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6745 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6746 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6747 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6748
6749 // Move.
6750 x2 += 20; y2 -= 25;
6751 processPosition(mapper, x2, y2);
6752 processSync(mapper);
6753
6754 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6755 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6756 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6757 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6758 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6759 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6760 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6761
6762 // New finger down.
6763 int32_t x3 = 700, y3 = 300;
6764 processPosition(mapper, x2, y2);
6765 processSlot(mapper, 0);
6766 processId(mapper, 3);
6767 processPosition(mapper, x3, y3);
6768 processSync(mapper);
6769
6770 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6771 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6772 motionArgs.action);
6773 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6774 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6775 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6776 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6777 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6778 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6779 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6780 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6781 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6782
6783 // Second finger up.
6784 x3 += 30; y3 -= 20;
6785 processSlot(mapper, 1);
6786 processId(mapper, -1);
6787 processSlot(mapper, 0);
6788 processPosition(mapper, x3, y3);
6789 processSync(mapper);
6790
6791 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6792 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6793 motionArgs.action);
6794 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6795 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6796 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6797 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6798 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6799 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6800 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6801 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6802 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6803
6804 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6805 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6806 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6807 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6808 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6809 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6810 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6811
6812 // Last finger up.
6813 processId(mapper, -1);
6814 processSync(mapper);
6815
6816 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6817 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6818 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6819 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6820 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6821 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6822 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6823
6824 // Should not have sent any more keys or motions.
6825 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6826 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6827}
6828
6829TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006830 addConfigurationProperty("touch.deviceType", "touchScreen");
6831 prepareDisplay(DISPLAY_ORIENTATION_0);
6832 prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006833 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006834
6835 // These calculations are based on the input device calibration documentation.
6836 int32_t rawX = 100;
6837 int32_t rawY = 200;
6838 int32_t rawTouchMajor = 7;
6839 int32_t rawTouchMinor = 6;
6840 int32_t rawToolMajor = 9;
6841 int32_t rawToolMinor = 8;
6842 int32_t rawPressure = 11;
6843 int32_t rawDistance = 0;
6844 int32_t rawOrientation = 3;
6845 int32_t id = 5;
6846
6847 float x = toDisplayX(rawX);
6848 float y = toDisplayY(rawY);
6849 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
6850 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
6851 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
6852 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
6853 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
6854 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
6855 float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
6856 float distance = float(rawDistance);
6857
6858 processPosition(mapper, rawX, rawY);
6859 processTouchMajor(mapper, rawTouchMajor);
6860 processTouchMinor(mapper, rawTouchMinor);
6861 processToolMajor(mapper, rawToolMajor);
6862 processToolMinor(mapper, rawToolMinor);
6863 processPressure(mapper, rawPressure);
6864 processOrientation(mapper, rawOrientation);
6865 processDistance(mapper, rawDistance);
6866 processId(mapper, id);
6867 processMTSync(mapper);
6868 processSync(mapper);
6869
6870 NotifyMotionArgs args;
6871 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6872 ASSERT_EQ(0, args.pointerProperties[0].id);
6873 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6874 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
6875 orientation, distance));
6876}
6877
6878TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006879 addConfigurationProperty("touch.deviceType", "touchScreen");
6880 prepareDisplay(DISPLAY_ORIENTATION_0);
6881 prepareAxes(POSITION | TOUCH | TOOL | MINOR);
6882 addConfigurationProperty("touch.size.calibration", "geometric");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006883 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006884
6885 // These calculations are based on the input device calibration documentation.
6886 int32_t rawX = 100;
6887 int32_t rawY = 200;
6888 int32_t rawTouchMajor = 140;
6889 int32_t rawTouchMinor = 120;
6890 int32_t rawToolMajor = 180;
6891 int32_t rawToolMinor = 160;
6892
6893 float x = toDisplayX(rawX);
6894 float y = toDisplayY(rawY);
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
6901 processPosition(mapper, rawX, rawY);
6902 processTouchMajor(mapper, rawTouchMajor);
6903 processTouchMinor(mapper, rawTouchMinor);
6904 processToolMajor(mapper, rawToolMajor);
6905 processToolMinor(mapper, rawToolMinor);
6906 processMTSync(mapper);
6907 processSync(mapper);
6908
6909 NotifyMotionArgs args;
6910 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6911 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6912 x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
6913}
6914
6915TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006916 addConfigurationProperty("touch.deviceType", "touchScreen");
6917 prepareDisplay(DISPLAY_ORIENTATION_0);
6918 prepareAxes(POSITION | TOUCH | TOOL);
6919 addConfigurationProperty("touch.size.calibration", "diameter");
6920 addConfigurationProperty("touch.size.scale", "10");
6921 addConfigurationProperty("touch.size.bias", "160");
6922 addConfigurationProperty("touch.size.isSummed", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006923 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006924
6925 // These calculations are based on the input device calibration documentation.
6926 // Note: We only provide a single common touch/tool value because the device is assumed
6927 // not to emit separate values for each pointer (isSummed = 1).
6928 int32_t rawX = 100;
6929 int32_t rawY = 200;
6930 int32_t rawX2 = 150;
6931 int32_t rawY2 = 250;
6932 int32_t rawTouchMajor = 5;
6933 int32_t rawToolMajor = 8;
6934
6935 float x = toDisplayX(rawX);
6936 float y = toDisplayY(rawY);
6937 float x2 = toDisplayX(rawX2);
6938 float y2 = toDisplayY(rawY2);
6939 float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
6940 float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
6941 float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
6942
6943 processPosition(mapper, rawX, rawY);
6944 processTouchMajor(mapper, rawTouchMajor);
6945 processToolMajor(mapper, rawToolMajor);
6946 processMTSync(mapper);
6947 processPosition(mapper, rawX2, rawY2);
6948 processTouchMajor(mapper, rawTouchMajor);
6949 processToolMajor(mapper, rawToolMajor);
6950 processMTSync(mapper);
6951 processSync(mapper);
6952
6953 NotifyMotionArgs args;
6954 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6955 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
6956
6957 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6958 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6959 args.action);
6960 ASSERT_EQ(size_t(2), args.pointerCount);
6961 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6962 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
6963 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
6964 x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
6965}
6966
6967TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006968 addConfigurationProperty("touch.deviceType", "touchScreen");
6969 prepareDisplay(DISPLAY_ORIENTATION_0);
6970 prepareAxes(POSITION | TOUCH | TOOL);
6971 addConfigurationProperty("touch.size.calibration", "area");
6972 addConfigurationProperty("touch.size.scale", "43");
6973 addConfigurationProperty("touch.size.bias", "3");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006974 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006975
6976 // These calculations are based on the input device calibration documentation.
6977 int32_t rawX = 100;
6978 int32_t rawY = 200;
6979 int32_t rawTouchMajor = 5;
6980 int32_t rawToolMajor = 8;
6981
6982 float x = toDisplayX(rawX);
6983 float y = toDisplayY(rawY);
6984 float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
6985 float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
6986 float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
6987
6988 processPosition(mapper, rawX, rawY);
6989 processTouchMajor(mapper, rawTouchMajor);
6990 processToolMajor(mapper, rawToolMajor);
6991 processMTSync(mapper);
6992 processSync(mapper);
6993
6994 NotifyMotionArgs args;
6995 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6996 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6997 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
6998}
6999
7000TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007001 addConfigurationProperty("touch.deviceType", "touchScreen");
7002 prepareDisplay(DISPLAY_ORIENTATION_0);
7003 prepareAxes(POSITION | PRESSURE);
7004 addConfigurationProperty("touch.pressure.calibration", "amplitude");
7005 addConfigurationProperty("touch.pressure.scale", "0.01");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007006 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007007
Michael Wrightaa449c92017-12-13 21:21:43 +00007008 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007009 mapper.populateDeviceInfo(&info);
Michael Wrightaa449c92017-12-13 21:21:43 +00007010 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
7011 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TOUCHSCREEN,
7012 0.0f, RAW_PRESSURE_MAX * 0.01, 0.0f, 0.0f));
7013
Michael Wrightd02c5b62014-02-10 15:10:22 -08007014 // These calculations are based on the input device calibration documentation.
7015 int32_t rawX = 100;
7016 int32_t rawY = 200;
7017 int32_t rawPressure = 60;
7018
7019 float x = toDisplayX(rawX);
7020 float y = toDisplayY(rawY);
7021 float pressure = float(rawPressure) * 0.01f;
7022
7023 processPosition(mapper, rawX, rawY);
7024 processPressure(mapper, rawPressure);
7025 processMTSync(mapper);
7026 processSync(mapper);
7027
7028 NotifyMotionArgs args;
7029 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7030 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7031 x, y, pressure, 0, 0, 0, 0, 0, 0, 0));
7032}
7033
7034TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007035 addConfigurationProperty("touch.deviceType", "touchScreen");
7036 prepareDisplay(DISPLAY_ORIENTATION_0);
7037 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007038 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007039
7040 NotifyMotionArgs motionArgs;
7041 NotifyKeyArgs keyArgs;
7042
7043 processId(mapper, 1);
7044 processPosition(mapper, 100, 200);
7045 processSync(mapper);
7046 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7047 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7048 ASSERT_EQ(0, motionArgs.buttonState);
7049
7050 // press BTN_LEFT, release BTN_LEFT
7051 processKey(mapper, BTN_LEFT, 1);
7052 processSync(mapper);
7053 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7054 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7055 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
7056
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007057 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7058 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7059 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
7060
Michael Wrightd02c5b62014-02-10 15:10:22 -08007061 processKey(mapper, BTN_LEFT, 0);
7062 processSync(mapper);
7063 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007064 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007065 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007066
7067 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007068 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007069 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007070
7071 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
7072 processKey(mapper, BTN_RIGHT, 1);
7073 processKey(mapper, BTN_MIDDLE, 1);
7074 processSync(mapper);
7075 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7076 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7077 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
7078 motionArgs.buttonState);
7079
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007080 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7081 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7082 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
7083
7084 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7085 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7086 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
7087 motionArgs.buttonState);
7088
Michael Wrightd02c5b62014-02-10 15:10:22 -08007089 processKey(mapper, BTN_RIGHT, 0);
7090 processSync(mapper);
7091 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007092 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007093 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007094
7095 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007096 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007097 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007098
7099 processKey(mapper, BTN_MIDDLE, 0);
7100 processSync(mapper);
7101 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007102 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007103 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007104
7105 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007106 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007107 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007108
7109 // press BTN_BACK, release BTN_BACK
7110 processKey(mapper, BTN_BACK, 1);
7111 processSync(mapper);
7112 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7113 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
7114 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007115
Michael Wrightd02c5b62014-02-10 15:10:22 -08007116 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007117 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007118 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
7119
7120 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7121 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7122 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007123
7124 processKey(mapper, BTN_BACK, 0);
7125 processSync(mapper);
7126 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007127 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007128 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007129
7130 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007131 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007132 ASSERT_EQ(0, motionArgs.buttonState);
7133
Michael Wrightd02c5b62014-02-10 15:10:22 -08007134 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7135 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
7136 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
7137
7138 // press BTN_SIDE, release BTN_SIDE
7139 processKey(mapper, BTN_SIDE, 1);
7140 processSync(mapper);
7141 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7142 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
7143 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007144
Michael Wrightd02c5b62014-02-10 15:10:22 -08007145 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007146 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007147 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
7148
7149 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7150 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7151 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007152
7153 processKey(mapper, BTN_SIDE, 0);
7154 processSync(mapper);
7155 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007156 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007157 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007158
7159 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007160 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007161 ASSERT_EQ(0, motionArgs.buttonState);
7162
Michael Wrightd02c5b62014-02-10 15:10:22 -08007163 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7164 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
7165 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
7166
7167 // press BTN_FORWARD, release BTN_FORWARD
7168 processKey(mapper, BTN_FORWARD, 1);
7169 processSync(mapper);
7170 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7171 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
7172 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007173
Michael Wrightd02c5b62014-02-10 15:10:22 -08007174 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007175 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007176 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
7177
7178 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7179 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7180 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007181
7182 processKey(mapper, BTN_FORWARD, 0);
7183 processSync(mapper);
7184 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007185 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007186 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007187
7188 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007189 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007190 ASSERT_EQ(0, motionArgs.buttonState);
7191
Michael Wrightd02c5b62014-02-10 15:10:22 -08007192 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7193 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
7194 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
7195
7196 // press BTN_EXTRA, release BTN_EXTRA
7197 processKey(mapper, BTN_EXTRA, 1);
7198 processSync(mapper);
7199 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7200 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
7201 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007202
Michael Wrightd02c5b62014-02-10 15:10:22 -08007203 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007204 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007205 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
7206
7207 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7208 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7209 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007210
7211 processKey(mapper, BTN_EXTRA, 0);
7212 processSync(mapper);
7213 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007214 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007215 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007216
7217 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007218 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007219 ASSERT_EQ(0, motionArgs.buttonState);
7220
Michael Wrightd02c5b62014-02-10 15:10:22 -08007221 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7222 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
7223 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
7224
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007225 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7226
Michael Wrightd02c5b62014-02-10 15:10:22 -08007227 // press BTN_STYLUS, release BTN_STYLUS
7228 processKey(mapper, BTN_STYLUS, 1);
7229 processSync(mapper);
7230 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7231 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007232 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
7233
7234 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7235 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7236 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007237
7238 processKey(mapper, BTN_STYLUS, 0);
7239 processSync(mapper);
7240 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007241 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007242 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007243
7244 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007245 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007246 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007247
7248 // press BTN_STYLUS2, release BTN_STYLUS2
7249 processKey(mapper, BTN_STYLUS2, 1);
7250 processSync(mapper);
7251 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7252 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007253 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
7254
7255 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7256 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7257 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007258
7259 processKey(mapper, BTN_STYLUS2, 0);
7260 processSync(mapper);
7261 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007262 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007263 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007264
7265 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007266 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007267 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007268
7269 // release touch
7270 processId(mapper, -1);
7271 processSync(mapper);
7272 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7273 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7274 ASSERT_EQ(0, motionArgs.buttonState);
7275}
7276
7277TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007278 addConfigurationProperty("touch.deviceType", "touchScreen");
7279 prepareDisplay(DISPLAY_ORIENTATION_0);
7280 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007281 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007282
7283 NotifyMotionArgs motionArgs;
7284
7285 // default tool type is finger
7286 processId(mapper, 1);
7287 processPosition(mapper, 100, 200);
7288 processSync(mapper);
7289 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7290 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7291 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7292
7293 // eraser
7294 processKey(mapper, BTN_TOOL_RUBBER, 1);
7295 processSync(mapper);
7296 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7297 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7298 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
7299
7300 // stylus
7301 processKey(mapper, BTN_TOOL_RUBBER, 0);
7302 processKey(mapper, BTN_TOOL_PEN, 1);
7303 processSync(mapper);
7304 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7305 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7306 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7307
7308 // brush
7309 processKey(mapper, BTN_TOOL_PEN, 0);
7310 processKey(mapper, BTN_TOOL_BRUSH, 1);
7311 processSync(mapper);
7312 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7313 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7314 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7315
7316 // pencil
7317 processKey(mapper, BTN_TOOL_BRUSH, 0);
7318 processKey(mapper, BTN_TOOL_PENCIL, 1);
7319 processSync(mapper);
7320 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7321 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7322 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7323
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08007324 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08007325 processKey(mapper, BTN_TOOL_PENCIL, 0);
7326 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
7327 processSync(mapper);
7328 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7329 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7330 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7331
7332 // mouse
7333 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
7334 processKey(mapper, BTN_TOOL_MOUSE, 1);
7335 processSync(mapper);
7336 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7337 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7338 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
7339
7340 // lens
7341 processKey(mapper, BTN_TOOL_MOUSE, 0);
7342 processKey(mapper, BTN_TOOL_LENS, 1);
7343 processSync(mapper);
7344 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7345 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7346 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
7347
7348 // double-tap
7349 processKey(mapper, BTN_TOOL_LENS, 0);
7350 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
7351 processSync(mapper);
7352 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7353 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7354 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7355
7356 // triple-tap
7357 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
7358 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
7359 processSync(mapper);
7360 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7361 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7362 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7363
7364 // quad-tap
7365 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
7366 processKey(mapper, BTN_TOOL_QUADTAP, 1);
7367 processSync(mapper);
7368 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7369 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7370 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7371
7372 // finger
7373 processKey(mapper, BTN_TOOL_QUADTAP, 0);
7374 processKey(mapper, BTN_TOOL_FINGER, 1);
7375 processSync(mapper);
7376 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7377 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7378 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7379
7380 // stylus trumps finger
7381 processKey(mapper, BTN_TOOL_PEN, 1);
7382 processSync(mapper);
7383 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7384 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7385 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7386
7387 // eraser trumps stylus
7388 processKey(mapper, BTN_TOOL_RUBBER, 1);
7389 processSync(mapper);
7390 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7391 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7392 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
7393
7394 // mouse trumps eraser
7395 processKey(mapper, BTN_TOOL_MOUSE, 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_MOUSE, motionArgs.pointerProperties[0].toolType);
7400
7401 // MT tool type trumps BTN tool types: MT_TOOL_FINGER
7402 processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
7403 processSync(mapper);
7404 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7405 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7406 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7407
7408 // MT tool type trumps BTN tool types: MT_TOOL_PEN
7409 processToolType(mapper, MT_TOOL_PEN);
7410 processSync(mapper);
7411 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7412 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7413 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7414
7415 // back to default tool type
7416 processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
7417 processKey(mapper, BTN_TOOL_MOUSE, 0);
7418 processKey(mapper, BTN_TOOL_RUBBER, 0);
7419 processKey(mapper, BTN_TOOL_PEN, 0);
7420 processKey(mapper, BTN_TOOL_FINGER, 0);
7421 processSync(mapper);
7422 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7423 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7424 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7425}
7426
7427TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007428 addConfigurationProperty("touch.deviceType", "touchScreen");
7429 prepareDisplay(DISPLAY_ORIENTATION_0);
7430 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007431 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007432 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007433
7434 NotifyMotionArgs motionArgs;
7435
7436 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
7437 processId(mapper, 1);
7438 processPosition(mapper, 100, 200);
7439 processSync(mapper);
7440 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7441 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
7442 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7443 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
7444
7445 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7446 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7447 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7448 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
7449
7450 // move a little
7451 processPosition(mapper, 150, 250);
7452 processSync(mapper);
7453 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7454 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7455 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7456 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7457
7458 // down when BTN_TOUCH is pressed, pressure defaults to 1
7459 processKey(mapper, BTN_TOUCH, 1);
7460 processSync(mapper);
7461 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7462 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
7463 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7464 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7465
7466 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7467 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7468 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7469 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
7470
7471 // up when BTN_TOUCH is released, hover restored
7472 processKey(mapper, BTN_TOUCH, 0);
7473 processSync(mapper);
7474 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7475 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7476 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7477 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
7478
7479 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7480 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
7481 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7482 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7483
7484 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7485 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7486 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7487 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7488
7489 // exit hover when pointer goes away
7490 processId(mapper, -1);
7491 processSync(mapper);
7492 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7493 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
7494 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7495 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7496}
7497
7498TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007499 addConfigurationProperty("touch.deviceType", "touchScreen");
7500 prepareDisplay(DISPLAY_ORIENTATION_0);
7501 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007502 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007503
7504 NotifyMotionArgs motionArgs;
7505
7506 // initially hovering because pressure is 0
7507 processId(mapper, 1);
7508 processPosition(mapper, 100, 200);
7509 processPressure(mapper, 0);
7510 processSync(mapper);
7511 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7512 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
7513 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7514 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
7515
7516 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7517 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7518 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7519 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
7520
7521 // move a little
7522 processPosition(mapper, 150, 250);
7523 processSync(mapper);
7524 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7525 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, 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 // down when pressure becomes non-zero
7530 processPressure(mapper, RAW_PRESSURE_MAX);
7531 processSync(mapper);
7532 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7533 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
7534 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7535 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7536
7537 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7538 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7539 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7540 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
7541
7542 // up when pressure becomes 0, hover restored
7543 processPressure(mapper, 0);
7544 processSync(mapper);
7545 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7546 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7547 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7548 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
7549
7550 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7551 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
7552 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7553 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7554
7555 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7556 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7557 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7558 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7559
7560 // exit hover when pointer goes away
7561 processId(mapper, -1);
7562 processSync(mapper);
7563 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7564 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
7565 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7566 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7567}
7568
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07007569/**
7570 * Set the input device port <--> display port associations, and check that the
7571 * events are routed to the display that matches the display port.
7572 * This can be checked by looking at the displayId of the resulting NotifyMotionArgs.
7573 */
7574TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayPort) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07007575 const std::string usb2 = "USB2";
7576 const uint8_t hdmi1 = 0;
7577 const uint8_t hdmi2 = 1;
7578 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007579 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07007580
7581 addConfigurationProperty("touch.deviceType", "touchScreen");
7582 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007583 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07007584
7585 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
7586 mFakePolicy->addInputPortAssociation(usb2, hdmi2);
7587
7588 // We are intentionally not adding the viewport for display 1 yet. Since the port association
7589 // for this input device is specified, and the matching viewport is not present,
7590 // the input device should be disabled (at the mapper level).
7591
7592 // Add viewport for display 2 on hdmi2
7593 prepareSecondaryDisplay(type, hdmi2);
7594 // Send a touch event
7595 processPosition(mapper, 100, 100);
7596 processSync(mapper);
7597 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7598
7599 // Add viewport for display 1 on hdmi1
7600 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
7601 // Send a touch event again
7602 processPosition(mapper, 100, 100);
7603 processSync(mapper);
7604
7605 NotifyMotionArgs args;
7606 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7607 ASSERT_EQ(DISPLAY_ID, args.displayId);
7608}
Michael Wrightd02c5b62014-02-10 15:10:22 -08007609
Arthur Hungc7ad2d02018-12-18 17:41:29 +08007610TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShouldHandleDisplayId) {
Garfield Tan888a6a42020-01-09 11:39:16 -08007611 // Setup for second display.
Michael Wright17db18e2020-06-26 20:51:44 +01007612 std::shared_ptr<FakePointerController> fakePointerController =
7613 std::make_shared<FakePointerController>();
Garfield Tan888a6a42020-01-09 11:39:16 -08007614 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08007615 fakePointerController->setPosition(100, 200);
7616 fakePointerController->setButtonState(0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08007617 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
7618
Garfield Tan888a6a42020-01-09 11:39:16 -08007619 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007620 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08007621
Arthur Hungc7ad2d02018-12-18 17:41:29 +08007622 prepareDisplay(DISPLAY_ORIENTATION_0);
7623 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007624 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08007625
7626 // Check source is mouse that would obtain the PointerController.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007627 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08007628
7629 NotifyMotionArgs motionArgs;
7630 processPosition(mapper, 100, 100);
7631 processSync(mapper);
7632
7633 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7634 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7635 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
7636}
7637
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00007638/**
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007639 * Ensure that the readTime is set to the SYN_REPORT value when processing touch events.
7640 */
7641TEST_F(MultiTouchInputMapperTest, Process_SendsReadTime) {
7642 addConfigurationProperty("touch.deviceType", "touchScreen");
7643 prepareAxes(POSITION);
7644 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7645
7646 prepareDisplay(DISPLAY_ORIENTATION_0);
7647 process(mapper, 10, 11 /*readTime*/, EV_ABS, ABS_MT_TRACKING_ID, 1);
7648 process(mapper, 15, 16 /*readTime*/, EV_ABS, ABS_MT_POSITION_X, 100);
7649 process(mapper, 20, 21 /*readTime*/, EV_ABS, ABS_MT_POSITION_Y, 100);
7650 process(mapper, 25, 26 /*readTime*/, EV_SYN, SYN_REPORT, 0);
7651
7652 NotifyMotionArgs args;
7653 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7654 ASSERT_EQ(26, args.readTime);
7655
7656 process(mapper, 30, 31 /*readTime*/, EV_ABS, ABS_MT_POSITION_X, 110);
7657 process(mapper, 30, 32 /*readTime*/, EV_ABS, ABS_MT_POSITION_Y, 220);
7658 process(mapper, 30, 33 /*readTime*/, EV_SYN, SYN_REPORT, 0);
7659
7660 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7661 ASSERT_EQ(33, args.readTime);
7662}
7663
7664/**
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00007665 * When the viewport is not active (isActive=false), the touch mapper should be disabled and the
7666 * events should not be delivered to the listener.
7667 */
7668TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreDropped) {
7669 addConfigurationProperty("touch.deviceType", "touchScreen");
7670 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
7671 DISPLAY_ORIENTATION_0, false /*isActive*/, UNIQUE_ID, NO_PORT,
7672 ViewportType::INTERNAL);
7673 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
7674 prepareAxes(POSITION);
7675 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7676
7677 NotifyMotionArgs motionArgs;
7678 processPosition(mapper, 100, 100);
7679 processSync(mapper);
7680
7681 mFakeListener->assertNotifyMotionWasNotCalled();
7682}
7683
Garfield Tanc734e4f2021-01-15 20:01:39 -08007684TEST_F(MultiTouchInputMapperTest, Process_DeactivateViewport_AbortTouches) {
7685 addConfigurationProperty("touch.deviceType", "touchScreen");
7686 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
7687 DISPLAY_ORIENTATION_0, true /*isActive*/, UNIQUE_ID, NO_PORT,
7688 ViewportType::INTERNAL);
7689 std::optional<DisplayViewport> optionalDisplayViewport =
7690 mFakePolicy->getDisplayViewportByUniqueId(UNIQUE_ID);
7691 ASSERT_TRUE(optionalDisplayViewport.has_value());
7692 DisplayViewport displayViewport = *optionalDisplayViewport;
7693
7694 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
7695 prepareAxes(POSITION);
7696 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7697
7698 // Finger down
7699 int32_t x = 100, y = 100;
7700 processPosition(mapper, x, y);
7701 processSync(mapper);
7702
7703 NotifyMotionArgs motionArgs;
7704 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7705 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7706
7707 // Deactivate display viewport
7708 displayViewport.isActive = false;
7709 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
7710 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
7711
7712 // Finger move
7713 x += 10, y += 10;
7714 processPosition(mapper, x, y);
7715 processSync(mapper);
7716
7717 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7718 EXPECT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
7719
7720 // Reactivate display viewport
7721 displayViewport.isActive = true;
7722 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
7723 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
7724
7725 // Finger move again
7726 x += 10, y += 10;
7727 processPosition(mapper, x, y);
7728 processSync(mapper);
7729
7730 // Gesture is aborted, so events after display is activated won't be dispatched until there is
7731 // no pointer on the touch device.
7732 mFakeListener->assertNotifyMotionWasNotCalled();
7733}
7734
Arthur Hung7c645402019-01-25 17:45:42 +08007735TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShowTouches) {
7736 // Setup the first touch screen device.
Arthur Hung7c645402019-01-25 17:45:42 +08007737 prepareAxes(POSITION | ID | SLOT);
7738 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007739 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung7c645402019-01-25 17:45:42 +08007740
7741 // Create the second touch screen device, and enable multi fingers.
7742 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08007743 const std::string DEVICE_NAME2 = "TOUCHSCREEN2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08007744 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007745 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08007746 std::shared_ptr<InputDevice> device2 =
7747 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
7748 Flags<InputDeviceClass>(0));
7749
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007750 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
7751 0 /*flat*/, 0 /*fuzz*/);
7752 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
7753 0 /*flat*/, 0 /*fuzz*/);
7754 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX,
7755 0 /*flat*/, 0 /*fuzz*/);
7756 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX,
7757 0 /*flat*/, 0 /*fuzz*/);
7758 mFakeEventHub->setAbsoluteAxisValue(SECOND_EVENTHUB_ID, ABS_MT_SLOT, 0 /*value*/);
7759 mFakeEventHub->addConfigurationProperty(SECOND_EVENTHUB_ID, String8("touch.deviceType"),
7760 String8("touchScreen"));
Arthur Hung7c645402019-01-25 17:45:42 +08007761
7762 // Setup the second touch screen device.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007763 MultiTouchInputMapper& mapper2 = device2->addMapper<MultiTouchInputMapper>(SECOND_EVENTHUB_ID);
Arthur Hung7c645402019-01-25 17:45:42 +08007764 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
7765 device2->reset(ARBITRARY_TIME);
7766
7767 // Setup PointerController.
Michael Wright17db18e2020-06-26 20:51:44 +01007768 std::shared_ptr<FakePointerController> fakePointerController =
7769 std::make_shared<FakePointerController>();
Arthur Hung7c645402019-01-25 17:45:42 +08007770 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
7771 mFakePolicy->setPointerController(SECOND_DEVICE_ID, fakePointerController);
7772
7773 // Setup policy for associated displays and show touches.
7774 const uint8_t hdmi1 = 0;
7775 const uint8_t hdmi2 = 1;
7776 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
7777 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
7778 mFakePolicy->setShowTouches(true);
7779
7780 // Create displays.
7781 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007782 prepareSecondaryDisplay(ViewportType::EXTERNAL, hdmi2);
Arthur Hung7c645402019-01-25 17:45:42 +08007783
7784 // Default device will reconfigure above, need additional reconfiguration for another device.
7785 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007786 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung7c645402019-01-25 17:45:42 +08007787
7788 // Two fingers down at default display.
7789 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7790 processPosition(mapper, x1, y1);
7791 processId(mapper, 1);
7792 processSlot(mapper, 1);
7793 processPosition(mapper, x2, y2);
7794 processId(mapper, 2);
7795 processSync(mapper);
7796
7797 std::map<int32_t, std::vector<int32_t>>::const_iterator iter =
7798 fakePointerController->getSpots().find(DISPLAY_ID);
7799 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
7800 ASSERT_EQ(size_t(2), iter->second.size());
7801
7802 // Two fingers down at second display.
7803 processPosition(mapper2, x1, y1);
7804 processId(mapper2, 1);
7805 processSlot(mapper2, 1);
7806 processPosition(mapper2, x2, y2);
7807 processId(mapper2, 2);
7808 processSync(mapper2);
7809
7810 iter = fakePointerController->getSpots().find(SECONDARY_DISPLAY_ID);
7811 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
7812 ASSERT_EQ(size_t(2), iter->second.size());
7813}
7814
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06007815TEST_F(MultiTouchInputMapperTest, VideoFrames_ReceivedByListener) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06007816 prepareAxes(POSITION);
7817 addConfigurationProperty("touch.deviceType", "touchScreen");
7818 prepareDisplay(DISPLAY_ORIENTATION_0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007819 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06007820
7821 NotifyMotionArgs motionArgs;
7822 // Unrotated video frame
7823 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
7824 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007825 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06007826 processPosition(mapper, 100, 200);
7827 processSync(mapper);
7828 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7829 ASSERT_EQ(frames, motionArgs.videoFrames);
7830
7831 // Subsequent touch events should not have any videoframes
7832 // This is implemented separately in FakeEventHub,
7833 // but that should match the behaviour of TouchVideoDevice.
7834 processPosition(mapper, 200, 200);
7835 processSync(mapper);
7836 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7837 ASSERT_EQ(std::vector<TouchVideoFrame>(), motionArgs.videoFrames);
7838}
7839
Prabir Pradhanc14266f2021-05-12 15:56:24 -07007840TEST_F(MultiTouchInputMapperTest, VideoFrames_AreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007841 prepareAxes(POSITION);
7842 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007843 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007844 // Unrotated video frame
7845 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
7846 NotifyMotionArgs motionArgs;
7847
7848 // Test all 4 orientations
7849 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
Prabir Pradhanc14266f2021-05-12 15:56:24 -07007850 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
7851 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
7852 clearViewports();
7853 prepareDisplay(orientation);
7854 std::vector<TouchVideoFrame> frames{frame};
7855 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
7856 processPosition(mapper, 100, 200);
7857 processSync(mapper);
7858 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7859 ASSERT_EQ(frames, motionArgs.videoFrames);
7860 }
7861}
7862
7863TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_AreRotated) {
7864 prepareAxes(POSITION);
7865 addConfigurationProperty("touch.deviceType", "touchScreen");
7866 // Since InputReader works in the un-rotated coordinate space, only devices that are not
7867 // orientation-aware are affected by display rotation.
7868 addConfigurationProperty("touch.orientationAware", "0");
7869 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7870 // Unrotated video frame
7871 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
7872 NotifyMotionArgs motionArgs;
7873
7874 // Test all 4 orientations
7875 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007876 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
7877 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
7878 clearViewports();
7879 prepareDisplay(orientation);
7880 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007881 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007882 processPosition(mapper, 100, 200);
7883 processSync(mapper);
7884 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07007885 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
7886 // compared to the display. This is so that when the window transform (which contains the
7887 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
7888 // window's coordinate space.
7889 frames[0].rotate(getInverseRotation(orientation));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007890 ASSERT_EQ(frames, motionArgs.videoFrames);
7891 }
7892}
7893
Prabir Pradhanc14266f2021-05-12 15:56:24 -07007894TEST_F(MultiTouchInputMapperTest, VideoFrames_MultipleFramesAreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007895 prepareAxes(POSITION);
7896 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007897 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007898 // Unrotated video frames. There's no rule that they must all have the same dimensions,
7899 // so mix these.
7900 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
7901 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
7902 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
7903 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
7904 NotifyMotionArgs motionArgs;
7905
7906 prepareDisplay(DISPLAY_ORIENTATION_90);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007907 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007908 processPosition(mapper, 100, 200);
7909 processSync(mapper);
7910 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07007911 ASSERT_EQ(frames, motionArgs.videoFrames);
7912}
7913
7914TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_MultipleFramesAreRotated) {
7915 prepareAxes(POSITION);
7916 addConfigurationProperty("touch.deviceType", "touchScreen");
7917 // Since InputReader works in the un-rotated coordinate space, only devices that are not
7918 // orientation-aware are affected by display rotation.
7919 addConfigurationProperty("touch.orientationAware", "0");
7920 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7921 // Unrotated video frames. There's no rule that they must all have the same dimensions,
7922 // so mix these.
7923 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
7924 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
7925 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
7926 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
7927 NotifyMotionArgs motionArgs;
7928
7929 prepareDisplay(DISPLAY_ORIENTATION_90);
7930 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
7931 processPosition(mapper, 100, 200);
7932 processSync(mapper);
7933 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7934 std::for_each(frames.begin(), frames.end(), [](TouchVideoFrame& frame) {
7935 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
7936 // compared to the display. This is so that when the window transform (which contains the
7937 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
7938 // window's coordinate space.
7939 frame.rotate(getInverseRotation(DISPLAY_ORIENTATION_90));
7940 });
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007941 ASSERT_EQ(frames, motionArgs.videoFrames);
7942}
7943
Arthur Hung9da14732019-09-02 16:16:58 +08007944/**
7945 * If we had defined port associations, but the viewport is not ready, the touch device would be
7946 * expected to be disabled, and it should be enabled after the viewport has found.
7947 */
7948TEST_F(MultiTouchInputMapperTest, Configure_EnabledForAssociatedDisplay) {
Arthur Hung9da14732019-09-02 16:16:58 +08007949 constexpr uint8_t hdmi2 = 1;
7950 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007951 constexpr ViewportType type = ViewportType::EXTERNAL;
Arthur Hung9da14732019-09-02 16:16:58 +08007952
7953 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi2);
7954
7955 addConfigurationProperty("touch.deviceType", "touchScreen");
7956 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007957 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung9da14732019-09-02 16:16:58 +08007958
7959 ASSERT_EQ(mDevice->isEnabled(), false);
7960
7961 // Add display on hdmi2, the device should be enabled and can receive touch event.
7962 prepareSecondaryDisplay(type, hdmi2);
7963 ASSERT_EQ(mDevice->isEnabled(), true);
7964
7965 // Send a touch event.
7966 processPosition(mapper, 100, 100);
7967 processSync(mapper);
7968
7969 NotifyMotionArgs args;
7970 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7971 ASSERT_EQ(SECONDARY_DISPLAY_ID, args.displayId);
7972}
7973
Arthur Hung421eb1c2020-01-16 00:09:42 +08007974TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleSingleTouch) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08007975 addConfigurationProperty("touch.deviceType", "touchScreen");
7976 prepareDisplay(DISPLAY_ORIENTATION_0);
7977 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007978 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08007979
7980 NotifyMotionArgs motionArgs;
7981
7982 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
7983 // finger down
7984 processId(mapper, 1);
7985 processPosition(mapper, x1, y1);
7986 processSync(mapper);
7987 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7988 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7989 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7990
7991 // finger move
7992 processId(mapper, 1);
7993 processPosition(mapper, x2, y2);
7994 processSync(mapper);
7995 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7996 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7997 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7998
7999 // finger up.
8000 processId(mapper, -1);
8001 processSync(mapper);
8002 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8003 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8004 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8005
8006 // new finger down
8007 processId(mapper, 1);
8008 processPosition(mapper, x3, y3);
8009 processSync(mapper);
8010 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8011 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8012 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8013}
8014
8015/**
arthurhungcc7f9802020-04-30 17:55:40 +08008016 * Test single touch should be canceled when received the MT_TOOL_PALM event, and the following
8017 * MOVE and UP events should be ignored.
Arthur Hung421eb1c2020-01-16 00:09:42 +08008018 */
arthurhungcc7f9802020-04-30 17:55:40 +08008019TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_SinglePointer) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08008020 addConfigurationProperty("touch.deviceType", "touchScreen");
8021 prepareDisplay(DISPLAY_ORIENTATION_0);
8022 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008023 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08008024
8025 NotifyMotionArgs motionArgs;
8026
8027 // default tool type is finger
8028 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
arthurhungcc7f9802020-04-30 17:55:40 +08008029 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008030 processPosition(mapper, x1, y1);
8031 processSync(mapper);
8032 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8033 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8034 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8035
8036 // Tool changed to MT_TOOL_PALM expect sending the cancel event.
8037 processToolType(mapper, MT_TOOL_PALM);
8038 processSync(mapper);
8039 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8040 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
8041
8042 // Ignore the following MOVE and UP events if had detect a palm event.
arthurhungcc7f9802020-04-30 17:55:40 +08008043 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008044 processPosition(mapper, x2, y2);
8045 processSync(mapper);
8046 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8047
8048 // finger up.
arthurhungcc7f9802020-04-30 17:55:40 +08008049 processId(mapper, INVALID_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008050 processSync(mapper);
8051 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8052
8053 // new finger down
arthurhungcc7f9802020-04-30 17:55:40 +08008054 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008055 processToolType(mapper, MT_TOOL_FINGER);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008056 processPosition(mapper, x3, y3);
8057 processSync(mapper);
8058 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8059 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8060 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8061}
8062
arthurhungbf89a482020-04-17 17:37:55 +08008063/**
arthurhungcc7f9802020-04-30 17:55:40 +08008064 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
8065 * and the rest active fingers could still be allowed to receive the events
arthurhungbf89a482020-04-17 17:37:55 +08008066 */
arthurhungcc7f9802020-04-30 17:55:40 +08008067TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_TwoPointers) {
arthurhungbf89a482020-04-17 17:37:55 +08008068 addConfigurationProperty("touch.deviceType", "touchScreen");
8069 prepareDisplay(DISPLAY_ORIENTATION_0);
8070 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
8071 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8072
8073 NotifyMotionArgs motionArgs;
8074
8075 // default tool type is finger
arthurhungcc7f9802020-04-30 17:55:40 +08008076 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
8077 processId(mapper, FIRST_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08008078 processPosition(mapper, x1, y1);
8079 processSync(mapper);
8080 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8081 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8082 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8083
8084 // Second finger down.
arthurhungcc7f9802020-04-30 17:55:40 +08008085 processSlot(mapper, SECOND_SLOT);
8086 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08008087 processPosition(mapper, x2, y2);
arthurhungcc7f9802020-04-30 17:55:40 +08008088 processSync(mapper);
8089 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8090 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
8091 motionArgs.action);
8092 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8093
8094 // If the tool type of the first finger changes to MT_TOOL_PALM,
8095 // we expect to receive ACTION_POINTER_UP with cancel flag.
8096 processSlot(mapper, FIRST_SLOT);
8097 processId(mapper, FIRST_TRACKING_ID);
8098 processToolType(mapper, MT_TOOL_PALM);
8099 processSync(mapper);
8100 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8101 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
8102 motionArgs.action);
8103 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
8104
8105 // The following MOVE events of second finger should be processed.
8106 processSlot(mapper, SECOND_SLOT);
8107 processId(mapper, SECOND_TRACKING_ID);
8108 processPosition(mapper, x2 + 1, y2 + 1);
8109 processSync(mapper);
8110 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8111 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8112 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8113
8114 // First finger up. It used to be in palm mode, and we already generated ACTION_POINTER_UP for
8115 // it. Second finger receive move.
8116 processSlot(mapper, FIRST_SLOT);
8117 processId(mapper, INVALID_TRACKING_ID);
8118 processSync(mapper);
8119 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8120 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8121 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8122
8123 // Second finger keeps moving.
8124 processSlot(mapper, SECOND_SLOT);
8125 processId(mapper, SECOND_TRACKING_ID);
8126 processPosition(mapper, x2 + 2, y2 + 2);
8127 processSync(mapper);
8128 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8129 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8130 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8131
8132 // Second finger up.
8133 processId(mapper, INVALID_TRACKING_ID);
8134 processSync(mapper);
8135 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8136 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8137 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
8138}
8139
8140/**
8141 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event, if only 1 finger
8142 * is active, it should send CANCEL after receiving the MT_TOOL_PALM event.
8143 */
8144TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_ShouldCancelWhenAllTouchIsPalm) {
8145 addConfigurationProperty("touch.deviceType", "touchScreen");
8146 prepareDisplay(DISPLAY_ORIENTATION_0);
8147 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
8148 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8149
8150 NotifyMotionArgs motionArgs;
8151
8152 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
8153 // First finger down.
8154 processId(mapper, FIRST_TRACKING_ID);
8155 processPosition(mapper, x1, y1);
8156 processSync(mapper);
8157 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8158 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8159 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8160
8161 // Second finger down.
8162 processSlot(mapper, SECOND_SLOT);
8163 processId(mapper, SECOND_TRACKING_ID);
8164 processPosition(mapper, x2, y2);
arthurhungbf89a482020-04-17 17:37:55 +08008165 processSync(mapper);
8166 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8167 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
8168 motionArgs.action);
8169 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8170
arthurhungcc7f9802020-04-30 17:55:40 +08008171 // If the tool type of the first finger changes to MT_TOOL_PALM,
8172 // we expect to receive ACTION_POINTER_UP with cancel flag.
8173 processSlot(mapper, FIRST_SLOT);
8174 processId(mapper, FIRST_TRACKING_ID);
8175 processToolType(mapper, MT_TOOL_PALM);
8176 processSync(mapper);
8177 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8178 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
8179 motionArgs.action);
8180 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
8181
8182 // Second finger keeps moving.
8183 processSlot(mapper, SECOND_SLOT);
8184 processId(mapper, SECOND_TRACKING_ID);
8185 processPosition(mapper, x2 + 1, y2 + 1);
8186 processSync(mapper);
8187 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8188 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8189
8190 // second finger becomes palm, receive cancel due to only 1 finger is active.
8191 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08008192 processToolType(mapper, MT_TOOL_PALM);
8193 processSync(mapper);
8194 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8195 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
8196
arthurhungcc7f9802020-04-30 17:55:40 +08008197 // third finger down.
8198 processSlot(mapper, THIRD_SLOT);
8199 processId(mapper, THIRD_TRACKING_ID);
8200 processToolType(mapper, MT_TOOL_FINGER);
arthurhungbf89a482020-04-17 17:37:55 +08008201 processPosition(mapper, x3, y3);
8202 processSync(mapper);
arthurhungbf89a482020-04-17 17:37:55 +08008203 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8204 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8205 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08008206 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8207
8208 // third finger move
8209 processId(mapper, THIRD_TRACKING_ID);
8210 processPosition(mapper, x3 + 1, y3 + 1);
8211 processSync(mapper);
8212 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8213 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8214
8215 // first finger up, third finger receive move.
8216 processSlot(mapper, FIRST_SLOT);
8217 processId(mapper, INVALID_TRACKING_ID);
8218 processSync(mapper);
8219 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8220 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8221 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8222
8223 // second finger up, third finger receive move.
8224 processSlot(mapper, SECOND_SLOT);
8225 processId(mapper, INVALID_TRACKING_ID);
8226 processSync(mapper);
8227 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8228 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8229 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8230
8231 // third finger up.
8232 processSlot(mapper, THIRD_SLOT);
8233 processId(mapper, INVALID_TRACKING_ID);
8234 processSync(mapper);
8235 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8236 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8237 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
8238}
8239
8240/**
8241 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
8242 * and the active finger could still be allowed to receive the events
8243 */
8244TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_KeepFirstPointer) {
8245 addConfigurationProperty("touch.deviceType", "touchScreen");
8246 prepareDisplay(DISPLAY_ORIENTATION_0);
8247 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
8248 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8249
8250 NotifyMotionArgs motionArgs;
8251
8252 // default tool type is finger
8253 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
8254 processId(mapper, FIRST_TRACKING_ID);
8255 processPosition(mapper, x1, y1);
8256 processSync(mapper);
8257 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8258 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8259 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8260
8261 // Second finger down.
8262 processSlot(mapper, SECOND_SLOT);
8263 processId(mapper, SECOND_TRACKING_ID);
8264 processPosition(mapper, x2, y2);
8265 processSync(mapper);
8266 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8267 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
8268 motionArgs.action);
8269 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8270
8271 // If the tool type of the second finger changes to MT_TOOL_PALM,
8272 // we expect to receive ACTION_POINTER_UP with cancel flag.
8273 processId(mapper, SECOND_TRACKING_ID);
8274 processToolType(mapper, MT_TOOL_PALM);
8275 processSync(mapper);
8276 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8277 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
8278 motionArgs.action);
8279 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
8280
8281 // The following MOVE event should be processed.
8282 processSlot(mapper, FIRST_SLOT);
8283 processId(mapper, FIRST_TRACKING_ID);
8284 processPosition(mapper, x1 + 1, y1 + 1);
8285 processSync(mapper);
8286 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8287 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8288 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8289
8290 // second finger up.
8291 processSlot(mapper, SECOND_SLOT);
8292 processId(mapper, INVALID_TRACKING_ID);
8293 processSync(mapper);
8294 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8295 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8296
8297 // first finger keep moving
8298 processSlot(mapper, FIRST_SLOT);
8299 processId(mapper, FIRST_TRACKING_ID);
8300 processPosition(mapper, x1 + 2, y1 + 2);
8301 processSync(mapper);
8302 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8303 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8304
8305 // first finger up.
8306 processId(mapper, INVALID_TRACKING_ID);
8307 processSync(mapper);
8308 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8309 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8310 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
arthurhungbf89a482020-04-17 17:37:55 +08008311}
8312
Arthur Hung9ad18942021-06-19 02:04:46 +00008313/**
8314 * Test multi-touch should sent ACTION_POINTER_UP/ACTION_UP when received the INVALID_TRACKING_ID,
8315 * to prevent the driver side may send unexpected data after set tracking id as INVALID_TRACKING_ID
8316 * cause slot be valid again.
8317 */
8318TEST_F(MultiTouchInputMapperTest, Process_MultiTouch_WithInvalidTrackingId) {
8319 addConfigurationProperty("touch.deviceType", "touchScreen");
8320 prepareDisplay(DISPLAY_ORIENTATION_0);
8321 prepareAxes(POSITION | ID | SLOT | PRESSURE);
8322 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8323
8324 NotifyMotionArgs motionArgs;
8325
8326 constexpr int32_t x1 = 100, y1 = 200, x2 = 0, y2 = 0;
8327 // First finger down.
8328 processId(mapper, FIRST_TRACKING_ID);
8329 processPosition(mapper, x1, y1);
8330 processPressure(mapper, RAW_PRESSURE_MAX);
8331 processSync(mapper);
8332 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8333 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8334 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8335
8336 // First finger move.
8337 processId(mapper, FIRST_TRACKING_ID);
8338 processPosition(mapper, x1 + 1, y1 + 1);
8339 processPressure(mapper, RAW_PRESSURE_MAX);
8340 processSync(mapper);
8341 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8342 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8343 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8344
8345 // Second finger down.
8346 processSlot(mapper, SECOND_SLOT);
8347 processId(mapper, SECOND_TRACKING_ID);
8348 processPosition(mapper, x2, y2);
8349 processPressure(mapper, RAW_PRESSURE_MAX);
8350 processSync(mapper);
8351 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8352 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
8353 motionArgs.action);
8354 ASSERT_EQ(uint32_t(2), motionArgs.pointerCount);
8355
8356 // second finger up with some unexpected data.
8357 processSlot(mapper, SECOND_SLOT);
8358 processId(mapper, INVALID_TRACKING_ID);
8359 processPosition(mapper, x2, y2);
8360 processSync(mapper);
8361 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8362 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
8363 motionArgs.action);
8364 ASSERT_EQ(uint32_t(2), motionArgs.pointerCount);
8365
8366 // first finger up with some unexpected data.
8367 processSlot(mapper, FIRST_SLOT);
8368 processId(mapper, INVALID_TRACKING_ID);
8369 processPosition(mapper, x2, y2);
8370 processPressure(mapper, RAW_PRESSURE_MAX);
8371 processSync(mapper);
8372 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8373 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8374 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8375}
8376
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008377// --- MultiTouchInputMapperTest_ExternalDevice ---
8378
8379class MultiTouchInputMapperTest_ExternalDevice : public MultiTouchInputMapperTest {
8380protected:
Chris Yea52ade12020-08-27 16:49:20 -07008381 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008382};
8383
8384/**
8385 * Expect fallback to internal viewport if device is external and external viewport is not present.
8386 */
8387TEST_F(MultiTouchInputMapperTest_ExternalDevice, Viewports_Fallback) {
8388 prepareAxes(POSITION);
8389 addConfigurationProperty("touch.deviceType", "touchScreen");
8390 prepareDisplay(DISPLAY_ORIENTATION_0);
8391 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8392
8393 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
8394
8395 NotifyMotionArgs motionArgs;
8396
8397 // Expect the event to be sent to the internal viewport,
8398 // because an external viewport is not present.
8399 processPosition(mapper, 100, 100);
8400 processSync(mapper);
8401 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8402 ASSERT_EQ(ADISPLAY_ID_DEFAULT, motionArgs.displayId);
8403
8404 // Expect the event to be sent to the external viewport if it is present.
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008405 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008406 processPosition(mapper, 100, 100);
8407 processSync(mapper);
8408 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8409 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
8410}
Arthur Hung4197f6b2020-03-16 15:39:59 +08008411
8412/**
8413 * Test touch should not work if outside of surface.
8414 */
8415class MultiTouchInputMapperTest_SurfaceRange : public MultiTouchInputMapperTest {
8416protected:
8417 void halfDisplayToCenterHorizontal(int32_t orientation) {
8418 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008419 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Arthur Hung4197f6b2020-03-16 15:39:59 +08008420
8421 // Half display to (width/4, 0, width * 3/4, height) to make display has offset.
8422 internalViewport->orientation = orientation;
8423 if (orientation == DISPLAY_ORIENTATION_90 || orientation == DISPLAY_ORIENTATION_270) {
8424 internalViewport->logicalLeft = 0;
8425 internalViewport->logicalTop = 0;
8426 internalViewport->logicalRight = DISPLAY_HEIGHT;
8427 internalViewport->logicalBottom = DISPLAY_WIDTH / 2;
8428
8429 internalViewport->physicalLeft = 0;
8430 internalViewport->physicalTop = DISPLAY_WIDTH / 4;
8431 internalViewport->physicalRight = DISPLAY_HEIGHT;
8432 internalViewport->physicalBottom = DISPLAY_WIDTH * 3 / 4;
8433
8434 internalViewport->deviceWidth = DISPLAY_HEIGHT;
8435 internalViewport->deviceHeight = DISPLAY_WIDTH;
8436 } else {
8437 internalViewport->logicalLeft = 0;
8438 internalViewport->logicalTop = 0;
8439 internalViewport->logicalRight = DISPLAY_WIDTH / 2;
8440 internalViewport->logicalBottom = DISPLAY_HEIGHT;
8441
8442 internalViewport->physicalLeft = DISPLAY_WIDTH / 4;
8443 internalViewport->physicalTop = 0;
8444 internalViewport->physicalRight = DISPLAY_WIDTH * 3 / 4;
8445 internalViewport->physicalBottom = DISPLAY_HEIGHT;
8446
8447 internalViewport->deviceWidth = DISPLAY_WIDTH;
8448 internalViewport->deviceHeight = DISPLAY_HEIGHT;
8449 }
8450
8451 mFakePolicy->updateViewport(internalViewport.value());
8452 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8453 }
8454
arthurhung5d547942020-12-14 17:04:45 +08008455 void processPositionAndVerify(MultiTouchInputMapper& mapper, int32_t xOutside, int32_t yOutside,
8456 int32_t xInside, int32_t yInside, int32_t xExpected,
Arthur Hung4197f6b2020-03-16 15:39:59 +08008457 int32_t yExpected) {
8458 // touch on outside area should not work.
8459 processPosition(mapper, toRawX(xOutside), toRawY(yOutside));
8460 processSync(mapper);
8461 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8462
8463 // touch on inside area should receive the event.
8464 NotifyMotionArgs args;
8465 processPosition(mapper, toRawX(xInside), toRawY(yInside));
8466 processSync(mapper);
8467 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8468 ASSERT_NEAR(xExpected, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
8469 ASSERT_NEAR(yExpected, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
8470
8471 // Reset.
8472 mapper.reset(ARBITRARY_TIME);
8473 }
8474};
8475
arthurhung5d547942020-12-14 17:04:45 +08008476TEST_F(MultiTouchInputMapperTest_SurfaceRange, Viewports_SurfaceRange) {
Arthur Hung4197f6b2020-03-16 15:39:59 +08008477 addConfigurationProperty("touch.deviceType", "touchScreen");
8478 prepareDisplay(DISPLAY_ORIENTATION_0);
8479 prepareAxes(POSITION);
8480 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8481
8482 // Touch on center of normal display should work.
8483 const int32_t x = DISPLAY_WIDTH / 4;
8484 const int32_t y = DISPLAY_HEIGHT / 2;
8485 processPosition(mapper, toRawX(x), toRawY(y));
8486 processSync(mapper);
8487 NotifyMotionArgs args;
8488 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8489 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], x, y, 1.0f, 0.0f, 0.0f, 0.0f,
8490 0.0f, 0.0f, 0.0f, 0.0f));
8491 // Reset.
8492 mapper.reset(ARBITRARY_TIME);
8493
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008494 // Let physical display be different to device, and make surface and physical could be 1:1 in
8495 // all four orientations.
8496 for (int orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90, DISPLAY_ORIENTATION_180,
8497 DISPLAY_ORIENTATION_270}) {
8498 halfDisplayToCenterHorizontal(orientation);
Arthur Hung4197f6b2020-03-16 15:39:59 +08008499
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008500 const int32_t xExpected = (x + 1) - (DISPLAY_WIDTH / 4);
8501 const int32_t yExpected = y;
8502 processPositionAndVerify(mapper, x - 1, y, x + 1, y, xExpected, yExpected);
8503 }
Arthur Hung4197f6b2020-03-16 15:39:59 +08008504}
8505
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008506TEST_F(MultiTouchInputMapperTest_SurfaceRange, Viewports_SurfaceRange_90_NotOrientationAware) {
Arthur Hung4197f6b2020-03-16 15:39:59 +08008507 addConfigurationProperty("touch.deviceType", "touchScreen");
8508 prepareDisplay(DISPLAY_ORIENTATION_0);
8509 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008510 // Since InputReader works in the un-rotated coordinate space, only devices that are not
8511 // orientation-aware are affected by display rotation.
8512 addConfigurationProperty("touch.orientationAware", "0");
Arthur Hung4197f6b2020-03-16 15:39:59 +08008513 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8514
8515 // Half display to (width/4, 0, width * 3/4, height) and rotate 90-degrees.
8516 halfDisplayToCenterHorizontal(DISPLAY_ORIENTATION_90);
8517
8518 const int32_t x = DISPLAY_WIDTH / 4;
8519 const int32_t y = DISPLAY_HEIGHT / 2;
8520
Arthur Hung4197f6b2020-03-16 15:39:59 +08008521 // expect x/y = swap x/y then reverse x.
8522 constexpr int32_t xExpected = DISPLAY_HEIGHT - y;
8523 constexpr int32_t yExpected = (x + 1) - DISPLAY_WIDTH / 4;
8524 processPositionAndVerify(mapper, x - 1, y, x + 1, y, xExpected, yExpected);
8525}
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08008526
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008527TEST_F(MultiTouchInputMapperTest_SurfaceRange, Viewports_SurfaceRange_270_NotOrientationAware) {
arthurhunga36b28e2020-12-29 20:28:15 +08008528 addConfigurationProperty("touch.deviceType", "touchScreen");
8529 prepareDisplay(DISPLAY_ORIENTATION_0);
8530 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008531 // Since InputReader works in the un-rotated coordinate space, only devices that are not
8532 // orientation-aware are affected by display rotation.
8533 addConfigurationProperty("touch.orientationAware", "0");
8534 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8535
8536 // Half display to (width/4, 0, width * 3/4, height) and rotate 270-degrees.
8537 halfDisplayToCenterHorizontal(DISPLAY_ORIENTATION_270);
8538
8539 const int32_t x = DISPLAY_WIDTH / 4;
8540 const int32_t y = DISPLAY_HEIGHT / 2;
8541
8542 // expect x/y = swap x/y then reverse y.
8543 const int32_t xExpected = y;
8544 const int32_t yExpected = (DISPLAY_WIDTH * 3 / 4) - (x + 1);
8545 processPositionAndVerify(mapper, x - 1, y, x + 1, y, xExpected, yExpected);
8546}
8547
8548TEST_F(MultiTouchInputMapperTest_SurfaceRange, Viewports_SurfaceRange_Corner_NotOrientationAware) {
8549 addConfigurationProperty("touch.deviceType", "touchScreen");
8550 prepareDisplay(DISPLAY_ORIENTATION_0);
8551 prepareAxes(POSITION);
8552 // Since InputReader works in the un-rotated coordinate space, only devices that are not
8553 // orientation-aware are affected by display rotation.
8554 addConfigurationProperty("touch.orientationAware", "0");
arthurhunga36b28e2020-12-29 20:28:15 +08008555 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8556
8557 const int32_t x = 0;
8558 const int32_t y = 0;
8559
8560 const int32_t xExpected = x;
8561 const int32_t yExpected = y;
8562 processPositionAndVerify(mapper, x - 1, y, x, y, xExpected, yExpected);
8563
8564 clearViewports();
8565 prepareDisplay(DISPLAY_ORIENTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008566 // expect x/y = swap x/y then reverse x.
8567 const int32_t xExpected90 = DISPLAY_HEIGHT - 1;
8568 const int32_t yExpected90 = x;
arthurhunga36b28e2020-12-29 20:28:15 +08008569 processPositionAndVerify(mapper, x - 1, y, x, y, xExpected90, yExpected90);
8570
8571 clearViewports();
8572 prepareDisplay(DISPLAY_ORIENTATION_270);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008573 // expect x/y = swap x/y then reverse y.
8574 const int32_t xExpected270 = y;
8575 const int32_t yExpected270 = DISPLAY_WIDTH - 1;
arthurhunga36b28e2020-12-29 20:28:15 +08008576 processPositionAndVerify(mapper, x - 1, y, x, y, xExpected270, yExpected270);
8577}
8578
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08008579TEST_F(MultiTouchInputMapperTest, Process_TouchpadCapture) {
8580 // we need a pointer controller for mouse mode of touchpad (start pointer at 0,0)
8581 std::shared_ptr<FakePointerController> fakePointerController =
8582 std::make_shared<FakePointerController>();
8583 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
8584 fakePointerController->setPosition(0, 0);
8585 fakePointerController->setButtonState(0);
8586
8587 // prepare device and capture
8588 prepareDisplay(DISPLAY_ORIENTATION_0);
8589 prepareAxes(POSITION | ID | SLOT);
8590 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
8591 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
8592 mFakePolicy->setPointerCapture(true);
8593 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
8594 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8595
8596 // captured touchpad should be a touchpad source
8597 NotifyDeviceResetArgs resetArgs;
8598 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
8599 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
8600
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00008601 InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
Chris Yef74dc422020-09-02 22:41:50 -07008602
8603 const InputDeviceInfo::MotionRange* relRangeX =
8604 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_X, AINPUT_SOURCE_TOUCHPAD);
8605 ASSERT_NE(relRangeX, nullptr);
8606 ASSERT_EQ(relRangeX->min, -(RAW_X_MAX - RAW_X_MIN));
8607 ASSERT_EQ(relRangeX->max, RAW_X_MAX - RAW_X_MIN);
8608 const InputDeviceInfo::MotionRange* relRangeY =
8609 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_Y, AINPUT_SOURCE_TOUCHPAD);
8610 ASSERT_NE(relRangeY, nullptr);
8611 ASSERT_EQ(relRangeY->min, -(RAW_Y_MAX - RAW_Y_MIN));
8612 ASSERT_EQ(relRangeY->max, RAW_Y_MAX - RAW_Y_MIN);
8613
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08008614 // run captured pointer tests - note that this is unscaled, so input listener events should be
8615 // identical to what the hardware sends (accounting for any
8616 // calibration).
8617 // FINGER 0 DOWN
Chris Ye364fdb52020-08-05 15:07:56 -07008618 processSlot(mapper, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08008619 processId(mapper, 1);
8620 processPosition(mapper, 100 + RAW_X_MIN, 100 + RAW_Y_MIN);
8621 processKey(mapper, BTN_TOUCH, 1);
8622 processSync(mapper);
8623
8624 // expect coord[0] to contain initial location of touch 0
8625 NotifyMotionArgs args;
8626 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8627 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
8628 ASSERT_EQ(1U, args.pointerCount);
8629 ASSERT_EQ(0, args.pointerProperties[0].id);
8630 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, args.source);
8631 ASSERT_NO_FATAL_FAILURE(
8632 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
8633
8634 // FINGER 1 DOWN
8635 processSlot(mapper, 1);
8636 processId(mapper, 2);
8637 processPosition(mapper, 560 + RAW_X_MIN, 154 + RAW_Y_MIN);
8638 processSync(mapper);
8639
8640 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
8641 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Chris Ye364fdb52020-08-05 15:07:56 -07008642 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
8643 args.action);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08008644 ASSERT_EQ(2U, args.pointerCount);
8645 ASSERT_EQ(0, args.pointerProperties[0].id);
8646 ASSERT_EQ(1, args.pointerProperties[1].id);
8647 ASSERT_NO_FATAL_FAILURE(
8648 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
8649 ASSERT_NO_FATAL_FAILURE(
8650 assertPointerCoords(args.pointerCoords[1], 560, 154, 1, 0, 0, 0, 0, 0, 0, 0));
8651
8652 // FINGER 1 MOVE
8653 processPosition(mapper, 540 + RAW_X_MIN, 690 + RAW_Y_MIN);
8654 processSync(mapper);
8655
8656 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
8657 // from move
8658 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8659 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
8660 ASSERT_NO_FATAL_FAILURE(
8661 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
8662 ASSERT_NO_FATAL_FAILURE(
8663 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
8664
8665 // FINGER 0 MOVE
8666 processSlot(mapper, 0);
8667 processPosition(mapper, 50 + RAW_X_MIN, 800 + RAW_Y_MIN);
8668 processSync(mapper);
8669
8670 // expect coord[0] to contain new touch 0 location, coord[1] to contain previous location
8671 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8672 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
8673 ASSERT_NO_FATAL_FAILURE(
8674 assertPointerCoords(args.pointerCoords[0], 50, 800, 1, 0, 0, 0, 0, 0, 0, 0));
8675 ASSERT_NO_FATAL_FAILURE(
8676 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
8677
8678 // BUTTON DOWN
8679 processKey(mapper, BTN_LEFT, 1);
8680 processSync(mapper);
8681
8682 // touchinputmapper design sends a move before button press
8683 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8684 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
8685 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8686 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
8687
8688 // BUTTON UP
8689 processKey(mapper, BTN_LEFT, 0);
8690 processSync(mapper);
8691
8692 // touchinputmapper design sends a move after button release
8693 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8694 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
8695 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8696 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
8697
8698 // FINGER 0 UP
8699 processId(mapper, -1);
8700 processSync(mapper);
8701 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8702 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | 0x0000, args.action);
8703
8704 // FINGER 1 MOVE
8705 processSlot(mapper, 1);
8706 processPosition(mapper, 320 + RAW_X_MIN, 900 + RAW_Y_MIN);
8707 processSync(mapper);
8708
8709 // expect coord[0] to contain new location of touch 1, and properties[0].id to contain 1
8710 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8711 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
8712 ASSERT_EQ(1U, args.pointerCount);
8713 ASSERT_EQ(1, args.pointerProperties[0].id);
8714 ASSERT_NO_FATAL_FAILURE(
8715 assertPointerCoords(args.pointerCoords[0], 320, 900, 1, 0, 0, 0, 0, 0, 0, 0));
8716
8717 // FINGER 1 UP
8718 processId(mapper, -1);
8719 processKey(mapper, BTN_TOUCH, 0);
8720 processSync(mapper);
8721 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8722 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
8723
8724 // non captured touchpad should be a mouse source
8725 mFakePolicy->setPointerCapture(false);
8726 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
8727 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
8728 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
8729}
8730
8731TEST_F(MultiTouchInputMapperTest, Process_UnCapturedTouchpadPointer) {
8732 std::shared_ptr<FakePointerController> fakePointerController =
8733 std::make_shared<FakePointerController>();
8734 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
8735 fakePointerController->setPosition(0, 0);
8736 fakePointerController->setButtonState(0);
8737
8738 // prepare device and capture
8739 prepareDisplay(DISPLAY_ORIENTATION_0);
8740 prepareAxes(POSITION | ID | SLOT);
8741 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
8742 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
8743 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
8744 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8745 // run uncaptured pointer tests - pushes out generic events
8746 // FINGER 0 DOWN
8747 processId(mapper, 3);
8748 processPosition(mapper, 100, 100);
8749 processKey(mapper, BTN_TOUCH, 1);
8750 processSync(mapper);
8751
8752 // start at (100,100), cursor should be at (0,0) * scale
8753 NotifyMotionArgs args;
8754 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8755 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
8756 ASSERT_NO_FATAL_FAILURE(
8757 assertPointerCoords(args.pointerCoords[0], 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
8758
8759 // FINGER 0 MOVE
8760 processPosition(mapper, 200, 200);
8761 processSync(mapper);
8762
8763 // compute scaling to help with touch position checking
8764 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
8765 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
8766 float scale =
8767 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
8768
8769 // translate from (100,100) -> (200,200), cursor should have changed to (100,100) * scale)
8770 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8771 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
8772 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 100 * scale, 100 * scale, 0,
8773 0, 0, 0, 0, 0, 0, 0));
8774}
8775
8776TEST_F(MultiTouchInputMapperTest, WhenCapturedAndNotCaptured_GetSources) {
8777 std::shared_ptr<FakePointerController> fakePointerController =
8778 std::make_shared<FakePointerController>();
8779
8780 prepareDisplay(DISPLAY_ORIENTATION_0);
8781 prepareAxes(POSITION | ID | SLOT);
8782 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
8783 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
8784 mFakePolicy->setPointerCapture(false);
8785 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8786
8787 // uncaptured touchpad should be a pointer device
8788 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
8789
8790 // captured touchpad should be a touchpad device
8791 mFakePolicy->setPointerCapture(true);
8792 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
8793 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
8794}
8795
Chris Ye1dd2e5c2021-04-04 23:12:41 -07008796// --- PeripheralControllerTest ---
Chris Yee2b1e5c2021-03-10 22:45:12 -08008797
Chris Ye1dd2e5c2021-04-04 23:12:41 -07008798class PeripheralControllerTest : public testing::Test {
Chris Yee2b1e5c2021-03-10 22:45:12 -08008799protected:
8800 static const char* DEVICE_NAME;
8801 static const char* DEVICE_LOCATION;
8802 static const int32_t DEVICE_ID;
8803 static const int32_t DEVICE_GENERATION;
8804 static const int32_t DEVICE_CONTROLLER_NUMBER;
8805 static const Flags<InputDeviceClass> DEVICE_CLASSES;
8806 static const int32_t EVENTHUB_ID;
8807
8808 std::shared_ptr<FakeEventHub> mFakeEventHub;
8809 sp<FakeInputReaderPolicy> mFakePolicy;
8810 sp<TestInputListener> mFakeListener;
8811 std::unique_ptr<InstrumentedInputReader> mReader;
8812 std::shared_ptr<InputDevice> mDevice;
8813
8814 virtual void SetUp(Flags<InputDeviceClass> classes) {
8815 mFakeEventHub = std::make_unique<FakeEventHub>();
8816 mFakePolicy = new FakeInputReaderPolicy();
8817 mFakeListener = new TestInputListener();
8818 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
8819 mFakeListener);
8820 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
8821 }
8822
8823 void SetUp() override { SetUp(DEVICE_CLASSES); }
8824
8825 void TearDown() override {
8826 mFakeListener.clear();
8827 mFakePolicy.clear();
8828 }
8829
8830 void configureDevice(uint32_t changes) {
8831 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
8832 mReader->requestRefreshConfiguration(changes);
8833 mReader->loopOnce();
8834 }
8835 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
8836 }
8837
8838 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
8839 const std::string& location, int32_t eventHubId,
8840 Flags<InputDeviceClass> classes) {
8841 InputDeviceIdentifier identifier;
8842 identifier.name = name;
8843 identifier.location = location;
8844 std::shared_ptr<InputDevice> device =
8845 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
8846 identifier);
8847 mReader->pushNextDevice(device);
8848 mFakeEventHub->addDevice(eventHubId, name, classes);
8849 mReader->loopOnce();
8850 return device;
8851 }
8852
8853 template <class T, typename... Args>
8854 T& addControllerAndConfigure(Args... args) {
8855 T& controller = mDevice->addController<T>(EVENTHUB_ID, args...);
8856
8857 return controller;
8858 }
8859};
8860
Chris Ye1dd2e5c2021-04-04 23:12:41 -07008861const char* PeripheralControllerTest::DEVICE_NAME = "device";
8862const char* PeripheralControllerTest::DEVICE_LOCATION = "BLUETOOTH";
8863const int32_t PeripheralControllerTest::DEVICE_ID = END_RESERVED_ID + 1000;
8864const int32_t PeripheralControllerTest::DEVICE_GENERATION = 2;
8865const int32_t PeripheralControllerTest::DEVICE_CONTROLLER_NUMBER = 0;
8866const Flags<InputDeviceClass> PeripheralControllerTest::DEVICE_CLASSES =
Chris Yee2b1e5c2021-03-10 22:45:12 -08008867 Flags<InputDeviceClass>(0); // not needed for current tests
Chris Ye1dd2e5c2021-04-04 23:12:41 -07008868const int32_t PeripheralControllerTest::EVENTHUB_ID = 1;
Chris Yee2b1e5c2021-03-10 22:45:12 -08008869
8870// --- BatteryControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -07008871class BatteryControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -08008872protected:
8873 void SetUp() override {
Chris Ye1dd2e5c2021-04-04 23:12:41 -07008874 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::BATTERY);
Chris Yee2b1e5c2021-03-10 22:45:12 -08008875 }
8876};
8877
8878TEST_F(BatteryControllerTest, GetBatteryCapacity) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -07008879 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08008880
8881 ASSERT_TRUE(controller.getBatteryCapacity(DEFAULT_BATTERY));
8882 ASSERT_EQ(controller.getBatteryCapacity(DEFAULT_BATTERY).value_or(-1), BATTERY_CAPACITY);
8883}
8884
8885TEST_F(BatteryControllerTest, GetBatteryStatus) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -07008886 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08008887
8888 ASSERT_TRUE(controller.getBatteryStatus(DEFAULT_BATTERY));
8889 ASSERT_EQ(controller.getBatteryStatus(DEFAULT_BATTERY).value_or(-1), BATTERY_STATUS);
8890}
8891
8892// --- LightControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -07008893class LightControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -08008894protected:
Chris Ye1dd2e5c2021-04-04 23:12:41 -07008895 void SetUp() override {
8896 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::LIGHT);
8897 }
Chris Yee2b1e5c2021-03-10 22:45:12 -08008898};
8899
Chris Ye85758332021-05-16 23:05:17 -07008900TEST_F(LightControllerTest, MonoLight) {
8901 RawLightInfo infoMono = {.id = 1,
8902 .name = "Mono",
8903 .maxBrightness = 255,
8904 .flags = InputLightClass::BRIGHTNESS,
8905 .path = ""};
8906 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
Chris Yee2b1e5c2021-03-10 22:45:12 -08008907
Chris Ye1dd2e5c2021-04-04 23:12:41 -07008908 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08008909 InputDeviceInfo info;
8910 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00008911 std::vector<InputDeviceLightInfo> lights = info.getLights();
8912 ASSERT_EQ(1U, lights.size());
8913 ASSERT_EQ(InputDeviceLightType::MONO, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -08008914
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00008915 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
8916 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
Chris Yee2b1e5c2021-03-10 22:45:12 -08008917}
8918
8919TEST_F(LightControllerTest, RGBLight) {
8920 RawLightInfo infoRed = {.id = 1,
8921 .name = "red",
8922 .maxBrightness = 255,
8923 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
8924 .path = ""};
8925 RawLightInfo infoGreen = {.id = 2,
8926 .name = "green",
8927 .maxBrightness = 255,
8928 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
8929 .path = ""};
8930 RawLightInfo infoBlue = {.id = 3,
8931 .name = "blue",
8932 .maxBrightness = 255,
8933 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
8934 .path = ""};
8935 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
8936 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
8937 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
8938
Chris Ye1dd2e5c2021-04-04 23:12:41 -07008939 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08008940 InputDeviceInfo info;
8941 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00008942 std::vector<InputDeviceLightInfo> lights = info.getLights();
8943 ASSERT_EQ(1U, lights.size());
8944 ASSERT_EQ(InputDeviceLightType::RGB, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -08008945
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00008946 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
8947 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -08008948}
8949
8950TEST_F(LightControllerTest, MultiColorRGBLight) {
8951 RawLightInfo infoColor = {.id = 1,
8952 .name = "red",
8953 .maxBrightness = 255,
8954 .flags = InputLightClass::BRIGHTNESS |
8955 InputLightClass::MULTI_INTENSITY |
8956 InputLightClass::MULTI_INDEX,
8957 .path = ""};
8958
8959 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
8960
Chris Ye1dd2e5c2021-04-04 23:12:41 -07008961 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08008962 InputDeviceInfo info;
8963 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00008964 std::vector<InputDeviceLightInfo> lights = info.getLights();
8965 ASSERT_EQ(1U, lights.size());
8966 ASSERT_EQ(InputDeviceLightType::MULTI_COLOR, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -08008967
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00008968 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
8969 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -08008970}
8971
8972TEST_F(LightControllerTest, PlayerIdLight) {
8973 RawLightInfo info1 = {.id = 1,
8974 .name = "player1",
8975 .maxBrightness = 255,
8976 .flags = InputLightClass::BRIGHTNESS,
8977 .path = ""};
8978 RawLightInfo info2 = {.id = 2,
8979 .name = "player2",
8980 .maxBrightness = 255,
8981 .flags = InputLightClass::BRIGHTNESS,
8982 .path = ""};
8983 RawLightInfo info3 = {.id = 3,
8984 .name = "player3",
8985 .maxBrightness = 255,
8986 .flags = InputLightClass::BRIGHTNESS,
8987 .path = ""};
8988 RawLightInfo info4 = {.id = 4,
8989 .name = "player4",
8990 .maxBrightness = 255,
8991 .flags = InputLightClass::BRIGHTNESS,
8992 .path = ""};
8993 mFakeEventHub->addRawLightInfo(info1.id, std::move(info1));
8994 mFakeEventHub->addRawLightInfo(info2.id, std::move(info2));
8995 mFakeEventHub->addRawLightInfo(info3.id, std::move(info3));
8996 mFakeEventHub->addRawLightInfo(info4.id, std::move(info4));
8997
Chris Ye1dd2e5c2021-04-04 23:12:41 -07008998 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08008999 InputDeviceInfo info;
9000 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009001 std::vector<InputDeviceLightInfo> lights = info.getLights();
9002 ASSERT_EQ(1U, lights.size());
9003 ASSERT_EQ(InputDeviceLightType::PLAYER_ID, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009004
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009005 ASSERT_FALSE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
9006 ASSERT_TRUE(controller.setLightPlayerId(lights[0].id, LIGHT_PLAYER_ID));
9007 ASSERT_EQ(controller.getLightPlayerId(lights[0].id).value_or(-1), LIGHT_PLAYER_ID);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009008}
9009
Michael Wrightd02c5b62014-02-10 15:10:22 -08009010} // namespace android