blob: 3280e246e1159ed23b09b2ec2704f43f4066914a [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
Arthur Hungcb40a002021-08-03 14:31:01 +0000873 bool hasKeyCode(int32_t deviceId, int32_t keyCode) const override {
874 Device* device = getDevice(deviceId);
875 if (!device) {
876 return false;
877 }
878 for (size_t i = 0; i < device->keysByScanCode.size(); i++) {
879 if (keyCode == device->keysByScanCode.valueAt(i).keyCode) {
880 return true;
881 }
882 }
883 for (size_t j = 0; j < device->keysByUsageCode.size(); j++) {
884 if (keyCode == device->keysByUsageCode.valueAt(j).keyCode) {
885 return true;
886 }
887 }
888 return false;
889 }
890
Chris Yea52ade12020-08-27 16:49:20 -0700891 bool hasLed(int32_t deviceId, int32_t led) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800892 Device* device = getDevice(deviceId);
893 return device && device->leds.indexOfKey(led) >= 0;
894 }
895
Chris Yea52ade12020-08-27 16:49:20 -0700896 void setLedState(int32_t deviceId, int32_t led, bool on) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800897 Device* device = getDevice(deviceId);
898 if (device) {
899 ssize_t index = device->leds.indexOfKey(led);
900 if (index >= 0) {
901 device->leds.replaceValueAt(led, on);
902 } else {
903 ADD_FAILURE()
904 << "Attempted to set the state of an LED that the EventHub declared "
905 "was not present. led=" << led;
906 }
907 }
908 }
909
Chris Yea52ade12020-08-27 16:49:20 -0700910 void getVirtualKeyDefinitions(
911 int32_t deviceId, std::vector<VirtualKeyDefinition>& outVirtualKeys) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800912 outVirtualKeys.clear();
913
914 Device* device = getDevice(deviceId);
915 if (device) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800916 outVirtualKeys = device->virtualKeys;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800917 }
918 }
919
Chris Yea52ade12020-08-27 16:49:20 -0700920 const std::shared_ptr<KeyCharacterMap> getKeyCharacterMap(int32_t) const override {
Yi Kong9b14ac62018-07-17 13:48:38 -0700921 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800922 }
923
Chris Yea52ade12020-08-27 16:49:20 -0700924 bool setKeyboardLayoutOverlay(int32_t, std::shared_ptr<KeyCharacterMap>) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800925 return false;
926 }
927
Chris Yea52ade12020-08-27 16:49:20 -0700928 void vibrate(int32_t, const VibrationElement&) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800929
Chris Yea52ade12020-08-27 16:49:20 -0700930 void cancelVibrate(int32_t) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800931
Chris Ye87143712020-11-10 05:05:58 +0000932 std::vector<int32_t> getVibratorIds(int32_t deviceId) override { return mVibrators; };
933
Chris Yee2b1e5c2021-03-10 22:45:12 -0800934 std::optional<int32_t> getBatteryCapacity(int32_t, int32_t) const override {
935 return BATTERY_CAPACITY;
936 }
Kim Low03ea0352020-11-06 12:45:07 -0800937
Chris Yee2b1e5c2021-03-10 22:45:12 -0800938 std::optional<int32_t> getBatteryStatus(int32_t, int32_t) const override {
939 return BATTERY_STATUS;
940 }
941
942 const std::vector<int32_t> getRawBatteryIds(int32_t deviceId) { return {}; }
943
944 std::optional<RawBatteryInfo> getRawBatteryInfo(int32_t deviceId, int32_t batteryId) {
945 return std::nullopt;
946 }
Kim Low03ea0352020-11-06 12:45:07 -0800947
Chris Ye3fdbfef2021-01-06 18:45:18 -0800948 const std::vector<int32_t> getRawLightIds(int32_t deviceId) override {
949 std::vector<int32_t> ids;
950 for (const auto& [rawId, info] : mRawLightInfos) {
951 ids.push_back(rawId);
952 }
953 return ids;
954 }
955
956 std::optional<RawLightInfo> getRawLightInfo(int32_t deviceId, int32_t lightId) override {
957 auto it = mRawLightInfos.find(lightId);
958 if (it == mRawLightInfos.end()) {
959 return std::nullopt;
960 }
961 return it->second;
962 }
963
964 void setLightBrightness(int32_t deviceId, int32_t lightId, int32_t brightness) override {
965 mLightBrightness.emplace(lightId, brightness);
966 }
967
968 void setLightIntensities(int32_t deviceId, int32_t lightId,
969 std::unordered_map<LightColor, int32_t> intensities) override {
970 mLightIntensities.emplace(lightId, intensities);
971 };
972
973 std::optional<int32_t> getLightBrightness(int32_t deviceId, int32_t lightId) override {
974 auto lightIt = mLightBrightness.find(lightId);
975 if (lightIt == mLightBrightness.end()) {
976 return std::nullopt;
977 }
978 return lightIt->second;
979 }
980
981 std::optional<std::unordered_map<LightColor, int32_t>> getLightIntensities(
982 int32_t deviceId, int32_t lightId) override {
983 auto lightIt = mLightIntensities.find(lightId);
984 if (lightIt == mLightIntensities.end()) {
985 return std::nullopt;
986 }
987 return lightIt->second;
988 };
989
Narayan Kamath39efe3e2014-10-17 10:37:08 +0100990 virtual bool isExternal(int32_t) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800991 return false;
992 }
993
Chris Yea52ade12020-08-27 16:49:20 -0700994 void dump(std::string&) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800995
Chris Yea52ade12020-08-27 16:49:20 -0700996 void monitor() override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800997
Chris Yea52ade12020-08-27 16:49:20 -0700998 void requestReopenDevices() override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800999
Chris Yea52ade12020-08-27 16:49:20 -07001000 void wake() override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001001};
1002
Michael Wrightd02c5b62014-02-10 15:10:22 -08001003// --- FakeInputMapper ---
1004
1005class FakeInputMapper : public InputMapper {
1006 uint32_t mSources;
1007 int32_t mKeyboardType;
1008 int32_t mMetaState;
1009 KeyedVector<int32_t, int32_t> mKeyCodeStates;
1010 KeyedVector<int32_t, int32_t> mScanCodeStates;
1011 KeyedVector<int32_t, int32_t> mSwitchStates;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001012 std::vector<int32_t> mSupportedKeyCodes;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001013
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001014 std::mutex mLock;
1015 std::condition_variable mStateChangedCondition;
1016 bool mConfigureWasCalled GUARDED_BY(mLock);
1017 bool mResetWasCalled GUARDED_BY(mLock);
1018 bool mProcessWasCalled GUARDED_BY(mLock);
1019 RawEvent mLastEvent GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001020
Arthur Hungc23540e2018-11-29 20:42:11 +08001021 std::optional<DisplayViewport> mViewport;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001022public:
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001023 FakeInputMapper(InputDeviceContext& deviceContext, uint32_t sources)
1024 : InputMapper(deviceContext),
1025 mSources(sources),
1026 mKeyboardType(AINPUT_KEYBOARD_TYPE_NONE),
Michael Wrightd02c5b62014-02-10 15:10:22 -08001027 mMetaState(0),
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001028 mConfigureWasCalled(false),
1029 mResetWasCalled(false),
1030 mProcessWasCalled(false) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001031
Chris Yea52ade12020-08-27 16:49:20 -07001032 virtual ~FakeInputMapper() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001033
1034 void setKeyboardType(int32_t keyboardType) {
1035 mKeyboardType = keyboardType;
1036 }
1037
1038 void setMetaState(int32_t metaState) {
1039 mMetaState = metaState;
1040 }
1041
1042 void assertConfigureWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001043 std::unique_lock<std::mutex> lock(mLock);
1044 base::ScopedLockAssertion assumeLocked(mLock);
1045 const bool configureCalled =
1046 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1047 return mConfigureWasCalled;
1048 });
1049 if (!configureCalled) {
1050 FAIL() << "Expected configure() to have been called.";
1051 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001052 mConfigureWasCalled = false;
1053 }
1054
1055 void assertResetWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001056 std::unique_lock<std::mutex> lock(mLock);
1057 base::ScopedLockAssertion assumeLocked(mLock);
1058 const bool resetCalled =
1059 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1060 return mResetWasCalled;
1061 });
1062 if (!resetCalled) {
1063 FAIL() << "Expected reset() to have been called.";
1064 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001065 mResetWasCalled = false;
1066 }
1067
Yi Kong9b14ac62018-07-17 13:48:38 -07001068 void assertProcessWasCalled(RawEvent* outLastEvent = nullptr) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001069 std::unique_lock<std::mutex> lock(mLock);
1070 base::ScopedLockAssertion assumeLocked(mLock);
1071 const bool processCalled =
1072 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1073 return mProcessWasCalled;
1074 });
1075 if (!processCalled) {
1076 FAIL() << "Expected process() to have been called.";
1077 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001078 if (outLastEvent) {
1079 *outLastEvent = mLastEvent;
1080 }
1081 mProcessWasCalled = false;
1082 }
1083
1084 void setKeyCodeState(int32_t keyCode, int32_t state) {
1085 mKeyCodeStates.replaceValueFor(keyCode, state);
1086 }
1087
1088 void setScanCodeState(int32_t scanCode, int32_t state) {
1089 mScanCodeStates.replaceValueFor(scanCode, state);
1090 }
1091
1092 void setSwitchState(int32_t switchCode, int32_t state) {
1093 mSwitchStates.replaceValueFor(switchCode, state);
1094 }
1095
1096 void addSupportedKeyCode(int32_t keyCode) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001097 mSupportedKeyCodes.push_back(keyCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001098 }
1099
1100private:
Chris Yea52ade12020-08-27 16:49:20 -07001101 uint32_t getSources() override { return mSources; }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001102
Chris Yea52ade12020-08-27 16:49:20 -07001103 void populateDeviceInfo(InputDeviceInfo* deviceInfo) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001104 InputMapper::populateDeviceInfo(deviceInfo);
1105
1106 if (mKeyboardType != AINPUT_KEYBOARD_TYPE_NONE) {
1107 deviceInfo->setKeyboardType(mKeyboardType);
1108 }
1109 }
1110
Chris Yea52ade12020-08-27 16:49:20 -07001111 void configure(nsecs_t, const InputReaderConfiguration* config, uint32_t changes) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001112 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001113 mConfigureWasCalled = true;
Arthur Hungc23540e2018-11-29 20:42:11 +08001114
1115 // Find the associated viewport if exist.
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001116 const std::optional<uint8_t> displayPort = getDeviceContext().getAssociatedDisplayPort();
Arthur Hungc23540e2018-11-29 20:42:11 +08001117 if (displayPort && (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
1118 mViewport = config->getDisplayViewportByPort(*displayPort);
1119 }
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001120
1121 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001122 }
1123
Chris Yea52ade12020-08-27 16:49:20 -07001124 void reset(nsecs_t) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001125 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001126 mResetWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001127 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001128 }
1129
Chris Yea52ade12020-08-27 16:49:20 -07001130 void process(const RawEvent* rawEvent) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001131 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001132 mLastEvent = *rawEvent;
1133 mProcessWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001134 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001135 }
1136
Chris Yea52ade12020-08-27 16:49:20 -07001137 int32_t getKeyCodeState(uint32_t, int32_t keyCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001138 ssize_t index = mKeyCodeStates.indexOfKey(keyCode);
1139 return index >= 0 ? mKeyCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1140 }
1141
Chris Yea52ade12020-08-27 16:49:20 -07001142 int32_t getScanCodeState(uint32_t, int32_t scanCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001143 ssize_t index = mScanCodeStates.indexOfKey(scanCode);
1144 return index >= 0 ? mScanCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1145 }
1146
Chris Yea52ade12020-08-27 16:49:20 -07001147 int32_t getSwitchState(uint32_t, int32_t switchCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001148 ssize_t index = mSwitchStates.indexOfKey(switchCode);
1149 return index >= 0 ? mSwitchStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1150 }
1151
Chris Yea52ade12020-08-27 16:49:20 -07001152 // Return true if the device has non-empty key layout.
1153 bool markSupportedKeyCodes(uint32_t, size_t numCodes, const int32_t* keyCodes,
1154 uint8_t* outFlags) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001155 for (size_t i = 0; i < numCodes; i++) {
1156 for (size_t j = 0; j < mSupportedKeyCodes.size(); j++) {
1157 if (keyCodes[i] == mSupportedKeyCodes[j]) {
1158 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001159 }
1160 }
1161 }
Chris Yea52ade12020-08-27 16:49:20 -07001162 bool result = mSupportedKeyCodes.size() > 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001163 return result;
1164 }
1165
1166 virtual int32_t getMetaState() {
1167 return mMetaState;
1168 }
1169
1170 virtual void fadePointer() {
1171 }
Arthur Hungc23540e2018-11-29 20:42:11 +08001172
1173 virtual std::optional<int32_t> getAssociatedDisplay() {
1174 if (mViewport) {
1175 return std::make_optional(mViewport->displayId);
1176 }
1177 return std::nullopt;
1178 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001179};
1180
1181
1182// --- InstrumentedInputReader ---
1183
1184class InstrumentedInputReader : public InputReader {
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001185 std::queue<std::shared_ptr<InputDevice>> mNextDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001186
1187public:
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001188 InstrumentedInputReader(std::shared_ptr<EventHubInterface> eventHub,
1189 const sp<InputReaderPolicyInterface>& policy,
1190 const sp<InputListenerInterface>& listener)
arthurhungdcef2dc2020-08-11 14:47:50 +08001191 : InputReader(eventHub, policy, listener), mFakeContext(this) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001192
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001193 virtual ~InstrumentedInputReader() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001194
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001195 void pushNextDevice(std::shared_ptr<InputDevice> device) { mNextDevices.push(device); }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001196
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001197 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001198 const std::string& location = "") {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001199 InputDeviceIdentifier identifier;
1200 identifier.name = name;
Arthur Hungc23540e2018-11-29 20:42:11 +08001201 identifier.location = location;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001202 int32_t generation = deviceId + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08001203 return std::make_shared<InputDevice>(&mFakeContext, deviceId, generation, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001204 }
1205
Prabir Pradhan28efc192019-11-05 01:10:04 +00001206 // Make the protected loopOnce method accessible to tests.
1207 using InputReader::loopOnce;
1208
Michael Wrightd02c5b62014-02-10 15:10:22 -08001209protected:
Chris Ye1c2e0892020-11-30 21:41:44 -08001210 virtual std::shared_ptr<InputDevice> createDeviceLocked(int32_t eventHubId,
1211 const InputDeviceIdentifier& identifier)
1212 REQUIRES(mLock) {
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001213 if (!mNextDevices.empty()) {
1214 std::shared_ptr<InputDevice> device(std::move(mNextDevices.front()));
1215 mNextDevices.pop();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001216 return device;
1217 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001218 return InputReader::createDeviceLocked(eventHubId, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001219 }
1220
arthurhungdcef2dc2020-08-11 14:47:50 +08001221 // --- FakeInputReaderContext ---
1222 class FakeInputReaderContext : public ContextImpl {
1223 int32_t mGlobalMetaState;
1224 bool mUpdateGlobalMetaStateWasCalled;
1225 int32_t mGeneration;
1226
1227 public:
1228 FakeInputReaderContext(InputReader* reader)
1229 : ContextImpl(reader),
1230 mGlobalMetaState(0),
1231 mUpdateGlobalMetaStateWasCalled(false),
1232 mGeneration(1) {}
1233
1234 virtual ~FakeInputReaderContext() {}
1235
1236 void assertUpdateGlobalMetaStateWasCalled() {
1237 ASSERT_TRUE(mUpdateGlobalMetaStateWasCalled)
1238 << "Expected updateGlobalMetaState() to have been called.";
1239 mUpdateGlobalMetaStateWasCalled = false;
1240 }
1241
1242 void setGlobalMetaState(int32_t state) { mGlobalMetaState = state; }
1243
1244 uint32_t getGeneration() { return mGeneration; }
1245
1246 void updateGlobalMetaState() override {
1247 mUpdateGlobalMetaStateWasCalled = true;
1248 ContextImpl::updateGlobalMetaState();
1249 }
1250
1251 int32_t getGlobalMetaState() override {
1252 return mGlobalMetaState | ContextImpl::getGlobalMetaState();
1253 }
1254
1255 int32_t bumpGeneration() override {
1256 mGeneration = ContextImpl::bumpGeneration();
1257 return mGeneration;
1258 }
1259 } mFakeContext;
1260
Michael Wrightd02c5b62014-02-10 15:10:22 -08001261 friend class InputReaderTest;
arthurhungdcef2dc2020-08-11 14:47:50 +08001262
1263public:
1264 FakeInputReaderContext* getContext() { return &mFakeContext; }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001265};
1266
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001267// --- InputReaderPolicyTest ---
1268class InputReaderPolicyTest : public testing::Test {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001269protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001270 sp<FakeInputReaderPolicy> mFakePolicy;
1271
Chris Yea52ade12020-08-27 16:49:20 -07001272 void SetUp() override { mFakePolicy = new FakeInputReaderPolicy(); }
1273 void TearDown() override { mFakePolicy.clear(); }
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001274};
1275
1276/**
1277 * Check that empty set of viewports is an acceptable configuration.
1278 * Also try to get internal viewport two different ways - by type and by uniqueId.
1279 *
1280 * There will be confusion if two viewports with empty uniqueId and identical type are present.
1281 * Such configuration is not currently allowed.
1282 */
1283TEST_F(InputReaderPolicyTest, Viewports_GetCleared) {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001284 static const std::string uniqueId = "local:0";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001285
1286 // We didn't add any viewports yet, so there shouldn't be any.
1287 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001288 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001289 ASSERT_FALSE(internalViewport);
1290
1291 // Add an internal viewport, then clear it
1292 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001293 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId, NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001294 ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001295
1296 // Check matching by uniqueId
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001297 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001298 ASSERT_TRUE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001299 ASSERT_EQ(ViewportType::INTERNAL, internalViewport->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001300
1301 // Check matching by viewport type
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001302 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001303 ASSERT_TRUE(internalViewport);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001304 ASSERT_EQ(uniqueId, internalViewport->uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001305
1306 mFakePolicy->clearViewports();
1307 // Make sure nothing is found after clear
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001308 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001309 ASSERT_FALSE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001310 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001311 ASSERT_FALSE(internalViewport);
1312}
1313
1314TEST_F(InputReaderPolicyTest, Viewports_GetByType) {
1315 const std::string internalUniqueId = "local:0";
1316 const std::string externalUniqueId = "local:1";
1317 const std::string virtualUniqueId1 = "virtual:2";
1318 const std::string virtualUniqueId2 = "virtual:3";
1319 constexpr int32_t virtualDisplayId1 = 2;
1320 constexpr int32_t virtualDisplayId2 = 3;
1321
1322 // Add an internal viewport
1323 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001324 DISPLAY_ORIENTATION_0, true /*isActive*/, internalUniqueId,
1325 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001326 // Add an external viewport
1327 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001328 DISPLAY_ORIENTATION_0, true /*isActive*/, externalUniqueId,
1329 NO_PORT, ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001330 // Add an virtual viewport
1331 mFakePolicy->addDisplayViewport(virtualDisplayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001332 DISPLAY_ORIENTATION_0, true /*isActive*/, virtualUniqueId1,
1333 NO_PORT, ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001334 // Add another virtual viewport
1335 mFakePolicy->addDisplayViewport(virtualDisplayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001336 DISPLAY_ORIENTATION_0, true /*isActive*/, virtualUniqueId2,
1337 NO_PORT, ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001338
1339 // Check matching by type for internal
1340 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001341 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001342 ASSERT_TRUE(internalViewport);
1343 ASSERT_EQ(internalUniqueId, internalViewport->uniqueId);
1344
1345 // Check matching by type for external
1346 std::optional<DisplayViewport> externalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001347 mFakePolicy->getDisplayViewportByType(ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001348 ASSERT_TRUE(externalViewport);
1349 ASSERT_EQ(externalUniqueId, externalViewport->uniqueId);
1350
1351 // Check matching by uniqueId for virtual viewport #1
1352 std::optional<DisplayViewport> virtualViewport1 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001353 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001354 ASSERT_TRUE(virtualViewport1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001355 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport1->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001356 ASSERT_EQ(virtualUniqueId1, virtualViewport1->uniqueId);
1357 ASSERT_EQ(virtualDisplayId1, virtualViewport1->displayId);
1358
1359 // Check matching by uniqueId for virtual viewport #2
1360 std::optional<DisplayViewport> virtualViewport2 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001361 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001362 ASSERT_TRUE(virtualViewport2);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001363 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport2->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001364 ASSERT_EQ(virtualUniqueId2, virtualViewport2->uniqueId);
1365 ASSERT_EQ(virtualDisplayId2, virtualViewport2->displayId);
1366}
1367
1368
1369/**
1370 * We can have 2 viewports of the same kind. We can distinguish them by uniqueId, and confirm
1371 * that lookup works by checking display id.
1372 * Check that 2 viewports of each kind is possible, for all existing viewport types.
1373 */
1374TEST_F(InputReaderPolicyTest, Viewports_TwoOfSameType) {
1375 const std::string uniqueId1 = "uniqueId1";
1376 const std::string uniqueId2 = "uniqueId2";
1377 constexpr int32_t displayId1 = 2;
1378 constexpr int32_t displayId2 = 3;
1379
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001380 std::vector<ViewportType> types = {ViewportType::INTERNAL, ViewportType::EXTERNAL,
1381 ViewportType::VIRTUAL};
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001382 for (const ViewportType& type : types) {
1383 mFakePolicy->clearViewports();
1384 // Add a viewport
1385 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001386 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1,
1387 NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001388 // Add another viewport
1389 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001390 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2,
1391 NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001392
1393 // Check that correct display viewport was returned by comparing the display IDs.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001394 std::optional<DisplayViewport> viewport1 =
1395 mFakePolicy->getDisplayViewportByUniqueId(uniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001396 ASSERT_TRUE(viewport1);
1397 ASSERT_EQ(displayId1, viewport1->displayId);
1398 ASSERT_EQ(type, viewport1->type);
1399
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001400 std::optional<DisplayViewport> viewport2 =
1401 mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001402 ASSERT_TRUE(viewport2);
1403 ASSERT_EQ(displayId2, viewport2->displayId);
1404 ASSERT_EQ(type, viewport2->type);
1405
1406 // When there are multiple viewports of the same kind, and uniqueId is not specified
1407 // in the call to getDisplayViewport, then that situation is not supported.
1408 // The viewports can be stored in any order, so we cannot rely on the order, since that
1409 // is just implementation detail.
1410 // However, we can check that it still returns *a* viewport, we just cannot assert
1411 // which one specifically is returned.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001412 std::optional<DisplayViewport> someViewport = mFakePolicy->getDisplayViewportByType(type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001413 ASSERT_TRUE(someViewport);
1414 }
1415}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001416
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001417/**
Michael Wrightdde67b82020-10-27 16:09:22 +00001418 * When we have multiple internal displays make sure we always return the default display when
1419 * querying by type.
1420 */
1421TEST_F(InputReaderPolicyTest, Viewports_ByTypeReturnsDefaultForInternal) {
1422 const std::string uniqueId1 = "uniqueId1";
1423 const std::string uniqueId2 = "uniqueId2";
1424 constexpr int32_t nonDefaultDisplayId = 2;
1425 static_assert(nonDefaultDisplayId != ADISPLAY_ID_DEFAULT,
1426 "Test display ID should not be ADISPLAY_ID_DEFAULT");
1427
1428 // Add the default display first and ensure it gets returned.
1429 mFakePolicy->clearViewports();
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 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001434 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001435 ViewportType::INTERNAL);
1436
1437 std::optional<DisplayViewport> viewport =
1438 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
1439 ASSERT_TRUE(viewport);
1440 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
1441 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
1442
1443 // Add the default display second to make sure order doesn't matter.
1444 mFakePolicy->clearViewports();
1445 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001446 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001447 ViewportType::INTERNAL);
1448 mFakePolicy->addDisplayViewport(ADISPLAY_ID_DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001449 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001450 ViewportType::INTERNAL);
1451
1452 viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
1453 ASSERT_TRUE(viewport);
1454 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
1455 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
1456}
1457
1458/**
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001459 * Check getDisplayViewportByPort
1460 */
1461TEST_F(InputReaderPolicyTest, Viewports_GetByPort) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001462 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001463 const std::string uniqueId1 = "uniqueId1";
1464 const std::string uniqueId2 = "uniqueId2";
1465 constexpr int32_t displayId1 = 1;
1466 constexpr int32_t displayId2 = 2;
1467 const uint8_t hdmi1 = 0;
1468 const uint8_t hdmi2 = 1;
1469 const uint8_t hdmi3 = 2;
1470
1471 mFakePolicy->clearViewports();
1472 // Add a viewport that's associated with some display port that's not of interest.
1473 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001474 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, hdmi3,
1475 type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001476 // Add another viewport, connected to HDMI1 port
1477 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001478 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, hdmi1,
1479 type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001480
1481 // Check that correct display viewport was returned by comparing the display ports.
1482 std::optional<DisplayViewport> hdmi1Viewport = mFakePolicy->getDisplayViewportByPort(hdmi1);
1483 ASSERT_TRUE(hdmi1Viewport);
1484 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1485 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1486
1487 // Check that we can still get the same viewport using the uniqueId
1488 hdmi1Viewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
1489 ASSERT_TRUE(hdmi1Viewport);
1490 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1491 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1492 ASSERT_EQ(type, hdmi1Viewport->type);
1493
1494 // Check that we cannot find a port with "HDMI2", because we never added one
1495 std::optional<DisplayViewport> hdmi2Viewport = mFakePolicy->getDisplayViewportByPort(hdmi2);
1496 ASSERT_FALSE(hdmi2Viewport);
1497}
1498
Michael Wrightd02c5b62014-02-10 15:10:22 -08001499// --- InputReaderTest ---
1500
1501class InputReaderTest : public testing::Test {
1502protected:
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001503 sp<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001504 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001505 std::shared_ptr<FakeEventHub> mFakeEventHub;
Prabir Pradhan28efc192019-11-05 01:10:04 +00001506 std::unique_ptr<InstrumentedInputReader> mReader;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001507
Chris Yea52ade12020-08-27 16:49:20 -07001508 void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001509 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001510 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001511 mFakeListener = new TestInputListener();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001512
Prabir Pradhan28efc192019-11-05 01:10:04 +00001513 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
1514 mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001515 }
1516
Chris Yea52ade12020-08-27 16:49:20 -07001517 void TearDown() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001518 mFakeListener.clear();
1519 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001520 }
1521
Chris Ye1b0c7342020-07-28 21:57:03 -07001522 void addDevice(int32_t eventHubId, const std::string& name, Flags<InputDeviceClass> classes,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001523 const PropertyMap* configuration) {
1524 mFakeEventHub->addDevice(eventHubId, name, classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001525
1526 if (configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001527 mFakeEventHub->addConfigurationMap(eventHubId, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001528 }
1529 mFakeEventHub->finishDeviceScan();
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001530 mReader->loopOnce();
1531 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001532 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1533 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001534 }
1535
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001536 void disableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001537 mFakePolicy->addDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001538 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001539 }
1540
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001541 void enableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001542 mFakePolicy->removeDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001543 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001544 }
1545
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001546 FakeInputMapper& addDeviceWithFakeInputMapper(int32_t deviceId, int32_t eventHubId,
Chris Ye1b0c7342020-07-28 21:57:03 -07001547 const std::string& name,
1548 Flags<InputDeviceClass> classes, uint32_t sources,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001549 const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001550 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, name);
1551 FakeInputMapper& mapper = device->addMapper<FakeInputMapper>(eventHubId, sources);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001552 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001553 addDevice(eventHubId, name, classes, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001554 return mapper;
1555 }
1556};
1557
Chris Ye98d3f532020-10-01 21:48:59 -07001558TEST_F(InputReaderTest, PolicyGetInputDevices) {
1559 ASSERT_NO_FATAL_FAILURE(addDevice(1, "keyboard", InputDeviceClass::KEYBOARD, nullptr));
1560 ASSERT_NO_FATAL_FAILURE(addDevice(2, "ignored", Flags<InputDeviceClass>(0),
1561 nullptr)); // no classes so device will be ignored
Michael Wrightd02c5b62014-02-10 15:10:22 -08001562
1563 // Should also have received a notification describing the new input devices.
Chris Ye98d3f532020-10-01 21:48:59 -07001564 const std::vector<InputDeviceInfo>& inputDevices = mFakePolicy->getInputDevices();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001565 ASSERT_EQ(1U, inputDevices.size());
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001566 ASSERT_EQ(END_RESERVED_ID + 1, inputDevices[0].getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001567 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001568 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
1569 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00001570 ASSERT_EQ(0U, inputDevices[0].getMotionRanges().size());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001571}
1572
Chris Yee7310032020-09-22 15:36:28 -07001573TEST_F(InputReaderTest, GetMergedInputDevices) {
1574 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1575 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1576 // Add two subdevices to device
1577 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1578 // Must add at least one mapper or the device will be ignored!
1579 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1580 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1581
1582 // Push same device instance for next device to be added, so they'll have same identifier.
1583 mReader->pushNextDevice(device);
1584 mReader->pushNextDevice(device);
1585 ASSERT_NO_FATAL_FAILURE(
1586 addDevice(eventHubIds[0], "fake1", InputDeviceClass::KEYBOARD, nullptr));
1587 ASSERT_NO_FATAL_FAILURE(
1588 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
1589
1590 // Two devices will be merged to one input device as they have same identifier
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00001591 ASSERT_EQ(1U, mFakePolicy->getInputDevices().size());
Chris Yee7310032020-09-22 15:36:28 -07001592}
1593
Chris Yee14523a2020-12-19 13:46:00 -08001594TEST_F(InputReaderTest, GetMergedInputDevicesEnabled) {
1595 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1596 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1597 // Add two subdevices to device
1598 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1599 // Must add at least one mapper or the device will be ignored!
1600 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1601 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1602
1603 // Push same device instance for next device to be added, so they'll have same identifier.
1604 mReader->pushNextDevice(device);
1605 mReader->pushNextDevice(device);
1606 // Sensor device is initially disabled
1607 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1",
1608 InputDeviceClass::KEYBOARD | InputDeviceClass::SENSOR,
1609 nullptr));
1610 // Device is disabled because the only sub device is a sensor device and disabled initially.
1611 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1612 ASSERT_FALSE(device->isEnabled());
1613 ASSERT_NO_FATAL_FAILURE(
1614 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
1615 // The merged device is enabled if any sub device is enabled
1616 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1617 ASSERT_TRUE(device->isEnabled());
1618}
1619
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001620TEST_F(InputReaderTest, WhenEnabledChanges_SendsDeviceResetNotification) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001621 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001622 constexpr Flags<InputDeviceClass> deviceClass(InputDeviceClass::KEYBOARD);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001623 constexpr int32_t eventHubId = 1;
1624 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001625 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001626 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001627 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001628 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001629
Yi Kong9b14ac62018-07-17 13:48:38 -07001630 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001631
1632 NotifyDeviceResetArgs resetArgs;
1633 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001634 ASSERT_EQ(deviceId, resetArgs.deviceId);
1635
1636 ASSERT_EQ(device->isEnabled(), true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001637 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001638 mReader->loopOnce();
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001639
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001640 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001641 ASSERT_EQ(deviceId, resetArgs.deviceId);
1642 ASSERT_EQ(device->isEnabled(), false);
1643
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001644 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001645 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001646 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
1647 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasNotCalled());
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001648 ASSERT_EQ(device->isEnabled(), false);
1649
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001650 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001651 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001652 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001653 ASSERT_EQ(deviceId, resetArgs.deviceId);
1654 ASSERT_EQ(device->isEnabled(), true);
1655}
1656
Michael Wrightd02c5b62014-02-10 15:10:22 -08001657TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001658 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001659 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001660 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001661 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001662 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001663 AINPUT_SOURCE_KEYBOARD, nullptr);
1664 mapper.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001665
1666 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(0,
1667 AINPUT_SOURCE_ANY, AKEYCODE_A))
1668 << "Should return unknown when the device id is >= 0 but unknown.";
1669
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001670 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1671 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1672 << "Should return unknown when the device id is valid but the sources are not "
1673 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001674
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001675 ASSERT_EQ(AKEY_STATE_DOWN,
1676 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1677 AKEYCODE_A))
1678 << "Should return value provided by mapper when device id is valid and the device "
1679 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001680
1681 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(-1,
1682 AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1683 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1684
1685 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getKeyCodeState(-1,
1686 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1687 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1688}
1689
1690TEST_F(InputReaderTest, GetScanCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001691 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001692 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001693 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001694 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001695 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001696 AINPUT_SOURCE_KEYBOARD, nullptr);
1697 mapper.setScanCodeState(KEY_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001698
1699 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(0,
1700 AINPUT_SOURCE_ANY, KEY_A))
1701 << "Should return unknown when the device id is >= 0 but unknown.";
1702
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001703 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1704 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, KEY_A))
1705 << "Should return unknown when the device id is valid but the sources are not "
1706 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001707
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001708 ASSERT_EQ(AKEY_STATE_DOWN,
1709 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1710 KEY_A))
1711 << "Should return value provided by mapper when device id is valid and the device "
1712 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001713
1714 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(-1,
1715 AINPUT_SOURCE_TRACKBALL, KEY_A))
1716 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1717
1718 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getScanCodeState(-1,
1719 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, KEY_A))
1720 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1721}
1722
1723TEST_F(InputReaderTest, GetSwitchState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001724 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001725 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001726 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001727 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001728 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001729 AINPUT_SOURCE_KEYBOARD, nullptr);
1730 mapper.setSwitchState(SW_LID, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001731
1732 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(0,
1733 AINPUT_SOURCE_ANY, SW_LID))
1734 << "Should return unknown when the device id is >= 0 but unknown.";
1735
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001736 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1737 mReader->getSwitchState(deviceId, AINPUT_SOURCE_TRACKBALL, SW_LID))
1738 << "Should return unknown when the device id is valid but the sources are not "
1739 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001740
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001741 ASSERT_EQ(AKEY_STATE_DOWN,
1742 mReader->getSwitchState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1743 SW_LID))
1744 << "Should return value provided by mapper when device id is valid and the device "
1745 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001746
1747 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(-1,
1748 AINPUT_SOURCE_TRACKBALL, SW_LID))
1749 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1750
1751 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getSwitchState(-1,
1752 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, SW_LID))
1753 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1754}
1755
1756TEST_F(InputReaderTest, MarkSupportedKeyCodes_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001757 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001758 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001759 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001760 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001761 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001762 AINPUT_SOURCE_KEYBOARD, nullptr);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001763
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001764 mapper.addSupportedKeyCode(AKEYCODE_A);
1765 mapper.addSupportedKeyCode(AKEYCODE_B);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001766
1767 const int32_t keyCodes[4] = { AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2 };
1768 uint8_t flags[4] = { 0, 0, 0, 1 };
1769
1770 ASSERT_FALSE(mReader->hasKeys(0, AINPUT_SOURCE_ANY, 4, keyCodes, flags))
1771 << "Should return false when device id is >= 0 but unknown.";
1772 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1773
1774 flags[3] = 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001775 ASSERT_FALSE(mReader->hasKeys(deviceId, AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1776 << "Should return false when device id is valid but the sources are not supported by "
1777 "the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001778 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1779
1780 flags[3] = 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001781 ASSERT_TRUE(mReader->hasKeys(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, 4,
1782 keyCodes, flags))
1783 << "Should return value provided by mapper when device id is valid and the device "
1784 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001785 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1786
1787 flags[3] = 1;
1788 ASSERT_FALSE(mReader->hasKeys(-1, AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1789 << "Should return false when the device id is < 0 but the sources are not supported by any device.";
1790 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1791
1792 flags[3] = 1;
1793 ASSERT_TRUE(mReader->hasKeys(-1, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1794 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1795 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1796}
1797
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001798TEST_F(InputReaderTest, LoopOnce_WhenDeviceScanFinished_SendsConfigurationChanged) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001799 constexpr int32_t eventHubId = 1;
Chris Ye1b0c7342020-07-28 21:57:03 -07001800 addDevice(eventHubId, "ignored", InputDeviceClass::KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001801
1802 NotifyConfigurationChangedArgs args;
1803
1804 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(&args));
1805 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
1806}
1807
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001808TEST_F(InputReaderTest, LoopOnce_ForwardsRawEventsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001809 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001810 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001811 constexpr nsecs_t when = 0;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001812 constexpr int32_t eventHubId = 1;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001813 constexpr nsecs_t readTime = 2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001814 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001815 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001816 AINPUT_SOURCE_KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001817
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001818 mFakeEventHub->enqueueEvent(when, readTime, eventHubId, EV_KEY, KEY_A, 1);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001819 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001820 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
1821
1822 RawEvent event;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001823 ASSERT_NO_FATAL_FAILURE(mapper.assertProcessWasCalled(&event));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001824 ASSERT_EQ(when, event.when);
1825 ASSERT_EQ(readTime, event.readTime);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001826 ASSERT_EQ(eventHubId, event.deviceId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001827 ASSERT_EQ(EV_KEY, event.type);
1828 ASSERT_EQ(KEY_A, event.code);
1829 ASSERT_EQ(1, event.value);
1830}
1831
Garfield Tan1c7bc862020-01-28 13:24:04 -08001832TEST_F(InputReaderTest, DeviceReset_RandomId) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001833 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001834 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001835 constexpr int32_t eventHubId = 1;
1836 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Prabir Pradhan42611e02018-11-27 14:04:02 -08001837 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001838 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001839 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001840 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan42611e02018-11-27 14:04:02 -08001841
1842 NotifyDeviceResetArgs resetArgs;
1843 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001844 int32_t prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001845
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001846 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001847 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001848 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001849 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001850 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001851
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001852 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001853 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001854 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001855 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001856 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001857
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001858 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001859 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001860 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001861 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001862 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001863}
1864
Garfield Tan1c7bc862020-01-28 13:24:04 -08001865TEST_F(InputReaderTest, DeviceReset_GenerateIdWithInputReaderSource) {
1866 constexpr int32_t deviceId = 1;
Chris Ye1b0c7342020-07-28 21:57:03 -07001867 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Garfield Tan1c7bc862020-01-28 13:24:04 -08001868 constexpr int32_t eventHubId = 1;
1869 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1870 // Must add at least one mapper or the device will be ignored!
1871 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001872 mReader->pushNextDevice(device);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001873 ASSERT_NO_FATAL_FAILURE(addDevice(deviceId, "fake", deviceClass, nullptr));
1874
1875 NotifyDeviceResetArgs resetArgs;
1876 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1877 ASSERT_EQ(IdGenerator::Source::INPUT_READER, IdGenerator::getSource(resetArgs.id));
1878}
1879
Arthur Hungc23540e2018-11-29 20:42:11 +08001880TEST_F(InputReaderTest, Device_CanDispatchToDisplay) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001881 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001882 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001883 constexpr int32_t eventHubId = 1;
Arthur Hungc23540e2018-11-29 20:42:11 +08001884 const char* DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001885 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1886 FakeInputMapper& mapper =
1887 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001888 mReader->pushNextDevice(device);
Arthur Hungc23540e2018-11-29 20:42:11 +08001889
1890 const uint8_t hdmi1 = 1;
1891
1892 // Associated touch screen with second display.
1893 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
1894
1895 // Add default and second display.
Prabir Pradhan28efc192019-11-05 01:10:04 +00001896 mFakePolicy->clearViewports();
Arthur Hungc23540e2018-11-29 20:42:11 +08001897 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001898 DISPLAY_ORIENTATION_0, true /*isActive*/, "local:0", NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001899 ViewportType::INTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08001900 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001901 DISPLAY_ORIENTATION_0, true /*isActive*/, "local:1", hdmi1,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001902 ViewportType::EXTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08001903 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001904 mReader->loopOnce();
Prabir Pradhan28efc192019-11-05 01:10:04 +00001905
1906 // Add the device, and make sure all of the callbacks are triggered.
1907 // The device is added after the input port associations are processed since
1908 // we do not yet support dynamic device-to-display associations.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001909 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001910 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled());
Prabir Pradhan28efc192019-11-05 01:10:04 +00001911 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001912 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
Arthur Hungc23540e2018-11-29 20:42:11 +08001913
Arthur Hung2c9a3342019-07-23 14:18:59 +08001914 // Device should only dispatch to the specified display.
Arthur Hungc23540e2018-11-29 20:42:11 +08001915 ASSERT_EQ(deviceId, device->getId());
1916 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, DISPLAY_ID));
1917 ASSERT_TRUE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hung2c9a3342019-07-23 14:18:59 +08001918
1919 // Can't dispatch event from a disabled device.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001920 disableDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001921 mReader->loopOnce();
Arthur Hung2c9a3342019-07-23 14:18:59 +08001922 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hungc23540e2018-11-29 20:42:11 +08001923}
1924
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001925TEST_F(InputReaderTest, WhenEnabledChanges_AllSubdevicesAreUpdated) {
1926 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1927 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
1928 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1929 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1930 // Must add at least one mapper or the device will be ignored!
1931 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1932 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1933 mReader->pushNextDevice(device);
1934 mReader->pushNextDevice(device);
1935 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
1936 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
1937
1938 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
1939
1940 NotifyDeviceResetArgs resetArgs;
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 disableDevice(deviceId);
1948 mReader->loopOnce();
1949
1950 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1951 ASSERT_EQ(deviceId, resetArgs.deviceId);
1952 ASSERT_FALSE(device->isEnabled());
1953 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1954 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1955
1956 enableDevice(deviceId);
1957 mReader->loopOnce();
1958
1959 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1960 ASSERT_EQ(deviceId, resetArgs.deviceId);
1961 ASSERT_TRUE(device->isEnabled());
1962 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1963 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1964}
1965
1966TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToSubdeviceMappers) {
1967 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1968 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
1969 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1970 // Add two subdevices to device
1971 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1972 FakeInputMapper& mapperDevice1 =
1973 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1974 FakeInputMapper& mapperDevice2 =
1975 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1976 mReader->pushNextDevice(device);
1977 mReader->pushNextDevice(device);
1978 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
1979 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
1980
1981 mapperDevice1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
1982 mapperDevice2.setKeyCodeState(AKEYCODE_B, AKEY_STATE_DOWN);
1983
1984 ASSERT_EQ(AKEY_STATE_DOWN,
1985 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_A));
1986 ASSERT_EQ(AKEY_STATE_DOWN,
1987 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_B));
1988 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1989 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_C));
1990}
1991
Prabir Pradhan7e186182020-11-10 13:56:45 -08001992TEST_F(InputReaderTest, ChangingPointerCaptureNotifiesInputListener) {
1993 NotifyPointerCaptureChangedArgs args;
1994
1995 mFakePolicy->setPointerCapture(true);
1996 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
1997 mReader->loopOnce();
1998 mFakeListener->assertNotifyCaptureWasCalled(&args);
1999 ASSERT_TRUE(args.enabled) << "Pointer Capture should be enabled.";
2000
2001 mFakePolicy->setPointerCapture(false);
2002 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
2003 mReader->loopOnce();
2004 mFakeListener->assertNotifyCaptureWasCalled(&args);
2005 ASSERT_FALSE(args.enabled) << "Pointer Capture should be disabled.";
2006
2007 // Verify that the Pointer Capture state is re-configured correctly when the configuration value
2008 // does not change.
2009 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
2010 mReader->loopOnce();
2011 mFakeListener->assertNotifyCaptureWasCalled(&args);
2012 ASSERT_FALSE(args.enabled) << "Pointer Capture should be disabled.";
2013}
2014
Chris Ye87143712020-11-10 05:05:58 +00002015class FakeVibratorInputMapper : public FakeInputMapper {
2016public:
2017 FakeVibratorInputMapper(InputDeviceContext& deviceContext, uint32_t sources)
2018 : FakeInputMapper(deviceContext, sources) {}
2019
2020 std::vector<int32_t> getVibratorIds() override { return getDeviceContext().getVibratorIds(); }
2021};
2022
2023TEST_F(InputReaderTest, VibratorGetVibratorIds) {
2024 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
2025 Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD | InputDeviceClass::VIBRATOR;
2026 constexpr int32_t eventHubId = 1;
2027 const char* DEVICE_LOCATION = "BLUETOOTH";
2028 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
2029 FakeVibratorInputMapper& mapper =
2030 device->addMapper<FakeVibratorInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
2031 mReader->pushNextDevice(device);
2032
2033 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2034 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
2035
2036 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
2037 ASSERT_EQ(mReader->getVibratorIds(deviceId).size(), 2U);
2038}
2039
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002040// --- FakePeripheralController ---
Kim Low03ea0352020-11-06 12:45:07 -08002041
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002042class FakePeripheralController : public PeripheralControllerInterface {
Chris Yee2b1e5c2021-03-10 22:45:12 -08002043public:
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002044 FakePeripheralController(InputDeviceContext& deviceContext) : mDeviceContext(deviceContext) {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08002045
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002046 ~FakePeripheralController() override {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08002047
2048 void populateDeviceInfo(InputDeviceInfo* deviceInfo) override {}
2049
2050 void dump(std::string& dump) override {}
2051
2052 std::optional<int32_t> getBatteryCapacity(int32_t batteryId) override {
2053 return getDeviceContext().getBatteryCapacity(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08002054 }
2055
Chris Yee2b1e5c2021-03-10 22:45:12 -08002056 std::optional<int32_t> getBatteryStatus(int32_t batteryId) override {
2057 return getDeviceContext().getBatteryStatus(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08002058 }
Chris Ye3fdbfef2021-01-06 18:45:18 -08002059
2060 bool setLightColor(int32_t lightId, int32_t color) override {
2061 getDeviceContext().setLightBrightness(lightId, color >> 24);
2062 return true;
2063 }
2064
2065 std::optional<int32_t> getLightColor(int32_t lightId) override {
2066 std::optional<int32_t> result = getDeviceContext().getLightBrightness(lightId);
2067 if (!result.has_value()) {
2068 return std::nullopt;
2069 }
2070 return result.value() << 24;
2071 }
Chris Yee2b1e5c2021-03-10 22:45:12 -08002072
2073 bool setLightPlayerId(int32_t lightId, int32_t playerId) override { return true; }
2074
2075 std::optional<int32_t> getLightPlayerId(int32_t lightId) override { return std::nullopt; }
2076
2077private:
2078 InputDeviceContext& mDeviceContext;
2079 inline int32_t getDeviceId() { return mDeviceContext.getId(); }
2080 inline InputDeviceContext& getDeviceContext() { return mDeviceContext; }
Chris Ye3fdbfef2021-01-06 18:45:18 -08002081};
2082
Chris Yee2b1e5c2021-03-10 22:45:12 -08002083TEST_F(InputReaderTest, BatteryGetCapacity) {
2084 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
2085 Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
2086 constexpr int32_t eventHubId = 1;
2087 const char* DEVICE_LOCATION = "BLUETOOTH";
2088 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002089 FakePeripheralController& controller =
2090 device->addController<FakePeripheralController>(eventHubId);
Chris Yee2b1e5c2021-03-10 22:45:12 -08002091 mReader->pushNextDevice(device);
2092
2093 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2094
2095 ASSERT_EQ(controller.getBatteryCapacity(DEFAULT_BATTERY), BATTERY_CAPACITY);
2096 ASSERT_EQ(mReader->getBatteryCapacity(deviceId), BATTERY_CAPACITY);
2097}
2098
2099TEST_F(InputReaderTest, BatteryGetStatus) {
2100 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
2101 Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
2102 constexpr int32_t eventHubId = 1;
2103 const char* DEVICE_LOCATION = "BLUETOOTH";
2104 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002105 FakePeripheralController& controller =
2106 device->addController<FakePeripheralController>(eventHubId);
Chris Yee2b1e5c2021-03-10 22:45:12 -08002107 mReader->pushNextDevice(device);
2108
2109 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2110
2111 ASSERT_EQ(controller.getBatteryStatus(DEFAULT_BATTERY), BATTERY_STATUS);
2112 ASSERT_EQ(mReader->getBatteryStatus(deviceId), BATTERY_STATUS);
2113}
2114
Chris Ye3fdbfef2021-01-06 18:45:18 -08002115TEST_F(InputReaderTest, LightGetColor) {
2116 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
2117 Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD | InputDeviceClass::LIGHT;
2118 constexpr int32_t eventHubId = 1;
2119 const char* DEVICE_LOCATION = "BLUETOOTH";
2120 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002121 FakePeripheralController& controller =
2122 device->addController<FakePeripheralController>(eventHubId);
Chris Ye3fdbfef2021-01-06 18:45:18 -08002123 mReader->pushNextDevice(device);
2124 RawLightInfo info = {.id = 1,
2125 .name = "Mono",
2126 .maxBrightness = 255,
2127 .flags = InputLightClass::BRIGHTNESS,
2128 .path = ""};
2129 mFakeEventHub->addRawLightInfo(1 /* rawId */, std::move(info));
2130 mFakeEventHub->fakeLightBrightness(1 /* rawId */, 0x55);
2131
2132 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Chris Ye3fdbfef2021-01-06 18:45:18 -08002133
Chris Yee2b1e5c2021-03-10 22:45:12 -08002134 ASSERT_TRUE(controller.setLightColor(1 /* lightId */, LIGHT_BRIGHTNESS));
2135 ASSERT_EQ(controller.getLightColor(1 /* lightId */), LIGHT_BRIGHTNESS);
Chris Ye3fdbfef2021-01-06 18:45:18 -08002136 ASSERT_TRUE(mReader->setLightColor(deviceId, 1 /* lightId */, LIGHT_BRIGHTNESS));
2137 ASSERT_EQ(mReader->getLightColor(deviceId, 1 /* lightId */), LIGHT_BRIGHTNESS);
2138}
2139
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002140// --- InputReaderIntegrationTest ---
2141
2142// These tests create and interact with the InputReader only through its interface.
2143// The InputReader is started during SetUp(), which starts its processing in its own
2144// thread. The tests use linux uinput to emulate input devices.
2145// NOTE: Interacting with the physical device while these tests are running may cause
2146// the tests to fail.
2147class InputReaderIntegrationTest : public testing::Test {
2148protected:
2149 sp<TestInputListener> mTestListener;
2150 sp<FakeInputReaderPolicy> mFakePolicy;
2151 sp<InputReaderInterface> mReader;
2152
Chris Yea52ade12020-08-27 16:49:20 -07002153 void SetUp() override {
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002154 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakouf0db5b82020-04-08 19:22:14 -07002155 mTestListener = new TestInputListener(2000ms /*eventHappenedTimeout*/,
2156 30ms /*eventDidNotHappenTimeout*/);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002157
Prabir Pradhan9244aea2020-02-05 20:31:40 -08002158 mReader = new InputReader(std::make_shared<EventHub>(), mFakePolicy, mTestListener);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002159 ASSERT_EQ(mReader->start(), OK);
2160
2161 // Since this test is run on a real device, all the input devices connected
2162 // to the test device will show up in mReader. We wait for those input devices to
2163 // show up before beginning the tests.
2164 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2165 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2166 }
2167
Chris Yea52ade12020-08-27 16:49:20 -07002168 void TearDown() override {
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002169 ASSERT_EQ(mReader->stop(), OK);
2170 mTestListener.clear();
2171 mFakePolicy.clear();
2172 }
2173};
2174
2175TEST_F(InputReaderIntegrationTest, TestInvalidDevice) {
2176 // An invalid input device that is only used for this test.
2177 class InvalidUinputDevice : public UinputDevice {
2178 public:
2179 InvalidUinputDevice() : UinputDevice("Invalid Device") {}
2180
2181 private:
2182 void configureDevice(int fd, uinput_user_dev* device) override {}
2183 };
2184
2185 const size_t numDevices = mFakePolicy->getInputDevices().size();
2186
2187 // UinputDevice does not set any event or key bits, so InputReader should not
2188 // consider it as a valid device.
2189 std::unique_ptr<UinputDevice> invalidDevice = createUinputDevice<InvalidUinputDevice>();
2190 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
2191 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
2192 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
2193
2194 invalidDevice.reset();
2195 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
2196 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
2197 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
2198}
2199
2200TEST_F(InputReaderIntegrationTest, AddNewDevice) {
2201 const size_t initialNumDevices = mFakePolicy->getInputDevices().size();
2202
2203 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
2204 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2205 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2206 ASSERT_EQ(initialNumDevices + 1, mFakePolicy->getInputDevices().size());
2207
2208 // Find the test device by its name.
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002209 const std::vector<InputDeviceInfo> inputDevices = mFakePolicy->getInputDevices();
Chris Ye98d3f532020-10-01 21:48:59 -07002210 const auto& it =
2211 std::find_if(inputDevices.begin(), inputDevices.end(),
2212 [&keyboard](const InputDeviceInfo& info) {
2213 return info.getIdentifier().name == keyboard->getName();
2214 });
2215
2216 ASSERT_NE(it, inputDevices.end());
2217 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, it->getKeyboardType());
2218 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, it->getSources());
2219 ASSERT_EQ(0U, it->getMotionRanges().size());
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002220
2221 keyboard.reset();
2222 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2223 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2224 ASSERT_EQ(initialNumDevices, mFakePolicy->getInputDevices().size());
2225}
2226
2227TEST_F(InputReaderIntegrationTest, SendsEventsToInputListener) {
2228 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
2229 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2230
2231 NotifyConfigurationChangedArgs configChangedArgs;
2232 ASSERT_NO_FATAL_FAILURE(
2233 mTestListener->assertNotifyConfigurationChangedWasCalled(&configChangedArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002234 int32_t prevId = configChangedArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002235 nsecs_t prevTimestamp = configChangedArgs.eventTime;
2236
2237 NotifyKeyArgs keyArgs;
2238 keyboard->pressAndReleaseHomeKey();
2239 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
2240 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002241 ASSERT_NE(prevId, keyArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002242 prevId = keyArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002243 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002244 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002245 prevTimestamp = keyArgs.eventTime;
2246
2247 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
2248 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002249 ASSERT_NE(prevId, keyArgs.id);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002250 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002251 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002252}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002253
Siarhei Vishniakoua0d2b802020-05-13 14:00:31 -07002254/**
2255 * The Steam controller sends BTN_GEAR_DOWN and BTN_GEAR_UP for the two "paddle" buttons
2256 * on the back. In this test, we make sure that BTN_GEAR_DOWN / BTN_WHEEL and BTN_GEAR_UP
2257 * are passed to the listener.
2258 */
2259static_assert(BTN_GEAR_DOWN == BTN_WHEEL);
2260TEST_F(InputReaderIntegrationTest, SendsGearDownAndUpToInputListener) {
2261 std::unique_ptr<UinputSteamController> controller = createUinputDevice<UinputSteamController>();
2262 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2263 NotifyKeyArgs keyArgs;
2264
2265 controller->pressAndReleaseKey(BTN_GEAR_DOWN);
2266 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
2267 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
2268 ASSERT_EQ(BTN_GEAR_DOWN, keyArgs.scanCode);
2269
2270 controller->pressAndReleaseKey(BTN_GEAR_UP);
2271 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
2272 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
2273 ASSERT_EQ(BTN_GEAR_UP, keyArgs.scanCode);
2274}
2275
Arthur Hungaab25622020-01-16 11:22:11 +08002276// --- TouchProcessTest ---
2277class TouchIntegrationTest : public InputReaderIntegrationTest {
2278protected:
Arthur Hungaab25622020-01-16 11:22:11 +08002279 const std::string UNIQUE_ID = "local:0";
2280
Chris Yea52ade12020-08-27 16:49:20 -07002281 void SetUp() override {
Arthur Hungaab25622020-01-16 11:22:11 +08002282 InputReaderIntegrationTest::SetUp();
2283 // At least add an internal display.
2284 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2285 DISPLAY_ORIENTATION_0, UNIQUE_ID, NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002286 ViewportType::INTERNAL);
Arthur Hungaab25622020-01-16 11:22:11 +08002287
2288 mDevice = createUinputDevice<UinputTouchScreen>(Rect(0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT));
2289 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2290 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2291 }
2292
2293 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
2294 int32_t orientation, const std::string& uniqueId,
2295 std::optional<uint8_t> physicalPort,
2296 ViewportType viewportType) {
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002297 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, true /*isActive*/,
2298 uniqueId, physicalPort, viewportType);
Arthur Hungaab25622020-01-16 11:22:11 +08002299 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2300 }
2301
2302 std::unique_ptr<UinputTouchScreen> mDevice;
2303};
2304
2305TEST_F(TouchIntegrationTest, InputEvent_ProcessSingleTouch) {
2306 NotifyMotionArgs args;
2307 const Point centerPoint = mDevice->getCenterPoint();
2308
2309 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00002310 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002311 mDevice->sendDown(centerPoint);
2312 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2313 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2314
2315 // ACTION_MOVE
2316 mDevice->sendMove(centerPoint + Point(1, 1));
2317 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2318 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2319
2320 // ACTION_UP
2321 mDevice->sendUp();
2322 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2323 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2324}
2325
2326TEST_F(TouchIntegrationTest, InputEvent_ProcessMultiTouch) {
2327 NotifyMotionArgs args;
2328 const Point centerPoint = mDevice->getCenterPoint();
2329
2330 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00002331 mDevice->sendSlot(FIRST_SLOT);
2332 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002333 mDevice->sendDown(centerPoint);
2334 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2335 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2336
2337 // ACTION_POINTER_DOWN (Second slot)
2338 const Point secondPoint = centerPoint + Point(100, 100);
2339 mDevice->sendSlot(SECOND_SLOT);
2340 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2341 mDevice->sendDown(secondPoint + Point(1, 1));
2342 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2343 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
2344 args.action);
2345
2346 // ACTION_MOVE (Second slot)
2347 mDevice->sendMove(secondPoint);
2348 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2349 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2350
2351 // ACTION_POINTER_UP (Second slot)
arthurhungcc7f9802020-04-30 17:55:40 +08002352 mDevice->sendPointerUp();
Arthur Hungaab25622020-01-16 11:22:11 +08002353 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
arthurhungcc7f9802020-04-30 17:55:40 +08002354 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
Arthur Hungaab25622020-01-16 11:22:11 +08002355 args.action);
2356
2357 // ACTION_UP
2358 mDevice->sendSlot(FIRST_SLOT);
2359 mDevice->sendUp();
2360 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2361 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2362}
2363
2364TEST_F(TouchIntegrationTest, InputEvent_ProcessPalm) {
2365 NotifyMotionArgs args;
2366 const Point centerPoint = mDevice->getCenterPoint();
2367
2368 // ACTION_DOWN
arthurhungcc7f9802020-04-30 17:55:40 +08002369 mDevice->sendSlot(FIRST_SLOT);
2370 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002371 mDevice->sendDown(centerPoint);
2372 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2373 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2374
arthurhungcc7f9802020-04-30 17:55:40 +08002375 // ACTION_POINTER_DOWN (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002376 const Point secondPoint = centerPoint + Point(100, 100);
2377 mDevice->sendSlot(SECOND_SLOT);
2378 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2379 mDevice->sendDown(secondPoint);
2380 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2381 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
2382 args.action);
2383
arthurhungcc7f9802020-04-30 17:55:40 +08002384 // ACTION_MOVE (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002385 mDevice->sendMove(secondPoint + Point(1, 1));
2386 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2387 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2388
arthurhungcc7f9802020-04-30 17:55:40 +08002389 // Send MT_TOOL_PALM (second slot), which indicates that the touch IC has determined this to be
2390 // a palm event.
2391 // Expect to receive the ACTION_POINTER_UP with cancel flag.
Arthur Hungaab25622020-01-16 11:22:11 +08002392 mDevice->sendToolType(MT_TOOL_PALM);
2393 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
arthurhungcc7f9802020-04-30 17:55:40 +08002394 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
2395 args.action);
2396 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, args.flags);
Arthur Hungaab25622020-01-16 11:22:11 +08002397
arthurhungcc7f9802020-04-30 17:55:40 +08002398 // Send up to second slot, expect first slot send moving.
2399 mDevice->sendPointerUp();
2400 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2401 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002402
arthurhungcc7f9802020-04-30 17:55:40 +08002403 // Send ACTION_UP (first slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002404 mDevice->sendSlot(FIRST_SLOT);
2405 mDevice->sendUp();
2406
arthurhungcc7f9802020-04-30 17:55:40 +08002407 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2408 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002409}
2410
Michael Wrightd02c5b62014-02-10 15:10:22 -08002411// --- InputDeviceTest ---
Michael Wrightd02c5b62014-02-10 15:10:22 -08002412class InputDeviceTest : public testing::Test {
2413protected:
2414 static const char* DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002415 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002416 static const int32_t DEVICE_ID;
2417 static const int32_t DEVICE_GENERATION;
2418 static const int32_t DEVICE_CONTROLLER_NUMBER;
Chris Ye1b0c7342020-07-28 21:57:03 -07002419 static const Flags<InputDeviceClass> DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002420 static const int32_t EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002421
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002422 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002423 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08002424 sp<TestInputListener> mFakeListener;
arthurhungdcef2dc2020-08-11 14:47:50 +08002425 std::unique_ptr<InstrumentedInputReader> mReader;
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00002426 std::shared_ptr<InputDevice> mDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002427
Chris Yea52ade12020-08-27 16:49:20 -07002428 void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002429 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002430 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08002431 mFakeListener = new TestInputListener();
arthurhungdcef2dc2020-08-11 14:47:50 +08002432 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
2433 mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002434 InputDeviceIdentifier identifier;
2435 identifier.name = DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002436 identifier.location = DEVICE_LOCATION;
arthurhungdcef2dc2020-08-11 14:47:50 +08002437 mDevice = std::make_shared<InputDevice>(mReader->getContext(), DEVICE_ID, DEVICE_GENERATION,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002438 identifier);
arthurhungdcef2dc2020-08-11 14:47:50 +08002439 mReader->pushNextDevice(mDevice);
2440 mFakeEventHub->addDevice(EVENTHUB_ID, DEVICE_NAME, Flags<InputDeviceClass>(0));
2441 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002442 }
2443
Chris Yea52ade12020-08-27 16:49:20 -07002444 void TearDown() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002445 mFakeListener.clear();
2446 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002447 }
2448};
2449
2450const char* InputDeviceTest::DEVICE_NAME = "device";
Arthur Hung2c9a3342019-07-23 14:18:59 +08002451const char* InputDeviceTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002452const int32_t InputDeviceTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002453const int32_t InputDeviceTest::DEVICE_GENERATION = 2;
2454const int32_t InputDeviceTest::DEVICE_CONTROLLER_NUMBER = 0;
Chris Ye1b0c7342020-07-28 21:57:03 -07002455const Flags<InputDeviceClass> InputDeviceTest::DEVICE_CLASSES =
2456 InputDeviceClass::KEYBOARD | InputDeviceClass::TOUCH | InputDeviceClass::JOYSTICK;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002457const int32_t InputDeviceTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002458
2459TEST_F(InputDeviceTest, ImmutableProperties) {
2460 ASSERT_EQ(DEVICE_ID, mDevice->getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002461 ASSERT_STREQ(DEVICE_NAME, mDevice->getName().c_str());
Chris Ye1b0c7342020-07-28 21:57:03 -07002462 ASSERT_EQ(Flags<InputDeviceClass>(0), mDevice->getClasses());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002463}
2464
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002465TEST_F(InputDeviceTest, WhenDeviceCreated_EnabledIsFalse) {
2466 ASSERT_EQ(mDevice->isEnabled(), false);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07002467}
2468
Michael Wrightd02c5b62014-02-10 15:10:22 -08002469TEST_F(InputDeviceTest, WhenNoMappersAreRegistered_DeviceIsIgnored) {
2470 // Configuration.
2471 InputReaderConfiguration config;
2472 mDevice->configure(ARBITRARY_TIME, &config, 0);
2473
2474 // Reset.
2475 mDevice->reset(ARBITRARY_TIME);
2476
2477 NotifyDeviceResetArgs resetArgs;
2478 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2479 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2480 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2481
2482 // Metadata.
2483 ASSERT_TRUE(mDevice->isIgnored());
2484 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mDevice->getSources());
2485
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002486 InputDeviceInfo info = mDevice->getDeviceInfo();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002487 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002488 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002489 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NONE, info.getKeyboardType());
2490 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, info.getSources());
2491
2492 // State queries.
2493 ASSERT_EQ(0, mDevice->getMetaState());
2494
2495 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2496 << "Ignored device should return unknown key code state.";
2497 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2498 << "Ignored device should return unknown scan code state.";
2499 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 0))
2500 << "Ignored device should return unknown switch state.";
2501
2502 const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
2503 uint8_t flags[2] = { 0, 1 };
2504 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, 2, keyCodes, flags))
2505 << "Ignored device should never mark any key codes.";
2506 ASSERT_EQ(0, flags[0]) << "Flag for unsupported key should be unchanged.";
2507 ASSERT_EQ(1, flags[1]) << "Flag for unsupported key should be unchanged.";
2508}
2509
2510TEST_F(InputDeviceTest, WhenMappersAreRegistered_DeviceIsNotIgnoredAndForwardsRequestsToMappers) {
2511 // Configuration.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002512 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, String8("key"), String8("value"));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002513
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002514 FakeInputMapper& mapper1 =
2515 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002516 mapper1.setKeyboardType(AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2517 mapper1.setMetaState(AMETA_ALT_ON);
2518 mapper1.addSupportedKeyCode(AKEYCODE_A);
2519 mapper1.addSupportedKeyCode(AKEYCODE_B);
2520 mapper1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
2521 mapper1.setKeyCodeState(AKEYCODE_B, AKEY_STATE_UP);
2522 mapper1.setScanCodeState(2, AKEY_STATE_DOWN);
2523 mapper1.setScanCodeState(3, AKEY_STATE_UP);
2524 mapper1.setSwitchState(4, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002525
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002526 FakeInputMapper& mapper2 =
2527 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002528 mapper2.setMetaState(AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002529
2530 InputReaderConfiguration config;
2531 mDevice->configure(ARBITRARY_TIME, &config, 0);
2532
2533 String8 propertyValue;
2534 ASSERT_TRUE(mDevice->getConfiguration().tryGetProperty(String8("key"), propertyValue))
2535 << "Device should have read configuration during configuration phase.";
2536 ASSERT_STREQ("value", propertyValue.string());
2537
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002538 ASSERT_NO_FATAL_FAILURE(mapper1.assertConfigureWasCalled());
2539 ASSERT_NO_FATAL_FAILURE(mapper2.assertConfigureWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002540
2541 // Reset
2542 mDevice->reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002543 ASSERT_NO_FATAL_FAILURE(mapper1.assertResetWasCalled());
2544 ASSERT_NO_FATAL_FAILURE(mapper2.assertResetWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002545
2546 NotifyDeviceResetArgs resetArgs;
2547 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2548 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2549 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2550
2551 // Metadata.
2552 ASSERT_FALSE(mDevice->isIgnored());
2553 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), mDevice->getSources());
2554
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002555 InputDeviceInfo info = mDevice->getDeviceInfo();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002556 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002557 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002558 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_ALPHABETIC, info.getKeyboardType());
2559 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), info.getSources());
2560
2561 // State queries.
2562 ASSERT_EQ(AMETA_ALT_ON | AMETA_SHIFT_ON, mDevice->getMetaState())
2563 << "Should query mappers and combine meta states.";
2564
2565 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2566 << "Should return unknown key code state when source not supported.";
2567 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2568 << "Should return unknown scan code state when source not supported.";
2569 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2570 << "Should return unknown switch state when source not supported.";
2571
2572 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, AKEYCODE_A))
2573 << "Should query mapper when source is supported.";
2574 ASSERT_EQ(AKEY_STATE_UP, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 3))
2575 << "Should query mapper when source is supported.";
2576 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 4))
2577 << "Should query mapper when source is supported.";
2578
2579 const int32_t keyCodes[4] = { AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2 };
2580 uint8_t flags[4] = { 0, 0, 0, 1 };
2581 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
2582 << "Should do nothing when source is unsupported.";
2583 ASSERT_EQ(0, flags[0]) << "Flag should be unchanged when source is unsupported.";
2584 ASSERT_EQ(0, flags[1]) << "Flag should be unchanged when source is unsupported.";
2585 ASSERT_EQ(0, flags[2]) << "Flag should be unchanged when source is unsupported.";
2586 ASSERT_EQ(1, flags[3]) << "Flag should be unchanged when source is unsupported.";
2587
2588 ASSERT_TRUE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, 4, keyCodes, flags))
2589 << "Should query mapper when source is supported.";
2590 ASSERT_EQ(1, flags[0]) << "Flag for supported key should be set.";
2591 ASSERT_EQ(1, flags[1]) << "Flag for supported key should be set.";
2592 ASSERT_EQ(0, flags[2]) << "Flag for unsupported key should be unchanged.";
2593 ASSERT_EQ(1, flags[3]) << "Flag for unsupported key should be unchanged.";
2594
2595 // Event handling.
2596 RawEvent event;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002597 event.deviceId = EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002598 mDevice->process(&event, 1);
2599
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002600 ASSERT_NO_FATAL_FAILURE(mapper1.assertProcessWasCalled());
2601 ASSERT_NO_FATAL_FAILURE(mapper2.assertProcessWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002602}
2603
Arthur Hung2c9a3342019-07-23 14:18:59 +08002604// A single input device is associated with a specific display. Check that:
2605// 1. Device is disabled if the viewport corresponding to the associated display is not found
2606// 2. Device is disabled when setEnabled API is called
2607TEST_F(InputDeviceTest, Configure_AssignsDisplayPort) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002608 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002609
2610 // First Configuration.
2611 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
2612
2613 // Device should be enabled by default.
2614 ASSERT_TRUE(mDevice->isEnabled());
2615
2616 // Prepare associated info.
2617 constexpr uint8_t hdmi = 1;
2618 const std::string UNIQUE_ID = "local:1";
2619
2620 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi);
2621 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2622 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2623 // Device should be disabled because it is associated with a specific display via
2624 // input port <-> display port association, but the corresponding display is not found
2625 ASSERT_FALSE(mDevice->isEnabled());
2626
2627 // Prepare displays.
2628 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002629 DISPLAY_ORIENTATION_0, true /*isActive*/, UNIQUE_ID, hdmi,
2630 ViewportType::INTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002631 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2632 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2633 ASSERT_TRUE(mDevice->isEnabled());
2634
2635 // Device should be disabled after set disable.
2636 mFakePolicy->addDisabledDevice(mDevice->getId());
2637 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2638 InputReaderConfiguration::CHANGE_ENABLED_STATE);
2639 ASSERT_FALSE(mDevice->isEnabled());
2640
2641 // Device should still be disabled even found the associated display.
2642 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2643 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2644 ASSERT_FALSE(mDevice->isEnabled());
2645}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002646
Christine Franks1ba71cc2021-04-07 14:37:42 -07002647TEST_F(InputDeviceTest, Configure_AssignsDisplayUniqueId) {
2648 // Device should be enabled by default.
2649 mFakePolicy->clearViewports();
2650 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
2651 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
2652 ASSERT_TRUE(mDevice->isEnabled());
2653
2654 // Device should be disabled because it is associated with a specific display, but the
2655 // corresponding display is not found.
2656 const std::string DISPLAY_UNIQUE_ID = "displayUniqueId";
2657 mFakePolicy->addInputUniqueIdAssociation(DEVICE_NAME, DISPLAY_UNIQUE_ID);
2658 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2659 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2660 ASSERT_FALSE(mDevice->isEnabled());
2661
2662 // Device should be enabled when a display is found.
2663 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2664 DISPLAY_ORIENTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
2665 NO_PORT, ViewportType::INTERNAL);
2666 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2667 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2668 ASSERT_TRUE(mDevice->isEnabled());
2669
2670 // Device should be disabled after set disable.
2671 mFakePolicy->addDisabledDevice(mDevice->getId());
2672 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2673 InputReaderConfiguration::CHANGE_ENABLED_STATE);
2674 ASSERT_FALSE(mDevice->isEnabled());
2675
2676 // Device should still be disabled even found the associated display.
2677 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2678 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2679 ASSERT_FALSE(mDevice->isEnabled());
2680}
2681
Michael Wrightd02c5b62014-02-10 15:10:22 -08002682// --- InputMapperTest ---
2683
2684class InputMapperTest : public testing::Test {
2685protected:
2686 static const char* DEVICE_NAME;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002687 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002688 static const int32_t DEVICE_ID;
2689 static const int32_t DEVICE_GENERATION;
2690 static const int32_t DEVICE_CONTROLLER_NUMBER;
Chris Ye1b0c7342020-07-28 21:57:03 -07002691 static const Flags<InputDeviceClass> DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002692 static const int32_t EVENTHUB_ID;
Prabir Pradhanc14266f2021-05-12 15:56:24 -07002693 static const std::optional<bool> INITIAL_PER_WINDOW_INPUT_ROTATION_FLAG_VALUE;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002694
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002695 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002696 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08002697 sp<TestInputListener> mFakeListener;
arthurhungdcef2dc2020-08-11 14:47:50 +08002698 std::unique_ptr<InstrumentedInputReader> mReader;
2699 std::shared_ptr<InputDevice> mDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002700
Chris Ye1b0c7342020-07-28 21:57:03 -07002701 virtual void SetUp(Flags<InputDeviceClass> classes) {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002702 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002703 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08002704 mFakeListener = new TestInputListener();
arthurhungdcef2dc2020-08-11 14:47:50 +08002705 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
2706 mFakeListener);
2707 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002708 }
2709
Prabir Pradhanc14266f2021-05-12 15:56:24 -07002710 void SetUp() override {
2711 // Ensure per_window_input_rotation is enabled.
2712 sysprop::InputFlingerProperties::per_window_input_rotation(true);
2713
2714 SetUp(DEVICE_CLASSES);
2715 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002716
Chris Yea52ade12020-08-27 16:49:20 -07002717 void TearDown() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002718 mFakeListener.clear();
2719 mFakePolicy.clear();
Prabir Pradhanc14266f2021-05-12 15:56:24 -07002720
2721 sysprop::InputFlingerProperties::per_window_input_rotation(
2722 INITIAL_PER_WINDOW_INPUT_ROTATION_FLAG_VALUE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002723 }
2724
2725 void addConfigurationProperty(const char* key, const char* value) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002726 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, String8(key), String8(value));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002727 }
2728
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002729 void configureDevice(uint32_t changes) {
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -08002730 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
arthurhungdcef2dc2020-08-11 14:47:50 +08002731 mReader->requestRefreshConfiguration(changes);
2732 mReader->loopOnce();
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -08002733 }
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002734 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
2735 }
2736
arthurhungdcef2dc2020-08-11 14:47:50 +08002737 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
2738 const std::string& location, int32_t eventHubId,
2739 Flags<InputDeviceClass> classes) {
2740 InputDeviceIdentifier identifier;
2741 identifier.name = name;
2742 identifier.location = location;
2743 std::shared_ptr<InputDevice> device =
2744 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
2745 identifier);
2746 mReader->pushNextDevice(device);
2747 mFakeEventHub->addDevice(eventHubId, name, classes);
2748 mReader->loopOnce();
2749 return device;
2750 }
2751
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002752 template <class T, typename... Args>
2753 T& addMapperAndConfigure(Args... args) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002754 T& mapper = mDevice->addMapper<T>(EVENTHUB_ID, args...);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002755 configureDevice(0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002756 mDevice->reset(ARBITRARY_TIME);
Chris Ye42b06822020-08-07 11:39:33 -07002757 mapper.reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002758 return mapper;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002759 }
2760
2761 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002762 int32_t orientation, const std::string& uniqueId,
2763 std::optional<uint8_t> physicalPort, ViewportType viewportType) {
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002764 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, true /*isActive*/,
2765 uniqueId, physicalPort, viewportType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07002766 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2767 }
2768
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002769 void clearViewports() {
2770 mFakePolicy->clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002771 }
2772
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002773 void process(InputMapper& mapper, nsecs_t when, nsecs_t readTime, int32_t type, int32_t code,
2774 int32_t value) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002775 RawEvent event;
2776 event.when = when;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002777 event.readTime = readTime;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002778 event.deviceId = mapper.getDeviceContext().getEventHubId();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002779 event.type = type;
2780 event.code = code;
2781 event.value = value;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002782 mapper.process(&event);
arthurhungdcef2dc2020-08-11 14:47:50 +08002783 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002784 }
2785
2786 static void assertMotionRange(const InputDeviceInfo& info,
2787 int32_t axis, uint32_t source, float min, float max, float flat, float fuzz) {
2788 const InputDeviceInfo::MotionRange* range = info.getMotionRange(axis, source);
Yi Kong9b14ac62018-07-17 13:48:38 -07002789 ASSERT_TRUE(range != nullptr) << "Axis: " << axis << " Source: " << source;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002790 ASSERT_EQ(axis, range->axis) << "Axis: " << axis << " Source: " << source;
2791 ASSERT_EQ(source, range->source) << "Axis: " << axis << " Source: " << source;
2792 ASSERT_NEAR(min, range->min, EPSILON) << "Axis: " << axis << " Source: " << source;
2793 ASSERT_NEAR(max, range->max, EPSILON) << "Axis: " << axis << " Source: " << source;
2794 ASSERT_NEAR(flat, range->flat, EPSILON) << "Axis: " << axis << " Source: " << source;
2795 ASSERT_NEAR(fuzz, range->fuzz, EPSILON) << "Axis: " << axis << " Source: " << source;
2796 }
2797
Prabir Pradhanf5334b82021-05-13 14:00:39 -07002798 static void assertPointerCoords(const PointerCoords& coords, float x, float y, float pressure,
2799 float size, float touchMajor, float touchMinor, float toolMajor,
2800 float toolMinor, float orientation, float distance,
2801 float scaledAxisEpsilon = 1.f) {
2802 ASSERT_NEAR(x, coords.getAxisValue(AMOTION_EVENT_AXIS_X), scaledAxisEpsilon);
2803 ASSERT_NEAR(y, coords.getAxisValue(AMOTION_EVENT_AXIS_Y), scaledAxisEpsilon);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002804 ASSERT_NEAR(pressure, coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), EPSILON);
2805 ASSERT_NEAR(size, coords.getAxisValue(AMOTION_EVENT_AXIS_SIZE), EPSILON);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07002806 ASSERT_NEAR(touchMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR),
2807 scaledAxisEpsilon);
2808 ASSERT_NEAR(touchMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR),
2809 scaledAxisEpsilon);
2810 ASSERT_NEAR(toolMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR),
2811 scaledAxisEpsilon);
2812 ASSERT_NEAR(toolMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR),
2813 scaledAxisEpsilon);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002814 ASSERT_NEAR(orientation, coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION), EPSILON);
2815 ASSERT_NEAR(distance, coords.getAxisValue(AMOTION_EVENT_AXIS_DISTANCE), EPSILON);
2816 }
2817
Michael Wright17db18e2020-06-26 20:51:44 +01002818 static void assertPosition(const FakePointerController& controller, float x, float y) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002819 float actualX, actualY;
Michael Wright17db18e2020-06-26 20:51:44 +01002820 controller.getPosition(&actualX, &actualY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002821 ASSERT_NEAR(x, actualX, 1);
2822 ASSERT_NEAR(y, actualY, 1);
2823 }
2824};
2825
2826const char* InputMapperTest::DEVICE_NAME = "device";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002827const char* InputMapperTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002828const int32_t InputMapperTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002829const int32_t InputMapperTest::DEVICE_GENERATION = 2;
2830const int32_t InputMapperTest::DEVICE_CONTROLLER_NUMBER = 0;
Chris Ye1b0c7342020-07-28 21:57:03 -07002831const Flags<InputDeviceClass> InputMapperTest::DEVICE_CLASSES =
2832 Flags<InputDeviceClass>(0); // not needed for current tests
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002833const int32_t InputMapperTest::EVENTHUB_ID = 1;
Prabir Pradhanc14266f2021-05-12 15:56:24 -07002834const std::optional<bool> InputMapperTest::INITIAL_PER_WINDOW_INPUT_ROTATION_FLAG_VALUE =
2835 sysprop::InputFlingerProperties::per_window_input_rotation();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002836
2837// --- SwitchInputMapperTest ---
2838
2839class SwitchInputMapperTest : public InputMapperTest {
2840protected:
2841};
2842
2843TEST_F(SwitchInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002844 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002845
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002846 ASSERT_EQ(uint32_t(AINPUT_SOURCE_SWITCH), mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002847}
2848
2849TEST_F(SwitchInputMapperTest, GetSwitchState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002850 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002851
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002852 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002853 ASSERT_EQ(1, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002854
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002855 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002856 ASSERT_EQ(0, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002857}
2858
2859TEST_F(SwitchInputMapperTest, Process) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002860 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002861
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002862 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_LID, 1);
2863 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_JACK_PHYSICAL_INSERT, 1);
2864 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_HEADPHONE_INSERT, 0);
2865 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002866
2867 NotifySwitchArgs args;
2868 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySwitchWasCalled(&args));
2869 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
Dan Albert1bd2fc02016-02-02 15:11:57 -08002870 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT), args.switchValues);
2871 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT) | (1 << SW_HEADPHONE_INSERT),
Michael Wrightd02c5b62014-02-10 15:10:22 -08002872 args.switchMask);
2873 ASSERT_EQ(uint32_t(0), args.policyFlags);
2874}
2875
Chris Ye87143712020-11-10 05:05:58 +00002876// --- VibratorInputMapperTest ---
2877class VibratorInputMapperTest : public InputMapperTest {
2878protected:
2879 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::VIBRATOR); }
2880};
2881
2882TEST_F(VibratorInputMapperTest, GetSources) {
2883 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
2884
2885 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mapper.getSources());
2886}
2887
2888TEST_F(VibratorInputMapperTest, GetVibratorIds) {
2889 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
2890
2891 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
2892}
2893
2894TEST_F(VibratorInputMapperTest, Vibrate) {
2895 constexpr uint8_t DEFAULT_AMPLITUDE = 192;
Chris Yefb552902021-02-03 17:18:37 -08002896 constexpr int32_t VIBRATION_TOKEN = 100;
Chris Ye87143712020-11-10 05:05:58 +00002897 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
2898
2899 VibrationElement pattern(2);
2900 VibrationSequence sequence(2);
2901 pattern.duration = std::chrono::milliseconds(200);
2902 pattern.channels = {{0 /* vibratorId */, DEFAULT_AMPLITUDE / 2},
2903 {1 /* vibratorId */, DEFAULT_AMPLITUDE}};
2904 sequence.addElement(pattern);
2905 pattern.duration = std::chrono::milliseconds(500);
2906 pattern.channels = {{0 /* vibratorId */, DEFAULT_AMPLITUDE / 4},
2907 {1 /* vibratorId */, DEFAULT_AMPLITUDE}};
2908 sequence.addElement(pattern);
2909
2910 std::vector<int64_t> timings = {0, 1};
2911 std::vector<uint8_t> amplitudes = {DEFAULT_AMPLITUDE, DEFAULT_AMPLITUDE / 2};
2912
2913 ASSERT_FALSE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08002914 // Start vibrating
2915 mapper.vibrate(sequence, -1 /* repeat */, VIBRATION_TOKEN);
Chris Ye87143712020-11-10 05:05:58 +00002916 ASSERT_TRUE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08002917 // Verify vibrator state listener was notified.
2918 mReader->loopOnce();
2919 NotifyVibratorStateArgs args;
2920 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyVibratorStateWasCalled(&args));
2921 ASSERT_EQ(DEVICE_ID, args.deviceId);
2922 ASSERT_TRUE(args.isOn);
2923 // Stop vibrating
2924 mapper.cancelVibrate(VIBRATION_TOKEN);
2925 ASSERT_FALSE(mapper.isVibrating());
2926 // Verify vibrator state listener was notified.
2927 mReader->loopOnce();
2928 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyVibratorStateWasCalled(&args));
2929 ASSERT_EQ(DEVICE_ID, args.deviceId);
2930 ASSERT_FALSE(args.isOn);
Chris Ye87143712020-11-10 05:05:58 +00002931}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002932
Chris Yef59a2f42020-10-16 12:55:26 -07002933// --- SensorInputMapperTest ---
2934
2935class SensorInputMapperTest : public InputMapperTest {
2936protected:
2937 static const int32_t ACCEL_RAW_MIN;
2938 static const int32_t ACCEL_RAW_MAX;
2939 static const int32_t ACCEL_RAW_FUZZ;
2940 static const int32_t ACCEL_RAW_FLAT;
2941 static const int32_t ACCEL_RAW_RESOLUTION;
2942
2943 static const int32_t GYRO_RAW_MIN;
2944 static const int32_t GYRO_RAW_MAX;
2945 static const int32_t GYRO_RAW_FUZZ;
2946 static const int32_t GYRO_RAW_FLAT;
2947 static const int32_t GYRO_RAW_RESOLUTION;
2948
2949 static const float GRAVITY_MS2_UNIT;
2950 static const float DEGREE_RADIAN_UNIT;
2951
2952 void prepareAccelAxes();
2953 void prepareGyroAxes();
2954 void setAccelProperties();
2955 void setGyroProperties();
2956 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::SENSOR); }
2957};
2958
2959const int32_t SensorInputMapperTest::ACCEL_RAW_MIN = -32768;
2960const int32_t SensorInputMapperTest::ACCEL_RAW_MAX = 32768;
2961const int32_t SensorInputMapperTest::ACCEL_RAW_FUZZ = 16;
2962const int32_t SensorInputMapperTest::ACCEL_RAW_FLAT = 0;
2963const int32_t SensorInputMapperTest::ACCEL_RAW_RESOLUTION = 8192;
2964
2965const int32_t SensorInputMapperTest::GYRO_RAW_MIN = -2097152;
2966const int32_t SensorInputMapperTest::GYRO_RAW_MAX = 2097152;
2967const int32_t SensorInputMapperTest::GYRO_RAW_FUZZ = 16;
2968const int32_t SensorInputMapperTest::GYRO_RAW_FLAT = 0;
2969const int32_t SensorInputMapperTest::GYRO_RAW_RESOLUTION = 1024;
2970
2971const float SensorInputMapperTest::GRAVITY_MS2_UNIT = 9.80665f;
2972const float SensorInputMapperTest::DEGREE_RADIAN_UNIT = 0.0174533f;
2973
2974void SensorInputMapperTest::prepareAccelAxes() {
2975 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
2976 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
2977 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
2978 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
2979 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Z, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
2980 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
2981}
2982
2983void SensorInputMapperTest::prepareGyroAxes() {
2984 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RX, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
2985 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
2986 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RY, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
2987 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
2988 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RZ, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
2989 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
2990}
2991
2992void SensorInputMapperTest::setAccelProperties() {
2993 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 0, InputDeviceSensorType::ACCELEROMETER,
2994 /* sensorDataIndex */ 0);
2995 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 1, InputDeviceSensorType::ACCELEROMETER,
2996 /* sensorDataIndex */ 1);
2997 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 2, InputDeviceSensorType::ACCELEROMETER,
2998 /* sensorDataIndex */ 2);
2999 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
3000 addConfigurationProperty("sensor.accelerometer.reportingMode", "0");
3001 addConfigurationProperty("sensor.accelerometer.maxDelay", "100000");
3002 addConfigurationProperty("sensor.accelerometer.minDelay", "5000");
3003 addConfigurationProperty("sensor.accelerometer.power", "1.5");
3004}
3005
3006void SensorInputMapperTest::setGyroProperties() {
3007 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 3, InputDeviceSensorType::GYROSCOPE,
3008 /* sensorDataIndex */ 0);
3009 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 4, InputDeviceSensorType::GYROSCOPE,
3010 /* sensorDataIndex */ 1);
3011 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 5, InputDeviceSensorType::GYROSCOPE,
3012 /* sensorDataIndex */ 2);
3013 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
3014 addConfigurationProperty("sensor.gyroscope.reportingMode", "0");
3015 addConfigurationProperty("sensor.gyroscope.maxDelay", "100000");
3016 addConfigurationProperty("sensor.gyroscope.minDelay", "5000");
3017 addConfigurationProperty("sensor.gyroscope.power", "0.8");
3018}
3019
3020TEST_F(SensorInputMapperTest, GetSources) {
3021 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3022
3023 ASSERT_EQ(static_cast<uint32_t>(AINPUT_SOURCE_SENSOR), mapper.getSources());
3024}
3025
3026TEST_F(SensorInputMapperTest, ProcessAccelerometerSensor) {
3027 setAccelProperties();
3028 prepareAccelAxes();
3029 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3030
3031 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::ACCELEROMETER,
3032 std::chrono::microseconds(10000),
3033 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08003034 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003035 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, 20000);
3036 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, -20000);
3037 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Z, 40000);
3038 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
3039 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07003040
3041 NotifySensorArgs args;
3042 std::vector<float> values = {20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
3043 -20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
3044 40000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT};
3045
3046 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
3047 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
3048 ASSERT_EQ(args.deviceId, DEVICE_ID);
3049 ASSERT_EQ(args.sensorType, InputDeviceSensorType::ACCELEROMETER);
3050 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
3051 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
3052 ASSERT_EQ(args.values, values);
3053 mapper.flushSensor(InputDeviceSensorType::ACCELEROMETER);
3054}
3055
3056TEST_F(SensorInputMapperTest, ProcessGyroscopeSensor) {
3057 setGyroProperties();
3058 prepareGyroAxes();
3059 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3060
3061 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::GYROSCOPE,
3062 std::chrono::microseconds(10000),
3063 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08003064 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003065 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RX, 20000);
3066 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RY, -20000);
3067 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RZ, 40000);
3068 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
3069 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07003070
3071 NotifySensorArgs args;
3072 std::vector<float> values = {20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
3073 -20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
3074 40000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT};
3075
3076 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
3077 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
3078 ASSERT_EQ(args.deviceId, DEVICE_ID);
3079 ASSERT_EQ(args.sensorType, InputDeviceSensorType::GYROSCOPE);
3080 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
3081 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
3082 ASSERT_EQ(args.values, values);
3083 mapper.flushSensor(InputDeviceSensorType::GYROSCOPE);
3084}
3085
Michael Wrightd02c5b62014-02-10 15:10:22 -08003086// --- KeyboardInputMapperTest ---
3087
3088class KeyboardInputMapperTest : public InputMapperTest {
3089protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003090 const std::string UNIQUE_ID = "local:0";
3091
3092 void prepareDisplay(int32_t orientation);
3093
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003094 void testDPadKeyRotation(KeyboardInputMapper& mapper, int32_t originalScanCode,
Arthur Hung2c9a3342019-07-23 14:18:59 +08003095 int32_t originalKeyCode, int32_t rotatedKeyCode,
3096 int32_t displayId = ADISPLAY_ID_NONE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003097};
3098
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003099/* Similar to setDisplayInfoAndReconfigure, but pre-populates all parameters except for the
3100 * orientation.
3101 */
3102void KeyboardInputMapperTest::prepareDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003103 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
3104 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003105}
3106
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003107void KeyboardInputMapperTest::testDPadKeyRotation(KeyboardInputMapper& mapper,
Arthur Hung2c9a3342019-07-23 14:18:59 +08003108 int32_t originalScanCode, int32_t originalKeyCode,
3109 int32_t rotatedKeyCode, int32_t displayId) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003110 NotifyKeyArgs args;
3111
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003112 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003113 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3114 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3115 ASSERT_EQ(originalScanCode, args.scanCode);
3116 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003117 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003118
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003119 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003120 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3121 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3122 ASSERT_EQ(originalScanCode, args.scanCode);
3123 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003124 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003125}
3126
Michael Wrightd02c5b62014-02-10 15:10:22 -08003127TEST_F(KeyboardInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003128 KeyboardInputMapper& mapper =
3129 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3130 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003131
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003132 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003133}
3134
3135TEST_F(KeyboardInputMapperTest, Process_SimpleKeyPress) {
3136 const int32_t USAGE_A = 0x070004;
3137 const int32_t USAGE_UNKNOWN = 0x07ffff;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003138 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3139 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
Chris Yea52ade12020-08-27 16:49:20 -07003140 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, POLICY_FLAG_WAKE);
3141 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, POLICY_FLAG_WAKE);
3142 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003143
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003144 KeyboardInputMapper& mapper =
3145 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3146 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
arthurhungc903df12020-08-11 15:08:42 +08003147 // Initial metastate to AMETA_NONE.
3148 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
3149 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003150
3151 // Key down by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003152 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003153 NotifyKeyArgs args;
3154 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3155 ASSERT_EQ(DEVICE_ID, args.deviceId);
3156 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3157 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3158 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3159 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3160 ASSERT_EQ(KEY_HOME, args.scanCode);
3161 ASSERT_EQ(AMETA_NONE, args.metaState);
3162 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3163 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3164 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3165
3166 // Key up by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003167 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003168 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3169 ASSERT_EQ(DEVICE_ID, args.deviceId);
3170 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3171 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3172 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3173 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3174 ASSERT_EQ(KEY_HOME, args.scanCode);
3175 ASSERT_EQ(AMETA_NONE, args.metaState);
3176 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3177 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3178 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3179
3180 // Key down by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003181 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3182 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, 0, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003183 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3184 ASSERT_EQ(DEVICE_ID, args.deviceId);
3185 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3186 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3187 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3188 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3189 ASSERT_EQ(0, args.scanCode);
3190 ASSERT_EQ(AMETA_NONE, args.metaState);
3191 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3192 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3193 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3194
3195 // Key up by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003196 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3197 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003198 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3199 ASSERT_EQ(DEVICE_ID, args.deviceId);
3200 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3201 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3202 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3203 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3204 ASSERT_EQ(0, args.scanCode);
3205 ASSERT_EQ(AMETA_NONE, args.metaState);
3206 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3207 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3208 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3209
3210 // Key down with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003211 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3212 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UNKNOWN, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003213 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3214 ASSERT_EQ(DEVICE_ID, args.deviceId);
3215 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3216 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3217 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3218 ASSERT_EQ(0, args.keyCode);
3219 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3220 ASSERT_EQ(AMETA_NONE, args.metaState);
3221 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3222 ASSERT_EQ(0U, args.policyFlags);
3223 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3224
3225 // Key up with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003226 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3227 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003228 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3229 ASSERT_EQ(DEVICE_ID, args.deviceId);
3230 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3231 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3232 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3233 ASSERT_EQ(0, args.keyCode);
3234 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3235 ASSERT_EQ(AMETA_NONE, args.metaState);
3236 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3237 ASSERT_EQ(0U, args.policyFlags);
3238 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3239}
3240
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003241/**
3242 * Ensure that the readTime is set to the time when the EV_KEY is received.
3243 */
3244TEST_F(KeyboardInputMapperTest, Process_SendsReadTime) {
3245 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3246
3247 KeyboardInputMapper& mapper =
3248 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3249 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3250 NotifyKeyArgs args;
3251
3252 // Key down
3253 process(mapper, ARBITRARY_TIME, 12 /*readTime*/, EV_KEY, KEY_HOME, 1);
3254 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3255 ASSERT_EQ(12, args.readTime);
3256
3257 // Key up
3258 process(mapper, ARBITRARY_TIME, 15 /*readTime*/, EV_KEY, KEY_HOME, 1);
3259 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3260 ASSERT_EQ(15, args.readTime);
3261}
3262
Michael Wrightd02c5b62014-02-10 15:10:22 -08003263TEST_F(KeyboardInputMapperTest, Process_ShouldUpdateMetaState) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003264 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFTSHIFT, 0, AKEYCODE_SHIFT_LEFT, 0);
3265 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003266 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, 0);
3267 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, 0);
3268 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003269
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003270 KeyboardInputMapper& mapper =
3271 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3272 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003273
arthurhungc903df12020-08-11 15:08:42 +08003274 // Initial metastate to AMETA_NONE.
3275 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
3276 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003277
3278 // Metakey down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003279 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003280 NotifyKeyArgs args;
3281 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3282 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003283 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003284 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003285
3286 // Key down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003287 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_A, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003288 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3289 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003290 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003291
3292 // Key up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003293 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, KEY_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003294 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3295 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003296 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003297
3298 // Metakey up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003299 process(mapper, ARBITRARY_TIME + 3, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003300 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3301 ASSERT_EQ(AMETA_NONE, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003302 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003303 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003304}
3305
3306TEST_F(KeyboardInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003307 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3308 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3309 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3310 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003311
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003312 KeyboardInputMapper& mapper =
3313 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3314 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003315
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003316 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003317 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3318 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
3319 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3320 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
3321 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3322 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
3323 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3324 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
3325}
3326
3327TEST_F(KeyboardInputMapperTest, Process_WhenOrientationAware_ShouldRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003328 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3329 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3330 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3331 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003332
Michael Wrightd02c5b62014-02-10 15:10:22 -08003333 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003334 KeyboardInputMapper& mapper =
3335 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3336 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003337
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003338 prepareDisplay(DISPLAY_ORIENTATION_0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003339 ASSERT_NO_FATAL_FAILURE(
3340 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3341 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3342 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3343 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3344 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3345 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3346 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003347
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003348 clearViewports();
3349 prepareDisplay(DISPLAY_ORIENTATION_90);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003350 ASSERT_NO_FATAL_FAILURE(
3351 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3352 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3353 AKEYCODE_DPAD_UP, DISPLAY_ID));
3354 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3355 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3356 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3357 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003358
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003359 clearViewports();
3360 prepareDisplay(DISPLAY_ORIENTATION_180);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003361 ASSERT_NO_FATAL_FAILURE(
3362 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3363 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3364 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3365 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3366 AKEYCODE_DPAD_UP, DISPLAY_ID));
3367 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3368 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003369
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003370 clearViewports();
3371 prepareDisplay(DISPLAY_ORIENTATION_270);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003372 ASSERT_NO_FATAL_FAILURE(
3373 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3374 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3375 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3376 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3377 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3378 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3379 AKEYCODE_DPAD_UP, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003380
3381 // Special case: if orientation changes while key is down, we still emit the same keycode
3382 // in the key up as we did in the key down.
3383 NotifyKeyArgs args;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003384 clearViewports();
3385 prepareDisplay(DISPLAY_ORIENTATION_270);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003386 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003387 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3388 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3389 ASSERT_EQ(KEY_UP, args.scanCode);
3390 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3391
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003392 clearViewports();
3393 prepareDisplay(DISPLAY_ORIENTATION_180);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003394 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003395 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3396 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3397 ASSERT_EQ(KEY_UP, args.scanCode);
3398 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3399}
3400
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003401TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_NotOrientationAware) {
3402 // If the keyboard is not orientation aware,
3403 // key events should not be associated with a specific display id
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003404 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003405
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003406 KeyboardInputMapper& mapper =
3407 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3408 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003409 NotifyKeyArgs args;
3410
3411 // Display id should be ADISPLAY_ID_NONE without any display configuration.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003412 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003413 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003414 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003415 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3416 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3417
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003418 prepareDisplay(DISPLAY_ORIENTATION_0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003419 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003420 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003421 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003422 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3423 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3424}
3425
3426TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_OrientationAware) {
3427 // If the keyboard is orientation aware,
3428 // key events should be associated with the internal viewport
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003429 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003430
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003431 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003432 KeyboardInputMapper& mapper =
3433 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3434 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003435 NotifyKeyArgs args;
3436
3437 // Display id should be ADISPLAY_ID_NONE without any display configuration.
3438 // ^--- already checked by the previous test
3439
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003440 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003441 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003442 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003443 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003444 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003445 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3446 ASSERT_EQ(DISPLAY_ID, args.displayId);
3447
3448 constexpr int32_t newDisplayId = 2;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003449 clearViewports();
3450 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003451 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003452 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003453 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003454 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003455 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3456 ASSERT_EQ(newDisplayId, args.displayId);
3457}
3458
Michael Wrightd02c5b62014-02-10 15:10:22 -08003459TEST_F(KeyboardInputMapperTest, GetKeyCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003460 KeyboardInputMapper& mapper =
3461 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3462 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003463
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003464 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003465 ASSERT_EQ(1, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003466
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003467 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003468 ASSERT_EQ(0, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003469}
3470
3471TEST_F(KeyboardInputMapperTest, GetScanCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003472 KeyboardInputMapper& mapper =
3473 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3474 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003475
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003476 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003477 ASSERT_EQ(1, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003478
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003479 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003480 ASSERT_EQ(0, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003481}
3482
3483TEST_F(KeyboardInputMapperTest, MarkSupportedKeyCodes) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003484 KeyboardInputMapper& mapper =
3485 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3486 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003487
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003488 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003489
3490 const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
3491 uint8_t flags[2] = { 0, 0 };
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003492 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, 1, keyCodes, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003493 ASSERT_TRUE(flags[0]);
3494 ASSERT_FALSE(flags[1]);
3495}
3496
3497TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleMetaStateAndLeds) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003498 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3499 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3500 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3501 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3502 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3503 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003504
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003505 KeyboardInputMapper& mapper =
3506 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3507 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Chris Yea52ade12020-08-27 16:49:20 -07003508 // Initialize metastate to AMETA_NUM_LOCK_ON.
arthurhungc903df12020-08-11 15:08:42 +08003509 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
3510 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003511
3512 // Initialization should have turned all of the lights off.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003513 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3514 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3515 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003516
3517 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003518 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3519 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003520 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3521 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3522 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003523 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003524
3525 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003526 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3527 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003528 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3529 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3530 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003531 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003532
3533 // Toggle caps lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003534 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3535 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003536 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3537 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3538 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003539 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003540
3541 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003542 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3543 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003544 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3545 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3546 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003547 ASSERT_EQ(AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003548
3549 // Toggle num lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003550 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3551 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003552 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3553 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3554 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003555 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003556
3557 // Toggle scroll lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003558 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3559 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003560 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3561 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3562 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003563 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003564}
3565
Chris Yea52ade12020-08-27 16:49:20 -07003566TEST_F(KeyboardInputMapperTest, NoMetaStateWhenMetaKeysNotPresent) {
3567 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_BUTTON_A, 0);
3568 mFakeEventHub->addKey(EVENTHUB_ID, BTN_B, 0, AKEYCODE_BUTTON_B, 0);
3569 mFakeEventHub->addKey(EVENTHUB_ID, BTN_X, 0, AKEYCODE_BUTTON_X, 0);
3570 mFakeEventHub->addKey(EVENTHUB_ID, BTN_Y, 0, AKEYCODE_BUTTON_Y, 0);
3571
3572 KeyboardInputMapper& mapper =
3573 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3574 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
3575
3576 // Initial metastate should be AMETA_NONE as no meta keys added.
3577 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3578 // Meta state should be AMETA_NONE after reset
3579 mapper.reset(ARBITRARY_TIME);
3580 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3581 // Meta state should be AMETA_NONE with update, as device doesn't have the keys.
3582 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
3583 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3584
3585 NotifyKeyArgs args;
3586 // Press button "A"
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003587 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_A, 1);
Chris Yea52ade12020-08-27 16:49:20 -07003588 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3589 ASSERT_EQ(AMETA_NONE, args.metaState);
3590 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3591 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3592 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3593
3594 // Button up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003595 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003596 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3597 ASSERT_EQ(AMETA_NONE, args.metaState);
3598 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3599 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3600 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3601}
3602
Arthur Hung2c9a3342019-07-23 14:18:59 +08003603TEST_F(KeyboardInputMapperTest, Configure_AssignsDisplayPort) {
3604 // keyboard 1.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003605 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3606 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3607 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3608 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003609
3610 // keyboard 2.
3611 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08003612 const std::string DEVICE_NAME2 = "KEYBOARD2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08003613 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003614 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08003615 std::shared_ptr<InputDevice> device2 =
3616 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
3617 Flags<InputDeviceClass>(0));
3618
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003619 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3620 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3621 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3622 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003623
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003624 KeyboardInputMapper& mapper =
3625 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3626 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003627
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003628 KeyboardInputMapper& mapper2 =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003629 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003630 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003631 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
3632 device2->reset(ARBITRARY_TIME);
3633
3634 // Prepared displays and associated info.
3635 constexpr uint8_t hdmi1 = 0;
3636 constexpr uint8_t hdmi2 = 1;
3637 const std::string SECONDARY_UNIQUE_ID = "local:1";
3638
3639 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
3640 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
3641
3642 // No associated display viewport found, should disable the device.
3643 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3644 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3645 ASSERT_FALSE(device2->isEnabled());
3646
3647 // Prepare second display.
3648 constexpr int32_t newDisplayId = 2;
3649 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003650 UNIQUE_ID, hdmi1, ViewportType::INTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003651 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003652 SECONDARY_UNIQUE_ID, hdmi2, ViewportType::EXTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003653 // Default device will reconfigure above, need additional reconfiguration for another device.
3654 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3655 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3656
3657 // Device should be enabled after the associated display is found.
3658 ASSERT_TRUE(mDevice->isEnabled());
3659 ASSERT_TRUE(device2->isEnabled());
3660
3661 // Test pad key events
3662 ASSERT_NO_FATAL_FAILURE(
3663 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3664 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3665 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3666 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3667 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3668 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3669 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3670
3671 ASSERT_NO_FATAL_FAILURE(
3672 testDPadKeyRotation(mapper2, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, newDisplayId));
3673 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3674 AKEYCODE_DPAD_RIGHT, newDisplayId));
3675 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3676 AKEYCODE_DPAD_DOWN, newDisplayId));
3677 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3678 AKEYCODE_DPAD_LEFT, newDisplayId));
3679}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003680
arthurhungc903df12020-08-11 15:08:42 +08003681TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleAfterReattach) {
3682 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3683 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3684 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3685 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3686 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3687 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3688
3689 KeyboardInputMapper& mapper =
3690 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3691 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3692 // Initial metastate to AMETA_NONE.
3693 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
3694 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
3695
3696 // Initialization should have turned all of the lights off.
3697 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3698 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3699 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3700
3701 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003702 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3703 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003704 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3705 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
3706
3707 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003708 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3709 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003710 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3711 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
3712
3713 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003714 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3715 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003716 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3717 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
3718
3719 mFakeEventHub->removeDevice(EVENTHUB_ID);
3720 mReader->loopOnce();
3721
3722 // keyboard 2 should default toggle keys.
3723 const std::string USB2 = "USB2";
3724 const std::string DEVICE_NAME2 = "KEYBOARD2";
3725 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
3726 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
3727 std::shared_ptr<InputDevice> device2 =
3728 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
3729 Flags<InputDeviceClass>(0));
3730 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3731 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
3732 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3733 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3734 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3735 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3736
arthurhung6fe95782020-10-05 22:41:16 +08003737 KeyboardInputMapper& mapper2 =
3738 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
3739 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
arthurhungc903df12020-08-11 15:08:42 +08003740 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
3741 device2->reset(ARBITRARY_TIME);
3742
3743 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_CAPSL));
3744 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_NUML));
3745 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_SCROLLL));
arthurhung6fe95782020-10-05 22:41:16 +08003746 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON,
3747 mapper2.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08003748}
3749
Arthur Hungcb40a002021-08-03 14:31:01 +00003750TEST_F(KeyboardInputMapperTest, Process_toggleCapsLockState) {
3751 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3752 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3753 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3754
3755 // Suppose we have two mappers. (DPAD + KEYBOARD)
3756 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_DPAD,
3757 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
3758 KeyboardInputMapper& mapper =
3759 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3760 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3761 // Initialize metastate to AMETA_NUM_LOCK_ON.
3762 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
3763 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
3764
3765 mReader->toggleCapsLockState(DEVICE_ID);
3766 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
3767}
3768
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003769// --- KeyboardInputMapperTest_ExternalDevice ---
3770
3771class KeyboardInputMapperTest_ExternalDevice : public InputMapperTest {
3772protected:
Chris Yea52ade12020-08-27 16:49:20 -07003773 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003774};
3775
3776TEST_F(KeyboardInputMapperTest_ExternalDevice, WakeBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07003777 // For external devices, non-media keys will trigger wake on key down. Media keys need to be
3778 // marked as WAKE in the keylayout file to trigger wake.
Powei Fengd041c5d2019-05-03 17:11:33 -07003779
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003780 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, 0);
3781 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, 0);
3782 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAYPAUSE, 0, AKEYCODE_MEDIA_PLAY_PAUSE,
3783 POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07003784
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003785 KeyboardInputMapper& mapper =
3786 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3787 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07003788
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003789 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003790 NotifyKeyArgs args;
3791 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3792 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3793
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003794 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003795 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3796 ASSERT_EQ(uint32_t(0), args.policyFlags);
3797
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003798 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003799 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3800 ASSERT_EQ(uint32_t(0), args.policyFlags);
3801
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003802 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003803 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3804 ASSERT_EQ(uint32_t(0), args.policyFlags);
3805
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003806 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003807 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3808 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3809
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003810 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003811 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3812 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3813}
3814
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003815TEST_F(KeyboardInputMapperTest_ExternalDevice, DoNotWakeByDefaultBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07003816 // Tv Remote key's wake behavior is prescribed by the keylayout file.
Powei Fengd041c5d2019-05-03 17:11:33 -07003817
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003818 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3819 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3820 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07003821
Powei Fengd041c5d2019-05-03 17:11:33 -07003822 addConfigurationProperty("keyboard.doNotWakeByDefault", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003823 KeyboardInputMapper& mapper =
3824 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3825 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07003826
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003827 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003828 NotifyKeyArgs args;
3829 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3830 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3831
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003832 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003833 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3834 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3835
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003836 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_DOWN, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003837 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3838 ASSERT_EQ(uint32_t(0), args.policyFlags);
3839
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003840 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_DOWN, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003841 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3842 ASSERT_EQ(uint32_t(0), args.policyFlags);
3843
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003844 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003845 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3846 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3847
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003848 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003849 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3850 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3851}
3852
Michael Wrightd02c5b62014-02-10 15:10:22 -08003853// --- CursorInputMapperTest ---
3854
3855class CursorInputMapperTest : public InputMapperTest {
3856protected:
3857 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD;
3858
Michael Wright17db18e2020-06-26 20:51:44 +01003859 std::shared_ptr<FakePointerController> mFakePointerController;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003860
Chris Yea52ade12020-08-27 16:49:20 -07003861 void SetUp() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003862 InputMapperTest::SetUp();
3863
Michael Wright17db18e2020-06-26 20:51:44 +01003864 mFakePointerController = std::make_shared<FakePointerController>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003865 mFakePolicy->setPointerController(mDevice->getId(), mFakePointerController);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003866 }
3867
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003868 void testMotionRotation(CursorInputMapper& mapper, int32_t originalX, int32_t originalY,
3869 int32_t rotatedX, int32_t rotatedY);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003870
3871 void prepareDisplay(int32_t orientation) {
3872 const std::string uniqueId = "local:0";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003873 const ViewportType viewportType = ViewportType::INTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003874 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
3875 orientation, uniqueId, NO_PORT, viewportType);
3876 }
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003877
3878 static void assertCursorPointerCoords(const PointerCoords& coords, float x, float y,
3879 float pressure) {
3880 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(coords, x, y, pressure, 0.0f, 0.0f, 0.0f, 0.0f,
3881 0.0f, 0.0f, 0.0f, EPSILON));
3882 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08003883};
3884
3885const int32_t CursorInputMapperTest::TRACKBALL_MOVEMENT_THRESHOLD = 6;
3886
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003887void CursorInputMapperTest::testMotionRotation(CursorInputMapper& mapper, int32_t originalX,
3888 int32_t originalY, int32_t rotatedX,
3889 int32_t rotatedY) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003890 NotifyMotionArgs args;
3891
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003892 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, originalX);
3893 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, originalY);
3894 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003895 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3896 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003897 ASSERT_NO_FATAL_FAILURE(
3898 assertCursorPointerCoords(args.pointerCoords[0],
3899 float(rotatedX) / TRACKBALL_MOVEMENT_THRESHOLD,
3900 float(rotatedY) / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003901}
3902
3903TEST_F(CursorInputMapperTest, WhenModeIsPointer_GetSources_ReturnsMouse) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003904 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003905 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003906
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003907 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003908}
3909
3910TEST_F(CursorInputMapperTest, WhenModeIsNavigation_GetSources_ReturnsTrackball) {
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
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003914 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003915}
3916
3917TEST_F(CursorInputMapperTest, WhenModeIsPointer_PopulateDeviceInfo_ReturnsRangeFromPointerController) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003918 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003919 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003920
3921 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003922 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003923
3924 // Initially there may not be a valid motion range.
Yi Kong9b14ac62018-07-17 13:48:38 -07003925 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE));
3926 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003927 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3928 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE, 0.0f, 1.0f, 0.0f, 0.0f));
3929
3930 // When the bounds are set, then there should be a valid motion range.
3931 mFakePointerController->setBounds(1, 2, 800 - 1, 480 - 1);
3932
3933 InputDeviceInfo info2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003934 mapper.populateDeviceInfo(&info2);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003935
3936 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3937 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE,
3938 1, 800 - 1, 0.0f, 0.0f));
3939 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3940 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE,
3941 2, 480 - 1, 0.0f, 0.0f));
3942 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3943 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE,
3944 0.0f, 1.0f, 0.0f, 0.0f));
3945}
3946
3947TEST_F(CursorInputMapperTest, WhenModeIsNavigation_PopulateDeviceInfo_ReturnsScaledRange) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003948 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003949 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003950
3951 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003952 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003953
3954 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3955 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_TRACKBALL,
3956 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
3957 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3958 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_TRACKBALL,
3959 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
3960 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3961 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TRACKBALL,
3962 0.0f, 1.0f, 0.0f, 0.0f));
3963}
3964
3965TEST_F(CursorInputMapperTest, Process_ShouldSetAllFieldsAndIncludeGlobalMetaState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003966 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003967 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003968
arthurhungdcef2dc2020-08-11 14:47:50 +08003969 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003970
3971 NotifyMotionArgs args;
3972
3973 // Button press.
3974 // Mostly testing non x/y behavior here so we don't need to check again elsewhere.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003975 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
3976 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003977 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3978 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3979 ASSERT_EQ(DEVICE_ID, args.deviceId);
3980 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3981 ASSERT_EQ(uint32_t(0), args.policyFlags);
3982 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3983 ASSERT_EQ(0, args.flags);
3984 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3985 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
3986 ASSERT_EQ(0, args.edgeFlags);
3987 ASSERT_EQ(uint32_t(1), args.pointerCount);
3988 ASSERT_EQ(0, args.pointerProperties[0].id);
3989 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003990 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003991 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3992 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3993 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3994
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003995 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3996 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3997 ASSERT_EQ(DEVICE_ID, args.deviceId);
3998 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3999 ASSERT_EQ(uint32_t(0), args.policyFlags);
4000 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
4001 ASSERT_EQ(0, args.flags);
4002 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4003 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
4004 ASSERT_EQ(0, args.edgeFlags);
4005 ASSERT_EQ(uint32_t(1), args.pointerCount);
4006 ASSERT_EQ(0, args.pointerProperties[0].id);
4007 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004008 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004009 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4010 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4011 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4012
Michael Wrightd02c5b62014-02-10 15:10:22 -08004013 // Button release. Should have same down time.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004014 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4015 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004016 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4017 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
4018 ASSERT_EQ(DEVICE_ID, args.deviceId);
4019 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4020 ASSERT_EQ(uint32_t(0), args.policyFlags);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004021 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
4022 ASSERT_EQ(0, args.flags);
4023 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4024 ASSERT_EQ(0, args.buttonState);
4025 ASSERT_EQ(0, args.edgeFlags);
4026 ASSERT_EQ(uint32_t(1), args.pointerCount);
4027 ASSERT_EQ(0, args.pointerProperties[0].id);
4028 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004029 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004030 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4031 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4032 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4033
4034 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4035 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
4036 ASSERT_EQ(DEVICE_ID, args.deviceId);
4037 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4038 ASSERT_EQ(uint32_t(0), args.policyFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004039 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
4040 ASSERT_EQ(0, args.flags);
4041 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4042 ASSERT_EQ(0, args.buttonState);
4043 ASSERT_EQ(0, args.edgeFlags);
4044 ASSERT_EQ(uint32_t(1), args.pointerCount);
4045 ASSERT_EQ(0, args.pointerProperties[0].id);
4046 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004047 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004048 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4049 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4050 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4051}
4052
4053TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentXYUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004054 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004055 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004056
4057 NotifyMotionArgs args;
4058
4059 // Motion in X but not Y.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004060 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4061 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004062 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4063 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004064 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4065 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f,
4066 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004067
4068 // Motion in Y but not X.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004069 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
4070 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004071 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4072 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004073 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f,
4074 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004075}
4076
4077TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004078 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004079 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004080
4081 NotifyMotionArgs args;
4082
4083 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004084 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4085 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004086 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4087 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004088 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004089
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004090 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4091 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004092 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004093
Michael Wrightd02c5b62014-02-10 15:10:22 -08004094 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004095 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4096 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004097 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004098 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004099 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004100
4101 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004102 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004103 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004104}
4105
4106TEST_F(CursorInputMapperTest, Process_ShouldHandleCombinedXYAndButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004107 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004108 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004109
4110 NotifyMotionArgs args;
4111
4112 // Combined X, Y and Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004113 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4114 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
4115 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4116 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004117 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4118 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004119 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4120 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4121 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004122
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004123 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4124 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004125 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4126 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4127 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004128
Michael Wrightd02c5b62014-02-10 15:10:22 -08004129 // Move X, Y a bit while pressed.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004130 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 2);
4131 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 1);
4132 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004133 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4134 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004135 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4136 2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4137 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004138
4139 // Release Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004140 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4141 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004142 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004143 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004144 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004145
4146 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004147 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004148 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004149}
4150
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004151TEST_F(CursorInputMapperTest, Process_WhenOrientationAware_ShouldNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004152 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004153 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
4154 // need to be rotated.
4155 addConfigurationProperty("cursor.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004156 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004157
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004158 prepareDisplay(DISPLAY_ORIENTATION_90);
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}
4168
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004169TEST_F(CursorInputMapperTest, Process_WhenNotOrientationAware_ShouldRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004170 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004171 // Since InputReader works in the un-rotated coordinate space, only devices that are not
4172 // orientation-aware are affected by display rotation.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004173 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004174
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004175 prepareDisplay(DISPLAY_ORIENTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004176 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4177 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4178 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4179 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4180 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4181 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4182 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4183 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4184
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004185 prepareDisplay(DISPLAY_ORIENTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004186 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, -1, 0));
4187 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, 1));
4188 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, 1));
4189 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, 1));
4190 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 1, 0));
4191 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, -1));
4192 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, -1));
4193 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, -1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004194
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004195 prepareDisplay(DISPLAY_ORIENTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004196 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, -1));
4197 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, -1));
4198 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, -1, 0));
4199 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, 1));
4200 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, 1));
4201 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, 1));
4202 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 1, 0));
4203 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, -1));
4204
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004205 prepareDisplay(DISPLAY_ORIENTATION_270);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004206 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 1, 0));
4207 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, -1));
4208 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, -1));
4209 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, -1));
4210 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, -1, 0));
4211 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, 1));
4212 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, 1));
4213 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, 1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004214}
4215
4216TEST_F(CursorInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004217 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004218 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004219
4220 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4221 mFakePointerController->setPosition(100, 200);
4222 mFakePointerController->setButtonState(0);
4223
4224 NotifyMotionArgs motionArgs;
4225 NotifyKeyArgs keyArgs;
4226
4227 // press BTN_LEFT, release BTN_LEFT
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004228 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 1);
4229 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004230 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4231 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4232 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4233 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004234 ASSERT_NO_FATAL_FAILURE(
4235 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004236
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004237 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4238 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4239 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4240 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004241 ASSERT_NO_FATAL_FAILURE(
4242 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004243
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004244 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 0);
4245 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004246 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004247 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004248 ASSERT_EQ(0, motionArgs.buttonState);
4249 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004250 ASSERT_NO_FATAL_FAILURE(
4251 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004252
4253 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004254 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004255 ASSERT_EQ(0, motionArgs.buttonState);
4256 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004257 ASSERT_NO_FATAL_FAILURE(
4258 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004259
4260 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004261 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004262 ASSERT_EQ(0, motionArgs.buttonState);
4263 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004264 ASSERT_NO_FATAL_FAILURE(
4265 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004266
4267 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004268 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 1);
4269 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 1);
4270 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004271 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4272 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4273 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4274 motionArgs.buttonState);
4275 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4276 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004277 ASSERT_NO_FATAL_FAILURE(
4278 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004279
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004280 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4281 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4282 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4283 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4284 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004285 ASSERT_NO_FATAL_FAILURE(
4286 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004287
4288 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4289 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4290 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4291 motionArgs.buttonState);
4292 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4293 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004294 ASSERT_NO_FATAL_FAILURE(
4295 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004296
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004297 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 0);
4298 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004299 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004300 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004301 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4302 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004303 ASSERT_NO_FATAL_FAILURE(
4304 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004305
4306 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004307 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004308 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4309 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004310 ASSERT_NO_FATAL_FAILURE(
4311 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004312
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004313 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4314 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004315 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004316 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
4317 ASSERT_EQ(0, motionArgs.buttonState);
4318 ASSERT_EQ(0, 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));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004321 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4322 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004323
4324 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004325 ASSERT_EQ(0, motionArgs.buttonState);
4326 ASSERT_EQ(0, mFakePointerController->getButtonState());
4327 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
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
Michael Wrightd02c5b62014-02-10 15:10:22 -08004331 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4332 ASSERT_EQ(0, motionArgs.buttonState);
4333 ASSERT_EQ(0, mFakePointerController->getButtonState());
4334 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004335 ASSERT_NO_FATAL_FAILURE(
4336 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004337
4338 // press BTN_BACK, release BTN_BACK
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004339 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 1);
4340 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004341 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4342 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4343 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004344
Michael Wrightd02c5b62014-02-10 15:10:22 -08004345 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004346 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004347 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4348 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004349 ASSERT_NO_FATAL_FAILURE(
4350 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004351
4352 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4353 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4354 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4355 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004356 ASSERT_NO_FATAL_FAILURE(
4357 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004358
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004359 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 0);
4360 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004361 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004362 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004363 ASSERT_EQ(0, motionArgs.buttonState);
4364 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004365 ASSERT_NO_FATAL_FAILURE(
4366 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004367
4368 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004369 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004370 ASSERT_EQ(0, motionArgs.buttonState);
4371 ASSERT_EQ(0, mFakePointerController->getButtonState());
4372
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004373 ASSERT_NO_FATAL_FAILURE(
4374 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004375 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4376 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4377 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4378
4379 // press BTN_SIDE, release BTN_SIDE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004380 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 1);
4381 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004382 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4383 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4384 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004385
Michael Wrightd02c5b62014-02-10 15:10:22 -08004386 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004387 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004388 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4389 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004390 ASSERT_NO_FATAL_FAILURE(
4391 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004392
4393 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4394 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4395 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4396 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004397 ASSERT_NO_FATAL_FAILURE(
4398 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004399
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004400 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 0);
4401 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004402 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004403 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004404 ASSERT_EQ(0, motionArgs.buttonState);
4405 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004406 ASSERT_NO_FATAL_FAILURE(
4407 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004408
4409 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4410 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4411 ASSERT_EQ(0, motionArgs.buttonState);
4412 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004413 ASSERT_NO_FATAL_FAILURE(
4414 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004415
Michael Wrightd02c5b62014-02-10 15:10:22 -08004416 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4417 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4418 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4419
4420 // press BTN_FORWARD, release BTN_FORWARD
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004421 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 1);
4422 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004423 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4424 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4425 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004426
Michael Wrightd02c5b62014-02-10 15:10:22 -08004427 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004428 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004429 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4430 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004431 ASSERT_NO_FATAL_FAILURE(
4432 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004433
4434 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4435 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4436 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4437 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004438 ASSERT_NO_FATAL_FAILURE(
4439 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004440
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004441 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 0);
4442 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004443 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004444 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004445 ASSERT_EQ(0, motionArgs.buttonState);
4446 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004447 ASSERT_NO_FATAL_FAILURE(
4448 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004449
4450 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4451 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4452 ASSERT_EQ(0, motionArgs.buttonState);
4453 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004454 ASSERT_NO_FATAL_FAILURE(
4455 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004456
Michael Wrightd02c5b62014-02-10 15:10:22 -08004457 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4458 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4459 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4460
4461 // press BTN_EXTRA, release BTN_EXTRA
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004462 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 1);
4463 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004464 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4465 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4466 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004467
Michael Wrightd02c5b62014-02-10 15:10:22 -08004468 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004469 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004470 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4471 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004472 ASSERT_NO_FATAL_FAILURE(
4473 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004474
4475 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4476 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4477 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4478 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004479 ASSERT_NO_FATAL_FAILURE(
4480 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004481
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004482 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 0);
4483 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004484 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004485 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004486 ASSERT_EQ(0, motionArgs.buttonState);
4487 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004488 ASSERT_NO_FATAL_FAILURE(
4489 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004490
4491 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4492 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4493 ASSERT_EQ(0, motionArgs.buttonState);
4494 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004495 ASSERT_NO_FATAL_FAILURE(
4496 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004497
Michael Wrightd02c5b62014-02-10 15:10:22 -08004498 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4499 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4500 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4501}
4502
4503TEST_F(CursorInputMapperTest, Process_WhenModeIsPointer_ShouldMoveThePointerAround) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004504 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004505 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004506
4507 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4508 mFakePointerController->setPosition(100, 200);
4509 mFakePointerController->setButtonState(0);
4510
4511 NotifyMotionArgs args;
4512
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004513 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4514 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4515 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004516 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004517 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4518 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4519 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4520 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 +01004521 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004522}
4523
4524TEST_F(CursorInputMapperTest, Process_PointerCapture) {
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004525 addConfigurationProperty("cursor.mode", "pointer");
4526 mFakePolicy->setPointerCapture(true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004527 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004528
4529 NotifyDeviceResetArgs resetArgs;
4530 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4531 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4532 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4533
4534 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4535 mFakePointerController->setPosition(100, 200);
4536 mFakePointerController->setButtonState(0);
4537
4538 NotifyMotionArgs args;
4539
4540 // Move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004541 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4542 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4543 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004544 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4545 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4546 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4547 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4548 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 +01004549 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004550
4551 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004552 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4553 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004554 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4555 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4556 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
4557 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4558 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4559 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4560 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4561 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
4562 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4563 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4564
4565 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004566 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4567 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004568 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4569 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4570 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
4571 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4572 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4573 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4574 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4575 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
4576 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4577 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4578
4579 // Another move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004580 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 30);
4581 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 40);
4582 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004583 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4584 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4585 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4586 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4587 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 +01004588 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004589
4590 // Disable pointer capture and check that the device generation got bumped
4591 // and events are generated the usual way.
arthurhungdcef2dc2020-08-11 14:47:50 +08004592 const uint32_t generation = mReader->getContext()->getGeneration();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004593 mFakePolicy->setPointerCapture(false);
4594 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
arthurhungdcef2dc2020-08-11 14:47:50 +08004595 ASSERT_TRUE(mReader->getContext()->getGeneration() != generation);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004596
4597 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4598 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4599 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4600
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004601 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4602 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4603 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004604 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4605 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004606 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4607 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4608 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 +01004609 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004610}
4611
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004612TEST_F(CursorInputMapperTest, Process_ShouldHandleDisplayId) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004613 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004614
Garfield Tan888a6a42020-01-09 11:39:16 -08004615 // Setup for second display.
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004616 constexpr int32_t SECOND_DISPLAY_ID = 1;
Garfield Tan888a6a42020-01-09 11:39:16 -08004617 const std::string SECOND_DISPLAY_UNIQUE_ID = "local:1";
4618 mFakePolicy->addDisplayViewport(SECOND_DISPLAY_ID, 800, 480, DISPLAY_ORIENTATION_0,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00004619 true /*isActive*/, SECOND_DISPLAY_UNIQUE_ID, NO_PORT,
4620 ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08004621 mFakePolicy->setDefaultPointerDisplayId(SECOND_DISPLAY_ID);
4622 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
4623
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004624 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4625 mFakePointerController->setPosition(100, 200);
4626 mFakePointerController->setButtonState(0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004627
4628 NotifyMotionArgs args;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004629 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4630 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4631 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004632 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4633 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4634 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4635 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4636 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 +01004637 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004638 ASSERT_EQ(SECOND_DISPLAY_ID, args.displayId);
4639}
4640
Michael Wrightd02c5b62014-02-10 15:10:22 -08004641// --- TouchInputMapperTest ---
4642
4643class TouchInputMapperTest : public InputMapperTest {
4644protected:
4645 static const int32_t RAW_X_MIN;
4646 static const int32_t RAW_X_MAX;
4647 static const int32_t RAW_Y_MIN;
4648 static const int32_t RAW_Y_MAX;
4649 static const int32_t RAW_TOUCH_MIN;
4650 static const int32_t RAW_TOUCH_MAX;
4651 static const int32_t RAW_TOOL_MIN;
4652 static const int32_t RAW_TOOL_MAX;
4653 static const int32_t RAW_PRESSURE_MIN;
4654 static const int32_t RAW_PRESSURE_MAX;
4655 static const int32_t RAW_ORIENTATION_MIN;
4656 static const int32_t RAW_ORIENTATION_MAX;
4657 static const int32_t RAW_DISTANCE_MIN;
4658 static const int32_t RAW_DISTANCE_MAX;
4659 static const int32_t RAW_TILT_MIN;
4660 static const int32_t RAW_TILT_MAX;
4661 static const int32_t RAW_ID_MIN;
4662 static const int32_t RAW_ID_MAX;
4663 static const int32_t RAW_SLOT_MIN;
4664 static const int32_t RAW_SLOT_MAX;
4665 static const float X_PRECISION;
4666 static const float Y_PRECISION;
Santos Cordonfa5cf462017-04-05 10:37:00 -07004667 static const float X_PRECISION_VIRTUAL;
4668 static const float Y_PRECISION_VIRTUAL;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004669
4670 static const float GEOMETRIC_SCALE;
Jason Gerecke489fda82012-09-07 17:19:40 -07004671 static const TouchAffineTransformation AFFINE_TRANSFORM;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004672
4673 static const VirtualKeyDefinition VIRTUAL_KEYS[2];
4674
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004675 const std::string UNIQUE_ID = "local:0";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004676 const std::string SECONDARY_UNIQUE_ID = "local:1";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004677
Michael Wrightd02c5b62014-02-10 15:10:22 -08004678 enum Axes {
4679 POSITION = 1 << 0,
4680 TOUCH = 1 << 1,
4681 TOOL = 1 << 2,
4682 PRESSURE = 1 << 3,
4683 ORIENTATION = 1 << 4,
4684 MINOR = 1 << 5,
4685 ID = 1 << 6,
4686 DISTANCE = 1 << 7,
4687 TILT = 1 << 8,
4688 SLOT = 1 << 9,
4689 TOOL_TYPE = 1 << 10,
4690 };
4691
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004692 void prepareDisplay(int32_t orientation, std::optional<uint8_t> port = NO_PORT);
4693 void prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port = NO_PORT);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004694 void prepareVirtualDisplay(int32_t orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004695 void prepareVirtualKeys();
Jason Gerecke489fda82012-09-07 17:19:40 -07004696 void prepareLocationCalibration();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004697 int32_t toRawX(float displayX);
4698 int32_t toRawY(float displayY);
Jason Gerecke489fda82012-09-07 17:19:40 -07004699 float toCookedX(float rawX, float rawY);
4700 float toCookedY(float rawX, float rawY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004701 float toDisplayX(int32_t rawX);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004702 float toDisplayX(int32_t rawX, int32_t displayWidth);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004703 float toDisplayY(int32_t rawY);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004704 float toDisplayY(int32_t rawY, int32_t displayHeight);
4705
Michael Wrightd02c5b62014-02-10 15:10:22 -08004706};
4707
4708const int32_t TouchInputMapperTest::RAW_X_MIN = 25;
4709const int32_t TouchInputMapperTest::RAW_X_MAX = 1019;
4710const int32_t TouchInputMapperTest::RAW_Y_MIN = 30;
4711const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009;
4712const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0;
4713const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31;
4714const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0;
4715const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15;
Michael Wrightaa449c92017-12-13 21:21:43 +00004716const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = 0;
4717const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = 255;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004718const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7;
4719const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
4720const int32_t TouchInputMapperTest::RAW_DISTANCE_MIN = 0;
4721const int32_t TouchInputMapperTest::RAW_DISTANCE_MAX = 7;
4722const int32_t TouchInputMapperTest::RAW_TILT_MIN = 0;
4723const int32_t TouchInputMapperTest::RAW_TILT_MAX = 150;
4724const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
4725const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
4726const int32_t TouchInputMapperTest::RAW_SLOT_MIN = 0;
4727const int32_t TouchInputMapperTest::RAW_SLOT_MAX = 9;
4728const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH;
4729const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT;
Santos Cordonfa5cf462017-04-05 10:37:00 -07004730const float TouchInputMapperTest::X_PRECISION_VIRTUAL =
4731 float(RAW_X_MAX - RAW_X_MIN + 1) / VIRTUAL_DISPLAY_WIDTH;
4732const float TouchInputMapperTest::Y_PRECISION_VIRTUAL =
4733 float(RAW_Y_MAX - RAW_Y_MIN + 1) / VIRTUAL_DISPLAY_HEIGHT;
Jason Gerecke489fda82012-09-07 17:19:40 -07004734const TouchAffineTransformation TouchInputMapperTest::AFFINE_TRANSFORM =
4735 TouchAffineTransformation(1, -2, 3, -4, 5, -6);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004736
4737const float TouchInputMapperTest::GEOMETRIC_SCALE =
4738 avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1),
4739 float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1));
4740
4741const VirtualKeyDefinition TouchInputMapperTest::VIRTUAL_KEYS[2] = {
4742 { KEY_HOME, 60, DISPLAY_HEIGHT + 15, 20, 20 },
4743 { KEY_MENU, DISPLAY_HEIGHT - 60, DISPLAY_WIDTH + 15, 20, 20 },
4744};
4745
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004746void TouchInputMapperTest::prepareDisplay(int32_t orientation, std::optional<uint8_t> port) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004747 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
4748 port, ViewportType::INTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004749}
4750
4751void TouchInputMapperTest::prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port) {
4752 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
4753 DISPLAY_ORIENTATION_0, SECONDARY_UNIQUE_ID, port, type);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004754}
4755
Santos Cordonfa5cf462017-04-05 10:37:00 -07004756void TouchInputMapperTest::prepareVirtualDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004757 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH, VIRTUAL_DISPLAY_HEIGHT,
4758 orientation, VIRTUAL_DISPLAY_UNIQUE_ID, NO_PORT,
4759 ViewportType::VIRTUAL);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004760}
4761
Michael Wrightd02c5b62014-02-10 15:10:22 -08004762void TouchInputMapperTest::prepareVirtualKeys() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004763 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[0]);
4764 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[1]);
4765 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4766 mFakeEventHub->addKey(EVENTHUB_ID, KEY_MENU, 0, AKEYCODE_MENU, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004767}
4768
Jason Gerecke489fda82012-09-07 17:19:40 -07004769void TouchInputMapperTest::prepareLocationCalibration() {
4770 mFakePolicy->setTouchAffineTransformation(AFFINE_TRANSFORM);
4771}
4772
Michael Wrightd02c5b62014-02-10 15:10:22 -08004773int32_t TouchInputMapperTest::toRawX(float displayX) {
4774 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN);
4775}
4776
4777int32_t TouchInputMapperTest::toRawY(float displayY) {
4778 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN);
4779}
4780
Jason Gerecke489fda82012-09-07 17:19:40 -07004781float TouchInputMapperTest::toCookedX(float rawX, float rawY) {
4782 AFFINE_TRANSFORM.applyTo(rawX, rawY);
4783 return rawX;
4784}
4785
4786float TouchInputMapperTest::toCookedY(float rawX, float rawY) {
4787 AFFINE_TRANSFORM.applyTo(rawX, rawY);
4788 return rawY;
4789}
4790
Michael Wrightd02c5b62014-02-10 15:10:22 -08004791float TouchInputMapperTest::toDisplayX(int32_t rawX) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004792 return toDisplayX(rawX, DISPLAY_WIDTH);
4793}
4794
4795float TouchInputMapperTest::toDisplayX(int32_t rawX, int32_t displayWidth) {
4796 return float(rawX - RAW_X_MIN) * displayWidth / (RAW_X_MAX - RAW_X_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004797}
4798
4799float TouchInputMapperTest::toDisplayY(int32_t rawY) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004800 return toDisplayY(rawY, DISPLAY_HEIGHT);
4801}
4802
4803float TouchInputMapperTest::toDisplayY(int32_t rawY, int32_t displayHeight) {
4804 return float(rawY - RAW_Y_MIN) * displayHeight / (RAW_Y_MAX - RAW_Y_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004805}
4806
4807
4808// --- SingleTouchInputMapperTest ---
4809
4810class SingleTouchInputMapperTest : public TouchInputMapperTest {
4811protected:
4812 void prepareButtons();
4813 void prepareAxes(int axes);
4814
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004815 void processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
4816 void processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
4817 void processUp(SingleTouchInputMapper& mappery);
4818 void processPressure(SingleTouchInputMapper& mapper, int32_t pressure);
4819 void processToolMajor(SingleTouchInputMapper& mapper, int32_t toolMajor);
4820 void processDistance(SingleTouchInputMapper& mapper, int32_t distance);
4821 void processTilt(SingleTouchInputMapper& mapper, int32_t tiltX, int32_t tiltY);
4822 void processKey(SingleTouchInputMapper& mapper, int32_t code, int32_t value);
4823 void processSync(SingleTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004824};
4825
4826void SingleTouchInputMapperTest::prepareButtons() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004827 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004828}
4829
4830void SingleTouchInputMapperTest::prepareAxes(int axes) {
4831 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004832 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
4833 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004834 }
4835 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004836 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MIN,
4837 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004838 }
4839 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004840 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TOOL_WIDTH, RAW_TOOL_MIN, RAW_TOOL_MAX, 0,
4841 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004842 }
4843 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004844 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_DISTANCE, RAW_DISTANCE_MIN,
4845 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004846 }
4847 if (axes & TILT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004848 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_X, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
4849 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_Y, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004850 }
4851}
4852
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004853void SingleTouchInputMapperTest::processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004854 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
4855 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
4856 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004857}
4858
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004859void SingleTouchInputMapperTest::processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004860 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
4861 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004862}
4863
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004864void SingleTouchInputMapperTest::processUp(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004865 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004866}
4867
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004868void SingleTouchInputMapperTest::processPressure(SingleTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004869 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004870}
4871
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004872void SingleTouchInputMapperTest::processToolMajor(SingleTouchInputMapper& mapper,
4873 int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004874 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TOOL_WIDTH, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004875}
4876
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004877void SingleTouchInputMapperTest::processDistance(SingleTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004878 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004879}
4880
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004881void SingleTouchInputMapperTest::processTilt(SingleTouchInputMapper& mapper, int32_t tiltX,
4882 int32_t tiltY) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004883 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_X, tiltX);
4884 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_Y, tiltY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004885}
4886
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004887void SingleTouchInputMapperTest::processKey(SingleTouchInputMapper& mapper, int32_t code,
4888 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004889 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004890}
4891
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004892void SingleTouchInputMapperTest::processSync(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004893 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004894}
4895
Michael Wrightd02c5b62014-02-10 15:10:22 -08004896TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004897 prepareButtons();
4898 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004899 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004900
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004901 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004902}
4903
4904TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndIsACursor_ReturnsTouchPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004905 mFakeEventHub->addRelativeAxis(EVENTHUB_ID, REL_X);
4906 mFakeEventHub->addRelativeAxis(EVENTHUB_ID, REL_Y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004907 prepareButtons();
4908 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004909 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004910
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004911 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004912}
4913
4914TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchPad_ReturnsTouchPad) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004915 prepareButtons();
4916 prepareAxes(POSITION);
4917 addConfigurationProperty("touch.deviceType", "touchPad");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004918 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004919
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004920 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004921}
4922
4923TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004924 prepareButtons();
4925 prepareAxes(POSITION);
4926 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004927 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004928
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004929 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004930}
4931
4932TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004933 addConfigurationProperty("touch.deviceType", "touchScreen");
4934 prepareDisplay(DISPLAY_ORIENTATION_0);
4935 prepareButtons();
4936 prepareAxes(POSITION);
4937 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004938 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004939
4940 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004941 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004942
4943 // Virtual key is down.
4944 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4945 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4946 processDown(mapper, x, y);
4947 processSync(mapper);
4948 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4949
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004950 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004951
4952 // Virtual key is up.
4953 processUp(mapper);
4954 processSync(mapper);
4955 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4956
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004957 ASSERT_EQ(AKEY_STATE_UP, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004958}
4959
4960TEST_F(SingleTouchInputMapperTest, GetScanCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004961 addConfigurationProperty("touch.deviceType", "touchScreen");
4962 prepareDisplay(DISPLAY_ORIENTATION_0);
4963 prepareButtons();
4964 prepareAxes(POSITION);
4965 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004966 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004967
4968 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004969 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004970
4971 // Virtual key is down.
4972 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4973 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4974 processDown(mapper, x, y);
4975 processSync(mapper);
4976 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4977
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004978 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004979
4980 // Virtual key is up.
4981 processUp(mapper);
4982 processSync(mapper);
4983 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4984
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004985 ASSERT_EQ(AKEY_STATE_UP, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004986}
4987
4988TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004989 addConfigurationProperty("touch.deviceType", "touchScreen");
4990 prepareDisplay(DISPLAY_ORIENTATION_0);
4991 prepareButtons();
4992 prepareAxes(POSITION);
4993 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004994 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004995
4996 const int32_t keys[2] = { AKEYCODE_HOME, AKEYCODE_A };
4997 uint8_t flags[2] = { 0, 0 };
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004998 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, 2, keys, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004999 ASSERT_TRUE(flags[0]);
5000 ASSERT_FALSE(flags[1]);
5001}
5002
5003TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005004 addConfigurationProperty("touch.deviceType", "touchScreen");
5005 prepareDisplay(DISPLAY_ORIENTATION_0);
5006 prepareButtons();
5007 prepareAxes(POSITION);
5008 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005009 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005010
arthurhungdcef2dc2020-08-11 14:47:50 +08005011 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005012
5013 NotifyKeyArgs args;
5014
5015 // Press virtual key.
5016 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5017 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5018 processDown(mapper, x, y);
5019 processSync(mapper);
5020
5021 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
5022 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5023 ASSERT_EQ(DEVICE_ID, args.deviceId);
5024 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
5025 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
5026 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
5027 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5028 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5029 ASSERT_EQ(KEY_HOME, args.scanCode);
5030 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5031 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5032
5033 // Release virtual key.
5034 processUp(mapper);
5035 processSync(mapper);
5036
5037 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
5038 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5039 ASSERT_EQ(DEVICE_ID, args.deviceId);
5040 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
5041 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
5042 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
5043 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5044 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5045 ASSERT_EQ(KEY_HOME, args.scanCode);
5046 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5047 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5048
5049 // Should not have sent any motions.
5050 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5051}
5052
5053TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005054 addConfigurationProperty("touch.deviceType", "touchScreen");
5055 prepareDisplay(DISPLAY_ORIENTATION_0);
5056 prepareButtons();
5057 prepareAxes(POSITION);
5058 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005059 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005060
arthurhungdcef2dc2020-08-11 14:47:50 +08005061 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005062
5063 NotifyKeyArgs keyArgs;
5064
5065 // Press virtual key.
5066 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5067 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5068 processDown(mapper, x, y);
5069 processSync(mapper);
5070
5071 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5072 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5073 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5074 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5075 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5076 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5077 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags);
5078 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5079 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5080 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5081 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5082
5083 // Move out of bounds. This should generate a cancel and a pointer down since we moved
5084 // into the display area.
5085 y -= 100;
5086 processMove(mapper, x, y);
5087 processSync(mapper);
5088
5089 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5090 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5091 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5092 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5093 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5094 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5095 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
5096 | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags);
5097 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5098 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5099 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5100 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5101
5102 NotifyMotionArgs motionArgs;
5103 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5104 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5105 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5106 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5107 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5108 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5109 ASSERT_EQ(0, motionArgs.flags);
5110 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5111 ASSERT_EQ(0, motionArgs.buttonState);
5112 ASSERT_EQ(0, motionArgs.edgeFlags);
5113 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5114 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5115 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5116 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5117 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5118 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5119 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5120 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5121
5122 // Keep moving out of bounds. Should generate a pointer move.
5123 y -= 50;
5124 processMove(mapper, x, y);
5125 processSync(mapper);
5126
5127 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5128 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5129 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5130 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5131 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5132 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5133 ASSERT_EQ(0, motionArgs.flags);
5134 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5135 ASSERT_EQ(0, motionArgs.buttonState);
5136 ASSERT_EQ(0, motionArgs.edgeFlags);
5137 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5138 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5139 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5140 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5141 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5142 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5143 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5144 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5145
5146 // Release out of bounds. Should generate a pointer up.
5147 processUp(mapper);
5148 processSync(mapper);
5149
5150 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5151 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5152 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5153 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5154 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5155 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5156 ASSERT_EQ(0, motionArgs.flags);
5157 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5158 ASSERT_EQ(0, motionArgs.buttonState);
5159 ASSERT_EQ(0, motionArgs.edgeFlags);
5160 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5161 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5162 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5163 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5164 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5165 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5166 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5167 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5168
5169 // Should not have sent any more keys or motions.
5170 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5171 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5172}
5173
5174TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005175 addConfigurationProperty("touch.deviceType", "touchScreen");
5176 prepareDisplay(DISPLAY_ORIENTATION_0);
5177 prepareButtons();
5178 prepareAxes(POSITION);
5179 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005180 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005181
arthurhungdcef2dc2020-08-11 14:47:50 +08005182 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005183
5184 NotifyMotionArgs motionArgs;
5185
5186 // Initially go down out of bounds.
5187 int32_t x = -10;
5188 int32_t y = -10;
5189 processDown(mapper, x, y);
5190 processSync(mapper);
5191
5192 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5193
5194 // Move into the display area. Should generate a pointer down.
5195 x = 50;
5196 y = 75;
5197 processMove(mapper, x, y);
5198 processSync(mapper);
5199
5200 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5201 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5202 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5203 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5204 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5205 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5206 ASSERT_EQ(0, motionArgs.flags);
5207 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5208 ASSERT_EQ(0, motionArgs.buttonState);
5209 ASSERT_EQ(0, motionArgs.edgeFlags);
5210 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5211 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5212 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5213 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5214 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5215 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5216 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5217 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5218
5219 // Release. Should generate a pointer up.
5220 processUp(mapper);
5221 processSync(mapper);
5222
5223 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5224 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5225 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5226 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5227 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5228 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5229 ASSERT_EQ(0, motionArgs.flags);
5230 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5231 ASSERT_EQ(0, motionArgs.buttonState);
5232 ASSERT_EQ(0, motionArgs.edgeFlags);
5233 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5234 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5235 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5236 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5237 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5238 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5239 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5240 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5241
5242 // Should not have sent any more keys or motions.
5243 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5244 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5245}
5246
Santos Cordonfa5cf462017-04-05 10:37:00 -07005247TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture_VirtualDisplay) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005248 addConfigurationProperty("touch.deviceType", "touchScreen");
5249 addConfigurationProperty("touch.displayId", VIRTUAL_DISPLAY_UNIQUE_ID);
5250
5251 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
5252 prepareButtons();
5253 prepareAxes(POSITION);
5254 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005255 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Santos Cordonfa5cf462017-04-05 10:37:00 -07005256
arthurhungdcef2dc2020-08-11 14:47:50 +08005257 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005258
5259 NotifyMotionArgs motionArgs;
5260
5261 // Down.
5262 int32_t x = 100;
5263 int32_t y = 125;
5264 processDown(mapper, x, y);
5265 processSync(mapper);
5266
5267 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5268 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5269 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5270 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5271 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5272 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5273 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5274 ASSERT_EQ(0, motionArgs.flags);
5275 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5276 ASSERT_EQ(0, motionArgs.buttonState);
5277 ASSERT_EQ(0, motionArgs.edgeFlags);
5278 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5279 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5280 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5281 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5282 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5283 1, 0, 0, 0, 0, 0, 0, 0));
5284 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5285 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5286 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5287
5288 // Move.
5289 x += 50;
5290 y += 75;
5291 processMove(mapper, x, y);
5292 processSync(mapper);
5293
5294 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5295 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5296 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5297 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5298 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5299 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5300 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5301 ASSERT_EQ(0, motionArgs.flags);
5302 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5303 ASSERT_EQ(0, motionArgs.buttonState);
5304 ASSERT_EQ(0, motionArgs.edgeFlags);
5305 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5306 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5307 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5308 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5309 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5310 1, 0, 0, 0, 0, 0, 0, 0));
5311 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5312 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5313 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5314
5315 // Up.
5316 processUp(mapper);
5317 processSync(mapper);
5318
5319 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5320 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5321 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5322 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5323 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5324 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5325 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5326 ASSERT_EQ(0, motionArgs.flags);
5327 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5328 ASSERT_EQ(0, motionArgs.buttonState);
5329 ASSERT_EQ(0, motionArgs.edgeFlags);
5330 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5331 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5332 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5333 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5334 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5335 1, 0, 0, 0, 0, 0, 0, 0));
5336 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5337 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5338 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5339
5340 // Should not have sent any more keys or motions.
5341 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5342 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5343}
5344
Michael Wrightd02c5b62014-02-10 15:10:22 -08005345TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005346 addConfigurationProperty("touch.deviceType", "touchScreen");
5347 prepareDisplay(DISPLAY_ORIENTATION_0);
5348 prepareButtons();
5349 prepareAxes(POSITION);
5350 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005351 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005352
arthurhungdcef2dc2020-08-11 14:47:50 +08005353 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005354
5355 NotifyMotionArgs motionArgs;
5356
5357 // Down.
5358 int32_t x = 100;
5359 int32_t y = 125;
5360 processDown(mapper, x, y);
5361 processSync(mapper);
5362
5363 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5364 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5365 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5366 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5367 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5368 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5369 ASSERT_EQ(0, motionArgs.flags);
5370 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5371 ASSERT_EQ(0, motionArgs.buttonState);
5372 ASSERT_EQ(0, motionArgs.edgeFlags);
5373 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5374 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5375 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5376 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5377 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5378 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5379 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5380 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5381
5382 // Move.
5383 x += 50;
5384 y += 75;
5385 processMove(mapper, x, y);
5386 processSync(mapper);
5387
5388 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5389 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5390 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5391 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5392 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5393 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5394 ASSERT_EQ(0, motionArgs.flags);
5395 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5396 ASSERT_EQ(0, motionArgs.buttonState);
5397 ASSERT_EQ(0, motionArgs.edgeFlags);
5398 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5399 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5400 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5401 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5402 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5403 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5404 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5405 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5406
5407 // Up.
5408 processUp(mapper);
5409 processSync(mapper);
5410
5411 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5412 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5413 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5414 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5415 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5416 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5417 ASSERT_EQ(0, motionArgs.flags);
5418 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5419 ASSERT_EQ(0, motionArgs.buttonState);
5420 ASSERT_EQ(0, motionArgs.edgeFlags);
5421 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5422 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5423 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5424 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5425 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5426 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5427 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5428 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5429
5430 // Should not have sent any more keys or motions.
5431 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5432 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5433}
5434
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005435TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_DoesNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005436 addConfigurationProperty("touch.deviceType", "touchScreen");
5437 prepareButtons();
5438 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005439 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
5440 // need to be rotated. Touchscreens are orientation-aware by default.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005441 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005442
5443 NotifyMotionArgs args;
5444
5445 // Rotation 90.
5446 prepareDisplay(DISPLAY_ORIENTATION_90);
5447 processDown(mapper, toRawX(50), toRawY(75));
5448 processSync(mapper);
5449
5450 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5451 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5452 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5453
5454 processUp(mapper);
5455 processSync(mapper);
5456 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5457}
5458
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005459TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_RotatesMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005460 addConfigurationProperty("touch.deviceType", "touchScreen");
5461 prepareButtons();
5462 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005463 // Since InputReader works in the un-rotated coordinate space, only devices that are not
5464 // orientation-aware are affected by display rotation.
5465 addConfigurationProperty("touch.orientationAware", "0");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005466 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005467
5468 NotifyMotionArgs args;
5469
5470 // Rotation 0.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005471 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005472 prepareDisplay(DISPLAY_ORIENTATION_0);
5473 processDown(mapper, toRawX(50), toRawY(75));
5474 processSync(mapper);
5475
5476 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5477 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5478 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5479
5480 processUp(mapper);
5481 processSync(mapper);
5482 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5483
5484 // Rotation 90.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005485 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005486 prepareDisplay(DISPLAY_ORIENTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005487 processDown(mapper, toRawX(75), RAW_Y_MAX - toRawY(50) + RAW_Y_MIN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005488 processSync(mapper);
5489
5490 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5491 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5492 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5493
5494 processUp(mapper);
5495 processSync(mapper);
5496 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5497
5498 // Rotation 180.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005499 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005500 prepareDisplay(DISPLAY_ORIENTATION_180);
5501 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
5502 processSync(mapper);
5503
5504 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5505 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5506 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5507
5508 processUp(mapper);
5509 processSync(mapper);
5510 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5511
5512 // Rotation 270.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005513 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005514 prepareDisplay(DISPLAY_ORIENTATION_270);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005515 processDown(mapper, RAW_X_MAX - toRawX(75) + RAW_X_MIN, toRawY(50));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005516 processSync(mapper);
5517
5518 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5519 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5520 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5521
5522 processUp(mapper);
5523 processSync(mapper);
5524 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5525}
5526
5527TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005528 addConfigurationProperty("touch.deviceType", "touchScreen");
5529 prepareDisplay(DISPLAY_ORIENTATION_0);
5530 prepareButtons();
5531 prepareAxes(POSITION | PRESSURE | TOOL | DISTANCE | TILT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005532 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005533
5534 // These calculations are based on the input device calibration documentation.
5535 int32_t rawX = 100;
5536 int32_t rawY = 200;
5537 int32_t rawPressure = 10;
5538 int32_t rawToolMajor = 12;
5539 int32_t rawDistance = 2;
5540 int32_t rawTiltX = 30;
5541 int32_t rawTiltY = 110;
5542
5543 float x = toDisplayX(rawX);
5544 float y = toDisplayY(rawY);
5545 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
5546 float size = float(rawToolMajor) / RAW_TOOL_MAX;
5547 float tool = float(rawToolMajor) * GEOMETRIC_SCALE;
5548 float distance = float(rawDistance);
5549
5550 float tiltCenter = (RAW_TILT_MAX + RAW_TILT_MIN) * 0.5f;
5551 float tiltScale = M_PI / 180;
5552 float tiltXAngle = (rawTiltX - tiltCenter) * tiltScale;
5553 float tiltYAngle = (rawTiltY - tiltCenter) * tiltScale;
5554 float orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
5555 float tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
5556
5557 processDown(mapper, rawX, rawY);
5558 processPressure(mapper, rawPressure);
5559 processToolMajor(mapper, rawToolMajor);
5560 processDistance(mapper, rawDistance);
5561 processTilt(mapper, rawTiltX, rawTiltY);
5562 processSync(mapper);
5563
5564 NotifyMotionArgs args;
5565 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5566 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5567 x, y, pressure, size, tool, tool, tool, tool, orientation, distance));
5568 ASSERT_EQ(tilt, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TILT));
5569}
5570
Jason Gerecke489fda82012-09-07 17:19:40 -07005571TEST_F(SingleTouchInputMapperTest, Process_XYAxes_AffineCalibration) {
Jason Gerecke489fda82012-09-07 17:19:40 -07005572 addConfigurationProperty("touch.deviceType", "touchScreen");
5573 prepareDisplay(DISPLAY_ORIENTATION_0);
5574 prepareLocationCalibration();
5575 prepareButtons();
5576 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005577 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Jason Gerecke489fda82012-09-07 17:19:40 -07005578
5579 int32_t rawX = 100;
5580 int32_t rawY = 200;
5581
5582 float x = toDisplayX(toCookedX(rawX, rawY));
5583 float y = toDisplayY(toCookedY(rawX, rawY));
5584
5585 processDown(mapper, rawX, rawY);
5586 processSync(mapper);
5587
5588 NotifyMotionArgs args;
5589 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5590 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5591 x, y, 1, 0, 0, 0, 0, 0, 0, 0));
5592}
5593
Michael Wrightd02c5b62014-02-10 15:10:22 -08005594TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005595 addConfigurationProperty("touch.deviceType", "touchScreen");
5596 prepareDisplay(DISPLAY_ORIENTATION_0);
5597 prepareButtons();
5598 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005599 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005600
5601 NotifyMotionArgs motionArgs;
5602 NotifyKeyArgs keyArgs;
5603
5604 processDown(mapper, 100, 200);
5605 processSync(mapper);
5606 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5607 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5608 ASSERT_EQ(0, motionArgs.buttonState);
5609
5610 // press BTN_LEFT, release BTN_LEFT
5611 processKey(mapper, BTN_LEFT, 1);
5612 processSync(mapper);
5613 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5614 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5615 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
5616
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005617 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5618 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5619 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
5620
Michael Wrightd02c5b62014-02-10 15:10:22 -08005621 processKey(mapper, BTN_LEFT, 0);
5622 processSync(mapper);
5623 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005624 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005625 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005626
5627 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005628 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005629 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005630
5631 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
5632 processKey(mapper, BTN_RIGHT, 1);
5633 processKey(mapper, BTN_MIDDLE, 1);
5634 processSync(mapper);
5635 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5636 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5637 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5638 motionArgs.buttonState);
5639
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005640 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5641 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5642 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
5643
5644 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5645 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5646 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5647 motionArgs.buttonState);
5648
Michael Wrightd02c5b62014-02-10 15:10:22 -08005649 processKey(mapper, BTN_RIGHT, 0);
5650 processSync(mapper);
5651 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005652 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005653 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005654
5655 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005656 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005657 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005658
5659 processKey(mapper, BTN_MIDDLE, 0);
5660 processSync(mapper);
5661 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005662 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005663 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005664
5665 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005666 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005667 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005668
5669 // press BTN_BACK, release BTN_BACK
5670 processKey(mapper, BTN_BACK, 1);
5671 processSync(mapper);
5672 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5673 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5674 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005675
Michael Wrightd02c5b62014-02-10 15:10:22 -08005676 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005677 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005678 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5679
5680 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5681 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5682 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005683
5684 processKey(mapper, BTN_BACK, 0);
5685 processSync(mapper);
5686 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005687 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005688 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005689
5690 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005691 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005692 ASSERT_EQ(0, motionArgs.buttonState);
5693
Michael Wrightd02c5b62014-02-10 15:10:22 -08005694 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5695 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5696 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
5697
5698 // press BTN_SIDE, release BTN_SIDE
5699 processKey(mapper, BTN_SIDE, 1);
5700 processSync(mapper);
5701 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5702 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5703 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005704
Michael Wrightd02c5b62014-02-10 15:10:22 -08005705 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005706 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005707 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5708
5709 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5710 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5711 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005712
5713 processKey(mapper, BTN_SIDE, 0);
5714 processSync(mapper);
5715 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005716 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005717 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005718
5719 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005720 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005721 ASSERT_EQ(0, motionArgs.buttonState);
5722
Michael Wrightd02c5b62014-02-10 15:10:22 -08005723 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5724 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5725 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
5726
5727 // press BTN_FORWARD, release BTN_FORWARD
5728 processKey(mapper, BTN_FORWARD, 1);
5729 processSync(mapper);
5730 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5731 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5732 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005733
Michael Wrightd02c5b62014-02-10 15:10:22 -08005734 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005735 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005736 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5737
5738 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5739 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5740 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005741
5742 processKey(mapper, BTN_FORWARD, 0);
5743 processSync(mapper);
5744 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005745 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005746 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005747
5748 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005749 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005750 ASSERT_EQ(0, motionArgs.buttonState);
5751
Michael Wrightd02c5b62014-02-10 15:10:22 -08005752 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5753 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5754 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
5755
5756 // press BTN_EXTRA, release BTN_EXTRA
5757 processKey(mapper, BTN_EXTRA, 1);
5758 processSync(mapper);
5759 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5760 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5761 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005762
Michael Wrightd02c5b62014-02-10 15:10:22 -08005763 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005764 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005765 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5766
5767 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5768 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5769 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005770
5771 processKey(mapper, BTN_EXTRA, 0);
5772 processSync(mapper);
5773 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005774 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005775 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005776
5777 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005778 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005779 ASSERT_EQ(0, motionArgs.buttonState);
5780
Michael Wrightd02c5b62014-02-10 15:10:22 -08005781 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5782 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5783 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
5784
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005785 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5786
Michael Wrightd02c5b62014-02-10 15:10:22 -08005787 // press BTN_STYLUS, release BTN_STYLUS
5788 processKey(mapper, BTN_STYLUS, 1);
5789 processSync(mapper);
5790 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5791 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005792 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
5793
5794 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5795 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5796 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005797
5798 processKey(mapper, BTN_STYLUS, 0);
5799 processSync(mapper);
5800 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005801 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005802 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005803
5804 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005805 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005806 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005807
5808 // press BTN_STYLUS2, release BTN_STYLUS2
5809 processKey(mapper, BTN_STYLUS2, 1);
5810 processSync(mapper);
5811 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5812 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005813 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
5814
5815 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5816 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5817 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005818
5819 processKey(mapper, BTN_STYLUS2, 0);
5820 processSync(mapper);
5821 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005822 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005823 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005824
5825 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005826 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005827 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005828
5829 // release touch
5830 processUp(mapper);
5831 processSync(mapper);
5832 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5833 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5834 ASSERT_EQ(0, motionArgs.buttonState);
5835}
5836
5837TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005838 addConfigurationProperty("touch.deviceType", "touchScreen");
5839 prepareDisplay(DISPLAY_ORIENTATION_0);
5840 prepareButtons();
5841 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005842 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005843
5844 NotifyMotionArgs motionArgs;
5845
5846 // default tool type is finger
5847 processDown(mapper, 100, 200);
5848 processSync(mapper);
5849 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5850 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5851 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5852
5853 // eraser
5854 processKey(mapper, BTN_TOOL_RUBBER, 1);
5855 processSync(mapper);
5856 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5857 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5858 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
5859
5860 // stylus
5861 processKey(mapper, BTN_TOOL_RUBBER, 0);
5862 processKey(mapper, BTN_TOOL_PEN, 1);
5863 processSync(mapper);
5864 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5865 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5866 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5867
5868 // brush
5869 processKey(mapper, BTN_TOOL_PEN, 0);
5870 processKey(mapper, BTN_TOOL_BRUSH, 1);
5871 processSync(mapper);
5872 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5873 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5874 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5875
5876 // pencil
5877 processKey(mapper, BTN_TOOL_BRUSH, 0);
5878 processKey(mapper, BTN_TOOL_PENCIL, 1);
5879 processSync(mapper);
5880 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5881 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5882 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5883
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08005884 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08005885 processKey(mapper, BTN_TOOL_PENCIL, 0);
5886 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
5887 processSync(mapper);
5888 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5889 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5890 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5891
5892 // mouse
5893 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
5894 processKey(mapper, BTN_TOOL_MOUSE, 1);
5895 processSync(mapper);
5896 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5897 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5898 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
5899
5900 // lens
5901 processKey(mapper, BTN_TOOL_MOUSE, 0);
5902 processKey(mapper, BTN_TOOL_LENS, 1);
5903 processSync(mapper);
5904 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5905 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5906 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
5907
5908 // double-tap
5909 processKey(mapper, BTN_TOOL_LENS, 0);
5910 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
5911 processSync(mapper);
5912 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5913 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5914 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5915
5916 // triple-tap
5917 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
5918 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
5919 processSync(mapper);
5920 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5921 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5922 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5923
5924 // quad-tap
5925 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
5926 processKey(mapper, BTN_TOOL_QUADTAP, 1);
5927 processSync(mapper);
5928 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5929 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5930 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5931
5932 // finger
5933 processKey(mapper, BTN_TOOL_QUADTAP, 0);
5934 processKey(mapper, BTN_TOOL_FINGER, 1);
5935 processSync(mapper);
5936 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5937 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5938 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5939
5940 // stylus trumps finger
5941 processKey(mapper, BTN_TOOL_PEN, 1);
5942 processSync(mapper);
5943 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5944 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5945 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5946
5947 // eraser trumps stylus
5948 processKey(mapper, BTN_TOOL_RUBBER, 1);
5949 processSync(mapper);
5950 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5951 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5952 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
5953
5954 // mouse trumps eraser
5955 processKey(mapper, BTN_TOOL_MOUSE, 1);
5956 processSync(mapper);
5957 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5958 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5959 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
5960
5961 // back to default tool type
5962 processKey(mapper, BTN_TOOL_MOUSE, 0);
5963 processKey(mapper, BTN_TOOL_RUBBER, 0);
5964 processKey(mapper, BTN_TOOL_PEN, 0);
5965 processKey(mapper, BTN_TOOL_FINGER, 0);
5966 processSync(mapper);
5967 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5968 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5969 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5970}
5971
5972TEST_F(SingleTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005973 addConfigurationProperty("touch.deviceType", "touchScreen");
5974 prepareDisplay(DISPLAY_ORIENTATION_0);
5975 prepareButtons();
5976 prepareAxes(POSITION);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005977 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_FINGER, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005978 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005979
5980 NotifyMotionArgs motionArgs;
5981
5982 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
5983 processKey(mapper, BTN_TOOL_FINGER, 1);
5984 processMove(mapper, 100, 200);
5985 processSync(mapper);
5986 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5987 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5988 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5989 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5990
5991 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5992 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5993 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5994 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5995
5996 // move a little
5997 processMove(mapper, 150, 250);
5998 processSync(mapper);
5999 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6000 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6001 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6002 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6003
6004 // down when BTN_TOUCH is pressed, pressure defaults to 1
6005 processKey(mapper, BTN_TOUCH, 1);
6006 processSync(mapper);
6007 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6008 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6009 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6010 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6011
6012 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6013 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6014 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6015 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6016
6017 // up when BTN_TOUCH is released, hover restored
6018 processKey(mapper, BTN_TOUCH, 0);
6019 processSync(mapper);
6020 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6021 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6022 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6023 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6024
6025 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6026 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6027 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6028 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6029
6030 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6031 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6032 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6033 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6034
6035 // exit hover when pointer goes away
6036 processKey(mapper, BTN_TOOL_FINGER, 0);
6037 processSync(mapper);
6038 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6039 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6040 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6041 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6042}
6043
6044TEST_F(SingleTouchInputMapperTest, Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006045 addConfigurationProperty("touch.deviceType", "touchScreen");
6046 prepareDisplay(DISPLAY_ORIENTATION_0);
6047 prepareButtons();
6048 prepareAxes(POSITION | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006049 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006050
6051 NotifyMotionArgs motionArgs;
6052
6053 // initially hovering because pressure is 0
6054 processDown(mapper, 100, 200);
6055 processPressure(mapper, 0);
6056 processSync(mapper);
6057 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6058 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6059 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6060 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6061
6062 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6063 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6064 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6065 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6066
6067 // move a little
6068 processMove(mapper, 150, 250);
6069 processSync(mapper);
6070 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6071 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6072 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6073 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6074
6075 // down when pressure is non-zero
6076 processPressure(mapper, RAW_PRESSURE_MAX);
6077 processSync(mapper);
6078 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6079 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6080 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6081 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6082
6083 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6084 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6085 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6086 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6087
6088 // up when pressure becomes 0, hover restored
6089 processPressure(mapper, 0);
6090 processSync(mapper);
6091 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6092 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6093 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6094 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6095
6096 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6097 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6098 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6099 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6100
6101 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6102 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6103 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6104 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6105
6106 // exit hover when pointer goes away
6107 processUp(mapper);
6108 processSync(mapper);
6109 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6110 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6111 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6112 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6113}
6114
Michael Wrightd02c5b62014-02-10 15:10:22 -08006115// --- MultiTouchInputMapperTest ---
6116
6117class MultiTouchInputMapperTest : public TouchInputMapperTest {
6118protected:
6119 void prepareAxes(int axes);
6120
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006121 void processPosition(MultiTouchInputMapper& mapper, int32_t x, int32_t y);
6122 void processTouchMajor(MultiTouchInputMapper& mapper, int32_t touchMajor);
6123 void processTouchMinor(MultiTouchInputMapper& mapper, int32_t touchMinor);
6124 void processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor);
6125 void processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor);
6126 void processOrientation(MultiTouchInputMapper& mapper, int32_t orientation);
6127 void processPressure(MultiTouchInputMapper& mapper, int32_t pressure);
6128 void processDistance(MultiTouchInputMapper& mapper, int32_t distance);
6129 void processId(MultiTouchInputMapper& mapper, int32_t id);
6130 void processSlot(MultiTouchInputMapper& mapper, int32_t slot);
6131 void processToolType(MultiTouchInputMapper& mapper, int32_t toolType);
6132 void processKey(MultiTouchInputMapper& mapper, int32_t code, int32_t value);
6133 void processMTSync(MultiTouchInputMapper& mapper);
6134 void processSync(MultiTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006135};
6136
6137void MultiTouchInputMapperTest::prepareAxes(int axes) {
6138 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006139 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
6140 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006141 }
6142 if (axes & TOUCH) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006143 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN,
6144 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006145 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006146 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN,
6147 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006148 }
6149 }
6150 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006151 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
6152 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006153 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006154 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MAX,
6155 RAW_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006156 }
6157 }
6158 if (axes & ORIENTATION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006159 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_ORIENTATION, RAW_ORIENTATION_MIN,
6160 RAW_ORIENTATION_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006161 }
6162 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006163 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_PRESSURE, RAW_PRESSURE_MIN,
6164 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006165 }
6166 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006167 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_DISTANCE, RAW_DISTANCE_MIN,
6168 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006169 }
6170 if (axes & ID) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006171 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX, 0,
6172 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006173 }
6174 if (axes & SLOT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006175 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
6176 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_MT_SLOT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006177 }
6178 if (axes & TOOL_TYPE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006179 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOOL_TYPE, 0, MT_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006180 }
6181}
6182
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006183void MultiTouchInputMapperTest::processPosition(MultiTouchInputMapper& mapper, int32_t x,
6184 int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006185 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_X, x);
6186 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006187}
6188
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006189void MultiTouchInputMapperTest::processTouchMajor(MultiTouchInputMapper& mapper,
6190 int32_t touchMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006191 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006192}
6193
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006194void MultiTouchInputMapperTest::processTouchMinor(MultiTouchInputMapper& mapper,
6195 int32_t touchMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006196 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006197}
6198
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006199void MultiTouchInputMapperTest::processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006200 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006201}
6202
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006203void MultiTouchInputMapperTest::processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006204 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006205}
6206
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006207void MultiTouchInputMapperTest::processOrientation(MultiTouchInputMapper& mapper,
6208 int32_t orientation) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006209 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_ORIENTATION, orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006210}
6211
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006212void MultiTouchInputMapperTest::processPressure(MultiTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006213 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006214}
6215
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006216void MultiTouchInputMapperTest::processDistance(MultiTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006217 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006218}
6219
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006220void MultiTouchInputMapperTest::processId(MultiTouchInputMapper& mapper, int32_t id) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006221 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TRACKING_ID, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006222}
6223
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006224void MultiTouchInputMapperTest::processSlot(MultiTouchInputMapper& mapper, int32_t slot) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006225 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_SLOT, slot);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006226}
6227
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006228void MultiTouchInputMapperTest::processToolType(MultiTouchInputMapper& mapper, int32_t toolType) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006229 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006230}
6231
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006232void MultiTouchInputMapperTest::processKey(MultiTouchInputMapper& mapper, int32_t code,
6233 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006234 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006235}
6236
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006237void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006238 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_MT_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006239}
6240
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006241void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006242 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006243}
6244
Michael Wrightd02c5b62014-02-10 15:10:22 -08006245TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006246 addConfigurationProperty("touch.deviceType", "touchScreen");
6247 prepareDisplay(DISPLAY_ORIENTATION_0);
6248 prepareAxes(POSITION);
6249 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006250 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006251
arthurhungdcef2dc2020-08-11 14:47:50 +08006252 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006253
6254 NotifyMotionArgs motionArgs;
6255
6256 // Two fingers down at once.
6257 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
6258 processPosition(mapper, x1, y1);
6259 processMTSync(mapper);
6260 processPosition(mapper, x2, y2);
6261 processMTSync(mapper);
6262 processSync(mapper);
6263
6264 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6265 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6266 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6267 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6268 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6269 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6270 ASSERT_EQ(0, motionArgs.flags);
6271 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6272 ASSERT_EQ(0, motionArgs.buttonState);
6273 ASSERT_EQ(0, motionArgs.edgeFlags);
6274 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6275 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6276 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6277 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6278 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6279 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6280 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6281 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6282
6283 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6284 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6285 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6286 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6287 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6288 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6289 motionArgs.action);
6290 ASSERT_EQ(0, motionArgs.flags);
6291 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6292 ASSERT_EQ(0, motionArgs.buttonState);
6293 ASSERT_EQ(0, motionArgs.edgeFlags);
6294 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6295 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6296 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6297 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6298 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6299 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6300 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6301 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6302 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6303 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6304 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6305 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6306
6307 // Move.
6308 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
6309 processPosition(mapper, x1, y1);
6310 processMTSync(mapper);
6311 processPosition(mapper, x2, y2);
6312 processMTSync(mapper);
6313 processSync(mapper);
6314
6315 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6316 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6317 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6318 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6319 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6320 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6321 ASSERT_EQ(0, motionArgs.flags);
6322 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6323 ASSERT_EQ(0, motionArgs.buttonState);
6324 ASSERT_EQ(0, motionArgs.edgeFlags);
6325 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6326 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6327 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6328 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6329 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6330 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6331 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6332 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6333 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6334 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6335 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6336 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6337
6338 // First finger up.
6339 x2 += 15; y2 -= 20;
6340 processPosition(mapper, x2, y2);
6341 processMTSync(mapper);
6342 processSync(mapper);
6343
6344 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6345 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6346 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6347 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6348 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6349 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6350 motionArgs.action);
6351 ASSERT_EQ(0, motionArgs.flags);
6352 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6353 ASSERT_EQ(0, motionArgs.buttonState);
6354 ASSERT_EQ(0, motionArgs.edgeFlags);
6355 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6356 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6357 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6358 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6359 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6360 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6361 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6362 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6363 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6364 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6365 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6366 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6367
6368 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6369 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6370 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6371 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6372 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6373 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6374 ASSERT_EQ(0, motionArgs.flags);
6375 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6376 ASSERT_EQ(0, motionArgs.buttonState);
6377 ASSERT_EQ(0, motionArgs.edgeFlags);
6378 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6379 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6380 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6381 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6382 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6383 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6384 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6385 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6386
6387 // Move.
6388 x2 += 20; y2 -= 25;
6389 processPosition(mapper, x2, y2);
6390 processMTSync(mapper);
6391 processSync(mapper);
6392
6393 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6394 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6395 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6396 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6397 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6398 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6399 ASSERT_EQ(0, motionArgs.flags);
6400 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6401 ASSERT_EQ(0, motionArgs.buttonState);
6402 ASSERT_EQ(0, motionArgs.edgeFlags);
6403 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6404 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6405 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6406 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6407 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6408 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6409 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6410 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6411
6412 // New finger down.
6413 int32_t x3 = 700, y3 = 300;
6414 processPosition(mapper, x2, y2);
6415 processMTSync(mapper);
6416 processPosition(mapper, x3, y3);
6417 processMTSync(mapper);
6418 processSync(mapper);
6419
6420 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6421 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6422 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6423 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6424 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6425 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6426 motionArgs.action);
6427 ASSERT_EQ(0, motionArgs.flags);
6428 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6429 ASSERT_EQ(0, motionArgs.buttonState);
6430 ASSERT_EQ(0, motionArgs.edgeFlags);
6431 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6432 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6433 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6434 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6435 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6436 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6437 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6438 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6439 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6440 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6441 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6442 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6443
6444 // Second finger up.
6445 x3 += 30; y3 -= 20;
6446 processPosition(mapper, x3, y3);
6447 processMTSync(mapper);
6448 processSync(mapper);
6449
6450 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6451 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6452 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6453 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6454 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6455 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6456 motionArgs.action);
6457 ASSERT_EQ(0, motionArgs.flags);
6458 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6459 ASSERT_EQ(0, motionArgs.buttonState);
6460 ASSERT_EQ(0, motionArgs.edgeFlags);
6461 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6462 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6463 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6464 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6465 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6466 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6467 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6468 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6469 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6470 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6471 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6472 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6473
6474 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6475 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6476 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6477 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6478 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6479 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6480 ASSERT_EQ(0, motionArgs.flags);
6481 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6482 ASSERT_EQ(0, motionArgs.buttonState);
6483 ASSERT_EQ(0, motionArgs.edgeFlags);
6484 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6485 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6486 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6487 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6488 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6489 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6490 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6491 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6492
6493 // Last finger up.
6494 processMTSync(mapper);
6495 processSync(mapper);
6496
6497 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6498 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6499 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6500 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6501 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6502 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6503 ASSERT_EQ(0, motionArgs.flags);
6504 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6505 ASSERT_EQ(0, motionArgs.buttonState);
6506 ASSERT_EQ(0, motionArgs.edgeFlags);
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(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6512 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6513 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6514 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6515
6516 // Should not have sent any more keys or motions.
6517 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6518 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6519}
6520
6521TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006522 addConfigurationProperty("touch.deviceType", "touchScreen");
6523 prepareDisplay(DISPLAY_ORIENTATION_0);
6524 prepareAxes(POSITION | ID);
6525 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006526 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006527
arthurhungdcef2dc2020-08-11 14:47:50 +08006528 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006529
6530 NotifyMotionArgs motionArgs;
6531
6532 // Two fingers down at once.
6533 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
6534 processPosition(mapper, x1, y1);
6535 processId(mapper, 1);
6536 processMTSync(mapper);
6537 processPosition(mapper, x2, y2);
6538 processId(mapper, 2);
6539 processMTSync(mapper);
6540 processSync(mapper);
6541
6542 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6543 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6544 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6545 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6546 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6547 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6548 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6549
6550 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6551 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6552 motionArgs.action);
6553 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6554 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6555 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6556 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6557 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6558 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6559 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6560 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6561 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6562
6563 // Move.
6564 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
6565 processPosition(mapper, x1, y1);
6566 processId(mapper, 1);
6567 processMTSync(mapper);
6568 processPosition(mapper, x2, y2);
6569 processId(mapper, 2);
6570 processMTSync(mapper);
6571 processSync(mapper);
6572
6573 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6574 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6575 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6576 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6577 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6578 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6579 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6580 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6581 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6582 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6583 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6584
6585 // First finger up.
6586 x2 += 15; y2 -= 20;
6587 processPosition(mapper, x2, y2);
6588 processId(mapper, 2);
6589 processMTSync(mapper);
6590 processSync(mapper);
6591
6592 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6593 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6594 motionArgs.action);
6595 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6596 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6597 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6598 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6599 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6600 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6601 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6602 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6603 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6604
6605 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6606 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6607 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6608 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6609 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6610 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6611 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6612
6613 // Move.
6614 x2 += 20; y2 -= 25;
6615 processPosition(mapper, x2, y2);
6616 processId(mapper, 2);
6617 processMTSync(mapper);
6618 processSync(mapper);
6619
6620 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6621 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6622 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6623 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6624 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6625 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6626 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6627
6628 // New finger down.
6629 int32_t x3 = 700, y3 = 300;
6630 processPosition(mapper, x2, y2);
6631 processId(mapper, 2);
6632 processMTSync(mapper);
6633 processPosition(mapper, x3, y3);
6634 processId(mapper, 3);
6635 processMTSync(mapper);
6636 processSync(mapper);
6637
6638 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6639 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6640 motionArgs.action);
6641 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6642 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6643 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6644 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6645 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6646 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6647 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6648 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6649 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6650
6651 // Second finger up.
6652 x3 += 30; y3 -= 20;
6653 processPosition(mapper, x3, y3);
6654 processId(mapper, 3);
6655 processMTSync(mapper);
6656 processSync(mapper);
6657
6658 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6659 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6660 motionArgs.action);
6661 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6662 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6663 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6664 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6665 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6666 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6667 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6668 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6669 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6670
6671 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6672 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6673 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6674 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6675 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6676 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6677 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6678
6679 // Last finger up.
6680 processMTSync(mapper);
6681 processSync(mapper);
6682
6683 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6684 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6685 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6686 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6687 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6688 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6689 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6690
6691 // Should not have sent any more keys or motions.
6692 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6693 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6694}
6695
6696TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006697 addConfigurationProperty("touch.deviceType", "touchScreen");
6698 prepareDisplay(DISPLAY_ORIENTATION_0);
6699 prepareAxes(POSITION | ID | SLOT);
6700 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006701 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006702
arthurhungdcef2dc2020-08-11 14:47:50 +08006703 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006704
6705 NotifyMotionArgs motionArgs;
6706
6707 // Two fingers down at once.
6708 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
6709 processPosition(mapper, x1, y1);
6710 processId(mapper, 1);
6711 processSlot(mapper, 1);
6712 processPosition(mapper, x2, y2);
6713 processId(mapper, 2);
6714 processSync(mapper);
6715
6716 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6717 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6718 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6719 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6720 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6721 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6722 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6723
6724 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6725 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6726 motionArgs.action);
6727 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6728 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6729 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6730 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6731 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6732 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6733 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6734 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6735 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6736
6737 // Move.
6738 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
6739 processSlot(mapper, 0);
6740 processPosition(mapper, x1, y1);
6741 processSlot(mapper, 1);
6742 processPosition(mapper, x2, y2);
6743 processSync(mapper);
6744
6745 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6746 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6747 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6748 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6749 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6750 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6751 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6752 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6753 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6754 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6755 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6756
6757 // First finger up.
6758 x2 += 15; y2 -= 20;
6759 processSlot(mapper, 0);
6760 processId(mapper, -1);
6761 processSlot(mapper, 1);
6762 processPosition(mapper, x2, y2);
6763 processSync(mapper);
6764
6765 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6766 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6767 motionArgs.action);
6768 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6769 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6770 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6771 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6772 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6773 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6774 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6775 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6776 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6777
6778 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6779 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6780 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6781 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6782 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6783 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6784 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6785
6786 // Move.
6787 x2 += 20; y2 -= 25;
6788 processPosition(mapper, x2, y2);
6789 processSync(mapper);
6790
6791 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6792 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6793 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6794 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6795 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6796 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6797 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6798
6799 // New finger down.
6800 int32_t x3 = 700, y3 = 300;
6801 processPosition(mapper, x2, y2);
6802 processSlot(mapper, 0);
6803 processId(mapper, 3);
6804 processPosition(mapper, x3, y3);
6805 processSync(mapper);
6806
6807 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6808 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6809 motionArgs.action);
6810 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6811 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6812 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6813 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6814 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6815 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6816 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6817 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6818 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6819
6820 // Second finger up.
6821 x3 += 30; y3 -= 20;
6822 processSlot(mapper, 1);
6823 processId(mapper, -1);
6824 processSlot(mapper, 0);
6825 processPosition(mapper, x3, y3);
6826 processSync(mapper);
6827
6828 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6829 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6830 motionArgs.action);
6831 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6832 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6833 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6834 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6835 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6836 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6837 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6838 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6839 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6840
6841 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6842 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6843 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6844 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6845 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6846 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6847 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6848
6849 // Last finger up.
6850 processId(mapper, -1);
6851 processSync(mapper);
6852
6853 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6854 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6855 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6856 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6857 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6858 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6859 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6860
6861 // Should not have sent any more keys or motions.
6862 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6863 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6864}
6865
6866TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006867 addConfigurationProperty("touch.deviceType", "touchScreen");
6868 prepareDisplay(DISPLAY_ORIENTATION_0);
6869 prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006870 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006871
6872 // These calculations are based on the input device calibration documentation.
6873 int32_t rawX = 100;
6874 int32_t rawY = 200;
6875 int32_t rawTouchMajor = 7;
6876 int32_t rawTouchMinor = 6;
6877 int32_t rawToolMajor = 9;
6878 int32_t rawToolMinor = 8;
6879 int32_t rawPressure = 11;
6880 int32_t rawDistance = 0;
6881 int32_t rawOrientation = 3;
6882 int32_t id = 5;
6883
6884 float x = toDisplayX(rawX);
6885 float y = toDisplayY(rawY);
6886 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
6887 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
6888 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
6889 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
6890 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
6891 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
6892 float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
6893 float distance = float(rawDistance);
6894
6895 processPosition(mapper, rawX, rawY);
6896 processTouchMajor(mapper, rawTouchMajor);
6897 processTouchMinor(mapper, rawTouchMinor);
6898 processToolMajor(mapper, rawToolMajor);
6899 processToolMinor(mapper, rawToolMinor);
6900 processPressure(mapper, rawPressure);
6901 processOrientation(mapper, rawOrientation);
6902 processDistance(mapper, rawDistance);
6903 processId(mapper, id);
6904 processMTSync(mapper);
6905 processSync(mapper);
6906
6907 NotifyMotionArgs args;
6908 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6909 ASSERT_EQ(0, args.pointerProperties[0].id);
6910 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6911 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
6912 orientation, distance));
6913}
6914
6915TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006916 addConfigurationProperty("touch.deviceType", "touchScreen");
6917 prepareDisplay(DISPLAY_ORIENTATION_0);
6918 prepareAxes(POSITION | TOUCH | TOOL | MINOR);
6919 addConfigurationProperty("touch.size.calibration", "geometric");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006920 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006921
6922 // These calculations are based on the input device calibration documentation.
6923 int32_t rawX = 100;
6924 int32_t rawY = 200;
6925 int32_t rawTouchMajor = 140;
6926 int32_t rawTouchMinor = 120;
6927 int32_t rawToolMajor = 180;
6928 int32_t rawToolMinor = 160;
6929
6930 float x = toDisplayX(rawX);
6931 float y = toDisplayY(rawY);
6932 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
6933 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
6934 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
6935 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
6936 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
6937
6938 processPosition(mapper, rawX, rawY);
6939 processTouchMajor(mapper, rawTouchMajor);
6940 processTouchMinor(mapper, rawTouchMinor);
6941 processToolMajor(mapper, rawToolMajor);
6942 processToolMinor(mapper, rawToolMinor);
6943 processMTSync(mapper);
6944 processSync(mapper);
6945
6946 NotifyMotionArgs args;
6947 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6948 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6949 x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
6950}
6951
6952TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006953 addConfigurationProperty("touch.deviceType", "touchScreen");
6954 prepareDisplay(DISPLAY_ORIENTATION_0);
6955 prepareAxes(POSITION | TOUCH | TOOL);
6956 addConfigurationProperty("touch.size.calibration", "diameter");
6957 addConfigurationProperty("touch.size.scale", "10");
6958 addConfigurationProperty("touch.size.bias", "160");
6959 addConfigurationProperty("touch.size.isSummed", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006960 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006961
6962 // These calculations are based on the input device calibration documentation.
6963 // Note: We only provide a single common touch/tool value because the device is assumed
6964 // not to emit separate values for each pointer (isSummed = 1).
6965 int32_t rawX = 100;
6966 int32_t rawY = 200;
6967 int32_t rawX2 = 150;
6968 int32_t rawY2 = 250;
6969 int32_t rawTouchMajor = 5;
6970 int32_t rawToolMajor = 8;
6971
6972 float x = toDisplayX(rawX);
6973 float y = toDisplayY(rawY);
6974 float x2 = toDisplayX(rawX2);
6975 float y2 = toDisplayY(rawY2);
6976 float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
6977 float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
6978 float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
6979
6980 processPosition(mapper, rawX, rawY);
6981 processTouchMajor(mapper, rawTouchMajor);
6982 processToolMajor(mapper, rawToolMajor);
6983 processMTSync(mapper);
6984 processPosition(mapper, rawX2, rawY2);
6985 processTouchMajor(mapper, rawTouchMajor);
6986 processToolMajor(mapper, rawToolMajor);
6987 processMTSync(mapper);
6988 processSync(mapper);
6989
6990 NotifyMotionArgs args;
6991 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6992 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
6993
6994 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6995 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
6996 args.action);
6997 ASSERT_EQ(size_t(2), args.pointerCount);
6998 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6999 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
7000 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
7001 x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
7002}
7003
7004TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007005 addConfigurationProperty("touch.deviceType", "touchScreen");
7006 prepareDisplay(DISPLAY_ORIENTATION_0);
7007 prepareAxes(POSITION | TOUCH | TOOL);
7008 addConfigurationProperty("touch.size.calibration", "area");
7009 addConfigurationProperty("touch.size.scale", "43");
7010 addConfigurationProperty("touch.size.bias", "3");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007011 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007012
7013 // These calculations are based on the input device calibration documentation.
7014 int32_t rawX = 100;
7015 int32_t rawY = 200;
7016 int32_t rawTouchMajor = 5;
7017 int32_t rawToolMajor = 8;
7018
7019 float x = toDisplayX(rawX);
7020 float y = toDisplayY(rawY);
7021 float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
7022 float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
7023 float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
7024
7025 processPosition(mapper, rawX, rawY);
7026 processTouchMajor(mapper, rawTouchMajor);
7027 processToolMajor(mapper, rawToolMajor);
7028 processMTSync(mapper);
7029 processSync(mapper);
7030
7031 NotifyMotionArgs args;
7032 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7033 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7034 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
7035}
7036
7037TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007038 addConfigurationProperty("touch.deviceType", "touchScreen");
7039 prepareDisplay(DISPLAY_ORIENTATION_0);
7040 prepareAxes(POSITION | PRESSURE);
7041 addConfigurationProperty("touch.pressure.calibration", "amplitude");
7042 addConfigurationProperty("touch.pressure.scale", "0.01");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007043 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007044
Michael Wrightaa449c92017-12-13 21:21:43 +00007045 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007046 mapper.populateDeviceInfo(&info);
Michael Wrightaa449c92017-12-13 21:21:43 +00007047 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
7048 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TOUCHSCREEN,
7049 0.0f, RAW_PRESSURE_MAX * 0.01, 0.0f, 0.0f));
7050
Michael Wrightd02c5b62014-02-10 15:10:22 -08007051 // These calculations are based on the input device calibration documentation.
7052 int32_t rawX = 100;
7053 int32_t rawY = 200;
7054 int32_t rawPressure = 60;
7055
7056 float x = toDisplayX(rawX);
7057 float y = toDisplayY(rawY);
7058 float pressure = float(rawPressure) * 0.01f;
7059
7060 processPosition(mapper, rawX, rawY);
7061 processPressure(mapper, rawPressure);
7062 processMTSync(mapper);
7063 processSync(mapper);
7064
7065 NotifyMotionArgs args;
7066 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7067 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7068 x, y, pressure, 0, 0, 0, 0, 0, 0, 0));
7069}
7070
7071TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007072 addConfigurationProperty("touch.deviceType", "touchScreen");
7073 prepareDisplay(DISPLAY_ORIENTATION_0);
7074 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007075 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007076
7077 NotifyMotionArgs motionArgs;
7078 NotifyKeyArgs keyArgs;
7079
7080 processId(mapper, 1);
7081 processPosition(mapper, 100, 200);
7082 processSync(mapper);
7083 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7084 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7085 ASSERT_EQ(0, motionArgs.buttonState);
7086
7087 // press BTN_LEFT, release BTN_LEFT
7088 processKey(mapper, BTN_LEFT, 1);
7089 processSync(mapper);
7090 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7091 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7092 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
7093
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007094 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7095 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7096 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
7097
Michael Wrightd02c5b62014-02-10 15:10:22 -08007098 processKey(mapper, BTN_LEFT, 0);
7099 processSync(mapper);
7100 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007101 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007102 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007103
7104 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007105 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007106 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007107
7108 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
7109 processKey(mapper, BTN_RIGHT, 1);
7110 processKey(mapper, BTN_MIDDLE, 1);
7111 processSync(mapper);
7112 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7113 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7114 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
7115 motionArgs.buttonState);
7116
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007117 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7118 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7119 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
7120
7121 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7122 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7123 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
7124 motionArgs.buttonState);
7125
Michael Wrightd02c5b62014-02-10 15:10:22 -08007126 processKey(mapper, BTN_RIGHT, 0);
7127 processSync(mapper);
7128 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007129 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007130 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007131
7132 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007133 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007134 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007135
7136 processKey(mapper, BTN_MIDDLE, 0);
7137 processSync(mapper);
7138 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007139 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007140 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007141
7142 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007143 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007144 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007145
7146 // press BTN_BACK, release BTN_BACK
7147 processKey(mapper, BTN_BACK, 1);
7148 processSync(mapper);
7149 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7150 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
7151 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007152
Michael Wrightd02c5b62014-02-10 15:10:22 -08007153 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007154 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007155 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
7156
7157 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7158 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7159 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007160
7161 processKey(mapper, BTN_BACK, 0);
7162 processSync(mapper);
7163 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007164 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007165 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007166
7167 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007168 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007169 ASSERT_EQ(0, motionArgs.buttonState);
7170
Michael Wrightd02c5b62014-02-10 15:10:22 -08007171 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7172 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
7173 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
7174
7175 // press BTN_SIDE, release BTN_SIDE
7176 processKey(mapper, BTN_SIDE, 1);
7177 processSync(mapper);
7178 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7179 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
7180 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007181
Michael Wrightd02c5b62014-02-10 15:10:22 -08007182 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007183 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007184 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
7185
7186 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7187 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7188 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007189
7190 processKey(mapper, BTN_SIDE, 0);
7191 processSync(mapper);
7192 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007193 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007194 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007195
7196 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007197 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007198 ASSERT_EQ(0, motionArgs.buttonState);
7199
Michael Wrightd02c5b62014-02-10 15:10:22 -08007200 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7201 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
7202 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
7203
7204 // press BTN_FORWARD, release BTN_FORWARD
7205 processKey(mapper, BTN_FORWARD, 1);
7206 processSync(mapper);
7207 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7208 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
7209 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007210
Michael Wrightd02c5b62014-02-10 15:10:22 -08007211 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007212 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007213 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
7214
7215 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7216 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7217 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007218
7219 processKey(mapper, BTN_FORWARD, 0);
7220 processSync(mapper);
7221 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007222 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007223 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007224
7225 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007226 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007227 ASSERT_EQ(0, motionArgs.buttonState);
7228
Michael Wrightd02c5b62014-02-10 15:10:22 -08007229 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7230 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
7231 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
7232
7233 // press BTN_EXTRA, release BTN_EXTRA
7234 processKey(mapper, BTN_EXTRA, 1);
7235 processSync(mapper);
7236 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7237 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
7238 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007239
Michael Wrightd02c5b62014-02-10 15:10:22 -08007240 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007241 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007242 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
7243
7244 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7245 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7246 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007247
7248 processKey(mapper, BTN_EXTRA, 0);
7249 processSync(mapper);
7250 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007251 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007252 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007253
7254 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007255 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007256 ASSERT_EQ(0, motionArgs.buttonState);
7257
Michael Wrightd02c5b62014-02-10 15:10:22 -08007258 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7259 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
7260 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
7261
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007262 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7263
Michael Wrightd02c5b62014-02-10 15:10:22 -08007264 // press BTN_STYLUS, release BTN_STYLUS
7265 processKey(mapper, BTN_STYLUS, 1);
7266 processSync(mapper);
7267 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7268 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007269 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
7270
7271 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7272 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7273 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007274
7275 processKey(mapper, BTN_STYLUS, 0);
7276 processSync(mapper);
7277 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007278 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007279 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007280
7281 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007282 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007283 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007284
7285 // press BTN_STYLUS2, release BTN_STYLUS2
7286 processKey(mapper, BTN_STYLUS2, 1);
7287 processSync(mapper);
7288 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7289 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007290 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
7291
7292 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7293 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7294 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007295
7296 processKey(mapper, BTN_STYLUS2, 0);
7297 processSync(mapper);
7298 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007299 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007300 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007301
7302 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007303 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007304 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007305
7306 // release touch
7307 processId(mapper, -1);
7308 processSync(mapper);
7309 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7310 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7311 ASSERT_EQ(0, motionArgs.buttonState);
7312}
7313
7314TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007315 addConfigurationProperty("touch.deviceType", "touchScreen");
7316 prepareDisplay(DISPLAY_ORIENTATION_0);
7317 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007318 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007319
7320 NotifyMotionArgs motionArgs;
7321
7322 // default tool type is finger
7323 processId(mapper, 1);
7324 processPosition(mapper, 100, 200);
7325 processSync(mapper);
7326 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7327 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7328 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7329
7330 // eraser
7331 processKey(mapper, BTN_TOOL_RUBBER, 1);
7332 processSync(mapper);
7333 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7334 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7335 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
7336
7337 // stylus
7338 processKey(mapper, BTN_TOOL_RUBBER, 0);
7339 processKey(mapper, BTN_TOOL_PEN, 1);
7340 processSync(mapper);
7341 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7342 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7343 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7344
7345 // brush
7346 processKey(mapper, BTN_TOOL_PEN, 0);
7347 processKey(mapper, BTN_TOOL_BRUSH, 1);
7348 processSync(mapper);
7349 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7350 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7351 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7352
7353 // pencil
7354 processKey(mapper, BTN_TOOL_BRUSH, 0);
7355 processKey(mapper, BTN_TOOL_PENCIL, 1);
7356 processSync(mapper);
7357 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7358 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7359 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7360
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08007361 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08007362 processKey(mapper, BTN_TOOL_PENCIL, 0);
7363 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
7364 processSync(mapper);
7365 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7366 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7367 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7368
7369 // mouse
7370 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
7371 processKey(mapper, BTN_TOOL_MOUSE, 1);
7372 processSync(mapper);
7373 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7374 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7375 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
7376
7377 // lens
7378 processKey(mapper, BTN_TOOL_MOUSE, 0);
7379 processKey(mapper, BTN_TOOL_LENS, 1);
7380 processSync(mapper);
7381 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7382 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7383 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
7384
7385 // double-tap
7386 processKey(mapper, BTN_TOOL_LENS, 0);
7387 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
7388 processSync(mapper);
7389 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7390 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7391 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7392
7393 // triple-tap
7394 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
7395 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
7396 processSync(mapper);
7397 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7398 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7399 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7400
7401 // quad-tap
7402 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
7403 processKey(mapper, BTN_TOOL_QUADTAP, 1);
7404 processSync(mapper);
7405 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7406 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7407 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7408
7409 // finger
7410 processKey(mapper, BTN_TOOL_QUADTAP, 0);
7411 processKey(mapper, BTN_TOOL_FINGER, 1);
7412 processSync(mapper);
7413 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7414 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7415 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7416
7417 // stylus trumps finger
7418 processKey(mapper, BTN_TOOL_PEN, 1);
7419 processSync(mapper);
7420 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7421 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7422 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7423
7424 // eraser trumps stylus
7425 processKey(mapper, BTN_TOOL_RUBBER, 1);
7426 processSync(mapper);
7427 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7428 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7429 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
7430
7431 // mouse trumps eraser
7432 processKey(mapper, BTN_TOOL_MOUSE, 1);
7433 processSync(mapper);
7434 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7435 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7436 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
7437
7438 // MT tool type trumps BTN tool types: MT_TOOL_FINGER
7439 processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
7440 processSync(mapper);
7441 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7442 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7443 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7444
7445 // MT tool type trumps BTN tool types: MT_TOOL_PEN
7446 processToolType(mapper, MT_TOOL_PEN);
7447 processSync(mapper);
7448 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7449 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7450 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7451
7452 // back to default tool type
7453 processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
7454 processKey(mapper, BTN_TOOL_MOUSE, 0);
7455 processKey(mapper, BTN_TOOL_RUBBER, 0);
7456 processKey(mapper, BTN_TOOL_PEN, 0);
7457 processKey(mapper, BTN_TOOL_FINGER, 0);
7458 processSync(mapper);
7459 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7460 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7461 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7462}
7463
7464TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007465 addConfigurationProperty("touch.deviceType", "touchScreen");
7466 prepareDisplay(DISPLAY_ORIENTATION_0);
7467 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007468 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007469 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007470
7471 NotifyMotionArgs motionArgs;
7472
7473 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
7474 processId(mapper, 1);
7475 processPosition(mapper, 100, 200);
7476 processSync(mapper);
7477 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7478 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
7479 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7480 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
7481
7482 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7483 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7484 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7485 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
7486
7487 // move a little
7488 processPosition(mapper, 150, 250);
7489 processSync(mapper);
7490 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7491 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7492 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7493 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7494
7495 // down when BTN_TOUCH is pressed, pressure defaults to 1
7496 processKey(mapper, BTN_TOUCH, 1);
7497 processSync(mapper);
7498 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7499 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
7500 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7501 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7502
7503 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7504 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7505 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7506 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
7507
7508 // up when BTN_TOUCH is released, hover restored
7509 processKey(mapper, BTN_TOUCH, 0);
7510 processSync(mapper);
7511 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7512 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7513 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7514 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
7515
7516 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7517 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
7518 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7519 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7520
7521 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7522 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7523 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7524 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7525
7526 // exit hover when pointer goes away
7527 processId(mapper, -1);
7528 processSync(mapper);
7529 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7530 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
7531 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7532 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7533}
7534
7535TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007536 addConfigurationProperty("touch.deviceType", "touchScreen");
7537 prepareDisplay(DISPLAY_ORIENTATION_0);
7538 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007539 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007540
7541 NotifyMotionArgs motionArgs;
7542
7543 // initially hovering because pressure is 0
7544 processId(mapper, 1);
7545 processPosition(mapper, 100, 200);
7546 processPressure(mapper, 0);
7547 processSync(mapper);
7548 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7549 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
7550 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7551 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
7552
7553 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7554 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7555 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7556 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
7557
7558 // move a little
7559 processPosition(mapper, 150, 250);
7560 processSync(mapper);
7561 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7562 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7563 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7564 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7565
7566 // down when pressure becomes non-zero
7567 processPressure(mapper, RAW_PRESSURE_MAX);
7568 processSync(mapper);
7569 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7570 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
7571 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7572 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7573
7574 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7575 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7576 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7577 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
7578
7579 // up when pressure becomes 0, hover restored
7580 processPressure(mapper, 0);
7581 processSync(mapper);
7582 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7583 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7584 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7585 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
7586
7587 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7588 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
7589 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7590 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7591
7592 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7593 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7594 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7595 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7596
7597 // exit hover when pointer goes away
7598 processId(mapper, -1);
7599 processSync(mapper);
7600 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7601 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
7602 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7603 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7604}
7605
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07007606/**
7607 * Set the input device port <--> display port associations, and check that the
7608 * events are routed to the display that matches the display port.
7609 * This can be checked by looking at the displayId of the resulting NotifyMotionArgs.
7610 */
7611TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayPort) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07007612 const std::string usb2 = "USB2";
7613 const uint8_t hdmi1 = 0;
7614 const uint8_t hdmi2 = 1;
7615 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007616 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07007617
7618 addConfigurationProperty("touch.deviceType", "touchScreen");
7619 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007620 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07007621
7622 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
7623 mFakePolicy->addInputPortAssociation(usb2, hdmi2);
7624
7625 // We are intentionally not adding the viewport for display 1 yet. Since the port association
7626 // for this input device is specified, and the matching viewport is not present,
7627 // the input device should be disabled (at the mapper level).
7628
7629 // Add viewport for display 2 on hdmi2
7630 prepareSecondaryDisplay(type, hdmi2);
7631 // Send a touch event
7632 processPosition(mapper, 100, 100);
7633 processSync(mapper);
7634 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7635
7636 // Add viewport for display 1 on hdmi1
7637 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
7638 // Send a touch event again
7639 processPosition(mapper, 100, 100);
7640 processSync(mapper);
7641
7642 NotifyMotionArgs args;
7643 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7644 ASSERT_EQ(DISPLAY_ID, args.displayId);
7645}
Michael Wrightd02c5b62014-02-10 15:10:22 -08007646
Arthur Hungc7ad2d02018-12-18 17:41:29 +08007647TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShouldHandleDisplayId) {
Garfield Tan888a6a42020-01-09 11:39:16 -08007648 // Setup for second display.
Michael Wright17db18e2020-06-26 20:51:44 +01007649 std::shared_ptr<FakePointerController> fakePointerController =
7650 std::make_shared<FakePointerController>();
Garfield Tan888a6a42020-01-09 11:39:16 -08007651 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08007652 fakePointerController->setPosition(100, 200);
7653 fakePointerController->setButtonState(0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08007654 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
7655
Garfield Tan888a6a42020-01-09 11:39:16 -08007656 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007657 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08007658
Arthur Hungc7ad2d02018-12-18 17:41:29 +08007659 prepareDisplay(DISPLAY_ORIENTATION_0);
7660 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007661 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08007662
7663 // Check source is mouse that would obtain the PointerController.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007664 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08007665
7666 NotifyMotionArgs motionArgs;
7667 processPosition(mapper, 100, 100);
7668 processSync(mapper);
7669
7670 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7671 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7672 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
7673}
7674
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00007675/**
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007676 * Ensure that the readTime is set to the SYN_REPORT value when processing touch events.
7677 */
7678TEST_F(MultiTouchInputMapperTest, Process_SendsReadTime) {
7679 addConfigurationProperty("touch.deviceType", "touchScreen");
7680 prepareAxes(POSITION);
7681 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7682
7683 prepareDisplay(DISPLAY_ORIENTATION_0);
7684 process(mapper, 10, 11 /*readTime*/, EV_ABS, ABS_MT_TRACKING_ID, 1);
7685 process(mapper, 15, 16 /*readTime*/, EV_ABS, ABS_MT_POSITION_X, 100);
7686 process(mapper, 20, 21 /*readTime*/, EV_ABS, ABS_MT_POSITION_Y, 100);
7687 process(mapper, 25, 26 /*readTime*/, EV_SYN, SYN_REPORT, 0);
7688
7689 NotifyMotionArgs args;
7690 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7691 ASSERT_EQ(26, args.readTime);
7692
7693 process(mapper, 30, 31 /*readTime*/, EV_ABS, ABS_MT_POSITION_X, 110);
7694 process(mapper, 30, 32 /*readTime*/, EV_ABS, ABS_MT_POSITION_Y, 220);
7695 process(mapper, 30, 33 /*readTime*/, EV_SYN, SYN_REPORT, 0);
7696
7697 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7698 ASSERT_EQ(33, args.readTime);
7699}
7700
7701/**
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00007702 * When the viewport is not active (isActive=false), the touch mapper should be disabled and the
7703 * events should not be delivered to the listener.
7704 */
7705TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreDropped) {
7706 addConfigurationProperty("touch.deviceType", "touchScreen");
7707 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
7708 DISPLAY_ORIENTATION_0, false /*isActive*/, UNIQUE_ID, NO_PORT,
7709 ViewportType::INTERNAL);
7710 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
7711 prepareAxes(POSITION);
7712 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7713
7714 NotifyMotionArgs motionArgs;
7715 processPosition(mapper, 100, 100);
7716 processSync(mapper);
7717
7718 mFakeListener->assertNotifyMotionWasNotCalled();
7719}
7720
Garfield Tanc734e4f2021-01-15 20:01:39 -08007721TEST_F(MultiTouchInputMapperTest, Process_DeactivateViewport_AbortTouches) {
7722 addConfigurationProperty("touch.deviceType", "touchScreen");
7723 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
7724 DISPLAY_ORIENTATION_0, true /*isActive*/, UNIQUE_ID, NO_PORT,
7725 ViewportType::INTERNAL);
7726 std::optional<DisplayViewport> optionalDisplayViewport =
7727 mFakePolicy->getDisplayViewportByUniqueId(UNIQUE_ID);
7728 ASSERT_TRUE(optionalDisplayViewport.has_value());
7729 DisplayViewport displayViewport = *optionalDisplayViewport;
7730
7731 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
7732 prepareAxes(POSITION);
7733 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7734
7735 // Finger down
7736 int32_t x = 100, y = 100;
7737 processPosition(mapper, x, y);
7738 processSync(mapper);
7739
7740 NotifyMotionArgs motionArgs;
7741 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7742 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7743
7744 // Deactivate display viewport
7745 displayViewport.isActive = false;
7746 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
7747 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
7748
7749 // Finger move
7750 x += 10, y += 10;
7751 processPosition(mapper, x, y);
7752 processSync(mapper);
7753
7754 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7755 EXPECT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
7756
7757 // Reactivate display viewport
7758 displayViewport.isActive = true;
7759 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
7760 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
7761
7762 // Finger move again
7763 x += 10, y += 10;
7764 processPosition(mapper, x, y);
7765 processSync(mapper);
7766
7767 // Gesture is aborted, so events after display is activated won't be dispatched until there is
7768 // no pointer on the touch device.
7769 mFakeListener->assertNotifyMotionWasNotCalled();
7770}
7771
Arthur Hung7c645402019-01-25 17:45:42 +08007772TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShowTouches) {
7773 // Setup the first touch screen device.
Arthur Hung7c645402019-01-25 17:45:42 +08007774 prepareAxes(POSITION | ID | SLOT);
7775 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007776 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung7c645402019-01-25 17:45:42 +08007777
7778 // Create the second touch screen device, and enable multi fingers.
7779 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08007780 const std::string DEVICE_NAME2 = "TOUCHSCREEN2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08007781 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007782 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08007783 std::shared_ptr<InputDevice> device2 =
7784 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
7785 Flags<InputDeviceClass>(0));
7786
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007787 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
7788 0 /*flat*/, 0 /*fuzz*/);
7789 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
7790 0 /*flat*/, 0 /*fuzz*/);
7791 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX,
7792 0 /*flat*/, 0 /*fuzz*/);
7793 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX,
7794 0 /*flat*/, 0 /*fuzz*/);
7795 mFakeEventHub->setAbsoluteAxisValue(SECOND_EVENTHUB_ID, ABS_MT_SLOT, 0 /*value*/);
7796 mFakeEventHub->addConfigurationProperty(SECOND_EVENTHUB_ID, String8("touch.deviceType"),
7797 String8("touchScreen"));
Arthur Hung7c645402019-01-25 17:45:42 +08007798
7799 // Setup the second touch screen device.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007800 MultiTouchInputMapper& mapper2 = device2->addMapper<MultiTouchInputMapper>(SECOND_EVENTHUB_ID);
Arthur Hung7c645402019-01-25 17:45:42 +08007801 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
7802 device2->reset(ARBITRARY_TIME);
7803
7804 // Setup PointerController.
Michael Wright17db18e2020-06-26 20:51:44 +01007805 std::shared_ptr<FakePointerController> fakePointerController =
7806 std::make_shared<FakePointerController>();
Arthur Hung7c645402019-01-25 17:45:42 +08007807 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
7808 mFakePolicy->setPointerController(SECOND_DEVICE_ID, fakePointerController);
7809
7810 // Setup policy for associated displays and show touches.
7811 const uint8_t hdmi1 = 0;
7812 const uint8_t hdmi2 = 1;
7813 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
7814 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
7815 mFakePolicy->setShowTouches(true);
7816
7817 // Create displays.
7818 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007819 prepareSecondaryDisplay(ViewportType::EXTERNAL, hdmi2);
Arthur Hung7c645402019-01-25 17:45:42 +08007820
7821 // Default device will reconfigure above, need additional reconfiguration for another device.
7822 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007823 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung7c645402019-01-25 17:45:42 +08007824
7825 // Two fingers down at default display.
7826 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7827 processPosition(mapper, x1, y1);
7828 processId(mapper, 1);
7829 processSlot(mapper, 1);
7830 processPosition(mapper, x2, y2);
7831 processId(mapper, 2);
7832 processSync(mapper);
7833
7834 std::map<int32_t, std::vector<int32_t>>::const_iterator iter =
7835 fakePointerController->getSpots().find(DISPLAY_ID);
7836 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
7837 ASSERT_EQ(size_t(2), iter->second.size());
7838
7839 // Two fingers down at second display.
7840 processPosition(mapper2, x1, y1);
7841 processId(mapper2, 1);
7842 processSlot(mapper2, 1);
7843 processPosition(mapper2, x2, y2);
7844 processId(mapper2, 2);
7845 processSync(mapper2);
7846
7847 iter = fakePointerController->getSpots().find(SECONDARY_DISPLAY_ID);
7848 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
7849 ASSERT_EQ(size_t(2), iter->second.size());
7850}
7851
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06007852TEST_F(MultiTouchInputMapperTest, VideoFrames_ReceivedByListener) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06007853 prepareAxes(POSITION);
7854 addConfigurationProperty("touch.deviceType", "touchScreen");
7855 prepareDisplay(DISPLAY_ORIENTATION_0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007856 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06007857
7858 NotifyMotionArgs motionArgs;
7859 // Unrotated video frame
7860 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
7861 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007862 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06007863 processPosition(mapper, 100, 200);
7864 processSync(mapper);
7865 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7866 ASSERT_EQ(frames, motionArgs.videoFrames);
7867
7868 // Subsequent touch events should not have any videoframes
7869 // This is implemented separately in FakeEventHub,
7870 // but that should match the behaviour of TouchVideoDevice.
7871 processPosition(mapper, 200, 200);
7872 processSync(mapper);
7873 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7874 ASSERT_EQ(std::vector<TouchVideoFrame>(), motionArgs.videoFrames);
7875}
7876
Prabir Pradhanc14266f2021-05-12 15:56:24 -07007877TEST_F(MultiTouchInputMapperTest, VideoFrames_AreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007878 prepareAxes(POSITION);
7879 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007880 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007881 // Unrotated video frame
7882 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
7883 NotifyMotionArgs motionArgs;
7884
7885 // Test all 4 orientations
7886 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
Prabir Pradhanc14266f2021-05-12 15:56:24 -07007887 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
7888 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
7889 clearViewports();
7890 prepareDisplay(orientation);
7891 std::vector<TouchVideoFrame> frames{frame};
7892 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
7893 processPosition(mapper, 100, 200);
7894 processSync(mapper);
7895 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7896 ASSERT_EQ(frames, motionArgs.videoFrames);
7897 }
7898}
7899
7900TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_AreRotated) {
7901 prepareAxes(POSITION);
7902 addConfigurationProperty("touch.deviceType", "touchScreen");
7903 // Since InputReader works in the un-rotated coordinate space, only devices that are not
7904 // orientation-aware are affected by display rotation.
7905 addConfigurationProperty("touch.orientationAware", "0");
7906 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7907 // Unrotated video frame
7908 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
7909 NotifyMotionArgs motionArgs;
7910
7911 // Test all 4 orientations
7912 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007913 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
7914 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
7915 clearViewports();
7916 prepareDisplay(orientation);
7917 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007918 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007919 processPosition(mapper, 100, 200);
7920 processSync(mapper);
7921 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07007922 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
7923 // compared to the display. This is so that when the window transform (which contains the
7924 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
7925 // window's coordinate space.
7926 frames[0].rotate(getInverseRotation(orientation));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007927 ASSERT_EQ(frames, motionArgs.videoFrames);
7928 }
7929}
7930
Prabir Pradhanc14266f2021-05-12 15:56:24 -07007931TEST_F(MultiTouchInputMapperTest, VideoFrames_MultipleFramesAreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007932 prepareAxes(POSITION);
7933 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007934 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007935 // Unrotated video frames. There's no rule that they must all have the same dimensions,
7936 // so mix these.
7937 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
7938 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
7939 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
7940 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
7941 NotifyMotionArgs motionArgs;
7942
7943 prepareDisplay(DISPLAY_ORIENTATION_90);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007944 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007945 processPosition(mapper, 100, 200);
7946 processSync(mapper);
7947 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07007948 ASSERT_EQ(frames, motionArgs.videoFrames);
7949}
7950
7951TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_MultipleFramesAreRotated) {
7952 prepareAxes(POSITION);
7953 addConfigurationProperty("touch.deviceType", "touchScreen");
7954 // Since InputReader works in the un-rotated coordinate space, only devices that are not
7955 // orientation-aware are affected by display rotation.
7956 addConfigurationProperty("touch.orientationAware", "0");
7957 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7958 // Unrotated video frames. There's no rule that they must all have the same dimensions,
7959 // so mix these.
7960 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
7961 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
7962 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
7963 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
7964 NotifyMotionArgs motionArgs;
7965
7966 prepareDisplay(DISPLAY_ORIENTATION_90);
7967 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
7968 processPosition(mapper, 100, 200);
7969 processSync(mapper);
7970 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7971 std::for_each(frames.begin(), frames.end(), [](TouchVideoFrame& frame) {
7972 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
7973 // compared to the display. This is so that when the window transform (which contains the
7974 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
7975 // window's coordinate space.
7976 frame.rotate(getInverseRotation(DISPLAY_ORIENTATION_90));
7977 });
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06007978 ASSERT_EQ(frames, motionArgs.videoFrames);
7979}
7980
Arthur Hung9da14732019-09-02 16:16:58 +08007981/**
7982 * If we had defined port associations, but the viewport is not ready, the touch device would be
7983 * expected to be disabled, and it should be enabled after the viewport has found.
7984 */
7985TEST_F(MultiTouchInputMapperTest, Configure_EnabledForAssociatedDisplay) {
Arthur Hung9da14732019-09-02 16:16:58 +08007986 constexpr uint8_t hdmi2 = 1;
7987 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01007988 constexpr ViewportType type = ViewportType::EXTERNAL;
Arthur Hung9da14732019-09-02 16:16:58 +08007989
7990 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi2);
7991
7992 addConfigurationProperty("touch.deviceType", "touchScreen");
7993 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007994 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung9da14732019-09-02 16:16:58 +08007995
7996 ASSERT_EQ(mDevice->isEnabled(), false);
7997
7998 // Add display on hdmi2, the device should be enabled and can receive touch event.
7999 prepareSecondaryDisplay(type, hdmi2);
8000 ASSERT_EQ(mDevice->isEnabled(), true);
8001
8002 // Send a touch event.
8003 processPosition(mapper, 100, 100);
8004 processSync(mapper);
8005
8006 NotifyMotionArgs args;
8007 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8008 ASSERT_EQ(SECONDARY_DISPLAY_ID, args.displayId);
8009}
8010
Arthur Hung421eb1c2020-01-16 00:09:42 +08008011TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleSingleTouch) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08008012 addConfigurationProperty("touch.deviceType", "touchScreen");
8013 prepareDisplay(DISPLAY_ORIENTATION_0);
8014 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008015 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08008016
8017 NotifyMotionArgs motionArgs;
8018
8019 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
8020 // finger down
8021 processId(mapper, 1);
8022 processPosition(mapper, x1, y1);
8023 processSync(mapper);
8024 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8025 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8026 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8027
8028 // finger move
8029 processId(mapper, 1);
8030 processPosition(mapper, x2, y2);
8031 processSync(mapper);
8032 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8033 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8034 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8035
8036 // finger up.
8037 processId(mapper, -1);
8038 processSync(mapper);
8039 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8040 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8041 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8042
8043 // new finger down
8044 processId(mapper, 1);
8045 processPosition(mapper, x3, y3);
8046 processSync(mapper);
8047 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8048 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8049 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8050}
8051
8052/**
arthurhungcc7f9802020-04-30 17:55:40 +08008053 * Test single touch should be canceled when received the MT_TOOL_PALM event, and the following
8054 * MOVE and UP events should be ignored.
Arthur Hung421eb1c2020-01-16 00:09:42 +08008055 */
arthurhungcc7f9802020-04-30 17:55:40 +08008056TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_SinglePointer) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08008057 addConfigurationProperty("touch.deviceType", "touchScreen");
8058 prepareDisplay(DISPLAY_ORIENTATION_0);
8059 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008060 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08008061
8062 NotifyMotionArgs motionArgs;
8063
8064 // default tool type is finger
8065 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
arthurhungcc7f9802020-04-30 17:55:40 +08008066 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008067 processPosition(mapper, x1, y1);
8068 processSync(mapper);
8069 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8070 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8071 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8072
8073 // Tool changed to MT_TOOL_PALM expect sending the cancel event.
8074 processToolType(mapper, MT_TOOL_PALM);
8075 processSync(mapper);
8076 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8077 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
8078
8079 // Ignore the following MOVE and UP events if had detect a palm event.
arthurhungcc7f9802020-04-30 17:55:40 +08008080 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008081 processPosition(mapper, x2, y2);
8082 processSync(mapper);
8083 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8084
8085 // finger up.
arthurhungcc7f9802020-04-30 17:55:40 +08008086 processId(mapper, INVALID_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008087 processSync(mapper);
8088 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8089
8090 // new finger down
arthurhungcc7f9802020-04-30 17:55:40 +08008091 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008092 processToolType(mapper, MT_TOOL_FINGER);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008093 processPosition(mapper, x3, y3);
8094 processSync(mapper);
8095 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8096 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8097 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8098}
8099
arthurhungbf89a482020-04-17 17:37:55 +08008100/**
arthurhungcc7f9802020-04-30 17:55:40 +08008101 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
8102 * and the rest active fingers could still be allowed to receive the events
arthurhungbf89a482020-04-17 17:37:55 +08008103 */
arthurhungcc7f9802020-04-30 17:55:40 +08008104TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_TwoPointers) {
arthurhungbf89a482020-04-17 17:37:55 +08008105 addConfigurationProperty("touch.deviceType", "touchScreen");
8106 prepareDisplay(DISPLAY_ORIENTATION_0);
8107 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
8108 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8109
8110 NotifyMotionArgs motionArgs;
8111
8112 // default tool type is finger
arthurhungcc7f9802020-04-30 17:55:40 +08008113 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
8114 processId(mapper, FIRST_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08008115 processPosition(mapper, x1, y1);
8116 processSync(mapper);
8117 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8118 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8119 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8120
8121 // Second finger down.
arthurhungcc7f9802020-04-30 17:55:40 +08008122 processSlot(mapper, SECOND_SLOT);
8123 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08008124 processPosition(mapper, x2, y2);
arthurhungcc7f9802020-04-30 17:55:40 +08008125 processSync(mapper);
8126 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8127 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
8128 motionArgs.action);
8129 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8130
8131 // If the tool type of the first finger changes to MT_TOOL_PALM,
8132 // we expect to receive ACTION_POINTER_UP with cancel flag.
8133 processSlot(mapper, FIRST_SLOT);
8134 processId(mapper, FIRST_TRACKING_ID);
8135 processToolType(mapper, MT_TOOL_PALM);
8136 processSync(mapper);
8137 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8138 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
8139 motionArgs.action);
8140 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
8141
8142 // The following MOVE events of second finger should be processed.
8143 processSlot(mapper, SECOND_SLOT);
8144 processId(mapper, SECOND_TRACKING_ID);
8145 processPosition(mapper, x2 + 1, y2 + 1);
8146 processSync(mapper);
8147 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8148 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8149 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8150
8151 // First finger up. It used to be in palm mode, and we already generated ACTION_POINTER_UP for
8152 // it. Second finger receive move.
8153 processSlot(mapper, FIRST_SLOT);
8154 processId(mapper, INVALID_TRACKING_ID);
8155 processSync(mapper);
8156 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8157 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8158 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8159
8160 // Second finger keeps moving.
8161 processSlot(mapper, SECOND_SLOT);
8162 processId(mapper, SECOND_TRACKING_ID);
8163 processPosition(mapper, x2 + 2, y2 + 2);
8164 processSync(mapper);
8165 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8166 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8167 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8168
8169 // Second finger up.
8170 processId(mapper, INVALID_TRACKING_ID);
8171 processSync(mapper);
8172 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8173 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8174 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
8175}
8176
8177/**
8178 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event, if only 1 finger
8179 * is active, it should send CANCEL after receiving the MT_TOOL_PALM event.
8180 */
8181TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_ShouldCancelWhenAllTouchIsPalm) {
8182 addConfigurationProperty("touch.deviceType", "touchScreen");
8183 prepareDisplay(DISPLAY_ORIENTATION_0);
8184 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
8185 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8186
8187 NotifyMotionArgs motionArgs;
8188
8189 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
8190 // First finger down.
8191 processId(mapper, FIRST_TRACKING_ID);
8192 processPosition(mapper, x1, y1);
8193 processSync(mapper);
8194 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8195 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8196 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8197
8198 // Second finger down.
8199 processSlot(mapper, SECOND_SLOT);
8200 processId(mapper, SECOND_TRACKING_ID);
8201 processPosition(mapper, x2, y2);
arthurhungbf89a482020-04-17 17:37:55 +08008202 processSync(mapper);
8203 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8204 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
8205 motionArgs.action);
8206 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8207
arthurhungcc7f9802020-04-30 17:55:40 +08008208 // If the tool type of the first finger changes to MT_TOOL_PALM,
8209 // we expect to receive ACTION_POINTER_UP with cancel flag.
8210 processSlot(mapper, FIRST_SLOT);
8211 processId(mapper, FIRST_TRACKING_ID);
8212 processToolType(mapper, MT_TOOL_PALM);
8213 processSync(mapper);
8214 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8215 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
8216 motionArgs.action);
8217 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
8218
8219 // Second finger keeps moving.
8220 processSlot(mapper, SECOND_SLOT);
8221 processId(mapper, SECOND_TRACKING_ID);
8222 processPosition(mapper, x2 + 1, y2 + 1);
8223 processSync(mapper);
8224 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8225 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8226
8227 // second finger becomes palm, receive cancel due to only 1 finger is active.
8228 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08008229 processToolType(mapper, MT_TOOL_PALM);
8230 processSync(mapper);
8231 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8232 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
8233
arthurhungcc7f9802020-04-30 17:55:40 +08008234 // third finger down.
8235 processSlot(mapper, THIRD_SLOT);
8236 processId(mapper, THIRD_TRACKING_ID);
8237 processToolType(mapper, MT_TOOL_FINGER);
arthurhungbf89a482020-04-17 17:37:55 +08008238 processPosition(mapper, x3, y3);
8239 processSync(mapper);
arthurhungbf89a482020-04-17 17:37:55 +08008240 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8241 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8242 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08008243 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8244
8245 // third finger move
8246 processId(mapper, THIRD_TRACKING_ID);
8247 processPosition(mapper, x3 + 1, y3 + 1);
8248 processSync(mapper);
8249 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8250 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8251
8252 // first finger up, third finger receive move.
8253 processSlot(mapper, FIRST_SLOT);
8254 processId(mapper, INVALID_TRACKING_ID);
8255 processSync(mapper);
8256 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8257 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8258 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8259
8260 // second finger up, third finger receive move.
8261 processSlot(mapper, SECOND_SLOT);
8262 processId(mapper, INVALID_TRACKING_ID);
8263 processSync(mapper);
8264 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8265 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8266 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8267
8268 // third finger up.
8269 processSlot(mapper, THIRD_SLOT);
8270 processId(mapper, INVALID_TRACKING_ID);
8271 processSync(mapper);
8272 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8273 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8274 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
8275}
8276
8277/**
8278 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
8279 * and the active finger could still be allowed to receive the events
8280 */
8281TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_KeepFirstPointer) {
8282 addConfigurationProperty("touch.deviceType", "touchScreen");
8283 prepareDisplay(DISPLAY_ORIENTATION_0);
8284 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
8285 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8286
8287 NotifyMotionArgs motionArgs;
8288
8289 // default tool type is finger
8290 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
8291 processId(mapper, FIRST_TRACKING_ID);
8292 processPosition(mapper, x1, y1);
8293 processSync(mapper);
8294 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8295 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8296 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8297
8298 // Second finger down.
8299 processSlot(mapper, SECOND_SLOT);
8300 processId(mapper, SECOND_TRACKING_ID);
8301 processPosition(mapper, x2, y2);
8302 processSync(mapper);
8303 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8304 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
8305 motionArgs.action);
8306 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8307
8308 // If the tool type of the second finger changes to MT_TOOL_PALM,
8309 // we expect to receive ACTION_POINTER_UP with cancel flag.
8310 processId(mapper, SECOND_TRACKING_ID);
8311 processToolType(mapper, MT_TOOL_PALM);
8312 processSync(mapper);
8313 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8314 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
8315 motionArgs.action);
8316 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
8317
8318 // The following MOVE event should be processed.
8319 processSlot(mapper, FIRST_SLOT);
8320 processId(mapper, FIRST_TRACKING_ID);
8321 processPosition(mapper, x1 + 1, y1 + 1);
8322 processSync(mapper);
8323 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8324 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8325 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8326
8327 // second finger up.
8328 processSlot(mapper, SECOND_SLOT);
8329 processId(mapper, INVALID_TRACKING_ID);
8330 processSync(mapper);
8331 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8332 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8333
8334 // first finger keep moving
8335 processSlot(mapper, FIRST_SLOT);
8336 processId(mapper, FIRST_TRACKING_ID);
8337 processPosition(mapper, x1 + 2, y1 + 2);
8338 processSync(mapper);
8339 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8340 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8341
8342 // first finger up.
8343 processId(mapper, INVALID_TRACKING_ID);
8344 processSync(mapper);
8345 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8346 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8347 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
arthurhungbf89a482020-04-17 17:37:55 +08008348}
8349
Arthur Hung9ad18942021-06-19 02:04:46 +00008350/**
8351 * Test multi-touch should sent ACTION_POINTER_UP/ACTION_UP when received the INVALID_TRACKING_ID,
8352 * to prevent the driver side may send unexpected data after set tracking id as INVALID_TRACKING_ID
8353 * cause slot be valid again.
8354 */
8355TEST_F(MultiTouchInputMapperTest, Process_MultiTouch_WithInvalidTrackingId) {
8356 addConfigurationProperty("touch.deviceType", "touchScreen");
8357 prepareDisplay(DISPLAY_ORIENTATION_0);
8358 prepareAxes(POSITION | ID | SLOT | PRESSURE);
8359 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8360
8361 NotifyMotionArgs motionArgs;
8362
8363 constexpr int32_t x1 = 100, y1 = 200, x2 = 0, y2 = 0;
8364 // First finger down.
8365 processId(mapper, FIRST_TRACKING_ID);
8366 processPosition(mapper, x1, y1);
8367 processPressure(mapper, RAW_PRESSURE_MAX);
8368 processSync(mapper);
8369 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8370 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8371 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8372
8373 // First finger move.
8374 processId(mapper, FIRST_TRACKING_ID);
8375 processPosition(mapper, x1 + 1, y1 + 1);
8376 processPressure(mapper, RAW_PRESSURE_MAX);
8377 processSync(mapper);
8378 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8379 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8380 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8381
8382 // Second finger down.
8383 processSlot(mapper, SECOND_SLOT);
8384 processId(mapper, SECOND_TRACKING_ID);
8385 processPosition(mapper, x2, y2);
8386 processPressure(mapper, RAW_PRESSURE_MAX);
8387 processSync(mapper);
8388 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8389 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
8390 motionArgs.action);
8391 ASSERT_EQ(uint32_t(2), motionArgs.pointerCount);
8392
8393 // second finger up with some unexpected data.
8394 processSlot(mapper, SECOND_SLOT);
8395 processId(mapper, INVALID_TRACKING_ID);
8396 processPosition(mapper, x2, y2);
8397 processSync(mapper);
8398 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8399 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
8400 motionArgs.action);
8401 ASSERT_EQ(uint32_t(2), motionArgs.pointerCount);
8402
8403 // first finger up with some unexpected data.
8404 processSlot(mapper, FIRST_SLOT);
8405 processId(mapper, INVALID_TRACKING_ID);
8406 processPosition(mapper, x2, y2);
8407 processPressure(mapper, RAW_PRESSURE_MAX);
8408 processSync(mapper);
8409 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8410 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8411 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8412}
8413
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008414// --- MultiTouchInputMapperTest_ExternalDevice ---
8415
8416class MultiTouchInputMapperTest_ExternalDevice : public MultiTouchInputMapperTest {
8417protected:
Chris Yea52ade12020-08-27 16:49:20 -07008418 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008419};
8420
8421/**
8422 * Expect fallback to internal viewport if device is external and external viewport is not present.
8423 */
8424TEST_F(MultiTouchInputMapperTest_ExternalDevice, Viewports_Fallback) {
8425 prepareAxes(POSITION);
8426 addConfigurationProperty("touch.deviceType", "touchScreen");
8427 prepareDisplay(DISPLAY_ORIENTATION_0);
8428 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8429
8430 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
8431
8432 NotifyMotionArgs motionArgs;
8433
8434 // Expect the event to be sent to the internal viewport,
8435 // because an external viewport is not present.
8436 processPosition(mapper, 100, 100);
8437 processSync(mapper);
8438 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8439 ASSERT_EQ(ADISPLAY_ID_DEFAULT, motionArgs.displayId);
8440
8441 // Expect the event to be sent to the external viewport if it is present.
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008442 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008443 processPosition(mapper, 100, 100);
8444 processSync(mapper);
8445 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8446 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
8447}
Arthur Hung4197f6b2020-03-16 15:39:59 +08008448
8449/**
8450 * Test touch should not work if outside of surface.
8451 */
8452class MultiTouchInputMapperTest_SurfaceRange : public MultiTouchInputMapperTest {
8453protected:
8454 void halfDisplayToCenterHorizontal(int32_t orientation) {
8455 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008456 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Arthur Hung4197f6b2020-03-16 15:39:59 +08008457
8458 // Half display to (width/4, 0, width * 3/4, height) to make display has offset.
8459 internalViewport->orientation = orientation;
8460 if (orientation == DISPLAY_ORIENTATION_90 || orientation == DISPLAY_ORIENTATION_270) {
8461 internalViewport->logicalLeft = 0;
8462 internalViewport->logicalTop = 0;
8463 internalViewport->logicalRight = DISPLAY_HEIGHT;
8464 internalViewport->logicalBottom = DISPLAY_WIDTH / 2;
8465
8466 internalViewport->physicalLeft = 0;
8467 internalViewport->physicalTop = DISPLAY_WIDTH / 4;
8468 internalViewport->physicalRight = DISPLAY_HEIGHT;
8469 internalViewport->physicalBottom = DISPLAY_WIDTH * 3 / 4;
8470
8471 internalViewport->deviceWidth = DISPLAY_HEIGHT;
8472 internalViewport->deviceHeight = DISPLAY_WIDTH;
8473 } else {
8474 internalViewport->logicalLeft = 0;
8475 internalViewport->logicalTop = 0;
8476 internalViewport->logicalRight = DISPLAY_WIDTH / 2;
8477 internalViewport->logicalBottom = DISPLAY_HEIGHT;
8478
8479 internalViewport->physicalLeft = DISPLAY_WIDTH / 4;
8480 internalViewport->physicalTop = 0;
8481 internalViewport->physicalRight = DISPLAY_WIDTH * 3 / 4;
8482 internalViewport->physicalBottom = DISPLAY_HEIGHT;
8483
8484 internalViewport->deviceWidth = DISPLAY_WIDTH;
8485 internalViewport->deviceHeight = DISPLAY_HEIGHT;
8486 }
8487
8488 mFakePolicy->updateViewport(internalViewport.value());
8489 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8490 }
8491
arthurhung5d547942020-12-14 17:04:45 +08008492 void processPositionAndVerify(MultiTouchInputMapper& mapper, int32_t xOutside, int32_t yOutside,
8493 int32_t xInside, int32_t yInside, int32_t xExpected,
Arthur Hung4197f6b2020-03-16 15:39:59 +08008494 int32_t yExpected) {
8495 // touch on outside area should not work.
8496 processPosition(mapper, toRawX(xOutside), toRawY(yOutside));
8497 processSync(mapper);
8498 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8499
8500 // touch on inside area should receive the event.
8501 NotifyMotionArgs args;
8502 processPosition(mapper, toRawX(xInside), toRawY(yInside));
8503 processSync(mapper);
8504 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8505 ASSERT_NEAR(xExpected, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
8506 ASSERT_NEAR(yExpected, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
8507
8508 // Reset.
8509 mapper.reset(ARBITRARY_TIME);
8510 }
8511};
8512
arthurhung5d547942020-12-14 17:04:45 +08008513TEST_F(MultiTouchInputMapperTest_SurfaceRange, Viewports_SurfaceRange) {
Arthur Hung4197f6b2020-03-16 15:39:59 +08008514 addConfigurationProperty("touch.deviceType", "touchScreen");
8515 prepareDisplay(DISPLAY_ORIENTATION_0);
8516 prepareAxes(POSITION);
8517 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8518
8519 // Touch on center of normal display should work.
8520 const int32_t x = DISPLAY_WIDTH / 4;
8521 const int32_t y = DISPLAY_HEIGHT / 2;
8522 processPosition(mapper, toRawX(x), toRawY(y));
8523 processSync(mapper);
8524 NotifyMotionArgs args;
8525 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8526 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], x, y, 1.0f, 0.0f, 0.0f, 0.0f,
8527 0.0f, 0.0f, 0.0f, 0.0f));
8528 // Reset.
8529 mapper.reset(ARBITRARY_TIME);
8530
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008531 // Let physical display be different to device, and make surface and physical could be 1:1 in
8532 // all four orientations.
8533 for (int orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90, DISPLAY_ORIENTATION_180,
8534 DISPLAY_ORIENTATION_270}) {
8535 halfDisplayToCenterHorizontal(orientation);
Arthur Hung4197f6b2020-03-16 15:39:59 +08008536
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008537 const int32_t xExpected = (x + 1) - (DISPLAY_WIDTH / 4);
8538 const int32_t yExpected = y;
8539 processPositionAndVerify(mapper, x - 1, y, x + 1, y, xExpected, yExpected);
8540 }
Arthur Hung4197f6b2020-03-16 15:39:59 +08008541}
8542
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008543TEST_F(MultiTouchInputMapperTest_SurfaceRange, Viewports_SurfaceRange_90_NotOrientationAware) {
Arthur Hung4197f6b2020-03-16 15:39:59 +08008544 addConfigurationProperty("touch.deviceType", "touchScreen");
8545 prepareDisplay(DISPLAY_ORIENTATION_0);
8546 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008547 // Since InputReader works in the un-rotated coordinate space, only devices that are not
8548 // orientation-aware are affected by display rotation.
8549 addConfigurationProperty("touch.orientationAware", "0");
Arthur Hung4197f6b2020-03-16 15:39:59 +08008550 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8551
8552 // Half display to (width/4, 0, width * 3/4, height) and rotate 90-degrees.
8553 halfDisplayToCenterHorizontal(DISPLAY_ORIENTATION_90);
8554
8555 const int32_t x = DISPLAY_WIDTH / 4;
8556 const int32_t y = DISPLAY_HEIGHT / 2;
8557
Arthur Hung4197f6b2020-03-16 15:39:59 +08008558 // expect x/y = swap x/y then reverse x.
8559 constexpr int32_t xExpected = DISPLAY_HEIGHT - y;
8560 constexpr int32_t yExpected = (x + 1) - DISPLAY_WIDTH / 4;
8561 processPositionAndVerify(mapper, x - 1, y, x + 1, y, xExpected, yExpected);
8562}
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08008563
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008564TEST_F(MultiTouchInputMapperTest_SurfaceRange, Viewports_SurfaceRange_270_NotOrientationAware) {
arthurhunga36b28e2020-12-29 20:28:15 +08008565 addConfigurationProperty("touch.deviceType", "touchScreen");
8566 prepareDisplay(DISPLAY_ORIENTATION_0);
8567 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008568 // Since InputReader works in the un-rotated coordinate space, only devices that are not
8569 // orientation-aware are affected by display rotation.
8570 addConfigurationProperty("touch.orientationAware", "0");
8571 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8572
8573 // Half display to (width/4, 0, width * 3/4, height) and rotate 270-degrees.
8574 halfDisplayToCenterHorizontal(DISPLAY_ORIENTATION_270);
8575
8576 const int32_t x = DISPLAY_WIDTH / 4;
8577 const int32_t y = DISPLAY_HEIGHT / 2;
8578
8579 // expect x/y = swap x/y then reverse y.
8580 const int32_t xExpected = y;
8581 const int32_t yExpected = (DISPLAY_WIDTH * 3 / 4) - (x + 1);
8582 processPositionAndVerify(mapper, x - 1, y, x + 1, y, xExpected, yExpected);
8583}
8584
8585TEST_F(MultiTouchInputMapperTest_SurfaceRange, Viewports_SurfaceRange_Corner_NotOrientationAware) {
8586 addConfigurationProperty("touch.deviceType", "touchScreen");
8587 prepareDisplay(DISPLAY_ORIENTATION_0);
8588 prepareAxes(POSITION);
8589 // Since InputReader works in the un-rotated coordinate space, only devices that are not
8590 // orientation-aware are affected by display rotation.
8591 addConfigurationProperty("touch.orientationAware", "0");
arthurhunga36b28e2020-12-29 20:28:15 +08008592 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8593
8594 const int32_t x = 0;
8595 const int32_t y = 0;
8596
8597 const int32_t xExpected = x;
8598 const int32_t yExpected = y;
8599 processPositionAndVerify(mapper, x - 1, y, x, y, xExpected, yExpected);
8600
8601 clearViewports();
8602 prepareDisplay(DISPLAY_ORIENTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008603 // expect x/y = swap x/y then reverse x.
8604 const int32_t xExpected90 = DISPLAY_HEIGHT - 1;
8605 const int32_t yExpected90 = x;
arthurhunga36b28e2020-12-29 20:28:15 +08008606 processPositionAndVerify(mapper, x - 1, y, x, y, xExpected90, yExpected90);
8607
8608 clearViewports();
8609 prepareDisplay(DISPLAY_ORIENTATION_270);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008610 // expect x/y = swap x/y then reverse y.
8611 const int32_t xExpected270 = y;
8612 const int32_t yExpected270 = DISPLAY_WIDTH - 1;
arthurhunga36b28e2020-12-29 20:28:15 +08008613 processPositionAndVerify(mapper, x - 1, y, x, y, xExpected270, yExpected270);
8614}
8615
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08008616TEST_F(MultiTouchInputMapperTest, Process_TouchpadCapture) {
8617 // we need a pointer controller for mouse mode of touchpad (start pointer at 0,0)
8618 std::shared_ptr<FakePointerController> fakePointerController =
8619 std::make_shared<FakePointerController>();
8620 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
8621 fakePointerController->setPosition(0, 0);
8622 fakePointerController->setButtonState(0);
8623
8624 // prepare device and capture
8625 prepareDisplay(DISPLAY_ORIENTATION_0);
8626 prepareAxes(POSITION | ID | SLOT);
8627 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
8628 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
8629 mFakePolicy->setPointerCapture(true);
8630 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
8631 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8632
8633 // captured touchpad should be a touchpad source
8634 NotifyDeviceResetArgs resetArgs;
8635 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
8636 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
8637
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00008638 InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
Chris Yef74dc422020-09-02 22:41:50 -07008639
8640 const InputDeviceInfo::MotionRange* relRangeX =
8641 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_X, AINPUT_SOURCE_TOUCHPAD);
8642 ASSERT_NE(relRangeX, nullptr);
8643 ASSERT_EQ(relRangeX->min, -(RAW_X_MAX - RAW_X_MIN));
8644 ASSERT_EQ(relRangeX->max, RAW_X_MAX - RAW_X_MIN);
8645 const InputDeviceInfo::MotionRange* relRangeY =
8646 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_Y, AINPUT_SOURCE_TOUCHPAD);
8647 ASSERT_NE(relRangeY, nullptr);
8648 ASSERT_EQ(relRangeY->min, -(RAW_Y_MAX - RAW_Y_MIN));
8649 ASSERT_EQ(relRangeY->max, RAW_Y_MAX - RAW_Y_MIN);
8650
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08008651 // run captured pointer tests - note that this is unscaled, so input listener events should be
8652 // identical to what the hardware sends (accounting for any
8653 // calibration).
8654 // FINGER 0 DOWN
Chris Ye364fdb52020-08-05 15:07:56 -07008655 processSlot(mapper, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08008656 processId(mapper, 1);
8657 processPosition(mapper, 100 + RAW_X_MIN, 100 + RAW_Y_MIN);
8658 processKey(mapper, BTN_TOUCH, 1);
8659 processSync(mapper);
8660
8661 // expect coord[0] to contain initial location of touch 0
8662 NotifyMotionArgs args;
8663 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8664 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
8665 ASSERT_EQ(1U, args.pointerCount);
8666 ASSERT_EQ(0, args.pointerProperties[0].id);
8667 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, args.source);
8668 ASSERT_NO_FATAL_FAILURE(
8669 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
8670
8671 // FINGER 1 DOWN
8672 processSlot(mapper, 1);
8673 processId(mapper, 2);
8674 processPosition(mapper, 560 + RAW_X_MIN, 154 + RAW_Y_MIN);
8675 processSync(mapper);
8676
8677 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
8678 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Chris Ye364fdb52020-08-05 15:07:56 -07008679 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
8680 args.action);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08008681 ASSERT_EQ(2U, args.pointerCount);
8682 ASSERT_EQ(0, args.pointerProperties[0].id);
8683 ASSERT_EQ(1, args.pointerProperties[1].id);
8684 ASSERT_NO_FATAL_FAILURE(
8685 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
8686 ASSERT_NO_FATAL_FAILURE(
8687 assertPointerCoords(args.pointerCoords[1], 560, 154, 1, 0, 0, 0, 0, 0, 0, 0));
8688
8689 // FINGER 1 MOVE
8690 processPosition(mapper, 540 + RAW_X_MIN, 690 + RAW_Y_MIN);
8691 processSync(mapper);
8692
8693 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
8694 // from move
8695 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8696 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
8697 ASSERT_NO_FATAL_FAILURE(
8698 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
8699 ASSERT_NO_FATAL_FAILURE(
8700 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
8701
8702 // FINGER 0 MOVE
8703 processSlot(mapper, 0);
8704 processPosition(mapper, 50 + RAW_X_MIN, 800 + RAW_Y_MIN);
8705 processSync(mapper);
8706
8707 // expect coord[0] to contain new touch 0 location, coord[1] to contain previous location
8708 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8709 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
8710 ASSERT_NO_FATAL_FAILURE(
8711 assertPointerCoords(args.pointerCoords[0], 50, 800, 1, 0, 0, 0, 0, 0, 0, 0));
8712 ASSERT_NO_FATAL_FAILURE(
8713 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
8714
8715 // BUTTON DOWN
8716 processKey(mapper, BTN_LEFT, 1);
8717 processSync(mapper);
8718
8719 // touchinputmapper design sends a move before button press
8720 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8721 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
8722 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8723 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
8724
8725 // BUTTON UP
8726 processKey(mapper, BTN_LEFT, 0);
8727 processSync(mapper);
8728
8729 // touchinputmapper design sends a move after button release
8730 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8731 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
8732 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8733 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
8734
8735 // FINGER 0 UP
8736 processId(mapper, -1);
8737 processSync(mapper);
8738 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8739 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | 0x0000, args.action);
8740
8741 // FINGER 1 MOVE
8742 processSlot(mapper, 1);
8743 processPosition(mapper, 320 + RAW_X_MIN, 900 + RAW_Y_MIN);
8744 processSync(mapper);
8745
8746 // expect coord[0] to contain new location of touch 1, and properties[0].id to contain 1
8747 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8748 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
8749 ASSERT_EQ(1U, args.pointerCount);
8750 ASSERT_EQ(1, args.pointerProperties[0].id);
8751 ASSERT_NO_FATAL_FAILURE(
8752 assertPointerCoords(args.pointerCoords[0], 320, 900, 1, 0, 0, 0, 0, 0, 0, 0));
8753
8754 // FINGER 1 UP
8755 processId(mapper, -1);
8756 processKey(mapper, BTN_TOUCH, 0);
8757 processSync(mapper);
8758 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8759 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
8760
8761 // non captured touchpad should be a mouse source
8762 mFakePolicy->setPointerCapture(false);
8763 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
8764 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
8765 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
8766}
8767
8768TEST_F(MultiTouchInputMapperTest, Process_UnCapturedTouchpadPointer) {
8769 std::shared_ptr<FakePointerController> fakePointerController =
8770 std::make_shared<FakePointerController>();
8771 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
8772 fakePointerController->setPosition(0, 0);
8773 fakePointerController->setButtonState(0);
8774
8775 // prepare device and capture
8776 prepareDisplay(DISPLAY_ORIENTATION_0);
8777 prepareAxes(POSITION | ID | SLOT);
8778 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
8779 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
8780 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
8781 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8782 // run uncaptured pointer tests - pushes out generic events
8783 // FINGER 0 DOWN
8784 processId(mapper, 3);
8785 processPosition(mapper, 100, 100);
8786 processKey(mapper, BTN_TOUCH, 1);
8787 processSync(mapper);
8788
8789 // start at (100,100), cursor should be at (0,0) * scale
8790 NotifyMotionArgs args;
8791 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8792 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
8793 ASSERT_NO_FATAL_FAILURE(
8794 assertPointerCoords(args.pointerCoords[0], 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
8795
8796 // FINGER 0 MOVE
8797 processPosition(mapper, 200, 200);
8798 processSync(mapper);
8799
8800 // compute scaling to help with touch position checking
8801 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
8802 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
8803 float scale =
8804 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
8805
8806 // translate from (100,100) -> (200,200), cursor should have changed to (100,100) * scale)
8807 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8808 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
8809 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 100 * scale, 100 * scale, 0,
8810 0, 0, 0, 0, 0, 0, 0));
8811}
8812
8813TEST_F(MultiTouchInputMapperTest, WhenCapturedAndNotCaptured_GetSources) {
8814 std::shared_ptr<FakePointerController> fakePointerController =
8815 std::make_shared<FakePointerController>();
8816
8817 prepareDisplay(DISPLAY_ORIENTATION_0);
8818 prepareAxes(POSITION | ID | SLOT);
8819 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
8820 mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
8821 mFakePolicy->setPointerCapture(false);
8822 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8823
8824 // uncaptured touchpad should be a pointer device
8825 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
8826
8827 // captured touchpad should be a touchpad device
8828 mFakePolicy->setPointerCapture(true);
8829 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
8830 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
8831}
8832
Chris Ye1dd2e5c2021-04-04 23:12:41 -07008833// --- PeripheralControllerTest ---
Chris Yee2b1e5c2021-03-10 22:45:12 -08008834
Chris Ye1dd2e5c2021-04-04 23:12:41 -07008835class PeripheralControllerTest : public testing::Test {
Chris Yee2b1e5c2021-03-10 22:45:12 -08008836protected:
8837 static const char* DEVICE_NAME;
8838 static const char* DEVICE_LOCATION;
8839 static const int32_t DEVICE_ID;
8840 static const int32_t DEVICE_GENERATION;
8841 static const int32_t DEVICE_CONTROLLER_NUMBER;
8842 static const Flags<InputDeviceClass> DEVICE_CLASSES;
8843 static const int32_t EVENTHUB_ID;
8844
8845 std::shared_ptr<FakeEventHub> mFakeEventHub;
8846 sp<FakeInputReaderPolicy> mFakePolicy;
8847 sp<TestInputListener> mFakeListener;
8848 std::unique_ptr<InstrumentedInputReader> mReader;
8849 std::shared_ptr<InputDevice> mDevice;
8850
8851 virtual void SetUp(Flags<InputDeviceClass> classes) {
8852 mFakeEventHub = std::make_unique<FakeEventHub>();
8853 mFakePolicy = new FakeInputReaderPolicy();
8854 mFakeListener = new TestInputListener();
8855 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
8856 mFakeListener);
8857 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
8858 }
8859
8860 void SetUp() override { SetUp(DEVICE_CLASSES); }
8861
8862 void TearDown() override {
8863 mFakeListener.clear();
8864 mFakePolicy.clear();
8865 }
8866
8867 void configureDevice(uint32_t changes) {
8868 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
8869 mReader->requestRefreshConfiguration(changes);
8870 mReader->loopOnce();
8871 }
8872 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
8873 }
8874
8875 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
8876 const std::string& location, int32_t eventHubId,
8877 Flags<InputDeviceClass> classes) {
8878 InputDeviceIdentifier identifier;
8879 identifier.name = name;
8880 identifier.location = location;
8881 std::shared_ptr<InputDevice> device =
8882 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
8883 identifier);
8884 mReader->pushNextDevice(device);
8885 mFakeEventHub->addDevice(eventHubId, name, classes);
8886 mReader->loopOnce();
8887 return device;
8888 }
8889
8890 template <class T, typename... Args>
8891 T& addControllerAndConfigure(Args... args) {
8892 T& controller = mDevice->addController<T>(EVENTHUB_ID, args...);
8893
8894 return controller;
8895 }
8896};
8897
Chris Ye1dd2e5c2021-04-04 23:12:41 -07008898const char* PeripheralControllerTest::DEVICE_NAME = "device";
8899const char* PeripheralControllerTest::DEVICE_LOCATION = "BLUETOOTH";
8900const int32_t PeripheralControllerTest::DEVICE_ID = END_RESERVED_ID + 1000;
8901const int32_t PeripheralControllerTest::DEVICE_GENERATION = 2;
8902const int32_t PeripheralControllerTest::DEVICE_CONTROLLER_NUMBER = 0;
8903const Flags<InputDeviceClass> PeripheralControllerTest::DEVICE_CLASSES =
Chris Yee2b1e5c2021-03-10 22:45:12 -08008904 Flags<InputDeviceClass>(0); // not needed for current tests
Chris Ye1dd2e5c2021-04-04 23:12:41 -07008905const int32_t PeripheralControllerTest::EVENTHUB_ID = 1;
Chris Yee2b1e5c2021-03-10 22:45:12 -08008906
8907// --- BatteryControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -07008908class BatteryControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -08008909protected:
8910 void SetUp() override {
Chris Ye1dd2e5c2021-04-04 23:12:41 -07008911 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::BATTERY);
Chris Yee2b1e5c2021-03-10 22:45:12 -08008912 }
8913};
8914
8915TEST_F(BatteryControllerTest, GetBatteryCapacity) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -07008916 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08008917
8918 ASSERT_TRUE(controller.getBatteryCapacity(DEFAULT_BATTERY));
8919 ASSERT_EQ(controller.getBatteryCapacity(DEFAULT_BATTERY).value_or(-1), BATTERY_CAPACITY);
8920}
8921
8922TEST_F(BatteryControllerTest, GetBatteryStatus) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -07008923 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08008924
8925 ASSERT_TRUE(controller.getBatteryStatus(DEFAULT_BATTERY));
8926 ASSERT_EQ(controller.getBatteryStatus(DEFAULT_BATTERY).value_or(-1), BATTERY_STATUS);
8927}
8928
8929// --- LightControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -07008930class LightControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -08008931protected:
Chris Ye1dd2e5c2021-04-04 23:12:41 -07008932 void SetUp() override {
8933 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::LIGHT);
8934 }
Chris Yee2b1e5c2021-03-10 22:45:12 -08008935};
8936
Chris Ye85758332021-05-16 23:05:17 -07008937TEST_F(LightControllerTest, MonoLight) {
8938 RawLightInfo infoMono = {.id = 1,
8939 .name = "Mono",
8940 .maxBrightness = 255,
8941 .flags = InputLightClass::BRIGHTNESS,
8942 .path = ""};
8943 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
Chris Yee2b1e5c2021-03-10 22:45:12 -08008944
Chris Ye1dd2e5c2021-04-04 23:12:41 -07008945 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08008946 InputDeviceInfo info;
8947 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00008948 std::vector<InputDeviceLightInfo> lights = info.getLights();
8949 ASSERT_EQ(1U, lights.size());
8950 ASSERT_EQ(InputDeviceLightType::MONO, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -08008951
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00008952 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
8953 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
Chris Yee2b1e5c2021-03-10 22:45:12 -08008954}
8955
8956TEST_F(LightControllerTest, RGBLight) {
8957 RawLightInfo infoRed = {.id = 1,
8958 .name = "red",
8959 .maxBrightness = 255,
8960 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
8961 .path = ""};
8962 RawLightInfo infoGreen = {.id = 2,
8963 .name = "green",
8964 .maxBrightness = 255,
8965 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
8966 .path = ""};
8967 RawLightInfo infoBlue = {.id = 3,
8968 .name = "blue",
8969 .maxBrightness = 255,
8970 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
8971 .path = ""};
8972 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
8973 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
8974 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
8975
Chris Ye1dd2e5c2021-04-04 23:12:41 -07008976 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08008977 InputDeviceInfo info;
8978 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00008979 std::vector<InputDeviceLightInfo> lights = info.getLights();
8980 ASSERT_EQ(1U, lights.size());
8981 ASSERT_EQ(InputDeviceLightType::RGB, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -08008982
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00008983 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
8984 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -08008985}
8986
8987TEST_F(LightControllerTest, MultiColorRGBLight) {
8988 RawLightInfo infoColor = {.id = 1,
8989 .name = "red",
8990 .maxBrightness = 255,
8991 .flags = InputLightClass::BRIGHTNESS |
8992 InputLightClass::MULTI_INTENSITY |
8993 InputLightClass::MULTI_INDEX,
8994 .path = ""};
8995
8996 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
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::MULTI_COLOR, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009004
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009005 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
9006 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009007}
9008
9009TEST_F(LightControllerTest, PlayerIdLight) {
9010 RawLightInfo info1 = {.id = 1,
9011 .name = "player1",
9012 .maxBrightness = 255,
9013 .flags = InputLightClass::BRIGHTNESS,
9014 .path = ""};
9015 RawLightInfo info2 = {.id = 2,
9016 .name = "player2",
9017 .maxBrightness = 255,
9018 .flags = InputLightClass::BRIGHTNESS,
9019 .path = ""};
9020 RawLightInfo info3 = {.id = 3,
9021 .name = "player3",
9022 .maxBrightness = 255,
9023 .flags = InputLightClass::BRIGHTNESS,
9024 .path = ""};
9025 RawLightInfo info4 = {.id = 4,
9026 .name = "player4",
9027 .maxBrightness = 255,
9028 .flags = InputLightClass::BRIGHTNESS,
9029 .path = ""};
9030 mFakeEventHub->addRawLightInfo(info1.id, std::move(info1));
9031 mFakeEventHub->addRawLightInfo(info2.id, std::move(info2));
9032 mFakeEventHub->addRawLightInfo(info3.id, std::move(info3));
9033 mFakeEventHub->addRawLightInfo(info4.id, std::move(info4));
9034
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009035 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009036 InputDeviceInfo info;
9037 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009038 std::vector<InputDeviceLightInfo> lights = info.getLights();
9039 ASSERT_EQ(1U, lights.size());
9040 ASSERT_EQ(InputDeviceLightType::PLAYER_ID, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009041
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009042 ASSERT_FALSE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
9043 ASSERT_TRUE(controller.setLightPlayerId(lights[0].id, LIGHT_PLAYER_ID));
9044 ASSERT_EQ(controller.getLightPlayerId(lights[0].id).value_or(-1), LIGHT_PLAYER_ID);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009045}
9046
Michael Wrightd02c5b62014-02-10 15:10:22 -08009047} // namespace android